Skip to content

Instantly share code, notes, and snippets.

@MrChico
Created May 12, 2020 10:37
Show Gist options
  • Save MrChico/73bfb879a231a406bf2d8fd5f6df3d97 to your computer and use it in GitHub Desktop.
Save MrChico/73bfb879a231a406bf2d8fd5f6df3d97 to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.0;
import "../deposit_contract.sol";
contract DepositContractFactory {
bool public deployed;
uint public deploymentTime;
DepositContract depositContract;
constructor(uint _deploymentTime) public {
deploymentTime = _deploymentTime;
}
function deploy() external {
require(block.timestamp >= deploymentTime, "not yet!");
require(!deployed, "already deployed");
depositContract = new DepositContract();
deployed = true;
}
}
@axic
Copy link

axic commented May 12, 2020

Export depositContract and use immutable:

pragma solidity ^0.6.0;

import "../deposit_contract.sol";

contract DepositContractFactory {
  bool public deployed;
  uint public immutable deploymentTime;
  DepositContract public depositContract;

  constructor(uint _deploymentTime) public {
    deploymentTime = _deploymentTime;
  }

  function deploy() external {
    require(block.timestamp >= deploymentTime, "not yet!");
    require(!deployed, "already deployed");
    depositContract = new DepositContract();
    deployed = true;
  }
}

@axic
Copy link

axic commented May 12, 2020

And this one adds an extra check on the resulting codehash (if we are not to trust the factory enough):

pragma solidity ^0.6.0;

import "../deposit_contract.sol";

contract DepositContractFactory {
  bool public deployed;
  uint public immutable deploymentTime;
  bytes32 public immutable depositContractHash;
  DepositContract public depositContract;

  constructor(uint _deploymentTime, bytes32 _depositContractHash) public {
    deploymentTime = _deploymentTime;
    depositContractHash = _depositContractHash;
  }

  function deploy() external {
    require(block.timestamp >= deploymentTime, "not yet!");
    require(!deployed, "already deployed");
    depositContract = new DepositContract();
    // Soon to be supported as `address(depositContract).codehash`
    bytes32 codehash;
    assembly {
       codehash := extcodehash(depositContract)
    }
    require(codehash == depositContractHash, "broken!?");
    deployed = true;
  }
}

@axic
Copy link

axic commented May 12, 2020

Additionally could use CREATE2:

depositContract = new DepositContract{salt = <salt>}();

@MrChico
Copy link
Author

MrChico commented May 12, 2020

Export depositContract and use immutable:

👍

Additionally could use CREATE2:

yeah, but the address is already predictable regardless

@axic
Copy link

axic commented May 12, 2020

I reckon actually CREATE2 would be a bad idea if the salt exists in the factory because the contract could be deployed outside the factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment