@primuslabs/fund-js-sdk 0.1.3 → 0.1.5

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.d.mts CHANGED
@@ -43,7 +43,9 @@ type RecipientBaseInfo = {
43
43
  socialPlatform: string;// The name of the social platform.
44
44
  userIdentifier: string;// The user’s unique identifier on the platforms.
45
45
  }
46
-
46
+ type RefundParam = RecipientBaseInfo &{
47
+ tipTimestamp?: number; // unit: s
48
+ }
47
49
  type RecipientInfo = RecipientBaseInfo & {
48
50
  tokenAmount: string;// The amount of the token
49
51
  nftIds?: bigint[] | [];// The nft token ids when token is nft.
@@ -65,4 +67,4 @@ type AttestParams = {
65
67
  address: string
66
68
  }
67
69
 
68
- export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
70
+ export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, RefundParam, TokenInfo, TokenType };
package/dist/index.d.ts CHANGED
@@ -43,7 +43,9 @@ type RecipientBaseInfo = {
43
43
  socialPlatform: string;// The name of the social platform.
44
44
  userIdentifier: string;// The user’s unique identifier on the platforms.
45
45
  }
46
-
46
+ type RefundParam = RecipientBaseInfo &{
47
+ tipTimestamp?: number; // unit: s
48
+ }
47
49
  type RecipientInfo = RecipientBaseInfo & {
48
50
  tokenAmount: string;// The amount of the token
49
51
  nftIds?: bigint[] | [];// The nft token ids when token is nft.
@@ -65,4 +67,4 @@ type AttestParams = {
65
67
  address: string
66
68
  }
67
69
 
68
- export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
70
+ export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, RefundParam, TokenInfo, TokenType };
package/dist/index.js CHANGED
@@ -42,6 +42,10 @@ var DATASOURCETEMPLATESMAP = {
42
42
  "google account": {
43
43
  id: "3bad8a55-4415-4bec-9b47-a4c7bbe93518",
44
44
  field: "2"
45
+ },
46
+ xiaohongshu: {
47
+ id: "93c6e6df-63ab-41af-8cba-f2927c0d2f1c",
48
+ field: "red_id"
45
49
  }
46
50
  };
47
51
  var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATESMAP);
@@ -80,6 +84,16 @@ var SUPPORTEDCHAINIDS = Object.keys(Fund_CONTRACTS).map((i) => Number(i));
80
84
 
81
85
  // src/classes/Contract.ts
82
86
  var import_ethers = require("ethers");
87
+
88
+ // src/utils/utils.ts
89
+ var hasErrorFlagFn = (curErrorArr, targetErrorStrArr) => {
90
+ return curErrorArr.some((curErrorStr) => {
91
+ let f = targetErrorStrArr.some((targetErrorStr) => curErrorStr.toLowerCase().includes(targetErrorStr.toLowerCase()));
92
+ return f;
93
+ });
94
+ };
95
+
96
+ // src/classes/Contract.ts
83
97
  var Contract = class {
84
98
  address;
85
99
  provider;
@@ -124,24 +138,31 @@ var Contract = class {
124
138
  return reject(`Method ${functionName} does not exist on the contract`);
125
139
  }
126
140
  try {
127
- console.log("sendTransaction params:", ...functionParams);
141
+ console.log("sendTransaction params:", functionName, ...functionParams);
128
142
  const tx = await this.contractInstance[functionName](...functionParams);
129
143
  const txreceipt = await tx.wait();
130
144
  console.log("txreceipt", txreceipt);
131
145
  resolve(txreceipt);
132
146
  } catch (error) {
133
147
  console.log("sendTransaction error:", error);
134
- const errStr = error?.toString()?.toLowerCase() || "";
148
+ const errStr = error?.message || error?.toString()?.toLowerCase() || "";
149
+ const errorMsg1 = typeof error === "string" ? error : error instanceof Error ? error.message : typeof error.message === "string" ? error.message : JSON.stringify(error);
150
+ const errorMsg2 = typeof error === "object" ? JSON.stringify(error) : error?.toString();
151
+ const curErrorStrArr = [errorMsg1, errorMsg2];
135
152
  const userRejectErrStrArr = ["user rejected", "approval denied"];
136
- const isUserRejected = userRejectErrStrArr.some((str) => errStr.indexOf(str) > -1);
153
+ const isUserRejected = hasErrorFlagFn(curErrorStrArr, userRejectErrStrArr);
137
154
  if (error?.code === "ACTION_REJECTED" || isUserRejected) {
138
155
  return reject("user rejected transaction");
139
156
  }
140
157
  const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
141
- const isInsufficientBalance = insufficientBalanceErrStrArr.some((str) => errStr.indexOf(str) > -1);
158
+ const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
142
159
  if (isInsufficientBalance) {
143
160
  return reject("insufficient balance");
144
161
  }
162
+ const isNoPendingWithdrawals = hasErrorFlagFn(curErrorStrArr, ["no pending withdrawals"]);
163
+ if (isNoPendingWithdrawals) {
164
+ return reject("no pending withdrawals");
165
+ }
145
166
  return reject(error);
146
167
  }
147
168
  });
