8

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

2 Answers 2

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

3

Yes. Solidity version 0.6.2 you can do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.