This guide demonstrates how to transfer assets between accounts and contracts, and how to validate your balance prior to a transfer.
Transferring assets between wallets is really simple within the SDK.
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const recipient = Wallet.generate({ provider });
const txResponse = await myWallet.transfer(recipient.address, 100, baseAssetId);
await txResponse.waitForResult();
After waiting the transaction to be processed, the assets are successfully moved to the recipient's wallet.
It is also possible to specify the recipient's address as a string:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const address = 'fuel1zc7r2rwuzl3uskfc0w737780uqd8sn6lfm3wgqf9wa767gs3sems5d6kxj';
const txResponse = await myWallet.transfer(address, 100, baseAssetId);
When transferring the base chain coin like ETH, you can omit the assetId
:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const recipient = Wallet.generate({ provider });
const txResponse = await myWallet.transfer(recipient.address, 100);
Transferring assets from your wallet to a deployed contract is straightforward. All you need is the contract's address.
You can transfer assets to a deployed contract instance by using its id
:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const txResponse = await myWallet.transferToContract(contract.id, 100, baseAssetId);
await txResponse.waitForResult();
Alternatively, you can simply use the contract's string address in the Bech32
format:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const contractAddress = contract.id.toString();
const txResponse = await myWallet.transferToContract(contractAddress, 100, baseAssetId);
await txResponse.waitForResult();
Before transferring assets, ensure your wallet has sufficient funds. Attempting a transfer without enough funds will result in an error: not enough coins to fit the target
.
You can see how to check your balance at the checking-balances
page.