|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.17; |
| 3 | + |
| 4 | +import {ArbWasm} from "../precompiles/ArbWasm.sol"; |
| 5 | + |
| 6 | +/// @title A Stylus contract deployer, activator and initializer |
| 7 | +/// @author The name of the author |
| 8 | +/// @notice Stylus contracts do not support constructors. Instead, Stylus devs can use this contract to deploy and |
| 9 | +/// initialize their contracts atomically |
| 10 | +contract StylusDeployer { |
| 11 | + ArbWasm constant ARB_WASM = ArbWasm(0x0000000000000000000000000000000000000071); |
| 12 | + |
| 13 | + event ContractDeployed(address deployedContract); |
| 14 | + |
| 15 | + error ContractDeploymentError(bytes bytecode); |
| 16 | + error ContractInitializationError(address newContract); |
| 17 | + error RefundExcessValueError(uint256 excessValue); |
| 18 | + error EmptyBytecode(); |
| 19 | + error InitValueButNotInitData(); |
| 20 | + |
| 21 | + /// @notice Deploy, activate and initialize a stylus contract |
| 22 | + /// In order to call a stylus contract, and therefore initialize it, it must first be activated. |
| 23 | + /// This contract provides an atomic way of deploying, activating and initializing a stylus contract. |
| 24 | + /// |
| 25 | + /// Initialisation will be called if initData is supplied, other initialization is skipped |
| 26 | + /// Activation is not always necessary. If a contract has the same code has as another recently activated |
| 27 | + /// contract then activation will be skipped. |
| 28 | + /// If additional value remains in the contract after activation it will be transferred to the msg.sender |
| 29 | + /// to that end the caller must ensure that they can receive eth. |
| 30 | + /// |
| 31 | + /// The caller should do the following before calling this contract: |
| 32 | + /// 1. Check whether the contract will require activation, and if so what the cost will be. |
| 33 | + /// This can be done by spoofing an address with the contract code, then calling ArbWasm.programVersion to compare the |
| 34 | + /// the returned result against ArbWasm.stylusVersion. If activation is required ArbWasm.activateProgram can then be called |
| 35 | + /// to find the returned dataFee. |
| 36 | + /// 2. Next this deploy function can be called. The value of the call must be set to the previously ascertained dataFee + initValue |
| 37 | + /// If activation is not require, the value of the call should be set to initValue |
| 38 | + /// |
| 39 | + /// Note: Stylus contract caching is not done by the deployer, and will have to be done separately after deployment. |
| 40 | + /// See https://docs.arbitrum.io/stylus/how-tos/caching-contracts for more details on caching |
| 41 | + /// @param bytecode The bytecode of the stylus contract to be deployed |
| 42 | + /// @param initData Initialisation call data. After deployment the contract will be called with this data |
| 43 | + /// If no initialisation data is provided then the newly deployed contract will not be called. |
| 44 | + /// This means that the receive or dataless fallback function cannot be called from this contract. |
| 45 | + /// @param initValue Initialisation value. After deployment, the contract will be called with the init data and this value. |
| 46 | + /// At least as much eth as init value must be provided with this call. Init value is specified here separately |
| 47 | + /// rather than using the msg.value since the msg.value may need to be greater than the init value to accomodate activation data fee. |
| 48 | + /// See the @notice block above for more details. |
| 49 | + /// @param salt If a non zero salt is provided the contract will be created using CREATE2 instead of CREATE |
| 50 | + /// The supplied salt will be hashed with the initData so that wherever the address is observed |
| 51 | + /// it was initialised with the same variables. |
| 52 | + /// @return The address of the deployed conract |
| 53 | + function deploy( |
| 54 | + bytes calldata bytecode, |
| 55 | + bytes calldata initData, |
| 56 | + uint256 initValue, |
| 57 | + bytes32 salt |
| 58 | + ) public payable returns (address) { |
| 59 | + if (salt != 0) { |
| 60 | + // if a salt was supplied, hash the salt with the init data. This guarantees that |
| 61 | + // anywhere the address of this contract is seen the same init data was used |
| 62 | + salt = initSalt(salt, initData); |
| 63 | + } |
| 64 | + |
| 65 | + address newContractAddress = deployContract(bytecode, salt); |
| 66 | + bool shouldActivate = requiresActivation(newContractAddress); |
| 67 | + uint256 dataFee = 0; |
| 68 | + if (shouldActivate) { |
| 69 | + // ensure there will be enough left over for init |
| 70 | + // activateProgram will return unused value back to this contract without an EVM call |
| 71 | + uint256 activationValue = msg.value - initValue; |
| 72 | + (, dataFee) = ARB_WASM.activateProgram{value: activationValue}(newContractAddress); |
| 73 | + } |
| 74 | + |
| 75 | + // initialize - this will fail if the program wasn't activated by this point |
| 76 | + // we check if initData exists to avoid calling contracts unnecessarily |
| 77 | + if (initData.length != 0) { |
| 78 | + (bool success,) = address(newContractAddress).call{value: initValue}(initData); |
| 79 | + if (!success) { |
| 80 | + revert ContractInitializationError(newContractAddress); |
| 81 | + } |
| 82 | + } else if (initValue != 0) { |
| 83 | + // if initValue exists init data should too |
| 84 | + revert InitValueButNotInitData(); |
| 85 | + } |
| 86 | + |
| 87 | + // refund any remaining value |
| 88 | + uint256 bal = msg.value - dataFee - initValue; |
| 89 | + if (bal != 0) { |
| 90 | + // the caller must be payable |
| 91 | + (bool sent,) = payable(msg.sender).call{value: bal}(""); |
| 92 | + if (!sent) { |
| 93 | + revert RefundExcessValueError(bal); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // activation already emits the following event: |
| 98 | + // event ProgramActivated(bytes32 indexed codehash, bytes32 moduleHash, address program, uint256 dataFee, uint16 version); |
| 99 | + emit ContractDeployed(newContractAddress); |
| 100 | + |
| 101 | + return newContractAddress; |
| 102 | + } |
| 103 | + |
| 104 | + /// @notice When using CREATE2 the deployer includes the init data and value in the salt so that callers |
| 105 | + /// can be sure that wherever they encourter this address it was initialized with the same data and value |
| 106 | + /// @param salt A user supplied salt |
| 107 | + /// @param initData The init data that will be used to init the deployed contract |
| 108 | + function initSalt(bytes32 salt, bytes calldata initData) public pure returns (bytes32) { |
| 109 | + return keccak256(abi.encodePacked(salt, initData)); |
| 110 | + } |
| 111 | + |
| 112 | + /// @notice Checks whether a contract requires activation |
| 113 | + function requiresActivation( |
| 114 | + address addr |
| 115 | + ) public view returns (bool) { |
| 116 | + // currently codeHashVersion returns an error when codeHashVersion != stylus version |
| 117 | + // so we do a try/catch to to check it |
| 118 | + uint16 codeHashVersion; |
| 119 | + try ARB_WASM.codehashVersion(addr.codehash) returns (uint16 version) { |
| 120 | + codeHashVersion = version; |
| 121 | + } catch { |
| 122 | + // stylus version is always >= 1 |
| 123 | + codeHashVersion = 0; |
| 124 | + } |
| 125 | + |
| 126 | + // due to the bug in ARB_WASM.codeHashVersion we know that codeHashVersion will either be 0 or the current |
| 127 | + // version. We still check that is not equal to the stylusVersion |
| 128 | + return codeHashVersion != ARB_WASM.stylusVersion(); |
| 129 | + } |
| 130 | + |
| 131 | + /// @notice Deploy the a contract with the supplied bytecode. |
| 132 | + /// Will create2 if the supplied salt is non zero |
| 133 | + function deployContract(bytes memory bytecode, bytes32 salt) internal returns (address) { |
| 134 | + if (bytecode.length == 0) { |
| 135 | + revert EmptyBytecode(); |
| 136 | + } |
| 137 | + |
| 138 | + address newContractAddress; |
| 139 | + if (salt != 0) { |
| 140 | + assembly { |
| 141 | + newContractAddress := create2(0, add(bytecode, 0x20), mload(bytecode), salt) |
| 142 | + } |
| 143 | + } else { |
| 144 | + assembly { |
| 145 | + newContractAddress := create(0, add(bytecode, 0x20), mload(bytecode)) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // bubble up the revert if there was one |
| 150 | + assembly { |
| 151 | + if and(iszero(newContractAddress), not(iszero(returndatasize()))) { |
| 152 | + let p := mload(0x40) |
| 153 | + returndatacopy(p, 0, returndatasize()) |
| 154 | + revert(p, returndatasize()) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (newContractAddress == address(0)) { |
| 159 | + revert ContractDeploymentError(bytecode); |
| 160 | + } |
| 161 | + |
| 162 | + return newContractAddress; |
| 163 | + } |
| 164 | +} |
0 commit comments