UNPKG

@ethereum-waffle/provider

Version:

A mock provider for your blockchain testing needs.

71 lines 2.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockProvider = void 0; const ethers_1 = require("ethers"); const CallHistory_1 = require("./CallHistory"); const defaultAccounts_1 = require("./defaultAccounts"); const ens_1 = require("@ethereum-waffle/ens"); const revertString_1 = require("./revertString"); class MockProvider extends ethers_1.providers.Web3Provider { constructor(options) { const mergedOptions = { wallet: { accounts: defaultAccounts_1.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_1.CallHistory(); const patchedProvider = (0, revertString_1.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 ethers_1.Wallet(x.secretKey, this)); } createEmptyWallet() { return ethers_1.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 (0, ens_1.deployENS)(wallet); this.network.ensAddress = ens.ens.address; this._ens = ens; } } exports.MockProvider = MockProvider; //# sourceMappingURL=MockProvider.js.map