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// Send a transaction to transfer the asset to the receiver address
13const response = await wallet.transfer(toAddress, amount, assetId, {
14  gasLimit: 5_000,
15});
16console.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
3useEffect(() => {
4  Provider.create("http://localhost:4000/v1/graphql").then((provider) => {
5    setAssetId(provider.getBaseAssetId());
6  });
7}, []);
8
9async function transfer(amount: BN, receiverAddress: string, assetId: string) {
10  if (!wallet) {
11    throw new Error("Wallet not found!");
12  }
13  // Create a Address instance to the receiver address
14  const toAddress = Address.fromString(receiverAddress);
15
16  // Send a transaction to transfer the asset to the receiver address
17  const response = await wallet.transfer(toAddress, bn(amount), assetId, {
18    gasLimit: 5_000,
19  });
20  console.log("Transaction created!", response.id);
21}

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, isPending, error } = useAddAssets();
2
3async function handleAddAssets(assets: Asset[]) {
4  console.log("Add Assets", assets);
5  addAssets(assets);
6}