@ethereum-waffle/chai
Version:
A sweet set of chai matchers for your blockchain testing needs.
78 lines • 4.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBalanceChanges = exports.supportChangeEtherBalances = void 0;
const ethers_1 = require("ethers");
const call_promise_1 = require("../call-promise");
const account_1 = require("./misc/account");
const balance_1 = require("./misc/balance");
function supportChangeEtherBalances(Assertion) {
Assertion.addMethod('changeEtherBalances', function (accounts, balanceChanges, 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 changeEtherBalances matcher must be called on a transaction');
}
if (accounts.some(account => typeof account === 'string')) {
throw new Error('A string address cannot be used as an account in changeEtherBalances.' +
' Expecting an instance of Ethers Account.');
}
return Promise.all([
getBalanceChanges(this.txResponse, accounts, options),
(0, balance_1.getAddresses)(accounts)
]);
}).then(([actualChanges, accountAddresses]) => {
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(actualChanges.every((change, ind) => change.lte(ethers_1.BigNumber.from(balanceChanges[ind]).add(margin)) &&
change.gte(ethers_1.BigNumber.from(balanceChanges[ind]).sub(margin))), `Expected ${accountAddresses} to change balance by ${balanceChanges} wei, ` +
`but it has changed by ${actualChanges} wei`, `Expected ${accountAddresses} to not change balance by ${balanceChanges} wei,`, balanceChanges.map((balanceChange) => balanceChange.toString()), actualChanges.map((actualChange) => actualChange.toString()));
}
else {
actualChanges.forEach((change, ind) => {
const low = ethers_1.BigNumber.from(balanceChanges[ind]).sub(margin);
const high = ethers_1.BigNumber.from(balanceChanges[ind]).add(margin);
this.assert(change.lte(high) &&
change.gte(low), `Expected "${accountAddresses[ind]}" balance to change within [${[low, high]}] wei, ` +
`but it has changed by ${change} wei`, `Expected "${accountAddresses[ind]}" balance to not change within [${[low, high]}] wei`, balanceChanges[ind], change);
});
}
this.__flags.negate = isCurrentlyNegated;
});
this.then = derivedPromise.then.bind(derivedPromise);
this.catch = derivedPromise.catch.bind(derivedPromise);
this.callPromise = derivedPromise;
return this;
});
}
exports.supportChangeEtherBalances = supportChangeEtherBalances;
async function getBalanceChanges(txResponse, accounts, options) {
const txReceipt = await txResponse.wait();
const txBlockNumber = txReceipt.blockNumber;
const balancesAfter = await (0, balance_1.getBalances)(accounts, txBlockNumber);
const balancesBefore = await (0, balance_1.getBalances)(accounts, txBlockNumber - 1);
const txFees = await getTxFees(accounts, txResponse, options);
return balancesAfter.map((balance, ind) => balance.add(txFees[ind]).sub(balancesBefore[ind]));
}
exports.getBalanceChanges = getBalanceChanges;
async function getTxFees(accounts, txResponse, options) {
return Promise.all(accounts.map(async (account) => {
var _a;
if ((options === null || options === void 0 ? void 0 : options.includeFee) !== true && await (0, account_1.getAddressOf)(account) === txResponse.from) {
const txReceipt = await txResponse.wait();
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 txFee.add(l1Fee);
}
return txFee;
}
return 0;
}));
}
//# sourceMappingURL=changeEtherBalances.js.map