-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrailsIntentEntrypoint.t.sol
More file actions
2117 lines (1812 loc) · 69 KB
/
TrailsIntentEntrypoint.t.sol
File metadata and controls
2117 lines (1812 loc) · 69 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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {TrailsIntentEntrypoint} from "../src/TrailsIntentEntrypoint.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import {MockNonStandardERC20} from "./mocks/MockNonStandardERC20.sol";
// Mock ERC20 token with permit functionality for testing
contract MockERC20Permit is ERC20, ERC20Permit {
constructor() ERC20("Mock Token", "MTK") ERC20Permit("Mock Token") {
_mint(msg.sender, 1000000 * 10 ** decimals());
}
}
// -----------------------------------------------------------------------------
// Test Contract
// -----------------------------------------------------------------------------
contract TrailsIntentEntrypointTest is Test {
// Mirror events for expectEmit if needed
event FeePaid(address indexed user, address indexed feeToken, uint256 feeAmount, address indexed feeCollector);
// -------------------------------------------------------------------------
// Test State Variables
// -------------------------------------------------------------------------
TrailsIntentEntrypoint public entrypoint;
MockERC20Permit public token;
address public user = vm.addr(0x123456789);
uint256 public userPrivateKey = 0x123456789;
function setUp() public {
entrypoint = new TrailsIntentEntrypoint();
token = new MockERC20Permit();
// Give user some tokens and check transfer success
assertTrue(token.transfer(user, 1000 * 10 ** token.decimals()));
}
function testConstructor() public view {
// Simple constructor test - just verify the contract was deployed
assertTrue(address(entrypoint) != address(0));
}
function testExecuteIntentWithPermit() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 3600;
uint256 nonce = entrypoint.nonces(user);
// Create permit signature
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
// Create intent signature
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
// Record balances before
uint256 userBalanceBefore = token.balanceOf(user);
uint256 intentBalanceBefore = token.balanceOf(intentAddress);
// Execute intent with permit
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount, // permitAmount - same as amount for this test
intentAddress,
deadline,
nonce,
0, // no fee amount
address(0), // no fee collector
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
// Check balances after
uint256 userBalanceAfter = token.balanceOf(user);
uint256 intentBalanceAfter = token.balanceOf(intentAddress);
assertEq(userBalanceAfter, userBalanceBefore - amount);
assertEq(intentBalanceAfter, intentBalanceBefore + amount);
vm.stopPrank();
}
function testExecuteIntentWithPermit_permit_frontrun() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 3600;
uint256 nonce = entrypoint.nonces(user);
// Create permit signature
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
// Create intent signature
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
// FRONTRUN: Call permit directly before depositToIntentWithPermit
token.permit(user, address(entrypoint), amount, deadline, permitV, permitR, permitS);
// Record balances before
uint256 userBalanceBefore = token.balanceOf(user);
uint256 intentBalanceBefore = token.balanceOf(intentAddress);
// Execute intent with permit - should still succeed due to try/catch
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount, // permitAmount - same as amount for this test
intentAddress,
deadline,
nonce,
0, // no fee amount
address(0), // no fee collector
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
// Check balances after
uint256 userBalanceAfter = token.balanceOf(user);
uint256 intentBalanceAfter = token.balanceOf(intentAddress);
assertEq(userBalanceAfter, userBalanceBefore - amount);
assertEq(intentBalanceAfter, intentBalanceBefore + amount);
vm.stopPrank();
}
function testExecuteIntentWithPermitExpired() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp - 1; // Expired
uint256 nonce = entrypoint.nonces(user);
// Create permit signature
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
// Create intent signature
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
vm.expectRevert();
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount, // permitAmount - same as amount for this test
intentAddress,
deadline,
nonce,
0, // no fee amount
address(0), // no fee collector
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
vm.stopPrank();
}
function testExecuteIntentWithPermitInvalidSignature() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 3600;
uint256 nonce = entrypoint.nonces(user);
// Use wrong private key for signature
uint256 wrongPrivateKey = 0x987654321;
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(wrongPrivateKey, permitHash);
// Create intent signature
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(wrongPrivateKey, intentDigest);
vm.expectRevert();
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount, // permitAmount - same as amount for this test
intentAddress,
deadline,
nonce,
0, // no fee amount
address(0), // no fee collector
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
vm.stopPrank();
}
function testExecuteIntentWithFee() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
address feeCollector = address(0x9999);
uint256 amount = 50 * 10 ** token.decimals();
uint256 feeAmount = 5 * 10 ** token.decimals();
uint256 totalAmount = amount + feeAmount;
uint256 deadline = block.timestamp + 3600;
uint256 nonce = entrypoint.nonces(user);
// Create permit signature for total amount (deposit + fee)
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
totalAmount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
// Create intent signature
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
feeAmount,
feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
// Record balances before
uint256 userBalanceBefore = token.balanceOf(user);
uint256 intentBalanceBefore = token.balanceOf(intentAddress);
uint256 feeCollectorBalanceBefore = token.balanceOf(feeCollector);
// Execute intent with permit and fee (fee token is same as deposit token)
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
totalAmount, // permitAmount - total amount needed (deposit + fee)
intentAddress,
deadline,
nonce,
feeAmount,
feeCollector,
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
// Check balances after
uint256 userBalanceAfter = token.balanceOf(user);
uint256 intentBalanceAfter = token.balanceOf(intentAddress);
uint256 feeCollectorBalanceAfter = token.balanceOf(feeCollector);
assertEq(userBalanceAfter, userBalanceBefore - totalAmount);
assertEq(intentBalanceAfter, intentBalanceBefore + amount);
assertEq(feeCollectorBalanceAfter, feeCollectorBalanceBefore + feeAmount);
vm.stopPrank();
}
// Test: Infinite approval allows subsequent deposits without new permits
function testInfiniteApprovalFlow() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 3600;
uint256 permitAmount = amount; // Exact amount (no fee)
// First deposit with permit
uint256 nonce1 = entrypoint.nonces(user);
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
permitAmount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
bytes32 intentHash1 = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce1,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest1 = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash1));
(uint8 sigV1, bytes32 sigR1, bytes32 sigS1) = vm.sign(userPrivateKey, intentDigest1);
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
permitAmount,
intentAddress,
deadline,
nonce1,
0,
address(0),
permitV,
permitR,
permitS,
sigV1,
sigR1,
sigS1
);
// Verify allowance is consumed
assertEq(token.allowance(user, address(entrypoint)), 0);
// Second deposit without permit
uint256 nonce2 = entrypoint.nonces(user);
assertEq(nonce2, 1);
bytes32 intentHash2 = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce2,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest2 = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash2));
(uint8 sigV2, bytes32 sigR2, bytes32 sigS2) = vm.sign(userPrivateKey, intentDigest2);
uint256 userBalBefore = token.balanceOf(user);
// Approve for second deposit since exact permit was consumed by first deposit
token.approve(address(entrypoint), amount);
entrypoint.depositToIntent(
user, address(token), amount, intentAddress, deadline, nonce2, 0, address(0), sigV2, sigR2, sigS2
);
assertEq(token.balanceOf(user), userBalBefore - amount);
vm.stopPrank();
}
// Test: Exact approval requires new permit for subsequent deposits
function testExactApprovalFlow() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 3600;
// First deposit with exact approval
uint256 nonce1 = entrypoint.nonces(user);
bytes32 permitHash1 = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV1, bytes32 permitR1, bytes32 permitS1) = vm.sign(userPrivateKey, permitHash1);
bytes32 intentHash1 = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce1,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest1 = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash1));
(uint8 sigV1, bytes32 sigR1, bytes32 sigS1) = vm.sign(userPrivateKey, intentDigest1);
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount,
intentAddress,
deadline,
nonce1,
0,
address(0),
permitV1,
permitR1,
permitS1,
sigV1,
sigR1,
sigS1
);
// Verify allowance is consumed
assertEq(token.allowance(user, address(entrypoint)), 0);
// Second deposit requires new permit
uint256 nonce2 = entrypoint.nonces(user);
bytes32 permitHash2 = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV2, bytes32 permitR2, bytes32 permitS2) = vm.sign(userPrivateKey, permitHash2);
bytes32 intentHash2 = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce2,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest2 = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash2));
(uint8 sigV2, bytes32 sigR2, bytes32 sigS2) = vm.sign(userPrivateKey, intentDigest2);
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount,
intentAddress,
deadline,
nonce2,
0,
address(0),
permitV2,
permitR2,
permitS2,
sigV2,
sigR2,
sigS2
);
assertEq(token.allowance(user, address(entrypoint)), 0);
vm.stopPrank();
}
// Test: Fee collector receives fees with permit
function testFeeCollectorReceivesFees() public {
vm.startPrank(user);
uint256 amt = 50e18;
uint256 fee = 5e18;
uint256 dl = block.timestamp + 1 hours;
uint256 nonce1 = entrypoint.nonces(user);
(uint8 pv, bytes32 pr, bytes32 ps) = _signPermit(user, amt + fee, dl);
(uint8 sv, bytes32 sr, bytes32 ss) = _signIntent2(user, amt, address(0x5678), dl, nonce1, fee, address(0x9999));
entrypoint.depositToIntentWithPermit(
user,
address(token),
amt,
amt + fee,
address(0x5678),
dl,
nonce1,
fee,
address(0x9999),
pv,
pr,
ps,
sv,
sr,
ss
);
assertEq(token.balanceOf(address(0x9999)), fee);
vm.stopPrank();
}
// Test: Fee collector receives fees without permit
function testFeeCollectorReceivesFeesWithoutPermit() public {
vm.startPrank(user);
uint256 amt = 50e18;
uint256 fee = 5e18;
uint256 dl = block.timestamp + 1 hours;
uint256 nonce = entrypoint.nonces(user);
(uint8 sv, bytes32 sr, bytes32 ss) = _signIntent2(user, amt, address(0x5678), dl, nonce, fee, address(0x9999));
token.approve(address(entrypoint), amt + fee);
entrypoint.depositToIntent(
user, address(token), amt, address(0x5678), dl, nonce, fee, address(0x9999), sv, sr, ss
);
assertEq(token.balanceOf(address(0x9999)), fee);
vm.stopPrank();
}
// Additional tests from reference file for maximum coverage
function testConstructorAndDomainSeparator() public view {
assertTrue(address(entrypoint) != address(0));
bytes32 expectedDomain = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes("TrailsIntentEntrypoint")),
keccak256(bytes(entrypoint.VERSION())),
block.chainid,
address(entrypoint)
)
);
assertEq(entrypoint.DOMAIN_SEPARATOR(), expectedDomain);
}
function testDepositToIntentWithoutPermit_RequiresIntentAddress() public {
vm.startPrank(user);
address intentAddress = address(0);
uint256 amount = 10 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 1;
uint256 nonce = entrypoint.nonces(user);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, digest);
vm.expectRevert(TrailsIntentEntrypoint.InvalidIntentAddress.selector);
entrypoint.depositToIntent(user, address(token), amount, intentAddress, deadline, nonce, 0, address(0), v, r, s);
vm.stopPrank();
}
function testDepositToIntentRequiresNonZeroAmount() public {
vm.startPrank(user);
address intentAddress = address(0x1234);
uint256 amount = 0; // Zero amount
uint256 deadline = block.timestamp + 100;
uint256 nonce = entrypoint.nonces(user);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, digest);
vm.expectRevert(TrailsIntentEntrypoint.InvalidAmount.selector);
entrypoint.depositToIntent(user, address(token), amount, intentAddress, deadline, nonce, 0, address(0), v, r, s);
vm.stopPrank();
}
function testDepositToIntentWithPermitRequiresNonZeroAmount() public {
vm.startPrank(user);
address intentAddress = address(0x1234);
uint256 amount = 0; // Zero amount
uint256 deadline = block.timestamp + 100;
uint256 nonce = entrypoint.nonces(user);
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
vm.expectRevert(TrailsIntentEntrypoint.InvalidAmount.selector);
entrypoint.depositToIntentWithPermit(
user,
address(token),
amount,
amount,
intentAddress,
deadline,
nonce,
0,
address(0),
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
vm.stopPrank();
}
function testDepositToIntentRequiresValidToken() public {
vm.startPrank(user);
address intentAddress = address(0x1234);
uint256 amount = 10 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 100;
uint256 nonce = entrypoint.nonces(user);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(0),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, digest);
vm.expectRevert(TrailsIntentEntrypoint.InvalidToken.selector);
entrypoint.depositToIntent(user, address(0), amount, intentAddress, deadline, nonce, 0, address(0), v, r, s);
vm.stopPrank();
}
function testDepositToIntentWithPermitRequiresValidToken() public {
vm.startPrank(user);
address intentAddress = address(0x1234);
uint256 amount = 10 * 10 ** token.decimals();
uint256 deadline = block.timestamp + 100;
uint256 nonce = entrypoint.nonces(user);
bytes32 permitHash = keccak256(
abi.encodePacked(
"\x19\x01",
token.DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
user,
address(entrypoint),
amount,
token.nonces(user),
deadline
)
)
)
);
(uint8 permitV, bytes32 permitR, bytes32 permitS) = vm.sign(userPrivateKey, permitHash);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(0),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 intentDigest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));
(uint8 sigV, bytes32 sigR, bytes32 sigS) = vm.sign(userPrivateKey, intentDigest);
vm.expectRevert(TrailsIntentEntrypoint.InvalidToken.selector);
entrypoint.depositToIntentWithPermit(
user,
address(0),
amount,
amount,
intentAddress,
deadline,
nonce,
0,
address(0),
permitV,
permitR,
permitS,
sigV,
sigR,
sigS
);
vm.stopPrank();
}
function testDepositToIntentExpiredDeadline() public {
vm.startPrank(user);
address intentAddress = address(0x5678);
uint256 amount = 50 * 10 ** token.decimals();
uint256 deadline = block.timestamp - 1; // Already expired
uint256 nonce = entrypoint.nonces(user);
bytes32 intentHash = keccak256(
abi.encode(
entrypoint.TRAILS_INTENT_TYPEHASH(),
user,
address(token),
amount,
intentAddress,
deadline,
block.chainid,
nonce,
0, // feeAmount
address(0) // feeCollector
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", entrypoint.DOMAIN_SEPARATOR(), intentHash));