Skip to content

Commit fa79c11

Browse files
authored
Merge pull request #400 from LIT-Protocol/feat/e2e-add-vincent-dev-setup
chore(release): add setupVincentDevelopmentEnvironment function
2 parents 6ae09b2 + c03e76f commit fa79c11

File tree

6 files changed

+142
-101
lines changed

6 files changed

+142
-101
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
e2e-test-utils: minor
3+
---
4+
5+
Adds a `setupVincentDevelopmentEnvironment` function to streamline/abstract the developer experience.

packages/apps/ability-aerodrome-swap/test/e2e/swap-no-sponsorship.spec.ts

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import {
2-
delegator,
3-
delegatee,
4-
funder,
5-
appManager,
6-
ensureUnexpiredCapacityToken,
2+
setupVincentDevelopmentEnvironment,
73
getChainHelpers,
84
getEnv,
95
type PkpInfo,
@@ -46,61 +42,20 @@ describe('Aerodrome Swap Ability E2E Tests without Gas Sponsorship', () => {
4642
let baseRpcProvider: providers.JsonRpcProvider;
4743

4844
beforeAll(async () => {
49-
await funder.checkFunderBalance();
50-
await delegatee.ensureAppDelegateeFunded();
51-
await appManager.ensureAppManagerFunded();
52-
53-
const chainHelpers = await getChainHelpers();
54-
wallets = chainHelpers.wallets;
55-
baseRpcProvider = chainHelpers.providers.base;
56-
57-
await ensureUnexpiredCapacityToken(wallets.appDelegatee);
58-
5945
const PERMISSION_DATA: PermissionData = {
6046
// Aerodrome Swap Ability has no policies
6147
[aerodromeBundledAbility.ipfsCid]: {},
6248
};
6349

64-
const abilityIpfsCids: string[] = Object.keys(PERMISSION_DATA);
65-
const abilityPolicies: string[][] = abilityIpfsCids.map((abilityIpfsCid) => {
66-
return Object.keys(PERMISSION_DATA[abilityIpfsCid]);
67-
});
68-
69-
// If an app exists for the delegatee, we will create a new app version with the new ipfs cids
70-
// Otherwise, we will create an app w/ version 1 appVersion with the new ipfs cids
71-
const existingApp = await delegatee.getAppInfo();
72-
console.log('[beforeAll] existingApp', existingApp);
73-
let appId: number;
74-
let appVersion: number;
75-
if (!existingApp) {
76-
console.log('[beforeAll] No existing app, registering new app');
77-
const newApp = await appManager.registerNewApp({ abilityIpfsCids, abilityPolicies });
78-
appId = newApp.appId;
79-
appVersion = newApp.appVersion;
80-
} else {
81-
console.log('[beforeAll] Existing app, registering new app version');
82-
const newAppVersion = await appManager.registerNewAppVersion({
83-
abilityIpfsCids,
84-
abilityPolicies,
85-
});
86-
appId = existingApp.appId;
87-
appVersion = newAppVersion.appVersion;
88-
}
89-
90-
agentPkpInfo = await delegator.getFundedAgentPkp();
91-
92-
await delegator.permitAppVersionForAgentWalletPkp({
50+
const setupResult = await setupVincentDevelopmentEnvironment({
9351
permissionData: PERMISSION_DATA,
94-
appId,
95-
appVersion,
96-
agentPkpInfo,
9752
});
9853

99-
await delegator.addPermissionForAbilities(
100-
wallets.agentWalletOwner,
101-
agentPkpInfo.tokenId,
102-
abilityIpfsCids,
103-
);
54+
agentPkpInfo = setupResult.agentPkpInfo;
55+
wallets = setupResult.wallets;
56+
57+
const chainHelpers = await getChainHelpers();
58+
baseRpcProvider = chainHelpers.providers.base;
10459
});
10560

10661
afterAll(async () => {

packages/apps/ability-aerodrome-swap/test/e2e/swap-with-sponsorship.spec.ts

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import {
2-
delegator,
3-
delegatee,
4-
funder,
5-
appManager,
6-
ensureUnexpiredCapacityToken,
2+
setupVincentDevelopmentEnvironment,
73
getChainHelpers,
84
getEnv,
95
type PkpInfo,
@@ -54,58 +50,20 @@ describe('Aerodrome Swap Ability E2E Tests with Alchemy Gas Sponsorship', () =>
5450
let SMART_ACCOUNT_CLIENT: SmartAccountClient;
5551

5652
beforeAll(async () => {
57-
await funder.checkFunderBalance();
58-
await delegatee.ensureAppDelegateeFunded();
59-
await appManager.ensureAppManagerFunded();
60-
61-
const chainHelpers = await getChainHelpers();
62-
wallets = chainHelpers.wallets;
63-
baseRpcProvider = chainHelpers.providers.base;
64-
65-
await ensureUnexpiredCapacityToken(wallets.appDelegatee);
66-
6753
const PERMISSION_DATA: PermissionData = {
6854
// Aerodrome Swap Ability has no policies
6955
[aerodromeBundledAbility.ipfsCid]: {},
7056
};
7157

72-
const abilityIpfsCids: string[] = Object.keys(PERMISSION_DATA);
73-
const abilityPolicies: string[][] = abilityIpfsCids.map((abilityIpfsCid) => {
74-
return Object.keys(PERMISSION_DATA[abilityIpfsCid]);
75-
});
76-
77-
// If an app exists for the delegatee, we will create a new app version with the new ipfs cids
78-
// Otherwise, we will create an app w/ version 1 appVersion with the new ipfs cids
79-
const existingApp = await delegatee.getAppInfo();
80-
let appId: number;
81-
let appVersion: number;
82-
if (!existingApp) {
83-
const newApp = await appManager.registerNewApp({ abilityIpfsCids, abilityPolicies });
84-
appId = newApp.appId;
85-
appVersion = newApp.appVersion;
86-
} else {
87-
const newAppVersion = await appManager.registerNewAppVersion({
88-
abilityIpfsCids,
89-
abilityPolicies,
90-
});
91-
appId = existingApp.appId;
92-
appVersion = newAppVersion.appVersion;
93-
}
94-
95-
agentPkpInfo = await delegator.getFundedAgentPkp();
96-
97-
await delegator.permitAppVersionForAgentWalletPkp({
58+
const setupResult = await setupVincentDevelopmentEnvironment({
9859
permissionData: PERMISSION_DATA,
99-
appId,
100-
appVersion,
101-
agentPkpInfo,
10260
});
10361

104-
await delegator.addPermissionForAbilities(
105-
wallets.agentWalletOwner,
106-
agentPkpInfo.tokenId,
107-
abilityIpfsCids,
108-
);
62+
agentPkpInfo = setupResult.agentPkpInfo;
63+
wallets = setupResult.wallets;
64+
65+
const chainHelpers = await getChainHelpers();
66+
baseRpcProvider = chainHelpers.providers.base;
10967

11068
SMART_ACCOUNT_CLIENT = await createModularAccountV2Client({
11169
mode: '7702' as const,

packages/libs/e2e-test-utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export {
99
getEnv,
1010
getChainHelpers,
1111
createRandomVincentWallets,
12+
setupVincentDevelopmentEnvironment,
1213
} from './lib';
1314

1415
export type { PkpInfo } from './lib/mint-new-pkp';
16+
export type { VincentDevEnvironment } from './lib/setup-vincent-development-environment';

packages/libs/e2e-test-utils/src/lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export { ensureUnexpiredCapacityToken } from './ensure-capacity-credit';
1818
export { getEnv } from './env';
1919
export { getChainHelpers } from './chain';
2020
export { createRandomVincentWallets } from './create-random-vincent-wallets';
21+
export {
22+
setupVincentDevelopmentEnvironment,
23+
type VincentDevEnvironment,
24+
} from './setup-vincent-development-environment';
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import type { Wallet } from 'ethers';
2+
3+
import type { PermissionData } from '@lit-protocol/vincent-contracts-sdk';
4+
5+
import type { PkpInfo } from './mint-new-pkp';
6+
7+
import * as appManager from './appManager';
8+
import { getChainHelpers } from './chain';
9+
import * as delegatee from './delegatee';
10+
import * as delegator from './delegator';
11+
import { ensureUnexpiredCapacityToken } from './ensure-capacity-credit';
12+
import * as funder from './funder';
13+
14+
export interface VincentDevEnvironment {
15+
agentPkpInfo: PkpInfo;
16+
wallets: {
17+
appDelegatee: Wallet;
18+
funder: Wallet;
19+
appManager: Wallet;
20+
agentWalletOwner: Wallet;
21+
};
22+
appId: number;
23+
appVersion: number;
24+
}
25+
26+
/**
27+
* Helper function to set up a Vincent development environment.
28+
* This function handles all the necessary setup steps including:
29+
* - Checking and funding all required accounts (funder, app delegatee, app manager)
30+
* - Registering or updating your app with abilities and policies
31+
* - Creating or using an existing agent PKP
32+
* - Setting up permissions for the agent PKP
33+
* - Ensuring a valid capacity token exists
34+
*
35+
* @param permissionData permission data containing abilities and their policies
36+
* @returns the setup result including agent PKP info, wallets, app ID, and app version
37+
* @example
38+
* ```typescript
39+
* // Example with no policies
40+
* const permissionData = {
41+
* [bundledVincentAbility.ipfsCid]: {},
42+
* };
43+
*
44+
* // Example with policies
45+
* const permissionDataWithPolicies = {
46+
* [bundledVincentAbility.ipfsCid]: {
47+
* [spendingLimitPolicy.ipfsCid]: {
48+
* limit: '1000000',
49+
* period: '86400',
50+
* },
51+
* },
52+
* };
53+
*
54+
* const result = await setupVincentDevelopmentEnvironment({ permissionData });
55+
* ```
56+
*/
57+
export const setupVincentDevelopmentEnvironment = async ({
58+
permissionData,
59+
}: {
60+
permissionData: PermissionData;
61+
}): Promise<VincentDevEnvironment> => {
62+
// Check and fund all required accounts
63+
await funder.checkFunderBalance();
64+
await delegatee.ensureAppDelegateeFunded();
65+
await appManager.ensureAppManagerFunded();
66+
67+
const chainHelpers = await getChainHelpers();
68+
const wallets = chainHelpers.wallets;
69+
70+
const abilityIpfsCids: string[] = Object.keys(permissionData);
71+
const abilityPolicies: string[][] = abilityIpfsCids.map((abilityIpfsCid) => {
72+
return Object.keys(permissionData[abilityIpfsCid]);
73+
});
74+
75+
// If an app exists for the delegatee, we will create a new app version with the new ipfs cids
76+
// Otherwise, we will create an app w/ version 1 appVersion with the new ipfs cids
77+
const existingApp = await delegatee.getAppInfo();
78+
let appId: number;
79+
let appVersion: number;
80+
if (!existingApp) {
81+
const newApp = await appManager.registerNewApp({ abilityIpfsCids, abilityPolicies });
82+
appId = newApp.appId;
83+
appVersion = newApp.appVersion;
84+
} else {
85+
const newAppVersion = await appManager.registerNewAppVersion({
86+
abilityIpfsCids,
87+
abilityPolicies,
88+
});
89+
appId = existingApp.appId;
90+
appVersion = newAppVersion.appVersion;
91+
}
92+
93+
const agentPkpInfo = await delegator.getFundedAgentPkp();
94+
95+
await delegator.permitAppVersionForAgentWalletPkp({
96+
permissionData,
97+
appId,
98+
appVersion,
99+
agentPkpInfo,
100+
});
101+
102+
await delegator.addPermissionForAbilities(
103+
wallets.agentWalletOwner,
104+
agentPkpInfo.tokenId,
105+
abilityIpfsCids,
106+
);
107+
108+
// Ensure capacity token is valid and unexpired
109+
await ensureUnexpiredCapacityToken(wallets.appDelegatee);
110+
111+
return {
112+
agentPkpInfo,
113+
wallets,
114+
appId,
115+
appVersion,
116+
};
117+
};

0 commit comments

Comments
 (0)