@@ -21,12 +21,16 @@ describe('payment delegation test', () => {
2121 bobAccount = await createTestAccount ( testEnv , {
2222 label : 'Bob' ,
2323 fundAccount : true ,
24- fundLedger : true ,
24+ hasEoaAuthContext : true ,
25+ fundLedger : false ,
2526 hasPKP : true ,
26- fundPKP : true ,
27- fundPKPLedger : true ,
27+ fundPKP : false ,
28+ hasPKPAuthContext : false ,
29+ fundPKPLedger : false ,
2830 } ) ;
2931
32+ console . log ( 'bobAccount:' , bobAccount ) ;
33+
3034 if ( ! bobAccount . pkp ?. ethAddress ) {
3135 throw new Error ( "Bob's PKP does not have an ethAddress" ) ;
3236 }
@@ -35,27 +39,74 @@ describe('payment delegation test', () => {
3539 alice = await createTestAccount ( testEnv , {
3640 label : 'Alice' ,
3741 fundAccount : true ,
38- fundLedger : false ,
42+ fundLedger : true ,
3943 hasPKP : true ,
4044 fundPKP : true ,
4145 fundPKPLedger : true ,
4246 sponsor : {
4347 restrictions : {
44- totalMaxPriceInEth : '0.05 ' ,
48+ totalMaxPriceInWei : '1000000000000000000 ' ,
4549 requestsPerPeriod : '100' ,
4650 periodSeconds : '5' ,
4751 } ,
48- userAddresses : [ bobAccount . pkp . ethAddress ] ,
52+ userAddresses : [ bobAccount . account . address ] ,
4953 } ,
5054 } ) ;
5155
52- // 3. Now, Bob tries to execute JS using Alice's sponsorship
53- const res = await testEnv . litClient . chain . ethereum . pkpSign ( {
54- authContext : bobAccount . pkpAuthContext ! ,
56+ // 3. Take a snapshot of Alice's Ledger balance before Bob's request
57+ const aliceBeforeBalance = await testEnv . masterPaymentManager . getBalance ( {
58+ userAddress : alice . account . address ,
59+ } ) ;
60+
61+ console . log (
62+ "[BEFORE] Alice's Ledger balance before Bob's request:" ,
63+ aliceBeforeBalance
64+ ) ;
65+
66+ // 3. Now, Bob tries to sign with his PKP using Alice's sponsorship
67+ await testEnv . litClient . chain . ethereum . pkpSign ( {
68+ authContext : bobAccount . eoaAuthContext ! ,
5569 pubKey : bobAccount . pkp ?. pubkey ! ,
5670 toSign : 'Hello, world!' ,
71+ userMaxPrice : 200000000000000000n , // 0.2 ETH in Wei
72+ } ) ;
73+
74+ // 4. Finally, check that Alice's Ledger balance has decreased
75+ const aliceBalanceAfter = await testEnv . masterPaymentManager . getBalance ( {
76+ userAddress : alice . account . address ,
5777 } ) ;
5878
59- console . log ( 'res:' , res ) ;
79+ console . log (
80+ "[AFTER] Alice's Ledger balance after Bob's request:" ,
81+ aliceBalanceAfter
82+ ) ;
83+
84+ expect ( BigInt ( aliceBalanceAfter . raw . availableBalance ) ) . toBeLessThan (
85+ BigInt ( aliceBeforeBalance . raw . availableBalance )
86+ ) ;
87+
88+ // 5. Now, Alice removes Bob from her sponsorship
89+ await alice . paymentManager ! . undelegatePaymentsBatch ( {
90+ userAddresses : [ bobAccount . account . address ] ,
91+ } ) ;
92+
93+ // 6. Bob should now fail to sign with his PKP due to lack of sponsorship
94+ let didFail = false ;
95+ try {
96+ await testEnv . litClient . chain . ethereum . pkpSign ( {
97+ authContext : bobAccount . eoaAuthContext ! ,
98+ pubKey : bobAccount . pkp ?. pubkey ! ,
99+ toSign : 'Hello again, world!' ,
100+ userMaxPrice : 200000000000000000n , // 0.2 ETH in Wei
101+ } ) ;
102+ } catch ( e ) {
103+ didFail = true ;
104+ console . log (
105+ "As expected, Bob's PKP sign failed after Alice removed sponsorship:" ,
106+ e
107+ ) ;
108+ }
109+
110+ expect ( didFail ) . toBe ( true ) ;
60111 } ) ;
61112} ) ;
0 commit comments