Skip to main content

Build a Basic Output

You can build a simple output using the Client.buildBasicOutput(options) function.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create a SecretManager from a mnemonic.
  3. Generate a public address.
  4. Build a simple output.

Code Example

Dotenv

This example uses dotenv, which is not safe to use in production environments.

// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example basic --release

use iota_client::{
block::output::{
feature::MetadataFeature,
unlock_condition::{
AddressUnlockCondition, ExpirationUnlockCondition, StorageDepositReturnUnlockCondition,
TimelockUnlockCondition, UnlockCondition,
},
BasicOutputBuilder, Feature,
},
secret::{mnemonic::MnemonicSecretManager, SecretManager},
utils::request_funds_from_faucet,
Client, Result,
};

/// In this example we will send basic outputs with different feature blocks

#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production!
// Configure your own mnemonic in the ".env" file. Since the output amount cannot be zero, the seed must contain
// non-zero balance.
dotenv::dotenv().ok();

let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

// Create a client instance.
let client = Client::builder().with_node(&node_url)?.finish()?;

let secret_manager = SecretManager::Mnemonic(MnemonicSecretManager::try_from_mnemonic(
&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap(),
)?);

let token_supply = client.get_token_supply().await?;

let address = client.get_addresses(&secret_manager).with_range(0..1).get_raw().await?[0];
println!(
"{}",
request_funds_from_faucet(&faucet_url, &address.to_bech32(client.get_bech32_hrp().await?)).await?
);

let basic_output_builder = BasicOutputBuilder::new_with_amount(1_000_000)?
.add_unlock_condition(UnlockCondition::Address(AddressUnlockCondition::new(address)));

let outputs = vec![
// most simple output
basic_output_builder.clone().finish_output(token_supply)?,
// with metadata feature block
basic_output_builder
.clone()
.add_feature(Feature::Metadata(MetadataFeature::new(vec![13, 37])?))
.finish_output(token_supply)?,
// with storage deposit return
basic_output_builder
.clone()
.with_amount(234_100)?
.add_unlock_condition(UnlockCondition::StorageDepositReturn(
StorageDepositReturnUnlockCondition::new(address, 234_000, token_supply)?,
))
.finish_output(token_supply)?,
// with expiration
basic_output_builder
.clone()
.add_unlock_condition(UnlockCondition::Expiration(ExpirationUnlockCondition::new(address, 1)?))
.finish_output(token_supply)?,
// with timelock
basic_output_builder
.clone()
.add_unlock_condition(UnlockCondition::Timelock(TimelockUnlockCondition::new(1)?))
.finish_output(token_supply)?,
];

let block = client
.block()
.with_secret_manager(&secret_manager)
.with_outputs(outputs)?
.finish()
.await?;

println!("Transaction sent: {node_url}/api/core/v2/blocks/{}", block.id());
println!("Block metadata: {node_url}/api/core/v2/blocks/{}/metadata", block.id());
let _ = client.retry_until_included(&block.id(), None, None).await?;
Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example basic --release

Expected Output

Transaction sent: https://api.testnet.shimmer.network/api/core/v2/blocks/0x7e85b3a62beeedafadbb3dc89a6f5813c34417dfe44fc7a679f3b42b0df3210e
Block metadata: https://api.testnet.shimmer.network/api/core/v2/blocks/0x7e85b3a62beeedafadbb3dc89a6f5813c34417dfe44fc7a679f3b42b0df3210e/metadata