@ethereum-waffle/provider
Version:
A mock provider for your blockchain testing needs.
67 lines • 2.52 kB
JavaScript
import { providers, Wallet } from 'ethers';
import { CallHistory } from './CallHistory';
import { defaultAccounts } from './defaultAccounts';
import { deployENS } from '@ethereum-waffle/ens';
import { injectRevertString } from './revertString';
export class MockProvider extends providers.Web3Provider {
constructor(options) {
const mergedOptions = {
wallet: {
accounts: defaultAccounts
},
logging: { quiet: true },
chain: {
hardfork: 'berlin'
},
...options === null || options === void 0 ? void 0 : options.ganacheOptions
};
const provider = require('ganache').provider(mergedOptions);
const callHistory = new CallHistory();
const patchedProvider = injectRevertString(callHistory.record(provider));
super(patchedProvider);
this.options = options;
this._callHistory = callHistory;
/**
* The override to the provider's formatter allows us to inject
* additional values to a transaction receipt.
* We inject a `revertString` in overriden `eth_getTransactionReceipt` handler.
* Ethers do not bubble up a revert error message when a transaction reverts,
* but it does bubble it up when a call (query) reverts.
* In order to make the revert string accessible for matchers like `revertedWith`,
* we need to simulate transactions as queries and add the revert string to the receipt.
*/
this.formatter.formats = {
...this.formatter.formats,
receipt: {
...this.formatter.formats.receipt,
revertString: (val) => val
}
};
}
getWallets() {
const accounts = this.provider.getInitialAccounts();
return Object.values(accounts).map((x) => new Wallet(x.secretKey, this));
}
createEmptyWallet() {
return Wallet.createRandom().connect(this);
}
clearCallHistory() {
this._callHistory.clear();
}
get callHistory() {
return this._callHistory.getCalls();
}
get ens() {
return this._ens;
}
async setupENS(wallet) {
if (!wallet) {
const wallets = this.getWallets();
wallet = wallets[wallets.length - 1];
}
const ens = await deployENS(wallet);
this.network.ensAddress = ens.ens.address;
this._ens = ens;
}
}
//# sourceMappingURL=MockProvider.js.map