Skip to content

Commit

Permalink
πŸ‘©πŸ»β€πŸ³ MockProvider: Use actual wallets from ganache in getWallets (#770
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rzadp authored Jul 29, 2022
1 parent 1fe07bb commit 1bc6cd0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-feet-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ethereum-waffle/provider": patch
---

πŸ‘©πŸ»β€πŸ³ MockProvider: Use actual wallets from ganache in `getWallets`
6 changes: 3 additions & 3 deletions waffle-provider/src/MockProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {providers, Wallet} from 'ethers';
import {CallHistory, RecordedCall} from './CallHistory';
import {defaultAccounts} from './defaultAccounts';
import type {Provider} from 'ganache';
import type {EthereumProvider, Provider} from 'ganache';
import type {EthereumProviderOptions} from '@ganache/ethereum-options';

import {deployENS, ENS} from '@ethereum-waffle/ens';
Expand Down Expand Up @@ -54,8 +54,8 @@ export class MockProvider extends providers.Web3Provider {
}

getWallets() {
const items = this.options?.ganacheOptions.wallet?.accounts ?? defaultAccounts;
return items.map((x: any) => new Wallet(x.secretKey, this));
const accounts = (this.provider as unknown as EthereumProvider).getInitialAccounts();
return Object.values(accounts).map((x: any) => new Wallet(x.secretKey, this));
}

createEmptyWallet() {
Expand Down
17 changes: 1 addition & 16 deletions waffle-provider/test/MockProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {expect} from 'chai';
import {BigNumber, constants, utils, Wallet} from 'ethers';
import {MockProvider} from '../src/MockProvider';
import {BigNumber, constants, utils} from 'ethers';
import {deployToken} from './BasicToken';
import {describeMockProviderCases} from './MockProviderCases';

Expand All @@ -15,20 +14,6 @@ describeMockProviderCases('INTEGRATION: MockProvider', (provider) => {
}
});

it('accepts options', () => {
const original = Wallet.createRandom();
const provider = new MockProvider({
ganacheOptions: {
wallet: {
accounts: [{balance: '0x64', secretKey: original.privateKey}]
}
}
});
const wallets = provider.getWallets();
expect(wallets.length).to.equal(1);
expect(wallets[0].address).to.equal(original.address);
});

it('can send simple transactions', async () => {
const [sender] = provider.getWallets();
const recipient = provider.createEmptyWallet();
Expand Down
80 changes: 80 additions & 0 deletions waffle-provider/test/MockProviderWallets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {expect} from 'chai';
import {utils, Wallet} from 'ethers';
import {MockProvider} from '../src/MockProvider';

describe('MockProvider - Ganache Wallets', async () => {
const assertWalletsWithBalances = async (provider: MockProvider, wallets: Wallet[]) => {
for (const wallet of wallets) {
const balance = await wallet.getBalance();
expect(balance.gt(0)).to.equal(true);
expect(wallet.provider).to.equal(provider);
}
};

it('returns default wallets', async () => {
const provider = new MockProvider();
const wallets = provider.getWallets();
expect(wallets.length).to.equal(10);
await assertWalletsWithBalances(provider, wallets);
});

it('accepts override of accounts', () => {
const original = Wallet.createRandom();
const provider = new MockProvider({
ganacheOptions: {
wallet: {
accounts: [{balance: '0x64', secretKey: original.privateKey}]
}
}
});
const wallets = provider.getWallets();
expect(wallets.length).to.equal(1);
expect(wallets[0].address).to.equal(original.address);
});

it('Can generate a different number of accounts', async () => {
const provider = new MockProvider({
ganacheOptions: {
wallet: {
totalAccounts: 25
}
}
});
const wallets = provider.getWallets();
expect(wallets.length).to.equal(25);
await assertWalletsWithBalances(provider, wallets);
});

it('Can generate accounts based on a seed', async () => {
const mnemonic = Wallet.createRandom().mnemonic;
const provider = new MockProvider({
ganacheOptions: {
wallet: {
totalAccounts: 25,
mnemonic: mnemonic.phrase
}
}
});
const wallets = provider.getWallets();
expect(wallets.length).to.equal(25);
await assertWalletsWithBalances(provider, wallets);

const defaultProvider = new MockProvider();
expect(defaultProvider.getWallets()[0].address).to.not.be.eq(wallets[0].address);
});

it('Can generate wallets with non-default balance', async () => {
const provider = new MockProvider({
ganacheOptions: {
wallet: {
totalAccounts: 25,
defaultBalance: 101
}
}
});
const wallets = provider.getWallets();
expect(wallets.length).to.equal(25);
await assertWalletsWithBalances(provider, wallets);
expect((await wallets[0].getBalance()).toString()).to.eq(utils.parseEther('101').toString());
});
});

0 comments on commit 1bc6cd0

Please sign in to comment.