Can I deploy a contract in Solidity (not in-line assembly) using create2?
- 18,036
- 20
- 54
- 82
2 Answers
Yes. Solidity version 0.6.2 introduced a high-level way to use the create2 opcode. From the release docs:
When creating a contract, you can specify the salt as a "function call option":
new Contract{salt: 0x1234}(arg1, arg2)
As an example, the following deploy() function will deploy the Test contract using a salt of 0x1234 and a constructor param of 123.
pragma solidity 0.6.2;
contract Test {
uint256 public a;
constructor (uint256 _a) public {
a = _a;
}
}
contract DeployTest {
function deploy() public {
new Test{salt: 0x1234}(123);
}
}
NOTE: salt has to be bytes32. See here: How to convert a string to bytes32?
For example: ethers.utils.formatBytes32String("test"); ➡️ 0x7465737400000000000000000000000000000000000000000000000000000000
- 1,011
- 1
- 15
- 35
- 18,036
- 20
- 54
- 82