|
1 | | -import { createEnvVars } from '../helper/createEnvVars'; |
2 | | -import { |
3 | | - createTestAccount, |
4 | | - CreateTestAccountResult, |
5 | | -} from '../helper/createTestAccount'; |
6 | | -import { createTestEnv } from '../helper/createTestEnv'; |
| 1 | +import { registerPaymentDelegationTicketSuite } from './delegation.suite'; |
7 | 2 |
|
8 | | -describe('payment delegation test', () => { |
9 | | - let envVars: ReturnType<typeof createEnvVars>; |
10 | | - let testEnv: Awaited<ReturnType<typeof createTestEnv>>; |
11 | | - let alice: CreateTestAccountResult; |
12 | | - let bobAccount: CreateTestAccountResult; |
13 | | - |
14 | | - beforeAll(async () => { |
15 | | - envVars = createEnvVars(); |
16 | | - testEnv = await createTestEnv(envVars); |
17 | | - }); |
18 | | - |
19 | | - it("should allow Bob to use Alice's sponsorship to pay for PKP execution", async () => { |
20 | | - // 1. First, create Bob |
21 | | - bobAccount = await createTestAccount(testEnv, { |
22 | | - label: 'Bob', |
23 | | - fundAccount: true, |
24 | | - hasEoaAuthContext: true, |
25 | | - fundLedger: false, |
26 | | - hasPKP: true, |
27 | | - fundPKP: false, |
28 | | - hasPKPAuthContext: false, |
29 | | - fundPKPLedger: false, |
30 | | - }); |
31 | | - |
32 | | - console.log('bobAccount:', bobAccount); |
33 | | - |
34 | | - if (!bobAccount.pkp?.ethAddress) { |
35 | | - throw new Error("Bob's PKP does not have an ethAddress"); |
36 | | - } |
37 | | - |
38 | | - // 2. Next, create Alice, who will sponsor Bob |
39 | | - alice = await createTestAccount(testEnv, { |
40 | | - label: 'Alice', |
41 | | - fundAccount: true, |
42 | | - fundLedger: true, |
43 | | - hasPKP: true, |
44 | | - fundPKP: true, |
45 | | - fundPKPLedger: true, |
46 | | - sponsor: { |
47 | | - restrictions: { |
48 | | - totalMaxPriceInWei: '1000000000000000000', |
49 | | - requestsPerPeriod: '100', |
50 | | - periodSeconds: '5', |
51 | | - }, |
52 | | - userAddresses: [bobAccount.account.address], |
53 | | - }, |
54 | | - }); |
55 | | - |
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!, |
69 | | - pubKey: bobAccount.pkp?.pubkey!, |
70 | | - 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, |
77 | | - }); |
78 | | - |
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); |
111 | | - }); |
112 | | -}); |
| 3 | +registerPaymentDelegationTicketSuite(); |
0 commit comments