@coinbase/agentkit 0.10.1 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -40
- package/dist/action-providers/across/schemas.d.ts +1 -1
- package/dist/action-providers/baseAccount/baseAccountActionProvider.d.ts +46 -0
- package/dist/action-providers/baseAccount/baseAccountActionProvider.js +404 -0
- package/dist/action-providers/baseAccount/baseAccountActionProvider.test.d.ts +1 -0
- package/dist/action-providers/baseAccount/baseAccountActionProvider.test.js +325 -0
- package/dist/action-providers/baseAccount/index.d.ts +2 -0
- package/dist/action-providers/baseAccount/index.js +18 -0
- package/dist/action-providers/baseAccount/schemas.d.ts +43 -0
- package/dist/action-providers/baseAccount/schemas.js +62 -0
- package/dist/action-providers/baseAccount/types.d.ts +17 -0
- package/dist/action-providers/baseAccount/types.js +2 -0
- package/dist/action-providers/baseAccount/utils.d.ts +14 -0
- package/dist/action-providers/baseAccount/utils.js +57 -0
- package/dist/action-providers/cdp/cdpEvmWalletActionProvider.js +2 -1
- package/dist/action-providers/cdp/cdpEvmWalletActionProvider.test.js +3 -1
- package/dist/action-providers/cdp/cdpSmartWalletActionProvider.js +2 -1
- package/dist/action-providers/cdp/cdpSmartWalletActionProvider.test.js +3 -1
- package/dist/action-providers/cdp/swapUtils.d.ts +0 -9
- package/dist/action-providers/cdp/swapUtils.js +0 -36
- package/dist/action-providers/clanker/schemas.d.ts +2 -2
- package/dist/action-providers/flaunch/client_utils.d.ts +25 -0
- package/dist/action-providers/flaunch/client_utils.js +62 -0
- package/dist/action-providers/flaunch/constants.d.ts +41 -20
- package/dist/action-providers/flaunch/constants.js +111 -36
- package/dist/action-providers/flaunch/flaunchActionProvider.d.ts +4 -43
- package/dist/action-providers/flaunch/flaunchActionProvider.js +132 -200
- package/dist/action-providers/flaunch/flaunchActionProvider.test.js +108 -13
- package/dist/action-providers/flaunch/metadata_utils.d.ts +12 -0
- package/dist/action-providers/flaunch/metadata_utils.js +216 -0
- package/dist/action-providers/flaunch/schemas.d.ts +39 -3
- package/dist/action-providers/flaunch/schemas.js +62 -10
- package/dist/action-providers/flaunch/{utils.d.ts → swap_utils.d.ts} +17 -19
- package/dist/action-providers/flaunch/{utils.js → swap_utils.js} +137 -172
- package/dist/action-providers/index.d.ts +1 -0
- package/dist/action-providers/index.js +1 -0
- package/dist/action-providers/pyth/pythActionProvider.d.ts +2 -2
- package/dist/action-providers/pyth/pythActionProvider.js +83 -31
- package/dist/action-providers/pyth/pythActionProvider.test.js +178 -26
- package/dist/action-providers/pyth/schemas.d.ts +6 -0
- package/dist/action-providers/pyth/schemas.js +9 -1
- package/dist/action-providers/superfluid/superfluidSuperTokenCreatorActionProvider.js +5 -4
- package/dist/action-providers/superfluid/utils/parseLogs.d.ts +2 -1
- package/dist/action-providers/superfluid/utils/parseLogs.js +6 -3
- package/dist/action-providers/wallet/walletActionProvider.js +3 -0
- package/dist/action-providers/x402/schemas.d.ts +7 -0
- package/dist/action-providers/x402/schemas.js +11 -1
- package/dist/action-providers/x402/utils.d.ts +55 -0
- package/dist/action-providers/x402/utils.js +160 -0
- package/dist/action-providers/x402/x402ActionProvider.d.ts +9 -9
- package/dist/action-providers/x402/x402ActionProvider.js +158 -39
- package/dist/action-providers/x402/x402ActionProvider.test.js +116 -10
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +43 -13
- package/dist/wallet-providers/cdpEvmWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/cdpEvmWalletProvider.js +14 -21
- package/dist/wallet-providers/cdpEvmWalletProvider.test.js +7 -0
- package/dist/wallet-providers/cdpSmartWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/cdpSmartWalletProvider.js +23 -6
- package/dist/wallet-providers/cdpSmartWalletProvider.test.js +6 -10
- package/dist/wallet-providers/evmWalletProvider.d.ts +9 -2
- package/dist/wallet-providers/evmWalletProvider.js +4 -0
- package/dist/wallet-providers/legacyCdpSmartWalletProvider.d.ts +9 -0
- package/dist/wallet-providers/legacyCdpSmartWalletProvider.js +11 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.js +16 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.test.js +6 -0
- package/dist/wallet-providers/privyEvmDelegatedEmbeddedWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/privyEvmDelegatedEmbeddedWalletProvider.js +27 -0
- package/dist/wallet-providers/privyEvmWalletProvider.test.js +11 -0
- package/dist/wallet-providers/viemWalletProvider.d.ts +8 -1
- package/dist/wallet-providers/viemWalletProvider.js +21 -1
- package/dist/wallet-providers/viemWalletProvider.test.js +21 -1
- package/dist/wallet-providers/zeroDevWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/zeroDevWalletProvider.js +12 -0
- package/dist/wallet-providers/zeroDevWalletProvider.test.js +10 -0
- package/package.json +6 -4
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const baseAccountActionProvider_1 = require("./baseAccountActionProvider");
|
|
4
|
+
const schemas_1 = require("./schemas");
|
|
5
|
+
const utils_1 = require("../erc20/utils");
|
|
6
|
+
// Mock the getTokenDetails function from ERC20 utils
|
|
7
|
+
jest.mock("../erc20/utils", () => ({
|
|
8
|
+
getTokenDetails: jest.fn(),
|
|
9
|
+
}));
|
|
10
|
+
// Mock the @base-org/account package to avoid ES module import issues in Jest
|
|
11
|
+
jest.mock("@base-org/account/spend-permission", () => ({
|
|
12
|
+
fetchPermissions: jest.fn(),
|
|
13
|
+
getPermissionStatus: jest.fn(),
|
|
14
|
+
prepareSpendCallData: jest.fn(),
|
|
15
|
+
prepareRevokeCallData: jest.fn(),
|
|
16
|
+
}));
|
|
17
|
+
const mockGetTokenDetails = utils_1.getTokenDetails;
|
|
18
|
+
// Get references to the mocked functions from the mocked modules
|
|
19
|
+
const mockFetchPermissions = jest.fn();
|
|
20
|
+
const mockGetPermissionStatus = jest.fn();
|
|
21
|
+
const mockPrepareSpendCallData = jest.fn();
|
|
22
|
+
const mockPrepareRevokeCallData = jest.fn();
|
|
23
|
+
// Override the mocked module with our jest functions
|
|
24
|
+
jest.mocked(jest.requireMock("@base-org/account/spend-permission")).fetchPermissions =
|
|
25
|
+
mockFetchPermissions;
|
|
26
|
+
jest.mocked(jest.requireMock("@base-org/account/spend-permission")).getPermissionStatus =
|
|
27
|
+
mockGetPermissionStatus;
|
|
28
|
+
jest.mocked(jest.requireMock("@base-org/account/spend-permission")).prepareSpendCallData =
|
|
29
|
+
mockPrepareSpendCallData;
|
|
30
|
+
jest.mocked(jest.requireMock("@base-org/account/spend-permission")).prepareRevokeCallData =
|
|
31
|
+
mockPrepareRevokeCallData;
|
|
32
|
+
const MOCK_BASE_ACCOUNT = "0x1234567890123456789012345678901234567890";
|
|
33
|
+
const MOCK_SPENDER_ADDRESS = "0x9876543210987654321098765432109876543210";
|
|
34
|
+
const MOCK_AMOUNT_USD = 10.5;
|
|
35
|
+
const MOCK_TOKEN_ADDRESS = "0xA0b86a33E6441E0100d473BB88F5C8b3d8c13ab3"; // Mock USDC address
|
|
36
|
+
// Mock permission data
|
|
37
|
+
const mockPermission = {
|
|
38
|
+
permissionHash: "0xmockHash",
|
|
39
|
+
signature: "0xmockSignature",
|
|
40
|
+
chainId: 8453,
|
|
41
|
+
createdAt: 1700000000,
|
|
42
|
+
permission: {
|
|
43
|
+
account: MOCK_BASE_ACCOUNT,
|
|
44
|
+
spender: MOCK_SPENDER_ADDRESS,
|
|
45
|
+
token: MOCK_TOKEN_ADDRESS,
|
|
46
|
+
allowance: "1000000000", // 1000 USDC (6 decimals) as string
|
|
47
|
+
period: 86400, // 1 day
|
|
48
|
+
start: 1700000000,
|
|
49
|
+
end: 1700086400,
|
|
50
|
+
salt: "0xsalt",
|
|
51
|
+
extraData: "0x",
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const mockPermissionStatus = {
|
|
55
|
+
remainingSpend: BigInt("500000000"), // 500 USDC remaining
|
|
56
|
+
nextPeriodStart: new Date(1700086400 * 1000), // Convert timestamp to Date
|
|
57
|
+
isActive: true,
|
|
58
|
+
};
|
|
59
|
+
// Mock token details for USDC
|
|
60
|
+
const mockTokenDetails = {
|
|
61
|
+
name: "USD Coin",
|
|
62
|
+
decimals: 6,
|
|
63
|
+
balance: BigInt("1000000000"), // 1000 USDC
|
|
64
|
+
formattedBalance: "1000",
|
|
65
|
+
};
|
|
66
|
+
describe("ListBaseAccountSpendPermissionsSchema", () => {
|
|
67
|
+
it("should successfully parse valid input", () => {
|
|
68
|
+
const validInput = {
|
|
69
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
70
|
+
};
|
|
71
|
+
const result = schemas_1.ListBaseAccountSpendPermissionsSchema.safeParse(validInput);
|
|
72
|
+
expect(result.success).toBe(true);
|
|
73
|
+
expect(result.data).toEqual(validInput);
|
|
74
|
+
});
|
|
75
|
+
it("should fail parsing invalid address", () => {
|
|
76
|
+
const invalidInput = {
|
|
77
|
+
baseAccount: "invalid-address",
|
|
78
|
+
};
|
|
79
|
+
const result = schemas_1.ListBaseAccountSpendPermissionsSchema.safeParse(invalidInput);
|
|
80
|
+
expect(result.success).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
it("should fail parsing empty input", () => {
|
|
83
|
+
const emptyInput = {};
|
|
84
|
+
const result = schemas_1.ListBaseAccountSpendPermissionsSchema.safeParse(emptyInput);
|
|
85
|
+
expect(result.success).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe("UseBaseAccountSpendPermissionSchema", () => {
|
|
89
|
+
it("should successfully parse valid input", () => {
|
|
90
|
+
const validInput = {
|
|
91
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
92
|
+
amount: MOCK_AMOUNT_USD,
|
|
93
|
+
};
|
|
94
|
+
const result = schemas_1.UseBaseAccountSpendPermissionSchema.safeParse(validInput);
|
|
95
|
+
expect(result.success).toBe(true);
|
|
96
|
+
expect(result.data).toEqual(validInput);
|
|
97
|
+
});
|
|
98
|
+
it("should fail parsing negative amount", () => {
|
|
99
|
+
const invalidInput = {
|
|
100
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
101
|
+
amount: -5,
|
|
102
|
+
};
|
|
103
|
+
const result = schemas_1.UseBaseAccountSpendPermissionSchema.safeParse(invalidInput);
|
|
104
|
+
expect(result.success).toBe(false);
|
|
105
|
+
});
|
|
106
|
+
it("should fail parsing zero amount", () => {
|
|
107
|
+
const invalidInput = {
|
|
108
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
109
|
+
amount: 0,
|
|
110
|
+
};
|
|
111
|
+
const result = schemas_1.UseBaseAccountSpendPermissionSchema.safeParse(invalidInput);
|
|
112
|
+
expect(result.success).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
describe("RevokeBaseAccountSpendPermissionSchema", () => {
|
|
116
|
+
it("should successfully parse valid input with permission index", () => {
|
|
117
|
+
const validInput = {
|
|
118
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
119
|
+
permissionIndex: 2,
|
|
120
|
+
};
|
|
121
|
+
const result = schemas_1.RevokeBaseAccountSpendPermissionSchema.safeParse(validInput);
|
|
122
|
+
expect(result.success).toBe(true);
|
|
123
|
+
expect(result.data).toEqual(validInput);
|
|
124
|
+
});
|
|
125
|
+
it("should successfully parse valid input without permission index", () => {
|
|
126
|
+
const validInput = {
|
|
127
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
128
|
+
};
|
|
129
|
+
const result = schemas_1.RevokeBaseAccountSpendPermissionSchema.safeParse(validInput);
|
|
130
|
+
expect(result.success).toBe(true);
|
|
131
|
+
expect(result.data).toEqual(validInput);
|
|
132
|
+
});
|
|
133
|
+
it("should fail parsing negative permission index", () => {
|
|
134
|
+
const invalidInput = {
|
|
135
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
136
|
+
permissionIndex: -1,
|
|
137
|
+
};
|
|
138
|
+
const result = schemas_1.RevokeBaseAccountSpendPermissionSchema.safeParse(invalidInput);
|
|
139
|
+
expect(result.success).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
describe("BaseAccountActionProvider", () => {
|
|
143
|
+
let mockWallet;
|
|
144
|
+
const actionProvider = (0, baseAccountActionProvider_1.baseAccountActionProvider)();
|
|
145
|
+
beforeEach(() => {
|
|
146
|
+
mockWallet = {
|
|
147
|
+
getAddress: jest.fn().mockReturnValue(MOCK_SPENDER_ADDRESS),
|
|
148
|
+
getNetwork: jest.fn().mockReturnValue({
|
|
149
|
+
protocolFamily: "evm",
|
|
150
|
+
networkId: "base-mainnet",
|
|
151
|
+
}),
|
|
152
|
+
sendTransaction: jest.fn().mockResolvedValue("0xmockTransactionHash"),
|
|
153
|
+
waitForTransactionReceipt: jest.fn(),
|
|
154
|
+
};
|
|
155
|
+
// Reset mocks before each test
|
|
156
|
+
mockFetchPermissions.mockReset();
|
|
157
|
+
mockGetPermissionStatus.mockReset();
|
|
158
|
+
mockPrepareSpendCallData.mockReset();
|
|
159
|
+
mockPrepareRevokeCallData.mockReset();
|
|
160
|
+
mockGetTokenDetails.mockReset();
|
|
161
|
+
});
|
|
162
|
+
describe("supportsNetwork", () => {
|
|
163
|
+
it("should return true for base-mainnet", () => {
|
|
164
|
+
expect(actionProvider.supportsNetwork({ networkId: "base-mainnet", protocolFamily: "evm" })).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
it("should return false for other networks", () => {
|
|
167
|
+
expect(actionProvider.supportsNetwork({ networkId: "base-sepolia", protocolFamily: "evm" })).toBe(false);
|
|
168
|
+
expect(actionProvider.supportsNetwork({ networkId: "ethereum-mainnet", protocolFamily: "evm" })).toBe(false);
|
|
169
|
+
expect(actionProvider.supportsNetwork({ protocolFamily: "solana" })).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe("listBaseAccountSpendPermissions", () => {
|
|
173
|
+
it("should return empty permissions when none found", async () => {
|
|
174
|
+
mockFetchPermissions.mockResolvedValue([]);
|
|
175
|
+
const args = {
|
|
176
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
177
|
+
};
|
|
178
|
+
const response = await actionProvider.listBaseAccountSpendPermissions(mockWallet, args);
|
|
179
|
+
const parsedResponse = JSON.parse(response);
|
|
180
|
+
expect(parsedResponse.success).toBe(false);
|
|
181
|
+
expect(parsedResponse.error).toContain("No spend permissions found for Base Account");
|
|
182
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
183
|
+
expect(parsedResponse.spender).toBe(MOCK_SPENDER_ADDRESS);
|
|
184
|
+
expect(parsedResponse.permissionsCount).toBe(0);
|
|
185
|
+
});
|
|
186
|
+
it("should list permissions when found", async () => {
|
|
187
|
+
mockFetchPermissions.mockResolvedValue([mockPermission]);
|
|
188
|
+
const args = {
|
|
189
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
190
|
+
};
|
|
191
|
+
const response = await actionProvider.listBaseAccountSpendPermissions(mockWallet, args);
|
|
192
|
+
const parsedResponse = JSON.parse(response);
|
|
193
|
+
expect(parsedResponse.success).toBe(true);
|
|
194
|
+
expect(parsedResponse.permissionsCount).toBe(1);
|
|
195
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
196
|
+
expect(parsedResponse.spender).toBe(MOCK_SPENDER_ADDRESS);
|
|
197
|
+
expect(parsedResponse.permissions).toHaveLength(1);
|
|
198
|
+
expect(parsedResponse.permissions[0].permissionIndex).toBe(1);
|
|
199
|
+
expect(parsedResponse.permissions[0].token).toBe(MOCK_TOKEN_ADDRESS);
|
|
200
|
+
});
|
|
201
|
+
it("should normalize addresses properly", async () => {
|
|
202
|
+
mockFetchPermissions.mockResolvedValue([]);
|
|
203
|
+
const args = {
|
|
204
|
+
baseAccount: MOCK_BASE_ACCOUNT.toLowerCase(), // Test address normalization
|
|
205
|
+
};
|
|
206
|
+
const response = await actionProvider.listBaseAccountSpendPermissions(mockWallet, args);
|
|
207
|
+
const parsedResponse = JSON.parse(response);
|
|
208
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT); // Should be checksummed
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
describe("spendFromBaseAccountPermission", () => {
|
|
212
|
+
it("should handle no permissions available", async () => {
|
|
213
|
+
mockFetchPermissions.mockResolvedValue([]);
|
|
214
|
+
const args = {
|
|
215
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
216
|
+
amount: MOCK_AMOUNT_USD,
|
|
217
|
+
};
|
|
218
|
+
const response = await actionProvider.spendFromBaseAccountPermission(mockWallet, args);
|
|
219
|
+
const parsedResponse = JSON.parse(response);
|
|
220
|
+
expect(parsedResponse.success).toBe(false);
|
|
221
|
+
expect(parsedResponse.error).toContain("No spend permissions found for Base Account");
|
|
222
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
223
|
+
expect(parsedResponse.spender).toBe(MOCK_SPENDER_ADDRESS);
|
|
224
|
+
});
|
|
225
|
+
it("should successfully spend from permission", async () => {
|
|
226
|
+
mockFetchPermissions.mockResolvedValue([mockPermission]);
|
|
227
|
+
mockGetPermissionStatus.mockResolvedValue(mockPermissionStatus);
|
|
228
|
+
mockGetTokenDetails.mockResolvedValue(mockTokenDetails);
|
|
229
|
+
mockPrepareSpendCallData.mockResolvedValue([
|
|
230
|
+
{
|
|
231
|
+
to: "0xSpendContract",
|
|
232
|
+
data: "0xspendCallData",
|
|
233
|
+
value: "0x0",
|
|
234
|
+
},
|
|
235
|
+
]);
|
|
236
|
+
const args = {
|
|
237
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
238
|
+
amount: 10.5, // 10.5 USDC
|
|
239
|
+
};
|
|
240
|
+
const response = await actionProvider.spendFromBaseAccountPermission(mockWallet, args);
|
|
241
|
+
const parsedResponse = JSON.parse(response);
|
|
242
|
+
expect(parsedResponse.success).toBe(true);
|
|
243
|
+
expect(parsedResponse.transactionHash).toBe("0xmockTransactionHash");
|
|
244
|
+
expect(parsedResponse.amountSpent).toBe("10.5");
|
|
245
|
+
expect(parsedResponse.tokenAddress).toBe(MOCK_TOKEN_ADDRESS);
|
|
246
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
247
|
+
expect(parsedResponse.permissionIndex).toBe(1);
|
|
248
|
+
expect(mockWallet.sendTransaction).toHaveBeenCalledWith({
|
|
249
|
+
to: "0xSpendContract",
|
|
250
|
+
data: "0xspendCallData",
|
|
251
|
+
value: BigInt("0x0"),
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
it("should handle insufficient allowance", async () => {
|
|
255
|
+
mockFetchPermissions.mockResolvedValue([mockPermission]);
|
|
256
|
+
const lowAllowanceStatus = {
|
|
257
|
+
remainingSpend: BigInt("1000000"), // Only 1 USDC remaining
|
|
258
|
+
nextPeriodStart: new Date(1700086400 * 1000),
|
|
259
|
+
isActive: true,
|
|
260
|
+
};
|
|
261
|
+
mockGetPermissionStatus.mockResolvedValue(lowAllowanceStatus);
|
|
262
|
+
mockGetTokenDetails.mockResolvedValue(mockTokenDetails);
|
|
263
|
+
const args = {
|
|
264
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
265
|
+
amount: 10.5, // Trying to spend 10.5 USDC but only 1 available
|
|
266
|
+
};
|
|
267
|
+
const response = await actionProvider.spendFromBaseAccountPermission(mockWallet, args);
|
|
268
|
+
const parsedResponse = JSON.parse(response);
|
|
269
|
+
expect(parsedResponse.success).toBe(false);
|
|
270
|
+
expect(parsedResponse.error).toContain("Insufficient remaining allowance");
|
|
271
|
+
expect(parsedResponse.requestedAmount).toBe("10.5");
|
|
272
|
+
expect(parsedResponse.availableAmount).toBe("1");
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
describe("revokeBaseAccountSpendPermission", () => {
|
|
276
|
+
it("should handle no permissions to revoke", async () => {
|
|
277
|
+
mockFetchPermissions.mockResolvedValue([]);
|
|
278
|
+
const args = {
|
|
279
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
280
|
+
};
|
|
281
|
+
const response = await actionProvider.revokeBaseAccountSpendPermission(mockWallet, args);
|
|
282
|
+
const parsedResponse = JSON.parse(response);
|
|
283
|
+
expect(parsedResponse.success).toBe(false);
|
|
284
|
+
expect(parsedResponse.error).toContain("No spend permissions found for Base Account");
|
|
285
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
286
|
+
expect(parsedResponse.spender).toBe(MOCK_SPENDER_ADDRESS);
|
|
287
|
+
});
|
|
288
|
+
it("should successfully revoke a permission", async () => {
|
|
289
|
+
mockFetchPermissions.mockResolvedValue([mockPermission]);
|
|
290
|
+
mockPrepareRevokeCallData.mockResolvedValue({
|
|
291
|
+
to: "0xRevokeContract",
|
|
292
|
+
data: "0xrevokeCallData",
|
|
293
|
+
value: "0x0",
|
|
294
|
+
});
|
|
295
|
+
const args = {
|
|
296
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
297
|
+
permissionIndex: 1,
|
|
298
|
+
};
|
|
299
|
+
const response = await actionProvider.revokeBaseAccountSpendPermission(mockWallet, args);
|
|
300
|
+
const parsedResponse = JSON.parse(response);
|
|
301
|
+
expect(parsedResponse.success).toBe(true);
|
|
302
|
+
expect(parsedResponse.transactionHash).toBe("0xmockTransactionHash");
|
|
303
|
+
expect(parsedResponse.revokedPermissionIndex).toBe(1);
|
|
304
|
+
expect(parsedResponse.baseAccount).toBe(MOCK_BASE_ACCOUNT);
|
|
305
|
+
expect(parsedResponse.spender).toBe(MOCK_SPENDER_ADDRESS);
|
|
306
|
+
expect(mockWallet.sendTransaction).toHaveBeenCalledWith({
|
|
307
|
+
to: "0xRevokeContract",
|
|
308
|
+
data: "0xrevokeCallData",
|
|
309
|
+
value: BigInt("0x0"),
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
it("should handle out of range permission index", async () => {
|
|
313
|
+
mockFetchPermissions.mockResolvedValue([mockPermission]);
|
|
314
|
+
const args = {
|
|
315
|
+
baseAccount: MOCK_BASE_ACCOUNT,
|
|
316
|
+
permissionIndex: 5, // Only 1 permission available
|
|
317
|
+
};
|
|
318
|
+
const response = await actionProvider.revokeBaseAccountSpendPermission(mockWallet, args);
|
|
319
|
+
const parsedResponse = JSON.parse(response);
|
|
320
|
+
expect(parsedResponse.success).toBe(false);
|
|
321
|
+
expect(parsedResponse.error).toContain("Permission index 5 is out of range");
|
|
322
|
+
expect(parsedResponse.availablePermissions).toBe(1);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./baseAccountActionProvider"), exports);
|
|
18
|
+
__exportStar(require("./schemas"), exports);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Input schema for listing Base Account spend permissions action.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ListBaseAccountSpendPermissionsSchema: z.ZodObject<{
|
|
6
|
+
baseAccount: z.ZodString;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
baseAccount: string;
|
|
9
|
+
}, {
|
|
10
|
+
baseAccount: string;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Input schema for using a Base Account spend permission action.
|
|
14
|
+
*/
|
|
15
|
+
export declare const UseBaseAccountSpendPermissionSchema: z.ZodObject<{
|
|
16
|
+
baseAccount: z.ZodString;
|
|
17
|
+
amount: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
tokenAddress: z.ZodOptional<z.ZodString>;
|
|
19
|
+
permissionIndex: z.ZodOptional<z.ZodNumber>;
|
|
20
|
+
}, "strip", z.ZodTypeAny, {
|
|
21
|
+
baseAccount: string;
|
|
22
|
+
amount?: number | undefined;
|
|
23
|
+
tokenAddress?: string | undefined;
|
|
24
|
+
permissionIndex?: number | undefined;
|
|
25
|
+
}, {
|
|
26
|
+
baseAccount: string;
|
|
27
|
+
amount?: number | undefined;
|
|
28
|
+
tokenAddress?: string | undefined;
|
|
29
|
+
permissionIndex?: number | undefined;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Input schema for revoking a Base Account spend permission action.
|
|
33
|
+
*/
|
|
34
|
+
export declare const RevokeBaseAccountSpendPermissionSchema: z.ZodObject<{
|
|
35
|
+
baseAccount: z.ZodString;
|
|
36
|
+
permissionIndex: z.ZodOptional<z.ZodNumber>;
|
|
37
|
+
}, "strip", z.ZodTypeAny, {
|
|
38
|
+
baseAccount: string;
|
|
39
|
+
permissionIndex?: number | undefined;
|
|
40
|
+
}, {
|
|
41
|
+
baseAccount: string;
|
|
42
|
+
permissionIndex?: number | undefined;
|
|
43
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RevokeBaseAccountSpendPermissionSchema = exports.UseBaseAccountSpendPermissionSchema = exports.ListBaseAccountSpendPermissionsSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
/**
|
|
6
|
+
* Input schema for listing Base Account spend permissions action.
|
|
7
|
+
*/
|
|
8
|
+
exports.ListBaseAccountSpendPermissionsSchema = zod_1.z
|
|
9
|
+
.object({
|
|
10
|
+
baseAccount: zod_1.z
|
|
11
|
+
.string()
|
|
12
|
+
.regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address format")
|
|
13
|
+
.describe("The Base Account address to query if it has granted spend permissions"),
|
|
14
|
+
})
|
|
15
|
+
.strip()
|
|
16
|
+
.describe("Instructions for listing spend permissions for a Base Account");
|
|
17
|
+
/**
|
|
18
|
+
* Input schema for using a Base Account spend permission action.
|
|
19
|
+
*/
|
|
20
|
+
exports.UseBaseAccountSpendPermissionSchema = zod_1.z
|
|
21
|
+
.object({
|
|
22
|
+
baseAccount: zod_1.z
|
|
23
|
+
.string()
|
|
24
|
+
.regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address format")
|
|
25
|
+
.describe("The Base Account address that has granted the spend permission"),
|
|
26
|
+
amount: zod_1.z
|
|
27
|
+
.number()
|
|
28
|
+
.positive()
|
|
29
|
+
.optional()
|
|
30
|
+
.describe("The amount to spend in whole units of the token (e.g. 4.6 for 4.6 tokens). If not provided, will withdraw the full remaining allowance"),
|
|
31
|
+
tokenAddress: zod_1.z
|
|
32
|
+
.string()
|
|
33
|
+
.regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address format")
|
|
34
|
+
.optional()
|
|
35
|
+
.describe("The token contract address. If not provided, will use the first available permission token"),
|
|
36
|
+
permissionIndex: zod_1.z
|
|
37
|
+
.number()
|
|
38
|
+
.int()
|
|
39
|
+
.positive()
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("The index of the permission to use (1-based). If not provided, the first permission will be used"),
|
|
42
|
+
})
|
|
43
|
+
.strip()
|
|
44
|
+
.describe("Instructions for using a Base Account spend permission");
|
|
45
|
+
/**
|
|
46
|
+
* Input schema for revoking a Base Account spend permission action.
|
|
47
|
+
*/
|
|
48
|
+
exports.RevokeBaseAccountSpendPermissionSchema = zod_1.z
|
|
49
|
+
.object({
|
|
50
|
+
baseAccount: zod_1.z
|
|
51
|
+
.string()
|
|
52
|
+
.regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address format")
|
|
53
|
+
.describe("The Base Account address that has granted the spend permission"),
|
|
54
|
+
permissionIndex: zod_1.z
|
|
55
|
+
.number()
|
|
56
|
+
.int()
|
|
57
|
+
.positive()
|
|
58
|
+
.optional()
|
|
59
|
+
.describe("The index of the permission to revoke (1-based). If not provided, the first permission will be revoked"),
|
|
60
|
+
})
|
|
61
|
+
.strip()
|
|
62
|
+
.describe("Instructions for revoking a Base Account spend permission");
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface PermissionData {
|
|
2
|
+
account?: string;
|
|
3
|
+
spender?: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
allowance?: bigint | string;
|
|
6
|
+
period?: number;
|
|
7
|
+
start?: number;
|
|
8
|
+
end?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface FetchedPermission {
|
|
11
|
+
permissionHash?: string;
|
|
12
|
+
signature?: string;
|
|
13
|
+
chainId?: number;
|
|
14
|
+
createdAt?: number;
|
|
15
|
+
permission?: PermissionData;
|
|
16
|
+
allowance?: bigint | string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert Unix timestamp to human-readable format
|
|
3
|
+
*
|
|
4
|
+
* @param timestamp - Unix timestamp to convert
|
|
5
|
+
* @returns Human-readable date string
|
|
6
|
+
*/
|
|
7
|
+
export declare function formatTimestamp(timestamp: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Convert period in seconds to human-readable format
|
|
10
|
+
*
|
|
11
|
+
* @param seconds - Period in seconds to convert
|
|
12
|
+
* @returns Human-readable period string
|
|
13
|
+
*/
|
|
14
|
+
export declare function formatPeriod(seconds: number): string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatTimestamp = formatTimestamp;
|
|
4
|
+
exports.formatPeriod = formatPeriod;
|
|
5
|
+
/**
|
|
6
|
+
* Convert Unix timestamp to human-readable format
|
|
7
|
+
*
|
|
8
|
+
* @param timestamp - Unix timestamp to convert
|
|
9
|
+
* @returns Human-readable date string
|
|
10
|
+
*/
|
|
11
|
+
function formatTimestamp(timestamp) {
|
|
12
|
+
// Handle special case for maximum timestamp (never expires)
|
|
13
|
+
// 281474976710655 is the maximum value used in spend permissions to indicate "never expires"
|
|
14
|
+
if (timestamp >= 281474976710655) {
|
|
15
|
+
return "Never expires";
|
|
16
|
+
}
|
|
17
|
+
// Check if the timestamp would be valid when converted to milliseconds
|
|
18
|
+
const timestampMs = timestamp * 1000;
|
|
19
|
+
if (timestampMs > Number.MAX_SAFE_INTEGER) {
|
|
20
|
+
return "Far future";
|
|
21
|
+
}
|
|
22
|
+
const date = new Date(timestampMs);
|
|
23
|
+
if (isNaN(date.getTime())) {
|
|
24
|
+
return "Invalid timestamp";
|
|
25
|
+
}
|
|
26
|
+
return date.toLocaleString("en-US", {
|
|
27
|
+
year: "numeric",
|
|
28
|
+
month: "short",
|
|
29
|
+
day: "numeric",
|
|
30
|
+
hour: "2-digit",
|
|
31
|
+
minute: "2-digit",
|
|
32
|
+
second: "2-digit",
|
|
33
|
+
timeZoneName: "short",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Convert period in seconds to human-readable format
|
|
38
|
+
*
|
|
39
|
+
* @param seconds - Period in seconds to convert
|
|
40
|
+
* @returns Human-readable period string
|
|
41
|
+
*/
|
|
42
|
+
function formatPeriod(seconds) {
|
|
43
|
+
const days = Math.floor(seconds / 86400);
|
|
44
|
+
const hours = Math.floor((seconds % 86400) / 3600);
|
|
45
|
+
const minutes = Math.floor((seconds % 3600) / 60);
|
|
46
|
+
const remainingSeconds = seconds % 60;
|
|
47
|
+
const parts = [];
|
|
48
|
+
if (days > 0)
|
|
49
|
+
parts.push(`${days} day${days !== 1 ? "s" : ""}`);
|
|
50
|
+
if (hours > 0)
|
|
51
|
+
parts.push(`${hours} hour${hours !== 1 ? "s" : ""}`);
|
|
52
|
+
if (minutes > 0)
|
|
53
|
+
parts.push(`${minutes} minute${minutes !== 1 ? "s" : ""}`);
|
|
54
|
+
if (remainingSeconds > 0)
|
|
55
|
+
parts.push(`${remainingSeconds} second${remainingSeconds !== 1 ? "s" : ""}`);
|
|
56
|
+
return parts.length > 0 ? parts.join(", ") : "0 seconds";
|
|
57
|
+
}
|
|
@@ -20,6 +20,7 @@ const schemas_1 = require("./schemas");
|
|
|
20
20
|
const spendPermissionUtils_1 = require("./spendPermissionUtils");
|
|
21
21
|
const swapUtils_1 = require("./swapUtils");
|
|
22
22
|
const viem_1 = require("viem");
|
|
23
|
+
const utils_1 = require("../../utils");
|
|
23
24
|
/**
|
|
24
25
|
* CdpEvmWalletActionProvider is an action provider for CDP EVM Wallet specific actions.
|
|
25
26
|
*
|
|
@@ -232,7 +233,7 @@ class CdpEvmWalletActionProvider extends actionProvider_1.ActionProvider {
|
|
|
232
233
|
}
|
|
233
234
|
}
|
|
234
235
|
// Execute swap using the all-in-one pattern with retry logic
|
|
235
|
-
const swapResult = await (0,
|
|
236
|
+
const swapResult = await (0, utils_1.retryWithExponentialBackoff)(async () => {
|
|
236
237
|
return (await account.swap({
|
|
237
238
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
238
239
|
network: cdpNetwork,
|
|
@@ -37,17 +37,19 @@ const cdpEvmWalletActionProvider_1 = require("./cdpEvmWalletActionProvider");
|
|
|
37
37
|
const schemas_1 = require("./schemas");
|
|
38
38
|
const spendPermissionUtils = __importStar(require("./spendPermissionUtils"));
|
|
39
39
|
const swapUtils = __importStar(require("./swapUtils"));
|
|
40
|
+
const utils = __importStar(require("../../utils"));
|
|
40
41
|
// Mock the CDP SDK and utility functions
|
|
41
42
|
jest.mock("@coinbase/cdp-sdk");
|
|
42
43
|
jest.mock("./spendPermissionUtils");
|
|
43
44
|
jest.mock("./swapUtils");
|
|
45
|
+
jest.mock("../../utils");
|
|
44
46
|
describe("CDP EVM Wallet Action Provider", () => {
|
|
45
47
|
let actionProvider;
|
|
46
48
|
let mockWalletProvider;
|
|
47
49
|
let mockCdpClient;
|
|
48
50
|
let mockAccount;
|
|
49
51
|
const mockGetTokenDetails = swapUtils.getTokenDetails;
|
|
50
|
-
const mockRetryWithExponentialBackoff =
|
|
52
|
+
const mockRetryWithExponentialBackoff = utils.retryWithExponentialBackoff;
|
|
51
53
|
beforeEach(() => {
|
|
52
54
|
jest.clearAllMocks();
|
|
53
55
|
mockAccount = {
|
|
@@ -18,6 +18,7 @@ const schemas_1 = require("./schemas");
|
|
|
18
18
|
const spendPermissionUtils_1 = require("./spendPermissionUtils");
|
|
19
19
|
const swapUtils_1 = require("./swapUtils");
|
|
20
20
|
const viem_1 = require("viem");
|
|
21
|
+
const utils_1 = require("../../utils");
|
|
21
22
|
/**
|
|
22
23
|
* CdpSmartWalletActionProvider is an action provider for CDP Smart Wallet specific actions.
|
|
23
24
|
*
|
|
@@ -216,7 +217,7 @@ class CdpSmartWalletActionProvider extends actionProvider_1.ActionProvider {
|
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
219
|
// Execute swap using the all-in-one pattern with retry logic
|
|
219
|
-
const swapResult = await (0,
|
|
220
|
+
const swapResult = await (0, utils_1.retryWithExponentialBackoff)(async () => {
|
|
220
221
|
return (await walletProvider.smartAccount.swap({
|
|
221
222
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
222
223
|
network: cdpNetwork,
|
|
@@ -37,17 +37,19 @@ const cdpSmartWalletActionProvider_1 = require("./cdpSmartWalletActionProvider")
|
|
|
37
37
|
const schemas_1 = require("./schemas");
|
|
38
38
|
const spendPermissionUtils = __importStar(require("./spendPermissionUtils"));
|
|
39
39
|
const swapUtils = __importStar(require("./swapUtils"));
|
|
40
|
+
const utils = __importStar(require("../../utils"));
|
|
40
41
|
// Mock the CDP SDK and utility functions
|
|
41
42
|
jest.mock("@coinbase/cdp-sdk");
|
|
42
43
|
jest.mock("./spendPermissionUtils");
|
|
43
44
|
jest.mock("./swapUtils");
|
|
45
|
+
jest.mock("../../utils");
|
|
44
46
|
describe("CDP Smart Wallet Action Provider", () => {
|
|
45
47
|
let actionProvider;
|
|
46
48
|
let mockWalletProvider;
|
|
47
49
|
let mockCdpClient;
|
|
48
50
|
let mockSmartAccount;
|
|
49
51
|
const mockGetTokenDetails = swapUtils.getTokenDetails;
|
|
50
|
-
const mockRetryWithExponentialBackoff =
|
|
52
|
+
const mockRetryWithExponentialBackoff = utils.retryWithExponentialBackoff;
|
|
51
53
|
beforeEach(() => {
|
|
52
54
|
jest.clearAllMocks();
|
|
53
55
|
mockSmartAccount = {
|
|
@@ -21,12 +21,3 @@ export declare function getTokenDetails(walletProvider: EvmWalletProvider, fromT
|
|
|
21
21
|
fromTokenName: string;
|
|
22
22
|
toTokenName: string;
|
|
23
23
|
}>;
|
|
24
|
-
/**
|
|
25
|
-
* Retry function with exponential backoff
|
|
26
|
-
*
|
|
27
|
-
* @param fn - The function to retry
|
|
28
|
-
* @param maxRetries - Maximum number of retries (default: 3)
|
|
29
|
-
* @param baseDelay - Base delay in milliseconds (default: 1000)
|
|
30
|
-
* @returns Promise that resolves with the function result or rejects with the last error
|
|
31
|
-
*/
|
|
32
|
-
export declare function retryWithExponentialBackoff<T>(fn: () => Promise<T>, maxRetries?: number, baseDelay?: number): Promise<T>;
|