|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Test, console2} from "forge-std/Test.sol"; |
| 5 | +import {Passage} from "../src/Passage.sol"; |
| 6 | +import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; |
| 7 | + |
| 8 | +contract TestERC20 is ERC20 { |
| 9 | + constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) {} |
| 10 | + |
| 11 | + function mint(address recipient, uint256 amount) external { |
| 12 | + _mint(recipient, amount); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +contract ZenithTest is Test { |
| 17 | + Passage public target; |
| 18 | + address token; |
| 19 | + address newToken; |
| 20 | + uint256 chainId = 3; |
| 21 | + address recipient = address(0x123); |
| 22 | + uint256 amount = 200; |
| 23 | + |
| 24 | + address to = address(0x01); |
| 25 | + bytes data = abi.encodeWithSelector(ERC20.transfer.selector, recipient, amount); |
| 26 | + uint256 value = 100; |
| 27 | + uint256 gas = 10_000_000; |
| 28 | + uint256 maxFeePerGas = 50; |
| 29 | + |
| 30 | + uint256 tokenAdminKey = 123; |
| 31 | + |
| 32 | + event Enter(uint256 indexed rollupChainId, address indexed rollupRecipient, uint256 amount); |
| 33 | + |
| 34 | + event EnterToken( |
| 35 | + uint256 indexed rollupChainId, address indexed rollupRecipient, address indexed token, uint256 amount |
| 36 | + ); |
| 37 | + |
| 38 | + event Transact( |
| 39 | + uint256 indexed rollupChainId, |
| 40 | + address indexed sender, |
| 41 | + address indexed to, |
| 42 | + bytes data, |
| 43 | + uint256 value, |
| 44 | + uint256 gas, |
| 45 | + uint256 maxFeePerGas |
| 46 | + ); |
| 47 | + |
| 48 | + event Withdrawal(address indexed token, address indexed recipient, uint256 amount); |
| 49 | + |
| 50 | + event EnterConfigured(address indexed token, bool indexed canEnter); |
| 51 | + |
| 52 | + function setUp() public { |
| 53 | + // deploy token one, configured at deploy time |
| 54 | + token = address(new TestERC20("hi", "HI")); |
| 55 | + TestERC20(token).mint(address(this), amount * 10000); |
| 56 | + |
| 57 | + // deploy target |
| 58 | + address[] memory initialEnterTokens = new address[](1); |
| 59 | + initialEnterTokens[0] = token; |
| 60 | + target = new Passage(block.chainid + 1, address(this), initialEnterTokens); |
| 61 | + TestERC20(token).approve(address(target), amount * 10000); |
| 62 | + |
| 63 | + // deploy token two, don't configure |
| 64 | + newToken = address(new TestERC20("bye", "BYE")); |
| 65 | + TestERC20(newToken).mint(address(this), amount * 10000); |
| 66 | + TestERC20(newToken).approve(address(target), amount * 10000); |
| 67 | + } |
| 68 | + |
| 69 | + function test_setUp() public { |
| 70 | + assertEq(target.defaultRollupChainId(), block.chainid + 1); |
| 71 | + assertEq(target.tokenAdmin(), address(this)); |
| 72 | + assertTrue(target.canEnter(token)); |
| 73 | + assertFalse(target.canEnter(newToken)); |
| 74 | + } |
| 75 | + |
| 76 | + function test_onlyTokenAdmin() public { |
| 77 | + vm.startPrank(address(0x01)); |
| 78 | + vm.expectRevert(Passage.OnlyTokenAdmin.selector); |
| 79 | + target.withdraw(token, recipient, amount); |
| 80 | + |
| 81 | + vm.expectRevert(Passage.OnlyTokenAdmin.selector); |
| 82 | + target.configureEnter(token, true); |
| 83 | + } |
| 84 | + |
| 85 | + function test_disallowedEnter() public { |
| 86 | + vm.expectRevert(abi.encodeWithSelector(Passage.DisallowedEnter.selector, newToken)); |
| 87 | + target.enterToken(recipient, newToken, amount); |
| 88 | + } |
| 89 | + |
| 90 | + function test_configureEnter() public { |
| 91 | + // enter not allowed by default |
| 92 | + assertFalse(target.canEnter(newToken)); |
| 93 | + vm.expectRevert(abi.encodeWithSelector(Passage.DisallowedEnter.selector, newToken)); |
| 94 | + target.enterToken(chainId, recipient, newToken, amount); |
| 95 | + |
| 96 | + // allow enter |
| 97 | + vm.expectEmit(); |
| 98 | + emit EnterConfigured(newToken, true); |
| 99 | + target.configureEnter(newToken, true); |
| 100 | + |
| 101 | + // enter is allowed |
| 102 | + assertTrue(target.canEnter(newToken)); |
| 103 | + vm.expectEmit(); |
| 104 | + emit EnterToken(chainId, recipient, newToken, amount); |
| 105 | + target.enterToken(chainId, recipient, newToken, amount); |
| 106 | + |
| 107 | + // disallow enter |
| 108 | + vm.expectEmit(); |
| 109 | + emit EnterConfigured(newToken, false); |
| 110 | + target.configureEnter(newToken, false); |
| 111 | + |
| 112 | + // enter not allowed again |
| 113 | + assertFalse(target.canEnter(newToken)); |
| 114 | + vm.expectRevert(abi.encodeWithSelector(Passage.DisallowedEnter.selector, newToken)); |
| 115 | + target.enterToken(chainId, recipient, newToken, amount); |
| 116 | + } |
| 117 | + |
| 118 | + function test_receive() public { |
| 119 | + vm.expectEmit(); |
| 120 | + emit Enter(target.defaultRollupChainId(), address(this), amount); |
| 121 | + address(target).call{value: amount}(""); |
| 122 | + } |
| 123 | + |
| 124 | + function test_fallback() public { |
| 125 | + vm.expectEmit(); |
| 126 | + emit Enter(target.defaultRollupChainId(), address(this), amount); |
| 127 | + address(target).call{value: amount}("0xabcd"); |
| 128 | + } |
| 129 | + |
| 130 | + function test_enter() public { |
| 131 | + vm.expectEmit(); |
| 132 | + emit Enter(chainId, recipient, amount); |
| 133 | + target.enter{value: amount}(chainId, recipient); |
| 134 | + } |
| 135 | + |
| 136 | + function test_enter_defaultChain() public { |
| 137 | + vm.expectEmit(); |
| 138 | + emit Enter(target.defaultRollupChainId(), recipient, amount); |
| 139 | + target.enter{value: amount}(recipient); |
| 140 | + } |
| 141 | + |
| 142 | + function test_enterToken() public { |
| 143 | + vm.expectEmit(); |
| 144 | + emit EnterToken(chainId, recipient, token, amount); |
| 145 | + vm.expectCall( |
| 146 | + token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount) |
| 147 | + ); |
| 148 | + target.enterToken(chainId, recipient, token, amount); |
| 149 | + } |
| 150 | + |
| 151 | + function test_enterToken_defaultChain() public { |
| 152 | + vm.expectEmit(); |
| 153 | + emit EnterToken(target.defaultRollupChainId(), recipient, token, amount); |
| 154 | + vm.expectCall( |
| 155 | + token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount) |
| 156 | + ); |
| 157 | + target.enterToken(recipient, token, amount); |
| 158 | + } |
| 159 | + |
| 160 | + function test_transact() public { |
| 161 | + vm.expectEmit(); |
| 162 | + emit Transact(chainId, address(this), to, data, value, gas, maxFeePerGas); |
| 163 | + target.transact(chainId, to, data, value, gas, maxFeePerGas); |
| 164 | + |
| 165 | + vm.expectEmit(); |
| 166 | + emit Enter(chainId, address(this), amount); |
| 167 | + target.transact{value: amount}(chainId, to, data, value, gas, maxFeePerGas); |
| 168 | + } |
| 169 | + |
| 170 | + function test_transact_defaultChain() public { |
| 171 | + vm.expectEmit(); |
| 172 | + emit Transact(target.defaultRollupChainId(), address(this), to, data, value, gas, maxFeePerGas); |
| 173 | + target.transact(to, data, value, gas, maxFeePerGas); |
| 174 | + |
| 175 | + vm.expectEmit(); |
| 176 | + emit Enter(target.defaultRollupChainId(), address(this), amount); |
| 177 | + target.transact{value: amount}(to, data, value, gas, maxFeePerGas); |
| 178 | + } |
| 179 | + |
| 180 | + function test_enterTransact() public { |
| 181 | + vm.expectEmit(); |
| 182 | + emit Transact(chainId, address(this), to, data, value, gas, maxFeePerGas); |
| 183 | + target.enterTransact(chainId, recipient, to, data, value, gas, maxFeePerGas); |
| 184 | + |
| 185 | + vm.expectEmit(); |
| 186 | + emit Enter(chainId, recipient, amount); |
| 187 | + target.enterTransact{value: amount}(chainId, recipient, to, data, value, gas, maxFeePerGas); |
| 188 | + } |
| 189 | + |
| 190 | + function test_withdraw() public { |
| 191 | + TestERC20(token).mint(address(target), amount); |
| 192 | + |
| 193 | + vm.expectEmit(); |
| 194 | + emit Withdrawal(token, recipient, amount); |
| 195 | + vm.expectCall(token, abi.encodeWithSelector(ERC20.transfer.selector, recipient, amount)); |
| 196 | + target.withdraw(token, recipient, amount); |
| 197 | + } |
| 198 | +} |
0 commit comments