Skip to content

Commit

Permalink
rename verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Feb 23, 2023
1 parent b560e7f commit 2e87c32
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 191 deletions.
File renamed without changes.
97 changes: 5 additions & 92 deletions deployment/createGenesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const {
MemDB, ZkEVMDB, getPoseidon, smtUtils,
} = require('@0xpolygonhermez/zkevm-commonjs');

const { deployPolygonZkEVMDeployer, create2Deployment } = require('./helpers/deployment-helpers');

const deployParametersPath = argv.input ? argv.input : './deploy_parameters.json';
const deployParameters = require(deployParametersPath);

Expand Down Expand Up @@ -66,13 +68,13 @@ async function main() {
const proxyAdminFactory = await ethers.getContractFactory('ProxyAdmin', deployer);
const deployTransactionAdmin = (proxyAdminFactory.getDeployTransaction()).data;
const dataCallAdmin = proxyAdminFactory.interface.encodeFunctionData('transferOwnership', [deployer.address]);
const proxyAdminAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionAdmin, dataCallAdmin);
const proxyAdminAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionAdmin, dataCallAdmin, deployer);

// Deploy implementation PolygonZkEVMBridg
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge', deployer);
const deployTransactionBridge = (polygonZkEVMBridgeFactory.getDeployTransaction()).data;
// Mandatory to override the gasLimit since the estimation with create are mess up D:
const overrideGasLimit = ethers.BigNumber.from(6000000); // ; // Should be more than enough with 5M
const overrideGasLimit = ethers.BigNumber.from(5500000);
const bridgeImplementationAddress = await create2Deployment(
zkEVMDeployerContract,
salt,
Expand Down Expand Up @@ -102,7 +104,7 @@ async function main() {
zkevmAddressL2,
],
);
const proxyBridgeAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionProxy, dataCallProxy);
const proxyBridgeAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionProxy, dataCallProxy, deployer);

// Import OZ manifest the deployed contracts, its enough to import just the proyx, the rest are imported automatically ( admin/impl)
await upgrades.forceImport(proxyBridgeAddress, polygonZkEVMBridgeFactory, 'transparent');
Expand Down Expand Up @@ -319,95 +321,6 @@ main().catch((e) => {
process.exit(1);
});

async function deployPolygonZkEVMDeployer(deployer) {
const PolgonZKEVMDeployerFactory = await ethers.getContractFactory('PolygonZkEVMDeployer', deployer);

const deployTxZKEVMDeployer = (PolgonZKEVMDeployerFactory.getDeployTransaction(
deployer.address,
)).data;

const gasLimit = ethers.BigNumber.from(1000000); // Put 1 Million, aprox 650k are necessary
const gasPrice = ethers.BigNumber.from(ethers.utils.parseUnits('100', 'gwei')); // just in case , seems pretty standard
const to = '0x'; // bc deployment transaction, "to" is "0x"
const tx = {
to,
nonce: 0,
value: 0,
gasLimit: gasLimit.toHexString(),
gasPrice: gasPrice.toHexString(),
data: deployTxZKEVMDeployer,
};

const signature = {
v: 27,
r: '0x247000', // Equals 0x0000000000000000000000000000000000000000000000000000000000247000 TODO
s: '0x2470', // Equals 0x0000000000000000000000000000000000000000000000000000000000002470 TODO
};
const serializedTransaction = ethers.utils.serializeTransaction(tx, signature);
const resultTransaction = ethers.utils.parseTransaction(serializedTransaction);
const totalEther = gasLimit.mul(gasPrice); // 0.1 ether

// Check if it's already deployed
const zkEVMDeployerAddress = ethers.utils.getContractAddress(resultTransaction);
if (await ethers.provider.getCode(zkEVMDeployerAddress) !== '0x') {
const zkEVMDeployerContract = PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return [zkEVMDeployerContract, resultTransaction.from];
}
// Fund keyless deployment
const params = {
to: resultTransaction.from,
value: totalEther.toHexString(),
};
await (await deployer.sendTransaction(params)).wait();

// Deploy zkEVMDeployer
await (await ethers.provider.sendTransaction(serializedTransaction)).wait();

const zkEVMDeployerContract = await PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return [zkEVMDeployerContract, resultTransaction.from];
}

