@primuslabs/fund-js-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/dist/index.mjs ADDED
@@ -0,0 +1,1358 @@
1
+ // src/index.ts
2
+ import { ethers as ethers3 } from "ethers";
3
+
4
+ // src/classes/Fund.ts
5
+ import { ethers as ethers2 } from "ethers";
6
+ import { PrimusZKTLS } from "@primuslabs/zktls-js-sdk";
7
+
8
+ // src/config/constants.ts
9
+ var Fund_CONTRACTS = {
10
+ // monad testnet
11
+ 10143: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
12
+ };
13
+ var SUPPORTEDCHAINIDS = Object.keys(Fund_CONTRACTS).map((i) => Number(i));
14
+ var DATASOURCETEMPLATEMAP = {
15
+ x: "2e3160ae-8b1e-45e3-8c59-426366278b9d",
16
+ tiktok: "2b22c9f8-686d-4482-a0cf-c9c43c1181ad",
17
+ "google account": ""
18
+ };
19
+ var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATEMAP);
20
+
21
+ // src/classes/Contract.ts
22
+ import { ethers } from "ethers";
23
+ var Contract = class {
24
+ address;
25
+ provider;
26
+ contractInstance;
27
+ /**
28
+ * @param chainName The name of the chain, used to identify and differentiate between different chains.
29
+ * @param provider The provider object for the blockchain, used to establish and manage the connection with the blockchain.
30
+ */
31
+ constructor(provider, address, abiJson) {
32
+ if (!provider || !address || !abiJson) {
33
+ throw new Error("provider, address, and abiJson are required");
34
+ }
35
+ this.address = address;
36
+ this.provider = provider;
37
+ const signer = this.provider.getSigner();
38
+ this.contractInstance = new ethers.Contract(this.address, abiJson, signer);
39
+ }
40
+ // Example method to read from the contract
41
+ async callMethod(functionName, functionParams) {
42
+ return new Promise(async (resolve, reject) => {
43
+ if (!this.contractInstance[functionName]) {
44
+ return reject(`Method ${functionName} does not exist on the contract`);
45
+ }
46
+ try {
47
+ const result = await this.contractInstance[functionName](...functionParams);
48
+ return resolve(result);
49
+ } catch (error) {
50
+ return reject(error);
51
+ }
52
+ });
53
+ }
54
+ // Example method to send a transaction
55
+ async sendTransaction(functionName, functionParams) {
56
+ return new Promise(async (resolve, reject) => {
57
+ if (!this.contractInstance[functionName]) {
58
+ return reject(`Method ${functionName} does not exist on the contract`);
59
+ }
60
+ try {
61
+ console.log("sendTransaction params:", ...functionParams);
62
+ const tx = await this.contractInstance[functionName](...functionParams);
63
+ const txreceipt = await tx.wait();
64
+ console.log("txreceipt", txreceipt);
65
+ resolve(txreceipt);
66
+ } catch (error) {
67
+ console.log("sendTransaction error:", error);
68
+ if (error?.code === "ACTION_REJECTED") {
69
+ return reject("user rejected transaction");
70
+ }
71
+ if (error?.reason) {
72
+ return reject(error.reason);
73
+ }
74
+ if (error?.data?.message === "insufficient balance") {
75
+ return reject(error?.data?.message);
76
+ }
77
+ }
78
+ });
79
+ }
80
+ };
81
+ var Contract_default = Contract;
82
+
83
+ // src/config/abi.json
84
+ var abi_default = [
85
+ {
86
+ type: "function",
87
+ name: "addBatchIdSource",
88
+ inputs: [
89
+ { name: "sourceName_", type: "string[]", internalType: "string[]" },
90
+ { name: "url_", type: "string[]", internalType: "string[]" },
91
+ { name: "jsonPath_", type: "string[]", internalType: "string[]" }
92
+ ],
93
+ outputs: [],
94
+ stateMutability: "nonpayable"
95
+ },
96
+ {
97
+ type: "function",
98
+ name: "claimByMultiSource",
99
+ inputs: [
100
+ { name: "idSources", type: "string[]", internalType: "string[]" },
101
+ {
102
+ name: "att",
103
+ type: "tuple[]",
104
+ internalType: "struct Attestation[]",
105
+ components: [
106
+ { name: "recipient", type: "address", internalType: "address" },
107
+ {
108
+ name: "request",
109
+ type: "tuple",
110
+ internalType: "struct AttNetworkRequest",
111
+ components: [
112
+ { name: "url", type: "string", internalType: "string" },
113
+ { name: "header", type: "string", internalType: "string" },
114
+ { name: "method", type: "string", internalType: "string" },
115
+ { name: "body", type: "string", internalType: "string" }
116
+ ]
117
+ },
118
+ {
119
+ name: "reponseResolve",
120
+ type: "tuple[]",
121
+ internalType: "struct AttNetworkResponseResolve[]",
122
+ components: [
123
+ { name: "keyName", type: "string", internalType: "string" },
124
+ {
125
+ name: "parseType",
126
+ type: "string",
127
+ internalType: "string"
128
+ },
129
+ {
130
+ name: "parsePath",
131
+ type: "string",
132
+ internalType: "string"
133
+ }
134
+ ]
135
+ },
136
+ { name: "data", type: "string", internalType: "string" },
137
+ {
138
+ name: "attConditions",
139
+ type: "string",
140
+ internalType: "string"
141
+ },
142
+ { name: "timestamp", type: "uint64", internalType: "uint64" },
143
+ {
144
+ name: "additionParams",
145
+ type: "string",
146
+ internalType: "string"
147
+ },
148
+ {
149
+ name: "attestors",
150
+ type: "tuple[]",
151
+ internalType: "struct Attestor[]",
152
+ components: [
153
+ {
154
+ name: "attestorAddr",
155
+ type: "address",
156
+ internalType: "address"
157
+ },
158
+ { name: "url", type: "string", internalType: "string" }
159
+ ]
160
+ },
161
+ { name: "signatures", type: "bytes[]", internalType: "bytes[]" }
162
+ ]
163
+ }
164
+ ],
165
+ outputs: [],
166
+ stateMutability: "payable"
167
+ },
168
+ {
169
+ type: "function",
170
+ name: "claimBySource",
171
+ inputs: [
172
+ { name: "idSource", type: "string", internalType: "string" },
173
+ {
174
+ name: "att",
175
+ type: "tuple",
176
+ internalType: "struct Attestation",
177
+ components: [
178
+ { name: "recipient", type: "address", internalType: "address" },
179
+ {
180
+ name: "request",
181
+ type: "tuple",
182
+ internalType: "struct AttNetworkRequest",
183
+ components: [
184
+ { name: "url", type: "string", internalType: "string" },
185
+ { name: "header", type: "string", internalType: "string" },
186
+ { name: "method", type: "string", internalType: "string" },
187
+ { name: "body", type: "string", internalType: "string" }
188
+ ]
189
+ },
190
+ {
191
+ name: "reponseResolve",
192
+ type: "tuple[]",
193
+ internalType: "struct AttNetworkResponseResolve[]",
194
+ components: [
195
+ { name: "keyName", type: "string", internalType: "string" },
196
+ {
197
+ name: "parseType",
198
+ type: "string",
199
+ internalType: "string"
200
+ },
201
+ {
202
+ name: "parsePath",
203
+ type: "string",
204
+ internalType: "string"
205
+ }
206
+ ]
207
+ },
208
+ { name: "data", type: "string", internalType: "string" },
209
+ {
210
+ name: "attConditions",
211
+ type: "string",
212
+ internalType: "string"
213
+ },
214
+ { name: "timestamp", type: "uint64", internalType: "uint64" },
215
+ {
216
+ name: "additionParams",
217
+ type: "string",
218
+ internalType: "string"
219
+ },
220
+ {
221
+ name: "attestors",
222
+ type: "tuple[]",
223
+ internalType: "struct Attestor[]",
224
+ components: [
225
+ {
226
+ name: "attestorAddr",
227
+ type: "address",
228
+ internalType: "address"
229
+ },
230
+ { name: "url", type: "string", internalType: "string" }
231
+ ]
232
+ },
233
+ { name: "signatures", type: "bytes[]", internalType: "bytes[]" }
234
+ ]
235
+ }
236
+ ],
237
+ outputs: [],
238
+ stateMutability: "payable"
239
+ },
240
+ {
241
+ type: "function",
242
+ name: "claimBySourceAndTipIndex",
243
+ inputs: [
244
+ { name: "idSource", type: "string", internalType: "string" },
245
+ {
246
+ name: "att",
247
+ type: "tuple",
248
+ internalType: "struct Attestation",
249
+ components: [
250
+ { name: "recipient", type: "address", internalType: "address" },
251
+ {
252
+ name: "request",
253
+ type: "tuple",
254
+ internalType: "struct AttNetworkRequest",
255
+ components: [
256
+ { name: "url", type: "string", internalType: "string" },
257
+ { name: "header", type: "string", internalType: "string" },
258
+ { name: "method", type: "string", internalType: "string" },
259
+ { name: "body", type: "string", internalType: "string" }
260
+ ]
261
+ },
262
+ {
263
+ name: "reponseResolve",
264
+ type: "tuple[]",
265
+ internalType: "struct AttNetworkResponseResolve[]",
266
+ components: [
267
+ { name: "keyName", type: "string", internalType: "string" },
268
+ {
269
+ name: "parseType",
270
+ type: "string",
271
+ internalType: "string"
272
+ },
273
+ {
274
+ name: "parsePath",
275
+ type: "string",
276
+ internalType: "string"
277
+ }
278
+ ]
279
+ },
280
+ { name: "data", type: "string", internalType: "string" },
281
+ {
282
+ name: "attConditions",
283
+ type: "string",
284
+ internalType: "string"
285
+ },
286
+ { name: "timestamp", type: "uint64", internalType: "uint64" },
287
+ {
288
+ name: "additionParams",
289
+ type: "string",
290
+ internalType: "string"
291
+ },
292
+ {
293
+ name: "attestors",
294
+ type: "tuple[]",
295
+ internalType: "struct Attestor[]",
296
+ components: [
297
+ {
298
+ name: "attestorAddr",
299
+ type: "address",
300
+ internalType: "address"
301
+ },
302
+ { name: "url", type: "string", internalType: "string" }
303
+ ]
304
+ },
305
+ { name: "signatures", type: "bytes[]", internalType: "bytes[]" }
306
+ ]
307
+ },
308
+ { name: "index", type: "uint32", internalType: "uint32" }
309
+ ],
310
+ outputs: [],
311
+ stateMutability: "payable"
312
+ },
313
+ {
314
+ type: "function",
315
+ name: "claimFee",
316
+ inputs: [],
317
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
318
+ stateMutability: "view"
319
+ },
320
+ {
321
+ type: "function",
322
+ name: "feeRecipient",
323
+ inputs: [],
324
+ outputs: [{ name: "", type: "address", internalType: "address" }],
325
+ stateMutability: "view"
326
+ },
327
+ {
328
+ type: "function",
329
+ name: "getTipRecords",
330
+ inputs: [
331
+ {
332
+ name: "tipRecipient",
333
+ type: "tuple",
334
+ internalType: "struct TipRecipient",
335
+ components: [
336
+ { name: "idSource", type: "string", internalType: "string" },
337
+ { name: "id", type: "string", internalType: "string" }
338
+ ]
339
+ }
340
+ ],
341
+ outputs: [
342
+ {
343
+ name: "",
344
+ type: "tuple[]",
345
+ internalType: "struct TipRecord[]",
346
+ components: [
347
+ { name: "amount", type: "uint256", internalType: "uint256" },
348
+ {
349
+ name: "tipToken",
350
+ type: "tuple",
351
+ internalType: "struct TipToken",
352
+ components: [
353
+ {
354
+ name: "tokenType",
355
+ type: "uint32",
356
+ internalType: "uint32"
357
+ },
358
+ {
359
+ name: "tokenAddress",
360
+ type: "address",
361
+ internalType: "address"
362
+ }
363
+ ]
364
+ },
365
+ { name: "timestamp", type: "uint64", internalType: "uint64" },
366
+ { name: "tipper", type: "address", internalType: "address" },
367
+ { name: "nftIds", type: "uint256[]", internalType: "uint256[]" }
368
+ ]
369
+ }
370
+ ],
371
+ stateMutability: "view"
372
+ },
373
+ {
374
+ type: "function",
375
+ name: "idSourceCache",
376
+ inputs: [{ name: "", type: "string", internalType: "string" }],
377
+ outputs: [
378
+ { name: "url", type: "string", internalType: "string" },
379
+ { name: "jsonPath", type: "string", internalType: "string" }
380
+ ],
381
+ stateMutability: "view"
382
+ },
383
+ {
384
+ type: "function",
385
+ name: "initialize",
386
+ inputs: [
387
+ { name: "owner", type: "address", internalType: "address" },
388
+ {
389
+ name: "primusZKTLS_",
390
+ type: "address",
391
+ internalType: "contract IPrimusZKTLS"
392
+ },
393
+ { name: "feeRecipient_", type: "address", internalType: "address" },
394
+ { name: "claimFee_", type: "uint256", internalType: "uint256" }
395
+ ],
396
+ outputs: [],
397
+ stateMutability: "nonpayable"
398
+ },
399
+ {
400
+ type: "function",
401
+ name: "owner",
402
+ inputs: [],
403
+ outputs: [{ name: "", type: "address", internalType: "address" }],
404
+ stateMutability: "view"
405
+ },
406
+ {
407
+ type: "function",
408
+ name: "primusZKTLS",
409
+ inputs: [],
410
+ outputs: [
411
+ { name: "", type: "address", internalType: "contract IPrimusZKTLS" }
412
+ ],
413
+ stateMutability: "view"
414
+ },
415
+ {
416
+ type: "function",
417
+ name: "renounceOwnership",
418
+ inputs: [],
419
+ outputs: [],
420
+ stateMutability: "nonpayable"
421
+ },
422
+ {
423
+ type: "function",
424
+ name: "setClaimFee",
425
+ inputs: [
426
+ { name: "claimFee_", type: "uint256", internalType: "uint256" }
427
+ ],
428
+ outputs: [],
429
+ stateMutability: "nonpayable"
430
+ },
431
+ {
432
+ type: "function",
433
+ name: "setFeeRecipient",
434
+ inputs: [
435
+ { name: "feeRecipient_", type: "address", internalType: "address" }
436
+ ],
437
+ outputs: [],
438
+ stateMutability: "nonpayable"
439
+ },
440
+ {
441
+ type: "function",
442
+ name: "setPrimusZKTLS",
443
+ inputs: [
444
+ {
445
+ name: "primusZKTLS_",
446
+ type: "address",
447
+ internalType: "contract IPrimusZKTLS"
448
+ }
449
+ ],
450
+ outputs: [],
451
+ stateMutability: "nonpayable"
452
+ },
453
+ {
454
+ type: "function",
455
+ name: "setWithdrawDelay",
456
+ inputs: [
457
+ { name: "delay", type: "uint256", internalType: "uint256" }
458
+ ],
459
+ outputs: [],
460
+ stateMutability: "nonpayable"
461
+ },
462
+ {
463
+ type: "function",
464
+ name: "tip",
465
+ inputs: [
466
+ {
467
+ name: "token",
468
+ type: "tuple",
469
+ internalType: "struct TipToken",
470
+ components: [
471
+ { name: "tokenType", type: "uint32", internalType: "uint32" },
472
+ {
473
+ name: "tokenAddress",
474
+ type: "address",
475
+ internalType: "address"
476
+ }
477
+ ]
478
+ },
479
+ {
480
+ name: "recipient",
481
+ type: "tuple",
482
+ internalType: "struct TipRecipientInfo",
483
+ components: [
484
+ { name: "idSource", type: "string", internalType: "string" },
485
+ { name: "id", type: "string", internalType: "string" },
486
+ { name: "amount", type: "uint256", internalType: "uint256" },
487
+ { name: "nftIds", type: "uint256[]", internalType: "uint256[]" }
488
+ ]
489
+ }
490
+ ],
491
+ outputs: [],
492
+ stateMutability: "payable"
493
+ },
494
+ {
495
+ type: "function",
496
+ name: "tipBatch",
497
+ inputs: [
498
+ {
499
+ name: "token",
500
+ type: "tuple",
501
+ internalType: "struct TipToken",
502
+ components: [
503
+ { name: "tokenType", type: "uint32", internalType: "uint32" },
504
+ {
505
+ name: "tokenAddress",
506
+ type: "address",
507
+ internalType: "address"
508
+ }
509
+ ]
510
+ },
511
+ {
512
+ name: "recipients",
513
+ type: "tuple[]",
514
+ internalType: "struct TipRecipientInfo[]",
515
+ components: [
516
+ { name: "idSource", type: "string", internalType: "string" },
517
+ { name: "id", type: "string", internalType: "string" },
518
+ { name: "amount", type: "uint256", internalType: "uint256" },
519
+ { name: "nftIds", type: "uint256[]", internalType: "uint256[]" }
520
+ ]
521
+ }
522
+ ],
523
+ outputs: [],
524
+ stateMutability: "payable"
525
+ },
526
+ {
527
+ type: "function",
528
+ name: "tipperWithdraw",
529
+ inputs: [
530
+ {
531
+ name: "tipRecipients",
532
+ type: "tuple[]",
533
+ internalType: "struct TipRecipient[]",
534
+ components: [
535
+ { name: "idSource", type: "string", internalType: "string" },
536
+ { name: "id", type: "string", internalType: "string" }
537
+ ]
538
+ }
539
+ ],
540
+ outputs: [],
541
+ stateMutability: "nonpayable"
542
+ },
543
+ {
544
+ type: "function",
545
+ name: "transferOwnership",
546
+ inputs: [
547
+ { name: "newOwner", type: "address", internalType: "address" }
548
+ ],
549
+ outputs: [],
550
+ stateMutability: "nonpayable"
551
+ },
552
+ {
553
+ type: "function",
554
+ name: "withdrawDelay",
555
+ inputs: [],
556
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
557
+ stateMutability: "view"
558
+ },
559
+ {
560
+ type: "event",
561
+ name: "ClaimEvent",
562
+ inputs: [
563
+ {
564
+ name: "recipient",
565
+ type: "address",
566
+ indexed: true,
567
+ internalType: "address"
568
+ },
569
+ {
570
+ name: "idSource",
571
+ type: "string",
572
+ indexed: false,
573
+ internalType: "string"
574
+ },
575
+ {
576
+ name: "id",
577
+ type: "string",
578
+ indexed: false,
579
+ internalType: "string"
580
+ },
581
+ {
582
+ name: "tipper",
583
+ type: "address",
584
+ indexed: false,
585
+ internalType: "address"
586
+ },
587
+ {
588
+ name: "tokenAddr",
589
+ type: "address",
590
+ indexed: false,
591
+ internalType: "address"
592
+ },
593
+ {
594
+ name: "amount",
595
+ type: "uint256",
596
+ indexed: false,
597
+ internalType: "uint256"
598
+ }
599
+ ],
600
+ anonymous: false
601
+ },
602
+ {
603
+ type: "event",
604
+ name: "Initialized",
605
+ inputs: [
606
+ {
607
+ name: "version",
608
+ type: "uint64",
609
+ indexed: false,
610
+ internalType: "uint64"
611
+ }
612
+ ],
613
+ anonymous: false
614
+ },
615
+ {
616
+ type: "event",
617
+ name: "OwnershipTransferred",
618
+ inputs: [
619
+ {
620
+ name: "previousOwner",
621
+ type: "address",
622
+ indexed: true,
623
+ internalType: "address"
624
+ },
625
+ {
626
+ name: "newOwner",
627
+ type: "address",
628
+ indexed: true,
629
+ internalType: "address"
630
+ }
631
+ ],
632
+ anonymous: false
633
+ },
634
+ {
635
+ type: "event",
636
+ name: "TipEvent",
637
+ inputs: [
638
+ {
639
+ name: "idSource",
640
+ type: "string",
641
+ indexed: false,
642
+ internalType: "string"
643
+ },
644
+ {
645
+ name: "id",
646
+ type: "string",
647
+ indexed: false,
648
+ internalType: "string"
649
+ },
650
+ {
651
+ name: "tipper",
652
+ type: "address",
653
+ indexed: false,
654
+ internalType: "address"
655
+ }
656
+ ],
657
+ anonymous: false
658
+ },
659
+ {
660
+ type: "event",
661
+ name: "WithdrawEvent",
662
+ inputs: [
663
+ {
664
+ name: "tipper",
665
+ type: "address",
666
+ indexed: true,
667
+ internalType: "address"
668
+ },
669
+ {
670
+ name: "tokenAddr",
671
+ type: "address",
672
+ indexed: true,
673
+ internalType: "address"
674
+ },
675
+ {
676
+ name: "amount",
677
+ type: "uint256",
678
+ indexed: false,
679
+ internalType: "uint256"
680
+ }
681
+ ],
682
+ anonymous: false
683
+ },
684
+ { type: "error", name: "InvalidInitialization", inputs: [] },
685
+ { type: "error", name: "NotInitializing", inputs: [] },
686
+ {
687
+ type: "error",
688
+ name: "OwnableInvalidOwner",
689
+ inputs: [
690
+ { name: "owner", type: "address", internalType: "address" }
691
+ ]
692
+ },
693
+ {
694
+ type: "error",
695
+ name: "OwnableUnauthorizedAccount",
696
+ inputs: [
697
+ { name: "account", type: "address", internalType: "address" }
698
+ ]
699
+ },
700
+ { type: "error", name: "ReentrancyGuardReentrantCall", inputs: [] }
701
+ ];
702
+
703
+ // src/config/erc20Abi.json
704
+ var erc20Abi_default = [
705
+ {
706
+ type: "function",
707
+ name: "allowance",
708
+ inputs: [
709
+ {
710
+ name: "owner",
711
+ type: "address",
712
+ internalType: "address"
713
+ },
714
+ {
715
+ name: "spender",
716
+ type: "address",
717
+ internalType: "address"
718
+ }
719
+ ],
720
+ outputs: [
721
+ {
722
+ name: "",
723
+ type: "uint256",
724
+ internalType: "uint256"
725
+ }
726
+ ],
727
+ stateMutability: "view"
728
+ },
729
+ {
730
+ type: "function",
731
+ name: "approve",
732
+ inputs: [
733
+ {
734
+ name: "spender",
735
+ type: "address",
736
+ internalType: "address"
737
+ },
738
+ {
739
+ name: "value",
740
+ type: "uint256",
741
+ internalType: "uint256"
742
+ }
743
+ ],
744
+ outputs: [
745
+ {
746
+ name: "",
747
+ type: "bool",
748
+ internalType: "bool"
749
+ }
750
+ ],
751
+ stateMutability: "nonpayable"
752
+ },
753
+ {
754
+ type: "function",
755
+ name: "balanceOf",
756
+ inputs: [
757
+ {
758
+ name: "account",
759
+ type: "address",
760
+ internalType: "address"
761
+ }
762
+ ],
763
+ outputs: [
764
+ {
765
+ name: "",
766
+ type: "uint256",
767
+ internalType: "uint256"
768
+ }
769
+ ],
770
+ stateMutability: "view"
771
+ },
772
+ {
773
+ type: "function",
774
+ name: "decimals",
775
+ inputs: [],
776
+ outputs: [
777
+ {
778
+ name: "",
779
+ type: "uint8",
780
+ internalType: "uint8"
781
+ }
782
+ ],
783
+ stateMutability: "view"
784
+ },
785
+ {
786
+ type: "function",
787
+ name: "name",
788
+ inputs: [],
789
+ outputs: [
790
+ {
791
+ name: "",
792
+ type: "string",
793
+ internalType: "string"
794
+ }
795
+ ],
796
+ stateMutability: "view"
797
+ },
798
+ {
799
+ type: "function",
800
+ name: "symbol",
801
+ inputs: [],
802
+ outputs: [
803
+ {
804
+ name: "",
805
+ type: "string",
806
+ internalType: "string"
807
+ }
808
+ ],
809
+ stateMutability: "view"
810
+ },
811
+ {
812
+ type: "function",
813
+ name: "totalSupply",
814
+ inputs: [],
815
+ outputs: [
816
+ {
817
+ name: "",
818
+ type: "uint256",
819
+ internalType: "uint256"
820
+ }
821
+ ],
822
+ stateMutability: "view"
823
+ },
824
+ {
825
+ type: "function",
826
+ name: "transfer",
827
+ inputs: [
828
+ {
829
+ name: "to",
830
+ type: "address",
831
+ internalType: "address"
832
+ },
833
+ {
834
+ name: "value",
835
+ type: "uint256",
836
+ internalType: "uint256"
837
+ }
838
+ ],
839
+ outputs: [
840
+ {
841
+ name: "",
842
+ type: "bool",
843
+ internalType: "bool"
844
+ }
845
+ ],
846
+ stateMutability: "nonpayable"
847
+ },
848
+ {
849
+ type: "function",
850
+ name: "transferFrom",
851
+ inputs: [
852
+ {
853
+ name: "from",
854
+ type: "address",
855
+ internalType: "address"
856
+ },
857
+ {
858
+ name: "to",
859
+ type: "address",
860
+ internalType: "address"
861
+ },
862
+ {
863
+ name: "value",
864
+ type: "uint256",
865
+ internalType: "uint256"
866
+ }
867
+ ],
868
+ outputs: [
869
+ {
870
+ name: "",
871
+ type: "bool",
872
+ internalType: "bool"
873
+ }
874
+ ],
875
+ stateMutability: "nonpayable"
876
+ },
877
+ {
878
+ type: "event",
879
+ name: "Approval",
880
+ inputs: [
881
+ {
882
+ name: "owner",
883
+ type: "address",
884
+ indexed: true,
885
+ internalType: "address"
886
+ },
887
+ {
888
+ name: "spender",
889
+ type: "address",
890
+ indexed: true,
891
+ internalType: "address"
892
+ },
893
+ {
894
+ name: "value",
895
+ type: "uint256",
896
+ indexed: false,
897
+ internalType: "uint256"
898
+ }
899
+ ],
900
+ anonymous: false
901
+ },
902
+ {
903
+ type: "event",
904
+ name: "Transfer",
905
+ inputs: [
906
+ {
907
+ name: "from",
908
+ type: "address",
909
+ indexed: true,
910
+ internalType: "address"
911
+ },
912
+ {
913
+ name: "to",
914
+ type: "address",
915
+ indexed: true,
916
+ internalType: "address"
917
+ },
918
+ {
919
+ name: "value",
920
+ type: "uint256",
921
+ indexed: false,
922
+ internalType: "uint256"
923
+ }
924
+ ],
925
+ anonymous: false
926
+ },
927
+ {
928
+ type: "error",
929
+ name: "ERC20InsufficientAllowance",
930
+ inputs: [
931
+ {
932
+ name: "spender",
933
+ type: "address",
934
+ internalType: "address"
935
+ },
936
+ {
937
+ name: "allowance",
938
+ type: "uint256",
939
+ internalType: "uint256"
940
+ },
941
+ {
942
+ name: "needed",
943
+ type: "uint256",
944
+ internalType: "uint256"
945
+ }
946
+ ]
947
+ },
948
+ {
949
+ type: "error",
950
+ name: "ERC20InsufficientBalance",
951
+ inputs: [
952
+ {
953
+ name: "sender",
954
+ type: "address",
955
+ internalType: "address"
956
+ },
957
+ {
958
+ name: "balance",
959
+ type: "uint256",
960
+ internalType: "uint256"
961
+ },
962
+ {
963
+ name: "needed",
964
+ type: "uint256",
965
+ internalType: "uint256"
966
+ }
967
+ ]
968
+ },
969
+ {
970
+ type: "error",
971
+ name: "ERC20InvalidApprover",
972
+ inputs: [
973
+ {
974
+ name: "approver",
975
+ type: "address",
976
+ internalType: "address"
977
+ }
978
+ ]
979
+ },
980
+ {
981
+ type: "error",
982
+ name: "ERC20InvalidReceiver",
983
+ inputs: [
984
+ {
985
+ name: "receiver",
986
+ type: "address",
987
+ internalType: "address"
988
+ }
989
+ ]
990
+ },
991
+ {
992
+ type: "error",
993
+ name: "ERC20InvalidSender",
994
+ inputs: [
995
+ {
996
+ name: "sender",
997
+ type: "address",
998
+ internalType: "address"
999
+ }
1000
+ ]
1001
+ },
1002
+ {
1003
+ type: "error",
1004
+ name: "ERC20InvalidSpender",
1005
+ inputs: [
1006
+ {
1007
+ name: "spender",
1008
+ type: "address",
1009
+ internalType: "address"
1010
+ }
1011
+ ]
1012
+ }
1013
+ ];
1014
+
1015
+ // src/classes/Fund.ts
1016
+ var Fund = class {
1017
+ _attestLoading;
1018
+ zkTlsSdk;
1019
+ fundContract;
1020
+ provider;
1021
+ _dataSourceTemplateMap = DATASOURCETEMPLATEMAP;
1022
+ constructor() {
1023
+ this._attestLoading = false;
1024
+ }
1025
+ getZkTlsSdk() {
1026
+ return this.zkTlsSdk;
1027
+ }
1028
+ async init(provider, chainId, appId) {
1029
+ return new Promise(async (resolve, reject) => {
1030
+ try {
1031
+ const network = await provider.getNetwork();
1032
+ const providerChainId = network.chainId;
1033
+ console.log("init provider", provider, network);
1034
+ if (providerChainId !== chainId) {
1035
+ return reject(`Please connect to the chain with ID ${chainId} first.`);
1036
+ }
1037
+ const fundContractAddress = Fund_CONTRACTS[chainId];
1038
+ if (!fundContractAddress) {
1039
+ return reject(`Unsupported chainId:${chainId}`);
1040
+ }
1041
+ this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
1042
+ this.provider = provider;
1043
+ if (appId) {
1044
+ this.zkTlsSdk = new PrimusZKTLS();
1045
+ const extensionVersion = await this.zkTlsSdk.init(
1046
+ appId
1047
+ );
1048
+ resolve(extensionVersion);
1049
+ } else {
1050
+ resolve("success");
1051
+ }
1052
+ } catch (error) {
1053
+ return reject(error);
1054
+ }
1055
+ });
1056
+ }
1057
+ async fund(tokenInfo, recipientInfo) {
1058
+ return new Promise(async (resolve, reject) => {
1059
+ try {
1060
+ const recipientInfos = [];
1061
+ recipientInfos[0] = recipientInfo;
1062
+ let decimals = 18;
1063
+ let params = [];
1064
+ if (tokenInfo.tokenType === 0) {
1065
+ await this.approve(tokenInfo, recipientInfos);
1066
+ const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1067
+ decimals = await erc20Contract.decimals();
1068
+ }
1069
+ const tokenAmount = ethers2.utils.parseUnits(recipientInfo.tokenAmount.toString(), decimals);
1070
+ const newFundRecipientInfo = {
1071
+ idSource: recipientInfo.socialPlatform,
1072
+ id: recipientInfo.userIdentifier,
1073
+ amount: tokenAmount,
1074
+ nftIds: []
1075
+ };
1076
+ if (tokenInfo.tokenType === 0) {
1077
+ params = [tokenInfo, newFundRecipientInfo];
1078
+ } else {
1079
+ params = [tokenInfo, newFundRecipientInfo, { value: tokenAmount }];
1080
+ }
1081
+ const result = await this.fundContract.sendTransaction("tip", params);
1082
+ resolve(result);
1083
+ } catch (error) {
1084
+ return reject(error);
1085
+ }
1086
+ });
1087
+ }
1088
+ async refund(recipients) {
1089
+ return new Promise(async (resolve, reject) => {
1090
+ try {
1091
+ const newRecipients = recipients.map((i) => {
1092
+ return {
1093
+ idSource: i.socialPlatform,
1094
+ id: i.userIdentifier
1095
+ };
1096
+ });
1097
+ const result = await this.fundContract.sendTransaction("tipperWithdraw", [newRecipients]);
1098
+ return resolve(result);
1099
+ } catch (error) {
1100
+ return reject(error);
1101
+ }
1102
+ });
1103
+ }
1104
+ async fundBatch(tokenInfo, recipientInfoList) {
1105
+ return new Promise(async (resolve, reject) => {
1106
+ try {
1107
+ let decimals = 18;
1108
+ let params = [];
1109
+ if (tokenInfo.tokenType === 0) {
1110
+ await this.approve(tokenInfo, recipientInfoList);
1111
+ const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1112
+ decimals = await erc20Contract.decimals();
1113
+ }
1114
+ let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(ethers2.utils.parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
1115
+ const newRecipientInfoList = recipientInfoList.map((i) => {
1116
+ const formatAmount = ethers2.utils.parseUnits(i.tokenAmount.toString(), decimals);
1117
+ return {
1118
+ idSource: i.socialPlatform,
1119
+ id: i.userIdentifier,
1120
+ amount: formatAmount,
1121
+ nftIds: []
1122
+ };
1123
+ });
1124
+ if (tokenInfo.tokenType === 0) {
1125
+ params = [tokenInfo, newRecipientInfoList];
1126
+ } else {
1127
+ params = [tokenInfo, newRecipientInfoList, { value: totalFormatAmount }];
1128
+ }
1129
+ const result = await this.fundContract.sendTransaction("tipBatch", params);
1130
+ return resolve(result);
1131
+ } catch (error) {
1132
+ return reject(error);
1133
+ }
1134
+ });
1135
+ }
1136
+ async approve(tokenInfo, recipientInfoList) {
1137
+ return new Promise(async (resolve, reject) => {
1138
+ try {
1139
+ const signer = this.provider.getSigner();
1140
+ const address = await signer.getAddress();
1141
+ const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
1142
+ const allowance = await erc20Contract.allowance(address, this.fundContract.address);
1143
+ const decimals = await erc20Contract.decimals();
1144
+ const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(ethers2.utils.parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
1145
+ if (allowance.lt(requiredAllowance)) {
1146
+ const tx = await erc20Contract.approve(this.fundContract.address, requiredAllowance);
1147
+ await tx.wait();
1148
+ console.log(`Approved: ${requiredAllowance.toString()}`);
1149
+ } else {
1150
+ console.log(`Already approved: ${allowance.toString()}`);
1151
+ }
1152
+ resolve("Approved");
1153
+ } catch (error) {
1154
+ console.error("Approval failed:", error);
1155
+ if (error?.code === "ACTION_REJECTED") {
1156
+ return reject("user rejected transaction");
1157
+ }
1158
+ return reject(error);
1159
+ }
1160
+ });
1161
+ }
1162
+ async attest(socialPlatform, address, genAppSignature) {
1163
+ return new Promise(async (resolve, reject) => {
1164
+ if (!this.zkTlsSdk?.padoExtensionVersion) {
1165
+ return reject(`Uninitialized!`);
1166
+ }
1167
+ if (this._attestLoading) {
1168
+ return reject(`Under proof!`);
1169
+ }
1170
+ this._attestLoading = true;
1171
+ const templateId = this._dataSourceTemplateMap[socialPlatform];
1172
+ const attRequest = this.zkTlsSdk.generateRequestParams(
1173
+ templateId,
1174
+ address
1175
+ );
1176
+ const signParams = attRequest.toJsonString();
1177
+ const signature = await genAppSignature(signParams);
1178
+ if (!signature) {
1179
+ return reject(`appSignature is empty!`);
1180
+ }
1181
+ try {
1182
+ const formatAttestParams = {
1183
+ attRequest: {
1184
+ ...JSON.parse(signParams)
1185
+ },
1186
+ appSignature: signature
1187
+ };
1188
+ const attestation = await this.zkTlsSdk.startAttestation(
1189
+ JSON.stringify(formatAttestParams)
1190
+ );
1191
+ this._attestLoading = false;
1192
+ resolve(attestation);
1193
+ } catch (error) {
1194
+ return reject(error);
1195
+ }
1196
+ });
1197
+ }
1198
+ async claimBySource(socialPlatform, userIdentifier, attestation) {
1199
+ return new Promise(async (resolve, reject) => {
1200
+ try {
1201
+ const claimFee = await this.fundContract.callMethod("claimFee", []);
1202
+ const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: socialPlatform, id: userIdentifier }]);
1203
+ console.log("fundRecords", fundRecords);
1204
+ const recordCount = fundRecords.length;
1205
+ if (fundRecords <= 0) {
1206
+ return reject(`No fund records.`);
1207
+ } else {
1208
+ const totalFee = claimFee.mul(recordCount);
1209
+ const txreceipt = await this.fundContract.sendTransaction("claimBySource", [socialPlatform, attestation, { value: totalFee }]);
1210
+ return resolve(txreceipt);
1211
+ }
1212
+ } catch (error) {
1213
+ return reject(error);
1214
+ }
1215
+ });
1216
+ }
1217
+ async claimByMultiSource(socialPlatforms, userIdentifiers, attestationList) {
1218
+ return new Promise(async (resolve, reject) => {
1219
+ try {
1220
+ if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestationList.length) {
1221
+ return reject(`socialPlatforms, userIdentifiers, attestationList length must be equal.`);
1222
+ }
1223
+ const claimFee = await this.fundContract.callMethod("claimFee", []);
1224
+ let allFundRecords = [];
1225
+ let recordCount = 0;
1226
+ for (const [index, value] of socialPlatforms.entries()) {
1227
+ const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: value, id: userIdentifiers[index] }]);
1228
+ allFundRecords.push(...fundRecords);
1229
+ recordCount += fundRecords.length;
1230
+ }
1231
+ if (recordCount <= 0) {
1232
+ return reject(`No fund records.`);
1233
+ } else {
1234
+ const totalFee = claimFee.mul(recordCount);
1235
+ const txreceipt = await this.fundContract.sendTransaction("claimByMultiSource", [socialPlatforms, userIdentifiers, attestationList, { value: totalFee }]);
1236
+ return resolve(txreceipt);
1237
+ }
1238
+ } catch (error) {
1239
+ console.error("claimByMultiSource", error);
1240
+ return reject(error);
1241
+ }
1242
+ });
1243
+ }
1244
+ };
1245
+
1246
+ // src/index.ts
1247
+ var PrimusFund = class {
1248
+ supportedChainIds = SUPPORTEDCHAINIDS;
1249
+ supportedSocialPlatforms = SUPPORTEDSOCIALPLATFORMS;
1250
+ _fund;
1251
+ async init(provider, chainId, appId) {
1252
+ return new Promise(async (resolve, reject) => {
1253
+ try {
1254
+ if (!this.supportedChainIds.includes(chainId)) {
1255
+ return reject("chainId is not supported");
1256
+ }
1257
+ this._fund = new Fund();
1258
+ const result = await this._fund.init(new ethers3.providers.Web3Provider(provider), chainId, appId);
1259
+ return resolve(result);
1260
+ } catch (error) {
1261
+ return reject(error);
1262
+ }
1263
+ });
1264
+ }
1265
+ async fund(fundParam) {
1266
+ return new Promise(async (resolve, reject) => {
1267
+ try {
1268
+ const { tokenInfo, recipientInfos } = fundParam;
1269
+ if (!recipientInfos || recipientInfos.length === 0) {
1270
+ return reject("recipientInfos is empty");
1271
+ }
1272
+ const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
1273
+ if (hasUnsupportedSocialPlatforms) {
1274
+ return reject("socialPlatform is not supported");
1275
+ }
1276
+ if (tokenInfo.tokenType === 1) {
1277
+ tokenInfo.tokenAddress = ethers3.constants.AddressZero;
1278
+ }
1279
+ const newFundRecipientInfos = recipientInfos.map((i) => {
1280
+ i.nftIds = [];
1281
+ i.socialPlatform = i.socialPlatform.toLowerCase();
1282
+ return i;
1283
+ });
1284
+ if (recipientInfos.length === 1) {
1285
+ const result = await this._fund?.fund(tokenInfo, newFundRecipientInfos[0]);
1286
+ return resolve(result);
1287
+ } else if (recipientInfos.length > 1) {
1288
+ const result = await this._fund?.fundBatch(tokenInfo, newFundRecipientInfos);
1289
+ return resolve(result);
1290
+ }
1291
+ } catch (error) {
1292
+ return reject(error);
1293
+ }
1294
+ });
1295
+ }
1296
+ async refund(recipients) {
1297
+ return new Promise(async (resolve, reject) => {
1298
+ try {
1299
+ if (!recipients || recipients.length === 0) {
1300
+ return reject("recipients is empty");
1301
+ }
1302
+ const newRecipients = recipients.map((i) => {
1303
+ i.socialPlatform = i.socialPlatform.toLowerCase();
1304
+ return i;
1305
+ });
1306
+ const result = await this._fund?.refund(newRecipients);
1307
+ return resolve(result);
1308
+ } catch (error) {
1309
+ return reject(error);
1310
+ }
1311
+ });
1312
+ }
1313
+ async attest(socialPlatform, address, genAppSignature) {
1314
+ return new Promise(async (resolve, reject) => {
1315
+ try {
1316
+ const lcSocialPlatform = socialPlatform.toLowerCase();
1317
+ const attestation = await this._fund?.attest(lcSocialPlatform, address, genAppSignature);
1318
+ return resolve(attestation);
1319
+ } catch (error) {
1320
+ return reject(error);
1321
+ }
1322
+ });
1323
+ }
1324
+ async claim(claimParams) {
1325
+ const claimParamList = Array.isArray(claimParams) ? claimParams : [claimParams];
1326
+ return new Promise(async (resolve, reject) => {
1327
+ if (!claimParamList || claimParamList?.length === 0) {
1328
+ const error = new Error("claimParams is empty");
1329
+ return reject(error);
1330
+ }
1331
+ const socialPlatforms = [];
1332
+ const userIdentifiers = [];
1333
+ const attestations = [];
1334
+ for (let i = 0; i < claimParamList.length; i++) {
1335
+ socialPlatforms[i] = claimParamList[i].socialPlatform.toLowerCase();
1336
+ attestations[i] = claimParamList[i].attestation;
1337
+ userIdentifiers[i] = claimParamList[i].userIdentifier;
1338
+ }
1339
+ if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestations.length) {
1340
+ return reject(`claimParamList is wrong`);
1341
+ }
1342
+ try {
1343
+ if (socialPlatforms.length === 1) {
1344
+ const result = await this._fund?.claimBySource(socialPlatforms[0], userIdentifiers[0], attestations[0]);
1345
+ resolve(result);
1346
+ } else if (socialPlatforms.length > 1) {
1347
+ const result = await this._fund?.claimByMultiSource(socialPlatforms, userIdentifiers, attestations);
1348
+ return resolve(result);
1349
+ }
1350
+ } catch (error) {
1351
+ return reject(error);
1352
+ }
1353
+ });
1354
+ }
1355
+ };
1356
+ export {
1357
+ PrimusFund
1358
+ };