1+ import { init } from '../../init' ;
2+ import { assert } from '../assertions' ;
3+
4+ export const createPaymentManagerFlowTest = (
5+ ctx : Awaited < ReturnType < typeof init > > ,
6+ getAuthContext : ( ) => any
7+ ) => {
8+ return async ( ) => {
9+ console . log ( '🏦 Testing Payment Manager flow' ) ;
10+
11+ const authContext = getAuthContext ( ) ;
12+ const paymentManager = await ctx . litClient . getPaymentManager ( { account : ctx . aliceViemAccount } ) ;
13+
14+ // Get the user's address from authContext (assuming it has a wallet or account)
15+ const userAddress = authContext . wallet ?. account ?. address || authContext . account ?. address || ctx . aliceViemAccount . address ;
16+
17+ console . log ( '💰 Testing deposit functionality...' ) ;
18+ // Test deposit
19+ const depositAmount = '0.00001' ; // Very small amount for testing (account only has 0.0001 ETH)
20+ const depositResult = await paymentManager . deposit ( { amountInEth : depositAmount } ) ;
21+
22+ assert . toBeDefined ( depositResult . hash , "Deposit transaction hash should be defined" ) ;
23+ assert . toBeDefined ( depositResult . receipt , "Deposit transaction receipt should be defined" ) ;
24+ console . log ( '✅ Deposit successful:' , depositResult . hash ) ;
25+
26+ console . log ( '📊 Testing balance checking...' ) ;
27+ // Check balance after deposit
28+ const balanceInfo = await paymentManager . getBalance ( { userAddress } ) ;
29+
30+ assert . toBeDefined ( balanceInfo . totalBalance , "Total balance should be defined" ) ;
31+ assert . toBeDefined ( balanceInfo . availableBalance , "Available balance should be defined" ) ;
32+ assert . toBeGreaterThan ( Number ( balanceInfo . raw . totalBalance ) , 0 , "Balance should be greater than 0" ) ;
33+
34+ console . log ( '💰 Current balance:' , balanceInfo . totalBalance , 'ETH' ) ;
35+ console . log ( '💳 Available balance:' , balanceInfo . availableBalance , 'ETH' ) ;
36+
37+ console . log ( '🔄 Testing withdrawal request...' ) ;
38+ // Test withdrawal request
39+ const withdrawAmount = '0.000005' ; // Half of deposited amount
40+ const withdrawRequestResult = await paymentManager . requestWithdraw ( { amountInEth : withdrawAmount } ) ;
41+
42+ assert . toBeDefined ( withdrawRequestResult . hash , "Withdrawal request transaction hash should be defined" ) ;
43+ assert . toBeDefined ( withdrawRequestResult . receipt , "Withdrawal request transaction receipt should be defined" ) ;
44+ console . log ( '✅ Withdrawal request successful:' , withdrawRequestResult . hash ) ;
45+
46+ console . log ( '📋 Testing withdrawal request status...' ) ;
47+ // Check withdrawal request status
48+ const withdrawRequestInfo = await paymentManager . getWithdrawRequest ( { userAddress } ) ;
49+
50+ assert . toBe ( withdrawRequestInfo . isPending , true , "Withdrawal request should be pending" ) ;
51+ assert . toBe ( withdrawRequestInfo . amount , withdrawAmount , "Withdrawal amount should match" ) ;
52+ assert . toBeGreaterThan ( Number ( withdrawRequestInfo . timestamp ) , 0 , "Withdrawal timestamp should be greater than 0" ) ;
53+
54+ console . log ( '⏰ Withdrawal request timestamp:' , withdrawRequestInfo . timestamp ) ;
55+ console . log ( '💸 Withdrawal request amount:' , withdrawRequestInfo . amount , 'ETH' ) ;
56+
57+ console . log ( '⏱️ Testing withdrawal delay...' ) ;
58+ // Get withdrawal delay
59+ const delayInfo = await paymentManager . getWithdrawDelay ( ) ;
60+
61+ assert . toBeDefined ( delayInfo . delaySeconds , "Delay seconds should be defined" ) ;
62+ assert . toBeGreaterThan ( Number ( delayInfo . raw ) , 0 , "Delay should be greater than 0" ) ;
63+
64+ console . log ( '⏳ Withdrawal delay:' , delayInfo . delaySeconds , 'seconds' ) ;
65+
66+ console . log ( '🔍 Testing withdrawal execution check...' ) ;
67+ // Check if withdrawal can be executed
68+ const canExecuteInfo = await paymentManager . canExecuteWithdraw ( { userAddress } ) ;
69+
70+ assert . toBeDefined ( canExecuteInfo . canExecute , "canExecute should be defined" ) ;
71+ assert . toBe ( canExecuteInfo . withdrawRequest . isPending , true , "Withdrawal request should be pending" ) ;
72+
73+ if ( canExecuteInfo . canExecute ) {
74+ console . log ( '✅ Withdrawal can be executed immediately' ) ;
75+
76+ console . log ( '💸 Testing withdrawal execution...' ) ;
77+ // Execute withdrawal if possible
78+ const withdrawResult = await paymentManager . withdraw ( { amountInEth : withdrawAmount } ) ;
79+
80+ assert . toBeDefined ( withdrawResult . hash , "Withdrawal execution transaction hash should be defined" ) ;
81+ assert . toBeDefined ( withdrawResult . receipt , "Withdrawal execution transaction receipt should be defined" ) ;
82+ console . log ( '✅ Withdrawal executed successfully:' , withdrawResult . hash ) ;
83+
84+ // Check balance after withdrawal
85+ const finalBalanceInfo = await paymentManager . getBalance ( { userAddress } ) ;
86+ console . log ( '📊 Final balance:' , finalBalanceInfo . totalBalance , 'ETH' ) ;
87+
88+ } else {
89+ console . log ( '⏱️ Withdrawal cannot be executed yet. Time remaining:' , canExecuteInfo . timeRemaining , 'seconds' ) ;
90+ }
91+
92+ console . log ( '🧪 Testing deposit for user functionality...' ) ;
93+ // Test deposit for another user (using alice's address as target)
94+ const targetUserAddress = ctx . aliceViemAccount . address ;
95+ const depositForUserResult = await paymentManager . depositForUser ( {
96+ userAddress : targetUserAddress ,
97+ amountInEth : '0.00001'
98+ } ) ;
99+
100+ assert . toBeDefined ( depositForUserResult . hash , "Deposit for user transaction hash should be defined" ) ;
101+ assert . toBeDefined ( depositForUserResult . receipt , "Deposit for user transaction receipt should be defined" ) ;
102+ console . log ( '✅ Deposit for user successful:' , depositForUserResult . hash ) ;
103+
104+ // Check target user's balance
105+ const targetUserBalance = await paymentManager . getBalance ( { userAddress : targetUserAddress } ) ;
106+ assert . toBeGreaterThan ( Number ( targetUserBalance . raw . totalBalance ) , 0 , "Target user balance should be greater than 0" ) ;
107+ console . log ( '💰 Target user balance:' , targetUserBalance . totalBalance , 'ETH' ) ;
108+
109+ console . log ( '✅ Payment Manager flow test completed successfully!' ) ;
110+ } ;
111+ } ;
0 commit comments