async function create2Deployment(polgonZKEVMDeployerContract, salt, deployTransaction, dataCall, deployer, hardcodedGasLimit) {
// Encode deploy transaction
const hashInitCode = ethers.utils.solidityKeccak256(['bytes'], [deployTransaction]);

// Precalculate create2 address
const precalculatedAddressDeployed = ethers.utils.getCreate2Address(polgonZKEVMDeployerContract.address, salt, hashInitCode);
const amount = 0;

if (dataCall) {
// Deploy using create2 and call
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministicAndCall(
amount,
salt,
deployTransaction,
dataCall,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministicAndCall(amount, salt, deployTransaction, dataCall)).wait();
}
} else {
// Deploy using create2
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministic(
amount,
salt,
deployTransaction,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministic(amount, salt, deployTransaction)).wait();
}
}
return precalculatedAddressDeployed;
}

async function getAddressInfo(address) {
const nonce = await ethers.provider.getTransactionCount(address);
const bytecode = await ethers.provider.getCode(address);
Expand Down
104 changes: 6 additions & 98 deletions deployment/deployContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
const fs = require('fs');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });

const { deployPolygonZkEVMDeployer, create2Deployment } = require('./helpers/deployment-helpers');

const pathOutputJson = path.join(__dirname, './deploy_output.json');
const deployParameters = require('./deploy_parameters.json');
const genesis = require('./genesis.json');
Expand Down Expand Up @@ -96,7 +98,7 @@ async function main() {
console.log('Verifier deployed to:', verifierContract.address);

// Deploy PolygonZkEVMDeployer if is not deployed already using keyless deployment
const zkEVMDeployerContract = await deployPolygonZkEVMDeployer(deployer);
const [zkEVMDeployerContract, keylessDeployer] = await deployPolygonZkEVMDeployer(deployer);

/*
* Deploy Bridge
Expand All @@ -107,7 +109,7 @@ async function main() {
const proxyAdminFactory = await ethers.getContractFactory('ProxyAdmin', deployer);
const deployTransactionAdmin = (proxyAdminFactory.getDeployTransaction()).data;
const dataCallAdmin = proxyAdminFactory.interface.encodeFunctionData('transferOwnership', [deployer.address]);
const proxyAdminAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionAdmin, dataCallAdmin);
const proxyAdminAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionAdmin, dataCallAdmin, deployer);

console.log('#######################\n');
console.log('Proxy admin deployed to:', proxyAdminAddress);
Expand All @@ -116,7 +118,7 @@ async function main() {
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge', deployer);
const deployTransactionBridge = (polygonZkEVMBridgeFactory.getDeployTransaction()).data;
// Mandatory to override the gasLimit since the estimation with create are mess up D:
const overrideGasLimit = ethers.BigNumber.from(5500000); // Should be more than enough with 5M
const overrideGasLimit = ethers.BigNumber.from(5500000);
const bridgeImplementationAddress = await create2Deployment(
zkEVMDeployerContract,
salt,
Expand Down Expand Up @@ -159,7 +161,7 @@ async function main() {
precalculateZkevmAddress,
],
);
const proxyBridgeAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionProxy, dataCallProxy);
const proxyBridgeAddress = await create2Deployment(zkEVMDeployerContract, salt, deployTransactionProxy, dataCallProxy, deployer);
const polygonZkEVMBridgeContract = polygonZkEVMBridgeFactory.attach(proxyBridgeAddress);

console.log('#######################\n');
Expand Down Expand Up @@ -379,97 +381,3 @@ main().catch((e) => {
console.error(e);
process.exit(1);
});

async function deployPolygonZkEVMDeployer(deployer) {
const PolgonZKEVMDeployerFactory = await ethers.getContractFactory('PolygonZkEVMDeployer', deployer);

const deployTxZKEVMDeployer = (PolgonZKEVMDeployerFactory.getDeployTransaction(
deployer.address,
)).data;

const gasLimit = ethers.BigNumber.from(1000000); // Put 1 Million, aprox 650k are necessary
const gasPrice = ethers.BigNumber.from(ethers.utils.parseUnits('100', 'gwei')); // just in case , seems pretty standard
const to = '0x'; // bc deployment transaction, "to" is "0x"
const tx = {
to,
nonce: 0,
value: 0,
gasLimit: gasLimit.toHexString(),
gasPrice: gasPrice.toHexString(),
data: deployTxZKEVMDeployer,
};

const signature = {
v: 27,
r: '0x247000', // Equals 0x0000000000000000000000000000000000000000000000000000000000247000 TODO
s: '0x2470', // Equals 0x0000000000000000000000000000000000000000000000000000000000002470 TODO
};
const serializedTransaction = ethers.utils.serializeTransaction(tx, signature);
const resultTransaction = ethers.utils.parseTransaction(serializedTransaction);
const totalEther = gasLimit.mul(gasPrice); // 0.1 ether

// Check if it's already deployed
const zkEVMDeployerAddress = ethers.utils.getContractAddress(resultTransaction);
if (await ethers.provider.getCode(zkEVMDeployerAddress) !== '0x') {
const zkEVMDeployerContract = PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return zkEVMDeployerContract;
}

// Fund keyless deployment
const params = {
to: resultTransaction.from,
value: totalEther.toHexString(),
};
await (await deployer.sendTransaction(params)).wait();

// Deploy zkEVMDeployer
await (await ethers.provider.sendTransaction(serializedTransaction)).wait();

const zkEVMDeployerContract = await PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return zkEVMDeployerContract;
}

async function create2Deployment(polgonZKEVMDeployerContract, salt, deployTransaction, dataCall, deployer, hardcodedGasLimit) {
// Encode deploy transaction
const hashInitCode = ethers.utils.solidityKeccak256(['bytes'], [deployTransaction]);

// Precalculate create2 address
const precalculatedAddressDeployed = ethers.utils.getCreate2Address(polgonZKEVMDeployerContract.address, salt, hashInitCode);
const amount = 0;

if (await ethers.provider.getCode(precalculatedAddressDeployed) !== '0x') {
return precalculatedAddressDeployed;
}

if (dataCall) {
// Deploy using create2 and call
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministicAndCall(
amount,
salt,
deployTransaction,
dataCall,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministicAndCall(amount, salt, deployTransaction, dataCall)).wait();
}
} else {
// Deploy using create2
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministic(
amount,
salt,
deployTransaction,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministic(amount, salt, deployTransaction)).wait();
}
}
return precalculatedAddressDeployed;
}
103 changes: 103 additions & 0 deletions deployment/helpers/deployment-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* eslint-disable no-await-in-loop, no-use-before-define, no-lonely-if, import/no-dynamic-require */
/* eslint-disable no-console, no-inner-declarations, no-undef, import/no-unresolved */
const { expect } = require('chai');
const { ethers } = require('hardhat');

async function deployPolygonZkEVMDeployer(deployer) {
const PolgonZKEVMDeployerFactory = await ethers.getContractFactory('PolygonZkEVMDeployer', deployer);

const deployTxZKEVMDeployer = (PolgonZKEVMDeployerFactory.getDeployTransaction(
deployer.address,
)).data;

const gasLimit = ethers.BigNumber.from(1000000); // Put 1 Million, aprox 650k are necessary
const gasPrice = ethers.BigNumber.from(ethers.utils.parseUnits('100', 'gwei')); // just in case , seems pretty standard
const to = '0x'; // bc deployment transaction, "to" is "0x"
const tx = {
to,
nonce: 0,
value: 0,
gasLimit: gasLimit.toHexString(),
gasPrice: gasPrice.toHexString(),
data: deployTxZKEVMDeployer,
};

const signature = {
v: 27,
r: '0x247000', // Equals 0x0000000000000000000000000000000000000000000000000000000000247000 TODO
s: '0x2470', // Equals 0x0000000000000000000000000000000000000000000000000000000000002470 TODO
};
const serializedTransaction = ethers.utils.serializeTransaction(tx, signature);
const resultTransaction = ethers.utils.parseTransaction(serializedTransaction);
const totalEther = gasLimit.mul(gasPrice); // 0.1 ether

// Check if it's already deployed
const zkEVMDeployerAddress = ethers.utils.getContractAddress(resultTransaction);
if (await deployer.provider.getCode(zkEVMDeployerAddress) !== '0x') {
const zkEVMDeployerContract = PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return zkEVMDeployerContract;
}

// Fund keyless deployment
const params = {
to: resultTransaction.from,
value: totalEther.toHexString(),
};
await (await deployer.sendTransaction(params)).wait();

// Deploy zkEVMDeployer
await (await deployer.provider.sendTransaction(serializedTransaction)).wait();

const zkEVMDeployerContract = await PolgonZKEVMDeployerFactory.attach(zkEVMDeployerAddress);
expect(await zkEVMDeployerContract.owner()).to.be.equal(deployer.address);
return [zkEVMDeployerContract, resultTransaction.from];
}

async function create2Deployment(polgonZKEVMDeployerContract, salt, deployTransaction, dataCall, deployer, hardcodedGasLimit) {
// Encode deploy transaction
const hashInitCode = ethers.utils.solidityKeccak256(['bytes'], [deployTransaction]);

// Precalculate create2 address
const precalculatedAddressDeployed = ethers.utils.getCreate2Address(polgonZKEVMDeployerContract.address, salt, hashInitCode);
const amount = 0;

if (await deployer.provider.getCode(precalculatedAddressDeployed) !== '0x') {
return precalculatedAddressDeployed;
}

if (dataCall) {
// Deploy using create2 and call
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministicAndCall(
amount,
salt,
deployTransaction,
dataCall,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministicAndCall(amount, salt, deployTransaction, dataCall)).wait();
}
} else {
// Deploy using create2
if (hardcodedGasLimit) {
const populatedTransaction = await polgonZKEVMDeployerContract.populateTransaction.deployDeterministic(
amount,
salt,
deployTransaction,
);
populatedTransaction.gasLimit = ethers.BigNumber.from(hardcodedGasLimit);
await (await deployer.sendTransaction(populatedTransaction)).wait();
} else {
await (await polgonZKEVMDeployerContract.deployDeterministic(amount, salt, deployTransaction)).wait();
}
}
return precalculatedAddressDeployed;
}

module.exports = {
deployPolygonZkEVMDeployer,
create2Deployment,
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports.PolygonZkEVMGlobalExitRoot = require('./compiled-contracts/Polygo
module.exports.PolygonZkEVMGlobalExitRootL2 = require('./compiled-contracts/PolygonZkEVMGlobalExitRootL2.json');
module.exports.PolygonZkEVM = require('./compiled-contracts/PolygonZkEVM.json');
module.exports.TokenWrapped = require('./compiled-contracts/TokenWrapped.json');
module.exports.Verifier = require('./compiled-contracts/Verifier.json');
module.exports.FflonkVerifier = require('./compiled-contracts/FflonkVerifier.json');
module.exports.PolygonZkEVMBridgeMock = require('./compiled-contracts/PolygonZkEVMBridgeMock.json');
module.exports.ERC20PermitMock = require('./compiled-contracts/ERC20PermitMock.json');
module.exports.PolygonZkEVMGlobalExitRootL2Mock = require('./compiled-contracts/PolygonZkEVMGlobalExitRootL2Mock.json');
Expand Down

0 comments on commit 2e87c32

Please sign in to comment.