Skip to content

Commit

Permalink
Migrate ERC20 extensions tests to ethers v6 (OpenZeppelin#4773)
Browse files Browse the repository at this point in the history
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
  • Loading branch information
3 people authored Dec 13, 2023
1 parent 6ba452d commit 88512b2
Show file tree
Hide file tree
Showing 19 changed files with 1,046 additions and 1,391 deletions.
6 changes: 1 addition & 5 deletions test/governance/Governor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;

const Enums = require('../helpers/enums');
const {
getDomain,
domainType,
types: { Ballot },
} = require('../helpers/eip712');
const { getDomain, domainType, Ballot } = require('../helpers/eip712');
const { GovernorHelper, proposalStatesToBitMap } = require('../helpers/governance');
const { clockFromReceipt } = require('../helpers/time');
const { expectRevertCustomError } = require('../helpers/customError');
Expand Down
6 changes: 1 addition & 5 deletions test/governance/extensions/GovernorWithParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;

const Enums = require('../../helpers/enums');
const {
getDomain,
domainType,
types: { ExtendedBallot },
} = require('../../helpers/eip712');
const { getDomain, domainType, ExtendedBallot } = require('../../helpers/eip712');
const { GovernorHelper } = require('../../helpers/governance');
const { expectRevertCustomError } = require('../../helpers/customError');

Expand Down
6 changes: 1 addition & 5 deletions test/governance/utils/Votes.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;

const { shouldBehaveLikeERC6372 } = require('./ERC6372.behavior');
const {
getDomain,
domainType,
types: { Delegation },
} = require('../../helpers/eip712');
const { getDomain, domainType, Delegation } = require('../../helpers/eip712');
const { clockFromReceipt } = require('../../helpers/time');
const { expectRevertCustomError } = require('../../helpers/customError');

Expand Down
96 changes: 52 additions & 44 deletions test/helpers/eip712-types.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
module.exports = {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
{ name: 'salt', type: 'bytes32' },
],
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
Ballot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'voter', type: 'address' },
{ name: 'nonce', type: 'uint256' },
],
ExtendedBallot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'voter', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'reason', type: 'string' },
{ name: 'params', type: 'bytes' },
],
Delegation: [
{ name: 'delegatee', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' },
],
ForwardRequest: [
{ name: 'from', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'gas', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint48' },
{ name: 'data', type: 'bytes' },
],
};
const { mapValues } = require('./iterate');

const formatType = schema => Object.entries(schema).map(([name, type]) => ({ name, type }));

module.exports = mapValues(
{
EIP712Domain: {
name: 'string',
version: 'string',
chainId: 'uint256',
verifyingContract: 'address',
salt: 'bytes32',
},
Permit: {
owner: 'address',
spender: 'address',
value: 'uint256',
nonce: 'uint256',
deadline: 'uint256',
},
Ballot: {
proposalId: 'uint256',
support: 'uint8',
voter: 'address',
nonce: 'uint256',
},
ExtendedBallot: {
proposalId: 'uint256',
support: 'uint8',
voter: 'address',
nonce: 'uint256',
reason: 'string',
params: 'bytes',
},
Delegation: {
delegatee: 'address',
nonce: 'uint256',
expiry: 'uint256',
},
ForwardRequest: {
from: 'address',
to: 'address',
value: 'uint256',
gas: 'uint256',
nonce: 'uint256',
deadline: 'uint48',
data: 'bytes',
},
},
formatType,
);
module.exports.formatType = formatType;
2 changes: 1 addition & 1 deletion test/helpers/eip712.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function hashTypedData(domain, structHash) {
}

module.exports = {
types,
getDomain,
domainType,
domainSeparator: ethers.TypedDataEncoder.hashDomain,
hashTypedData,
...types,
};
9 changes: 4 additions & 5 deletions test/helpers/time.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { time, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers');

const mapObject = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value)]));
const { mapValues } = require('./iterate');

module.exports = {
clock: {
Expand All @@ -22,8 +21,8 @@ module.exports = {

// TODO: deprecate the old version in favor of this one
module.exports.bigint = {
clock: mapObject(module.exports.clock, fn => () => fn().then(BigInt)),
clockFromReceipt: mapObject(module.exports.clockFromReceipt, fn => receipt => fn(receipt).then(BigInt)),
clock: mapValues(module.exports.clock, fn => () => fn().then(BigInt)),
clockFromReceipt: mapValues(module.exports.clockFromReceipt, fn => receipt => fn(receipt).then(BigInt)),
forward: module.exports.forward,
duration: mapObject(module.exports.duration, fn => n => BigInt(fn(n))),
duration: mapValues(module.exports.duration, fn => n => BigInt(fn(n))),
};
14 changes: 2 additions & 12 deletions test/metatx/ERC2771Context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { impersonate } = require('../helpers/account');
const { getDomain } = require('../helpers/eip712');
const { getDomain, ForwardRequest } = require('../helpers/eip712');
const { MAX_UINT48 } = require('../helpers/constants');

const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior');
Expand All @@ -15,17 +15,7 @@ async function fixture() {
const forwarderAsSigner = await impersonate(forwarder.target);
const context = await ethers.deployContract('ERC2771ContextMock', [forwarder]);
const domain = await getDomain(forwarder);
const types = {
ForwardRequest: [
{ name: 'from', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'gas', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint48' },
{ name: 'data', type: 'bytes' },
],
};
const types = { ForwardRequest };

return { sender, other, forwarder, forwarderAsSigner, context, domain, types };
}
Expand Down
14 changes: 2 additions & 12 deletions test/metatx/ERC2771Forwarder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

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

Expand All @@ -12,17 +12,7 @@ async function fixture() {
const forwarder = await ethers.deployContract('ERC2771Forwarder', ['ERC2771Forwarder']);
const receiver = await ethers.deployContract('CallReceiverMockTrustingForwarder', [forwarder]);
const domain = await getDomain(forwarder);
const types = {
ForwardRequest: [
{ name: 'from', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'gas', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint48' },
{ name: 'data', type: 'bytes' },
],
};
const types = { ForwardRequest };

const forgeRequest = async (override = {}, signer = sender) => {
const req = {
Expand Down
116 changes: 0 additions & 116 deletions test/token/ERC20/extensions/ERC20Burnable.behavior.js

This file was deleted.

Loading

0 comments on commit 88512b2

Please sign in to comment.