@primuslabs/fund-js-sdk 0.1.0 → 0.1.1

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 CHANGED
@@ -7,4 +7,4 @@ For details, please refer to the following links:
7
7
  - [Overview](./README/overview.md)
8
8
  - [How to intall the SDK](./README/install.md)
9
9
  - [Example: Funding Tokens](./README/example.md)
10
- - [Receiver Guide](./README/receiver-guide.md)
10
+ - [Receiver Guide: Claiming Tokens](./README/receiver-guide.md)
package/dist/index.d.mts CHANGED
@@ -46,7 +46,7 @@ type RecipientBaseInfo = {
46
46
 
47
47
  type RecipientInfo = RecipientBaseInfo & {
48
48
  tokenAmount: string;// The amount of the token
49
- nftIds?: bigint[];// The nft token ids when token is nft.
49
+ nftIds?: bigint[] | [];// The nft token ids when token is nft.
50
50
  }
51
51
 
52
52
  type FundParam = {
@@ -59,4 +59,10 @@ type ClaimParam = RecipientBaseInfo & {
59
59
  fundIndex?: number;
60
60
  }
61
61
 
62
- export type { AttNetworkRequest, AttNetworkResponseResolve, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
62
+ type AttestParams = {
63
+ socialPlatform: string;
64
+ userIdentifier: string;
65
+ address: string
66
+ }
67
+
68
+ export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
package/dist/index.d.ts CHANGED
@@ -46,7 +46,7 @@ type RecipientBaseInfo = {
46
46
 
47
47
  type RecipientInfo = RecipientBaseInfo & {
48
48
  tokenAmount: string;// The amount of the token
49
- nftIds?: bigint[];// The nft token ids when token is nft.
49
+ nftIds?: bigint[] | [];// The nft token ids when token is nft.
50
50
  }
51
51
 
52
52
  type FundParam = {
@@ -59,4 +59,10 @@ type ClaimParam = RecipientBaseInfo & {
59
59
  fundIndex?: number;
60
60
  }
61
61
 
62
- export type { AttNetworkRequest, AttNetworkResponseResolve, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
62
+ type AttestParams = {
63
+ socialPlatform: string;
64
+ userIdentifier: string;
65
+ address: string
66
+ }
67
+
68
+ export type { AttNetworkRequest, AttNetworkResponseResolve, AttestParams, Attestation, Attestor, ClaimParam, FundParam, RecipientBaseInfo, RecipientInfo, TokenInfo, TokenType };
package/dist/index.js CHANGED
@@ -30,17 +30,53 @@ var import_ethers2 = require("ethers");
30
30
  var import_zktls_js_sdk = require("@primuslabs/zktls-js-sdk");
31
31
 
32
32
  // src/config/constants.ts
33
- var Fund_CONTRACTS = {
33
+ var DATASOURCETEMPLATESMAP = {
34
+ x: {
35
+ id: "2e3160ae-8b1e-45e3-8c59-426366278b9d",
36
+ field: "screen_name"
37
+ },
38
+ tiktok: {
39
+ id: "2b22c9f8-686d-4482-a0cf-c9c43c1181ad",
40
+ field: "username"
41
+ },
42
+ "google account": {
43
+ id: "3bad8a55-4415-4bec-9b47-a4c7bbe93518",
44
+ field: "2"
45
+ }
46
+ };
47
+ var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATESMAP);
48
+ var SUPPORTEDCHAINIDSMAP = {
49
+ 10143: {
50
+ chainId: 10143,
51
+ chainName: "Monad testnet",
52
+ nativeCurrency: {
53
+ name: "MON",
54
+ symbol: "MON",
55
+ decimals: 18
56
+ },
57
+ contractAddress: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
58
+ }
34
59
  // monad testnet
35
- 10143: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
36
60
  };
61
+ var NATIVETOKENS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
62
+ return {
63
+ ...prev,
64
+ [curr.chainId]: curr.nativeCurrency.symbol
65
+ };
66
+ }, {});
67
+ var CHAINNAMES = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
68
+ return {
69
+ ...prev,
70
+ [curr.chainId]: curr.chainName
71
+ };
72
+ }, {});
73
+ var Fund_CONTRACTS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
74
+ return {
75
+ ...prev,
76
+ [curr.chainId]: curr.contractAddress
77
+ };
78
+ }, {});
37
79
  var SUPPORTEDCHAINIDS = Object.keys(Fund_CONTRACTS).map((i) => Number(i));
