2022-02-23 17:27:44 +01:00
|
|
|
const { ethers } = require('hardhat')
|
|
|
|
const defaultConfig = require('../config')
|
|
|
|
|
2022-07-26 19:07:04 +02:00
|
|
|
async function upgradableContract({ contractName, implConstructorArgs, proxyConstructorArgs, salt }) {
|
|
|
|
const Implementation = await ethers.getContractFactory(contractName)
|
|
|
|
|
|
|
|
const implementationBytecode =
|
|
|
|
Implementation.bytecode + Implementation.interface.encodeDeploy(implConstructorArgs).slice(2)
|
|
|
|
|
|
|
|
const implementationAddress = ethers.utils.getCreate2Address(
|
|
|
|
defaultConfig.singletonFactory,
|
|
|
|
salt,
|
|
|
|
ethers.utils.keccak256(implementationBytecode),
|
|
|
|
)
|
|
|
|
|
|
|
|
const AdminUpgradeableProxy = await ethers.getContractFactory('AdminUpgradeableProxy')
|
|
|
|
const proxyConst = [implementationAddress, ...proxyConstructorArgs]
|
|
|
|
const proxyBytecode =
|
|
|
|
AdminUpgradeableProxy.bytecode + AdminUpgradeableProxy.interface.encodeDeploy(proxyConst).slice(2)
|
|
|
|
|
|
|
|
const proxyAddress = ethers.utils.getCreate2Address(
|
|
|
|
defaultConfig.singletonFactory,
|
|
|
|
salt,
|
|
|
|
ethers.utils.keccak256(proxyBytecode),
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
implementation: {
|
|
|
|
address: implementationAddress,
|
|
|
|
bytecode: implementationBytecode,
|
|
|
|
args: implConstructorArgs,
|
|
|
|
},
|
|
|
|
proxy: { address: proxyAddress, bytecode: proxyBytecode, args: proxyConst },
|
|
|
|
isProxy: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:27:44 +01:00
|
|
|
async function generate(config = defaultConfig) {
|
2022-07-26 19:07:04 +02:00
|
|
|
// factory contract -----------------------------------------------
|
2022-03-31 21:40:12 +02:00
|
|
|
const FactoryFactory = await ethers.getContractFactory('MultipleInstanceFactory')
|
2022-07-26 19:07:04 +02:00
|
|
|
const FactoryInitData = FactoryFactory.interface.encodeFunctionData('initialize', [
|
|
|
|
config.verifier,
|
|
|
|
config.hasher,
|
|
|
|
config.merkleTreeHeight,
|
|
|
|
config.admin,
|
|
|
|
])
|
2022-03-09 22:43:57 +01:00
|
|
|
|
2022-07-26 19:07:04 +02:00
|
|
|
const factoryContract = await upgradableContract({
|
|
|
|
contractName: 'MultipleInstanceFactory',
|
|
|
|
implConstructorArgs: [],
|
|
|
|
proxyConstructorArgs: [config.admin, FactoryInitData],
|
|
|
|
salt: config.salt,
|
|
|
|
})
|
2022-03-09 22:43:57 +01:00
|
|
|
|
2022-07-26 19:07:04 +02:00
|
|
|
// factory with registry contract ---------------------------------
|
2022-03-09 22:43:57 +01:00
|
|
|
const FactoryWithRegistryFactory = await ethers.getContractFactory('InstanceFactoryWithRegistry')
|
2022-07-26 19:07:04 +02:00
|
|
|
const FactoryWithRegistryInitData = FactoryWithRegistryFactory.interface.encodeFunctionData(
|
|
|
|
'initialize(address,address,uint32,address,uint16,uint256)',
|
|
|
|
[
|
|
|
|
config.verifier,
|
|
|
|
config.hasher,
|
|
|
|
config.merkleTreeHeight,
|
|
|
|
config.governance,
|
|
|
|
config.TWAPSlotsMin,
|
|
|
|
config.creationFee,
|
|
|
|
],
|
2022-02-23 17:27:44 +01:00
|
|
|
)
|
|
|
|
|
2022-07-26 19:07:04 +02:00
|
|
|
const factoryWithRegistryContract = await upgradableContract({
|
|
|
|
contractName: 'InstanceFactoryWithRegistry',
|
|
|
|
implConstructorArgs: [
|
|
|
|
config.governance,
|
|
|
|
config.instanceRegistry,
|
|
|
|
config.TORN,
|
|
|
|
config.UniswapV3Factory,
|
|
|
|
config.WETH,
|
|
|
|
],
|
|
|
|
proxyConstructorArgs: [config.governance, FactoryWithRegistryInitData],
|
|
|
|
salt: config.salt,
|
|
|
|
})
|
|
|
|
|
2022-02-23 17:27:44 +01:00
|
|
|
const result = {
|
2022-07-26 19:07:04 +02:00
|
|
|
factoryContract,
|
|
|
|
factoryWithRegistryContract,
|
2022-02-23 17:27:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateWithLog() {
|
|
|
|
const contracts = await generate()
|
2022-07-26 19:07:04 +02:00
|
|
|
console.log('MultipleInstanceFactory contract: ', contracts.factoryContract.implementation.address)
|
|
|
|
console.log('MultipleInstanceFactory proxy contract: ', contracts.factoryContract.proxy.address)
|
|
|
|
console.log(
|
|
|
|
'Instance factory with registry contract: ',
|
|
|
|
contracts.factoryWithRegistryContract.implementation.address,
|
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
'Instance factory with registry proxy contract: ',
|
|
|
|
contracts.factoryWithRegistryContract.proxy.address,
|
|
|
|
)
|
2022-02-23 17:27:44 +01:00
|
|
|
return contracts
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
generate,
|
|
|
|
generateWithLog,
|
|
|
|
}
|