-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcross-chain-strategy.js
More file actions
894 lines (728 loc) · 31.2 KB
/
Copy pathcross-chain-strategy.js
File metadata and controls
894 lines (728 loc) · 31.2 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
const { expect } = require("chai");
const { isCI, ousdUnits } = require("../../helpers");
const {
createFixtureLoader,
crossChainFixtureUnit,
} = require("../../_fixture");
const { setERC20TokenBalance } = require("../../_fund");
const { units, usdcUnits } = require("../../helpers");
const { impersonateAndFund } = require("../../../utils/signers");
const {
encodeBalanceCheckMessageBody,
encodeCCTPMessage,
} = require("./_crosschain-helpers");
const loadFixture = createFixtureLoader(crossChainFixtureUnit);
const DAY_IN_SECONDS = 86400;
describe("ForkTest: CrossChainRemoteStrategy", function () {
this.timeout(0);
// Retry up to 3 times on CI
this.retries(isCI ? 3 : 0);
let fixture,
josh,
governor,
usdc,
crossChainRemoteStrategy,
crossChainMasterStrategy,
vault,
initialVaultValue;
beforeEach(async () => {
fixture = await loadFixture();
josh = fixture.josh;
governor = fixture.governor;
usdc = fixture.usdc;
crossChainRemoteStrategy = fixture.crossChainRemoteStrategy;
crossChainMasterStrategy = fixture.crossChainMasterStrategy;
vault = fixture.vault;
initialVaultValue = await vault.totalValue();
});
const mint = async (amount) => {
await usdc.connect(josh).approve(vault.address, await units(amount, usdc));
await vault.connect(josh).mint(await units(amount, usdc));
};
const depositToMasterStrategy = async (amount) => {
await vault
.connect(governor)
.depositToStrategy(
crossChainMasterStrategy.address,
[usdc.address],
[await units(amount, usdc)]
);
};
// Even though remote strategy has funds withdrawn the message initiates on master strategy
const withdrawFromRemoteStrategy = (amount) => {
return vault
.connect(governor)
.withdrawFromStrategy(
crossChainMasterStrategy.address,
[usdc.address],
[usdcUnits(amount, usdc)]
);
};
const withdrawAllFromRemoteStrategy = () => {
return vault
.connect(governor)
.withdrawAllFromStrategy(crossChainMasterStrategy.address);
};
// Withdraws from the remote strategy directly, without going through the master strategy
const directWithdrawFromRemoteStrategy = async (amount) => {
await crossChainRemoteStrategy
.connect(governor)
.withdraw(
crossChainRemoteStrategy.address,
usdc.address,
await units(amount, usdc)
);
};
// Withdraws all the remote strategy directly, without going through the master strategy
const directWithdrawAllFromRemoteStrategy = async () => {
await crossChainRemoteStrategy.connect(governor).withdrawAll();
};
const sendBalanceUpdateToMaster = async () => {
await crossChainRemoteStrategy.connect(governor).sendBalanceUpdate();
};
it("Should wire morpho vault and liquidity adapter in fixture", async function () {
const { morphoVault, morphoVaultLiquidityAdapter } = fixture;
await expect(await morphoVault.liquidityAdapter()).to.eq(
morphoVaultLiquidityAdapter.address
);
await expect(await morphoVaultLiquidityAdapter.morphoVaultV1()).to.eq(
morphoVault.address
);
await expect(await morphoVaultLiquidityAdapter.parentVault()).to.eq(
morphoVault.address
);
});
it("Should revert withdrawAll when morpho vault liquidity adapter is incompatible", async function () {
const { morphoVault } = fixture;
// Misconfigure adapter to an invalid value that does not implement IMorphoV2Adapter.
await morphoVault
.connect(governor)
.setLiquidityAdapter(morphoVault.address);
await expect(
crossChainRemoteStrategy.connect(governor).withdrawAll()
).to.be.revertedWithCustomError("IncompatibleAdapter(address)");
});
// Checks the diff in the total expected value in the vault
// (plus accompanying strategy value)
const assertVaultTotalValue = async (amountExpected) => {
const amountToCompare =
typeof amountExpected === "string"
? ousdUnits(amountExpected)
: amountExpected;
await expect((await vault.totalValue()).sub(initialVaultValue)).to.eq(
amountToCompare
);
};
const mintToMasterDepositToRemote = async (amount) => {
const { messageTransmitter, morphoVault } = fixture;
const amountBn = await units(amount, usdc);
await mint(amount);
const vaultDiffAfterMint = (await vault.totalValue()).sub(
initialVaultValue
);
const remoteBalanceBefore = await crossChainRemoteStrategy.checkBalance(
usdc.address
);
const remoteBalanceRecByMasterBefore =
await crossChainMasterStrategy.remoteStrategyBalance();
const messagesinQueueBefore = await messageTransmitter.messagesInQueue();
await assertVaultTotalValue(vaultDiffAfterMint);
await depositToMasterStrategy(amount);
await expect(await messageTransmitter.messagesInQueue()).to.eq(
messagesinQueueBefore + 1
);
await assertVaultTotalValue(vaultDiffAfterMint);
// Simulate off chain component processing deposit message
await expect(messageTransmitter.processFront())
.to.emit(crossChainRemoteStrategy, "Deposit")
.withArgs(usdc.address, morphoVault.address, amountBn);
await assertVaultTotalValue(vaultDiffAfterMint);
// 1 message is processed, another one (checkBalance) has entered the queue
await expect(await messageTransmitter.messagesInQueue()).to.eq(
messagesinQueueBefore + 1
);
await expect(
await morphoVault.balanceOf(crossChainRemoteStrategy.address)
).to.eq(remoteBalanceBefore.add(amountBn));
// Simulate off chain component processing checkBalance message
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(remoteBalanceBefore.add(amountBn));
await expect(await messageTransmitter.messagesInQueue()).to.eq(
messagesinQueueBefore
);
await assertVaultTotalValue(vaultDiffAfterMint);
await expect(await crossChainMasterStrategy.remoteStrategyBalance()).to.eq(
remoteBalanceRecByMasterBefore.add(amountBn)
);
};
const withdrawFromRemoteToVault = async (amount, expectWithdrawalEvent) => {
const { messageTransmitter, morphoVault } = fixture;
const amountBn = await units(amount, usdc);
const remoteBalanceBefore = await crossChainRemoteStrategy.checkBalance(
usdc.address
);
const remoteBalanceRecByMasterBefore =
await crossChainMasterStrategy.remoteStrategyBalance();
const messagesinQueueBefore = await messageTransmitter.messagesInQueue();
await withdrawFromRemoteStrategy(amount);
await expect(await messageTransmitter.messagesInQueue()).to.eq(
messagesinQueueBefore + 1
);
if (expectWithdrawalEvent) {
await expect(messageTransmitter.processFront())
.to.emit(crossChainRemoteStrategy, "Withdrawal")
.withArgs(usdc.address, morphoVault.address, amountBn)
.to.emit(crossChainRemoteStrategy, "TokensBridged");
} else {
await expect(messageTransmitter.processFront()).to.emit(
crossChainRemoteStrategy,
"TokensBridged"
);
}
await expect(await messageTransmitter.messagesInQueue()).to.eq(
messagesinQueueBefore + 1
);
// master strategy still has the old value fo the remote strategy balance
await expect(await crossChainMasterStrategy.remoteStrategyBalance()).to.eq(
remoteBalanceRecByMasterBefore
);
const remoteBalanceAfter = remoteBalanceBefore - amountBn;
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(remoteBalanceAfter);
// Simulate off chain component processing checkBalance message
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(remoteBalanceAfter);
await expect(await crossChainMasterStrategy.remoteStrategyBalance()).to.eq(
remoteBalanceAfter
);
};
it("Should mint USDC to master strategy, transfer to remote and update balance", async function () {
const { morphoVault } = fixture;
await assertVaultTotalValue("0");
await expect(await morphoVault.totalAssets()).to.eq(await units("0", usdc));
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
});
it("Should be able to withdraw from the remote strategy", async function () {
const { morphoVault } = fixture;
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
await withdrawFromRemoteToVault("500", true);
await assertVaultTotalValue("1000");
});
it("Should be able to direct withdraw from the remote strategy directly and collect to master", async function () {
const { morphoVault } = fixture;
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
await directWithdrawFromRemoteStrategy("500");
await assertVaultTotalValue("1000");
// 500 has been withdrawn from the Morpho vault but still remains on the
// remote strategy
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("1000", usdc));
// Next withdraw should not withdraw any additional funds from Morpho and just send
// 450 USDC to the master.
await withdrawFromRemoteToVault("450", false);
await assertVaultTotalValue("1000");
// The remote strategy should have 500 USDC in Morpho vault and 50 USDC on the contract
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("550", usdc));
await expect(await usdc.balanceOf(crossChainRemoteStrategy.address)).to.eq(
await units("50", usdc)
);
});
it("Should be able to direct withdraw from the remote strategy directly and withdrawing More from Morpho when collecting to the master", async function () {
const { morphoVault } = fixture;
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
await directWithdrawFromRemoteStrategy("500");
await assertVaultTotalValue("1000");
// 500 has been withdrawn from the Morpho vault but still remains on the
// remote strategy
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("1000", usdc));
// Next withdraw should withdraw 50 additional funds and send them with existing
// 500 USDC to the master.
await withdrawFromRemoteToVault("550", false);
await assertVaultTotalValue("1000");
// The remote strategy should have 500 USDC in Morpho vault and 50 USDC on the contract
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("450", usdc));
await expect(await usdc.balanceOf(crossChainRemoteStrategy.address)).to.eq(
await units("0", usdc)
);
});
it("Should fail when a withdrawal too large is requested", async function () {
const { morphoVault } = fixture;
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
// Master strategy should prevent withdrawing more than is available in the remote strategy
await expect(withdrawFromRemoteStrategy("1001")).to.be.revertedWith(
"Withdraw amount exceeds remote strategy balance"
);
await assertVaultTotalValue("1000");
});
it("Should be able to direct withdraw all from the remote strategy directly and collect to master", async function () {
const { morphoVault, messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await expect(await morphoVault.totalAssets()).to.eq(
await units("1000", usdc)
);
await directWithdrawAllFromRemoteStrategy();
await assertVaultTotalValue("1000");
// All has been withdrawn from the Morpho vault but still remains on the
// remote strategy
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("1000", usdc));
await withdrawFromRemoteStrategy("1000");
await expect(messageTransmitter.processFront()).not.to.emit(
crossChainRemoteStrategy,
"WithdrawUnderlyingFailed"
);
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(await units("0", usdc));
await assertVaultTotalValue("1000");
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("0", usdc));
// calling withdrawAll a second time should not fail
await directWithdrawAllFromRemoteStrategy();
});
it("Should fail when a withdrawal too large is requested on the remote strategy", async function () {
const { messageTransmitter } = fixture;
const remoteStrategySigner = await impersonateAndFund(
crossChainRemoteStrategy.address
);
await mintToMasterDepositToRemote("1000");
await assertVaultTotalValue("1000");
await directWithdrawFromRemoteStrategy("10");
// Trick the remote strategy into thinking it has 10 USDC more than it actually does
await usdc
.connect(remoteStrategySigner)
.transfer(vault.address, await units("10", usdc));
// Vault has 10 USDC more & Master strategy still thinks it has 1000 USDC
await assertVaultTotalValue("1010");
// This step should fail because the remote strategy no longer holds 1000 USDC
await withdrawFromRemoteStrategy("1000");
// Process on remote strategy
await expect(messageTransmitter.processFront())
.to.emit(crossChainRemoteStrategy, "WithdrawalFailed")
.withArgs(await units("1000", usdc), await units("0", usdc));
// Process on master strategy
// This event doesn't get triggerred as the master strategy considers the balance check update
// as a race condition, and is exoecting an "on TokenReceived " to be called instead
// which also causes the master strategy not to update the balance of the remote strategy
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(await units("990", usdc));
await expect(await messageTransmitter.messagesInQueue()).to.eq(0);
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("990", usdc));
await expect(
await crossChainMasterStrategy.checkBalance(usdc.address)
).to.eq(await units("990", usdc));
});
it("Should be able to process withdrawal & checkBalance on Remote strategy and in reverse order on master strategy", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
// Process on remote strategy
await expect(messageTransmitter.processFront());
// This sends a second balanceUpdate message to the CCTP bridge
await sendBalanceUpdateToMaster();
await expect(await messageTransmitter.messagesInQueue()).to.eq(2);
// first process the standalone balanceCheck message - meaning we process messages out of order
// this message should be ignored on Master
await expect(messageTransmitter.processBack()).to.not.emit(
crossChainMasterStrategy,
"RemoteStrategyBalanceUpdated"
);
// Second balance update message is part of the deposit / withdrawal process and should be processed
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(await units("700", usdc));
await expect(await messageTransmitter.messagesInQueue()).to.eq(0);
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(await units("700", usdc));
await expect(
await crossChainMasterStrategy.checkBalance(usdc.address)
).to.eq(await units("700", usdc));
await assertVaultTotalValue("1000");
});
it("Should reject direct replay of an already-relayed balance update message", async function () {
const { messageTransmitter } = fixture;
await sendBalanceUpdateToMaster();
const nonce = await crossChainMasterStrategy.lastTransferNonce();
const balance = await crossChainRemoteStrategy.checkBalance(usdc.address);
const latestBlock = await ethers.provider.getBlock("latest");
const body = encodeBalanceCheckMessageBody(
nonce,
balance,
false,
latestBlock.timestamp
);
const encodedMessage = encodeCCTPMessage(
await crossChainMasterStrategy.peerDomainID(),
crossChainRemoteStrategy.address,
crossChainMasterStrategy.address,
body
);
await expect(messageTransmitter.processFront())
.to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated")
.withArgs(balance);
await expect(await messageTransmitter.messagesInQueue()).to.eq(0);
const transmitterSigner = await impersonateAndFund(messageTransmitter.address);
await expect(
crossChainMasterStrategy
.connect(transmitterSigner)
.relay(encodedMessage, "0x")
).to.be.revertedWith("Message already processed");
});
it("Should emit a BalanceCheckIgnored event if balance update message is too old", async function () {
const { messageTransmitter, crossChainMasterStrategy } = fixture;
await sendBalanceUpdateToMaster();
const latestBlock = await ethers.provider.getBlock("latest");
const staleTimestamp = latestBlock.timestamp - DAY_IN_SECONDS - 1;
await expect(
messageTransmitter.processFrontOverrideMessageBody(
encodeBalanceCheckMessageBody("0", "1000", true, staleTimestamp)
)
)
.to.emit(crossChainMasterStrategy, "BalanceCheckIgnored")
.withArgs(0, staleTimestamp, true);
await expect(await messageTransmitter.messagesInQueue()).to.eq(0);
});
it("Should emit a RemoteStrategyBalanceUpdated event if balance update message is just in time", async function () {
const { messageTransmitter, crossChainMasterStrategy } = fixture;
await sendBalanceUpdateToMaster();
const latestBlock = await ethers.provider.getBlock("latest");
const staleTimestamp = latestBlock.timestamp - DAY_IN_SECONDS + 10;
await expect(
messageTransmitter.processFrontOverrideMessageBody(
encodeBalanceCheckMessageBody("0", "1000", true, staleTimestamp)
)
).to.emit(crossChainMasterStrategy, "RemoteStrategyBalanceUpdated");
await expect(await messageTransmitter.messagesInQueue()).to.eq(0);
});
it("Should fail on deposit if a previous one has not completed", async function () {
await mint("100");
await depositToMasterStrategy("50");
await expect(depositToMasterStrategy("50")).to.be.revertedWith(
"Unexpected pending amount"
);
});
it("Should fail to withdraw if a previous deposit has not completed", async function () {
await mintToMasterDepositToRemote("40");
await mint("50");
await depositToMasterStrategy("50");
await expect(withdrawFromRemoteStrategy("40")).to.be.revertedWith(
"Pending token transfer"
);
});
it("Should fail on deposit if a previous withdrawal has not completed", async function () {
await mintToMasterDepositToRemote("230");
await withdrawFromRemoteStrategy("50");
await mint("30");
await expect(depositToMasterStrategy("30")).to.be.revertedWith(
"Pending token transfer"
);
});
it("Should fail to withdraw if a previous withdrawal has not completed", async function () {
await mintToMasterDepositToRemote("230");
await withdrawFromRemoteStrategy("50");
await expect(withdrawFromRemoteStrategy("40")).to.be.revertedWith(
"Pending token transfer"
);
});
it("Should fail to deposit non usdc asset", async function () {
const { ousd, vault, vaultSigner, josh, crossChainMasterStrategy } =
fixture;
await mint("10");
await ousd.connect(josh).transfer(vault.address, await units("10", ousd));
await expect(
crossChainMasterStrategy
.connect(vaultSigner)
.deposit(ousd.address, await units("10", ousd))
).to.be.revertedWith("Unsupported asset");
});
it("Should not deposit less than 1 USDC using normal depositAll approach", async function () {
await mint("1");
// DepositAll function doesn't call _deposit when amount is less than 1 USDC
await expect(
vault
.connect(governor)
.depositToStrategy(
crossChainMasterStrategy.address,
[usdc.address],
[await units("0.5", usdc)]
)
).not.to.emit(crossChainMasterStrategy, "Deposit");
});
it("Should revert when depositing less than 1 USDC", async function () {
const { usdc, vaultSigner, crossChainMasterStrategy } = fixture;
await mint("10");
await expect(
crossChainMasterStrategy
.connect(vaultSigner)
.deposit(usdc.address, await units("0.5", usdc))
).to.be.revertedWith("Deposit amount too small");
});
it("Should not calling withdrawAll if one withdraw is pending", async function () {
await mintToMasterDepositToRemote("10");
await withdrawFromRemoteStrategy("5");
// 1 withdraw is pending, the withdrawAll won't issue another withdraw, but wont fail as well
await expect(withdrawAllFromRemoteStrategy()).to.not.emit(
crossChainMasterStrategy,
"WithdrawRequested"
);
});
it("Should not calling withdrawAll when to little balance is on the remote strategy", async function () {
await mintToMasterDepositToRemote("10");
await withdrawFromRemoteToVault("9.5", true);
await expect(
await crossChainRemoteStrategy.checkBalance(usdc.address)
).to.eq(usdcUnits("0.5"));
// Remote only has 0.5 USDC left, the withdrawAll won't issue another withdraw, but wont fail as well
await expect(withdrawAllFromRemoteStrategy()).to.not.emit(
crossChainMasterStrategy,
"WithdrawRequested"
);
});
it("Should revert if withdrawal amount is too small", async function () {
await mintToMasterDepositToRemote("10");
await expect(withdrawFromRemoteStrategy("0.9")).to.be.revertedWith(
"Withdraw amount too small"
);
});
it("Should revert if withdrawal exceeds remote strategy balance", async function () {
await mintToMasterDepositToRemote("10");
await expect(withdrawFromRemoteStrategy("11")).to.be.revertedWith(
"Withdraw amount exceeds remote strategy balance"
);
});
it("Should revert if withdrawal exceeds max transfer amount", async function () {
await setERC20TokenBalance(josh.address, usdc, "100000000");
await mintToMasterDepositToRemote("9000000");
await mintToMasterDepositToRemote("9000000");
await expect(withdrawFromRemoteStrategy("10000001")).to.be.revertedWith(
"Withdraw amount exceeds max transfer amount"
);
});
it("Should revert if balance update on the remote strategy is not called by operator or governor or strategist", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy.connect(josh).sendBalanceUpdate()
).to.be.revertedWith(
"Caller is not the Operator, Strategist or the Governor"
);
});
it("Should revert if deposit on the remote strategy is not called by the governor or strategist", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy
.connect(josh)
.deposit(usdc.address, await units("10", usdc))
).to.be.revertedWith("Caller is not the Strategist or Governor");
});
it("Should revert if depositAll on the remote strategy is not called by the governor or strategist", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy.connect(josh).depositAll()
).to.be.revertedWith("Caller is not the Strategist or Governor");
});
it("Should revert if withdraw on the remote strategy is not called by the governor or strategist", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy
.connect(josh)
.withdraw(vault.address, usdc.address, await units("10", usdc))
).to.be.revertedWith("Caller is not the Strategist or Governor");
});
it("Should revert if withdrawAll on the remote strategy is not called by the governor or strategist", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy.connect(josh).withdrawAll()
).to.be.revertedWith("Caller is not the Strategist or Governor");
});
it("Should revert if depositing 0 amount", async function () {
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy
.connect(governor)
.deposit(usdc.address, await units("0", usdc))
).to.be.revertedWith("Must deposit something");
});
it("Should revert if not depositing USDC", async function () {
const { ousd } = fixture;
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy
.connect(governor)
.deposit(ousd.address, await units("10", ousd))
).to.be.revertedWith("Unexpected asset address");
});
it("Check balance on the remote strategy should revert when not passing USDC address", async function () {
const { ousd } = fixture;
await mintToMasterDepositToRemote("10");
await expect(
crossChainRemoteStrategy.checkBalance(ousd.address)
).to.be.revertedWith("Unexpected asset address");
});
it("Check balance on the remote strategy should revert when not passing USDC address", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
// Process on remote strategy
await expect(
messageTransmitter.processFrontOverrideHeader("0x00000001")
).to.be.revertedWith("Unsupported message version");
});
it("Should revert if setMinFinalityThreshold does not equal 1000 or 2000", async function () {
await expect(
crossChainMasterStrategy.connect(governor).setMinFinalityThreshold(1001)
).to.be.revertedWith("Invalid threshold");
await expect(
crossChainMasterStrategy.connect(governor).setMinFinalityThreshold(2001)
).to.be.revertedWith("Invalid threshold");
});
it("Should set min finality threshold to 1000", async function () {
await crossChainMasterStrategy
.connect(governor)
.setMinFinalityThreshold(1000);
await expect(await crossChainMasterStrategy.minFinalityThreshold()).to.eq(
1000
);
});
it("Should set min finality threshold to 2000", async function () {
await crossChainMasterStrategy
.connect(governor)
.setMinFinalityThreshold(2000);
await expect(await crossChainMasterStrategy.minFinalityThreshold()).to.eq(
2000
);
});
it("Should set fee premium to 1000 bps successfully", async function () {
const initialFeeBps = await crossChainMasterStrategy.feePremiumBps();
expect(initialFeeBps).to.equal(0); // Default is 0
await expect(
crossChainMasterStrategy.connect(governor).setFeePremiumBps(1000)
)
.to.emit(crossChainMasterStrategy, "CCTPFeePremiumBpsSet")
.withArgs(1000);
// Verify state updated
expect(await crossChainMasterStrategy.feePremiumBps()).to.equal(1000);
});
it("Should revert when setting fee premium >3000 bps", async function () {
await expect(
crossChainMasterStrategy.connect(governor).setFeePremiumBps(3001)
).to.be.revertedWith("Fee premium too high");
});
it("Should revert if sender of the message is not correct", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
await messageTransmitter.connect(josh).overrideSender(josh.address);
// Process on remote strategy
await expect(messageTransmitter.processFront()).to.be.revertedWith(
"Unknown Sender"
);
});
it("Should revert if unfinalized messages are not supported", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
await messageTransmitter.connect(josh).overrideMessageFinality(1000);
// Process on remote strategy
await expect(messageTransmitter.processFront()).to.be.revertedWith(
"Unfinalized messages are not supported"
);
});
it("Should accept unfinalized messages if min finality threshold is set to 1000", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
await crossChainRemoteStrategy
.connect(governor)
.setMinFinalityThreshold(1000);
await messageTransmitter.connect(josh).overrideMessageFinality(1000);
// Process on remote strategy
await messageTransmitter.processFront();
});
it("Should revert is message finality is below 1000", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
await crossChainRemoteStrategy
.connect(governor)
.setMinFinalityThreshold(1000);
await messageTransmitter.connect(josh).overrideMessageFinality(999);
// Process on remote strategy
await expect(messageTransmitter.processFront()).to.be.revertedWith(
"Finality threshold too low"
);
});
it("Should revert if the source domain is not correct", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
await messageTransmitter.connect(josh).overrideSourceDomain(444);
// Process on remote strategy
await expect(messageTransmitter.processFront()).to.be.revertedWith(
"Unknown Source Domain"
);
});
it("Should revert if incorrect cctp message version is used", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
// Process on remote strategy
await expect(
messageTransmitter.processFrontOverrideVersion(2)
).to.be.revertedWith("Invalid CCTP message version");
});
it("Should revert if incorrect sender is used in the message header", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
// Process on remote strategy
await expect(
messageTransmitter.processFrontOverrideSender(josh.address)
).to.be.revertedWith("Incorrect sender/recipient address");
});
it("Should revert if incorrect sender is used in the message header", async function () {
const { messageTransmitter } = fixture;
await mintToMasterDepositToRemote("1000");
await withdrawFromRemoteStrategy("300");
// Process on remote strategy
await expect(
messageTransmitter.processFrontOverrideRecipient(josh.address)
).to.be.revertedWith("Unexpected recipient address");
});
});