Skip to content

Commit f89c653

Browse files
committed
fix(e2e): payment delegation test for naga-test
1 parent 43c7552 commit f89c653

File tree

4 files changed

+123
-113
lines changed

4 files changed

+123
-113
lines changed

packages/e2e/src/e2e.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
init,
1717
} from '@lit-protocol/e2e';
1818
import type { AuthContext } from '@lit-protocol/e2e';
19+
import { registerPaymentDelegationTicketSuite } from './tickets/delegation.suite';
1920

2021
const RPC_OVERRIDE = process.env['LIT_YELLOWSTONE_PRIVATE_RPC_URL'];
2122
if (RPC_OVERRIDE) {
@@ -172,4 +173,8 @@ describe('all', () => {
172173
it('eoaNativeAuthFlow', () => createEoaNativeAuthFlowTest(ctx)());
173174
});
174175
});
176+
177+
describe('Tickets', () => {
178+
registerPaymentDelegationTicketSuite();
179+
});
175180
});

packages/e2e/src/helper/createTestEnv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export const CONFIG = {
1616
ledgerDepositAmount: '2',
1717
},
1818
LIVE: {
19-
nativeFundingAmount: '0.01',
20-
ledgerDepositAmount: '0.01',
19+
nativeFundingAmount: '0.1',
20+
ledgerDepositAmount: '0.1',
2121
},
2222
};
2323

Lines changed: 2 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,3 @@
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';
72

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();
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { createEnvVars } from '../helper/createEnvVars';
2+
import {
3+
createTestAccount,
4+
CreateTestAccountResult,
5+
} from '../helper/createTestAccount';
6+
import { createTestEnv } from '../helper/createTestEnv';
7+
8+
export function registerPaymentDelegationTicketSuite() {
9+
describe('payment delegation test', () => {
10+
let envVars: ReturnType<typeof createEnvVars>;
11+
let testEnv: Awaited<ReturnType<typeof createTestEnv>>;
12+
let alice: CreateTestAccountResult;
13+
let bobAccount: CreateTestAccountResult;
14+
15+
beforeAll(async () => {
16+
envVars = createEnvVars();
17+
testEnv = await createTestEnv(envVars);
18+
});
19+
20+
it("should allow Bob to use Alice's sponsorship to pay for PKP execution", async () => {
21+
// 1. First, create Bob
22+
bobAccount = await createTestAccount(testEnv, {
23+
label: 'Bob',
24+
fundAccount: true,
25+
hasEoaAuthContext: true,
26+
fundLedger: false,
27+
hasPKP: true,
28+
fundPKP: false,
29+
hasPKPAuthContext: false,
30+
fundPKPLedger: false,
31+
});
32+
33+
console.log('bobAccount:', bobAccount);
34+
35+
if (!bobAccount.pkp?.ethAddress) {
36+
throw new Error("Bob's PKP does not have an ethAddress");
37+
}
38+
39+
// 2. Next, create Alice, who will sponsor Bob
40+
alice = await createTestAccount(testEnv, {
41+
label: 'Alice',
42+
fundAccount: true,
43+
fundLedger: true,
44+
hasPKP: true,
45+
fundPKP: true,
46+
fundPKPLedger: true,
47+
sponsor: {
48+
restrictions: {
49+
totalMaxPriceInWei: '1000000000000000000',
50+
requestsPerPeriod: '100',
51+
periodSeconds: '10',
52+
},
53+
userAddresses: [bobAccount.account.address],
54+
},
55+
});
56+
57+
// 3. Take a snapshot of Alice's Ledger balance before Bob's request
58+
const aliceBeforeBalance = await testEnv.masterPaymentManager.getBalance({
59+
userAddress: alice.account.address,
60+
});
61+
62+
console.log(
63+
"[BEFORE] Alice's Ledger balance before Bob's request:",
64+
aliceBeforeBalance
65+
);
66+
67+
// 3. Now, Bob tries to sign with his PKP using Alice's sponsorship
68+
await testEnv.litClient.chain.ethereum.pkpSign({
69+
authContext: bobAccount.eoaAuthContext!,
70+
pubKey: bobAccount.pkp?.pubkey!,
71+
toSign: 'Hello, world!',
72+
userMaxPrice: 1000000000000000000n, // 0.05 ETH in Wei
73+
});
74+
75+
// 4. Finally, check that Alice's Ledger balance has decreased
76+
const aliceBalanceAfter = await testEnv.masterPaymentManager.getBalance({
77+
userAddress: alice.account.address,
78+
});
79+
80+
console.log(
81+
"[AFTER] Alice's Ledger balance after Bob's request:",
82+
aliceBalanceAfter
83+
);
84+
85+
expect(BigInt(aliceBalanceAfter.raw.availableBalance)).toBeLessThan(
86+
BigInt(aliceBeforeBalance.raw.availableBalance)
87+
);
88+
89+
// 5. Now, Alice removes Bob from her sponsorship
90+
await alice.paymentManager!.undelegatePaymentsBatch({
91+
userAddresses: [bobAccount.account.address],
92+
});
93+
94+
// 6. Bob should now fail to sign with his PKP due to lack of sponsorship
95+
let didFail = false;
96+
try {
97+
await testEnv.litClient.chain.ethereum.pkpSign({
98+
authContext: bobAccount.eoaAuthContext!,
99+
pubKey: bobAccount.pkp?.pubkey!,
100+
toSign: 'Hello again, world!',
101+
userMaxPrice: 1000000000000000000n, // 0.05 ETH in Wei
102+
});
103+
} catch (e) {
104+
didFail = true;
105+
console.log(
106+
"As expected, Bob's PKP sign failed after Alice removed sponsorship:",
107+
e
108+
);
109+
}
110+
111+
expect(didFail).toBe(true);
112+
});
113+
});
114+
}

0 commit comments

Comments
 (0)