|
| 1 | +import {expect} from 'chai'; |
| 2 | +import {utils, Wallet} from 'ethers'; |
| 3 | +import {MockProvider} from '../src/MockProvider'; |
| 4 | + |
| 5 | +describe('MockProvider - Ganache Wallets', async () => { |
| 6 | + const assertWalletsWithBalances = async (provider: MockProvider, wallets: Wallet[]) => { |
| 7 | + for (const wallet of wallets) { |
| 8 | + const balance = await wallet.getBalance(); |
| 9 | + expect(balance.gt(0)).to.equal(true); |
| 10 | + expect(wallet.provider).to.equal(provider); |
| 11 | + } |
| 12 | + }; |
| 13 | + |
| 14 | + it('returns default wallets', async () => { |
| 15 | + const provider = new MockProvider(); |
| 16 | + const wallets = provider.getWallets(); |
| 17 | + expect(wallets.length).to.equal(10); |
| 18 | + await assertWalletsWithBalances(provider, wallets); |
| 19 | + }); |
| 20 | + |
| 21 | + it('accepts override of accounts', () => { |
| 22 | + const original = Wallet.createRandom(); |
| 23 | + const provider = new MockProvider({ |
| 24 | + ganacheOptions: { |
| 25 | + wallet: { |
| 26 | + accounts: [{balance: '0x64', secretKey: original.privateKey}] |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + const wallets = provider.getWallets(); |
| 31 | + expect(wallets.length).to.equal(1); |
| 32 | + expect(wallets[0].address).to.equal(original.address); |
| 33 | + }); |
| 34 | + |
| 35 | + it('Can generate a different number of accounts', async () => { |
| 36 | + const provider = new MockProvider({ |
| 37 | + ganacheOptions: { |
| 38 | + wallet: { |
| 39 | + totalAccounts: 25 |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + const wallets = provider.getWallets(); |
| 44 | + expect(wallets.length).to.equal(25); |
| 45 | + await assertWalletsWithBalances(provider, wallets); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Can generate accounts based on a seed', async () => { |
| 49 | + const mnemonic = Wallet.createRandom().mnemonic; |
| 50 | + const provider = new MockProvider({ |
| 51 | + ganacheOptions: { |
| 52 | + wallet: { |
| 53 | + totalAccounts: 25, |
| 54 | + mnemonic: mnemonic.phrase |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + const wallets = provider.getWallets(); |
| 59 | + expect(wallets.length).to.equal(25); |
| 60 | + await assertWalletsWithBalances(provider, wallets); |
| 61 | + |
| 62 | + const defaultProvider = new MockProvider(); |
| 63 | + expect(defaultProvider.getWallets()[0].address).to.not.be.eq(wallets[0].address); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Can generate wallets with non-default balance', async () => { |
| 67 | + const provider = new MockProvider({ |
| 68 | + ganacheOptions: { |
| 69 | + wallet: { |
| 70 | + totalAccounts: 25, |
| 71 | + defaultBalance: 101 |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + const wallets = provider.getWallets(); |
| 76 | + expect(wallets.length).to.equal(25); |
| 77 | + await assertWalletsWithBalances(provider, wallets); |
| 78 | + expect((await wallets[0].getBalance()).toString()).to.eq(utils.parseEther('101').toString()); |
| 79 | + }); |
| 80 | +}); |
0 commit comments