Skip to content

Commit 5f04cac

Browse files
committed
WIP: refactor tests with SignetStd for easy local/fork testing
1 parent c9c039b commit 5f04cac

File tree

8 files changed

+436
-292
lines changed

8 files changed

+436
-292
lines changed

test/Helpers.t.sol

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@ pragma solidity 0.8.26;
44
// system contracts
55
import {Zenith} from "../src/Zenith.sol";
66
import {UsesPermit2} from "../src/UsesPermit2.sol";
7+
import {RollupOrders} from "../src/orders/RollupOrders.sol";
78
// deps
8-
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
9-
import {ERC20Burnable} from "openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol";
109
import {ISignatureTransfer} from "permit2/src/interfaces/ISignatureTransfer.sol";
1110
import {Test, console2} from "forge-std/Test.sol";
11+
import {SignetStdTest, TestERC20} from "./SignetStdTest.t.sol";
1212

13-
contract TestERC20 is ERC20Burnable {
14-
constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) {}
15-
16-
function mint(address recipient, uint256 amount) external {
17-
_mint(recipient, amount);
18-
}
19-
}
20-
21-
contract Permit2Helpers is Test {
22-
address permit2Contract;
23-
13+
contract Permit2Helpers is SignetStdTest {
2414
/// @notice the address signing the Permit messages and its pk
2515
uint256 ownerKey = 123;
2616
address owner = vm.addr(ownerKey);
2717

2818
// permit consts
2919
UsesPermit2.Witness witness;
3020

21+
// single permit
22+
UsesPermit2.Permit2 singlePermit;
23+
ISignatureTransfer.SignatureTransferDetails singleTransferDetails;
24+
25+
// batch permit
26+
UsesPermit2.Permit2Batch batchPermit;
27+
ISignatureTransfer.SignatureTransferDetails[] batchTransferDetails;
28+
3129
bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
3230
uint256 private immutable _CACHED_CHAIN_ID;
3331

@@ -50,18 +48,6 @@ contract Permit2Helpers is Test {
5048
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME);
5149
}
5250

53-
function _setUpPermit2(address token, uint256 amount) internal {
54-
vm.label(owner, "owner");
55-
56-
// setup permit2 contract
57-
permit2Contract = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
58-
vm.label(address(permit2Contract), "permit2");
59-
60-
// approve permit2
61-
vm.prank(owner);
62-
TestERC20(token).approve(address(permit2Contract), amount * 10000);
63-
}
64-
6551
/// @notice given a Permit and a Witness, produce a signature from the `owner`
6652
function signPermit(
6753
uint256 signingKey,
@@ -154,7 +140,7 @@ contract Permit2Helpers is Test {
154140

155141
/// @notice Builds a domain separator using the current chainId and contract address.
156142
function _buildDomainSeparator(bytes32 typeHash, bytes32 nameHash) private view returns (bytes32) {
157-
return keccak256(abi.encode(typeHash, nameHash, block.chainid, permit2Contract));
143+
return keccak256(abi.encode(typeHash, nameHash, block.chainid, PERMIT2));
158144
}
159145

160146
/// @notice Creates an EIP-712 typed data hash
@@ -187,22 +173,3 @@ interface IBatchPermit {
187173
bytes calldata /*signature*/
188174
) external;
189175
}
190-
191-
contract HelpersTest is Test {
192-
Zenith public target;
193-
194-
function setUp() public {
195-
vm.createSelectFork("https://rpc.holesky.ethpandaops.io");
196-
target = new Zenith(0x29403F107781ea45Bf93710abf8df13F67f2008f);
197-
}
198-
199-
function check_signature() public {
200-
bytes32 hash = 0xdcd0af9a45fa82dcdd1e4f9ef703d8cd459b6950c0638154c67117e86facf9c1;
201-
uint8 v = 28;
202-
bytes32 r = 0xb89764d107f812dbbebb925711b320d336ff8d03f08570f051123df86334f3f5;
203-
bytes32 s = 0x394cd592577ce6307154045607b9b18ecc1de0eb636e996981477c2d9b1a7675;
204-
address signer = ecrecover(hash, v, r, s);
205-
vm.label(signer, "recovered signer");
206-
assertEq(signer, 0x5b0517Dc94c413a5871536872605522E54C85a03);
207-
}
208-
}

test/Orders.t.sol

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import {OrderOrigin} from "../src/orders/OrderOrigin.sol";
99
import {TestERC20} from "./Helpers.t.sol";
1010
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
1111
import {Test, console2} from "forge-std/Test.sol";
12+
import {SignetStdTest} from "./SignetStdTest.t.sol";
1213

13-
contract OrdersTest is Test {
14+
contract OrdersTest is SignetStdTest {
1415
RollupOrders public target;
16+
1517
IOrders.Input[] public inputs;
1618
IOrders.Output[] public outputs;
1719

1820
address token;
1921
address token2;
20-
uint32 chainId = 3;
22+
2123
address recipient = address(0x123);
2224
uint256 amount = 200;
2325
uint256 deadline = block.timestamp;
@@ -29,23 +31,25 @@ contract OrdersTest is Test {
2931
event Sweep(address indexed recipient, address indexed token, uint256 amount);
3032

3133
function setUp() public virtual {
32-
target = new RollupOrders(address(0));
34+
target = ROLLUP_ORDERS;
3335

34-
// setup token
35-
token = address(new TestERC20("hi", "HI"));
36+
// setup first token
37+
token = address(ROLLUP_WBTC);
38+
vm.prank(ROLLUP_MINTER);
3639
TestERC20(token).mint(address(this), amount * 10000);
3740
TestERC20(token).approve(address(target), amount * 10000);
3841

3942
// setup second token
40-
token2 = address(new TestERC20("bye", "BYE"));
43+
token2 = address(ROLLUP_WETH);
44+
vm.prank(ROLLUP_MINTER);
4145
TestERC20(token2).mint(address(this), amount * 10000);
4246
TestERC20(token2).approve(address(target), amount * 10000);
4347

4448
// setup simple Order Inputs/Outputs
4549
IOrders.Input memory input = IOrders.Input(token, amount);
4650
inputs.push(input);
4751

48-
IOrders.Output memory output = IOrders.Output(token, amount, recipient, chainId);
52+
IOrders.Output memory output = IOrders.Output(token, amount, recipient, ROLLUP_CHAIN_ID);
4953
outputs.push(output);
5054
}
5155

@@ -143,9 +147,6 @@ contract OrdersTest is Test {
143147
}
144148

145149
function test_sweepETH() public {
146-
// set self as Builder
147-
vm.coinbase(address(this));
148-
149150
// initiate an ETH order
150151
inputs[0].token = address(0);
151152
target.initiate{value: amount}(deadline, inputs, outputs);
@@ -161,9 +162,6 @@ contract OrdersTest is Test {
161162
}
162163

163164
function test_sweepERC20() public {
164-
// set self as Builder
165-
vm.coinbase(address(this));
166-
167165
// send ERC20 to the contract
168166
TestERC20(token).transfer(address(target), amount);
169167

@@ -194,7 +192,7 @@ contract OrdersTest is Test {
194192

195193
function test_fill_both() public {
196194
// add ETH output
197-
outputs.push(IOrders.Output(address(0), amount * 2, recipient, chainId));
195+
outputs.push(IOrders.Output(address(0), amount * 2, recipient, ROLLUP_CHAIN_ID));
198196

199197
// expect Outputs are filled, ERC20 is transferred
200198
vm.expectEmit();
@@ -211,7 +209,7 @@ contract OrdersTest is Test {
211209
// change first output to ETH
212210
outputs[0].token = address(0);
213211
// add second ETH oputput
214-
outputs.push(IOrders.Output(address(0), amount * 2, recipient, chainId));
212+
outputs.push(IOrders.Output(address(0), amount * 2, recipient, ROLLUP_CHAIN_ID));
215213

216214
// expect Order event is initiated
217215
vm.expectEmit();
@@ -226,7 +224,7 @@ contract OrdersTest is Test {
226224
// change first output to ETH
227225
outputs[0].token = address(0);
228226
// add second ETH output
229-
outputs.push(IOrders.Output(address(0), 1, recipient, chainId));
227+
outputs.push(IOrders.Output(address(0), 1, recipient, ROLLUP_CHAIN_ID));
230228

231229
// total ETH outputs should be `amount` + 1; function should underflow only sending `amount`
232230
vm.expectRevert();

test/Passage.t.sol

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,47 @@ import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
1010
import {ERC20Burnable} from "openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol";
1111
import {Address} from "openzeppelin-contracts/contracts/utils/Address.sol";
1212
import {Test, console2} from "forge-std/Test.sol";
13+
import {SignetStdTest} from "./SignetStdTest.t.sol";
14+
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
1315

14-
contract PassageTest is Test {
16+
contract PassageTest is SignetStdTest {
1517
using Address for address payable;
1618

1719
Passage public target;
1820
address token;
1921
address newToken;
20-
uint256 chainId = 3;
2122
address recipient = address(0x123);
2223
uint256 amount = 200;
2324

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-
3025
event Enter(uint256 indexed rollupChainId, address indexed rollupRecipient, uint256 amount);
3126

3227
event EnterToken(
3328
uint256 indexed rollupChainId, address indexed rollupRecipient, address indexed token, uint256 amount
3429
);
3530

36-
event Transact(
37-
uint256 indexed rollupChainId,
38-
address indexed sender,
39-
address indexed to,
40-
bytes data,
41-
uint256 value,
42-
uint256 gas,
43-
uint256 maxFeePerGas
44-
);
31+
event EnterConfigured(address indexed token, bool indexed canEnter);
4532

4633
event Withdrawal(address indexed token, address indexed recipient, uint256 amount);
4734

48-
event EnterConfigured(address indexed token, bool indexed canEnter);
49-
5035
function setUp() public {
51-
// deploy token one, configured at deploy time
52-
token = address(new TestERC20("hi", "HI"));
53-
TestERC20(token).mint(address(this), amount * 10000);
54-
5536
// deploy target
56-
address[] memory initialEnterTokens = new address[](1);
57-
initialEnterTokens[0] = token;
58-
target = new Passage(block.chainid + 1, address(this), initialEnterTokens, address(0));
37+
target = HOST_PASSAGE;
38+
39+
// setup token
40+
token = address(HOST_WETH);
41+
// mint WETH by sending ETH
42+
payable(token).sendValue(amount * 10000);
5943
TestERC20(token).approve(address(target), amount * 10000);
6044

61-
// deploy token two, don't configure
62-
newToken = address(new TestERC20("bye", "BYE"));
45+
// deploy new token that's not configured on Passage
46+
newToken = address(new TestERC20("bye", "BYE", 18));
6347
TestERC20(newToken).mint(address(this), amount * 10000);
6448
TestERC20(newToken).approve(address(target), amount * 10000);
6549
}
6650

6751
function test_setUp() public {
68-
assertEq(target.defaultRollupChainId(), block.chainid + 1);
69-
assertEq(target.tokenAdmin(), address(this));
52+
assertEq(target.defaultRollupChainId(), ROLLUP_CHAIN_ID);
53+
assertEq(target.tokenAdmin(), TOKEN_ADMIN);
7054
assertTrue(target.canEnter(token));
7155
assertFalse(target.canEnter(newToken));
7256
}
@@ -89,28 +73,32 @@ contract PassageTest is Test {
8973
// enter not allowed by default
9074
assertFalse(target.canEnter(newToken));
9175
vm.expectRevert(abi.encodeWithSelector(Passage.DisallowedEnter.selector, newToken));
92-
target.enterToken(chainId, recipient, newToken, amount);
76+
target.enterToken(ROLLUP_CHAIN_ID, recipient, newToken, amount);
9377

9478
// allow enter
79+
vm.startPrank(TOKEN_ADMIN);
9580
vm.expectEmit();
9681
emit EnterConfigured(newToken, true);
9782
target.configureEnter(newToken, true);
83+
vm.stopPrank();
9884

9985
// enter is allowed
10086
assertTrue(target.canEnter(newToken));
10187
vm.expectEmit();
102-
emit EnterToken(chainId, recipient, newToken, amount);
103-
target.enterToken(chainId, recipient, newToken, amount);
88+
emit EnterToken(ROLLUP_CHAIN_ID, recipient, newToken, amount);
89+
target.enterToken(ROLLUP_CHAIN_ID, recipient, newToken, amount);
10490

10591
// disallow enter
92+
vm.startPrank(TOKEN_ADMIN);
10693
vm.expectEmit();
10794
emit EnterConfigured(newToken, false);
10895
target.configureEnter(newToken, false);
96+
vm.stopPrank();
10997

11098
// enter not allowed again
11199
assertFalse(target.canEnter(newToken));
112100
vm.expectRevert(abi.encodeWithSelector(Passage.DisallowedEnter.selector, newToken));
113-
target.enterToken(chainId, recipient, newToken, amount);
101+
target.enterToken(ROLLUP_CHAIN_ID, recipient, newToken, amount);
114102
}
115103

116104
function test_receive() public {
@@ -127,8 +115,8 @@ contract PassageTest is Test {
127115

128116
function test_enter() public {
129117
vm.expectEmit();
130-
emit Enter(chainId, recipient, amount);
131-
target.enter{value: amount}(chainId, recipient);
118+
emit Enter(ROLLUP_CHAIN_ID, recipient, amount);
119+
target.enter{value: amount}(ROLLUP_CHAIN_ID, recipient);
132120
}
133121

134122
function test_enter_defaultChain() public {
@@ -139,11 +127,11 @@ contract PassageTest is Test {
139127

140128
function test_enterToken() public {
141129
vm.expectEmit();
142-
emit EnterToken(chainId, recipient, token, amount);
130+
emit EnterToken(ROLLUP_CHAIN_ID, recipient, token, amount);
143131
vm.expectCall(
144132
token, abi.encodeWithSelector(ERC20.transferFrom.selector, address(this), address(target), amount)
145133
);
146-
target.enterToken(chainId, recipient, token, amount);
134+
target.enterToken(ROLLUP_CHAIN_ID, recipient, token, amount);
147135
}
148136

149137
function test_enterToken_defaultChain() public {
@@ -156,16 +144,17 @@ contract PassageTest is Test {
156144
}
157145

158146
function test_withdraw() public {
159-
TestERC20(token).mint(address(target), amount);
147+
IERC20(token).transfer(address(target), amount);
160148

149+
vm.startPrank(TOKEN_ADMIN);
161150
vm.expectEmit();
162151
emit Withdrawal(token, recipient, amount);
163152
vm.expectCall(token, abi.encodeWithSelector(ERC20.transfer.selector, recipient, amount));
164153
target.withdraw(token, recipient, amount);
165154
}
166155
}
167156

168-
contract RollupPassageTest is Test {
157+
contract RollupPassageTest is SignetStdTest {
169158
using Address for address payable;
170159

171160
RollupPassage public target;
@@ -178,11 +167,12 @@ contract RollupPassageTest is Test {
178167
event ExitToken(address indexed hostRecipient, address indexed token, uint256 amount);
179168

180169
function setUp() public virtual {
181-
// deploy target
182-
target = new RollupPassage(address(0));
170+
// setup target
171+
target = ROLLUP_PASSAGE;
183172

184-
// deploy token
185-
token = address(new TestERC20("hi", "HI"));
173+
// setup token
174+
token = address(ROLLUP_WETH);
175+
vm.prank(ROLLUP_MINTER);
186176
TestERC20(token).mint(address(this), amount * 10000);
187177
TestERC20(token).approve(address(target), amount * 10000);
188178
}

0 commit comments

Comments
 (0)