Skip to content

Setting up test wallets

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

Create a single wallet

ts
import type { WalletLocked, WalletUnlocked } from 'fuels';
import { Wallet } from 'fuels';

// 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);
See code in context

Setting up multiple test wallets

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

ts
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import type { CoinQuantity, WalletUnlocked } from 'fuels';
import { FUEL_NETWORK_URL, Provider } from 'fuels';
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: baseAssetId, amount: bn(30) },
  { assetId: assetIdA, amount: bn(100) },
  { assetId: assetIdB, amount: bn(200) },
]);
expect(walletCBalances).toEqual([]);
See code in context