createx icon indicating copy to clipboard operation
createx copied to clipboard

[Feature-Request]: How to compute deploy address ?

Open eyalc900 opened this issue 1 year ago • 0 comments

Describe the desired feature:

the deployCreate2(salt,initCode) generates a deterministic address for the target contract. But how can i get that address beforehand? off-chain, I can do eth_call to get it, but on-chain (or foundry solidity script), we need a view function.

There is a view function computeCreate2Address(salt,initCodeHash) but it CANT be used: the reason is that deployCreate2 doesn't use the salt as-is, but "guard" it. even simple salt like "0" is replaced. There is also on view function to calculate the guarded value.

  • The documentation should be fixed, as the meaning of "salt" is completely different between those function families.
  • add a function that return the address of computeCreate2Address
  • or at minimum, add a view function that "guard" the salt.

Workaround

The following code can calculate the address in a script. It has 2 shortcomings:

  • besides being ugly 2-method wrapper, it wastes actual deployment gas, and thus can't be used on a real network.
  • It can't calculate the address if the contract is already deployed.
   function internalDeployAddress(bytes32 salt, bytes memory initCode) public {
        address addr = createX.deployCreate2(salt, initCode);
        assembly {
            mstore(0x0, addr)
            revert(0,32)
        }
    }

    //workaround for a missing getDeployCreate2Address in ICreateX
    function getDeployAddress(bytes32 salt, bytes memory initCode) internal returns (address ret) {
        try this.internalDeployAddress(salt, initCode) {
        } catch (bytes memory reason) {
            (ret) = abi.decode(reason, (address));
        }
    }

eyalc900 avatar Oct 05 '24 11:10 eyalc900