Migrate MerkleProof tests among other testing utilities (#4689)

This commit is contained in:
Hadrien Croubois
2023-10-23 20:24:46 +02:00
committed by GitHub
parent 149e1b79fe
commit 7c8b7a2728
17 changed files with 257 additions and 322 deletions

View File

@ -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);

View File

@ -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)),
};

View File

@ -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(),
};

View File

@ -1,6 +0,0 @@
const { ethers } = require('hardhat');
module.exports = {
computeCreateAddress: (from, nonce) => ethers.getCreateAddress({ from, nonce }),
computeCreate2Address: (salt, bytecode, from) => ethers.getCreate2Address(from, salt, ethers.keccak256(bytecode)),
};

View File

@ -1,5 +1,4 @@
const ethSigUtil = require('eth-sig-util');
const keccak256 = require('keccak256');
const { ethers } = require('ethers');
const EIP712Domain = [
{ name: 'name', type: 'string' },
@ -17,14 +16,6 @@ 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();
@ -32,7 +23,15 @@ async function getDomain(contract) {
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];
@ -46,15 +45,9 @@ 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)),
);
}
@ -62,6 +55,6 @@ module.exports = {
Permit,
getDomain,
domainType,
domainSeparator,
domainSeparator: ethers.TypedDataEncoder.hashDomain,
hashTypedData,
};

View File

@ -1,3 +1,4 @@
const { ethers } = require('hardhat');
const { getStorageAt, setStorageAt } = require('@nomicfoundation/hardhat-network-helpers');
const ImplementationLabel = 'eip1967.proxy.implementation';
@ -5,29 +6,27 @@ const AdminLabel = 'eip1967.proxy.admin';
const BeaconLabel = 'eip1967.proxy.beacon';
function labelToSlot(label) {
return '0x' + web3.utils.toBN(web3.utils.keccak256(label)).subn(1).toString(16);
return ethers.toBeHex(BigInt(ethers.keccak256(ethers.toUtf8Bytes(label))) - 1n);
}
function getSlot(address, slot) {
return getStorageAt(
web3.utils.isAddress(address) ? address : address.address,
web3.utils.isHex(slot) ? slot : labelToSlot(slot),
ethers.isAddress(address) ? address : address.address,
ethers.isBytesLike(slot) ? slot : labelToSlot(slot),
);
}
function setSlot(address, slot, value) {
const hexValue = web3.utils.isHex(value) ? value : web3.utils.toHex(value);
return setStorageAt(
web3.utils.isAddress(address) ? address : address.address,
web3.utils.isHex(slot) ? slot : labelToSlot(slot),
web3.utils.padLeft(hexValue, 64),
ethers.isAddress(address) ? address : address.address,
ethers.isBytesLike(slot) ? slot : labelToSlot(slot),
value,
);
}
async function getAddressInSlot(address, slot) {
const slotValue = await getSlot(address, slot);
return web3.utils.toChecksumAddress(slotValue.substring(slotValue.length - 40));
return ethers.getAddress(slotValue.substring(slotValue.length - 40));
}
module.exports = {

View File

@ -1,5 +1,5 @@
const { soliditySha3 } = require('web3-utils');
const { ethers } = require('hardhat');
module.exports = {
selector: signature => soliditySha3(signature).substring(0, 10),
selector: signature => ethers.FunctionFragment.from(signature).selector,
};

View File

@ -1,17 +1,18 @@
const ozHelpers = require('@openzeppelin/test-helpers');
const helpers = require('@nomicfoundation/hardhat-network-helpers');
const { time, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers');
module.exports = {
clock: {
blocknumber: () => helpers.time.latestBlock(),
timestamp: () => helpers.time.latest(),
blocknumber: () => time.latestBlock(),
timestamp: () => time.latest(),
},
clockFromReceipt: {
blocknumber: receipt => Promise.resolve(receipt.blockNumber),
timestamp: receipt => web3.eth.getBlock(receipt.blockNumber).then(block => block.timestamp),
// TODO: update for ethers receipt
// timestamp: receipt => receipt.getBlock().then(block => block.timestamp),
},
forward: {
blocknumber: ozHelpers.time.advanceBlockTo,
timestamp: helpers.time.increaseTo,
blocknumber: mineUpTo,
timestamp: time.increaseTo,
},
};