@vera-pay/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.
- package/README.md +388 -0
- package/dist/index.d.mts +772 -0
- package/dist/index.d.ts +772 -0
- package/dist/index.js +1409 -0
- package/dist/index.mjs +1359 -0
- package/package.json +50 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
|
|
3
|
+
interface Plan {
|
|
4
|
+
planId: bigint;
|
|
5
|
+
merchant: string;
|
|
6
|
+
paymentToken: string;
|
|
7
|
+
amount: bigint;
|
|
8
|
+
interval: bigint;
|
|
9
|
+
name: string;
|
|
10
|
+
metadataURI: string;
|
|
11
|
+
active: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface Subscription {
|
|
14
|
+
subscriptionId: bigint;
|
|
15
|
+
planId: bigint;
|
|
16
|
+
subscriber: string;
|
|
17
|
+
startTime: bigint;
|
|
18
|
+
lastPaymentTime: bigint;
|
|
19
|
+
paymentsCount: bigint;
|
|
20
|
+
active: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface PaymentReceipt {
|
|
23
|
+
subscriptionId: string;
|
|
24
|
+
planId: string;
|
|
25
|
+
subscriber: string;
|
|
26
|
+
merchant: string;
|
|
27
|
+
amount: string;
|
|
28
|
+
protocolFee: string;
|
|
29
|
+
timestamp: number;
|
|
30
|
+
txHash: string;
|
|
31
|
+
blockNumber: number;
|
|
32
|
+
chainId: number;
|
|
33
|
+
ipfsCid?: string;
|
|
34
|
+
}
|
|
35
|
+
interface CreatePlanParams {
|
|
36
|
+
paymentToken: string;
|
|
37
|
+
amount: bigint;
|
|
38
|
+
interval: bigint;
|
|
39
|
+
name: string;
|
|
40
|
+
metadataURI?: string;
|
|
41
|
+
}
|
|
42
|
+
interface VeraPayConfig {
|
|
43
|
+
contractAddress: string;
|
|
44
|
+
rpcUrl?: string;
|
|
45
|
+
chainId?: number;
|
|
46
|
+
ipfsGateway?: string;
|
|
47
|
+
}
|
|
48
|
+
type NetworkName = "flow-testnet" | "flow-mainnet";
|
|
49
|
+
interface NetworkConfig {
|
|
50
|
+
chainId: number;
|
|
51
|
+
rpcUrl: string;
|
|
52
|
+
blockExplorer: string;
|
|
53
|
+
evmBlockExplorer: string;
|
|
54
|
+
name: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface IPFSAdapter {
|
|
58
|
+
uploadJson(data: unknown): Promise<string>;
|
|
59
|
+
fetchJson<T = unknown>(cid: string): Promise<T>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Minimal adapter that talks to a Kubo-compatible IPFS HTTP API
|
|
63
|
+
* (e.g. `http://localhost:5001`).
|
|
64
|
+
*/
|
|
65
|
+
declare function createKuboAdapter(apiUrl: string): IPFSAdapter;
|
|
66
|
+
interface StorachaConfig {
|
|
67
|
+
key: string;
|
|
68
|
+
proof: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates an IPFS adapter backed by Storacha (formerly web3.storage).
|
|
72
|
+
*
|
|
73
|
+
* Accepts either a pre-built client (anything with `uploadFile`) or a
|
|
74
|
+
* `{ key, proof }` config. When config is provided, the Storacha client
|
|
75
|
+
* is lazily initialized on first upload using a memory store + Ed25519
|
|
76
|
+
* principal — no interactive auth or email prompts.
|
|
77
|
+
*
|
|
78
|
+
* Setup (one-time, via Storacha CLI):
|
|
79
|
+
* ```bash
|
|
80
|
+
* storacha key create --json # → { "did": "...", "key": "MgCa..." }
|
|
81
|
+
* storacha delegation create <did> \
|
|
82
|
+
* -c space/blob/add -c space/index/add \
|
|
83
|
+
* -c filecoin/offer -c upload/add --base64 # → mAYIEAP8...
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* Usage:
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { createStorachaAdapter } from "@verapay/sdk";
|
|
89
|
+
*
|
|
90
|
+
* const ipfs = createStorachaAdapter({
|
|
91
|
+
* key: process.env.STORACHA_KEY!,
|
|
92
|
+
* proof: process.env.STORACHA_PROOF!,
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
declare function createStorachaAdapter(clientOrConfig: {
|
|
97
|
+
uploadFile: (file: Blob) => Promise<{
|
|
98
|
+
toString(): string;
|
|
99
|
+
}>;
|
|
100
|
+
} | StorachaConfig): IPFSAdapter;
|
|
101
|
+
/**
|
|
102
|
+
* In-memory adapter useful for tests and demos where no real IPFS node is available.
|
|
103
|
+
*/
|
|
104
|
+
declare function createMemoryAdapter(): IPFSAdapter;
|
|
105
|
+
declare function buildPaymentReceipt(event: {
|
|
106
|
+
subscriptionId: bigint;
|
|
107
|
+
planId: bigint;
|
|
108
|
+
subscriber: string;
|
|
109
|
+
merchant: string;
|
|
110
|
+
amount: bigint;
|
|
111
|
+
protocolFee: bigint;
|
|
112
|
+
timestamp: bigint;
|
|
113
|
+
}, txHash: string, blockNumber: number, chainId: number): PaymentReceipt;
|
|
114
|
+
declare function ipfsGatewayUrl(cid: string): string;
|
|
115
|
+
|
|
116
|
+
declare class VeraPayClient {
|
|
117
|
+
readonly contract: ethers.Contract;
|
|
118
|
+
readonly provider: ethers.Provider;
|
|
119
|
+
readonly config: VeraPayConfig;
|
|
120
|
+
private signer?;
|
|
121
|
+
private ipfs?;
|
|
122
|
+
constructor(config: VeraPayConfig, signerOrProvider: ethers.Signer | ethers.Provider, ipfsAdapter?: IPFSAdapter);
|
|
123
|
+
/**
|
|
124
|
+
* Convenience factory that auto-configures for a known Flow network.
|
|
125
|
+
*/
|
|
126
|
+
static fromNetwork(network: NetworkName, contractAddress: string, signerOrProvider: ethers.Signer | ethers.Provider, ipfsAdapter?: IPFSAdapter): VeraPayClient;
|
|
127
|
+
createPlan(params: CreatePlanParams): Promise<{
|
|
128
|
+
planId: bigint;
|
|
129
|
+
tx: ethers.TransactionResponse;
|
|
130
|
+
}>;
|
|
131
|
+
togglePlan(planId: bigint): Promise<ethers.TransactionResponse>;
|
|
132
|
+
getMerchantPlans(merchant: string): Promise<bigint[]>;
|
|
133
|
+
/**
|
|
134
|
+
* Approve the VeraPay contract to pull `amount` of the given ERC-20 token,
|
|
135
|
+
* then subscribe to the plan. Returns subscription ID and the payment receipt
|
|
136
|
+
* (optionally pinned to IPFS).
|
|
137
|
+
*/
|
|
138
|
+
subscribeWithApproval(planId: bigint, options?: {
|
|
139
|
+
approveMax?: boolean;
|
|
140
|
+
}): Promise<{
|
|
141
|
+
subscriptionId: bigint;
|
|
142
|
+
receipt: PaymentReceipt;
|
|
143
|
+
tx: ethers.TransactionResponse;
|
|
144
|
+
}>;
|
|
145
|
+
subscribe(planId: bigint): Promise<{
|
|
146
|
+
subscriptionId: bigint;
|
|
147
|
+
receipt: PaymentReceipt;
|
|
148
|
+
tx: ethers.TransactionResponse;
|
|
149
|
+
}>;
|
|
150
|
+
cancelSubscription(subscriptionId: bigint): Promise<ethers.TransactionResponse>;
|
|
151
|
+
getSubscriberSubscriptions(subscriber: string): Promise<bigint[]>;
|
|
152
|
+
processPayment(subscriptionId: bigint): Promise<{
|
|
153
|
+
receipt: PaymentReceipt;
|
|
154
|
+
tx: ethers.TransactionResponse;
|
|
155
|
+
}>;
|
|
156
|
+
batchProcessPayments(subscriptionIds: bigint[]): Promise<ethers.TransactionResponse>;
|
|
157
|
+
isPaymentDue(subscriptionId: bigint): Promise<boolean>;
|
|
158
|
+
getDuePayments(subscriptionIds: bigint[]): Promise<bigint[]>;
|
|
159
|
+
/**
|
|
160
|
+
* Start a polling loop that checks for due payments and processes them.
|
|
161
|
+
* Returns a cleanup function to stop the loop.
|
|
162
|
+
*/
|
|
163
|
+
startKeeper(subscriptionIds: bigint[], intervalMs?: number, onPayment?: (receipt: PaymentReceipt) => void, onError?: (err: unknown) => void): () => void;
|
|
164
|
+
getNextPlanId(): Promise<bigint>;
|
|
165
|
+
getNextSubscriptionId(): Promise<bigint>;
|
|
166
|
+
listActivePlans(): Promise<Plan[]>;
|
|
167
|
+
getPlan(planId: bigint): Promise<Plan>;
|
|
168
|
+
getSubscription(subscriptionId: bigint): Promise<Subscription>;
|
|
169
|
+
getProtocolFeeBps(): Promise<bigint>;
|
|
170
|
+
get hasIPFS(): boolean;
|
|
171
|
+
get contractAddress(): string;
|
|
172
|
+
/**
|
|
173
|
+
* Pin a payment receipt to IPFS and return the CID.
|
|
174
|
+
*/
|
|
175
|
+
pinReceipt(receipt: PaymentReceipt): Promise<string>;
|
|
176
|
+
fetchReceipt(cid: string): Promise<PaymentReceipt>;
|
|
177
|
+
setIPFSAdapter(adapter: IPFSAdapter): void;
|
|
178
|
+
onPaymentProcessed(callback: (receipt: PaymentReceipt) => void, filter?: {
|
|
179
|
+
subscriptionId?: bigint;
|
|
180
|
+
planId?: bigint;
|
|
181
|
+
subscriber?: string;
|
|
182
|
+
}): ethers.Contract;
|
|
183
|
+
removeAllListeners(): void;
|
|
184
|
+
private requireSigner;
|
|
185
|
+
private extractPaymentReceipt;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
declare const VERA_PAY_ABI: readonly [{
|
|
189
|
+
readonly type: "constructor";
|
|
190
|
+
readonly inputs: readonly [{
|
|
191
|
+
readonly name: "_feeRecipient";
|
|
192
|
+
readonly type: "address";
|
|
193
|
+
}, {
|
|
194
|
+
readonly name: "_feeBps";
|
|
195
|
+
readonly type: "uint256";
|
|
196
|
+
}];
|
|
197
|
+
readonly stateMutability: "nonpayable";
|
|
198
|
+
}, {
|
|
199
|
+
readonly type: "function";
|
|
200
|
+
readonly name: "createPlan";
|
|
201
|
+
readonly inputs: readonly [{
|
|
202
|
+
readonly name: "_paymentToken";
|
|
203
|
+
readonly type: "address";
|
|
204
|
+
}, {
|
|
205
|
+
readonly name: "_amount";
|
|
206
|
+
readonly type: "uint256";
|
|
207
|
+
}, {
|
|
208
|
+
readonly name: "_interval";
|
|
209
|
+
readonly type: "uint256";
|
|
210
|
+
}, {
|
|
211
|
+
readonly name: "_name";
|
|
212
|
+
readonly type: "string";
|
|
213
|
+
}, {
|
|
214
|
+
readonly name: "_metadataURI";
|
|
215
|
+
readonly type: "string";
|
|
216
|
+
}];
|
|
217
|
+
readonly outputs: readonly [{
|
|
218
|
+
readonly name: "planId";
|
|
219
|
+
readonly type: "uint256";
|
|
220
|
+
}];
|
|
221
|
+
readonly stateMutability: "nonpayable";
|
|
222
|
+
}, {
|
|
223
|
+
readonly type: "function";
|
|
224
|
+
readonly name: "togglePlan";
|
|
225
|
+
readonly inputs: readonly [{
|
|
226
|
+
readonly name: "_planId";
|
|
227
|
+
readonly type: "uint256";
|
|
228
|
+
}];
|
|
229
|
+
readonly outputs: readonly [];
|
|
230
|
+
readonly stateMutability: "nonpayable";
|
|
231
|
+
}, {
|
|
232
|
+
readonly type: "function";
|
|
233
|
+
readonly name: "subscribe";
|
|
234
|
+
readonly inputs: readonly [{
|
|
235
|
+
readonly name: "_planId";
|
|
236
|
+
readonly type: "uint256";
|
|
237
|
+
}];
|
|
238
|
+
readonly outputs: readonly [{
|
|
239
|
+
readonly name: "subId";
|
|
240
|
+
readonly type: "uint256";
|
|
241
|
+
}];
|
|
242
|
+
readonly stateMutability: "nonpayable";
|
|
243
|
+
}, {
|
|
244
|
+
readonly type: "function";
|
|
245
|
+
readonly name: "cancelSubscription";
|
|
246
|
+
readonly inputs: readonly [{
|
|
247
|
+
readonly name: "_subId";
|
|
248
|
+
readonly type: "uint256";
|
|
249
|
+
}];
|
|
250
|
+
readonly outputs: readonly [];
|
|
251
|
+
readonly stateMutability: "nonpayable";
|
|
252
|
+
}, {
|
|
253
|
+
readonly type: "function";
|
|
254
|
+
readonly name: "processPayment";
|
|
255
|
+
readonly inputs: readonly [{
|
|
256
|
+
readonly name: "_subId";
|
|
257
|
+
readonly type: "uint256";
|
|
258
|
+
}];
|
|
259
|
+
readonly outputs: readonly [];
|
|
260
|
+
readonly stateMutability: "nonpayable";
|
|
261
|
+
}, {
|
|
262
|
+
readonly type: "function";
|
|
263
|
+
readonly name: "batchProcessPayments";
|
|
264
|
+
readonly inputs: readonly [{
|
|
265
|
+
readonly name: "_subIds";
|
|
266
|
+
readonly type: "uint256[]";
|
|
267
|
+
}];
|
|
268
|
+
readonly outputs: readonly [];
|
|
269
|
+
readonly stateMutability: "nonpayable";
|
|
270
|
+
}, {
|
|
271
|
+
readonly type: "function";
|
|
272
|
+
readonly name: "getPlan";
|
|
273
|
+
readonly inputs: readonly [{
|
|
274
|
+
readonly name: "_planId";
|
|
275
|
+
readonly type: "uint256";
|
|
276
|
+
}];
|
|
277
|
+
readonly outputs: readonly [{
|
|
278
|
+
readonly name: "";
|
|
279
|
+
readonly type: "tuple";
|
|
280
|
+
readonly components: readonly [{
|
|
281
|
+
readonly name: "merchant";
|
|
282
|
+
readonly type: "address";
|
|
283
|
+
}, {
|
|
284
|
+
readonly name: "paymentToken";
|
|
285
|
+
readonly type: "address";
|
|
286
|
+
}, {
|
|
287
|
+
readonly name: "amount";
|
|
288
|
+
readonly type: "uint256";
|
|
289
|
+
}, {
|
|
290
|
+
readonly name: "interval";
|
|
291
|
+
readonly type: "uint256";
|
|
292
|
+
}, {
|
|
293
|
+
readonly name: "name";
|
|
294
|
+
readonly type: "string";
|
|
295
|
+
}, {
|
|
296
|
+
readonly name: "metadataURI";
|
|
297
|
+
readonly type: "string";
|
|
298
|
+
}, {
|
|
299
|
+
readonly name: "active";
|
|
300
|
+
readonly type: "bool";
|
|
301
|
+
}];
|
|
302
|
+
}];
|
|
303
|
+
readonly stateMutability: "view";
|
|
304
|
+
}, {
|
|
305
|
+
readonly type: "function";
|
|
306
|
+
readonly name: "getSubscription";
|
|
307
|
+
readonly inputs: readonly [{
|
|
308
|
+
readonly name: "_subId";
|
|
309
|
+
readonly type: "uint256";
|
|
310
|
+
}];
|
|
311
|
+
readonly outputs: readonly [{
|
|
312
|
+
readonly name: "";
|
|
313
|
+
readonly type: "tuple";
|
|
314
|
+
readonly components: readonly [{
|
|
315
|
+
readonly name: "planId";
|
|
316
|
+
readonly type: "uint256";
|
|
317
|
+
}, {
|
|
318
|
+
readonly name: "subscriber";
|
|
319
|
+
readonly type: "address";
|
|
320
|
+
}, {
|
|
321
|
+
readonly name: "startTime";
|
|
322
|
+
readonly type: "uint256";
|
|
323
|
+
}, {
|
|
324
|
+
readonly name: "lastPaymentTime";
|
|
325
|
+
readonly type: "uint256";
|
|
326
|
+
}, {
|
|
327
|
+
readonly name: "paymentsCount";
|
|
328
|
+
readonly type: "uint256";
|
|
329
|
+
}, {
|
|
330
|
+
readonly name: "active";
|
|
331
|
+
readonly type: "bool";
|
|
332
|
+
}];
|
|
333
|
+
}];
|
|
334
|
+
readonly stateMutability: "view";
|
|
335
|
+
}, {
|
|
336
|
+
readonly type: "function";
|
|
337
|
+
readonly name: "getMerchantPlans";
|
|
338
|
+
readonly inputs: readonly [{
|
|
339
|
+
readonly name: "_merchant";
|
|
340
|
+
readonly type: "address";
|
|
341
|
+
}];
|
|
342
|
+
readonly outputs: readonly [{
|
|
343
|
+
readonly name: "";
|
|
344
|
+
readonly type: "uint256[]";
|
|
345
|
+
}];
|
|
346
|
+
readonly stateMutability: "view";
|
|
347
|
+
}, {
|
|
348
|
+
readonly type: "function";
|
|
349
|
+
readonly name: "getSubscriberSubscriptions";
|
|
350
|
+
readonly inputs: readonly [{
|
|
351
|
+
readonly name: "_subscriber";
|
|
352
|
+
readonly type: "address";
|
|
353
|
+
}];
|
|
354
|
+
readonly outputs: readonly [{
|
|
355
|
+
readonly name: "";
|
|
356
|
+
readonly type: "uint256[]";
|
|
357
|
+
}];
|
|
358
|
+
readonly stateMutability: "view";
|
|
359
|
+
}, {
|
|
360
|
+
readonly type: "function";
|
|
361
|
+
readonly name: "isPaymentDue";
|
|
362
|
+
readonly inputs: readonly [{
|
|
363
|
+
readonly name: "_subId";
|
|
364
|
+
readonly type: "uint256";
|
|
365
|
+
}];
|
|
366
|
+
readonly outputs: readonly [{
|
|
367
|
+
readonly name: "";
|
|
368
|
+
readonly type: "bool";
|
|
369
|
+
}];
|
|
370
|
+
readonly stateMutability: "view";
|
|
371
|
+
}, {
|
|
372
|
+
readonly type: "function";
|
|
373
|
+
readonly name: "getDuePayments";
|
|
374
|
+
readonly inputs: readonly [{
|
|
375
|
+
readonly name: "_subIds";
|
|
376
|
+
readonly type: "uint256[]";
|
|
377
|
+
}];
|
|
378
|
+
readonly outputs: readonly [{
|
|
379
|
+
readonly name: "due";
|
|
380
|
+
readonly type: "uint256[]";
|
|
381
|
+
}];
|
|
382
|
+
readonly stateMutability: "view";
|
|
383
|
+
}, {
|
|
384
|
+
readonly type: "function";
|
|
385
|
+
readonly name: "nextPlanId";
|
|
386
|
+
readonly inputs: readonly [];
|
|
387
|
+
readonly outputs: readonly [{
|
|
388
|
+
readonly name: "";
|
|
389
|
+
readonly type: "uint256";
|
|
390
|
+
}];
|
|
391
|
+
readonly stateMutability: "view";
|
|
392
|
+
}, {
|
|
393
|
+
readonly type: "function";
|
|
394
|
+
readonly name: "nextSubscriptionId";
|
|
395
|
+
readonly inputs: readonly [];
|
|
396
|
+
readonly outputs: readonly [{
|
|
397
|
+
readonly name: "";
|
|
398
|
+
readonly type: "uint256";
|
|
399
|
+
}];
|
|
400
|
+
readonly stateMutability: "view";
|
|
401
|
+
}, {
|
|
402
|
+
readonly type: "function";
|
|
403
|
+
readonly name: "protocolFeeBps";
|
|
404
|
+
readonly inputs: readonly [];
|
|
405
|
+
readonly outputs: readonly [{
|
|
406
|
+
readonly name: "";
|
|
407
|
+
readonly type: "uint256";
|
|
408
|
+
}];
|
|
409
|
+
readonly stateMutability: "view";
|
|
410
|
+
}, {
|
|
411
|
+
readonly type: "function";
|
|
412
|
+
readonly name: "activeSub";
|
|
413
|
+
readonly inputs: readonly [{
|
|
414
|
+
readonly name: "planId";
|
|
415
|
+
readonly type: "uint256";
|
|
416
|
+
}, {
|
|
417
|
+
readonly name: "subscriber";
|
|
418
|
+
readonly type: "address";
|
|
419
|
+
}];
|
|
420
|
+
readonly outputs: readonly [{
|
|
421
|
+
readonly name: "subId";
|
|
422
|
+
readonly type: "uint256";
|
|
423
|
+
}];
|
|
424
|
+
readonly stateMutability: "view";
|
|
425
|
+
}, {
|
|
426
|
+
readonly type: "event";
|
|
427
|
+
readonly name: "PlanCreated";
|
|
428
|
+
readonly inputs: readonly [{
|
|
429
|
+
readonly name: "planId";
|
|
430
|
+
readonly type: "uint256";
|
|
431
|
+
readonly indexed: true;
|
|
432
|
+
}, {
|
|
433
|
+
readonly name: "merchant";
|
|
434
|
+
readonly type: "address";
|
|
435
|
+
readonly indexed: true;
|
|
436
|
+
}, {
|
|
437
|
+
readonly name: "paymentToken";
|
|
438
|
+
readonly type: "address";
|
|
439
|
+
readonly indexed: false;
|
|
440
|
+
}, {
|
|
441
|
+
readonly name: "amount";
|
|
442
|
+
readonly type: "uint256";
|
|
443
|
+
readonly indexed: false;
|
|
444
|
+
}, {
|
|
445
|
+
readonly name: "interval";
|
|
446
|
+
readonly type: "uint256";
|
|
447
|
+
readonly indexed: false;
|
|
448
|
+
}, {
|
|
449
|
+
readonly name: "name";
|
|
450
|
+
readonly type: "string";
|
|
451
|
+
readonly indexed: false;
|
|
452
|
+
}, {
|
|
453
|
+
readonly name: "metadataURI";
|
|
454
|
+
readonly type: "string";
|
|
455
|
+
readonly indexed: false;
|
|
456
|
+
}];
|
|
457
|
+
readonly anonymous: false;
|
|
458
|
+
}, {
|
|
459
|
+
readonly type: "event";
|
|
460
|
+
readonly name: "PlanToggled";
|
|
461
|
+
readonly inputs: readonly [{
|
|
462
|
+
readonly name: "planId";
|
|
463
|
+
readonly type: "uint256";
|
|
464
|
+
readonly indexed: true;
|
|
465
|
+
}, {
|
|
466
|
+
readonly name: "active";
|
|
467
|
+
readonly type: "bool";
|
|
468
|
+
readonly indexed: false;
|
|
469
|
+
}];
|
|
470
|
+
readonly anonymous: false;
|
|
471
|
+
}, {
|
|
472
|
+
readonly type: "event";
|
|
473
|
+
readonly name: "Subscribed";
|
|
474
|
+
readonly inputs: readonly [{
|
|
475
|
+
readonly name: "subscriptionId";
|
|
476
|
+
readonly type: "uint256";
|
|
477
|
+
readonly indexed: true;
|
|
478
|
+
}, {
|
|
479
|
+
readonly name: "planId";
|
|
480
|
+
readonly type: "uint256";
|
|
481
|
+
readonly indexed: true;
|
|
482
|
+
}, {
|
|
483
|
+
readonly name: "subscriber";
|
|
484
|
+
readonly type: "address";
|
|
485
|
+
readonly indexed: true;
|
|
486
|
+
}];
|
|
487
|
+
readonly anonymous: false;
|
|
488
|
+
}, {
|
|
489
|
+
readonly type: "event";
|
|
490
|
+
readonly name: "PaymentProcessed";
|
|
491
|
+
readonly inputs: readonly [{
|
|
492
|
+
readonly name: "subscriptionId";
|
|
493
|
+
readonly type: "uint256";
|
|
494
|
+
readonly indexed: true;
|
|
495
|
+
}, {
|
|
496
|
+
readonly name: "planId";
|
|
497
|
+
readonly type: "uint256";
|
|
498
|
+
readonly indexed: true;
|
|
499
|
+
}, {
|
|
500
|
+
readonly name: "subscriber";
|
|
501
|
+
readonly type: "address";
|
|
502
|
+
readonly indexed: true;
|
|
503
|
+
}, {
|
|
504
|
+
readonly name: "merchant";
|
|
505
|
+
readonly type: "address";
|
|
506
|
+
readonly indexed: false;
|
|
507
|
+
}, {
|
|
508
|
+
readonly name: "amount";
|
|
509
|
+
readonly type: "uint256";
|
|
510
|
+
readonly indexed: false;
|
|
511
|
+
}, {
|
|
512
|
+
readonly name: "protocolFee";
|
|
513
|
+
readonly type: "uint256";
|
|
514
|
+
readonly indexed: false;
|
|
515
|
+
}, {
|
|
516
|
+
readonly name: "timestamp";
|
|
517
|
+
readonly type: "uint256";
|
|
518
|
+
readonly indexed: false;
|
|
519
|
+
}];
|
|
520
|
+
readonly anonymous: false;
|
|
521
|
+
}, {
|
|
522
|
+
readonly type: "event";
|
|
523
|
+
readonly name: "SubscriptionCancelled";
|
|
524
|
+
readonly inputs: readonly [{
|
|
525
|
+
readonly name: "subscriptionId";
|
|
526
|
+
readonly type: "uint256";
|
|
527
|
+
readonly indexed: true;
|
|
528
|
+
}, {
|
|
529
|
+
readonly name: "subscriber";
|
|
530
|
+
readonly type: "address";
|
|
531
|
+
readonly indexed: true;
|
|
532
|
+
}];
|
|
533
|
+
readonly anonymous: false;
|
|
534
|
+
}];
|
|
535
|
+
declare const ERC20_ABI: readonly [{
|
|
536
|
+
readonly type: "function";
|
|
537
|
+
readonly name: "approve";
|
|
538
|
+
readonly inputs: readonly [{
|
|
539
|
+
readonly name: "spender";
|
|
540
|
+
readonly type: "address";
|
|
541
|
+
}, {
|
|
542
|
+
readonly name: "amount";
|
|
543
|
+
readonly type: "uint256";
|
|
544
|
+
}];
|
|
545
|
+
readonly outputs: readonly [{
|
|
546
|
+
readonly name: "";
|
|
547
|
+
readonly type: "bool";
|
|
548
|
+
}];
|
|
549
|
+
readonly stateMutability: "nonpayable";
|
|
550
|
+
}, {
|
|
551
|
+
readonly type: "function";
|
|
552
|
+
readonly name: "allowance";
|
|
553
|
+
readonly inputs: readonly [{
|
|
554
|
+
readonly name: "owner";
|
|
555
|
+
readonly type: "address";
|
|
556
|
+
}, {
|
|
557
|
+
readonly name: "spender";
|
|
558
|
+
readonly type: "address";
|
|
559
|
+
}];
|
|
560
|
+
readonly outputs: readonly [{
|
|
561
|
+
readonly name: "";
|
|
562
|
+
readonly type: "uint256";
|
|
563
|
+
}];
|
|
564
|
+
readonly stateMutability: "view";
|
|
565
|
+
}, {
|
|
566
|
+
readonly type: "function";
|
|
567
|
+
readonly name: "balanceOf";
|
|
568
|
+
readonly inputs: readonly [{
|
|
569
|
+
readonly name: "account";
|
|
570
|
+
readonly type: "address";
|
|
571
|
+
}];
|
|
572
|
+
readonly outputs: readonly [{
|
|
573
|
+
readonly name: "";
|
|
574
|
+
readonly type: "uint256";
|
|
575
|
+
}];
|
|
576
|
+
readonly stateMutability: "view";
|
|
577
|
+
}, {
|
|
578
|
+
readonly type: "function";
|
|
579
|
+
readonly name: "decimals";
|
|
580
|
+
readonly inputs: readonly [];
|
|
581
|
+
readonly outputs: readonly [{
|
|
582
|
+
readonly name: "";
|
|
583
|
+
readonly type: "uint8";
|
|
584
|
+
}];
|
|
585
|
+
readonly stateMutability: "view";
|
|
586
|
+
}, {
|
|
587
|
+
readonly type: "function";
|
|
588
|
+
readonly name: "symbol";
|
|
589
|
+
readonly inputs: readonly [];
|
|
590
|
+
readonly outputs: readonly [{
|
|
591
|
+
readonly name: "";
|
|
592
|
+
readonly type: "string";
|
|
593
|
+
}];
|
|
594
|
+
readonly stateMutability: "view";
|
|
595
|
+
}, {
|
|
596
|
+
readonly type: "function";
|
|
597
|
+
readonly name: "mint";
|
|
598
|
+
readonly inputs: readonly [{
|
|
599
|
+
readonly name: "to";
|
|
600
|
+
readonly type: "address";
|
|
601
|
+
}, {
|
|
602
|
+
readonly name: "amount";
|
|
603
|
+
readonly type: "uint256";
|
|
604
|
+
}];
|
|
605
|
+
readonly outputs: readonly [];
|
|
606
|
+
readonly stateMutability: "nonpayable";
|
|
607
|
+
}];
|
|
608
|
+
|
|
609
|
+
declare const NETWORKS: Record<NetworkName, NetworkConfig>;
|
|
610
|
+
declare const DEPLOYED_CONTRACTS: Record<string, string>;
|
|
611
|
+
declare const KNOWN_TOKENS: Record<string, Record<string, string>>;
|
|
612
|
+
declare const CADENCE_HANDLERS: Record<string, string>;
|
|
613
|
+
declare const DEFAULT_IPFS_GATEWAY = "https://storacha.link/ipfs";
|
|
614
|
+
|
|
615
|
+
interface SchedulerConfig {
|
|
616
|
+
network: "testnet" | "mainnet";
|
|
617
|
+
/** Cadence address where VeraPayScheduledPaymentHandler is deployed (without 0x prefix) */
|
|
618
|
+
handlerAddress: string;
|
|
619
|
+
/** The VeraPay EVM contract address (with 0x prefix) for handler initialization */
|
|
620
|
+
evmContractAddress: string;
|
|
621
|
+
/** Optional IPFS adapter for pinning payment receipts */
|
|
622
|
+
ipfsAdapter?: IPFSAdapter;
|
|
623
|
+
}
|
|
624
|
+
interface SchedulePaymentParams {
|
|
625
|
+
subscriptionId: string;
|
|
626
|
+
/** Seconds from now until execution. Use "0.0" for immediate. */
|
|
627
|
+
delaySeconds: string;
|
|
628
|
+
/** 0 = High, 1 = Medium, 2 = Low */
|
|
629
|
+
priority: number;
|
|
630
|
+
/** Compute units for the scheduled tx (minimum 10) */
|
|
631
|
+
executionEffort: number;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* FlowScheduler provides a JS/browser interface to schedule and cancel
|
|
635
|
+
* VeraPay subscription payments using Flow's native scheduled transactions.
|
|
636
|
+
*
|
|
637
|
+
* It uses FCL (Flow Client Library) which handles wallet authentication
|
|
638
|
+
* through Flow's wallet discovery (Blocto, Lilico, etc.).
|
|
639
|
+
*
|
|
640
|
+
* Usage:
|
|
641
|
+
* ```ts
|
|
642
|
+
* import { FlowScheduler } from "@verapay/sdk";
|
|
643
|
+
*
|
|
644
|
+
* const scheduler = new FlowScheduler({
|
|
645
|
+
* network: "testnet",
|
|
646
|
+
* handlerAddress: "7c0bf27829276c6b",
|
|
647
|
+
* });
|
|
648
|
+
*
|
|
649
|
+
* // Authenticate with a Flow wallet
|
|
650
|
+
* await scheduler.authenticate();
|
|
651
|
+
*
|
|
652
|
+
* // Schedule a payment for subscription #1 in 1 hour
|
|
653
|
+
* const txId = await scheduler.schedulePayment({
|
|
654
|
+
* subscriptionId: "1",
|
|
655
|
+
* delaySeconds: "3600.0",
|
|
656
|
+
* priority: 1, // Medium
|
|
657
|
+
* executionEffort: 1000,
|
|
658
|
+
* });
|
|
659
|
+
*
|
|
660
|
+
* // Wait for execution
|
|
661
|
+
* await scheduler.waitForTransaction(txId);
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
interface SubscribeAndScheduleResult {
|
|
665
|
+
flowTxId: string;
|
|
666
|
+
/** The ID of the scheduled future transaction (from FlowTransactionScheduler) */
|
|
667
|
+
scheduledTxId?: string;
|
|
668
|
+
receipt?: PaymentReceipt;
|
|
669
|
+
ipfsCid?: string;
|
|
670
|
+
}
|
|
671
|
+
declare class FlowScheduler {
|
|
672
|
+
readonly config: SchedulerConfig;
|
|
673
|
+
private configured;
|
|
674
|
+
private ipfs?;
|
|
675
|
+
constructor(config: SchedulerConfig);
|
|
676
|
+
get hasIPFS(): boolean;
|
|
677
|
+
setIPFSAdapter(adapter: IPFSAdapter): void;
|
|
678
|
+
private ensureConfigured;
|
|
679
|
+
/** Trigger FCL wallet authentication (Blocto, Lilico, etc.) */
|
|
680
|
+
authenticate(): Promise<{
|
|
681
|
+
addr: string;
|
|
682
|
+
}>;
|
|
683
|
+
/** Disconnect the current wallet */
|
|
684
|
+
unauthenticate(): Promise<void>;
|
|
685
|
+
/** Get the currently authenticated Flow address, or null */
|
|
686
|
+
currentUser(): Promise<string | null>;
|
|
687
|
+
/**
|
|
688
|
+
* Create a Cadence Owned Account (COA) and fund it with FLOW for EVM gas.
|
|
689
|
+
* Idempotent — skips if COA already exists. Must be called before initHandler.
|
|
690
|
+
* Returns the Flow transaction ID.
|
|
691
|
+
*/
|
|
692
|
+
setupCOA(fundAmount?: string): Promise<string>;
|
|
693
|
+
/**
|
|
694
|
+
* Initialize the VeraPay scheduled payment handler on the connected account.
|
|
695
|
+
* Idempotent — skips if already initialized. Requires a COA (call setupCOA first).
|
|
696
|
+
* Returns the Flow transaction ID.
|
|
697
|
+
*/
|
|
698
|
+
initHandler(): Promise<string>;
|
|
699
|
+
/**
|
|
700
|
+
* Convenience method: runs setupCOA + initHandler in sequence.
|
|
701
|
+
* Both are idempotent so it's safe to call on every session.
|
|
702
|
+
*/
|
|
703
|
+
setup(fundAmount?: string): Promise<{
|
|
704
|
+
coaTxId: string;
|
|
705
|
+
handlerTxId: string;
|
|
706
|
+
}>;
|
|
707
|
+
/**
|
|
708
|
+
* Schedule a subscription payment to be executed at a future time.
|
|
709
|
+
* The connected Flow wallet pays the scheduling fees.
|
|
710
|
+
* Returns the Flow transaction ID.
|
|
711
|
+
*/
|
|
712
|
+
schedulePayment(params: SchedulePaymentParams): Promise<string>;
|
|
713
|
+
/**
|
|
714
|
+
* Cancel a previously scheduled payment and receive a partial fee refund.
|
|
715
|
+
* Returns the Flow transaction ID.
|
|
716
|
+
*/
|
|
717
|
+
cancelScheduledPayment(scheduledTransactionId: string): Promise<string>;
|
|
718
|
+
/**
|
|
719
|
+
* Make the COA approve an EVM spender (e.g., VeraPay) to transfer ERC20 tokens.
|
|
720
|
+
* This is required before the scheduled payment can pull tokens from the COA.
|
|
721
|
+
* Returns the Flow transaction ID.
|
|
722
|
+
*/
|
|
723
|
+
approveERC20(tokenAddress: string, amount?: string): Promise<string>;
|
|
724
|
+
/**
|
|
725
|
+
* Subscribe the COA to a VeraPay plan AND schedule the next recurring payment
|
|
726
|
+
* in a single Cadence transaction.
|
|
727
|
+
*
|
|
728
|
+
* 1. COA calls subscribe(planId) on the EVM contract (first payment is charged immediately)
|
|
729
|
+
* 2. Decodes the returned subscription ID from the EVM call
|
|
730
|
+
* 3. Schedules the next payment via FlowTransactionScheduler with the plan's interval as delay
|
|
731
|
+
*
|
|
732
|
+
* If an IPFS adapter is configured, the payment receipt is automatically
|
|
733
|
+
* pinned after the transaction seals.
|
|
734
|
+
*
|
|
735
|
+
* Returns the Flow tx ID, and optionally the receipt + IPFS CID.
|
|
736
|
+
*/
|
|
737
|
+
subscribeAndSchedule(params: {
|
|
738
|
+
planId: number | string;
|
|
739
|
+
intervalSeconds: string;
|
|
740
|
+
priority?: number;
|
|
741
|
+
executionEffort?: number;
|
|
742
|
+
}): Promise<SubscribeAndScheduleResult>;
|
|
743
|
+
/**
|
|
744
|
+
* Subscribe the COA to a VeraPay plan via an EVM call (without scheduling).
|
|
745
|
+
* The COA must have already approved the VeraPay contract to spend its tokens.
|
|
746
|
+
* Returns the Flow transaction ID.
|
|
747
|
+
*/
|
|
748
|
+
subscribeToPlan(planId: number | string): Promise<string>;
|
|
749
|
+
/**
|
|
750
|
+
* Query the COA's EVM address for a given Flow account.
|
|
751
|
+
* Returns the hex EVM address (0x...) or null if no COA exists.
|
|
752
|
+
*/
|
|
753
|
+
getCoaEvmAddress(flowAddress?: string): Promise<string | null>;
|
|
754
|
+
/** Wait for a Flow transaction to be sealed. Returns the transaction result with events. */
|
|
755
|
+
waitForTransaction(txId: string): Promise<{
|
|
756
|
+
status: number;
|
|
757
|
+
events: Array<{
|
|
758
|
+
type: string;
|
|
759
|
+
data: Record<string, unknown>;
|
|
760
|
+
}>;
|
|
761
|
+
}>;
|
|
762
|
+
/**
|
|
763
|
+
* Extract the scheduled transaction ID from sealed tx events.
|
|
764
|
+
* Looks for the FlowTransactionScheduler.TransactionScheduled event.
|
|
765
|
+
*/
|
|
766
|
+
static extractScheduledTxId(events: Array<{
|
|
767
|
+
type: string;
|
|
768
|
+
data: Record<string, unknown>;
|
|
769
|
+
}>): string | undefined;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
export { CADENCE_HANDLERS, type CreatePlanParams, DEFAULT_IPFS_GATEWAY, DEPLOYED_CONTRACTS, ERC20_ABI, FlowScheduler, type IPFSAdapter, KNOWN_TOKENS, NETWORKS, type NetworkConfig, type NetworkName, type PaymentReceipt, type Plan, type SchedulePaymentParams, type SchedulerConfig, type StorachaConfig, type SubscribeAndScheduleResult, type Subscription, VERA_PAY_ABI, VeraPayClient, type VeraPayConfig, buildPaymentReceipt, createKuboAdapter, createMemoryAdapter, createStorachaAdapter, ipfsGatewayUrl };
|