Send Native Tokens
After you have created a
native token,
you can easily send it by calling the Account.send_native_tokens(params, options)
function.
As with any output, you can set output unlock conditions. Keep in mind that if you set unlock conditions, whoever you send the native tokens to may need to claim them.
Example Code
- Rust
- Typescript (Node.js)
- Python
- Instantiate a
Wallet
, get Alice'sAccount
which was created in the first guide and sync it. - Search the account's balance for a native token with a balance of at least
SEND_NATIVE_TOKEN_AMOUNT
.
- Prepare the outputs for the transaction by creating a new
SendNativeTokensParams
.
- Send the native tokens by calling the
account.send_native_tokens()
function.
- Instantiate a
Wallet
, get Alice'sAccount
which was created in the first guide and sync it. - Search the account's balance for a native token with a balance of at least
SEND_NATIVE_TOKEN_AMOUNT
.
- Prepare the outputs for the transaction by creating a new
SendNativeTokensParams
.
- Create the transaction that will send the tokens using the
Account.sendNativeTokens()
function,
Instantiate a
Wallet
, get Alice'sAccount
which was created in the first guide and sync it.Search the account's balance for a native token with a balance of at least 10.
- Define the outputs for the transaction that will send the tokens by creating the
SendNativeTokensParams
.
- Send the outputs from the previous step using the
Account.send_native_tokens()
function.
Full Example Code
- Rust
- Typescript (Node.js)
- Python
sdk/examples/how_tos/native_tokens/send.rs
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we will send native tokens.
//!
//! Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
//! running the `./how_tos/accounts_and_addresses/create_account.rs` example!
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example send_native_tokens
//! ```
use iota_sdk::{
types::block::address::Bech32Address,
wallet::{Result, SendNativeTokensParams},
Wallet,
};
use primitive_types::U256;
// The native token amount to send
const SEND_NATIVE_TOKEN_AMOUNT: u64 = 10;
// The address to send the tokens to
const RECV_ADDRESS: &str = "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu";
#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();
for var in ["WALLET_DB_PATH", "STRONGHOLD_PASSWORD", "EXPLORER_URL"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
let wallet = Wallet::builder()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
let account = wallet.get_account("Alice").await?;
// May want to ensure the account is synced before sending a transaction.
let balance = account.sync(None).await?;
// Get a token with sufficient balance
if let Some(token_id) = balance
.native_tokens()
.iter()
.find(|t| t.available() >= U256::from(SEND_NATIVE_TOKEN_AMOUNT))
.map(|t| t.token_id())
{
let available_balance = balance
.native_tokens()
.iter()
.find(|t| t.token_id() == token_id)
.unwrap()
.available();
println!("Balance before sending: {available_balance}");
// Set the stronghold password
wallet
.set_stronghold_password(std::env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;
let bech32_address = RECV_ADDRESS.parse::<Bech32Address>()?;
let outputs = [SendNativeTokensParams::new(
bech32_address,
[(*token_id, U256::from(SEND_NATIVE_TOKEN_AMOUNT))],
)?];
let transaction = account.send_native_tokens(outputs, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
// Wait for transaction to get included
let block_id = account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
println!(
"Block included: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block_id
);
let balance = account.sync(None).await?;
let available_balance = balance
.native_tokens()
.iter()
.find(|t| t.token_id() == token_id)
.unwrap()
.available();
println!("Balance after sending: {available_balance}",);
} else {
println!("Insufficient native token funds");
}
Ok(())
}
bindings/nodejs/examples/how_tos/native_tokens/send.ts
loading...
bindings/python/examples/how_tos/native_tokens/send.py
loading...
Expected Output
Balance before sending: 50
Transaction sent: 0x9a1ea93962b5d4ba3a27053f4727dcc95b29ee35dc85c43d9d6be4635ab10463
Block included: https://explorer.shimmer.network/testnet/block/0xb22cba455a3176ef62352f907b0fc5e5e49b4797563826141f8057cfa960a640
Balance after sending: 40