forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2TxFeeVault.t.sol
154 lines (122 loc) · 5.57 KB
/
L2TxFeeVault.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {DSTestPlus} from "solmate/test/utils/DSTestPlus.sol";
import {MockScrollMessenger} from "./mocks/MockScrollMessenger.sol";
import {L2TxFeeVault} from "../L2/predeploys/L2TxFeeVault.sol";
contract L2TxFeeVaultTest is DSTestPlus {
// events
event UpdateMessenger(address indexed oldMessenger, address indexed newMessenger);
event UpdateRecipient(address indexed oldRecipient, address indexed newRecipient);
event UpdateMinWithdrawAmount(uint256 oldMinWithdrawAmount, uint256 newMinWithdrawAmount);
MockScrollMessenger private messenger;
L2TxFeeVault private vault;
function setUp() public {
messenger = new MockScrollMessenger();
vault = new L2TxFeeVault(address(this), address(1), 10 ether);
vault.updateMessenger(address(messenger));
}
function testCantWithdrawBelowMinimum() public {
hevm.deal(address(vault), 9 ether);
hevm.expectRevert("FeeVault: withdrawal amount must be greater than minimum withdrawal amount");
vault.withdraw();
}
function testCantWithdrawAmountBelowMinimum(uint256 amount) public {
amount = bound(amount, 0 ether, 10 ether - 1);
hevm.deal(address(vault), 100 ether);
hevm.expectRevert("FeeVault: withdrawal amount must be greater than minimum withdrawal amount");
vault.withdraw(amount);
}
function testCantWithdrawMoreThanBalance(uint256 amount) public {
hevm.assume(amount >= 10 ether);
hevm.deal(address(vault), amount - 1);
hevm.expectRevert("FeeVault: insufficient balance to withdraw");
vault.withdraw(amount);
}
function testWithdrawOnce() public {
hevm.deal(address(vault), 11 ether);
vault.withdraw();
assertEq(address(messenger).balance, 11 ether);
assertEq(vault.totalProcessed(), 11 ether);
}
function testWithdrawAmountOnce(uint256 amount) public {
amount = bound(amount, 10 ether, 100 ether);
hevm.deal(address(vault), 100 ether);
vault.withdraw(amount);
assertEq(address(messenger).balance, amount);
assertEq(vault.totalProcessed(), amount);
assertEq(address(vault).balance, 100 ether - amount);
}
function testWithdrawTwice() public {
hevm.deal(address(vault), 11 ether);
vault.withdraw();
assertEq(address(messenger).balance, 11 ether);
assertEq(vault.totalProcessed(), 11 ether);
hevm.deal(address(vault), 22 ether);
vault.withdraw();
assertEq(address(messenger).balance, 33 ether);
assertEq(vault.totalProcessed(), 33 ether);
}
function testWithdrawAmountTwice(uint256 amount1, uint256 amount2) public {
amount1 = bound(amount1, 10 ether, 100 ether);
amount2 = bound(amount2, 10 ether, 100 ether);
hevm.deal(address(vault), 200 ether);
vault.withdraw(amount1);
assertEq(address(messenger).balance, amount1);
assertEq(vault.totalProcessed(), amount1);
vault.withdraw(amount2);
assertEq(address(messenger).balance, amount1 + amount2);
assertEq(vault.totalProcessed(), amount1 + amount2);
assertEq(address(vault).balance, 200 ether - amount1 - amount2);
}
function testUpdateMinWithdrawAmount(uint256 amount1, uint256 amount2) external {
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("caller is not the owner");
vault.updateMinWithdrawAmount(amount1);
hevm.stopPrank();
// set by owner, should succeed
assertEq(10 ether, vault.minWithdrawAmount());
hevm.expectEmit(false, false, false, true);
emit UpdateMinWithdrawAmount(10 ether, amount1);
vault.updateMinWithdrawAmount(amount1);
assertEq(amount1, vault.minWithdrawAmount());
hevm.expectEmit(false, false, false, true);
emit UpdateMinWithdrawAmount(amount1, amount2);
vault.updateMinWithdrawAmount(amount2);
assertEq(amount2, vault.minWithdrawAmount());
}
function testUpdateRecipient(address recipient1, address recipient2) external {
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("caller is not the owner");
vault.updateRecipient(recipient1);
hevm.stopPrank();
// set by owner, should succeed
assertEq(address(1), vault.recipient());
hevm.expectEmit(true, true, false, true);
emit UpdateRecipient(address(1), recipient1);
vault.updateRecipient(recipient1);
assertEq(recipient1, vault.recipient());
hevm.expectEmit(true, true, false, true);
emit UpdateRecipient(recipient1, recipient2);
vault.updateRecipient(recipient2);
assertEq(recipient2, vault.recipient());
}
function testUpdateMessenger(address messenger1, address messenger2) external {
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("caller is not the owner");
vault.updateMessenger(messenger1);
hevm.stopPrank();
// set by owner, should succeed
assertEq(address(messenger), vault.messenger());
hevm.expectEmit(true, true, false, true);
emit UpdateMessenger(address(messenger), messenger1);
vault.updateMessenger(messenger1);
assertEq(messenger1, vault.messenger());
hevm.expectEmit(true, true, false, true);
emit UpdateMessenger(messenger1, messenger2);
vault.updateMessenger(messenger2);
assertEq(messenger2, vault.messenger());
}
}