8

Can I deploy a contract in Solidity (not in-line assembly) using create2?

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82

2 Answers2

17

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

Mars Robertson
  • 1,011
  • 1
  • 15
  • 35
Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82
3

Yes. Solidity version 0.6.2 you can do it

kavlit
  • 31
  • 2