@agentxpay/sdk 0.1.0

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.
@@ -0,0 +1,135 @@
1
+ import { ethers } from "ethers";
2
+ import { AgentXPayConfig, DiscoverFilter, Service, X402FetchOptions, X402PaymentInfo } from "./types";
3
+ import { ServicesModule } from "./modules/services";
4
+ import { PaymentsModule } from "./modules/payments";
5
+ import { SubscriptionsModule } from "./modules/subscriptions";
6
+ import { EscrowModule } from "./modules/escrow";
7
+ import { WalletModule } from "./modules/wallet";
8
+ import { DEFAULT_CONTRACTS, MONAD_TESTNET_RPC } from "./utils/constants";
9
+
10
+ export class AgentXPayClient {
11
+ public services: ServicesModule;
12
+ public payments: PaymentsModule;
13
+ public subscriptions: SubscriptionsModule;
14
+ public escrow: EscrowModule;
15
+ public wallet: WalletModule;
16
+
17
+ private provider: ethers.Provider;
18
+ private signer: ethers.Signer | null = null;
19
+
20
+ constructor(config: AgentXPayConfig) {
21
+ const rpcUrl = config.rpcUrl || MONAD_TESTNET_RPC;
22
+ this.provider = new ethers.JsonRpcProvider(rpcUrl);
23
+
24
+ if (config.signer) {
25
+ this.signer = config.signer;
26
+ } else if (config.privateKey) {
27
+ this.signer = new ethers.Wallet(config.privateKey, this.provider);
28
+ }
29
+
30
+ const signerOrProvider = this.signer || this.provider;
31
+ const network = config.network || "testnet";
32
+ const defaultContracts = DEFAULT_CONTRACTS[network] || {};
33
+ const contracts = {
34
+ serviceRegistry: config.contracts?.serviceRegistry || defaultContracts.serviceRegistry || "",
35
+ paymentManager: config.contracts?.paymentManager || defaultContracts.paymentManager || "",
36
+ subscriptionManager: config.contracts?.subscriptionManager || defaultContracts.subscriptionManager || "",
37
+ escrow: config.contracts?.escrow || defaultContracts.escrow || "",
38
+ agentWalletFactory: config.contracts?.agentWalletFactory || defaultContracts.agentWalletFactory || "",
39
+ };
40
+
41
+ // Validate required contract addresses
42
+ if (!contracts.serviceRegistry) {
43
+ throw new Error("ServiceRegistry address is required");
44
+ }
45
+
46
+ this.services = new ServicesModule(
47
+ contracts.serviceRegistry,
48
+ signerOrProvider
49
+ );
50
+ this.payments = new PaymentsModule(
51
+ contracts.paymentManager,
52
+ signerOrProvider
53
+ );
54
+ this.subscriptions = new SubscriptionsModule(
55
+ contracts.subscriptionManager,
56
+ signerOrProvider
57
+ );
58
+ this.escrow = new EscrowModule(
59
+ contracts.escrow,
60
+ signerOrProvider
61
+ );
62
+ this.wallet = new WalletModule(
63
+ contracts.agentWalletFactory,
64
+ signerOrProvider
65
+ );
66
+ }
67
+
68
+ async discoverServices(filter?: DiscoverFilter): Promise<Service[]> {
69
+ return this.services.discoverServices(filter);
70
+ }
71
+
72
+ async payAndCall(serviceId: bigint, amount: bigint): Promise<string> {
73
+ const result = await this.payments.payPerUse(serviceId, amount);
74
+ return result.txHash;
75
+ }
76
+
77
+ async subscribe(serviceId: bigint, planId: bigint, amount: bigint) {
78
+ return this.subscriptions.subscribe(serviceId, planId, amount);
79
+ }
80
+
81
+ /**
82
+ * x402-aware fetch: automatically handles HTTP 402 Payment Required responses
83
+ */
84
+ async fetch(url: string, options?: X402FetchOptions): Promise<Response> {
85
+ const autoPayment = options?.autoPayment ?? true;
86
+ const maxRetries = options?.maxRetries ?? 1;
87
+
88
+ let response = await globalThis.fetch(url, options);
89
+
90
+ if (response.status === 402 && autoPayment) {
91
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
92
+ const paymentInfo = this._parsePaymentHeaders(response);
93
+ if (!paymentInfo) throw new Error("Invalid 402 response: missing payment headers");
94
+
95
+ const result = await this.payments.payPerUse(
96
+ BigInt(paymentInfo.serviceId),
97
+ BigInt(paymentInfo.amount)
98
+ );
99
+
100
+ const retryHeaders = new Headers(options?.headers);
101
+ retryHeaders.set("X-Payment-TxHash", result.txHash);
102
+ retryHeaders.set("X-Payment-ChainId", paymentInfo.chainId);
103
+
104
+ response = await globalThis.fetch(url, {
105
+ ...options,
106
+ headers: retryHeaders,
107
+ });
108
+
109
+ if (response.status !== 402) break;
110
+ }
111
+ }
112
+
113
+ return response;
114
+ }
115
+
116
+ private _parsePaymentHeaders(response: Response): X402PaymentInfo | null {
117
+ const address = response.headers.get("X-Payment-Address");
118
+ const amount = response.headers.get("X-Payment-Amount");
119
+ const token = response.headers.get("X-Payment-Token") || "native";
120
+ const serviceId = response.headers.get("X-Payment-ServiceId");
121
+ const chainId = response.headers.get("X-Payment-ChainId") || "10143";
122
+
123
+ if (!address || !amount || !serviceId) return null;
124
+
125
+ return { address, amount, token, serviceId, chainId };
126
+ }
127
+
128
+ getProvider(): ethers.Provider {
129
+ return this.provider;
130
+ }
131
+
132
+ getSigner(): ethers.Signer | null {
133
+ return this.signer;
134
+ }
135
+ }
@@ -0,0 +1,360 @@
1
+ [
2
+ {
3
+ "type": "constructor",
4
+ "inputs": [
5
+ {
6
+ "name": "owner_",
7
+ "type": "address",
8
+ "internalType": "address"
9
+ },
10
+ {
11
+ "name": "dailyLimit",
12
+ "type": "uint256",
13
+ "internalType": "uint256"
14
+ }
15
+ ],
16
+ "stateMutability": "nonpayable"
17
+ },
18
+ {
19
+ "type": "receive",
20
+ "stateMutability": "payable"
21
+ },
22
+ {
23
+ "type": "function",
24
+ "name": "authorizeAgent",
25
+ "inputs": [
26
+ {
27
+ "name": "agent",
28
+ "type": "address",
29
+ "internalType": "address"
30
+ }
31
+ ],
32
+ "outputs": [],
33
+ "stateMutability": "nonpayable"
34
+ },
35
+ {
36
+ "type": "function",
37
+ "name": "execute",
38
+ "inputs": [
39
+ {
40
+ "name": "to",
41
+ "type": "address",
42
+ "internalType": "address"
43
+ },
44
+ {
45
+ "name": "value",
46
+ "type": "uint256",
47
+ "internalType": "uint256"
48
+ },
49
+ {
50
+ "name": "data",
51
+ "type": "bytes",
52
+ "internalType": "bytes"
53
+ }
54
+ ],
55
+ "outputs": [
56
+ {
57
+ "name": "",
58
+ "type": "bytes",
59
+ "internalType": "bytes"
60
+ }
61
+ ],
62
+ "stateMutability": "nonpayable"
63
+ },
64
+ {
65
+ "type": "function",
66
+ "name": "getDailySpendingLimit",
67
+ "inputs": [],
68
+ "outputs": [
69
+ {
70
+ "name": "",
71
+ "type": "uint256",
72
+ "internalType": "uint256"
73
+ }
74
+ ],
75
+ "stateMutability": "view"
76
+ },
77
+ {
78
+ "type": "function",
79
+ "name": "getDailySpent",
80
+ "inputs": [],
81
+ "outputs": [
82
+ {
83
+ "name": "",
84
+ "type": "uint256",
85
+ "internalType": "uint256"
86
+ }
87
+ ],
88
+ "stateMutability": "view"
89
+ },
90
+ {
91
+ "type": "function",
92
+ "name": "getOwner",
93
+ "inputs": [],
94
+ "outputs": [
95
+ {
96
+ "name": "",
97
+ "type": "address",
98
+ "internalType": "address"
99
+ }
100
+ ],
101
+ "stateMutability": "view"
102
+ },
103
+ {
104
+ "type": "function",
105
+ "name": "getRemainingDailyAllowance",
106
+ "inputs": [],
107
+ "outputs": [
108
+ {
109
+ "name": "",
110
+ "type": "uint256",
111
+ "internalType": "uint256"
112
+ }
113
+ ],
114
+ "stateMutability": "view"
115
+ },
116
+ {
117
+ "type": "function",
118
+ "name": "isAuthorizedAgent",
119
+ "inputs": [
120
+ {
121
+ "name": "agent",
122
+ "type": "address",
123
+ "internalType": "address"
124
+ }
125
+ ],
126
+ "outputs": [
127
+ {
128
+ "name": "",
129
+ "type": "bool",
130
+ "internalType": "bool"
131
+ }
132
+ ],
133
+ "stateMutability": "view"
134
+ },
135
+ {
136
+ "type": "function",
137
+ "name": "revokeAgent",
138
+ "inputs": [
139
+ {
140
+ "name": "agent",
141
+ "type": "address",
142
+ "internalType": "address"
143
+ }
144
+ ],
145
+ "outputs": [],
146
+ "stateMutability": "nonpayable"
147
+ },
148
+ {
149
+ "type": "function",
150
+ "name": "setDailySpendingLimit",
151
+ "inputs": [
152
+ {
153
+ "name": "limit",
154
+ "type": "uint256",
155
+ "internalType": "uint256"
156
+ }
157
+ ],
158
+ "outputs": [],
159
+ "stateMutability": "nonpayable"
160
+ },
161
+ {
162
+ "type": "function",
163
+ "name": "withdrawFunds",
164
+ "inputs": [
165
+ {
166
+ "name": "amount",
167
+ "type": "uint256",
168
+ "internalType": "uint256"
169
+ }
170
+ ],
171
+ "outputs": [],
172
+ "stateMutability": "nonpayable"
173
+ },
174
+ {
175
+ "type": "event",
176
+ "name": "AgentAuthorized",
177
+ "inputs": [
178
+ {
179
+ "name": "agent",
180
+ "type": "address",
181
+ "indexed": true,
182
+ "internalType": "address"
183
+ }
184
+ ],
185
+ "anonymous": false
186
+ },
187
+ {
188
+ "type": "event",
189
+ "name": "AgentRevoked",
190
+ "inputs": [
191
+ {
192
+ "name": "agent",
193
+ "type": "address",
194
+ "indexed": true,
195
+ "internalType": "address"
196
+ }
197
+ ],
198
+ "anonymous": false
199
+ },
200
+ {
201
+ "type": "event",
202
+ "name": "Deposited",
203
+ "inputs": [
204
+ {
205
+ "name": "from",
206
+ "type": "address",
207
+ "indexed": true,
208
+ "internalType": "address"
209
+ },
210
+ {
211
+ "name": "amount",
212
+ "type": "uint256",
213
+ "indexed": false,
214
+ "internalType": "uint256"
215
+ }
216
+ ],
217
+ "anonymous": false
218
+ },
219
+ {
220
+ "type": "event",
221
+ "name": "Executed",
222
+ "inputs": [
223
+ {
224
+ "name": "to",
225
+ "type": "address",
226
+ "indexed": true,
227
+ "internalType": "address"
228
+ },
229
+ {
230
+ "name": "value",
231
+ "type": "uint256",
232
+ "indexed": false,
233
+ "internalType": "uint256"
234
+ },
235
+ {
236
+ "name": "data",
237
+ "type": "bytes",
238
+ "indexed": false,
239
+ "internalType": "bytes"
240
+ }
241
+ ],
242
+ "anonymous": false
243
+ },
244
+ {
245
+ "type": "event",
246
+ "name": "SpendingLimitSet",
247
+ "inputs": [
248
+ {
249
+ "name": "dailyLimit",
250
+ "type": "uint256",
251
+ "indexed": false,
252
+ "internalType": "uint256"
253
+ }
254
+ ],
255
+ "anonymous": false
256
+ },
257
+ {
258
+ "type": "event",
259
+ "name": "Withdrawn",
260
+ "inputs": [
261
+ {
262
+ "name": "to",
263
+ "type": "address",
264
+ "indexed": true,
265
+ "internalType": "address"
266
+ },
267
+ {
268
+ "name": "amount",
269
+ "type": "uint256",
270
+ "indexed": false,
271
+ "internalType": "uint256"
272
+ }
273
+ ],
274
+ "anonymous": false
275
+ },
276
+ {
277
+ "type": "error",
278
+ "name": "ExceedsDailyLimit",
279
+ "inputs": [
280
+ {
281
+ "name": "requested",
282
+ "type": "uint256",
283
+ "internalType": "uint256"
284
+ },
285
+ {
286
+ "name": "remaining",
287
+ "type": "uint256",
288
+ "internalType": "uint256"
289
+ }
290
+ ]
291
+ },
292
+ {
293
+ "type": "error",
294
+ "name": "ExecutionFailed",
295
+ "inputs": [
296
+ {
297
+ "name": "to",
298
+ "type": "address",
299
+ "internalType": "address"
300
+ },
301
+ {
302
+ "name": "data",
303
+ "type": "bytes",
304
+ "internalType": "bytes"
305
+ }
306
+ ]
307
+ },
308
+ {
309
+ "type": "error",
310
+ "name": "InsufficientFunds",
311
+ "inputs": [
312
+ {
313
+ "name": "required",
314
+ "type": "uint256",
315
+ "internalType": "uint256"
316
+ },
317
+ {
318
+ "name": "available",
319
+ "type": "uint256",
320
+ "internalType": "uint256"
321
+ }
322
+ ]
323
+ },
324
+ {
325
+ "type": "error",
326
+ "name": "NotAuthorized",
327
+ "inputs": []
328
+ },
329
+ {
330
+ "type": "error",
331
+ "name": "NotOwner",
332
+ "inputs": []
333
+ },
334
+ {
335
+ "type": "error",
336
+ "name": "ReentrancyGuardReentrantCall",
337
+ "inputs": []
338
+ },
339
+ {
340
+ "type": "error",
341
+ "name": "TransferFailed",
342
+ "inputs": [
343
+ {
344
+ "name": "to",
345
+ "type": "address",
346
+ "internalType": "address"
347
+ },
348
+ {
349
+ "name": "amount",
350
+ "type": "uint256",
351
+ "internalType": "uint256"
352
+ }
353
+ ]
354
+ },
355
+ {
356
+ "type": "error",
357
+ "name": "ZeroAmount",
358
+ "inputs": []
359
+ }
360
+ ]
@@ -0,0 +1,84 @@
1
+ [
2
+ {
3
+ "type": "function",
4
+ "name": "createWallet",
5
+ "inputs": [
6
+ {
7
+ "name": "dailyLimit",
8
+ "type": "uint256",
9
+ "internalType": "uint256"
10
+ }
11
+ ],
12
+ "outputs": [
13
+ {
14
+ "name": "wallet",
15
+ "type": "address",
16
+ "internalType": "address"
17
+ }
18
+ ],
19
+ "stateMutability": "nonpayable"
20
+ },
21
+ {
22
+ "type": "function",
23
+ "name": "getWalletCount",
24
+ "inputs": [
25
+ {
26
+ "name": "owner",
27
+ "type": "address",
28
+ "internalType": "address"
29
+ }
30
+ ],
31
+ "outputs": [
32
+ {
33
+ "name": "",
34
+ "type": "uint256",
35
+ "internalType": "uint256"
36
+ }
37
+ ],
38
+ "stateMutability": "view"
39
+ },
40
+ {
41
+ "type": "function",
42
+ "name": "getWallets",
43
+ "inputs": [
44
+ {
45
+ "name": "owner",
46
+ "type": "address",
47
+ "internalType": "address"
48
+ }
49
+ ],
50
+ "outputs": [
51
+ {
52
+ "name": "",
53
+ "type": "address[]",
54
+ "internalType": "address[]"
55
+ }
56
+ ],
57
+ "stateMutability": "view"
58
+ },
59
+ {
60
+ "type": "event",
61
+ "name": "WalletCreated",
62
+ "inputs": [
63
+ {
64
+ "name": "owner",
65
+ "type": "address",
66
+ "indexed": true,
67
+ "internalType": "address"
68
+ },
69
+ {
70
+ "name": "wallet",
71
+ "type": "address",
72
+ "indexed": true,
73
+ "internalType": "address"
74
+ },
75
+ {
76
+ "name": "dailyLimit",
77
+ "type": "uint256",
78
+ "indexed": false,
79
+ "internalType": "uint256"
80
+ }
81
+ ],
82
+ "anonymous": false
83
+ }
84
+ ]