本指南演示了如何在帐户和合约之间转移资产,以及在转账之前如何验证您的余额。
在 SDK 中,在钱包之间转移资产非常简单。
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const recipient = Wallet.generate({ provider });
const txResponse = await myWallet.transfer(recipient.address, 100, baseAssetId);
await txResponse.waitForResult();
等待交易处理完成后,资产将成功转移到接收者的钱包中。
您还可以将接收者的地址指定为字符串:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const address = 'fuel1zc7r2rwuzl3uskfc0w737780uqd8sn6lfm3wgqf9wa767gs3sems5d6kxj';
const txResponse = await myWallet.transfer(address, 100, baseAssetId);
当转移基本链币种如 ETH 时,您可以省略 assetId
:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const recipient = Wallet.generate({ provider });
const txResponse = await myWallet.transfer(recipient.address, 100);
要将资产转移至多个钱包,使用 Account.batchTransfer
方法:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const recipient1 = Wallet.generate({ provider });
const recipient2 = Wallet.generate({ provider });
const transfersToMake: TransferParams[] = [
{ amount: 100, destination: recipient1.address, assetId: baseAssetId },
{ amount: 200, destination: recipient2.address, assetId: baseAssetId },
{ amount: 300, destination: recipient2.address, assetId: someOtherAssetId },
];
const tx = await myWallet.batchTransfer(transfersToMake);
const { isStatusSuccess } = await tx.waitForResult();
从您的钱包向部署的合约转移资产非常简单。您只需要合约的地址。
您可以通过使用其 id
将资产转移到已部署的合约实例:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const txResponse = await myWallet.transferToContract(contract.id, 100, baseAssetId);
await txResponse.waitForResult();
或者,您可以简单地使用合约的字符串地址,格式为 Bech32
:
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const contractAddress = contract.id.toString();
const txResponse = await myWallet.transferToContract(contractAddress, 100, baseAssetId);
await txResponse.waitForResult();
在转移资产之前,请确保您的钱包有足够的资金。如果资金不足,则尝试转账将导致错误:not enough coins to fit the target
。
您可以查看如何检查您的余额在 checking-balances
页面。