This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTest_ZivoeGlobals.sol
236 lines (166 loc) · 8.86 KB
/
Test_ZivoeGlobals.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
import "../Utility/Utility.sol";
import "../../lib/zivoe-core-foundry/src/lockers/OCG/OCG_Defaults.sol";
contract Test_ZivoeGlobals is Utility {
OCG_Defaults GenericDefaultsLocker;
function setUp() public {
deployCore(false);
}
// ----------------------
// Helper Functions
// ----------------------
function updatedDefaults(uint256 increaseBy, uint256 decreaseBy) public view returns (uint256 updated) {
updated = decreaseBy > increaseBy ? 0 : increaseBy - decreaseBy;
}
// ------------
// Events
// ------------
event TransferredZVL(address indexed controller);
event DefaultsDecreased(address indexed locker, uint256 amount, uint256 updatedDefaults);
event DefaultsIncreased(address indexed locker, uint256 amount, uint256 updatedDefaults);
event UpdatedKeeperStatus(address indexed account, bool status);
event UpdatedLockerStatus(address indexed locker, bool status);
event UpdatedStablecoinWhitelist(address indexed asset, bool allowed);
// ------------
// Unit tests
// ------------
// Validate restrictions of decreaseDefaults() / increaseDefaults().
// This includes:
// - _msgSender() must be a whitelisted ZivoeLocker.
function test_ZivoeGlobals_decreaseDefaults_restrictions_direct() public {
// Ensure non-whitelisted address may not call decrease default, directly via ZivoeGlobals.sol.
hevm.startPrank(address(bob));
hevm.expectRevert("ZivoeGlobals::decreaseDefaults() !isLocker[_msgSender()]");
GBL.decreaseDefaults(100 ether);
hevm.stopPrank();
}
function test_ZivoeGlobals_increaseDefaults_restrictions_direct() public {
// Non-whitelisted address can not call increase default.
hevm.startPrank(address(bob));
hevm.expectRevert("ZivoeGlobals::increaseDefaults() !isLocker[_msgSender()]");
GBL.increaseDefaults(100 ether);
hevm.stopPrank();
}
// Validate restrictions on OCG_Defaults locker endpoints.
function test_ZivoeGlobals_decreaseDefaults_restrictions_indirect() public {
// Create OCG_Defaults locker, with default adjustment capability, add this to whitelist.
GenericDefaultsLocker = new OCG_Defaults(address(DAO), address(GBL));
assert(zvl.try_updateIsLocker(address(GBL), address(GenericDefaultsLocker), true));
// Ensure the "onlyGovernance" modifier in OCG_Defaults::decreaseDefaults() prevents this call.
hevm.startPrank(address(bob));
hevm.expectRevert("OCG_Defaults::onlyGovernance() _msgSender!= IZivoeGlobals_OCG_Defaults(GBL).TLC()");
GenericDefaultsLocker.decreaseDefaults(100 ether);
hevm.stopPrank();
}
function test_ZivoeGlobals_increaseDefaults_restrictions_indirect() public {
// Create GenericDefaults locker, with default adjustment capability, add this to whitelist.
GenericDefaultsLocker = new OCG_Defaults(address(DAO), address(GBL));
assert(zvl.try_updateIsLocker(address(GBL), address(GenericDefaultsLocker), true));
// Ensure the "onlyGovernance" modifier in OCG_Defaults::increaseDefaults() prevents this call.
hevm.startPrank(address(bob));
hevm.expectRevert("OCG_Defaults::onlyGovernance() _msgSender!= IZivoeGlobals_OCG_Defaults(GBL).TLC()");
GenericDefaultsLocker.increaseDefaults(100 ether);
hevm.stopPrank();
}
// Validate state changes of increaseDefaults() / decreaseDefaults().
function test_ZivoeGlobals_increase_or_decreaseDefaults_state(uint96 increaseAmount, uint96 decreaseAmount) public {
uint256 increaseBy = uint256(increaseAmount);
uint256 decreaseBy = uint256(decreaseAmount);
// Create GenericDefaults locker, with default adjustment capability, add this to whitelist.
GenericDefaultsLocker = new OCG_Defaults(address(DAO), address(GBL));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedLockerStatus(address(GenericDefaultsLocker), true);
assert(zvl.try_updateIsLocker(address(GBL), address(GenericDefaultsLocker), true));
// Pre-state.
assertEq(GBL.defaults(), 0);
// increaseDefaults().
hevm.expectEmit(true, false, false, true, address(GBL));
emit DefaultsIncreased(address(GenericDefaultsLocker), increaseBy, increaseBy);
assert(god.try_increaseDefaults(address(GenericDefaultsLocker), increaseBy));
// Post-state, increaseDefaults().
assertEq(GBL.defaults(), increaseBy);
// decreaseDefaults().
uint256 updated = updatedDefaults(increaseBy, decreaseBy);
hevm.expectEmit(true, false, false, true, address(GBL));
emit DefaultsDecreased(address(GenericDefaultsLocker), decreaseBy, updated);
assert(god.try_decreaseDefaults(address(GenericDefaultsLocker), decreaseBy));
// Post-state, decreaseDefaults().
if (decreaseBy > increaseBy) {
assertEq(GBL.defaults(), 0);
}
else {
assertEq(GBL.defaults(), increaseBy - decreaseBy);
}
}
// Validate restrictions updateIsKeeper() / updateIsLocker() / updateStablecoinWhitelist().
// This includes:
// - _msgSender() MUST be "zvl"
function test_ZivoeGlobals_updateIsKeeper_restrictions_onlyZVL() public {
hevm.startPrank(address(bob));
hevm.expectRevert("ZivoeGlobals::onlyZVL() _msgSender() != ZVL");
GBL.updateIsKeeper(address(1), true);
hevm.stopPrank();
}
function test_ZivoeGlobals_updateIsLocker_restrictions_onlyZVL() public {
hevm.startPrank(address(bob));
hevm.expectRevert("ZivoeGlobals::onlyZVL() _msgSender() != ZVL");
GBL.updateIsLocker(address(1), true);
hevm.stopPrank();
}
function test_ZivoeGlobals_updateStablecoinWhitelist_restrictions_onlyZVL() public {
hevm.startPrank(address(bob));
hevm.expectRevert("ZivoeGlobals::onlyZVL() _msgSender() != ZVL");
GBL.updateStablecoinWhitelist(address(1), true);
hevm.stopPrank();
}
// Validate state changes updateIsKeeper() / updateIsLocker() / updateStablecoinWhitelist().
function test_ZivoeGlobals_onlyZVL_state(address entity) public {
// updateIsKeeper() false => true.
assert(!GBL.isKeeper(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedKeeperStatus(address(entity), true);
assert(zvl.try_updateIsKeeper(address(GBL), address(entity), true));
assert(GBL.isKeeper(entity));
// updateIsKeeper() true => false.
assert(GBL.isKeeper(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedKeeperStatus(address(entity), false);
assert(zvl.try_updateIsKeeper(address(GBL), address(entity), false));
assert(!GBL.isKeeper(entity));
// updateIsLocker() false => true.
assert(!GBL.isLocker(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedLockerStatus(address(entity), true);
assert(zvl.try_updateIsLocker(address(GBL), address(entity), true));
assert(GBL.isLocker(entity));
// updateIsLocker() true => false.
assert(GBL.isLocker(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedLockerStatus(address(entity), false);
assert(zvl.try_updateIsLocker(address(GBL), address(entity), false));
assert(!GBL.isLocker(entity));
// updateStablecoinWhitelist() false => true.
assert(!GBL.stablecoinWhitelist(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedStablecoinWhitelist(address(entity), true);
assert(zvl.try_updateStablecoinWhitelist(address(GBL), address(entity), true));
assert(GBL.stablecoinWhitelist(entity));
// updateStablecoinWhitelist() true => false.
assert(GBL.stablecoinWhitelist(entity));
hevm.expectEmit(true, false, false, true, address(GBL));
emit UpdatedStablecoinWhitelist(address(entity), false);
assert(zvl.try_updateStablecoinWhitelist(address(GBL), address(entity), false));
assert(!GBL.stablecoinWhitelist(entity));
}
// Validate various values for standardize() view function.
function test_ZivoeGlobals_standardize_view(uint96 amount) public {
uint256 conversionAmount = uint256(amount);
// USDC 6 Decimals -> 18 Decimals
// USDT 6 Decimals -> 18 Decimals
(uint256 standardizedAmountUSDC) = GBL.standardize(conversionAmount, USDC);
(uint256 standardizedAmountUSDT) = GBL.standardize(conversionAmount, USDT);
assertEq(standardizedAmountUSDC, conversionAmount * 10**12);
assertEq(standardizedAmountUSDT, conversionAmount * 10**12);
}
}