|  | 
|  | 1 | +// SPDX-License-Identifier: UNLICENSED | 
|  | 2 | +pragma solidity ^0.8.24; | 
|  | 3 | + | 
|  | 4 | +import {Test, console2} from "forge-std/Test.sol"; | 
|  | 5 | +import {RollupOrders, Input, Output, OrderOrigin} from "../src/Orders.sol"; | 
|  | 6 | +import {TestERC20} from "./Helpers.t.sol"; | 
|  | 7 | +import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; | 
|  | 8 | + | 
|  | 9 | +contract OrdersTest is Test { | 
|  | 10 | +    RollupOrders public target; | 
|  | 11 | +    Input[] public inputs; | 
|  | 12 | +    Output[] public outputs; | 
|  | 13 | + | 
|  | 14 | +    mapping(address => bool) isToken; | 
|  | 15 | + | 
|  | 16 | +    address token; | 
|  | 17 | +    uint32 chainId = 3; | 
|  | 18 | +    address recipient = address(0x123); | 
|  | 19 | +    uint256 amount = 200; | 
|  | 20 | +    uint256 deadline = block.timestamp; | 
|  | 21 | + | 
|  | 22 | +    event OutputFilled(uint256 indexed originChainId, address indexed recipient, address indexed token, uint256 amount); | 
|  | 23 | + | 
|  | 24 | +    event Order(uint256 deadline, Input[] inputs, Output[] outputs); | 
|  | 25 | + | 
|  | 26 | +    event Sweep(address indexed recipient, address indexed token, uint256 amount); | 
|  | 27 | + | 
|  | 28 | +    function setUp() public { | 
|  | 29 | +        target = new RollupOrders(); | 
|  | 30 | + | 
|  | 31 | +        // setup token | 
|  | 32 | +        token = address(new TestERC20("hi", "HI")); | 
|  | 33 | +        TestERC20(token).mint(address(this), amount * 10000); | 
|  | 34 | +        TestERC20(token).approve(address(target), amount * 10000); | 
|  | 35 | +        isToken[token] = true; | 
|  | 36 | + | 
|  | 37 | +        // setup Order Inputs/Outputs | 
|  | 38 | +        Input memory input = Input(token, amount); | 
|  | 39 | +        inputs.push(input); | 
|  | 40 | + | 
|  | 41 | +        Output memory output = Output(token, amount, recipient, chainId); | 
|  | 42 | +        outputs.push(output); | 
|  | 43 | +    } | 
|  | 44 | + | 
|  | 45 | +    // input ERC20 | 
|  | 46 | +    function test_initiate_ERC20() public { | 
|  | 47 | +        // expect Order event is initiated, ERC20 is transferred | 
|  | 48 | +        vm.expectEmit(); | 
|  | 49 | +        emit Order(deadline, inputs, outputs); | 
|  | 50 | +        vm.expectCall( | 
|  | 51 | +            token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount) | 
|  | 52 | +        ); | 
|  | 53 | +        target.initiate(deadline, inputs, outputs); | 
|  | 54 | +    } | 
|  | 55 | + | 
|  | 56 | +    // input ETH | 
|  | 57 | +    function test_initiate_ETH() public { | 
|  | 58 | +        // change input to ETH | 
|  | 59 | +        inputs[0].token = address(0); | 
|  | 60 | + | 
|  | 61 | +        // expect Order event is initiated | 
|  | 62 | +        vm.expectEmit(); | 
|  | 63 | +        emit Order(deadline, inputs, outputs); | 
|  | 64 | +        target.initiate{value: amount}(deadline, inputs, outputs); | 
|  | 65 | + | 
|  | 66 | +        // ETH is held in target contract | 
|  | 67 | +        assertEq(address(target).balance, amount); | 
|  | 68 | +    } | 
|  | 69 | + | 
|  | 70 | +    // input ETH and ERC20 | 
|  | 71 | +    function test_initiate_both() public { | 
|  | 72 | +        // add ETH input | 
|  | 73 | +        inputs.push(Input(address(0), amount)); | 
|  | 74 | + | 
|  | 75 | +        // expect Order event is initiated, ERC20 is transferred | 
|  | 76 | +        vm.expectEmit(); | 
|  | 77 | +        emit Order(deadline, inputs, outputs); | 
|  | 78 | +        vm.expectCall( | 
|  | 79 | +            token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount) | 
|  | 80 | +        ); | 
|  | 81 | +        target.initiate{value: amount}(deadline, inputs, outputs); | 
|  | 82 | + | 
|  | 83 | +        // ETH is held in target contract | 
|  | 84 | +        assertEq(address(target).balance, amount); | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    // input multiple ERC20s | 
|  | 88 | +    function test_initiate_multiERC20() public { | 
|  | 89 | +        // setup second token | 
|  | 90 | +        address token2 = address(new TestERC20("bye", "BYE")); | 
|  | 91 | +        TestERC20(token2).mint(address(this), amount * 10000); | 
|  | 92 | +        TestERC20(token2).approve(address(target), amount * 10000); | 
|  | 93 | + | 
|  | 94 | +        // add second token input | 
|  | 95 | +        inputs.push(Input(token2, amount * 2)); | 
|  | 96 | + | 
|  | 97 | +        // expect Order event is initiated, ERC20 is transferred | 
|  | 98 | +        vm.expectEmit(); | 
|  | 99 | +        emit Order(deadline, inputs, outputs); | 
|  | 100 | +        vm.expectCall( | 
|  | 101 | +            token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount) | 
|  | 102 | +        ); | 
|  | 103 | +        vm.expectCall( | 
|  | 104 | +            token2, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount * 2) | 
|  | 105 | +        ); | 
|  | 106 | +        target.initiate(deadline, inputs, outputs); | 
|  | 107 | +    } | 
|  | 108 | + | 
|  | 109 | +    // input multiple ETH inputs | 
|  | 110 | +    function test_initiate_multiETH() public { | 
|  | 111 | +        // change first input to ETH | 
|  | 112 | +        inputs[0].token = address(0); | 
|  | 113 | +        // add second ETH input | 
|  | 114 | +        inputs.push(Input(address(0), amount * 2)); | 
|  | 115 | + | 
|  | 116 | +        // expect Order event is initiated | 
|  | 117 | +        vm.expectEmit(); | 
|  | 118 | +        emit Order(deadline, inputs, outputs); | 
|  | 119 | +        target.initiate{value: amount * 3}(deadline, inputs, outputs); | 
|  | 120 | + | 
|  | 121 | +        // ETH is held in target contract | 
|  | 122 | +        assertEq(address(target).balance, amount * 3); | 
|  | 123 | +    } | 
|  | 124 | + | 
|  | 125 | +    function test_initiate_underflowETH() public { | 
|  | 126 | +        // change first input to ETH | 
|  | 127 | +        inputs[0].token = address(0); | 
|  | 128 | +        // add second ETH input | 
|  | 129 | +        inputs.push(Input(address(0), 1)); | 
|  | 130 | + | 
|  | 131 | +        // total ETH inputs should be `amount` + 1; function should underflow only sending `amount` | 
|  | 132 | +        vm.expectRevert(); | 
|  | 133 | +        target.initiate{value: amount}(deadline, inputs, outputs); | 
|  | 134 | +    } | 
|  | 135 | + | 
|  | 136 | +    function test_orderExpired() public { | 
|  | 137 | +        vm.warp(block.timestamp + 1); | 
|  | 138 | + | 
|  | 139 | +        vm.expectRevert(OrderOrigin.OrderExpired.selector); | 
|  | 140 | +        target.initiate(deadline, inputs, outputs); | 
|  | 141 | +    } | 
|  | 142 | + | 
|  | 143 | +    function test_sweep_ETH() public { | 
|  | 144 | +        // set self as Builder | 
|  | 145 | +        vm.coinbase(address(this)); | 
|  | 146 | + | 
|  | 147 | +        // initiate an ETH order | 
|  | 148 | +        inputs[0].token = address(0); | 
|  | 149 | +        target.initiate{value: amount}(deadline, inputs, outputs); | 
|  | 150 | + | 
|  | 151 | +        assertEq(address(target).balance, amount); | 
|  | 152 | + | 
|  | 153 | +        // sweep ETH | 
|  | 154 | +        vm.expectEmit(); | 
|  | 155 | +        emit Sweep(recipient, address(0), amount); | 
|  | 156 | +        target.sweep(recipient, address(0)); | 
|  | 157 | + | 
|  | 158 | +        assertEq(recipient.balance, amount); | 
|  | 159 | +    } | 
|  | 160 | + | 
|  | 161 | +    function test_sweep_ERC20() public { | 
|  | 162 | +        // set self as Builder | 
|  | 163 | +        vm.coinbase(address(this)); | 
|  | 164 | + | 
|  | 165 | +        // send ERC20 to the contract | 
|  | 166 | +        TestERC20(token).transfer(address(target), amount); | 
|  | 167 | + | 
|  | 168 | +        // sweep ERC20 | 
|  | 169 | +        vm.expectEmit(); | 
|  | 170 | +        emit Sweep(recipient, token, amount); | 
|  | 171 | +        vm.expectCall(token, abi.encodeWithSelector(ERC20.transfer.selector, recipient, amount)); | 
|  | 172 | +        target.sweep(recipient, token); | 
|  | 173 | +    } | 
|  | 174 | + | 
|  | 175 | +    function test_onlyBuilder() public { | 
|  | 176 | +        vm.expectRevert(OrderOrigin.OnlyBuilder.selector); | 
|  | 177 | +        target.sweep(recipient, token); | 
|  | 178 | +    } | 
|  | 179 | +} | 
0 commit comments