generated from aragon/osx-plugin-template-foundry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VotingChainSetup.t.sol
383 lines (324 loc) · 13 KB
/
VotingChainSetup.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
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
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.17;
import {IDAO} from "@aragon/osx/core/dao/IDAO.sol";
import {IPluginSetup} from "@aragon/osx/framework/plugin/setup/IPluginSetup.sol";
import {PermissionLib} from "@aragon/osx/core/permission/PermissionLib.sol";
import {AdminSetup, Admin} from "@aragon/admin/AdminSetup.sol";
import {PluginRepo} from "@aragon/osx/framework/plugin/repo/PluginRepo.sol";
import {DAO} from "@aragon/osx/core/dao/DAO.sol";
import {TestHelpers} from "test/helpers/TestHelpers.sol";
import {MockPluginSetupProcessor} from "@mocks/osx/MockPSP.sol";
import {MockDAOFactory, PluginSetupRef} from "@mocks/osx/MockDAOFactory.sol";
import {MockLzEndpointMinimal} from "@mocks/MockLzEndpoint.sol";
import {ToucanRelaySetup, ToucanRelay} from "@voting-chain/setup/ToucanRelaySetup.sol";
import {AdminXChainSetup, AdminXChain} from "@voting-chain/setup/AdminXChainSetup.sol";
import {OFTTokenBridge} from "@voting-chain/crosschain/OFTTokenBridge.sol";
import {GovernanceERC20VotingChain} from "@voting-chain/token/GovernanceERC20VotingChain.sol";
import "@utils/converters.sol";
import "@helpers/OSxHelpers.sol";
contract TestVotingChainOSx is TestHelpers {
address trustedDeployer = address(0x420420420);
// osx contracts
MockPluginSetupProcessor mockPSP;
MockDAOFactory mockDAOFactory;
// layer zero
MockLzEndpointMinimal lzEndpoint;
uint32 remoteEid = 123;
address remoteActionRelay = address(1);
address remoteReceiver = address(2);
address remoteAdapter = address(3);
// dao
DAO dao;
// plugin setups
AdminSetup adminSetup;
ToucanRelaySetup toucanRelaySetup;
AdminXChainSetup adminXChainSetup;
// plugins
GovernanceERC20VotingChain token;
OFTTokenBridge bridge;
Admin admin;
ToucanRelay toucanRelay;
AdminXChain adminXChain;
// setup
PermissionLib.MultiTargetPermission[] toucanRelayPermissions;
PermissionLib.MultiTargetPermission[] adminXChainPermissions;
PermissionLib.MultiTargetPermission[] adminUninstallPermissions;
function testIt() public {
_addLabels();
_deployL0();
_deployOSX();
_deployDAOAndAdmin();
_prepareSetupRelay();
_prepareSetupAdminXChain();
_prepareUninstallAdmin();
// you would wait until the execution chain is deployed and the addresses of the
// remote peers are known
_applyInstallationsSetPeersRevokeAdmin();
_validateEndState();
}
function _deployOSX() internal {
// deploy the mock PSP with the admin plugin
adminSetup = new AdminSetup();
mockPSP = new MockPluginSetupProcessor(address(adminSetup));
mockDAOFactory = new MockDAOFactory(mockPSP);
}
function _deployL0() internal {
lzEndpoint = new MockLzEndpointMinimal();
}
function _deployDAOAndAdmin() internal {
// use the OSx DAO factory with the Admin Plugin
bytes memory data = abi.encode(trustedDeployer);
dao = mockDAOFactory.createDao(_mockDAOSettings(), _mockPluginSettings(data));
// nonce 0 is something?
// nonce 1 is implementation contract
// nonce 2 is the admin contract behind the proxy
admin = Admin(computeAddress(address(adminSetup), 2));
assertEq(admin.isMember(trustedDeployer), true, "trustedDeployer should be a member");
vm.label(address(dao), "dao");
vm.label(address(admin), "admin");
}
function _addLabels() internal {
vm.label(trustedDeployer, "trustedDeployer");
}
function _prepareSetupRelay() internal {
// setup the voting chain: we need 2 setup contracts for the toucanRelay and the adminXChain
toucanRelaySetup = new ToucanRelaySetup(
new ToucanRelay(),
new OFTTokenBridge(),
new GovernanceERC20VotingChain(IDAO(address(dao)), "TestToken", "TT")
);
// set it on the mock psp
mockPSP.queueSetup(address(toucanRelaySetup));
ToucanRelaySetup.InstallationParams memory params = ToucanRelaySetup.InstallationParams({
lzEndpoint: address(lzEndpoint),
tokenName: "vTestToken",
tokenSymbol: "vTT",
dstEid: 1,
votingBridgeBuffer: 20 minutes
});
// prepare the installation
bytes memory data = abi.encode(params);
(
address toucanRelayAddress,
IPluginSetup.PreparedSetupData memory toucanRelaySetupData
) = mockPSP.prepareInstallation(address(dao), _mockPrepareInstallationParams(data));
// cannot write to storage from memory memory so just push here
for (uint256 i = 0; i < toucanRelaySetupData.permissions.length; i++) {
toucanRelayPermissions.push(toucanRelaySetupData.permissions[i]);
}
toucanRelay = ToucanRelay(toucanRelayAddress);
address[] memory helpers = toucanRelaySetupData.helpers;
token = GovernanceERC20VotingChain(helpers[0]);
bridge = OFTTokenBridge(helpers[1]);
vm.label(toucanRelayAddress, "toucanRelay");
vm.label(address(token), "token");
vm.label(address(bridge), "bridge");
}
function _prepareSetupAdminXChain() internal {
// setup the voting chain: we need 2 setup contracts for the toucanRelay and the adminXChain
adminXChainSetup = new AdminXChainSetup(new AdminXChain());
// set it on the mock psp
mockPSP.queueSetup(address(adminXChainSetup));
// prepare the installation
bytes memory data = abi.encode(address(lzEndpoint));
(
address adminXChainAddress,
IPluginSetup.PreparedSetupData memory adminXChainSetupData
) = mockPSP.prepareInstallation(address(dao), _mockPrepareInstallationParams(data));
// cannot write to storage from memory memory so just push here
for (uint256 i = 0; i < adminXChainSetupData.permissions.length; i++) {
adminXChainPermissions.push(adminXChainSetupData.permissions[i]);
}
adminXChain = AdminXChain(payable(adminXChainAddress));
vm.label(adminXChainAddress, "adminXChain");
}
function _prepareUninstallAdmin() internal {
// psp will use the admin setup in next call
mockPSP.queueSetup(address(adminSetup));
IPluginSetup.SetupPayload memory payload = IPluginSetup.SetupPayload({
plugin: address(admin),
currentHelpers: new address[](0),
data: new bytes(0)
});
// prepare the uninstallation
PermissionLib.MultiTargetPermission[] memory permissions = mockPSP.prepareUninstallation(
address(dao),
_mockPrepareUninstallationParams(payload)
);
// cannot write to storage from memory memory so just push here
for (uint256 i = 0; i < permissions.length; i++) {
adminUninstallPermissions.push(permissions[i]);
}
}
function _applyInstallationsSetPeersRevokeAdmin() internal {
IDAO.Action[] memory actions = new IDAO.Action[](6);
// action 0: apply the toucanRelay installation
actions[0] = IDAO.Action({
to: address(mockPSP),
value: 0,
data: abi.encodeCall(
mockPSP.applyInstallation,
(
address(dao),
_mockApplyInstallationParams(address(toucanRelay), toucanRelayPermissions)
)
)
});
// action 1: apply the adminXChain installation
actions[1] = IDAO.Action({
to: address(mockPSP),
value: 0,
data: abi.encodeCall(
mockPSP.applyInstallation,
(
address(dao),
_mockApplyInstallationParams(address(adminXChain), adminXChainPermissions)
)
)
});
// action 2,3,4: set the peers
actions[2] = IDAO.Action({
to: address(toucanRelay),
value: 0,
data: abi.encodeCall(toucanRelay.setPeer, (remoteEid, addressToBytes32(remoteReceiver)))
});
actions[3] = IDAO.Action({
to: address(adminXChain),
value: 0,
data: abi.encodeCall(
adminXChain.setPeer,
(remoteEid, addressToBytes32(remoteActionRelay))
)
});
actions[4] = IDAO.Action({
to: address(bridge),
value: 0,
data: abi.encodeCall(adminXChain.setPeer, (remoteEid, addressToBytes32(remoteAdapter)))
});
// action 5: uninstall the admin plugin
actions[5] = IDAO.Action({
to: address(mockPSP),
value: 0,
data: abi.encodeCall(
mockPSP.applyUninstallation,
(
address(dao),
_mockApplyUninstallationParams(address(admin), adminUninstallPermissions)
)
)
});
// wrap the actions in grant/revoke root permissions
IDAO.Action[] memory wrappedActions = wrapGrantRevokeRoot(dao, address(mockPSP), actions);
// execute the actions
vm.startPrank(trustedDeployer);
{
admin.executeProposal({_metadata: "", _actions: wrappedActions, _allowFailureMap: 0});
}
vm.stopPrank();
}
function _validateEndState() internal view {
// check that the admin is uninstalled
assertEq(
dao.hasPermission({
_who: address(admin),
_where: address(dao),
_permissionId: dao.EXECUTE_PERMISSION_ID(),
_data: ""
}),
false,
"admin should not have execute permission"
);
// check that xchain admin has execute on the dao
assertEq(
dao.hasPermission({
_who: address(adminXChain),
_where: address(dao),
_permissionId: dao.EXECUTE_PERMISSION_ID(),
_data: ""
}),
true,
"xchain admin should have execute permission"
);
// check the DAO is the OAPP admin for the xchain contracts: relay, xchain and bridge
assertEq(
dao.hasPermission({
_who: address(dao),
_where: address(toucanRelay),
_permissionId: toucanRelay.OAPP_ADMINISTRATOR_ID(),
_data: ""
}),
true,
"DAO should be the OAPP admin for the toucanRelay"
);
assertEq(
dao.hasPermission({
_who: address(dao),
_where: address(adminXChain),
_permissionId: adminXChain.OAPP_ADMINISTRATOR_ID(),
_data: ""
}),
true,
"DAO should be the OAPP admin for the adminXChain"
);
assertEq(
dao.hasPermission({
_who: address(dao),
_where: address(bridge),
_permissionId: bridge.OAPP_ADMINISTRATOR_ID(),
_data: ""
}),
true,
"DAO should be the OAPP admin for the bridge"
);
// check that the peers are all set for the crosschain contracts
assertEq(
bytes32ToAddress(toucanRelay.peers(remoteEid)),
remoteReceiver,
"toucanRelay should have the remote receiver as a peer"
);
assertEq(
bytes32ToAddress(adminXChain.peers(remoteEid)),
remoteActionRelay,
"adminXChain should have the remote action relay as a peer"
);
assertEq(
bytes32ToAddress(bridge.peers(remoteEid)),
remoteAdapter,
"bridge should have the remote adapter as a peer"
);
// check that the token is deployed and the bridge has mint/burn ability
assertEq(token.name(), "vTestToken", "token should be deployed with the correct name");
assertEq(token.symbol(), "vTT", "token should be deployed with the correct symbol");
assertEq(
dao.hasPermission({
_who: address(bridge),
_where: address(token),
_permissionId: token.MINT_PERMISSION_ID(),
_data: ""
}),
true,
"bridge should have mint permission on the token"
);
assertEq(
dao.hasPermission({
_who: address(bridge),
_where: address(token),
_permissionId: token.BURN_PERMISSION_ID(),
_data: ""
}),
true,
"bridge should have burn permission on the token"
);
// DAO should be able to sweep refunds from adminxchain
assertEq(
dao.hasPermission({
_who: address(dao),
_where: address(adminXChain),
_permissionId: adminXChain.SWEEP_COLLECTOR_ID(),
_data: ""
}),
true,
"DAO should be able to sweep refunds from adminXChain"
);
}
}