Transferring assets
With wallet.transfer
you can initiate a transaction to transfer an asset from your wallet to a target address.
use fuels::prelude::*;
// Setup 2 test wallets with 1 coin each
let num_wallets = Some(2);
let coins_per_wallet = Some(1);
let coin_amount = Some(1);
let wallets = launch_custom_provider_and_get_wallets(
WalletsConfig::new(num_wallets, coins_per_wallet, coin_amount),
None,
)
.await;
// Transfer the base asset with amount 1 from wallet 1 to wallet 2
let asset_id = Default::default();
let _receipts = wallets[0]
.transfer(wallets[1].address(), 1, asset_id, TxParameters::default())
.await?;
let wallet_2_final_coins = wallets[1].get_coins(BASE_ASSET_ID).await?;
// Check that wallet 2 now has 2 coins
assert_eq!(wallet_2_final_coins.len(), 2);
You can also transfer assets to a contract via wallet.force_transfer_to_contract
.
// Check the current balance of the contract with id 'contract_id'
let contract_coins = wallet
.get_provider()?
.get_contract_balances(&contract_id)
.await?;
assert!(contract_coins.is_empty());
// Transfer an amount of 100 of the default asset to the contract
let amount = 100;
let asset_id = Default::default();
let _receipts = wallet
.force_transfer_to_contract(&contract_id, amount, asset_id, TxParameters::default())
.await?;
// Check that the contract now has 1 coin
let contract_coins = wallet
.get_provider()?
.get_contract_balances(&contract_id)
.await?;
assert_eq!(contract_coins.len(), 1);