0xsequence / create3

CREATE3 (see EIP-3171) implemented in Solidity
280 stars 34 forks source link

Code produces different addresses and I dont know why #6

Closed 0xDEnYO closed 2 years ago

0xDEnYO commented 2 years ago

Hey Guys,

I am very excited about CREATE3 (thanks for sharing this !!!) and have tried to apply it in our context but I cant manage to produce same contract addresses across different networks.

I have a contract "CrossChainBridgeRouter" (which I want to deploy with same addresses) and in the same .sol-file I have another helper contract with this code:

`contract RouterDeployer { bytes private salt = bytes('ThisIsMySalt');

function deployRouter() external returns (address) { return Create3.create3(keccak256(salt), type(CrossChainBridgeRouter).creationCode); } }`

and then I have a deploy script that deploys the helper contract and calls the deploy function. But when I do that in various networks, it produces different addresses and I dont know why. The deployer wallet is the same, the salt is fixed and the contract code doesnt change either. So where is the variation coming from?

This is my deploy script:

` console.log("Deploying from address: ", deployer.address); console.log("Deploying helper contract that has CREATE3 deployer function..."); const RouterDeployer = await hre.ethers.getContractFactory('RouterDeployer'); const routerDeployer = await RouterDeployer.deploy(); await routerDeployer.deployed();

console.log("Now deploying actual router contract using CREATE3...");
const routerDeployedAt = await routerDeployer.callStatic.deployRouter();
console.log(routerDeployedAt);`

Do you have an idea what I am missing or doing wrong?

Agusx1211 commented 2 years ago

Hi Daniel, this is because CREATE3 alone is not meant to deploy a contract with the same address on multiple networks, it's meant to deploy a contract with arbitrary code pointing to a single deterministic address.

To achieve what you are trying to do you need to use something called Nick's method, is a little trickier to use but it will get you the same address on both chains, alternatively you can use a "deployer" that I already have deployed using this method, but it may not be available in the chain you are wanting to use (but you can always deploy it yourself).

See: https://gist.github.com/Agusx1211/de05dabf918d448d315aa018e2572031