Migrate utils to ethersjs v6 (#4736)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,35 +1,42 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { balance, ether, expectEvent, expectRevert, send } = require('@openzeppelin/test-helpers');
|
||||
const { expectRevertCustomError } = require('../helpers/customError');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const Create2 = artifacts.require('$Create2');
|
||||
const VestingWallet = artifacts.require('VestingWallet');
|
||||
// This should be a contract that:
|
||||
// - has no constructor arguments
|
||||
// - has no immutable variable populated during construction
|
||||
const ConstructorLessContract = Create2;
|
||||
async function fixture() {
|
||||
const [deployer, other] = await ethers.getSigners();
|
||||
|
||||
contract('Create2', function (accounts) {
|
||||
const [deployerAccount, other] = accounts;
|
||||
const factory = await ethers.deployContract('$Create2');
|
||||
|
||||
// Bytecode for deploying a contract that includes a constructor.
|
||||
// We use a vesting wallet, with 3 constructor arguments.
|
||||
const constructorByteCode = await ethers
|
||||
.getContractFactory('VestingWallet')
|
||||
.then(({ bytecode, interface }) => ethers.concat([bytecode, interface.encodeDeploy([other.address, 0n, 0n])]));
|
||||
|
||||
// Bytecode for deploying a contract that has no constructor log.
|
||||
// Here we use the Create2 helper factory.
|
||||
const constructorLessBytecode = await ethers
|
||||
.getContractFactory('$Create2')
|
||||
.then(({ bytecode, interface }) => ethers.concat([bytecode, interface.encodeDeploy([])]));
|
||||
|
||||
return { deployer, other, factory, constructorByteCode, constructorLessBytecode };
|
||||
}
|
||||
|
||||
describe('Create2', function () {
|
||||
const salt = 'salt message';
|
||||
const saltHex = web3.utils.soliditySha3(salt);
|
||||
|
||||
const encodedParams = web3.eth.abi.encodeParameters(['address', 'uint64', 'uint64'], [other, 0, 0]).slice(2);
|
||||
|
||||
const constructorByteCode = `${VestingWallet.bytecode}${encodedParams}`;
|
||||
const saltHex = ethers.id(salt);
|
||||
|
||||
beforeEach(async function () {
|
||||
this.factory = await Create2.new();
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('computeAddress', function () {
|
||||
it('computes the correct contract address', async function () {
|
||||
const onChainComputed = await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
|
||||
const onChainComputed = await this.factory.$computeAddress(saltHex, ethers.keccak256(this.constructorByteCode));
|
||||
const offChainComputed = ethers.getCreate2Address(
|
||||
this.factory.address,
|
||||
this.factory.target,
|
||||
saltHex,
|
||||
ethers.keccak256(constructorByteCode),
|
||||
ethers.keccak256(this.constructorByteCode),
|
||||
);
|
||||
expect(onChainComputed).to.equal(offChainComputed);
|
||||
});
|
||||
@ -37,13 +44,13 @@ contract('Create2', function (accounts) {
|
||||
it('computes the correct contract address with deployer', async function () {
|
||||
const onChainComputed = await this.factory.$computeAddress(
|
||||
saltHex,
|
||||
web3.utils.keccak256(constructorByteCode),
|
||||
deployerAccount,
|
||||
ethers.keccak256(this.constructorByteCode),
|
||||
ethers.Typed.address(this.deployer),
|
||||
);
|
||||
const offChainComputed = ethers.getCreate2Address(
|
||||
deployerAccount,
|
||||
this.deployer.address,
|
||||
saltHex,
|
||||
ethers.keccak256(constructorByteCode),
|
||||
ethers.keccak256(this.constructorByteCode),
|
||||
);
|
||||
expect(onChainComputed).to.equal(offChainComputed);
|
||||
});
|
||||
@ -52,71 +59,76 @@ contract('Create2', function (accounts) {
|
||||
describe('deploy', function () {
|
||||
it('deploys a contract without constructor', async function () {
|
||||
const offChainComputed = ethers.getCreate2Address(
|
||||
this.factory.address,
|
||||
this.factory.target,
|
||||
saltHex,
|
||||
ethers.keccak256(ConstructorLessContract.bytecode),
|
||||
ethers.keccak256(this.constructorLessBytecode),
|
||||
);
|
||||
|
||||
expectEvent(await this.factory.$deploy(0, saltHex, ConstructorLessContract.bytecode), 'return$deploy', {
|
||||
addr: offChainComputed,
|
||||
});
|
||||
await expect(this.factory.$deploy(0n, saltHex, this.constructorLessBytecode))
|
||||
.to.emit(this.factory, 'return$deploy')
|
||||
.withArgs(offChainComputed);
|
||||
|
||||
expect(ConstructorLessContract.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
|
||||
expect(this.constructorLessBytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
|
||||
});
|
||||
|
||||
it('deploys a contract with constructor arguments', async function () {
|
||||
const offChainComputed = ethers.getCreate2Address(
|
||||
this.factory.address,
|
||||
this.factory.target,
|
||||
saltHex,
|
||||
ethers.keccak256(constructorByteCode),
|
||||
ethers.keccak256(this.constructorByteCode),
|
||||
);
|
||||
|
||||
expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy', {
|
||||
addr: offChainComputed,
|
||||
});
|
||||
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode))
|
||||
.to.emit(this.factory, 'return$deploy')
|
||||
.withArgs(offChainComputed);
|
||||
|
||||
const instance = await VestingWallet.at(offChainComputed);
|
||||
const instance = await ethers.getContractAt('VestingWallet', offChainComputed);
|
||||
|
||||
expect(await instance.owner()).to.be.equal(other);
|
||||
expect(await instance.owner()).to.equal(this.other.address);
|
||||
});
|
||||
|
||||
it('deploys a contract with funds deposited in the factory', async function () {
|
||||
const deposit = ether('2');
|
||||
await send.ether(deployerAccount, this.factory.address, deposit);
|
||||
expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
|
||||
const value = 10n;
|
||||
|
||||
await this.deployer.sendTransaction({ to: this.factory, value });
|
||||
|
||||
const offChainComputed = ethers.getCreate2Address(
|
||||
this.factory.address,
|
||||
this.factory.target,
|
||||
saltHex,
|
||||
ethers.keccak256(constructorByteCode),
|
||||
ethers.keccak256(this.constructorByteCode),
|
||||
);
|
||||
|
||||
expectEvent(await this.factory.$deploy(deposit, saltHex, constructorByteCode), 'return$deploy', {
|
||||
addr: offChainComputed,
|
||||
});
|
||||
expect(await ethers.provider.getBalance(this.factory)).to.equal(value);
|
||||
expect(await ethers.provider.getBalance(offChainComputed)).to.equal(0n);
|
||||
|
||||
expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
|
||||
await expect(this.factory.$deploy(value, saltHex, this.constructorByteCode))
|
||||
.to.emit(this.factory, 'return$deploy')
|
||||
.withArgs(offChainComputed);
|
||||
|
||||
expect(await ethers.provider.getBalance(this.factory)).to.equal(0n);
|
||||
expect(await ethers.provider.getBalance(offChainComputed)).to.equal(value);
|
||||
});
|
||||
|
||||
it('fails deploying a contract in an existent address', async function () {
|
||||
expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy');
|
||||
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode)).to.emit(this.factory, 'return$deploy');
|
||||
|
||||
// TODO: Make sure it actually throws "Create2FailedDeployment".
|
||||
// For some unknown reason, the revert reason sometimes return:
|
||||
// `revert with unrecognized return data or custom error`
|
||||
await expectRevert.unspecified(this.factory.$deploy(0, saltHex, constructorByteCode));
|
||||
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode)).to.be.revertedWithCustomError(
|
||||
this.factory,
|
||||
'Create2FailedDeployment',
|
||||
);
|
||||
});
|
||||
|
||||
it('fails deploying a contract if the bytecode length is zero', async function () {
|
||||
await expectRevertCustomError(this.factory.$deploy(0, saltHex, '0x'), 'Create2EmptyBytecode', []);
|
||||
await expect(this.factory.$deploy(0n, saltHex, '0x')).to.be.revertedWithCustomError(
|
||||
this.factory,
|
||||
'Create2EmptyBytecode',
|
||||
);
|
||||
});
|
||||
|
||||
it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
|
||||
await expectRevertCustomError(
|
||||
this.factory.$deploy(1, saltHex, constructorByteCode),
|
||||
'Create2InsufficientBalance',
|
||||
[0, 1],
|
||||
);
|
||||
await expect(this.factory.$deploy(1n, saltHex, this.constructorByteCode))
|
||||
.to.be.revertedWithCustomError(this.factory, 'Create2InsufficientBalance')
|
||||
.withArgs(0n, 1n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user