Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate math tests to ethers.js v6 #4769

Merged
merged 9 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions test/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ function Enum(...options) {
return Object.fromEntries(options.map((key, i) => [key, web3.utils.toBN(i)]));
}

module.exports = {
Enum,
ProposalState: Enum('Pending', 'Active', 'Canceled', 'Defeated', 'Succeeded', 'Queued', 'Expired', 'Executed'),
VoteType: Enum('Against', 'For', 'Abstain'),
Rounding: Enum('Floor', 'Ceil', 'Trunc', 'Expand'),
OperationState: Enum('Unset', 'Waiting', 'Ready', 'Done'),
};
function EnumBigInt(...options) {
return Object.fromEntries(options.map((key, i) => [key, BigInt(i)]));
}

// TODO: remove web3, simplify code
function createExport(Enum) {
return {
Enum,
ProposalState: Enum('Pending', 'Active', 'Canceled', 'Defeated', 'Succeeded', 'Queued', 'Expired', 'Executed'),
VoteType: Enum('Against', 'For', 'Abstain'),
Rounding: Enum('Floor', 'Ceil', 'Trunc', 'Expand'),
OperationState: Enum('Unset', 'Waiting', 'Ready', 'Done'),
};
}

module.exports = createExport(Enum);
module.exports.bigint = createExport(EnumBigInt);
6 changes: 0 additions & 6 deletions test/helpers/iterate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Map values in an object
const mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, fn(v)]));

// Array of number or bigint
const max = (...values) => values.slice(1).reduce((x, y) => (x > y ? x : y), values[0]);
const min = (...values) => values.slice(1).reduce((x, y) => (x < y ? x : y), values[0]);

// Cartesian product of a list of arrays
const product = (...arrays) => arrays.reduce((a, b) => a.flatMap(ai => b.map(bi => [...ai, bi])), [[]]);

module.exports = {
mapValues,
max,
min,
product,
};
21 changes: 11 additions & 10 deletions test/helpers/math.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Array of number or bigint
const max = (...values) => values.slice(1).reduce((x, y) => (x > y ? x : y), values.at(0));
const min = (...values) => values.slice(1).reduce((x, y) => (x < y ? x : y), values.at(0));
const sum = (...values) => values.slice(1).reduce((x, y) => x + y, values.at(0));

module.exports = {
// sum of integer / bignumber
sum: (...args) => args.reduce((acc, n) => acc + n, 0),
bigintSum: (...args) => args.reduce((acc, n) => acc + n, 0n),
BNsum: (...args) => args.reduce((acc, n) => acc.add(n), web3.utils.toBN(0)),
// min of integer / bignumber
min: (...args) => args.slice(1).reduce((x, y) => (x < y ? x : y), args[0]),
BNmin: (...args) => args.slice(1).reduce((x, y) => (x.lt(y) ? x : y), args[0]),
// max of integer / bignumber
max: (...args) => args.slice(1).reduce((x, y) => (x > y ? x : y), args[0]),
BNmax: (...args) => args.slice(1).reduce((x, y) => (x.gt(y) ? x : y), args[0]),
// re-export min, max & sum of integer / bignumber
min,
max,
sum,
// deprecated: BN version of sum
BNsum: (...args) => args.slice(1).reduce((x, y) => x.add(y), args.at(0)),
};
2 changes: 1 addition & 1 deletion test/metatx/ERC2771Forwarder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { getDomain } = require('../helpers/eip712');
const { bigint: time } = require('../helpers/time');
const { bigintSum: sum } = require('../helpers/math');
const { sum } = require('../helpers/math');

async function fixture() {
const [sender, refundReceiver, another, ...accounts] = await ethers.getSigners();
Expand Down
3 changes: 2 additions & 1 deletion test/token/ERC721/extensions/ERC721Consecutive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ contract('ERC721Consecutive', function (accounts) {

it('balance & voting power are set', async function () {
for (const account of accounts) {
const balance = sum(...batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount));
const balance =
sum(...batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount)) ?? 0;

expect(await this.token.balanceOf(account)).to.be.bignumber.equal(web3.utils.toBN(balance));

Expand Down
Loading