@@ -599,10 +620,11 @@ var abi_default = [
599
620
  {
600
621
  name: "tipRecipients",
601
622
  type: "tuple[]",
602
- internalType: "struct TipRecipient[]",
623
+ internalType: "struct TipWithdrawInfo[]",
603
624
  components: [
604
625
  { name: "idSource", type: "string", internalType: "string" },
605
- { name: "id", type: "string", internalType: "string" }
626
+ { name: "id", type: "string", internalType: "string" },
627
+ { name: "tipTimestamp", type: "uint64", internalType: "uint64" }
606
628
  ]
607
629
  }
608
630
  ],
@@ -635,6 +657,12 @@ var abi_default = [
635
657
  indexed: true,
636
658
  internalType: "address"
637
659
  },
660
+ {
661
+ name: "claimTime",
662
+ type: "uint64",
663
+ indexed: false,
664
+ internalType: "uint64"
665
+ },
638
666
  {
639
667
  name: "idSource",
640
668
  type: "string",
@@ -664,6 +692,12 @@ var abi_default = [
664
692
  type: "uint256",
665
693
  indexed: false,
666
694
  internalType: "uint256"
695
+ },
696
+ {
697
+ name: "tipTime",
698
+ type: "uint64",
699
+ indexed: false,
700
+ internalType: "uint64"
667
701
  }
668
702
  ],
669
703
  anonymous: false
@@ -721,6 +755,24 @@ var abi_default = [
721
755
  type: "address",
722
756
  indexed: false,
723
757
  internalType: "address"
758
+ },
759
+ {
760
+ name: "tokenAddr",
761
+ type: "address",
762
+ indexed: false,
763
+ internalType: "address"
764
+ },
765
+ {
766
+ name: "amount",
767
+ type: "uint256",
768
+ indexed: false,
769
+ internalType: "uint256"
770
+ },
771
+ {
772
+ name: "tipTime",
773
+ type: "uint64",
774
+ indexed: false,
775
+ internalType: "uint64"
724
776
  }
725
777
  ],
726
778
  anonymous: false
