Skip to content

Commit

Permalink
feat(genesis): support deterministic L2 pre-deployed contract address…
Browse files Browse the repository at this point in the history
…es (#358)
  • Loading branch information
davidtaikocha committed Dec 1, 2022
1 parent 1d46b68 commit cd34f17
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
6 changes: 6 additions & 0 deletions packages/protocol/test/genesis/generate_genesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ action("Generate Genesis", function () {
const expectCode: string = alloc[address].code

expect(code.toLowerCase()).to.be.equal(expectCode.toLowerCase())

if (testConfig.contractAddresses[alloc[address].contractName]) {
expect(address).to.be.equal(
testConfig.contractAddresses[alloc[address].contractName]
)
}
}
})

Expand Down
42 changes: 41 additions & 1 deletion packages/protocol/test/genesis/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
{
"0xdD2FD4581271e230360230F9337D5c0430Bf44C0": 1024
},
{
"0xbDA5747bFD65F08deb54cb465eB87D40e51B197E": 1024
},
{
"0x2546BcD3c84621e976D8185a91A922aE77ECEc30": 1024
},
Expand All @@ -40,7 +43,44 @@
},
{
"0xBcd4042DE499D14e55001CcbB24a551F3b954096": 1024
},
{
"0xa0Ee7A142d267C1f36714E4a8F75612F20a79720": 1024
},
{
"0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f": 1024
},
{
"0x14dC79964da2C08b23698B3D3cc7Ca32193d9955": 1024
},
{
"0x976EA74026E726554dB657fA54763abd0C3a0aa9": 1024
},
{
"0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc": 1024
},
{
"0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65": 1024
},
{
"0x90F79bf6EB2c4f870365E785982E1f101E93b906": 1024
},
{
"0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC": 1024
},
{
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8": 1024
},
{
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": 1024
}
],
"contractAddresses": {
"V1TaikoL2": "0x0000777700000000000000000000000000000001",
"TokenVault": "0x0000777700000000000000000000000000000002",
"EtherVault": "0x0000777700000000000000000000000000000003",
"Bridge": "0x0000777700000000000000000000000000000004",
"TestERC20": "0x0000777700000000000000000000000000000005"
},
"predeployERC20": true
}
}
22 changes: 15 additions & 7 deletions packages/protocol/utils/generate_genesis/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ export async function deployERC20(
"./test/thirdparty/TestERC20.sol/TestERC20.json"
))

const address = ethers.utils.getCreate2Address(
contractOwner,
ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${chainId}${artifact.contractName}`)
),
ethers.utils.keccak256(ethers.utils.toUtf8Bytes(artifact.bytecode))
)
let address: string
if (
config.contractAddresses &&
ethers.utils.isAddress(config.contractAddresses[artifact.contractName])
) {
address = config.contractAddresses[artifact.contractName]
} else {
address = ethers.utils.getCreate2Address(
contractOwner,
ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${chainId}${artifact.contractName}`)
),
ethers.utils.keccak256(ethers.utils.toUtf8Bytes(artifact.bytecode))
)
}

const variables = {
_name: TOKEN_NAME,
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/utils/generate_genesis/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Config {
[key: string]: number
}>
predeployERC20: boolean
contractAddresses: Object
}

export interface Result {
Expand Down
27 changes: 18 additions & 9 deletions packages/protocol/utils/generate_genesis/v1TaikoL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export async function deployV1TaikoL2(

const contractConfigs: any = await generateContractConfigs(
contractOwner,
chainId
chainId,
config.contractAddresses
)

const storageLayouts: any = {}
Expand Down Expand Up @@ -88,7 +89,8 @@ export async function deployV1TaikoL2(
// and initialized variables.
async function generateContractConfigs(
contractOwner: string,
chainId: number
chainId: number,
hardCodedAddresses: any
): Promise<any> {
const contractArtifacts: any = {
// Libraries
Expand Down Expand Up @@ -165,13 +167,20 @@ async function generateContractConfigs(
bytecode = linkContractLibs(contractArtifacts.Bridge, addressMap)
}

addressMap[contractName] = ethers.utils.getCreate2Address(
contractOwner,
ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${chainId}${contractName}`)
),
ethers.utils.keccak256(ethers.utils.toUtf8Bytes(bytecode))
)
if (
hardCodedAddresses &&
ethers.utils.isAddress(hardCodedAddresses[contractName])
) {
addressMap[contractName] = hardCodedAddresses[contractName]
} else {
addressMap[contractName] = ethers.utils.getCreate2Address(
contractOwner,
ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${chainId}${contractName}`)
),
ethers.utils.keccak256(ethers.utils.toUtf8Bytes(bytecode))
)
}
}

console.log("pre-computed addresses:")
Expand Down

0 comments on commit cd34f17

Please sign in to comment.