forked from p2p-org/p2p-lending-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainnetIntegration.sol
1280 lines (1067 loc) · 45.3 KB
/
MainnetIntegration.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
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-FileCopyrightText: 2025 P2P Validator <[email protected]>
// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;
import "../src/@openzeppelin/contracts/interfaces/IERC4626.sol";
import "../src/@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../src/access/P2pOperator.sol";
import "../src/adapters/morpho/p2pMorphoProxy/P2pMorphoProxy.sol";
import "../src/adapters/morpho/p2pMorphoProxyFactory/P2pMorphoProxyFactory.sol";
import "../src/common/IMorphoBundler.sol";
import "../src/common/P2pStructs.sol";
import "../src/p2pLendingProxyFactory/P2pLendingProxyFactory.sol";
import {PermitHash} from "../src/@permit2/libraries/PermitHash.sol";
import "forge-std/Test.sol";
import "forge-std/Vm.sol";
import "forge-std/console.sol";
import "forge-std/console2.sol";
contract MainnetIntegration is Test {
using SafeERC20 for IERC20;
address constant P2pTreasury = 0x6Bb8b45a1C6eA816B70d76f83f7dC4f0f87365Ff;
P2pMorphoProxyFactory private factory;
address private clientAddress;
uint256 private clientPrivateKey;
address private p2pSignerAddress;
uint256 private p2pSignerPrivateKey;
address private p2pOperatorAddress;
address private nobody;
address constant MorphoEthereumBundlerV2 = 0x4095F064B8d3c3548A3bebfd0Bbfd04750E30077;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant VaultUSDC = 0x8eB67A509616cd6A7c1B3c8C21D48FF57df3d458;
address constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address constant VaultUSDT = 0xbEef047a543E45807105E51A8BBEFCc5950fcfBa;
address asset;
address vault;
uint256 constant SigDeadline = 1734464723;
uint96 constant ClientBasisPoints = 8700; // 13% fee
uint256 constant DepositAmount = 10000000;
address proxyAddress;
uint48 nonce;
function setUp() public {
vm.createSelectFork("mainnet", 21308893);
(clientAddress, clientPrivateKey) = makeAddrAndKey("client");
(p2pSignerAddress, p2pSignerPrivateKey) = makeAddrAndKey("p2pSigner");
p2pOperatorAddress = makeAddr("p2pOperator");
nobody = makeAddr("nobody");
vm.startPrank(p2pOperatorAddress);
factory = new P2pMorphoProxyFactory(
MorphoEthereumBundlerV2,
p2pSignerAddress,
P2pTreasury
);
vm.stopPrank();
proxyAddress = factory.predictP2pLendingProxyAddress(clientAddress, ClientBasisPoints);
_setRules();
asset = USDC;
vault = VaultUSDC;
}
function test_HappyPath_USDT_Mainnet() external {
asset = USDT;
vault = VaultUSDT;
_happyPath_Mainnet();
}
function test_HappyPath_USDC_Mainnet() external {
asset = USDC;
vault = VaultUSDC;
_happyPath_Mainnet();
}
function test_profitSplit_Mainnet() public {
asset = USDC;
vault = VaultUSDC;
deal(asset, clientAddress, 100e6);
uint256 clientAssetBalanceBefore = IERC20(asset).balanceOf(clientAddress);
uint256 p2pAssetBalanceBefore = IERC20(asset).balanceOf(P2pTreasury);
_doDeposit();
uint256 shares = IERC20(vault).balanceOf(proxyAddress);
uint256 assetsInMorphoBefore = IERC4626(vault).convertToAssets(shares);
_forward(10000000);
uint256 assetsInMorphoAfter = IERC4626(vault).convertToAssets(shares);
uint256 profit = assetsInMorphoAfter - assetsInMorphoBefore;
_doWithdraw(1);
uint256 clientAssetBalanceAfter = IERC20(asset).balanceOf(clientAddress);
uint256 p2pAssetBalanceAfter = IERC20(asset).balanceOf(P2pTreasury);
uint256 clientBalanceChange = clientAssetBalanceAfter - clientAssetBalanceBefore;
uint256 p2pBalanceChange = p2pAssetBalanceAfter - p2pAssetBalanceBefore;
uint256 sumOfBalanceChanges = clientBalanceChange + p2pBalanceChange;
assertApproxEqAbs(sumOfBalanceChanges, profit, 1);
uint256 clientBasisPointsDeFacto = clientBalanceChange * 10_000 / sumOfBalanceChanges;
uint256 p2pBasisPointsDeFacto = p2pBalanceChange * 10_000 / sumOfBalanceChanges;
assertApproxEqAbs(ClientBasisPoints, clientBasisPointsDeFacto, 1);
assertApproxEqAbs(10_000 - ClientBasisPoints, p2pBasisPointsDeFacto, 1);
}
function test_transferP2pSigner_Mainnet() public {
vm.startPrank(nobody);
vm.expectRevert(abi.encodeWithSelector(P2pOperator.P2pOperator__UnauthorizedAccount.selector, nobody));
factory.transferP2pSigner(nobody);
address oldSigner = factory.getP2pSigner();
assertEq(oldSigner, p2pSignerAddress);
vm.startPrank(p2pOperatorAddress);
factory.transferP2pSigner(nobody);
address newSigner = factory.getP2pSigner();
assertEq(newSigner, nobody);
}
function test_setCalldataRules_Mainnet() public {
vm.startPrank(nobody);
vm.expectRevert(abi.encodeWithSelector(P2pOperator.P2pOperator__UnauthorizedAccount.selector, nobody));
factory.setCalldataRules(P2pStructs.FunctionType.None, address(0), bytes4(0), new P2pStructs.Rule[](0));
vm.startPrank(p2pOperatorAddress);
vm.expectEmit();
emit IP2pLendingProxyFactory.P2pLendingProxyFactory__CalldataRulesSet(
P2pStructs.FunctionType.None,
address(0),
bytes4(0),
new P2pStructs.Rule[](0)
);
factory.setCalldataRules(P2pStructs.FunctionType.None, address(0), bytes4(0), new P2pStructs.Rule[](0));
}
function test_removeCalldataRules_Mainnet() public {
vm.startPrank(nobody);
vm.expectRevert(abi.encodeWithSelector(P2pOperator.P2pOperator__UnauthorizedAccount.selector, nobody));
factory.removeCalldataRules(P2pStructs.FunctionType.None, address(0), bytes4(0));
vm.startPrank(p2pOperatorAddress);
vm.expectEmit();
emit IP2pLendingProxyFactory.P2pLendingProxyFactory__CalldataRulesRemoved(
P2pStructs.FunctionType.None,
address(0),
bytes4(0)
);
factory.removeCalldataRules(P2pStructs.FunctionType.None, address(0), bytes4(0));
}
function test_clientBasisPointsGreaterThan10000_Mainnet() public {
uint96 invalidBasisPoints = 10001;
vm.startPrank(clientAddress);
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
invalidBasisPoints,
SigDeadline
);
vm.expectRevert(P2pMorphoProxyFactory__erc4626Deposit_receiver_ne_proxy.selector);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
invalidBasisPoints,
SigDeadline,
p2pSignerSignature
);
}
function test_zeroAddressAsset_Mainnet() public {
vm.startPrank(clientAddress);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
// Set token to zero address
permitSingle.details.token = address(0);
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
vm.expectRevert(P2pMorphoProxyFactory__approve2_token_ne_permitSingleForP2pLendingProxy_token.selector);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
}
function test_zeroAssetAmount_Mainnet() public {
vm.startPrank(clientAddress);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
// Set amount to zero
permitSingle.details.amount = 0;
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
vm.expectRevert(P2pMorphoProxyFactory__approve2_amount_ne_permitSingleForP2pLendingProxy_amount.selector);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
}
function test_depositDirectlyOnProxy_Mainnet() public {
vm.startPrank(clientAddress);
// Add this line to give initial tokens to the client
deal(asset, clientAddress, DepositAmount);
// Add this line to approve tokens for Permit2
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
// Create proxy first via factory
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
// Now try to call deposit directly on the proxy
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxy__NotFactoryCalled.selector,
clientAddress,
address(factory)
)
);
P2pMorphoProxy(proxyAddress).deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature
);
}
function test_initializeDirectlyOnProxy_Mainnet() public {
// Create the proxy first since we need a valid proxy address to test with
proxyAddress = factory.predictP2pLendingProxyAddress(clientAddress, ClientBasisPoints);
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
vm.startPrank(clientAddress);
// Add this line to give initial tokens to the client
deal(asset, clientAddress, DepositAmount);
// Add this line to approve tokens for Permit2
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
// This will create the proxy
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
// Now try to initialize it directly
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxy__NotFactoryCalled.selector,
clientAddress,
address(factory)
)
);
proxy.initialize(
clientAddress,
ClientBasisPoints
);
vm.stopPrank();
}
function test_withdrawOnProxyOnlyCallableByClient_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
vm.stopPrank();
// Try to withdraw as non-client
vm.startPrank(nobody);
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
// Get withdrawal calldata
uint256 sharesBalance = IERC20(vault).balanceOf(proxyAddress);
bytes memory withdrawalCallData = _getMulticallWithdrawalCallData(sharesBalance);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxy__NotClientCalled.selector,
nobody, // _msgSender (the nobody address trying to call)
clientAddress // _actualClient (the actual client address)
)
);
proxy.withdraw(
MorphoEthereumBundlerV2,
withdrawalCallData,
vault,
sharesBalance
);
vm.stopPrank();
}
function test_incorrectWithdrawalCalldata_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
// Try to withdraw with incorrect calldata
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
uint256 sharesBalance = IERC20(vault).balanceOf(proxyAddress);
// Create incorrect withdrawal calldata (empty bytes)
bytes memory incorrectWithdrawalCalldata = "";
vm.startPrank(clientAddress);
vm.expectRevert();
proxy.withdraw(
MorphoEthereumBundlerV2,
incorrectWithdrawalCalldata,
vault,
sharesBalance
);
vm.stopPrank();
}
function test_withdrawViaCallAnyFunction_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
// Try to withdraw using callAnyFunction
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
uint256 sharesBalance = IERC20(vault).balanceOf(proxyAddress);
bytes memory withdrawalCallData = _getMulticallWithdrawalCallData(sharesBalance);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__NoRulesDefined.selector,
P2pStructs.FunctionType.None,
MorphoEthereumBundlerV2,
IMorphoBundler.multicall.selector
)
);
proxy.callAnyFunction(
MorphoEthereumBundlerV2,
withdrawalCallData
);
vm.stopPrank();
}
function test_calldataTooShortForStartsWithRule_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
vm.stopPrank();
// Set rule that requires first 32 bytes to match
P2pStructs.Rule[] memory rules = new P2pStructs.Rule[](1);
rules[0] = P2pStructs.Rule({
ruleType: P2pStructs.RuleType.StartsWith,
index: 0,
allowedBytes: new bytes(32)
});
vm.startPrank(p2pOperatorAddress);
factory.setCalldataRules(
P2pStructs.FunctionType.None,
vault,
IERC20.balanceOf.selector,
rules
);
vm.stopPrank();
// Create calldata that's too short (only 4 bytes)
bytes memory shortCalldata = abi.encodeWithSelector(IERC20.balanceOf.selector);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__CalldataTooShortForStartsWithRule.selector,
0, // calldata length after selector
0, // rule index
32 // required bytes count
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
vault,
shortCalldata
);
vm.stopPrank();
}
function test_calldataStartsWithRuleViolated_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
vm.stopPrank();
// Set rule that requires first 32 bytes to match specific value
bytes memory expectedBytes = new bytes(32);
for(uint i = 0; i < 32; i++) {
expectedBytes[i] = bytes1(uint8(i));
}
P2pStructs.Rule[] memory rules = new P2pStructs.Rule[](1);
rules[0] = P2pStructs.Rule({
ruleType: P2pStructs.RuleType.StartsWith,
index: 0,
allowedBytes: expectedBytes
});
vm.startPrank(p2pOperatorAddress);
factory.setCalldataRules(
P2pStructs.FunctionType.None,
vault,
IERC20.balanceOf.selector,
rules
);
vm.stopPrank();
// Create calldata with different first 32 bytes
bytes memory differentBytes = new bytes(32);
bytes memory wrongCalldata = abi.encodePacked(
IERC20.balanceOf.selector,
differentBytes
);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__CalldataStartsWithRuleViolated.selector,
differentBytes,
expectedBytes
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
vault,
wrongCalldata
);
vm.stopPrank();
}
function test_calldataTooShortForEndsWithRule_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
vm.stopPrank();
// Set rule that requires last 32 bytes to match
P2pStructs.Rule[] memory rules = new P2pStructs.Rule[](1);
rules[0] = P2pStructs.Rule({
ruleType: P2pStructs.RuleType.EndsWith,
index: 0,
allowedBytes: new bytes(32)
});
vm.startPrank(p2pOperatorAddress);
factory.setCalldataRules(
P2pStructs.FunctionType.None,
vault,
IERC20.balanceOf.selector,
rules
);
vm.stopPrank();
// Create calldata that's too short (only selector)
bytes memory shortCalldata = abi.encodeWithSelector(IERC20.balanceOf.selector);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__CalldataTooShortForEndsWithRule.selector,
0, // calldata length after selector
32 // required bytes count
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
vault,
shortCalldata
);
vm.stopPrank();
}
function test_calldataEndsWithRuleViolated_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
vm.stopPrank();
// Set rule that requires last 32 bytes to match specific value
bytes memory expectedEndBytes = new bytes(32);
for(uint i = 0; i < 32; i++) {
expectedEndBytes[i] = bytes1(uint8(i));
}
P2pStructs.Rule[] memory rules = new P2pStructs.Rule[](1);
rules[0] = P2pStructs.Rule({
ruleType: P2pStructs.RuleType.EndsWith,
index: 0,
allowedBytes: expectedEndBytes
});
vm.startPrank(p2pOperatorAddress);
factory.setCalldataRules(
P2pStructs.FunctionType.None,
vault,
IERC20.balanceOf.selector,
rules
);
vm.stopPrank();
// Create calldata with different ending bytes
bytes memory wrongEndBytes = new bytes(32);
for(uint i = 0; i < 32; i++) {
wrongEndBytes[i] = bytes1(uint8(100 + i));
}
bytes memory wrongCalldata = abi.encodePacked(
IERC20.balanceOf.selector,
wrongEndBytes
);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__CalldataEndsWithRuleViolated.selector,
wrongEndBytes,
expectedEndBytes
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
vault,
wrongCalldata
);
vm.stopPrank();
}
function test_callBalanceOfViaCallAnyFunction_Mainnet() public {
// Create proxy and do initial deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Do initial deposit
_doDeposit();
vm.stopPrank();
bytes memory balanceOfCalldata = abi.encodeWithSelector(
IERC20.balanceOf.selector,
proxyAddress
);
vm.startPrank(clientAddress);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__NoRulesDefined.selector,
P2pStructs.FunctionType.None,
vault,
IERC20.balanceOf.selector
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
vault,
balanceOfCalldata
);
vm.stopPrank();
P2pStructs.Rule[] memory rules = new P2pStructs.Rule[](1);
rules[0] = P2pStructs.Rule({
ruleType: P2pStructs.RuleType.AnyCalldata,
index: 0,
allowedBytes: ""
});
vm.startPrank(p2pOperatorAddress);
factory.setCalldataRules(
P2pStructs.FunctionType.None, // This is correct
vault,
IERC20.balanceOf.selector,
rules
);
vm.stopPrank();
// Call balanceOf via callAnyFunction
vm.startPrank(clientAddress);
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
proxy.callAnyFunction(
vault,
balanceOfCalldata
);
vm.stopPrank();
}
function test_getP2pLendingProxyFactory__NoRulesDefined_Mainnet() public {
// Create proxy first via factory
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
// Add this line to give tokens to the client before attempting deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
// Add this line to approve tokens for Permit2
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
// Try to call a function with no rules defined
bytes memory someCalldata = abi.encodeWithSelector(
IERC20.transfer.selector,
address(0),
0
);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__NoRulesDefined.selector,
P2pStructs.FunctionType.None,
asset,
IERC20.transfer.selector
)
);
P2pMorphoProxy(proxyAddress).callAnyFunction(
asset,
someCalldata
);
vm.stopPrank();
}
function test_getP2pLendingProxyFactory__ZeroP2pSignerAddress_Mainnet() public {
vm.startPrank(p2pOperatorAddress);
vm.expectRevert(P2pLendingProxyFactory__ZeroP2pSignerAddress.selector);
factory.transferP2pSigner(address(0));
vm.stopPrank();
}
function test_getHashForP2pSigner_Mainnet() public view {
bytes32 expectedHash = keccak256(abi.encode(
clientAddress,
ClientBasisPoints,
SigDeadline,
address(factory),
block.chainid
));
bytes32 actualHash = factory.getHashForP2pSigner(
clientAddress,
ClientBasisPoints,
SigDeadline
);
assertEq(actualHash, expectedHash);
}
function test_getPermit2HashTypedData_Mainnet() public view {
// Create a permit single struct
IAllowanceTransfer.PermitSingle memory permitSingle = IAllowanceTransfer.PermitSingle({
details: IAllowanceTransfer.PermitDetails({
token: asset,
amount: uint160(DepositAmount),
expiration: uint48(SigDeadline),
nonce: 0
}),
spender: proxyAddress,
sigDeadline: SigDeadline
});
// Get the permit hash
bytes32 permitHash = factory.getPermitHash(permitSingle);
// Get the typed data hash
bytes32 actualTypedDataHash = factory.getPermit2HashTypedData(permitHash);
// Calculate expected hash
bytes32 expectedTypedDataHash = keccak256(
abi.encodePacked(
"\x19\x01",
Permit2Lib.PERMIT2.DOMAIN_SEPARATOR(),
permitHash
)
);
assertEq(actualTypedDataHash, expectedTypedDataHash);
// Test the overloaded function that takes PermitSingle directly
bytes32 actualTypedDataHashFromPermitSingle = factory.getPermit2HashTypedData(permitSingle);
assertEq(actualTypedDataHashFromPermitSingle, expectedTypedDataHash);
}
function test_supportsInterface_Mainnet() public view {
// Test IP2pLendingProxyFactory interface support
bool supportsP2pLendingProxyFactory = factory.supportsInterface(type(IP2pLendingProxyFactory).interfaceId);
assertTrue(supportsP2pLendingProxyFactory);
// Test IERC165 interface support
bool supportsERC165 = factory.supportsInterface(type(IERC165).interfaceId);
assertTrue(supportsERC165);
// Test non-supported interface
bytes4 nonSupportedInterfaceId = bytes4(keccak256("nonSupportedInterface()"));
bool supportsNonSupported = factory.supportsInterface(nonSupportedInterfaceId);
assertFalse(supportsNonSupported);
}
function test_p2pSignerSignatureExpired_Mainnet() public {
// Add this line to give tokens to the client before attempting deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
// Get p2p signer signature with expired deadline
uint256 expiredDeadline = block.timestamp - 1;
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
expiredDeadline
);
vm.expectRevert(
abi.encodeWithSelector(
P2pLendingProxyFactory__P2pSignerSignatureExpired.selector,
expiredDeadline
)
);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
expiredDeadline,
p2pSignerSignature
);
vm.stopPrank();
}
function test_invalidP2pSignerSignature_Mainnet() public {
// Add this line to give tokens to the client before attempting deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
// Create an invalid signature by using a different private key
uint256 wrongPrivateKey = 0x12345; // Some random private key
bytes32 messageHash = ECDSA.toEthSignedMessageHash(
factory.getHashForP2pSigner(
clientAddress,
ClientBasisPoints,
SigDeadline
)
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(wrongPrivateKey, messageHash);
bytes memory invalidSignature = abi.encodePacked(r, s, v);
vm.expectRevert(P2pLendingProxyFactory__InvalidP2pSignerSignature.selector);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
invalidSignature
);
vm.stopPrank();
}
function test_viewFunctions_Mainnet() public {
// Add this line to give tokens to the client before attempting deposit
deal(asset, clientAddress, DepositAmount);
vm.startPrank(clientAddress);
// Add this line to approve tokens for Permit2
IERC20(asset).safeApprove(address(Permit2Lib.PERMIT2), type(uint256).max);
// Create proxy first via factory
bytes memory p2pSignerSignature = _getP2pSignerSignature(
clientAddress,
ClientBasisPoints,
SigDeadline
);
// Get the multicall data and permit details
(bytes memory multicallCallData, IAllowanceTransfer.PermitSingle memory permitSingle) =
_getMulticallDataAndPermitSingleForP2pLendingProxy();
bytes memory permit2Signature = _getPermit2SignatureForP2pLendingProxy(permitSingle);
factory.deposit(
MorphoEthereumBundlerV2,
multicallCallData,
permitSingle,
permit2Signature,
ClientBasisPoints,
SigDeadline,
p2pSignerSignature
);
P2pMorphoProxy proxy = P2pMorphoProxy(proxyAddress);
assertEq(proxy.getFactory(), address(factory));
assertEq(proxy.getP2pTreasury(), P2pTreasury);
assertEq(proxy.getClient(), clientAddress);
assertEq(proxy.getClientBasisPoints(), ClientBasisPoints);
assertEq(proxy.getTotalDeposited(asset), DepositAmount);
assertEq(factory.getP2pSigner(), p2pSignerAddress);
assertEq(factory.predictP2pLendingProxyAddress(clientAddress, ClientBasisPoints), proxyAddress);
}
function test_acceptP2pOperator_Mainnet() public {
// Initial state check
assertEq(factory.getP2pOperator(), p2pOperatorAddress);
// Only operator can initiate transfer
vm.startPrank(nobody);
vm.expectRevert(
abi.encodeWithSelector(
P2pOperator.P2pOperator__UnauthorizedAccount.selector,
nobody
)
);
factory.transferP2pOperator(nobody);
vm.stopPrank();
// Operator initiates transfer
address newOperator = makeAddr("newOperator");
vm.startPrank(p2pOperatorAddress);
factory.transferP2pOperator(newOperator);
// Check pending operator is set
assertEq(factory.getPendingP2pOperator(), newOperator);
// Check current operator hasn't changed yet
assertEq(factory.getP2pOperator(), p2pOperatorAddress);
vm.stopPrank();
// Wrong address cannot accept transfer
vm.startPrank(nobody);
vm.expectRevert(
abi.encodeWithSelector(