@@ -729,16 +781,34 @@ var abi_default = [
729
781
  type: "event",
730
782
  name: "WithdrawEvent",
731
783
  inputs: [
784
+ {
785
+ name: "withdrawTime",
786
+ type: "uint64",
787
+ indexed: false,
788
+ internalType: "uint64"
789
+ },
790
+ {
791
+ name: "idSource",
792
+ type: "string",
793
+ indexed: false,
794
+ internalType: "string"
795
+ },
796
+ {
797
+ name: "id",
798
+ type: "string",
799
+ indexed: false,
800
+ internalType: "string"
801
+ },
732
802
  {
733
803
  name: "tipper",
734
804
  type: "address",
735
- indexed: true,
805
+ indexed: false,
736
806
  internalType: "address"
737
807
  },
738
808
  {
739
809
  name: "tokenAddr",
740
810
  type: "address",
741
- indexed: true,
811
+ indexed: false,
742
812
  internalType: "address"
743
813
  },
744
814
  {
@@ -746,6 +816,12 @@ var abi_default = [
746
816
  type: "uint256",
747
817
  indexed: false,
748
818
  internalType: "uint256"
819
+ },
820
+ {
821
+ name: "tipTime",
822
+ type: "uint64",
823
+ indexed: false,
824
+ internalType: "uint64"
749
825
  }
750
826
  ],
751
827
  anonymous: false
@@ -1141,7 +1217,8 @@ var Fund = class {
1141
1217
  let params = [];
1142
1218
  if (tokenInfo.tokenType === 0) {
1143
1219
  await this.approve(tokenInfo, recipientInfos);
1144
- const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1220
+ const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
1221
+ const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
1145
1222
  decimals = await erc20Contract.decimals();
1146
1223
  }
1147
1224
  const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
@@ -1169,7 +1246,8 @@ var Fund = class {
1169
1246
  const newRecipients = recipients.map((i) => {
1170
1247
  return {
1171
1248
  idSource: i.socialPlatform,
1172
- id: i.userIdentifier
1249
+ id: i.userIdentifier,
1250
+ tipTimestamp: i.tipTimestamp
1173
1251
  };
1174
1252
  });
1175
1253
  const result = await this.fundContract.sendTransaction("tipperWithdraw", [newRecipients]);
@@ -1186,7 +1264,8 @@ var Fund = class {
1186
1264
  let params = [];
1187
1265
  if (tokenInfo.tokenType === 0) {
1188
1266
  await this.approve(tokenInfo, recipientInfoList);
1189
- const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1267
+ const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
1268
+ const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
1190
1269
  decimals = await erc20Contract.decimals();
1191
1270
  }
1192
1271
  let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
@@ -1214,7 +1293,8 @@ var Fund = class {
1214
1293
  async approve(tokenInfo, recipientInfoList) {
1215
1294
  return new Promise(async (resolve, reject) => {
1216
1295
  try {
1217
- const signer = this.provider.getSigner();
1296
+ const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
1297
+ const signer = web3Provider.getSigner();
1218
1298
  const address = await signer.getAddress();
1219
1299
  const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
1220
1300
  const allowance = await erc20Contract.allowance(address, this.fundContract.address);
@@ -1348,7 +1428,13 @@ var Fund = class {
1348
1428
  let decimals = 18;
1349
1429
  let symbol = "";
1350
1430
  if (tokenType === 0) {
1351
- const erc20Contract = new import_ethers2.ethers.Contract(tokenAddress, erc20Abi_default, this.provider);
1431
+ let formatProvider;
1432
+ if (this.provider instanceof import_ethers2.ethers.providers.JsonRpcProvider) {
1433
+ formatProvider = this.provider;
1434
+ } else {
1435
+ formatProvider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
1436
+ }
1437
+ const erc20Contract = new import_ethers2.ethers.Contract(tokenAddress, erc20Abi_default, formatProvider);
1352
1438
  decimals = await erc20Contract.decimals();
1353
1439
  symbol = await erc20Contract.symbol();
1354
1440
  } else if (tokenType === 1) {
package/dist/index.mjs CHANGED
@@ -18,6 +18,10 @@ var DATASOURCETEMPLATESMAP = {
18
18
  "google account": {
19
19
  id: "3bad8a55-4415-4bec-9b47-a4c7bbe93518",
20
20
  field: "2"
21
+ },
22
+ xiaohongshu: {
23
+ id: "93c6e6df-63ab-41af-8cba-f2927c0d2f1c",
24
+ field: "red_id"
21
25
  }
22
26
  };
23
27
  var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATESMAP);
@@ -56,6 +60,16 @@ var SUPPORTEDCHAINIDS = Object.keys(Fund_CONTRACTS).map((i) => Number(i));
56
60
 
57
61
  // src/classes/Contract.ts
58
62
  import { ethers } from "ethers";
63
+
64
+ // src/utils/utils.ts
65
+ var hasErrorFlagFn = (curErrorArr, targetErrorStrArr) => {
66
+ return curErrorArr.some((curErrorStr) => {
67
+ let f = targetErrorStrArr.some((targetErrorStr) => curErrorStr.toLowerCase().includes(targetErrorStr.toLowerCase()));
68
+ return f;
69
+ });
70
+ };
71
+
72
+ // src/classes/Contract.ts
59
73
  var Contract = class {
60
74
  address;
61
75
  provider;
@@ -100,24 +114,31 @@ var Contract = class {
100
114
  return reject(`Method ${functionName} does not exist on the contract`);
101
115
  }
102
116
  try {
103
- console.log("sendTransaction params:", ...functionParams);
117
+ console.log("sendTransaction params:", functionName, ...functionParams);
104
118
  const tx = await this.contractInstance[functionName](...functionParams);
105
119
  const txreceipt = await tx.wait();
106
120
  console.log("txreceipt", txreceipt);
107
121
  resolve(txreceipt);
108
122
  } catch (error) {
109
123
  console.log("sendTransaction error:", error);
110
- const errStr = error?.toString()?.toLowerCase() || "";
124
+ const errStr = error?.message || error?.toString()?.toLowerCase() || "";
125
+ const errorMsg1 = typeof error === "string" ? error : error instanceof Error ? error.message : typeof error.message === "string" ? error.message : JSON.stringify(error);
126
+ const errorMsg2 = typeof error === "object" ? JSON.stringify(error) : error?.toString();
127
+ const curErrorStrArr = [errorMsg1, errorMsg2];
111
128
  const userRejectErrStrArr = ["user rejected", "approval denied"];
112
- const isUserRejected = userRejectErrStrArr.some((str) => errStr.indexOf(str) > -1);
129
+ const isUserRejected = hasErrorFlagFn(curErrorStrArr, userRejectErrStrArr);
113
130
  if (error?.code === "ACTION_REJECTED" || isUserRejected) {
114
131
  return reject("user rejected transaction");
115
132
  }
116
133
  const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
117
- const isInsufficientBalance = insufficientBalanceErrStrArr.some((str) => errStr.indexOf(str) > -1);
134
+ const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
118
135
  if (isInsufficientBalance) {
119
136
  return reject("insufficient balance");
120
137
  }
138
+ const isNoPendingWithdrawals = hasErrorFlagFn(curErrorStrArr, ["no pending withdrawals"]);
139
+ if (isNoPendingWithdrawals) {
140
+ return reject("no pending withdrawals");
141
+ }
121
142
  return reject(error);
122
143
  }
123
144
  });
@@ -575,10 +596,11 @@ var abi_default = [
575
596
  {
576
597
  name: "tipRecipients",
577
598
  type: "tuple[]",
578
- internalType: "struct TipRecipient[]",
599
+ internalType: "struct TipWithdrawInfo[]",
579
600
  components: [
580
601
  { name: "idSource", type: "string", internalType: "string" },
581
- { name: "id", type: "string", internalType: "string" }
602
+ { name: "id", type: "string", internalType: "string" },
603
+ { name: "tipTimestamp", type: "uint64", internalType: "uint64" }
582
604
  ]
583
605
  }
584
606
  ],
@@ -611,6 +633,12 @@ var abi_default = [
611
633
  indexed: true,
612
634
  internalType: "address"
613
635
  },
636
+ {
637
+ name: "claimTime",
638
+ type: "uint64",
639
+ indexed: false,
640
+ internalType: "uint64"
641
+ },
614
642
  {
615
643
  name: "idSource",
616
644
  type: "string",
@@ -640,6 +668,12 @@ var abi_default = [
640
668
  type: "uint256",
641
669
  indexed: false,
642
670
  internalType: "uint256"
671
+ },
672
+ {
673
+ name: "tipTime",
674
+ type: "uint64",
675
+ indexed: false,
676
+ internalType: "uint64"
643
677
  }
644
678
  ],
645
679
  anonymous: false
@@ -697,6 +731,24 @@ var abi_default = [
697
731
  type: "address",
698
732
  indexed: false,
699
733
  internalType: "address"
734
+ },
735
+ {
736
+ name: "tokenAddr",
737
+ type: "address",
738
+ indexed: false,
739
+ internalType: "address"
740
+ },
741
+ {
742
+ name: "amount",
743
+ type: "uint256",
744
+ indexed: false,
745
+ internalType: "uint256"
746
+ },
747
+ {
748
+ name: "tipTime",
749
+ type: "uint64",
750
+ indexed: false,
751
+ internalType: "uint64"
700
752
  }
701
753
  ],
702
754
  anonymous: false
@@ -705,16 +757,34 @@ var abi_default = [
705
757
  type: "event",
706
758
  name: "WithdrawEvent",
707
759
  inputs: [
760
+ {
761
+ name: "withdrawTime",
762
+ type: "uint64",
763
+ indexed: false,
764
+ internalType: "uint64"
765
+ },
766
+ {
767
+ name: "idSource",
768
+ type: "string",
769
+ indexed: false,
770
+ internalType: "string"
771
+ },
772
+ {
773
+ name: "id",
774
+ type: "string",
775
+ indexed: false,
776
+ internalType: "string"
777
+ },
708
778
  {
709
779
  name: "tipper",
710
780
  type: "address",
711
- indexed: true,
781
+ indexed: false,
712
782
  internalType: "address"
713
783
  },
714
784
  {
715
785
  name: "tokenAddr",
716
786
  type: "address",
717
- indexed: true,
787
+ indexed: false,
718
788
  internalType: "address"
719
789
  },
720
790
  {
@@ -722,6 +792,12 @@ var abi_default = [
722
792
  type: "uint256",
723
793
  indexed: false,
724
794
  internalType: "uint256"
795
+ },
796
+ {
797
+ name: "tipTime",
798
+ type: "uint64",
799
+ indexed: false,
800
+ internalType: "uint64"
725
801
  }
726
802
  ],
727
803
  anonymous: false
@@ -1117,7 +1193,8 @@ var Fund = class {
1117
1193
  let params = [];
1118
1194
  if (tokenInfo.tokenType === 0) {
1119
1195
  await this.approve(tokenInfo, recipientInfos);
1120
- const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1196
+ const web3Provider = new ethers2.providers.Web3Provider(this.provider);
1197
+ const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
1121
1198
  decimals = await erc20Contract.decimals();
1122
1199
  }
1123
1200
  const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
@@ -1145,7 +1222,8 @@ var Fund = class {
1145
1222
  const newRecipients = recipients.map((i) => {
1146
1223
  return {
1147
1224
  idSource: i.socialPlatform,
1148
- id: i.userIdentifier
1225
+ id: i.userIdentifier,
1226
+ tipTimestamp: i.tipTimestamp
1149
1227
  };
1150
1228
  });
1151
1229
  const result = await this.fundContract.sendTransaction("tipperWithdraw", [newRecipients]);
@@ -1162,7 +1240,8 @@ var Fund = class {
1162
1240
  let params = [];
1163
1241
  if (tokenInfo.tokenType === 0) {
1164
1242
  await this.approve(tokenInfo, recipientInfoList);
1165
- const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1243
+ const web3Provider = new ethers2.providers.Web3Provider(this.provider);
1244
+ const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
1166
1245
  decimals = await erc20Contract.decimals();
1167
1246
  }
1168
1247
  let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
@@ -1190,7 +1269,8 @@ var Fund = class {
1190
1269
  async approve(tokenInfo, recipientInfoList) {
1191
1270
  return new Promise(async (resolve, reject) => {
1192
1271
  try {
1193
- const signer = this.provider.getSigner();
1272
+ const web3Provider = new ethers2.providers.Web3Provider(this.provider);
1273
+ const signer = web3Provider.getSigner();
1194
1274
  const address = await signer.getAddress();
1195
1275
  const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
1196
1276
  const allowance = await erc20Contract.allowance(address, this.fundContract.address);
@@ -1324,7 +1404,13 @@ var Fund = class {
1324
1404
  let decimals = 18;
1325
1405
  let symbol = "";
1326
1406
  if (tokenType === 0) {
1327
- const erc20Contract = new ethers2.Contract(tokenAddress, erc20Abi_default, this.provider);
1407
+ let formatProvider;
1408
+ if (this.provider instanceof ethers2.providers.JsonRpcProvider) {
1409
+ formatProvider = this.provider;
1410
+ } else {
1411
+ formatProvider = new ethers2.providers.Web3Provider(this.provider);
1412
+ }
1413
+ const erc20Contract = new ethers2.Contract(tokenAddress, erc20Abi_default, formatProvider);
1328
1414
  decimals = await erc20Contract.decimals();
1329
1415
  symbol = await erc20Contract.symbol();
1330
1416
  } else if (tokenType === 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primuslabs/fund-js-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "author": "Primus Labs <dev@primuslabs.org>",
5
5
  "description": "Primus fund js sdk",
6
6
  "repository": {