1+ import { init } from '../../init' ;
2+ import { assert } from '../assertions' ;
3+
4+ export const createPaymentDelegationFlowTest = (
5+ ctx : Awaited < ReturnType < typeof init > > ,
6+ _getAuthContext : ( ) => any
7+ ) => {
8+ return async ( ) => {
9+ // Get Payment Manager for Alice (the payer/delegator)
10+ const alicePaymentManager = await ctx . litClient . getPaymentManager ( {
11+ account : ctx . aliceViemAccount ,
12+ } ) ;
13+
14+ // Get Payment Manager for Bob (the user/delegatee)
15+ const bobPaymentManager = await ctx . litClient . getPaymentManager ( {
16+ account : ctx . bobViemAccount ,
17+ } ) ;
18+
19+ assert . toBeDefined ( alicePaymentManager ) ;
20+ assert . toBeDefined ( bobPaymentManager ) ;
21+
22+ const aliceAddress = ctx . aliceViemAccount . address ;
23+ const bobAddress = ctx . bobViemAccount . address ;
24+
25+ // Test 1: Get initial payers for Bob
26+ const initialPayers = await bobPaymentManager . getPayers ( {
27+ userAddress : bobAddress ,
28+ } ) ;
29+ assert . toBe ( Array . isArray ( initialPayers ) , true ) ;
30+ const initialPayersCount = initialPayers . length ;
31+ console . log ( 'initialPayers' , initialPayers ) ;
32+
33+ // Test 2: Get initial users for Alice
34+ const initialUsers = await alicePaymentManager . getUsers ( {
35+ payerAddress : aliceAddress ,
36+ } ) ;
37+ assert . toBe ( Array . isArray ( initialUsers ) , true ) ;
38+ const initialUsersCount = initialUsers . length ;
39+ console . log ( 'initialUsers' , initialUsers ) ;
40+
41+ // Test 3: Alice delegates payment to Bob
42+ const delegateTx = await alicePaymentManager . delegatePayments ( {
43+ userAddress : bobAddress ,
44+ } ) ;
45+ assert . toBeDefined ( delegateTx . hash ) ;
46+ assert . toBeDefined ( delegateTx . receipt ) ;
47+ assert . toBe ( delegateTx . receipt . status , 'success' ) ;
48+
49+ // Test 4: Verify Bob now has Alice as a payer
50+ const payersAfterDelegate = await bobPaymentManager . getPayers ( {
51+ userAddress : bobAddress ,
52+ } ) ;
53+ assert . toBe ( payersAfterDelegate . length , initialPayersCount + 1 ) ;
54+ assert . toBe ( payersAfterDelegate . includes ( aliceAddress ) , true ) ;
55+ console . log ( 'payersAfterDelegate' , payersAfterDelegate ) ;
56+
57+ // Test 5: Verify Alice now has Bob as a user
58+ const usersAfterDelegate = await alicePaymentManager . getUsers ( {
59+ payerAddress : aliceAddress ,
60+ } ) ;
61+ assert . toBe ( usersAfterDelegate . length , initialUsersCount + 1 ) ;
62+ assert . toBe ( usersAfterDelegate . includes ( bobAddress ) , true ) ;
63+ console . log ( 'usersAfterDelegate' , usersAfterDelegate ) ;
64+
65+ // Test 6: Set a restriction for Alice
66+ const setRestrictionTx = await alicePaymentManager . setRestriction ( {
67+ totalMaxPrice : '1000000000000000000' , // 1 ETH
68+ requestsPerPeriod : '100' ,
69+ periodSeconds : '3600' , // 1 hour
70+ } ) ;
71+ assert . toBeDefined ( setRestrictionTx . hash ) ;
72+ assert . toBe ( setRestrictionTx . receipt . status , 'success' ) ;
73+ console . log ( 'setRestrictionTx' , setRestrictionTx ) ;
74+
75+ // Test 7: Get and verify the restriction
76+ const restriction = await alicePaymentManager . getRestriction ( {
77+ payerAddress : aliceAddress ,
78+ } ) ;
79+ assert . toBeDefined ( restriction ) ;
80+ assert . toBe ( restriction . totalMaxPrice , '1000000000000000000' ) ;
81+ assert . toBe ( restriction . requestsPerPeriod , '100' ) ;
82+ assert . toBe ( restriction . periodSeconds , '3600' ) ;
83+ console . log ( 'restriction' , restriction ) ;
84+ // Test 8: Test batch operations - create test addresses
85+ const testAddresses = [
86+ '0x1234567890123456789012345678901234567890' ,
87+ '0x2345678901234567890123456789012345678901' ,
88+ ] ;
89+
90+ // Delegate to multiple users
91+ const batchDelegateTx = await alicePaymentManager . delegatePaymentsBatch ( {
92+ userAddresses : testAddresses ,
93+ } ) ;
94+ assert . toBeDefined ( batchDelegateTx . hash ) ;
95+ assert . toBe ( batchDelegateTx . receipt . status , 'success' ) ;
96+ console . log ( 'batchDelegateTx' , batchDelegateTx ) ;
97+ // Test 9: Verify batch delegation
98+ const usersAfterBatch = await alicePaymentManager . getUsers ( {
99+ payerAddress : aliceAddress ,
100+ } ) ;
101+ assert . toBe ( usersAfterBatch . includes ( testAddresses [ 0 ] ) , true ) ;
102+ assert . toBe ( usersAfterBatch . includes ( testAddresses [ 1 ] ) , true ) ;
103+ console . log ( 'usersAfterBatch' , usersAfterBatch ) ;
104+ // Test 10: Get payers and restrictions for multiple users
105+ const payersAndRestrictions = await alicePaymentManager . getPayersAndRestrictions ( {
106+ userAddresses : [ bobAddress , testAddresses [ 0 ] ] ,
107+ } ) ;
108+ assert . toBeDefined ( payersAndRestrictions ) ;
109+ assert . toBe ( Array . isArray ( payersAndRestrictions . payers ) , true ) ;
110+ assert . toBe ( payersAndRestrictions . payers . length , 2 ) ;
111+ assert . toBe ( Array . isArray ( payersAndRestrictions . restrictions ) , true ) ;
112+ assert . toBe ( payersAndRestrictions . restrictions . length , 2 ) ;
113+ console . log ( 'payersAndRestrictions' , payersAndRestrictions ) ;
114+ // Test 11: Undelegate from batch users
115+ const batchUndelegateTx = await alicePaymentManager . undelegatePaymentsBatch ( {
116+ userAddresses : testAddresses ,
117+ } ) ;
118+ assert . toBeDefined ( batchUndelegateTx . hash ) ;
119+ assert . toBe ( batchUndelegateTx . receipt . status , 'success' ) ;
120+ console . log ( 'batchUndelegateTx' , batchUndelegateTx ) ;
121+ // Test 12: Alice undelegates payment from Bob
122+ const undelegateTx = await alicePaymentManager . undelegatePayments ( {
123+ userAddress : bobAddress ,
124+ } ) ;
125+ assert . toBeDefined ( undelegateTx . hash ) ;
126+ assert . toBe ( undelegateTx . receipt . status , 'success' ) ;
127+ console . log ( 'undelegateTx' , undelegateTx ) ;
128+ // Test 13: Verify Bob no longer has Alice as a payer
129+ const finalPayers = await bobPaymentManager . getPayers ( {
130+ userAddress : bobAddress ,
131+ } ) ;
132+ assert . toBe ( finalPayers . length , initialPayersCount ) ;
133+ assert . toBe ( finalPayers . includes ( aliceAddress ) , false ) ;
134+ console . log ( 'finalPayers' , finalPayers ) ;
135+ // Test 14: Verify Alice no longer has Bob as a user
136+ const finalUsers = await alicePaymentManager . getUsers ( {
137+ payerAddress : aliceAddress ,
138+ } ) ;
139+ assert . toBe ( finalUsers . includes ( bobAddress ) , false ) ;
140+ console . log ( 'finalUsers' , finalUsers ) ;
141+ } ;
142+ } ;
0 commit comments