To transfer a specific amount to another address, use the wallet.transfer()
method.
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);
You can list the assets added in the wallet by using the assets()
method.
1const assets = await fuel.assets();
2console.log("Assets ", assets);
To add custom assets, use the useAssets()
method to add a single asset, or the addAssets()
method to add multiple assets.
1console.log("Add Assets", assets);
2await fuel.addAssets(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.
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}
You can keep track of the assets in the users wallet by using the hook useAssets()
.
1const { assets } = useAssets();
You can keep track of the assets in the users wallet by using the hook useAddAssets()
.
1const { addAssets, isPending, error } = useAddAssets();
2
3async function handleAddAssets(assets: Asset[]) {
4 console.log("Add Assets", assets);
5 addAssets(assets);
6}