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 MerkleProof tests among other testing utilities #4689

Merged
merged 18 commits into from
Oct 23, 2023
157 changes: 68 additions & 89 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@nomiclabs/hardhat-truffle5": "^2.0.5",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/docs-utils": "^0.1.5",
"@openzeppelin/merkle-tree": "^1.0.5",
"@openzeppelin/test-helpers": "^0.5.13",
"@openzeppelin/upgrade-safe-transpiler": "^0.3.32",
"@openzeppelin/upgrades-core": "^1.20.6",
Expand All @@ -78,10 +79,8 @@
"hardhat-exposed": "^0.3.13",
"hardhat-gas-reporter": "^1.0.9",
"hardhat-ignore-warnings": "^0.2.0",
"keccak256": "^1.0.2",
"lodash.startcase": "^4.4.0",
"lodash.zip": "^4.2.0",
"merkletreejs": "^0.2.13",
"micromatch": "^4.0.2",
"p-limit": "^3.1.0",
"prettier": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions test/governance/extensions/GovernorTimelockCompound.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { ethers } = require('ethers');
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const Enums = require('../../helpers/enums');
const { GovernorHelper, proposalStatesToBitMap } = require('../../helpers/governance');
const { expectRevertCustomError } = require('../../helpers/customError');
const { computeCreateAddress } = require('../../helpers/create');
const { clockFromReceipt } = require('../../helpers/time');

const Timelock = artifacts.require('CompTimelock');
Expand Down Expand Up @@ -41,7 +41,7 @@ contract('GovernorTimelockCompound', function (accounts) {

// Need to predict governance address to set it as timelock admin with a delayed transfer
const nonce = await web3.eth.getTransactionCount(deployer);
const predictGovernor = computeCreateAddress(deployer, nonce + 1);
const predictGovernor = ethers.getCreateAddress({ from: deployer, nonce: nonce + 1 });

this.timelock = await Timelock.new(predictGovernor, defaultDelay);
this.mock = await Governor.new(
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/account.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { web3 } = require('hardhat');
const { ethers } = require('hardhat');
const { impersonateAccount, setBalance } = require('@nomicfoundation/hardhat-network-helpers');

// Hardhat default balance
const DEFAULT_BALANCE = web3.utils.toBN('10000000000000000000000');
const DEFAULT_BALANCE = 10000n * ethers.WeiPerEther;

async function impersonate(account, balance = DEFAULT_BALANCE) {
await impersonateAccount(account);
Expand Down
12 changes: 3 additions & 9 deletions test/helpers/chainid.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const hre = require('hardhat');

async function getChainId() {
const chainIdHex = await hre.network.provider.send('eth_chainId', []);
return new hre.web3.utils.BN(chainIdHex, 'hex');
}
const { ethers } = require('hardhat');

module.exports = {
getChainId,
// TODO: when tests are ready to support bigint chainId
// getChainId: ethers.provider.getNetwork().then(network => network.chainId),
// TODO: remove conversion toNumber() when bigint are supported
getChainId: () => ethers.provider.getNetwork().then(network => ethers.toNumber(network.chainId)),
};
8 changes: 3 additions & 5 deletions test/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const MAX_UINT48 = web3.utils.toBN(1).shln(48).subn(1).toString();
const MAX_UINT64 = web3.utils.toBN(1).shln(64).subn(1).toString();

// TODO: remove toString() when bigint are supported
module.exports = {
MAX_UINT48,
MAX_UINT64,
MAX_UINT48: (2n ** 48n - 1n).toString(),
MAX_UINT64: (2n ** 64n - 1n).toString(),
};
6 changes: 0 additions & 6 deletions test/helpers/create.js

This file was deleted.

33 changes: 13 additions & 20 deletions test/helpers/eip712.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const ethSigUtil = require('eth-sig-util');
const keccak256 = require('keccak256');
const { ethers } = require('ethers');

const EIP712Domain = [
{ name: 'name', type: 'string' },
Expand All @@ -17,22 +16,22 @@ const Permit = [
{ name: 'deadline', type: 'uint256' },
];

function bufferToHexString(buffer) {
return '0x' + buffer.toString('hex');
}

function hexStringToBuffer(hexstr) {
return Buffer.from(hexstr.replace(/^0x/, ''), 'hex');
}

async function getDomain(contract) {
const { fields, name, version, chainId, verifyingContract, salt, extensions } = await contract.eip712Domain();

if (extensions.length > 0) {
throw Error('Extensions not implemented');
}

const domain = { name, version, chainId, verifyingContract, salt };
const domain = {
name,
version,
// TODO: remove check when contracts are all migrated to ethers
chainId: web3.utils.isBN(chainId) ? chainId.toNumber() : chainId,
verifyingContract,
salt,
};

for (const [i, { name }] of EIP712Domain.entries()) {
if (!(fields & (1 << i))) {
delete domain[name];
Expand All @@ -46,22 +45,16 @@ function domainType(domain) {
return EIP712Domain.filter(({ name }) => domain[name] !== undefined);
}

function domainSeparator(domain) {
return bufferToHexString(
ethSigUtil.TypedDataUtils.hashStruct('EIP712Domain', domain, { EIP712Domain: domainType(domain) }),
);
}

function hashTypedData(domain, structHash) {
return bufferToHexString(
keccak256(Buffer.concat(['0x1901', domainSeparator(domain), structHash].map(str => hexStringToBuffer(str)))),
return ethers.keccak256(
Buffer.concat(['0x1901', ethers.TypedDataEncoder.hashDomain(domain), structHash].map(ethers.toBeArray)),
);
}

module.exports = {
Permit,
getDomain,
domainType,
domainSeparator,
domainSeparator: ethers.TypedDataEncoder.hashDomain,
hashTypedData,
};
Loading