-
Notifications
You must be signed in to change notification settings - Fork 9
/
StakingBridging.js
1080 lines (864 loc) · 58.7 KB
/
StakingBridging.js
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global describe, before, beforeEach, it, context*/
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("StakingBridging", async () => {
const initialMint = "1" + "0".repeat(26);
const defaultDeposit = "1" + "0".repeat(22);
const AddressZero = ethers.constants.AddressZero;
const HashZero = ethers.constants.HashZero;
const moreThanMaxUint96 = "79228162514264337593543950337";
const chainId = 31337;
const defaultAmount = 100;
const defaultCost = 100;
const defaultGasPrice = 100;
const defaultGasLimit = "2000000";
const defaultMsgValue = "1" + "0".repeat(16);
let signers;
let deployer;
let olas;
let stakingInstance;
let stakingProxyFactory;
let dispenser;
let bridgeRelayer;
let ethereumDepositProcessor;
let arbitrumDepositProcessorL1;
let arbitrumTargetDispenserL2;
let gnosisDepositProcessorL1;
let gnosisTargetDispenserL2;
let optimismDepositProcessorL1;
let optimismTargetDispenserL2;
let polygonDepositProcessorL1;
let polygonTargetDispenserL2;
let wormholeDepositProcessorL1;
let wormholeTargetDispenserL2;
// These should not be in beforeEach.
beforeEach(async () => {
signers = await ethers.getSigners();
deployer = signers[0];
const ERC20TokenOwnerless = await ethers.getContractFactory("ERC20TokenOwnerless");
olas = await ERC20TokenOwnerless.deploy();
await olas.deployed();
const MockStakingProxy = await ethers.getContractFactory("MockStakingProxy");
stakingInstance = await MockStakingProxy.deploy(olas.address);
await stakingInstance.deployed();
const MockStakingFactory = await ethers.getContractFactory("MockStakingFactory");
stakingProxyFactory = await MockStakingFactory.deploy();
await stakingProxyFactory.deployed();
// Add a default implementation mocked as a proxy address itself
await stakingProxyFactory.addImplementation(stakingInstance.address, stakingInstance.address);
const MockStakingDispenser = await ethers.getContractFactory("MockStakingDispenser");
dispenser = await MockStakingDispenser.deploy(olas.address);
await dispenser.deployed();
const BridgeRelayer = await ethers.getContractFactory("BridgeRelayer");
bridgeRelayer = await BridgeRelayer.deploy(olas.address);
await bridgeRelayer.deployed();
const EthereumDepositProcessor = await ethers.getContractFactory("EthereumDepositProcessor");
ethereumDepositProcessor = await EthereumDepositProcessor.deploy(olas.address, dispenser.address,
stakingProxyFactory.address, deployer.address);
await ethereumDepositProcessor.deployed();
const ArbitrumDepositProcessorL1 = await ethers.getContractFactory("ArbitrumDepositProcessorL1");
// L2 Target Dispenser address is a bridge contract as well such that it matches the required msg.sender
arbitrumDepositProcessorL1 = await ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId, bridgeRelayer.address, bridgeRelayer.address,
bridgeRelayer.address);
await arbitrumDepositProcessorL1.deployed();
const bridgedRelayerDeAliased = await bridgeRelayer.l1ToL2AliasedSender();
const ArbitrumTargetDispenserL2 = await ethers.getContractFactory("ArbitrumTargetDispenserL2");
arbitrumTargetDispenserL2 = await ArbitrumTargetDispenserL2.deploy(olas.address,
stakingProxyFactory.address, bridgeRelayer.address, bridgedRelayerDeAliased, chainId);
await arbitrumTargetDispenserL2.deployed();
// Set the arbitrumTargetDispenserL2 address in arbitrumDepositProcessorL1
await arbitrumDepositProcessorL1.setL2TargetDispenser(arbitrumTargetDispenserL2.address);
// Set arbitrum addresses in a bridge contract
await bridgeRelayer.setArbitrumAddresses(arbitrumDepositProcessorL1.address, arbitrumTargetDispenserL2.address);
const GnosisDepositProcessorL1 = await ethers.getContractFactory("GnosisDepositProcessorL1");
gnosisDepositProcessorL1 = await GnosisDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId);
await gnosisDepositProcessorL1.deployed();
const GnosisTargetDispenserL2 = await ethers.getContractFactory("GnosisTargetDispenserL2");
gnosisTargetDispenserL2 = await GnosisTargetDispenserL2.deploy(olas.address,
stakingProxyFactory.address, bridgeRelayer.address, gnosisDepositProcessorL1.address, chainId);
await gnosisTargetDispenserL2.deployed();
// Set the gnosisTargetDispenserL2 address in gnosisDepositProcessorL1
await gnosisDepositProcessorL1.setL2TargetDispenser(gnosisTargetDispenserL2.address);
// Set gnosis addresses in a bridge contract
await bridgeRelayer.setGnosisAddresses(gnosisDepositProcessorL1.address, gnosisTargetDispenserL2.address);
const OptimismDepositProcessorL1 = await ethers.getContractFactory("OptimismDepositProcessorL1");
optimismDepositProcessorL1 = await OptimismDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId, olas.address);
await optimismDepositProcessorL1.deployed();
const OptimismTargetDispenserL2 = await ethers.getContractFactory("OptimismTargetDispenserL2");
optimismTargetDispenserL2 = await OptimismTargetDispenserL2.deploy(olas.address,
stakingProxyFactory.address, bridgeRelayer.address, optimismDepositProcessorL1.address, chainId);
await optimismTargetDispenserL2.deployed();
// Set the optimismTargetDispenserL2 address in optimismDepositProcessorL1
await optimismDepositProcessorL1.setL2TargetDispenser(optimismTargetDispenserL2.address);
// Set optimism addresses in a bridge contract
await bridgeRelayer.setOptimismAddresses(optimismDepositProcessorL1.address, optimismTargetDispenserL2.address);
const PolygonDepositProcessorL1 = await ethers.getContractFactory("PolygonDepositProcessorL1");
polygonDepositProcessorL1 = await PolygonDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId, olas.address, bridgeRelayer.address);
await polygonDepositProcessorL1.deployed();
const PolygonTargetDispenserL2 = await ethers.getContractFactory("PolygonTargetDispenserL2");
polygonTargetDispenserL2 = await PolygonTargetDispenserL2.deploy(olas.address,
stakingProxyFactory.address, bridgeRelayer.address, polygonDepositProcessorL1.address, chainId);
await polygonTargetDispenserL2.deployed();
// Set the polygonTargetDispenserL2 address in polygonDepositProcessorL1
await polygonDepositProcessorL1.setL2TargetDispenser(polygonTargetDispenserL2.address);
// Set the polygonDepositProcessorL1 address in polygonTargetDispenserL2
await polygonTargetDispenserL2.setFxRootTunnel(polygonDepositProcessorL1.address);
// Set polygon addresses in a bridge contract
await bridgeRelayer.setPolygonAddresses(polygonDepositProcessorL1.address, polygonTargetDispenserL2.address);
const WormholeDepositProcessorL1 = await ethers.getContractFactory("WormholeDepositProcessorL1");
wormholeDepositProcessorL1 = await WormholeDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId, bridgeRelayer.address, chainId);
await wormholeDepositProcessorL1.deployed();
const WormholeTargetDispenserL2 = await ethers.getContractFactory("WormholeTargetDispenserL2");
wormholeTargetDispenserL2 = await WormholeTargetDispenserL2.deploy(olas.address,
stakingProxyFactory.address, bridgeRelayer.address, wormholeDepositProcessorL1.address, chainId,
bridgeRelayer.address, bridgeRelayer.address);
await wormholeTargetDispenserL2.deployed();
// Set the wormholeTargetDispenserL2 address in wormholeDepositProcessorL1
await wormholeDepositProcessorL1.setL2TargetDispenser(wormholeTargetDispenserL2.address);
// Set wormhole addresses in a bridge contract
await bridgeRelayer.setWormholeAddresses(wormholeDepositProcessorL1.address, wormholeTargetDispenserL2.address);
});
context("Ethereum", async function () {
it("Should fail with incorrect constructor parameters for L1", async function () {
const EthereumDepositProcessor = await ethers.getContractFactory("EthereumDepositProcessor");
// Zero OLAS token
await expect(
EthereumDepositProcessor.deploy(AddressZero, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(ethereumDepositProcessor, "ZeroAddress");
// Zero dispenser address
await expect(
EthereumDepositProcessor.deploy(olas.address, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(ethereumDepositProcessor, "ZeroAddress");
// Zero staking factory address
await expect(
EthereumDepositProcessor.deploy(olas.address, dispenser.address, AddressZero, AddressZero)
).to.be.revertedWithCustomError(ethereumDepositProcessor, "ZeroAddress");
// Zero timelock address
await expect(
EthereumDepositProcessor.deploy(olas.address, dispenser.address, stakingProxyFactory.address, AddressZero)
).to.be.revertedWithCustomError(ethereumDepositProcessor, "ZeroAddress");
});
it("Staking deposit", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
const bridgePayload = "0x";
// Try to deposit with a non-zero msg.value
await expect(
dispenser.mintAndSend(ethereumDepositProcessor.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue})
).to.be.reverted;
await expect(
dispenser.sendMessageBatch(ethereumDepositProcessor.address, [stakingTarget], [stakingIncentive],
bridgePayload, stakingIncentive, {value: defaultMsgValue})
).to.be.reverted;
// Send a message not from the dispenser
await expect(
ethereumDepositProcessor.sendMessage(stakingTarget, stakingIncentive, bridgePayload, stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ManagerOnly");
await expect(
ethereumDepositProcessor.sendMessageBatch([stakingTarget], [stakingIncentive], bridgePayload, stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ManagerOnly");
// Deposit with funds and a correct target
await dispenser.mintAndSend(ethereumDepositProcessor.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive);
// Deposit with funds and correct targets
await dispenser.sendMessageBatch(ethereumDepositProcessor.address, [stakingTarget, stakingTarget],
[stakingIncentive, stakingIncentive], bridgePayload, 2 * stakingIncentive);
// Try to deposit to an invalid target
await expect(
dispenser.mintAndSend(ethereumDepositProcessor.address, deployer.address, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(ethereumDepositProcessor, "TargetEmissionsZero");
});
});
context("Arbitrum", async function () {
it("Should fail with incorrect constructor parameters for L1", async function () {
const ArbitrumDepositProcessorL1 = await ethers.getContractFactory("ArbitrumDepositProcessorL1");
// Zero OLAS token
await expect(
ArbitrumDepositProcessorL1.deploy(AddressZero, AddressZero, AddressZero, AddressZero,
0, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero dispenser address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, AddressZero, AddressZero, AddressZero,
0, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero L1 token relayer address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, AddressZero, AddressZero,
0, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero L1 message relayer address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address, AddressZero,
0, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero L2 chain Id
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, 0, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
// Overflow in L2 chain Id
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, ethers.constants.MaxUint256, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "Overflow");
// Zero ERC20 Gateway address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, AddressZero, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero Outbox address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, bridgeRelayer.address, AddressZero, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Zero Bridge address
await expect(
ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, bridgeRelayer.address, bridgeRelayer.address, AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
// Deploy the L1 deposit processor
const depositProcessorL1 = await ArbitrumDepositProcessorL1.deploy(olas.address, dispenser.address,
bridgeRelayer.address, bridgeRelayer.address, chainId, bridgeRelayer.address, bridgeRelayer.address,
bridgeRelayer.address);
// Try to set a zero address for the L2 target dispenser
await expect(
depositProcessorL1.setL2TargetDispenser(AddressZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
});
it("Should fail with incorrect constructor parameters for L2", async function () {
const ArbitrumTargetDispenserL2 = await ethers.getContractFactory("ArbitrumTargetDispenserL2");
// Zero OLAS token
await expect(
ArbitrumTargetDispenserL2.deploy(AddressZero, AddressZero, AddressZero, AddressZero, 0)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroAddress");
// Zero proxy factory address
await expect(
ArbitrumTargetDispenserL2.deploy(olas.address, AddressZero, AddressZero, AddressZero, 0)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroAddress");
// Zero L2 message relayer address
await expect(
ArbitrumTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, AddressZero,
AddressZero, 0)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroAddress");
// Zero L1 deposit processor address
await expect(
ArbitrumTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
AddressZero, 0)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroAddress");
// Zero L1 chain Id
await expect(
ArbitrumTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
arbitrumDepositProcessorL1.address, 0)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroValue");
// Overflow L1 chain Id
await expect(
ArbitrumTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
arbitrumDepositProcessorL1.address, ethers.constants.MaxUint256)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "Overflow");
});
it("Changing the owner and pausing / unpausing", async function () {
const account = signers[1];
// Trying to change owner from a non-owner account address
await expect(
arbitrumTargetDispenserL2.connect(account).changeOwner(account.address)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
// Trying to change owner for the zero address
await expect(
arbitrumTargetDispenserL2.connect(deployer).changeOwner(AddressZero)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroAddress");
// Changing the owner
await arbitrumTargetDispenserL2.connect(deployer).changeOwner(account.address);
// Trying to change owner from the previous owner address
await expect(
arbitrumTargetDispenserL2.connect(deployer).changeOwner(deployer.address)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
// Trying to pause and unpause from a non-owner account address
await expect(
arbitrumTargetDispenserL2.connect(deployer).pause()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
await expect(
arbitrumTargetDispenserL2.connect(deployer).unpause()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
});
it("Send message with single target and amount from L1 to L2 and back", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Try to send a message with a zero or incorrect payload
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "IncorrectDataLength");
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, defaultCost, defaultGasLimit, defaultCost]);
// Send a message on L2 with funds
await dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue});
// Get the current staking batch hash
let stakingBatchNonce = await arbitrumDepositProcessorL1.stakingBatchNonce();
let batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, arbitrumDepositProcessorL1.address]));
// Send a message on L2 without enough funds
await dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
0, {value: defaultMsgValue});
// Add more funds for the L2 target dispenser - a simulation of a late transfer incoming
await olas.mint(arbitrumTargetDispenserL2.address, stakingIncentive);
// Try to redeem funds with a wrong staking batch nonce
await expect(
arbitrumTargetDispenserL2.redeem(stakingTarget, stakingIncentive, HashZero)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "TargetAmountNotQueued");
// Redeem funds
await arbitrumTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(arbitrumDepositProcessorL1.address, deployer.address, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue});
// Check the withheld amount
const withheldAmount = await arbitrumTargetDispenserL2.withheldAmount();
expect(Number(withheldAmount)).to.equal(stakingIncentive);
// Send withheld amount from L2 to L1
await arbitrumTargetDispenserL2.syncWithheldAmount("0x");
// Try to send withheld amount from L2 to L1 when there is none
await expect(
arbitrumTargetDispenserL2.syncWithheldAmount("0x")
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
// Get staking batch hash
stakingBatchNonce = await arbitrumDepositProcessorL1.stakingBatchNonce();
batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, arbitrumDepositProcessorL1.address]));
// Process data maintenance by the owner
const payload = ethers.utils.defaultAbiCoder.encode(["address[]", "uint256[]", "bytes32"],
[[stakingTarget], [stakingIncentive * 2], batchHash]);
await arbitrumTargetDispenserL2.processDataMaintenance(payload);
// Try to do it not from the owner
await expect(
arbitrumTargetDispenserL2.connect(signers[1]).processDataMaintenance("0x")
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "OwnerOnly");
// Try to redeem, but there are no funds
await expect(
arbitrumTargetDispenserL2.redeem(stakingTarget, stakingIncentive * 2, batchHash)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "InsufficientBalance");
// Try to send a batch message on L2 with funds
await expect(
arbitrumDepositProcessorL1.sendMessageBatch([], [], "0x", 0)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ManagerOnly");
// Send a batch message on L2 with funds
await dispenser.sendMessageBatch(arbitrumDepositProcessorL1.address, [stakingTarget, stakingTarget],
[stakingIncentive, stakingIncentive], bridgePayload, stakingIncentive, {value: defaultMsgValue});
});
it("Checks during a message sending on L1", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Try to send a message with a zero or incorrect payload
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "IncorrectDataLength");
// Try executing with wrong price and gas related parameters
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[AddressZero, 1, 0, 1, 0]);
// Try to send a message with the zero address refund account
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroAddress");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, 1, 0, 1, 0]);
// Try to send a message with a small token transfer gas price
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, 0, defaultGasLimit, defaultCost]);
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, defaultCost, 1, 0]);
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, defaultCost, defaultGasLimit, 0]);
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ZeroValue");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, defaultCost, 2, defaultCost]);
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "LowerThan");
// Not enough msg.value to cover the cost
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256", "uint256", "uint256", "uint256"],
[deployer.address, defaultGasPrice, defaultCost, defaultGasLimit, defaultCost]);
await expect(
dispenser.mintAndSend(arbitrumDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "LowerThan");
// Receiving a message not from the Bridge on L1
await expect(
arbitrumDepositProcessorL1.receiveMessage("0x")
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "TargetRelayerOnly");
// Send a message not from the dispenser
await expect(
arbitrumDepositProcessorL1.sendMessage(stakingTarget, stakingIncentive, bridgePayload, stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ManagerOnly");
await expect(
arbitrumDepositProcessorL1.sendMessageBatch([stakingTarget], [stakingIncentive], bridgePayload, stakingIncentive)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "ManagerOnly");
// Try to set the L2 dispenser address again
await expect(
arbitrumDepositProcessorL1.setL2TargetDispenser(arbitrumTargetDispenserL2.address)
).to.be.revertedWithCustomError(arbitrumDepositProcessorL1, "OwnerOnly");
// Try to receive a message on L2 not by the aliased L1 deposit processor
await expect(
arbitrumTargetDispenserL2.receiveMessage("0x")
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "WrongMessageSender");
});
it("Drain functionality on L2", async function () {
// Try to drain not by the owner
await expect(
arbitrumTargetDispenserL2.connect(signers[1]).drain()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
// Try to drain with the zero amount on a contract
await expect(
arbitrumTargetDispenserL2.drain()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ZeroValue");
// Receive funds by ArbitrumTargetDispenserL2
await deployer.sendTransaction({to: arbitrumTargetDispenserL2.address, value: ethers.utils.parseEther("1")});
// Drain by the owner
await arbitrumTargetDispenserL2.drain();
});
it("Migrate functionality on L2", async function () {
// Try to migrate not by the owner
await expect(
arbitrumTargetDispenserL2.connect(signers[1]).migrate(AddressZero)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
// Try to migrate when the contract is not paused
await expect(
arbitrumTargetDispenserL2.migrate(AddressZero)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "Unpaused");
// Pause the deposit processor
await arbitrumTargetDispenserL2.pause();
// Try to migrate not to the contract address
await expect(
arbitrumTargetDispenserL2.migrate(AddressZero)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "WrongAccount");
// Try to migrate to the same contract (just to kill the contract)
await expect(
arbitrumTargetDispenserL2.migrate(arbitrumTargetDispenserL2.address)
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "WrongAccount");
// Deposit some OLAS to the contract
await olas.mint(arbitrumTargetDispenserL2.address, defaultAmount);
// Migrate the contract to another one
await arbitrumTargetDispenserL2.migrate(arbitrumDepositProcessorL1.address);
// The contract is now frozen, pause is active and reentrancy revert is applied, where possible
await expect(
arbitrumTargetDispenserL2.drain()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "ReentrancyGuard");
expect(await arbitrumTargetDispenserL2.owner()).to.equal(AddressZero);
expect(await arbitrumTargetDispenserL2.paused()).to.equal(2);
// Any ownable function is going to revert
await expect(
arbitrumTargetDispenserL2.unpause()
).to.be.revertedWithCustomError(arbitrumTargetDispenserL2, "OwnerOnly");
});
});
context("Gnosis", async function () {
it("Send message with single target and amount from L1 to L2 and back", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Send a message on L2 with funds
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive);
// Pause the L2 contract
await gnosisTargetDispenserL2.pause();
// Get the current staking batch hash
let stakingBatchNonce = await gnosisDepositProcessorL1.stakingBatchNonce();
let batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, gnosisDepositProcessorL1.address]));
// Send a message on L2 with funds when the contract is paused - it must queue the amount
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive);
// Try to redeem
await expect(
gnosisTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash)
).to.be.revertedWithCustomError(gnosisTargetDispenserL2, "Paused");
// Unpause and redeem
await gnosisTargetDispenserL2.unpause();
await gnosisTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Get the current staking batch hash
stakingBatchNonce = await gnosisDepositProcessorL1.stakingBatchNonce();
batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, gnosisDepositProcessorL1.address]));
// Send a message on L2 without enough funds
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x", 0);
// Add more funds for the L2 target dispenser - a simulation of a late transfer incoming
await olas.mint(gnosisTargetDispenserL2.address, stakingIncentive);
// Redeem funds
await gnosisTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, deployer.address, stakingIncentive, "0x",
stakingIncentive);
// Check the withheld amount
const withheldAmount = await gnosisTargetDispenserL2.withheldAmount();
expect(Number(withheldAmount)).to.equal(stakingIncentive);
// Pause the L2 contract
await gnosisTargetDispenserL2.pause();
// Trying to sync withheld tokens when paused
await expect(
gnosisTargetDispenserL2.syncWithheldAmount("0x")
).to.be.revertedWithCustomError(gnosisTargetDispenserL2, "Paused");
// Unpause and send withheld amount from L2 to L1
await gnosisTargetDispenserL2.unpause();
// Send withheld token info from L2 to L1 when the gas is going to be adjusted from zero
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [0]);
await gnosisTargetDispenserL2.syncWithheldAmount(bridgePayload);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, deployer.address, stakingIncentive, "0x",
stakingIncentive);
// Send withheld token info from L2 to L1 when the gas is going to be adjusted without any payload
await gnosisTargetDispenserL2.syncWithheldAmount("0x");
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, deployer.address, stakingIncentive, "0x",
stakingIncentive);
// Send withheld token info from L2 to L1 when the gas is going to be adjusted from being too high
bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [moreThanMaxUint96]);
await gnosisTargetDispenserL2.syncWithheldAmount(bridgePayload);
});
it("Verify senders on L1 and L2", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Set the mode for the message sender on receiving side
await bridgeRelayer.setMode(2);
// Message receive will fail on the L1 message sender
await expect(
dispenser.mintAndSend(gnosisDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x", 0)
).to.be.revertedWithCustomError(gnosisTargetDispenserL2, "WrongMessageSender");
// Set the mode back to normal for the moment
await bridgeRelayer.setMode(0);
// Send tokens to the wrong address to withhold it
await dispenser.mintAndSend(gnosisDepositProcessorL1.address, deployer.address, stakingIncentive, "0x",
stakingIncentive);
// Set the mode for the message sender on receiving side
await bridgeRelayer.setMode(2);
// Try to receive a message with the wrong sender
await expect(
gnosisTargetDispenserL2.syncWithheldAmount(HashZero)
).to.be.revertedWithCustomError(gnosisDepositProcessorL1, "WrongMessageSender");
// Deploy another bridge relayer
const BridgeRelayer = await ethers.getContractFactory("BridgeRelayer");
const bridgeRelayer2 = await BridgeRelayer.deploy(olas.address);
await bridgeRelayer2.deployed();
let bridgePayload = gnosisTargetDispenserL2.interface.encodeFunctionData("receiveMessage", ["0x00"]);
// Try to send messages via a wrong bridge relayer
await expect(
bridgeRelayer2.requireToPassMessage(gnosisTargetDispenserL2.address, bridgePayload, 0)
).to.be.revertedWithCustomError(gnosisTargetDispenserL2, "TargetRelayerOnly");
await expect(
bridgeRelayer2.requireToPassMessage(gnosisDepositProcessorL1.address, bridgePayload, 0)
).to.be.revertedWithCustomError(gnosisDepositProcessorL1, "TargetRelayerOnly");
});
});
context("Optimism", async function () {
it("Should fail with incorrect constructor parameters for L1", async function () {
const OptimismDepositProcessorL1 = await ethers.getContractFactory("OptimismDepositProcessorL1");
// Zero OLAS L2 address
await expect(
OptimismDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, AddressZero)
).to.be.revertedWithCustomError(optimismTargetDispenserL2, "ZeroAddress");
});
it("Send message with single target and amount from L1 to L2 and back", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Send a message with a zero or incorrect payload
await dispenser.mintAndSend(optimismDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive);
// Send a message with a zero cost gas limit
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [0]);
// Send a message to the wrong address such that the amount is withheld
await dispenser.mintAndSend(optimismDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive);
// Use the default gas limit
bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [defaultGasLimit]);
// Send a message on L2 with funds
await dispenser.mintAndSend(optimismDepositProcessorL1.address, stakingTarget, stakingIncentive,
bridgePayload, stakingIncentive);
// Get the current staking batch hash
let stakingBatchNonce = await optimismDepositProcessorL1.stakingBatchNonce();
let batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, optimismDepositProcessorL1.address]));
// Send a message on L2 without enough funds
await dispenser.mintAndSend(optimismDepositProcessorL1.address, stakingTarget, stakingIncentive,
bridgePayload, 0);
// Add more funds for the L2 target dispenser - a simulation of a late transfer incoming
await olas.mint(optimismTargetDispenserL2.address, stakingIncentive);
// Redeem funds
await optimismTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(optimismDepositProcessorL1.address, deployer.address, stakingIncentive,
bridgePayload, stakingIncentive);
// Check the withheld amount
const withheldAmount = await optimismTargetDispenserL2.withheldAmount();
expect(Number(withheldAmount)).to.equal(stakingIncentive);
// Send withheld amount from L2 to L1 with the zero gas limit set
bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [0]);
await optimismTargetDispenserL2.syncWithheldAmount(bridgePayload);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(optimismDepositProcessorL1.address, deployer.address, stakingIncentive, bridgePayload,
stakingIncentive);
// Send withheld amount from L2 to L1 with the more than recommended gas limit
bridgePayload = ethers.utils.defaultAbiCoder.encode(["uint256"], [moreThanMaxUint96]);
await optimismTargetDispenserL2.syncWithheldAmount(bridgePayload);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(optimismDepositProcessorL1.address, deployer.address, stakingIncentive, bridgePayload,
stakingIncentive);
// Send withheld amount from L2 to L1 without any bridge payload
await optimismTargetDispenserL2.syncWithheldAmount("0x");
});
});
context("Polygon", async function () {
it("Should fail with incorrect constructor parameters for L1", async function () {
const PolygonDepositProcessorL1 = await ethers.getContractFactory("PolygonDepositProcessorL1");
// Zero checkpoint manager contract address
await expect(
PolygonDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, AddressZero, AddressZero)
).to.be.revertedWithCustomError(polygonDepositProcessorL1, "ZeroAddress");
// Zero ERC20 predicate contract address
await expect(
PolygonDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, bridgeRelayer.address, AddressZero)
).to.be.revertedWithCustomError(polygonDepositProcessorL1, "ZeroAddress");
});
it("Send message with single target and amount from L1 to L2 and back", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Send a message on L2 with funds
await dispenser.mintAndSend(polygonDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive);
// Get the current staking batch hash
let stakingBatchNonce = await polygonDepositProcessorL1.stakingBatchNonce();
let batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, polygonDepositProcessorL1.address]));
// Send a message on L2 without enough funds
await dispenser.mintAndSend(polygonDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x", 0);
// Add more funds for the L2 target dispenser - a simulation of a late transfer incoming
await olas.mint(polygonTargetDispenserL2.address, stakingIncentive);
// Redeem funds
await polygonTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(polygonDepositProcessorL1.address, deployer.address, stakingIncentive, "0x",
stakingIncentive);
// Check the withheld amount
const withheldAmount = await polygonTargetDispenserL2.withheldAmount();
expect(Number(withheldAmount)).to.equal(stakingIncentive);
// Send withheld amount from L2 to L1
await polygonTargetDispenserL2.syncWithheldAmount("0x");
});
});
context("Wormhole", async function () {
it("Should fail with incorrect constructor parameters for L1", async function () {
const WormholeDepositProcessorL1 = await ethers.getContractFactory("WormholeDepositProcessorL1");
// Zero L1 wormhole core contract address
await expect(
WormholeDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, AddressZero, 0)
).to.be.revertedWithCustomError(wormholeDepositProcessorL1, "ZeroAddress");
// Zero wormhole L2 chain Id
await expect(
WormholeDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, bridgeRelayer.address, 0)
).to.be.revertedWithCustomError(wormholeDepositProcessorL1, "ZeroValue");
// Overflow wormhole L2 chain Id
await expect(
WormholeDepositProcessorL1.deploy(olas.address, dispenser.address, bridgeRelayer.address,
bridgeRelayer.address, chainId, bridgeRelayer.address, 2**16 + 1)
).to.be.revertedWithCustomError(wormholeDepositProcessorL1, "Overflow");
});
it("Should fail with incorrect constructor parameters for L2", async function () {
const WormholeTargetDispenserL2 = await ethers.getContractFactory("WormholeTargetDispenserL2");
// Zero L2 wormhole core contract address
await expect(
WormholeTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
wormholeDepositProcessorL1.address, chainId, AddressZero, AddressZero)
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "ZeroAddress");
// Zero L2 token relayer contract address
await expect(
WormholeTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
wormholeDepositProcessorL1.address, chainId, bridgeRelayer.address, AddressZero)
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "ZeroAddress");
// Overflow L1 wormhole chain Id
await expect(
WormholeTargetDispenserL2.deploy(olas.address, stakingProxyFactory.address, bridgeRelayer.address,
wormholeDepositProcessorL1.address, 2**16 + 1, bridgeRelayer.address, bridgeRelayer.address)
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "Overflow");
});
it("Send message with single target and amount from L1 to L2 and back", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Try to send a message with a zero or incorrect payload
await expect(
dispenser.mintAndSend(wormholeDepositProcessorL1.address, stakingTarget, stakingIncentive, "0x",
stakingIncentive)
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "IncorrectDataLength");
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"],
[AddressZero, defaultGasLimit]);
// Try to send a message with a zero address refund account
await expect(
dispenser.mintAndSend(wormholeDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue})
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "ZeroAddress");
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"],
[deployer.address, defaultGasLimit]);
// Send a message on L2 with funds
await dispenser.mintAndSend(wormholeDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue});
// Get the current staking batch hash
let stakingBatchNonce = await wormholeDepositProcessorL1.stakingBatchNonce();
let batchHash = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["uint256", "uint256", "address"],
[stakingBatchNonce, chainId, wormholeDepositProcessorL1.address]));
// Send a message on L2 without enough funds
await dispenser.mintAndSend(wormholeDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload,
0, {value: defaultMsgValue});
// Add more funds for the L2 target dispenser - a simulation of a late transfer incoming
await olas.mint(wormholeTargetDispenserL2.address, stakingIncentive);
// Redeem funds
await wormholeTargetDispenserL2.redeem(stakingTarget, stakingIncentive, batchHash);
// Send a message on L2 with funds for a wrong address
await dispenser.mintAndSend(wormholeDepositProcessorL1.address, deployer.address, stakingIncentive, bridgePayload,
stakingIncentive, {value: defaultMsgValue});
// Check the withheld amount
const withheldAmount = await wormholeTargetDispenserL2.withheldAmount();
expect(Number(withheldAmount)).to.equal(stakingIncentive);
// Try to send withheld amount from L2 to L1 with insufficient normalized withheld amount
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [deployer.address, 0]);
await expect(
wormholeTargetDispenserL2.syncWithheldAmount(bridgePayload, {value: defaultMsgValue})
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "ZeroValue");
// Send a message on L2 with funds for a wrong address with a bigger amount
await dispenser.mintAndSend(wormholeDepositProcessorL1.address, deployer.address, ethers.utils.parseEther("1"),
bridgePayload, ethers.utils.parseEther("1"), {value: defaultMsgValue});
// Try to send withheld amount from L2 to L1 with a zero refund address
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [AddressZero, 0]);
await expect(
wormholeTargetDispenserL2.syncWithheldAmount(bridgePayload, {value: defaultMsgValue})
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "ZeroAddress");
// Send withheld amount from L2 to L1
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [deployer.address, 0]);
await wormholeTargetDispenserL2.syncWithheldAmount(bridgePayload, {value: defaultMsgValue});
});
it("Checks during a message sending on L1 and L2", async function () {
// Encode the staking data to emulate it being received on L2
const stakingTarget = stakingInstance.address;
const stakingIncentive = defaultAmount;
// Try to send a message withhout any payment for the message cost
let bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"],
[deployer.address, 0]);
await expect(
dispenser.mintAndSend(wormholeDepositProcessorL1.address, stakingTarget, stakingIncentive, bridgePayload, 0)
).to.be.revertedWithCustomError(wormholeDepositProcessorL1, "LowerThan");
// Try to receive a message by a wrong chain Id
await expect(
wormholeDepositProcessorL1.receiveWormholeMessages("0x", [], HashZero, 0, HashZero)
).to.be.revertedWithCustomError(wormholeDepositProcessorL1, "WrongChainId");
// Send a message on L2 with funds for a wrong address with enough funds for the normalized withheld amount
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"],
[deployer.address, defaultGasLimit]);
await dispenser.mintAndSend(wormholeDepositProcessorL1.address, deployer.address, ethers.utils.parseEther("1"),
bridgePayload, ethers.utils.parseEther("1"), {value: defaultMsgValue});
// Try to send withheld tokens with an incorrect payload
await expect(
wormholeTargetDispenserL2.syncWithheldAmount("0x")
).to.be.revertedWithCustomError(wormholeTargetDispenserL2, "IncorrectDataLength");
// Try to send withheld tokens without any msg.value covering the cost
bridgePayload = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [deployer.address, 0]);
await expect(
wormholeTargetDispenserL2.syncWithheldAmount(bridgePayload)