Why would someone use create2() inline assembly code to create a child contract in factory pattern as opposed to using
newContract() to create an instance of "newContract"?
What are the upsides and downsides of using one over the other?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
CREATE opcode is used by default when deploying smart contracts. The deployed contract address is calculated like this.
CREATE2 opcode is introduced later and allows you to predetermine the contract address. Contract address is computed like this.
keccak256(0xFF, senderAddress, salt, bytecode)
0xFF parameter is a constant to prevent collision with CREATE opcode.
salt parameter is a value sender sends when deploying contract.
bytecode parameter is, you probably guessed it, the bytecode of the smart contract you want to deploy.
If you want to predetermine the contract address before deploying, you can simply loop through different
salt values and select the one you like (or want).
A great example of using CREATE2 can be seen here. Application of
Edit: Creating a contract with
new keyword requires you to know the contract's source code. After creating a contract with
new keyword, it will return the created contract's address.
This also uses
CREATE opcode behind to create the contract.