-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateAIPStarknetPhaseI.sol
389 lines (343 loc) · 13.1 KB
/
ValidateAIPStarknetPhaseI.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;
import {BaseTest, console} from "./base/BaseTest.sol";
import {PayloadAaveStarknetPhaseI, ICollector, IOwnable, LibPropConstants} from "../PayloadAaveStarknetPhaseI.sol";
interface IAaveGov {
struct ProposalWithoutVotes {
uint256 id;
address creator;
address executor;
address[] targets;
uint256[] values;
string[] signatures;
bytes[] calldatas;
bool[] withDelegatecalls;
uint256 startBlock;
uint256 endBlock;
uint256 executionTime;
uint256 forVotes;
uint256 againstVotes;
bool executed;
bool canceled;
address strategy;
bytes32 ipfsHash;
}
enum ProposalState {
Pending,
Canceled,
Active,
Failed,
Succeeded,
Queued,
Expired,
Executed
}
struct SPropCreateParams {
address executor;
address[] targets;
uint256[] values;
string[] signatures;
bytes[] calldatas;
bool[] withDelegatecalls;
bytes32 ipfsHash;
}
function create(
address executor,
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
bool[] memory withDelegatecalls,
bytes32 ipfsHash
) external returns (uint256);
function queue(uint256 proposalId) external;
function execute(uint256 proposalId) external payable;
function submitVote(uint256 proposalId, bool support) external;
function getProposalById(uint256 proposalId)
external
view
returns (ProposalWithoutVotes memory);
function getProposalState(uint256 proposalId)
external
view
returns (ProposalState);
}
contract ValidateAIPStarknetPhaseI is BaseTest {
address internal constant AAVE_TREASURY =
0x25F2226B597E8F9514B3F68F00f494cF4f286491;
IAaveGov internal constant GOV =
IAaveGov(0xEC568fffba86c094cf06b22134B23074DFE2252c);
address SHORT_EXECUTOR = 0xEE56e2B3D491590B5b31738cC34d5232F378a8D5;
function setUp() public {}
/// @dev First deploys a fresh payload, then tests everything using it
function testProposalPrePayload() public {
// Block of proposal creation on Ethereum mainnet
if (block.number < 14255778) {
IAaveGov.SPropCreateParams memory proposalParams = _buildProposal(
address(new PayloadAaveStarknetPhaseI())
);
uint256 proposalId = _createProposal(proposalParams);
uint256 recipientUsdcBefore = LibPropConstants.USDC.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
uint256 recipientWethBefore = LibPropConstants.WETH.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
_passVoteAndQueue(proposalId, AAVE_TREASURY);
_executeProposal(proposalId, AAVE_TREASURY);
_validatePhaseIFunds(recipientUsdcBefore, recipientWethBefore);
address newControllerOfCollector = _validateNewCollector();
_validateNewControllerOfCollector(
ICollector(newControllerOfCollector)
);
}
}
/// @dev Uses an already deployed payload on the target network
function testProposalPostPayload() public {
if (block.number < 14255778) {
// Block of proposal creation on Ethereum mainnet
IAaveGov.SPropCreateParams memory proposalParams = _buildProposal(
0x4E76e1d71806aaE6Ccaac0FC67C3aa74cb245277
);
uint256 proposalId = _createProposal(proposalParams);
uint256 recipientUsdcBefore = LibPropConstants.USDC.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
uint256 recipientWethBefore = LibPropConstants.WETH.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
_passVoteAndQueue(proposalId, AAVE_TREASURY);
_executeProposal(proposalId, AAVE_TREASURY);
_validatePhaseIFunds(recipientUsdcBefore, recipientWethBefore);
address newControllerOfCollector = _validateNewCollector();
_validateNewControllerOfCollector(
ICollector(newControllerOfCollector)
);
}
}
function testProposalVoting() public {
// Voting range on Ethereum mainnet
if (block.number > 14255778 && block.number < 14275325) {
uint256 proposalId = 61;
uint256 recipientUsdcBefore = LibPropConstants.USDC.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
uint256 recipientWethBefore = LibPropConstants.WETH.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
_passVoteAndQueue(proposalId, AAVE_TREASURY);
_executeProposal(proposalId, AAVE_TREASURY);
_validatePhaseIFunds(recipientUsdcBefore, recipientWethBefore);
address newControllerOfCollector = _validateNewCollector();
_validateNewControllerOfCollector(
ICollector(newControllerOfCollector)
);
}
}
function testProposalQueued() public {
if (block.number > 14275325) { // TODO add execution block once available
// Block when the proposal has been queued on Ethereum mainnet
uint256 proposalId = 61;
uint256 recipientUsdcBefore = LibPropConstants.USDC.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
uint256 recipientWethBefore = LibPropConstants.WETH.balanceOf(
LibPropConstants.FUNDS_RECIPIENT
);
_executeProposal(proposalId, AAVE_TREASURY);
_validatePhaseIFunds(recipientUsdcBefore, recipientWethBefore);
address newControllerOfCollector = _validateNewCollector();
_validateNewControllerOfCollector(
ICollector(newControllerOfCollector)
);
}
}
function _buildProposal(address payload)
internal
view
returns (IAaveGov.SPropCreateParams memory)
{
address[] memory targets = new address[](1);
targets[0] = payload;
uint256[] memory values = new uint256[](1);
values[0] = 0;
string[] memory signatures = new string[](1);
signatures[0] = "execute()";
bytes[] memory calldatas = new bytes[](1);
calldatas[0] = "";
bool[] memory withDelegatecalls = new bool[](1);
withDelegatecalls[0] = true;
return
IAaveGov.SPropCreateParams({
executor: SHORT_EXECUTOR,
targets: targets,
values: values,
signatures: signatures,
calldatas: calldatas,
withDelegatecalls: withDelegatecalls,
ipfsHash: bytes32(0)
});
}
function _createProposal(IAaveGov.SPropCreateParams memory params)
internal
returns (uint256)
{
vm.deal(AAVE_TREASURY, 1 ether);
vm.startPrank(AAVE_TREASURY);
uint256 proposalId = GOV.create(
params.executor,
params.targets,
params.values,
params.signatures,
params.calldatas,
params.withDelegatecalls,
params.ipfsHash
);
vm.stopPrank();
return proposalId;
}
function _passVoteAndQueue(uint256 proposalId, address whaleVoter)
internal
{
vm.deal(whaleVoter, 1 ether);
vm.startPrank(whaleVoter);
vm.roll(block.number + 1);
GOV.submitVote(proposalId, true);
uint256 endBlock = GOV.getProposalById(proposalId).endBlock;
vm.roll(endBlock + 1);
GOV.queue(proposalId);
vm.stopPrank();
}
function _executeProposal(uint256 proposalId, address executorOfProposal)
internal
{
vm.deal(executorOfProposal, 1 ether);
vm.startPrank(executorOfProposal);
uint256 executionTime = GOV.getProposalById(proposalId).executionTime;
vm.warp(executionTime + 1);
GOV.execute(proposalId);
vm.stopPrank();
}
function _validatePhaseIFunds(
uint256 recipientUsdcBefore,
uint256 recipientWethBefore
) internal view {
require(
LibPropConstants.USDC.balanceOf(LibPropConstants.FUNDS_RECIPIENT) ==
LibPropConstants.USDC_AMOUNT + recipientUsdcBefore,
"INVALID_RECIPIENT_USDC_BALANCE"
);
require(
LibPropConstants.WETH.balanceOf(LibPropConstants.FUNDS_RECIPIENT) ==
LibPropConstants.ETH_AMOUNT + recipientWethBefore,
"INVALID_RECIPIENT_WETH_BALANCE"
);
}
function _validateNewControllerOfCollector(ICollector controllerOfCollector)
internal
{
require(
IOwnable(address(controllerOfCollector)).owner() == SHORT_EXECUTOR,
"NEW_CONTROLLER_TESTS: INVALID_OWNER"
);
// Negative tests of ownership
vm.startPrank(AAVE_TREASURY);
vm.expectRevert(bytes("Ownable: caller is not the owner"));
controllerOfCollector.transfer(LibPropConstants.AUSDC, address(0), 666);
vm.expectRevert(bytes("Ownable: caller is not the owner"));
controllerOfCollector.approve(LibPropConstants.AWETH, address(0), 999);
vm.stopPrank();
// Positive ownership tests
vm.startPrank(SHORT_EXECUTOR);
uint256 balanceMockRecipientBefore = LibPropConstants.AUSDC.balanceOf(
address(1)
);
controllerOfCollector.transfer(LibPropConstants.AUSDC, address(1), 666);
require(
_almostEqual(
LibPropConstants.AUSDC.balanceOf(address(1)),
balanceMockRecipientBefore + 666
),
"NEW_CONTROLLER_TESTS : INVALID_POST_TRANSFER_BALANCE"
);
controllerOfCollector.approve(LibPropConstants.AWETH, address(1), 777);
require(
(LibPropConstants.AWETH.allowance(
address(LibPropConstants.COLLECTOR_V2_PROXY),
address(1)
) == 777),
"NEW_CONTROLLER_TESTS : INVALID_POST_TRANSFER_ALLOWANCE"
);
vm.stopPrank();
}
function _validateNewCollector() internal returns (address) {
vm.startPrank(SHORT_EXECUTOR);
// Only the admin can call the admin() view function, so acts as assert of correctness
LibPropConstants.COLLECTOR_V2_PROXY.admin();
require(
LibPropConstants.COLLECTOR_V2_PROXY.implementation() ==
address(LibPropConstants.NEW_COLLECTOR_IMPL),
"NEW_COLLECTOR_TESTS : INVALID_NEW_COLLECTOR_IMPL"
);
vm.stopPrank();
require(
LibPropConstants.NEW_COLLECTOR_IMPL.REVISION() == 2,
"NEW_COLLECTOR_TESTS : INVALID_COLLECTOR_IMPL_REVISION"
);
address controllerOfCollector = ICollector(
address(LibPropConstants.COLLECTOR_V2_PROXY)
).getFundsAdmin();
ICollector collectorProxy = ICollector(
address(LibPropConstants.COLLECTOR_V2_PROXY)
);
// Negative tests of ownership
vm.startPrank(AAVE_TREASURY);
vm.expectRevert(bytes("ONLY_BY_FUNDS_ADMIN"));
collectorProxy.transfer(LibPropConstants.AUSDC, address(0), 666);
vm.expectRevert(bytes("ONLY_BY_FUNDS_ADMIN"));
collectorProxy.approve(LibPropConstants.AWETH, address(0), 999);
vm.stopPrank();
// Positive ownership tests
vm.startPrank(controllerOfCollector);
uint256 balanceMockRecipientBefore = LibPropConstants.AUSDC.balanceOf(
address(1)
);
collectorProxy.transfer(LibPropConstants.AUSDC, address(1), 666);
require(
_almostEqual(
LibPropConstants.AUSDC.balanceOf(address(1)),
balanceMockRecipientBefore + 666
),
"NEW_COLLECTOR_TESTS : INVALID_POST_TRANSFER_BALANCE"
);
uint256 allowanceMockRecipientBefore = LibPropConstants.AWETH.allowance(
address(collectorProxy),
address(1)
);
collectorProxy.approve(LibPropConstants.AWETH, address(1), 666);
require(
LibPropConstants.AWETH.allowance(
address(collectorProxy),
address(1)
) == allowanceMockRecipientBefore + 666,
"NEW_COLLECTOR_TESTS : INVALID_POST_TRANSFER_ALLOWANCE"
);
vm.stopPrank();
// No further initialisation can be done
vm.startPrank(AAVE_TREASURY);
vm.expectRevert(
bytes("Contract instance has already been initialized")
);
collectorProxy.initialize(address(1));
vm.stopPrank();
return controllerOfCollector;
}
/// @dev To contemplate +1/-1 precision issues when rounding, mainly on aTokens
function _almostEqual(uint256 a, uint256 b) internal pure returns (bool) {
if (b == 0) {
return (a == b) || (a == (b + 1));
} else {
return (a == b) || (a == (b + 1)) || (a == (b - 1));
}
}
}