To transfer a specific amount to another address, use the wallet.transfer()
method.
// Retrieve the current account address
const account = await fuel.currentAccount();
// If the current account is null this means the user has not authorized
// the currentAccount to the connection.
if (!account) {
throw new Error("Current account not authorized for this connection!");
}
// Create a Wallet instance from the current account
const wallet = await fuel.getWallet(account);
// Create a Address instance to the receiver address
const toAddress = Address.fromString(receiverAddress);
// Get the minGasPrice and maxGasPerTx for the network
const { minGasPrice } = await wallet.provider.getGasConfig();
// Send a transaction to transfer the asset to the receiver address
const response = await wallet.transfer(toAddress, amount, assetId, {
gasPrice: minGasPrice,
gasLimit: 5_000,
});
console.log("Transaction created!", response.id);
You can list the assets added in the wallet by using the assets()
method.
const assets = await fuel.assets();
console.log("Assets ", assets);
To add custom assets, use the useAssets()
method to add a single asset, or the addAssets()
method to add multiple assets.
console.log("Add Assets", assets);
await 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.
const { wallet } = useWallet(); // or useAccount(address);
async function transfer(amount: BN, receiverAddress: string, assetId: string) {
if (!wallet) {
throw new Error("Wallet not found!");
}
// Create a Address instance to the receiver address
const toAddress = Address.fromString(receiverAddress);
// Get the minGasPrice and maxGasPerTx for the network
const { minGasPrice } = await wallet.provider.getGasConfig();
// Send a transaction to transfer the asset to the receiver address
const response = await wallet.transfer(toAddress, bn(amount), assetId, {
gasPrice: minGasPrice,
gasLimit: 5_000,
});
console.log("Transaction created!", response.id);
}
You can keep track of the assets in the users wallet by using the hook useAssets()
.
const { assets } = useAssets();
You can keep track of the assets in the users wallet by using the hook useAddAssets()
.
const { addAssets, isPending, error } = useAddAssets();
async function handleAddAssets(assets: Asset[]) {
console.log("Add Assets", assets);
addAssets(assets);
}