Constructing transfers

Constructing transfers

The SDK provides methods for constructing, signing, and submitting transactions to the chain. These are accessed through the Chain Context object.

Preparation

Let's assume we have two accounts—Alice and Bob—added to the wallet as in the previous section and that Alice's account already contains sufficient funds.

Transparent Transfers

The following code will build a transparent-transfer tx that sends 1 NAM from Alice to Bob, then sign it and submit it to the chain.

use namada_sdk::args::TxTransparentTransferData;
use namada_sdk::args::InputAmount;
use namada_sdk::signing::default_sign;
 
// Prepare the tx arguments
let data = TxTransparentTransferData {
  source: sdk.wallet().await.find_address("alice".to_string()).expect("Invalid alias").into_owned(),
  target: sdk.wallet().await.find_address("bob".to_string()).expect("Invalid alias").into_owned(),
  token: sdk.native_token(),
  amount: InputAmount::from_str("1").expect("Invalid amount"), 
};
 
// Build the tx
let mut transfer_tx_builder = sdk
  .new_transparent_transfer(vec![data])
  .signing_keys(vec![alice_acct.public_key.clone()]);
let (mut transfer_tx, signing_data) = transfer_tx_builder
  .build(&sdk)
  .await
  .expect("unable to build transfer");
 
// Sign the tx
sdk.sign(&mut transfer_tx, &transfer_tx_builder.tx, signing_data, default_sign, ())
  .await
  .expect("unable to sign transparent-transfer tx");
 
// Submit the signed tx to the ledger for execution
match sdk.submit(transfer_tx, &transfer_tx_builder.tx).await {
  Ok(res) => println!("Tx result: {:?}", res),
  Err(e) => println!("Tx error: {:?}", e) 
}