Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Test Wallets

Icon LinkSetting up test wallets

You'll often want to create one or more test wallets when testing your contracts. Here's how to do it.

Icon LinkCreate a single wallet

// #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);

Icon LinkSetting up multiple test wallets

If you need multiple test wallets, they can be set up as follows:

// #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([]);