Skip to main content

Send a Transaction

Wallet.rs

Though it is possible to send transactions with iota.rs, we strongly recommend that you use the official wallet.rs library together with the stronghold.rs enclave for value-based transfers. This combination incorporates the best security practices while dealing with seeds, related addresses, and UTXO.

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 and post a block with an output address and amount to send.
  5. Log the transaction.
Iota.js

You can also find this guide in the native iota.js library

Code Example

Dotenv

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

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

//! In this example we will send a transaction.
//! Run: `cargo run --example transaction --release`.

use iota_client::{
secret::{mnemonic::MnemonicSecretManager, SecretManager},
Client, Result,
};

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

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

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 block = client
.block()
.with_secret_manager(&secret_manager)
// Insert the output address and amount to spent. The amount cannot be zero.
.with_output(
// We generate an address from our own mnemonic so that we send the funds to ourselves
&client.get_addresses(&secret_manager).with_range(1..2).finish().await?[0],
1_000_000,
)
.await?
.finish()
.await?;

println!("{:#?}", block);

println!(
"Transaction sent: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block.id()
);

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example transaction --release

Expected Output

Transaction sent: https://explorer.shimmer.network/testnet/block/0x0e4c43eb9402aad8af70bbabfc4eb43ca4b603feb77d7bbcec43ccd06d9389bb