-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathIntegration.t.sol
473 lines (381 loc) · 17.2 KB
/
Integration.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: AGPL-3.0-or-later
/// DomainJoin.sol -- xdomain join adapter
// Copyright (C) 2022 Dai Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.13;
import "dss-test/DSSTest.sol";
import "ds-value/value.sol";
import { EndAbstract } from "dss-interfaces/Interfaces.sol";
import { Cure } from "xdomain-dss/Cure.sol";
import { Dai } from "xdomain-dss/Dai.sol";
import { DaiJoin } from "xdomain-dss/DaiJoin.sol";
import { End } from "xdomain-dss/End.sol";
import { Pot } from "xdomain-dss/Pot.sol";
import { Jug } from "xdomain-dss/Jug.sol";
import { Spotter } from "xdomain-dss/Spotter.sol";
import { Vat } from "xdomain-dss/Vat.sol";
import { ClaimToken } from "../ClaimToken.sol";
import { DomainHost, TeleportGUID } from "../DomainHost.sol";
import { DomainGuest } from "../DomainGuest.sol";
import { BridgeOracle } from "../BridgeOracle.sol";
interface EscrowLike {
function approve(address token, address spender, uint256 value) external;
}
contract SimpleDomainHost is DomainHost {
DomainGuest guest;
constructor(bytes32 _ilk, address _daiJoin, address _escrow, address _router) DomainHost(_ilk, _daiJoin, _escrow, _router) {}
function setGuest(address _guest) external {
guest = DomainGuest(_guest);
}
function _isGuest(address usr) internal override view returns (bool) {
return usr == address(guest);
}
function _lift(uint256 _line, uint256 _minted) internal override {
guest.lift(_line, _minted);
}
function _rectify(uint256 wad) internal virtual override {
guest.rectify(wad);
}
function _cage() internal virtual override {
guest.cage();
}
function _mintClaim(address usr, uint256 claim) internal virtual override {
guest.mintClaim(usr, claim);
}
}
contract SimpleDomainGuest is DomainGuest {
DomainHost host;
constructor(bytes32 _domain, address _daiJoin, address _claimToken, address _host) DomainGuest(_domain, _daiJoin, _claimToken) {
host = DomainHost(_host);
}
function _isHost(address usr) internal override view returns (bool) {
return usr == address(usr);
}
function _release(uint256 burned) internal override {
host.release(burned);
}
function _surplus(uint256 wad) internal virtual override {
host.surplus(wad);
}
function _deficit(uint256 wad) internal virtual override {
host.deficit(wad);
}
function _tell(uint256 value) internal virtual override {
host.tell(value);
}
function _initiateTeleport(TeleportGUID memory teleport) internal virtual override {
host.initiateTeleport(teleport);
}
function _flush(bytes32 targetDomain, uint256 daiToFlush) internal virtual override {
host.flush(targetDomain, daiToFlush);
}
}
// TODO use actual dog when ready
contract DogMock {
function wards(address) external pure returns (uint256) {
return 1;
}
function file(bytes32,address) external {
// Do nothing
}
}
contract IntegrationTest is DSSTest {
using GodMode for *;
// Bridge
ClaimToken claimToken;
SimpleDomainHost host;
SimpleDomainGuest guest;
BridgeOracle pip;
// Local contracts
EscrowLike escrow;
// Remote domain MCD deploy
MCD rmcd;
bytes32 constant DOMAIN_ILK = "SOME-DOMAIN-A";
bytes32 constant REMOTE_COLL_ILK = "XCHAIN-COLLATERAL-A";
function setupEnv() internal virtual override returns (MCD) {
return autoDetectEnv();
}
function postSetup() internal virtual override {
escrow = EscrowLike(mcd.chainlog().getAddress("OPTIMISM_ESCROW"));
claimToken = new ClaimToken();
// TODO add support for Teleport router when it's available
host = new SimpleDomainHost(DOMAIN_ILK, address(mcd.daiJoin()), address(escrow), address(0));
Vat vat = new Vat();
Dai dai = new Dai();
DaiJoin daiJoin = new DaiJoin(address(vat), address(dai));
guest = new SimpleDomainGuest(DOMAIN_ILK, address(daiJoin), address(claimToken), address(host));
pip = new BridgeOracle(address(host));
claimToken.rely(address(guest));
// Setup remote instance
{
DogMock dog = new DogMock();
Spotter spotter = new Spotter(address(vat));
Pot pot = new Pot(address(vat));
Jug jug = new Jug(address(vat));
Cure cure = new Cure();
End end = new End();
rmcd = new MCD();
// FIXME this is prone to supplying args in the wrong order - want to improve
rmcd.loadCore(
address(vat),
address(daiJoin),
address(dai),
address(guest),
address(dog),
address(pot),
address(jug),
address(spotter),
address(end),
address(cure)
);
rmcd.init();
}
rmcd.end().file("claim", address(claimToken));
rmcd.end().file("wait", 1 hours);
// Setup the bridge
host.file("vow", address(mcd.vow()));
host.setGuest(address(guest));
host.rely(address(guest));
guest.rely(address(host));
guest.file("end", address(rmcd.end()));
guest.rely(address(rmcd.end()));
rmcd.vat().rely(address(guest));
rmcd.end().rely(address(guest));
// Setup host on MCD master instance
mcd.giveAdminAccess();
mcd.vat().rely(address(host));
address(escrow).setWard(address(this), 1);
escrow.approve(address(mcd.dai()), address(host), type(uint256).max);
mcd.initIlk(DOMAIN_ILK, address(host), address(pip));
mcd.vat().file(DOMAIN_ILK, "line", 1_000_000 * RAD);
mcd.cure().lift(address(host));
}
function testRaiseDebtCeiling() public {
uint256 escrowDai = mcd.dai().balanceOf(address(escrow));
(uint256 ink, uint256 art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 0);
assertEq(art, 0);
assertEq(host.grain(), 0);
assertEq(host.line(), 0);
assertEq(rmcd.vat().Line(), 0);
host.lift(100 ether);
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 100 ether);
assertEq(art, 100 ether);
assertEq(host.grain(), 100 ether);
assertEq(host.line(), 100 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 100 ether);
assertEq(rmcd.vat().Line(), 100 * RAD);
}
function testRaiseLowerDebtCeiling() public {
uint256 escrowDai = mcd.dai().balanceOf(address(escrow));
(uint256 ink, uint256 art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 0);
assertEq(art, 0);
assertEq(host.grain(), 0);
assertEq(host.line(), 0);
assertEq(rmcd.vat().Line(), 0);
host.lift(100 ether);
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 100 ether);
assertEq(art, 100 ether);
assertEq(host.grain(), 100 ether);
assertEq(host.line(), 100 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 100 ether);
assertEq(rmcd.vat().Line(), 100 * RAD);
// Pre-mint DAI is not released here
host.lift(50 ether);
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 100 ether);
assertEq(art, 100 ether);
assertEq(host.grain(), 100 ether);
assertEq(host.line(), 50 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 100 ether);
assertEq(rmcd.vat().Line(), 50 * RAD);
// Notify the host that the DAI is safe to remove
guest.release();
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 50 ether);
assertEq(art, 50 ether);
assertEq(host.grain(), 50 ether);
assertEq(host.line(), 50 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 50 ether);
assertEq(rmcd.vat().Line(), 50 * RAD);
assertEq(rmcd.vat().debt(), 0);
// Add some debt to the guest instance, lower the DC and release some more pre-mint
// This can only release pre-mint DAI up to the debt
rmcd.vat().suck(address(guest), address(this), 40 * RAD);
host.lift(25 ether);
guest.release();
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 40 ether);
assertEq(art, 40 ether);
assertEq(host.grain(), 40 ether);
assertEq(host.line(), 25 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 40 ether);
assertEq(rmcd.vat().Line(), 25 * RAD);
assertEq(rmcd.vat().debt(), 40 * RAD);
}
function testPushSurplus() public {
uint256 escrowDai = mcd.dai().balanceOf(address(escrow));
// Set global DC and add 50 DAI surplus + 20 DAI debt to vow
host.lift(100 ether);
rmcd.vat().suck(address(123), address(guest), 50 * RAD);
rmcd.vat().suck(address(guest), address(123), 20 * RAD);
uint256 vowDai = mcd.vat().dai(address(mcd.vow()));
uint256 vowSin = mcd.vat().sin(address(mcd.vow()));
assertEq(rmcd.vat().dai(address(guest)), 50 * RAD);
assertEq(rmcd.vat().sin(address(guest)), 20 * RAD);
assertEq(Vat(address(rmcd.vat())).surf(), 0);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 100 ether);
guest.push();
assertEq(rmcd.vat().dai(address(guest)), 0);
assertEq(rmcd.vat().sin(address(guest)), 0);
assertEq(Vat(address(rmcd.vat())).surf(), -int256(30 * RAD));
assertEq(mcd.vat().dai(address(mcd.vow())), vowDai + 30 * RAD);
assertEq(mcd.vat().sin(address(mcd.vow())), vowSin);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 70 ether);
}
function testPushDeficit() public {
uint256 escrowDai = mcd.dai().balanceOf(address(escrow));
// Set global DC and add 20 DAI surplus + 50 DAI debt to vow
host.lift(100 ether);
rmcd.vat().suck(address(123), address(guest), 20 * RAD);
rmcd.vat().suck(address(guest), address(123), 50 * RAD);
uint256 vowDai = mcd.vat().dai(address(mcd.vow()));
uint256 vowSin = mcd.vat().sin(address(mcd.vow()));
assertEq(rmcd.vat().dai(address(guest)), 20 * RAD);
assertEq(rmcd.vat().sin(address(guest)), 50 * RAD);
assertEq(Vat(address(rmcd.vat())).surf(), 0);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 100 ether);
guest.push();
assertEq(rmcd.vat().dai(address(guest)), 30 * RAD);
assertEq(rmcd.vat().sin(address(guest)), 30 * RAD);
assertEq(Vat(address(rmcd.vat())).surf(), int256(30 * RAD));
assertEq(mcd.vat().dai(address(mcd.vow())), vowDai);
assertEq(mcd.vat().sin(address(mcd.vow())), vowSin + 30 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 130 ether);
guest.heal();
assertEq(rmcd.vat().dai(address(guest)), 0);
assertEq(rmcd.vat().sin(address(guest)), 0);
assertEq(Vat(address(rmcd.vat())).surf(), int256(30 * RAD));
assertEq(mcd.vat().dai(address(mcd.vow())), vowDai);
assertEq(mcd.vat().sin(address(mcd.vow())), vowSin + 30 * RAD);
assertEq(mcd.dai().balanceOf(address(escrow)), escrowDai + 130 ether);
}
function testGlobalShutdown() public {
// Set up some debt in the guest instance
host.lift(100 ether);
rmcd.initIlk(REMOTE_COLL_ILK);
rmcd.vat().file(REMOTE_COLL_ILK, "line", 1_000_000 * RAD);
rmcd.vat().slip(REMOTE_COLL_ILK, address(this), 40 ether);
rmcd.vat().frob(REMOTE_COLL_ILK, address(this), address(this), address(this), 40 ether, 40 ether);
assertEq(mcd.vat().live(), 1);
assertEq(guest.live(), 1);
assertEq(host.live(), 1);
assertEq(rmcd.vat().live(), 1);
assertEq(rmcd.vat().debt(), 40 * RAD);
(uint256 ink, uint256 art) = rmcd.vat().urns(REMOTE_COLL_ILK, address(this));
assertEq(ink, 40 ether);
assertEq(art, 40 ether);
assertEq(pip.read(), bytes32(WAD));
mcd.end().cage();
host.deny(address(this)); // Confirm cage can be done permissionlessly
host.cage();
// Verify cannot cage the host ilk until a final cure is reported
assertRevert(address(mcd.end()), abi.encodeWithSignature("cage(bytes32)", DOMAIN_ILK), "BridgeOracle/haz-not");
assertEq(mcd.vat().live(), 0);
assertEq(guest.live(), 0);
assertEq(host.live(), 0);
assertEq(rmcd.vat().live(), 0);
assertEq(rmcd.vat().debt(), 40 * RAD);
(ink, art) = rmcd.vat().urns(REMOTE_COLL_ILK, address(this));
assertEq(ink, 40 ether);
assertEq(art, 40 ether);
assertEq(rmcd.vat().gem(REMOTE_COLL_ILK, address(rmcd.end())), 0);
assertEq(rmcd.vat().sin(address(guest)), 0);
// --- Settle out the Guest instance ---
rmcd.end().cage(REMOTE_COLL_ILK);
rmcd.end().skim(REMOTE_COLL_ILK, address(this));
(ink, art) = rmcd.vat().urns(REMOTE_COLL_ILK, address(this));
assertEq(ink, 0);
assertEq(art, 0);
assertEq(rmcd.vat().gem(REMOTE_COLL_ILK, address(rmcd.end())), 40 ether);
assertEq(rmcd.vat().sin(address(guest)), 40 * RAD);
vm.warp(block.timestamp + rmcd.end().wait());
rmcd.end().thaw();
assertEq(guest.grain(), 100 ether);
assertEq(host.cure(), 60 * RAD); // 60 pre-mint dai is unused
rmcd.end().flow(REMOTE_COLL_ILK);
// --- Settle out the Host instance ---
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 100 ether);
assertEq(art, 100 ether);
assertEq(mcd.vat().gem(DOMAIN_ILK, address(mcd.end())), 0);
uint256 vowSin = mcd.vat().sin(address(mcd.vow()));
mcd.end().cage(DOMAIN_ILK);
assertEq(mcd.end().tag(DOMAIN_ILK), 25 * RAY / 10); // Tag should be 2.5 (1 / $1 * 40% debt used)
assertEq(mcd.end().gap(DOMAIN_ILK), 0);
mcd.end().skim(DOMAIN_ILK, address(host));
assertEq(mcd.end().gap(DOMAIN_ILK), 150 * WAD);
(ink, art) = mcd.vat().urns(DOMAIN_ILK, address(host));
assertEq(ink, 0);
assertEq(art, 0);
assertEq(mcd.vat().gem(DOMAIN_ILK, address(mcd.end())), 100 ether);
assertEq(mcd.vat().sin(address(mcd.vow())), vowSin + 100 * RAD);
vm.warp(block.timestamp + mcd.end().wait());
// Clear out any surplus if it exists
uint256 vowDai = mcd.vat().dai(address(mcd.vow()));
mcd.vat().suck(address(mcd.vow()), address(123), vowDai);
mcd.vow().heal(vowDai);
// Check debt is deducted properly
uint256 debt = mcd.vat().debt();
mcd.cure().load(address(host));
mcd.end().thaw();
assertEq(mcd.end().debt(), debt - 60 * RAD);
mcd.end().flow(DOMAIN_ILK);
assertEq(mcd.end().fix(DOMAIN_ILK), (100 * RAD) / (mcd.end().debt() / RAY));
// --- Do user redemption for remote domain collateral ---
// Pretend you own 50% of all outstanding debt (should be a pro-rate claim on $20 for the remote domain)
uint256 myDai = (mcd.end().debt() / 2) / RAY;
mcd.vat().suck(address(123), address(this), myDai * RAY);
mcd.vat().hope(address(mcd.end()));
// Pack all your DAI
assertEq(mcd.end().bag(address(this)), 0);
mcd.end().pack(myDai);
assertEq(mcd.end().bag(address(this)), myDai);
// Should get 50 gems valued at $0.40 each
assertEq(mcd.vat().gem(DOMAIN_ILK, address(this)), 0);
mcd.end().cash(DOMAIN_ILK, myDai);
uint256 gems = mcd.vat().gem(DOMAIN_ILK, address(this));
assertApproxEqRel(gems, 50 ether, WAD / 10000);
// Exit to the remote domain
assertEq(claimToken.balanceOf(address(this)), 0);
host.exit(address(this), gems);
assertEq(mcd.vat().gem(DOMAIN_ILK, address(this)), 0);
uint256 tokens = claimToken.balanceOf(address(this));
assertApproxEqAbs(tokens, 20 ether, WAD / 10000);
// Can now get some collateral on the remote domain
claimToken.approve(address(rmcd.end()), type(uint256).max);
assertEq(rmcd.end().bag(address(this)), 0);
rmcd.end().pack(tokens);
assertEq(rmcd.end().bag(address(this)), tokens);
// Should get some of the dummy collateral gems
assertEq(rmcd.vat().gem(REMOTE_COLL_ILK, address(this)), 0);
rmcd.end().cash(REMOTE_COLL_ILK, tokens);
assertEq(rmcd.vat().gem(REMOTE_COLL_ILK, address(this)), tokens);
// We can now exit through gem join or other standard exit function
}
}