UNPKG

@ethereum-waffle/chai

Version:

A sweet set of chai matchers for your blockchain testing needs.

72 lines 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBalanceChange = exports.supportChangeEtherBalance = void 0; const ethers_1 = require("ethers"); const call_promise_1 = require("../call-promise"); const utils_1 = require("./calledOnContract/utils"); const account_1 = require("./misc/account"); function supportChangeEtherBalance(Assertion) { Assertion.addMethod('changeEtherBalance', function (account, balanceChange, options) { (0, call_promise_1.callPromise)(this); const isNegated = this.__flags.negate === true; const derivedPromise = this.callPromise.then(() => { if (!('txResponse' in this)) { throw new Error('The changeEtherBalance matcher must be called on a transaction'); } if (typeof account === 'string') { throw new Error('A string address cannot be used as an account in changeEtherBalance.' + ' Expecting an instance of Ethers Account.'); } return Promise.all([ getBalanceChange(this.txResponse, account, options), (0, account_1.getAddressOf)(account) ]); }).then(([actualChange, address]) => { const isCurrentlyNegated = this.__flags.negate === true; this.__flags.negate = isNegated; const margin = (options === null || options === void 0 ? void 0 : options.errorMargin) ? options.errorMargin : '0'; if (ethers_1.BigNumber.from(margin).eq(0)) { this.assert(actualChange.eq(ethers_1.BigNumber.from(balanceChange)), `Expected "${address}" to change balance by ${balanceChange} wei, ` + `but it has changed by ${actualChange} wei`, `Expected "${address}" to not change balance by ${balanceChange} wei,`, balanceChange, actualChange); } else { const low = ethers_1.BigNumber.from(balanceChange).sub(margin); const high = ethers_1.BigNumber.from(balanceChange).add(margin); this.assert(actualChange.lte(high) && actualChange.gte(low), `Expected "${address}" balance to change within [${[low, high]}] wei, ` + `but it has changed by ${actualChange} wei`, `Expected "${address}" balance to not change within [${[low, high]}] wei`, balanceChange, actualChange); } this.__flags.negate = isCurrentlyNegated; }); this.then = derivedPromise.then.bind(derivedPromise); this.catch = derivedPromise.catch.bind(derivedPromise); this.callPromise = derivedPromise; return this; }); } exports.supportChangeEtherBalance = supportChangeEtherBalance; async function getBalanceChange(txResponse, account, options) { var _a; (0, utils_1.ensure)(account.provider !== undefined, TypeError, 'Provider not found'); const txReceipt = await txResponse.wait(); const txBlockNumber = txReceipt.blockNumber; const address = await (0, account_1.getAddressOf)(account); const balanceAfter = await account.provider.getBalance(address, txBlockNumber); const balanceBefore = await account.provider.getBalance(address, txBlockNumber - 1); if ((options === null || options === void 0 ? void 0 : options.includeFee) !== true && address === txReceipt.from) { const gasPrice = (_a = txResponse.gasPrice) !== null && _a !== void 0 ? _a : txReceipt.effectiveGasPrice; const gasUsed = txReceipt.gasUsed; const txFee = gasPrice.mul(gasUsed); const provider = account.provider; if (typeof provider.getL1Fee === 'function') { const l1Fee = await provider.getL1Fee(txReceipt.transactionHash); return balanceAfter.add(txFee).add(l1Fee).sub(balanceBefore); } return balanceAfter.add(txFee).sub(balanceBefore); } else { return balanceAfter.sub(balanceBefore); } } exports.getBalanceChange = getBalanceChange; //# sourceMappingURL=changeEtherBalance.js.map