38
- var DATASOURCETEMPLATEMAP = {
39
- x: "2e3160ae-8b1e-45e3-8c59-426366278b9d",
40
- tiktok: "2b22c9f8-686d-4482-a0cf-c9c43c1181ad",
41
- "google account": ""
42
- };
43
- var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATEMAP);
44
80
 
45
81
  // src/classes/Contract.ts
46
82
  var import_ethers = require("ethers");
@@ -1037,12 +1073,14 @@ var erc20Abi_default = [
1037
1073
  ];
1038
1074
 
1039
1075
  // src/classes/Fund.ts
1076
+ var { parseUnits, formatUnits } = import_ethers2.ethers.utils;
1040
1077
  var Fund = class {
1041
1078
  _attestLoading;
1042
1079
  zkTlsSdk;
1043
1080
  fundContract;
1044
1081
  provider;
1045
- _dataSourceTemplateMap = DATASOURCETEMPLATEMAP;
1082
+ chainId;
1083
+ _dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
1046
1084
  constructor() {
1047
1085
  this._attestLoading = false;
1048
1086
  }
@@ -1064,6 +1102,7 @@ var Fund = class {
1064
1102
  }
1065
1103
  this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
1066
1104
  this.provider = provider;
1105
+ this.chainId = chainId;
1067
1106
  if (appId) {
1068
1107
  this.zkTlsSdk = new import_zktls_js_sdk.PrimusZKTLS();
1069
1108
  const extensionVersion = await this.zkTlsSdk.init(
@@ -1090,7 +1129,7 @@ var Fund = class {
1090
1129
  const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1091
1130
  decimals = await erc20Contract.decimals();
1092
1131
  }
1093
- const tokenAmount = import_ethers2.ethers.utils.parseUnits(recipientInfo.tokenAmount.toString(), decimals);
1132
+ const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
1094
1133
  const newFundRecipientInfo = {
1095
1134
  idSource: recipientInfo.socialPlatform,
1096
1135
  id: recipientInfo.userIdentifier,
@@ -1135,9 +1174,9 @@ var Fund = class {
1135
1174
  const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1136
1175
  decimals = await erc20Contract.decimals();
1137
1176
  }
1138
- let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(import_ethers2.ethers.utils.parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
1177
+ let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
1139
1178
  const newRecipientInfoList = recipientInfoList.map((i) => {
1140
- const formatAmount = import_ethers2.ethers.utils.parseUnits(i.tokenAmount.toString(), decimals);
1179
+ const formatAmount = parseUnits(i.tokenAmount.toString(), decimals);
1141
1180
  return {
1142
1181
  idSource: i.socialPlatform,
1143
1182
  id: i.userIdentifier,
@@ -1165,7 +1204,7 @@ var Fund = class {
1165
1204
  const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
1166
1205
  const allowance = await erc20Contract.allowance(address, this.fundContract.address);
1167
1206
  const decimals = await erc20Contract.decimals();
1168
- const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(import_ethers2.ethers.utils.parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
1207
+ const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
1169
1208
  if (allowance.lt(requiredAllowance)) {
1170
1209
  const tx = await erc20Contract.approve(this.fundContract.address, requiredAllowance);
1171
1210
  await tx.wait();
@@ -1183,7 +1222,7 @@ var Fund = class {
1183
1222
  }
1184
1223
  });
1185
1224
  }
1186
- async attest(socialPlatform, address, genAppSignature) {
1225
+ async attest(attestParams, genAppSignature) {
1187
1226
  return new Promise(async (resolve, reject) => {
1188
1227
  if (!this.zkTlsSdk?.padoExtensionVersion) {
1189
1228
  return reject(`Uninitialized!`);
@@ -1191,15 +1230,26 @@ var Fund = class {
1191
1230
  if (this._attestLoading) {
1192
1231
  return reject(`Under proof!`);
1193
1232
  }
1233
+ const { socialPlatform, userIdentifier, address } = attestParams;
1194
1234
  this._attestLoading = true;
1195
- const templateId = this._dataSourceTemplateMap[socialPlatform];
1235
+ const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
1196
1236
  const attRequest = this.zkTlsSdk.generateRequestParams(
1197
1237
  templateId,
1198
1238
  address
1199
1239
  );
1240
+ attRequest.setAttConditions([
1241
+ [
1242
+ {
1243
+ field,
1244
+ op: "STREQ",
1245
+ value: userIdentifier
1246
+ }
1247
+ ]
1248
+ ]);
1200
1249
  const signParams = attRequest.toJsonString();
1201
1250
  const signature = await genAppSignature(signParams);
1202
1251
  if (!signature) {
1252
+ this._attestLoading = false;
1203
1253
  return reject(`appSignature is empty!`);
1204
1254
  }
1205
1255
  try {
@@ -1213,8 +1263,9 @@ var Fund = class {
1213
1263
  JSON.stringify(formatAttestParams)
1214
1264
  );
1215
1265
  this._attestLoading = false;
1216
- resolve(attestation);
1266
+ return resolve(attestation);
1217
1267
  } catch (error) {
1268
+ this._attestLoading = false;
1218
1269
  return reject(error);
1219
1270
  }
1220
1271
  });
@@ -1226,7 +1277,7 @@ var Fund = class {
1226
1277
  const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: socialPlatform, id: userIdentifier }]);
1227
1278
  console.log("fundRecords", fundRecords);
1228
1279
  const recordCount = fundRecords.length;
1229
- if (fundRecords <= 0) {
1280
+ if (recordCount <= 0) {
1230
1281
  return reject(`No fund records.`);
1231
1282
  } else {
1232
1283
  const totalFee = claimFee.mul(recordCount);
@@ -1265,6 +1316,53 @@ var Fund = class {
1265
1316
  }
1266
1317
  });
1267
1318
  }
1319
+ async getTipRecords(getFundRecordsParams) {
1320
+ return new Promise(async (resolve, reject) => {
1321
+ try {
1322
+ const formatGetFundRecordsParams = getFundRecordsParams.map((item) => {
1323
+ return {
1324
+ idSource: item.socialPlatform,
1325
+ id: item.userIdentifier
1326
+ };
1327
+ });
1328
+ const fundRecords = await this.fundContract.callMethod("getTipRecords", formatGetFundRecordsParams);
1329
+ console.log("fundRecords", fundRecords);
1330
+ let formatRecords = [];
1331
+ for (const record of fundRecords) {
1332
+ const { tipper, timestamp, tipToken: [tokenType, tokenAddress], amount } = record;
1333
+ let decimals = 18;
1334
+ let symbol = "";
1335
+ if (tokenType === 0) {
1336
+ const erc20Contract = new import_ethers2.ethers.Contract(tokenAddress, erc20Abi_default, this.provider);
1337
+ decimals = await erc20Contract.decimals();
1338
+ symbol = await erc20Contract.symbol();
1339
+ } else if (tokenType === 1) {
1340
+ symbol = NATIVETOKENS[this.chainId];
1341
+ }
1342
+ let fundToken = {
1343
+ tokenType,
1344
+ // tokenAmount: formatUnits(amount, decimals),
1345
+ decimals,
1346
+ symbol,
1347
+ chainName: CHAINNAMES[this.chainId]
1348
+ };
1349
+ if (tokenType === 0) {
1350
+ fundToken.tokenAddress = tokenAddress;
1351
+ }
1352
+ formatRecords.push({
1353
+ funder: tipper,
1354
+ fundToken,
1355
+ amount: formatUnits(amount, decimals),
1356
+ timestamp: timestamp.toNumber() * 1e3
1357
+ });
1358
+ }
1359
+ console.log("formatRecords", formatRecords);
1360
+ return resolve(formatRecords);
1361
+ } catch (error) {
1362
+ return reject(error);
1363
+ }
1364
+ });
1365
+ }
1268
1366
  };
1269
1367
 
1270
1368
  // src/index.ts
@@ -1279,7 +1377,13 @@ var PrimusFund = class {
1279
1377
  return reject("chainId is not supported");
1280
1378
  }
1281
1379
  this._fund = new Fund();
1282
- const result = await this._fund.init(new import_ethers3.ethers.providers.Web3Provider(provider), chainId, appId);
1380
+ let formatProvider;
1381
+ if (provider instanceof import_ethers3.ethers.providers.JsonRpcProvider) {
1382
+ formatProvider = new import_ethers3.ethers.providers.JsonRpcProvider(defaultRpcUrl);
1383
+ } else {
1384
+ formatProvider = new import_ethers3.ethers.providers.Web3Provider(provider);
1385
+ }
1386
+ const result = await this._fund.init(formatProvider, chainId, appId);
1283
1387
  return resolve(result);
1284
1388
  } catch (error) {
1285
1389
  return reject(error);
@@ -1301,9 +1405,17 @@ var PrimusFund = class {
1301
1405
  tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
1302
1406
  }
1303
1407
  const newFundRecipientInfos = recipientInfos.map((i) => {
1304
- i.nftIds = [];
1305
- i.socialPlatform = i.socialPlatform.toLowerCase();
1306
- return i;
1408
+ const formatSocialPlatform = i.socialPlatform.toLowerCase();
1409
+ let formatUserIdentifier = i.userIdentifier;
1410
+ if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
1411
+ formatUserIdentifier = i.userIdentifier.slice(1);
1412
+ }
1413
+ return {
1414
+ nftIds: [],
1415
+ socialPlatform: formatSocialPlatform,
1416
+ userIdentifier: formatUserIdentifier,
1417
+ tokenAmount: i.tokenAmount
1418
+ };
1307
1419
  });
1308
1420
  if (recipientInfos.length === 1) {
1309
1421
  const result = await this._fund?.fund(tokenInfo, newFundRecipientInfos[0]);
@@ -1325,6 +1437,9 @@ var PrimusFund = class {
1325
1437
  }
1326
1438
  const newRecipients = recipients.map((i) => {
1327
1439
  i.socialPlatform = i.socialPlatform.toLowerCase();
1440
+ if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
1441
+ i.userIdentifier = i.userIdentifier.slice(1);
1442
+ }
1328
1443
  return i;
1329
1444
  });
1330
1445
  const result = await this._fund?.refund(newRecipients);
@@ -1334,11 +1449,15 @@ var PrimusFund = class {
1334
1449
  }
1335
1450
  });
1336
1451
  }
1337
- async attest(socialPlatform, address, genAppSignature) {
1452
+ async attest(attestParams, genAppSignature) {
1338
1453
  return new Promise(async (resolve, reject) => {
1339
1454
  try {
1455
+ const { socialPlatform } = attestParams;
1340
1456
  const lcSocialPlatform = socialPlatform.toLowerCase();
1341
- const attestation = await this._fund?.attest(lcSocialPlatform, address, genAppSignature);
1457
+ const attestation = await this._fund?.attest({
1458
+ ...attestParams,
1459
+ socialPlatform: lcSocialPlatform
1460
+ }, genAppSignature);
1342
1461
  return resolve(attestation);
1343
1462
  } catch (error) {
1344
1463
  return reject(error);
@@ -1359,9 +1478,12 @@ var PrimusFund = class {
1359
1478
  socialPlatforms[i] = claimParamList[i].socialPlatform.toLowerCase();
1360
1479
  attestations[i] = claimParamList[i].attestation;
1361
1480
  userIdentifiers[i] = claimParamList[i].userIdentifier;
1481
+ if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
1482
+ claimParamList[i].userIdentifier = claimParamList[i].userIdentifier.slice(1);
1483
+ }
1362
1484
  }
1363
1485
  if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestations.length) {
1364
- return reject(`claimParamList is wrong`);
1486
+ return reject(`claimParams is wrong`);
1365
1487
  }
1366
1488
  try {
1367
1489
  if (socialPlatforms.length === 1) {
@@ -1376,6 +1498,33 @@ var PrimusFund = class {
1376
1498
  }
1377
1499
  });
1378
1500
  }
1501
+ async getFundRecords(getFundRecordsParams) {
1502
+ const queryList = Array.isArray(getFundRecordsParams) ? getFundRecordsParams : [getFundRecordsParams];
1503
+ return new Promise(async (resolve, reject) => {
1504
+ if (!queryList || queryList?.length === 0) {
1505
+ const error = new Error("getFundRecordsParams is empty");
1506
+ return reject(error);
1507
+ }
1508
+ const socialPlatforms = [];
1509
+ const userIdentifiers = [];
1510
+ for (let i = 0; i < queryList.length; i++) {
1511
+ socialPlatforms[i] = queryList[i].socialPlatform.toLowerCase();
1512
+ userIdentifiers[i] = queryList[i].userIdentifier;
1513
+ if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
1514
+ queryList[i].userIdentifier = queryList[i].userIdentifier.slice(1);
1515
+ }
1516
+ }
1517
+ if (socialPlatforms.length !== userIdentifiers.length) {
1518
+ return reject(`getFundRecordsParams is wrong`);
1519
+ }
1520
+ try {
1521
+ const result = await this._fund?.getTipRecords(queryList);
1522
+ resolve(result);
1523
+ } catch (error) {
1524
+ return reject(error);
1525
+ }
1526
+ });
1527
+ }
1379
1528
  };
1380
1529
  // Annotate the CommonJS export names for ESM import in node:
1381
1530
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -6,17 +6,53 @@ import { ethers as ethers2 } from "ethers";
6
6
  import { PrimusZKTLS } from "@primuslabs/zktls-js-sdk";
7
7
 
8
8
  // src/config/constants.ts
9
- var Fund_CONTRACTS = {
9
+ var DATASOURCETEMPLATESMAP = {
10
+ x: {
11
+ id: "2e3160ae-8b1e-45e3-8c59-426366278b9d",
12
+ field: "screen_name"
13
+ },
14
+ tiktok: {
15
+ id: "2b22c9f8-686d-4482-a0cf-c9c43c1181ad",
16
+ field: "username"
17
+ },
18
+ "google account": {
19
+ id: "3bad8a55-4415-4bec-9b47-a4c7bbe93518",
20
+ field: "2"
21
+ }
22
+ };
23
+ var SUPPORTEDSOCIALPLATFORMS = Object.keys(DATASOURCETEMPLATESMAP);
24
+ var SUPPORTEDCHAINIDSMAP = {
25
+ 10143: {
26
+ chainId: 10143,
27
+ chainName: "Monad testnet",
28
+ nativeCurrency: {
29
+ name: "MON",
30
+ symbol: "MON",
31
+ decimals: 18
32
+ },
33
+ contractAddress: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
34
+ }
10
35
  // monad testnet
11
- 10143: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
12
36
  };
37
+ var NATIVETOKENS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
38
+ return {
39
+ ...prev,
40
+ [curr.chainId]: curr.nativeCurrency.symbol
41
+ };
42
+ }, {});
43
+ var CHAINNAMES = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
44
+ return {
45
+ ...prev,
46
+ [curr.chainId]: curr.chainName
47
+ };
48
+ }, {});
49
+ var Fund_CONTRACTS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
50
+ return {
51
+ ...prev,
52
+ [curr.chainId]: curr.contractAddress
53
+ };
54
+ }, {});
13
55
  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
