|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity >=0.8.0; |
| 3 | + |
| 4 | +interface ArbWasm { |
| 5 | + /// @notice Activate a wasm program |
| 6 | + /// @param program the program to activate |
| 7 | + /// @return version the stylus version the program was activated against |
| 8 | + /// @return dataFee the data fee paid to store the activated program |
| 9 | + function activateProgram( |
| 10 | + address program |
| 11 | + ) external payable returns (uint16 version, uint256 dataFee); |
| 12 | +} |
| 13 | + |
| 14 | +contract C { |
| 15 | + function test_create2() public payable { |
| 16 | + Greeter greeter = new Greeter(); |
| 17 | + print("greeter = {}".format(address(greeter))); |
| 18 | + |
| 19 | + ArbWasm arbWasm = ArbWasm(address(0x71)); |
| 20 | + (uint16 version, uint256 dataFee) = arbWasm.activateProgram{ |
| 21 | + value: msg.value |
| 22 | + }(address(greeter)); |
| 23 | + print("version = {}".format(version)); |
| 24 | + print("dataFee = {}".format(dataFee)); |
| 25 | + |
| 26 | + greeter.greet(); |
| 27 | + |
| 28 | + bytes initCode = contractDeploymentCalldata(address(greeter).code); |
| 29 | + print("initCode = {}".format(initCode)); |
| 30 | + |
| 31 | + Greeter greeter0; |
| 32 | + assembly { |
| 33 | + greeter0 := create2( |
| 34 | + 0, |
| 35 | + initCode, |
| 36 | + 0, // codeLen - ignored |
| 37 | + 0 // salt |
| 38 | + ) |
| 39 | + } |
| 40 | + print("greeter0 = {}".format(address(greeter0))); |
| 41 | + greeter0.greet(); |
| 42 | + |
| 43 | + Greeter greeter1; |
| 44 | + assembly { |
| 45 | + greeter1 := create2( |
| 46 | + 0, |
| 47 | + initCode, |
| 48 | + 0, // codeLen - ignored |
| 49 | + 1 // salt |
| 50 | + ) |
| 51 | + } |
| 52 | + print("greeter1 = {}".format(address(greeter1))); |
| 53 | + greeter1.greet(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +contract Greeter { |
| 58 | + function greet() public view { |
| 59 | + print("Greetings from 0x{}!".format(this)); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function contractDeploymentCalldata(bytes code) pure returns (bytes) { |
| 64 | + bytes memory deploy; |
| 65 | + uint256 codeLen = code.length; |
| 66 | + deploy.push(0x7f); // PUSH32 |
| 67 | + deploy = pushBytes(deploy, bytes32(codeLen)); |
| 68 | + deploy.push(0x80); // DUP1 |
| 69 | + deploy.push(0x60); // PUSH1 |
| 70 | + deploy.push(43); // prelude + version |
| 71 | + deploy.push(0x60); // PUSH1 |
| 72 | + deploy.push(0x00); |
| 73 | + deploy.push(0x39); // CODECOPY |
| 74 | + deploy.push(0x60); // PUSH1 |
| 75 | + deploy.push(0x00); |
| 76 | + deploy.push(0xf3); // RETURN |
| 77 | + deploy.push(0x00); // version |
| 78 | + deploy = pushBytes(deploy, code); |
| 79 | + return deploy; |
| 80 | +} |
| 81 | + |
| 82 | +function pushBytes(bytes memory xs, bytes memory ys) pure returns (bytes) { |
| 83 | + uint256 n = ys.length; |
| 84 | + for (uint256 i = 0; i < n; i++) { |
| 85 | + xs.push(ys[i]); |
| 86 | + } |
| 87 | + return xs; |
| 88 | +} |
0 commit comments