-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathIntegrationStandalone.t.sol
executable file
·724 lines (604 loc) · 29.2 KB
/
IntegrationStandalone.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
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
// SPDX-License-Identifier: Apache 2
pragma solidity >=0.8.8 <0.9.0;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import "../src/NttManager/NttManager.sol";
import "../src/Transceiver/Transceiver.sol";
import "../src/interfaces/INttManager.sol";
import "../src/interfaces/IRateLimiter.sol";
import "../src/interfaces/ITransceiver.sol";
import "../src/interfaces/IManagerBase.sol";
import "../src/interfaces/IRateLimiterEvents.sol";
import {Utils} from "./libraries/Utils.sol";
import {DummyToken, DummyTokenMintAndBurn} from "./NttManager.t.sol";
import "../src/interfaces/IWormholeTransceiver.sol";
import {WormholeTransceiver} from "../src/Transceiver/WormholeTransceiver/WormholeTransceiver.sol";
import "../src/libraries/TransceiverStructs.sol";
import "./mocks/MockNttManager.sol";
import "./mocks/MockTransceivers.sol";
import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "wormhole-solidity-sdk/interfaces/IWormhole.sol";
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
import "wormhole-solidity-sdk/Utils.sol";
//import "wormhole-solidity-sdk/testing/WormholeRelayerTest.sol";
contract TestEndToEndBase is Test, IRateLimiterEvents {
NttManager nttManagerChain1;
NttManager nttManagerChain2;
using TrimmedAmountLib for uint256;
using TrimmedAmountLib for TrimmedAmount;
uint16 constant chainId1 = 7;
uint16 constant chainId2 = 100;
uint8 constant FAST_CONSISTENCY_LEVEL = 200;
uint256 constant GAS_LIMIT = 500000;
uint16 constant SENDING_CHAIN_ID = 1;
uint256 constant DEVNET_GUARDIAN_PK =
0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0;
WormholeSimulator guardian;
uint256 initialBlockTimestamp;
WormholeTransceiver wormholeTransceiverChain1;
WormholeTransceiver wormholeTransceiverChain2;
address userA = address(0x123);
address userB = address(0x456);
address userC = address(0x789);
address userD = address(0xABC);
address relayer = address(0x28D8F1Be96f97C1387e94A53e00eCcFb4E75175a);
IWormhole wormhole = IWormhole(0x4a8bc80Ed5a4067f1CCf107057b8270E0cC11A78);
function setUp() public {
string memory url = "https://ethereum-sepolia-rpc.publicnode.com";
vm.createSelectFork(url);
initialBlockTimestamp = vm.getBlockTimestamp();
guardian = new WormholeSimulator(address(wormhole), DEVNET_GUARDIAN_PK);
vm.chainId(chainId1);
DummyToken t1 = new DummyToken();
NttManager implementation = new MockNttManagerContract(
address(t1), IManagerBase.Mode.LOCKING, chainId1, 1 days, false
);
nttManagerChain1 =
MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
nttManagerChain1.initialize();
WormholeTransceiver wormholeTransceiverChain1Implementation = new MockWormholeTransceiverContract(
address(nttManagerChain1),
address(wormhole),
address(relayer),
address(0x0),
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain1 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain1Implementation), ""))
);
// Only the deployer should be able to initialize
vm.prank(userA);
vm.expectRevert(
abi.encodeWithSelector(ITransceiver.UnexpectedDeployer.selector, address(this), userA)
);
wormholeTransceiverChain1.initialize();
// Actually initialize properly now
wormholeTransceiverChain1.initialize();
nttManagerChain1.setTransceiver(address(wormholeTransceiverChain1));
nttManagerChain1.setOutboundLimit(type(uint64).max);
nttManagerChain1.setInboundLimit(type(uint64).max, chainId2);
// Chain 2 setup
vm.chainId(chainId2);
DummyToken t2 = new DummyTokenMintAndBurn();
NttManager implementationChain2 = new MockNttManagerContract(
address(t2), IManagerBase.Mode.BURNING, chainId2, 1 days, false
);
nttManagerChain2 =
MockNttManagerContract(address(new ERC1967Proxy(address(implementationChain2), "")));
nttManagerChain2.initialize();
WormholeTransceiver wormholeTransceiverChain2Implementation = new MockWormholeTransceiverContract(
address(nttManagerChain2),
address(wormhole),
address(relayer),
address(0x0),
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain2 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain2Implementation), ""))
);
wormholeTransceiverChain2.initialize();
nttManagerChain2.setTransceiver(address(wormholeTransceiverChain2));
nttManagerChain2.setOutboundLimit(type(uint64).max);
nttManagerChain2.setInboundLimit(type(uint64).max, chainId1);
// Register peer contracts for the nttManager and transceiver. Transceivers and nttManager each have the concept of peers here.
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9, type(uint64).max
);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7, type(uint64).max
);
// Set peers for the transceivers
wormholeTransceiverChain1.setWormholePeer(
chainId2, bytes32(uint256(uint160(address(wormholeTransceiverChain2))))
);
wormholeTransceiverChain2.setWormholePeer(
chainId1, bytes32(uint256(uint160(address(wormholeTransceiverChain1))))
);
require(nttManagerChain1.getThreshold() != 0, "Threshold is zero with active transceivers");
// Actually set it
nttManagerChain1.setThreshold(1);
nttManagerChain2.setThreshold(1);
}
function test_chainToChainBase() public {
vm.chainId(chainId1);
// Setting up the transfer
DummyToken token1 = DummyToken(nttManagerChain1.token());
DummyToken token2 = DummyTokenMintAndBurn(nttManagerChain2.token());
uint8 decimals = token1.decimals();
uint256 sendingAmount = 5 * 10 ** decimals;
token1.mintDummy(address(userA), 5 * 10 ** decimals);
vm.startPrank(userA);
token1.approve(address(nttManagerChain1), sendingAmount);
vm.recordLogs();
// Send token through standard means (not relayer)
{
uint256 nttManagerBalanceBefore = token1.balanceOf(address(nttManagerChain1));
uint256 userBalanceBefore = token1.balanceOf(address(userA));
nttManagerChain1.transfer(sendingAmount, chainId2, bytes32(uint256(uint160(userB))));
// Balance check on funds going in and out working as expected
uint256 nttManagerBalanceAfter = token1.balanceOf(address(nttManagerChain1));
uint256 userBalanceAfter = token1.balanceOf(address(userB));
require(
nttManagerBalanceBefore + sendingAmount == nttManagerBalanceAfter,
"Should be locking the tokens"
);
require(
userBalanceBefore - sendingAmount == userBalanceAfter,
"User should have sent tokens"
);
}
vm.stopPrank();
// Get the TransferSent(bytes32) event digest to ensure it matches up with the TransferRedeemed(uint16,bytes32) event digest later
Vm.Log[] memory recordedLogs = vm.getRecordedLogs();
bytes32 sentEventDigest;
for (uint256 i = 0; i < recordedLogs.length; i++) {
if (recordedLogs[i].topics[0] == keccak256("TransferSent(bytes32)")) {
sentEventDigest = recordedLogs[i].topics[1];
break;
}
}
require(sentEventDigest != bytes32(0), "TransferSent(bytes32) event should be found");
// Get and sign the log to go down the other pipe. Thank you to whoever wrote this code in the past!
Vm.Log[] memory entries = guardian.fetchWormholeMessageFromLog(recordedLogs);
bytes[] memory encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId1);
}
// Chain2 verification and checks
vm.chainId(chainId2);
// Wrong chain receiving the signed VAA
vm.expectRevert(abi.encodeWithSelector(InvalidFork.selector, chainId1, chainId2));
wormholeTransceiverChain1.receiveMessage(encodedVMs[0]);
{
uint256 supplyBefore = token2.totalSupply();
wormholeTransceiverChain2.receiveMessage(encodedVMs[0]);
uint256 supplyAfter = token2.totalSupply();
require(sendingAmount + supplyBefore == supplyAfter, "Supplies dont match");
require(token2.balanceOf(userB) == sendingAmount, "User didn't receive tokens");
require(
token2.balanceOf(address(nttManagerChain2)) == 0, "NttManager has unintended funds"
);
}
// Get the TransferRedeemed(uint16,bytes32) event digest to ensure it matches up with the TransferSent(bytes32) event digest earlier
recordedLogs = vm.getRecordedLogs();
bytes32 recvEventDigest;
bytes32 recvSourceChain;
for (uint256 i = 0; i < recordedLogs.length; i++) {
if (recordedLogs[i].topics[0] == keccak256("TransferRedeemed(uint16,bytes32)")) {
recvSourceChain = recordedLogs[i].topics[1];
recvEventDigest = recordedLogs[i].topics[2];
break;
}
}
require(
recvSourceChain == bytes32(uint256(chainId1)),
"Incorrect TransferRedeemed(uint16,bytes32) event sourceChain"
);
require(
sentEventDigest == recvEventDigest,
"TransferRedeemed(uint16,bytes32) event digest should match TransferSent(bytes32) event digest"
);
// Can't resubmit the same message twice
(IWormhole.VM memory wormholeVM,,) = wormhole.parseAndVerifyVM(encodedVMs[0]);
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.TransferAlreadyCompleted.selector, wormholeVM.hash
)
);
wormholeTransceiverChain2.receiveMessage(encodedVMs[0]);
// Go back the other way from a THIRD user
vm.prank(userB);
token2.transfer(userC, sendingAmount);
vm.startPrank(userC);
token2.approve(address(nttManagerChain2), sendingAmount);
vm.recordLogs();
// Supply checks on the transfer
{
uint256 supplyBefore = token2.totalSupply();
nttManagerChain2.transfer(
sendingAmount,
chainId1,
toWormholeFormat(userD),
toWormholeFormat(userC),
false,
encodeTransceiverInstruction(true)
);
uint256 supplyAfter = token2.totalSupply();
require(sendingAmount - supplyBefore == supplyAfter, "Supplies don't match");
require(token2.balanceOf(userB) == 0, "OG user receive tokens");
require(token2.balanceOf(userC) == 0, "Sending user didn't receive tokens");
require(
token2.balanceOf(address(nttManagerChain2)) == 0,
"NttManager didn't receive unintended funds"
);
}
// Get and sign the log to go down the other pipe. Thank you to whoever wrote this code in the past!
entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId2);
}
// Chain1 verification and checks with the receiving of the message
vm.chainId(chainId1);
{
uint256 supplyBefore = token1.totalSupply();
wormholeTransceiverChain1.receiveMessage(encodedVMs[0]);
uint256 supplyAfter = token1.totalSupply();
require(supplyBefore == supplyAfter, "Supplies don't match between operations");
require(token1.balanceOf(userB) == 0, "OG user receive tokens");
require(token1.balanceOf(userC) == 0, "Sending user didn't receive tokens");
require(token1.balanceOf(userD) == sendingAmount, "User received funds");
}
}
function test_lotsOfReverts() public {
vm.chainId(chainId1);
// Setting up the transfer
DummyToken token1 = DummyToken(nttManagerChain1.token());
DummyToken token2 = DummyTokenMintAndBurn(nttManagerChain2.token());
uint8 decimals = token1.decimals();
uint256 sendingAmount = 5 * 10 ** decimals;
token1.mintDummy(address(userA), 5 * 10 ** decimals);
vm.startPrank(userA);
token1.approve(address(nttManagerChain1), sendingAmount);
vm.recordLogs();
// Send token through standard means (not relayer)
{
uint256 nttManagerBalanceBefore = token1.balanceOf(address(nttManagerChain1));
uint256 userBalanceBefore = token1.balanceOf(address(userA));
nttManagerChain1.transfer(
sendingAmount,
chainId2,
toWormholeFormat(userB),
toWormholeFormat(userA),
true,
encodeTransceiverInstruction(true)
);
// Balance check on funds going in and out working as expected
uint256 nttManagerBalanceAfter = token1.balanceOf(address(nttManagerChain1));
uint256 userBalanceAfter = token1.balanceOf(address(userB));
require(
nttManagerBalanceBefore + sendingAmount == nttManagerBalanceAfter,
"Should be locking the tokens"
);
require(
userBalanceBefore - sendingAmount == userBalanceAfter,
"User should have sent tokens"
);
}
vm.stopPrank();
// Get and sign the log to go down the other pipe. Thank you to whoever wrote this code in the past!
Vm.Log[] memory entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
bytes[] memory encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId1);
}
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.InvalidWormholePeer.selector,
chainId1,
wormholeTransceiverChain1
)
); // Wrong chain receiving the signed VAA
wormholeTransceiverChain1.receiveMessage(encodedVMs[0]);
vm.chainId(chainId2);
{
uint256 supplyBefore = token2.totalSupply();
wormholeTransceiverChain2.receiveMessage(encodedVMs[0]);
uint256 supplyAfter = token2.totalSupply();
require(sendingAmount + supplyBefore == supplyAfter, "Supplies dont match");
require(token2.balanceOf(userB) == sendingAmount, "User didn't receive tokens");
require(
token2.balanceOf(address(nttManagerChain2)) == 0, "NttManager has unintended funds"
);
}
// Can't resubmit the same message twice
(IWormhole.VM memory wormholeVM,,) = wormhole.parseAndVerifyVM(encodedVMs[0]);
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.TransferAlreadyCompleted.selector, wormholeVM.hash
)
);
wormholeTransceiverChain2.receiveMessage(encodedVMs[0]);
// Go back the other way from a THIRD user
vm.prank(userB);
token2.transfer(userC, sendingAmount);
vm.startPrank(userC);
token2.approve(address(nttManagerChain2), sendingAmount);
vm.recordLogs();
// Supply checks on the transfer
{
uint256 supplyBefore = token2.totalSupply();
vm.stopPrank();
nttManagerChain2.setOutboundLimit(0);
vm.startPrank(userC);
nttManagerChain2.transfer(
sendingAmount,
chainId1,
toWormholeFormat(userD),
toWormholeFormat(userC),
true,
encodeTransceiverInstruction(true)
);
// Test timing on the queues
vm.expectRevert(
abi.encodeWithSelector(
IRateLimiter.OutboundQueuedTransferStillQueued.selector,
0,
vm.getBlockTimestamp()
)
);
nttManagerChain2.completeOutboundQueuedTransfer(0);
vm.warp(vm.getBlockTimestamp() + 1 days - 1);
vm.expectRevert(
abi.encodeWithSelector(
IRateLimiter.OutboundQueuedTransferStillQueued.selector,
0,
vm.getBlockTimestamp() - 1 days + 1
)
);
nttManagerChain2.completeOutboundQueuedTransfer(0);
vm.warp(vm.getBlockTimestamp() + 1);
nttManagerChain2.completeOutboundQueuedTransfer(0);
// Replay - should be deleted
vm.expectRevert(
abi.encodeWithSelector(IRateLimiter.OutboundQueuedTransferNotFound.selector, 0)
);
nttManagerChain2.completeOutboundQueuedTransfer(0);
// Non-existant
vm.expectRevert(
abi.encodeWithSelector(IRateLimiter.OutboundQueuedTransferNotFound.selector, 1)
);
nttManagerChain2.completeOutboundQueuedTransfer(1);
uint256 supplyAfter = token2.totalSupply();
require(sendingAmount - supplyBefore == supplyAfter, "Supplies don't match");
require(token2.balanceOf(userB) == 0, "OG user receive tokens");
require(token2.balanceOf(userC) == 0, "Sending user didn't receive tokens");
require(
token2.balanceOf(address(nttManagerChain2)) == 0,
"NttManager didn't receive unintended funds"
);
}
// Get and sign the log to go down the other pipe. Thank you to whoever wrote this code in the past!
entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId2);
}
// Chain1 verification and checks with the receiving of the message
vm.chainId(chainId1);
vm.stopPrank(); // Back to the owner of everything for this one.
vm.recordLogs();
{
uint256 supplyBefore = token1.totalSupply();
nttManagerChain1.setInboundLimit(0, chainId2);
wormholeTransceiverChain1.receiveMessage(encodedVMs[0]);
bytes32[] memory queuedDigests =
Utils.fetchQueuedTransferDigestsFromLogs(vm.getRecordedLogs());
vm.warp(vm.getBlockTimestamp() + 100000);
nttManagerChain1.completeInboundQueuedTransfer(queuedDigests[0]);
// Double redeem
vm.warp(vm.getBlockTimestamp() + 100000);
vm.expectRevert(
abi.encodeWithSelector(
IRateLimiter.InboundQueuedTransferNotFound.selector, queuedDigests[0]
)
);
nttManagerChain1.completeInboundQueuedTransfer(queuedDigests[0]);
uint256 supplyAfter = token1.totalSupply();
require(supplyBefore == supplyAfter, "Supplies don't match between operations");
require(token1.balanceOf(userB) == 0, "OG user receive tokens");
require(token1.balanceOf(userC) == 0, "Sending user didn't receive tokens");
require(token1.balanceOf(userD) == sendingAmount, "User received funds");
}
}
function test_multiTransceiver() public {
vm.chainId(chainId1);
WormholeTransceiver wormholeTransceiverChain1_1 = wormholeTransceiverChain1;
// Dual transceiver setup
WormholeTransceiver wormholeTransceiverChain1_2 = new MockWormholeTransceiverContract(
address(nttManagerChain1),
address(wormhole),
address(relayer),
address(0x0),
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain1_2 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain1_2), ""))
);
wormholeTransceiverChain1_2.initialize();
vm.chainId(chainId2);
WormholeTransceiver wormholeTransceiverChain2_1 = wormholeTransceiverChain2;
// Dual transceiver setup
WormholeTransceiver wormholeTransceiverChain2_2 = new MockWormholeTransceiverContract(
address(nttManagerChain2),
address(wormhole),
address(relayer),
address(0x0),
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain2_2 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain2_2), ""))
);
wormholeTransceiverChain2_2.initialize();
// Setup the new entrypoint hook ups to allow the transfers to occur
wormholeTransceiverChain1_2.setWormholePeer(
chainId2, bytes32(uint256(uint160((address(wormholeTransceiverChain2_2)))))
);
wormholeTransceiverChain2_2.setWormholePeer(
chainId1, bytes32(uint256(uint160((address(wormholeTransceiverChain1_2)))))
);
nttManagerChain2.setTransceiver(address(wormholeTransceiverChain2_2));
nttManagerChain1.setTransceiver(address(wormholeTransceiverChain1_2));
// Change the threshold from the setUp functions 1 to 2.
nttManagerChain1.setThreshold(2);
nttManagerChain2.setThreshold(2);
// Setting up the transfer
DummyToken token1 = DummyToken(nttManagerChain1.token());
DummyToken token2 = DummyTokenMintAndBurn(nttManagerChain2.token());
vm.startPrank(userA);
uint8 decimals = token1.decimals();
uint256 sendingAmount = 5 * 10 ** decimals;
token1.mintDummy(address(userA), sendingAmount);
vm.startPrank(userA);
token1.approve(address(nttManagerChain1), sendingAmount);
vm.chainId(chainId1);
vm.recordLogs();
// Send token through standard means (not relayer)
{
nttManagerChain1.transfer(
sendingAmount,
chainId2,
toWormholeFormat(userB),
toWormholeFormat(userA),
false,
encodeTransceiverInstructions(true)
);
}
// Get and sign the event emissions to go to the other chain.
Vm.Log[] memory entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
bytes[] memory encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId1);
}
vm.chainId(chainId2);
// Send in the messages for the two transceivers to complete the transfer from chain1 to chain2
{
// vm.stopPrank();
uint256 supplyBefore = token2.totalSupply();
wormholeTransceiverChain2_1.receiveMessage(encodedVMs[0]);
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.InvalidWormholePeer.selector,
chainId1,
wormholeTransceiverChain1_1
)
);
wormholeTransceiverChain2_2.receiveMessage(encodedVMs[0]);
// Threshold check
require(supplyBefore == token2.totalSupply(), "Supplies have been updated too early");
require(token2.balanceOf(userB) == 0, "User received tokens to early");
// Finish the transfer out once the second VAA arrives
wormholeTransceiverChain2_2.receiveMessage(encodedVMs[1]);
uint256 supplyAfter = token2.totalSupply();
require(sendingAmount + supplyBefore == supplyAfter, "Supplies dont match");
require(token2.balanceOf(userB) == sendingAmount, "User didn't receive tokens");
require(
token2.balanceOf(address(nttManagerChain2)) == 0, "NttManager has unintended funds"
);
}
// Back the other way for the burn!
vm.startPrank(userB);
token2.approve(address(nttManagerChain2), sendingAmount);
vm.recordLogs();
// Send token through standard means (not relayer)
{
uint256 userBalanceBefore = token1.balanceOf(address(userB));
nttManagerChain2.transfer(
sendingAmount,
chainId1,
toWormholeFormat(userA),
toWormholeFormat(userB),
false,
encodeTransceiverInstructions(true)
);
uint256 nttManagerBalanceAfter = token1.balanceOf(address(nttManagerChain2));
uint256 userBalanceAfter = token1.balanceOf(address(userB));
require(userBalanceBefore - userBalanceAfter == 0, "No funds left for user");
require(nttManagerBalanceAfter == 0, "NttManager should burn all tranferred tokens");
}
// Get the VAA proof for the transfers to use
entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId2);
}
vm.chainId(chainId1);
{
uint256 supplyBefore = token1.totalSupply();
wormholeTransceiverChain1_1.receiveMessage(encodedVMs[0]);
require(supplyBefore == token1.totalSupply(), "Supplies have been updated too early");
require(token2.balanceOf(userA) == 0, "User received tokens to early");
// Finish the transfer out once the second VAA arrives
wormholeTransceiverChain1_2.receiveMessage(encodedVMs[1]);
uint256 supplyAfter = token1.totalSupply();
require(
supplyBefore == supplyAfter,
"Supplies don't match between operations. Should not increase."
);
require(token1.balanceOf(userB) == 0, "Sending user receive tokens");
require(
token1.balanceOf(userA) == sendingAmount, "Receiving user didn't receive tokens"
);
}
vm.stopPrank();
}
function copyBytes(
bytes memory _bytes
) private pure returns (bytes memory) {
bytes memory copy = new bytes(_bytes.length);
uint256 max = _bytes.length + 31;
for (uint256 i = 32; i <= max; i += 32) {
assembly {
mstore(add(copy, i), mload(add(_bytes, i)))
}
}
return copy;
}
function encodeTransceiverInstruction(
bool relayer_off
) public view returns (bytes memory) {
WormholeTransceiver.WormholeTransceiverInstruction memory instruction =
IWormholeTransceiver.WormholeTransceiverInstruction(relayer_off);
bytes memory encodedInstructionWormhole =
wormholeTransceiverChain1.encodeWormholeTransceiverInstruction(instruction);
TransceiverStructs.TransceiverInstruction memory TransceiverInstruction = TransceiverStructs
.TransceiverInstruction({index: 0, payload: encodedInstructionWormhole});
TransceiverStructs.TransceiverInstruction[] memory TransceiverInstructions =
new TransceiverStructs.TransceiverInstruction[](1);
TransceiverInstructions[0] = TransceiverInstruction;
return TransceiverStructs.encodeTransceiverInstructions(TransceiverInstructions);
}
// Encode an instruction for each of the relayers
function encodeTransceiverInstructions(
bool relayer_off
) public view returns (bytes memory) {
WormholeTransceiver.WormholeTransceiverInstruction memory instruction =
IWormholeTransceiver.WormholeTransceiverInstruction(relayer_off);
bytes memory encodedInstructionWormhole =
wormholeTransceiverChain1.encodeWormholeTransceiverInstruction(instruction);
TransceiverStructs.TransceiverInstruction memory TransceiverInstruction1 =
TransceiverStructs.TransceiverInstruction({index: 0, payload: encodedInstructionWormhole});
TransceiverStructs.TransceiverInstruction memory TransceiverInstruction2 =
TransceiverStructs.TransceiverInstruction({index: 1, payload: encodedInstructionWormhole});
TransceiverStructs.TransceiverInstruction[] memory TransceiverInstructions =
new TransceiverStructs.TransceiverInstruction[](2);
TransceiverInstructions[0] = TransceiverInstruction1;
TransceiverInstructions[1] = TransceiverInstruction2;
return TransceiverStructs.encodeTransceiverInstructions(TransceiverInstructions);
}
}