在测试合约时,通常需要创建一个或多个测试钱包。以下是如何执行此操作的方法。
// #import { Wallet, WalletLocked, WalletUnlocked };
// We can use the `generate` to create a new unlocked wallet.
const myWallet: WalletUnlocked = Wallet.generate({ provider });
// or use an Address to create a wallet
const someWallet: WalletLocked = Wallet.fromAddress(myWallet.address, provider);
如果需要多个测试钱包,则可以按照以下步骤设置:
// #import { FUEL_NETWORK_URL, Provider, WalletUnlocked, CoinQuantity, generateTestWallet };
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
const provider = await Provider.create(FUEL_NETWORK_URL);
const baseAssetId = provider.getBaseAssetId();
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
const assetIdB = '0x0202020202020202020202020202020202020202020202020202020202020202';
// single asset
const walletA: WalletUnlocked = await generateTestWallet(provider, [[42, baseAssetId]]);
// multiple assets
const walletB = await generateTestWallet(provider, [
// [Amount, AssetId]
[100, assetIdA],
[200, assetIdB],
[30, baseAssetId],
]);
// empty wallet
const walletC = await generateTestWallet(provider);
// retrieve balances of wallets
const walletABalances: CoinQuantity[] = await walletA.getBalances();
const walletBBalances = await walletB.getBalances();
const walletCBalances = await walletC.getBalances();
expect(walletABalances).toEqual([{ assetId: baseAssetId, amount: bn(42) }]);
expect(walletBBalances).toEqual([
{ assetId: assetIdA, amount: bn(100) },
{ assetId: assetIdB, amount: bn(200) },
{ assetId: baseAssetId, amount: bn(30) },
]);
expect(walletCBalances).toEqual([]);