Docs Icon ChevronRight For Developers Icon ChevronRight Assets

Assets

Transferring assets

To transfer a specific amount to another address, use the wallet.transfer() method.

TransferAssets.tsx Icon Link
1// Retrieve the current account address
2const account = await fuel.currentAccount();
3// If the current account is null this means the user has not authorized
4// the currentAccount to the connection.
5if (!account) {
6  throw new Error("Current account not authorized for this connection!");
7}
8// Create a Wallet instance from the current account
9const wallet = await fuel.getWallet(account);
10// Create a Address instance to the receiver address
11const toAddress = Address.fromString(receiverAddress);
12// Get the minGasPrice and maxGasPerTx for the network
13const { minGasPrice } = await wallet.provider.getGasConfig();
14// Send a transaction to transfer the asset to the receiver address
15const response = await wallet.transfer(toAddress, amount, assetId, {
16  gasPrice: minGasPrice,
17  gasLimit: 5_000,
18});
19console.log("Transaction created!", response.id);

List Assets in the Wallet

You can list the assets added in the wallet by using the assets() method.

ListAssets.tsx Icon Link
1const assets = await fuel.assets();
2console.log("Assets ", assets);

Adding custom Assets

To add custom assets, use the useAssets() method to add a single asset, or the addAssets() method to add multiple assets.

AddAssets.tsx Icon Link
1console.log("Add Assets", assets);
2await fuel.addAssets(assets);

With React

Transferring assets

To transfer an amount to other address, use the wallet.transfer() method and pass in the address you want to send to and the amount to send as well as the assetId.

TransferAssetsHook.tsx Icon Link
1const { wallet } = useWallet(); // or useAccount(address);
2
3async function transfer(amount: BN, receiverAddress: string, assetId: string) {
4  if (!wallet) {
5    throw new Error("Wallet not found!");
6  }
7  // Create a Address instance to the receiver address
8  const toAddress = Address.fromString(receiverAddress);
9  // Get the minGasPrice and maxGasPerTx for the network
10  const { minGasPrice } = await wallet.provider.getGasConfig();
11  // Send a transaction to transfer the asset to the receiver address
12  const response = await wallet.transfer(toAddress, bn(amount), assetId, {
13    gasPrice: minGasPrice,
14    gasLimit: 5_000,
15  });
16  console.log("Transaction created!", response.id);
17}

List Assets

You can keep track of the assets in the users wallet by using the hook useAssets().

ListAssetsHook.tsx Icon Link
1const { assets } = useAssets();

Add assets

You can keep track of the assets in the users wallet by using the hook useAddAssets().

AddAssetsHook.tsx Icon Link
1const { addAssets, isLoading, error } = useAddAssets();
2
3async function handleAddAssets(assets: Asset[]) {
4  console.log("Add Assets", assets);
5  addAssets(assets);
6}