56
 
21
57
  // src/classes/Contract.ts
22
58
  import { ethers } from "ethers";
@@ -1013,12 +1049,14 @@ var erc20Abi_default = [
1013
1049
  ];
1014
1050
 
1015
1051
  // src/classes/Fund.ts
1052
+ var { parseUnits, formatUnits } = ethers2.utils;
1016
1053
  var Fund = class {
1017
1054
  _attestLoading;
1018
1055
  zkTlsSdk;
1019
1056
  fundContract;
1020
1057
  provider;
1021
- _dataSourceTemplateMap = DATASOURCETEMPLATEMAP;
1058
+ chainId;
1059
+ _dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
1022
1060
  constructor() {
1023
1061
  this._attestLoading = false;
1024
1062
  }
@@ -1040,6 +1078,7 @@ var Fund = class {
1040
1078
  }
1041
1079
  this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
1042
1080
  this.provider = provider;
1081
+ this.chainId = chainId;
1043
1082
  if (appId) {
1044
1083
  this.zkTlsSdk = new PrimusZKTLS();
1045
1084
  const extensionVersion = await this.zkTlsSdk.init(
@@ -1066,7 +1105,7 @@ var Fund = class {
1066
1105
  const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1067
1106
  decimals = await erc20Contract.decimals();
1068
1107
  }
1069
- const tokenAmount = ethers2.utils.parseUnits(recipientInfo.tokenAmount.toString(), decimals);
1108
+ const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
1070
1109
  const newFundRecipientInfo = {
1071
1110
  idSource: recipientInfo.socialPlatform,
1072
1111
  id: recipientInfo.userIdentifier,
@@ -1111,9 +1150,9 @@ var Fund = class {
1111
1150
  const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
1112
1151
  decimals = await erc20Contract.decimals();
1113
1152
  }
1114
- let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(ethers2.utils.parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
1153
+ let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
1115
1154
  const newRecipientInfoList = recipientInfoList.map((i) => {
1116
- const formatAmount = ethers2.utils.parseUnits(i.tokenAmount.toString(), decimals);
1155
+ const formatAmount = parseUnits(i.tokenAmount.toString(), decimals);
1117
1156
  return {
1118
1157
  idSource: i.socialPlatform,
1119
1158
  id: i.userIdentifier,
@@ -1141,7 +1180,7 @@ var Fund = class {
1141
1180
  const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
1142
1181
  const allowance = await erc20Contract.allowance(address, this.fundContract.address);
1143
1182
  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));
1183
+ const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
1145
1184
  if (allowance.lt(requiredAllowance)) {
1146
1185
  const tx = await erc20Contract.approve(this.fundContract.address, requiredAllowance);
1147
1186
  await tx.wait();
@@ -1159,7 +1198,7 @@ var Fund = class {
1159
1198
  }
1160
1199
  });
1161
1200
  }
1162
- async attest(socialPlatform, address, genAppSignature) {
1201
+ async attest(attestParams, genAppSignature) {
1163
1202
  return new Promise(async (resolve, reject) => {
1164
1203
  if (!this.zkTlsSdk?.padoExtensionVersion) {
1165
1204
  return reject(`Uninitialized!`);
@@ -1167,15 +1206,26 @@ var Fund = class {
1167
1206
  if (this._attestLoading) {
1168
1207
  return reject(`Under proof!`);
1169
1208
  }
1209
+ const { socialPlatform, userIdentifier, address } = attestParams;
1170
1210
  this._attestLoading = true;
1171
- const templateId = this._dataSourceTemplateMap[socialPlatform];
1211
+ const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
1172
1212
  const attRequest = this.zkTlsSdk.generateRequestParams(
1173
1213
  templateId,
1174
1214
  address
1175
1215
  );
1216
+ attRequest.setAttConditions([
1217
+ [
1218
+ {
1219
+ field,
1220
+ op: "STREQ",
1221
+ value: userIdentifier
1222
+ }
1223
+ ]
1224
+ ]);
1176
1225
  const signParams = attRequest.toJsonString();
1177
1226
  const signature = await genAppSignature(signParams);
1178
1227
  if (!signature) {
1228
+ this._attestLoading = false;
1179
1229
  return reject(`appSignature is empty!`);
1180
1230
  }
1181
1231
  try {
@@ -1189,8 +1239,9 @@ var Fund = class {
1189
1239
  JSON.stringify(formatAttestParams)
1190
1240
  );
1191
1241
  this._attestLoading = false;
1192
- resolve(attestation);
1242
+ return resolve(attestation);
1193
1243
  } catch (error) {
1244
+ this._attestLoading = false;
1194
1245
  return reject(error);
1195
1246
  }
1196
1247
  });
@@ -1202,7 +1253,7 @@ var Fund = class {
1202
1253
  const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: socialPlatform, id: userIdentifier }]);
1203
1254
  console.log("fundRecords", fundRecords);
1204
1255
  const recordCount = fundRecords.length;
1205
- if (fundRecords <= 0) {
1256
+ if (recordCount <= 0) {
1206
1257
  return reject(`No fund records.`);
1207
1258
  } else {
1208
1259
  const totalFee = claimFee.mul(recordCount);
@@ -1241,6 +1292,53 @@ var Fund = class {
1241
1292
  }
1242
1293
  });
1243
1294
  }
1295
+ async getTipRecords(getFundRecordsParams) {
1296
+ return new Promise(async (resolve, reject) => {
1297
+ try {
1298
+ const formatGetFundRecordsParams = getFundRecordsParams.map((item) => {
1299
+ return {
1300
+ idSource: item.socialPlatform,
1301
+ id: item.userIdentifier
1302
+ };
1303
+ });
1304
+ const fundRecords = await this.fundContract.callMethod("getTipRecords", formatGetFundRecordsParams);
1305
+ console.log("fundRecords", fundRecords);
1306
+ let formatRecords = [];
1307
+ for (const record of fundRecords) {
1308
+ const { tipper, timestamp, tipToken: [tokenType, tokenAddress], amount } = record;
1309
+ let decimals = 18;
1310
+ let symbol = "";
1311
+ if (tokenType === 0) {
1312
+ const erc20Contract = new ethers2.Contract(tokenAddress, erc20Abi_default, this.provider);
1313
+ decimals = await erc20Contract.decimals();
1314
+ symbol = await erc20Contract.symbol();
1315
+ } else if (tokenType === 1) {
1316
+ symbol = NATIVETOKENS[this.chainId];
1317
+ }
1318
+ let fundToken = {
1319
+ tokenType,
1320
+ // tokenAmount: formatUnits(amount, decimals),
1321
+ decimals,
1322
+ symbol,
1323
+ chainName: CHAINNAMES[this.chainId]
1324
+ };
1325
+ if (tokenType === 0) {
1326
+ fundToken.tokenAddress = tokenAddress;
1327
+ }
1328
+ formatRecords.push({
1329
+ funder: tipper,
1330
+ fundToken,
1331
+ amount: formatUnits(amount, decimals),
1332
+ timestamp: timestamp.toNumber() * 1e3
1333
+ });
1334
+ }
1335
+ console.log("formatRecords", formatRecords);
1336
+ return resolve(formatRecords);
1337
+ } catch (error) {
1338
+ return reject(error);
1339
+ }
1340
+ });
1341
+ }
1244
1342
  };
1245
1343
 
1246
1344
  // src/index.ts
@@ -1255,7 +1353,13 @@ var PrimusFund = class {
1255
1353
  return reject("chainId is not supported");
1256
1354
  }
1257
1355
  this._fund = new Fund();
1258
- const result = await this._fund.init(new ethers3.providers.Web3Provider(provider), chainId, appId);
1356
+ let formatProvider;
1357
+ if (provider instanceof ethers3.providers.JsonRpcProvider) {
1358
+ formatProvider = new ethers3.providers.JsonRpcProvider(defaultRpcUrl);
1359
+ } else {
1360
+ formatProvider = new ethers3.providers.Web3Provider(provider);
1361
+ }
1362
+ const result = await this._fund.init(formatProvider, chainId, appId);
1259
1363
  return resolve(result);
1260
1364
  } catch (error) {
1261
1365
  return reject(error);
@@ -1277,9 +1381,17 @@ var PrimusFund = class {
1277
1381
  tokenInfo.tokenAddress = ethers3.constants.AddressZero;
1278
1382
  }
1279
1383
  const newFundRecipientInfos = recipientInfos.map((i) => {
1280
- i.nftIds = [];
1281
- i.socialPlatform = i.socialPlatform.toLowerCase();
1282
- return i;
1384
+ const formatSocialPlatform = i.socialPlatform.toLowerCase();
1385
+ let formatUserIdentifier = i.userIdentifier;
1386
+ if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
1387
+ formatUserIdentifier = i.userIdentifier.slice(1);
1388
+ }
1389
+ return {
1390
+ nftIds: [],
1391
+ socialPlatform: formatSocialPlatform,
1392
+ userIdentifier: formatUserIdentifier,
1393
+ tokenAmount: i.tokenAmount
1394
+ };
1283
1395
  });
1284
1396
  if (recipientInfos.length === 1) {
1285
1397
  const result = await this._fund?.fund(tokenInfo, newFundRecipientInfos[0]);
@@ -1301,6 +1413,9 @@ var PrimusFund = class {
1301
1413
  }
1302
1414
  const newRecipients = recipients.map((i) => {
1303
1415
  i.socialPlatform = i.socialPlatform.toLowerCase();
1416
+ if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
1417
+ i.userIdentifier = i.userIdentifier.slice(1);
1418
+ }
1304
1419
  return i;
1305
1420
  });
1306
1421
  const result = await this._fund?.refund(newRecipients);
@@ -1310,11 +1425,15 @@ var PrimusFund = class {
1310
1425
  }
1311
1426
  });
1312
1427
  }
1313
- async attest(socialPlatform, address, genAppSignature) {
1428
+ async attest(attestParams, genAppSignature) {
1314
1429
  return new Promise(async (resolve, reject) => {
1315
1430
  try {
1431
+ const { socialPlatform } = attestParams;
1316
1432
  const lcSocialPlatform = socialPlatform.toLowerCase();
1317
- const attestation = await this._fund?.attest(lcSocialPlatform, address, genAppSignature);
1433
+ const attestation = await this._fund?.attest({
1434
+ ...attestParams,
1435
+ socialPlatform: lcSocialPlatform
1436
+ }, genAppSignature);
1318
1437
  return resolve(attestation);
1319
1438
  } catch (error) {
1320
1439
  return reject(error);
@@ -1335,9 +1454,12 @@ var PrimusFund = class {
1335
1454
  socialPlatforms[i] = claimParamList[i].socialPlatform.toLowerCase();
1336
1455
  attestations[i] = claimParamList[i].attestation;
1337
1456
  userIdentifiers[i] = claimParamList[i].userIdentifier;
1457
+ if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
1458
+ claimParamList[i].userIdentifier = claimParamList[i].userIdentifier.slice(1);
1459
+ }
1338
1460
  }
1339
1461
  if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestations.length) {
1340
- return reject(`claimParamList is wrong`);
1462
+ return reject(`claimParams is wrong`);
1341
1463
  }
1342
1464
  try {
1343
1465
  if (socialPlatforms.length === 1) {
@@ -1352,6 +1474,33 @@ var PrimusFund = class {
1352
1474
  }
1353
1475
  });
1354
1476
  }
1477
+ async getFundRecords(getFundRecordsParams) {
1478
+ const queryList = Array.isArray(getFundRecordsParams) ? getFundRecordsParams : [getFundRecordsParams];
1479
+ return new Promise(async (resolve, reject) => {
1480
+ if (!queryList || queryList?.length === 0) {
1481
+ const error = new Error("getFundRecordsParams is empty");
1482
+ return reject(error);
1483
+ }
1484
+ const socialPlatforms = [];
1485
+ const userIdentifiers = [];
1486
+ for (let i = 0; i < queryList.length; i++) {
1487
+ socialPlatforms[i] = queryList[i].socialPlatform.toLowerCase();
1488
+ userIdentifiers[i] = queryList[i].userIdentifier;
1489
+ if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
1490
+ queryList[i].userIdentifier = queryList[i].userIdentifier.slice(1);
1491
+ }
1492
+ }
1493
+ if (socialPlatforms.length !== userIdentifiers.length) {
1494
+ return reject(`getFundRecordsParams is wrong`);
1495
+ }
1496
+ try {
1497
+ const result = await this._fund?.getTipRecords(queryList);
1498
+ resolve(result);
1499
+ } catch (error) {
1500
+ return reject(error);
1501
+ }
1502
+ });
1503
+ }
1355
1504
  };
1356
1505
  export {
1357
1506
  PrimusFund
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primuslabs/fund-js-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "author": "Primus Labs <dev@primuslabs.org>",
5
5
  "description": "Primus fund js sdk",
6
6
  "repository": {