The EVM has an opcode called CREATE2 which, given a salt, deploys a new contract deterministically. The address is derived from several parameters, such as the deployer address, the hash of the contract bytecode, and the salt.
We are currently using this opcode on the CartesiDApp factory contract, and providing an entry-point for anyone to calculate the deterministic address even before deploying an application. This feature is interesting if you want to postpone the deployment of smart contracts until the point where there is a dispute on the result of a computation.
Lately, however, we have noticed an increase in demand of creating factories for our own contracts, such as Authority and History. This has led us to question whether we can refactor our code, by reusing code that has already been written related to deterministic deployment.
Our objective is to reduce code duplication and, consequentially, avoid code defects.
This is a blocker to implement new factories, as we want to avoid any new code duplication.
This only affects smart contracts that calculate deterministic deployment addresses.
✔️ Solution
Use OpenZeppelin's Create2 library. In particular, the computeAddress function seems to be a great fit for this problem. This solution is interesting too because we don't need to add any new dependencies to the project, as we are already using OpenZeppelin's contracts in several of our smart contracts.
📈 Subtasks
[x] Create a PR that uses Create2 wherever possible
📚 Context
The EVM has an opcode called
CREATE2
which, given a salt, deploys a new contract deterministically. The address is derived from several parameters, such as the deployer address, the hash of the contract bytecode, and the salt.We are currently using this opcode on the
CartesiDApp
factory contract, and providing an entry-point for anyone to calculate the deterministic address even before deploying an application. This feature is interesting if you want to postpone the deployment of smart contracts until the point where there is a dispute on the result of a computation.Lately, however, we have noticed an increase in demand of creating factories for our own contracts, such as
Authority
andHistory
. This has led us to question whether we can refactor our code, by reusing code that has already been written related to deterministic deployment.Our objective is to reduce code duplication and, consequentially, avoid code defects. This is a blocker to implement new factories, as we want to avoid any new code duplication. This only affects smart contracts that calculate deterministic deployment addresses.
✔️ Solution
Use OpenZeppelin's
Create2
library. In particular, thecomputeAddress
function seems to be a great fit for this problem. This solution is interesting too because we don't need to add any new dependencies to the project, as we are already using OpenZeppelin's contracts in several of our smart contracts.📈 Subtasks
Create2
wherever possible