@primuslabs/fund-js-sdk 0.1.0 → 0.1.2
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 +1 -1
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +176 -25
- package/dist/index.mjs +176 -25
- package/package.json +1 -1
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
|
-
|
|
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
|
-
|
|
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
|
|
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");
|
|
@@ -89,7 +125,8 @@ var Contract = class {
|
|
|
89
125
|
resolve(txreceipt);
|
|
90
126
|
} catch (error) {
|
|
91
127
|
console.log("sendTransaction error:", error);
|
|
92
|
-
|
|
128
|
+
const errStr = error?.toString()?.toLowerCase() || "";
|
|
129
|
+
if (error?.code === "ACTION_REJECTED" || errStr.indexOf("user rejected") > -1 || errStr.indexOf("approval denied") > -1) {
|
|
93
130
|
return reject("user rejected transaction");
|
|
94
131
|
}
|
|
95
132
|
if (error?.reason) {
|
|
@@ -98,6 +135,7 @@ var Contract = class {
|
|
|
98
135
|
if (error?.data?.message === "insufficient balance") {
|
|
99
136
|
return reject(error?.data?.message);
|
|
100
137
|
}
|
|
138
|
+
return reject(error);
|
|
101
139
|
}
|
|
102
140
|
});
|
|
103
141
|
}
|
|
@@ -1037,12 +1075,14 @@ var erc20Abi_default = [
|
|
|
1037
1075
|
];
|
|
1038
1076
|
|
|
1039
1077
|
// src/classes/Fund.ts
|
|
1078
|
+
var { parseUnits, formatUnits } = import_ethers2.ethers.utils;
|
|
1040
1079
|
var Fund = class {
|
|
1041
1080
|
_attestLoading;
|
|
1042
1081
|
zkTlsSdk;
|
|
1043
1082
|
fundContract;
|
|
1044
1083
|
provider;
|
|
1045
|
-
|
|
1084
|
+
chainId;
|
|
1085
|
+
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1046
1086
|
constructor() {
|
|
1047
1087
|
this._attestLoading = false;
|
|
1048
1088
|
}
|
|
@@ -1064,6 +1104,7 @@ var Fund = class {
|
|
|
1064
1104
|
}
|
|
1065
1105
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1066
1106
|
this.provider = provider;
|
|
1107
|
+
this.chainId = chainId;
|
|
1067
1108
|
if (appId) {
|
|
1068
1109
|
this.zkTlsSdk = new import_zktls_js_sdk.PrimusZKTLS();
|
|
1069
1110
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
@@ -1090,7 +1131,7 @@ var Fund = class {
|
|
|
1090
1131
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
|
|
1091
1132
|
decimals = await erc20Contract.decimals();
|
|
1092
1133
|
}
|
|
1093
|
-
const tokenAmount =
|
|
1134
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1094
1135
|
const newFundRecipientInfo = {
|
|
1095
1136
|
idSource: recipientInfo.socialPlatform,
|
|
1096
1137
|
id: recipientInfo.userIdentifier,
|
|
@@ -1135,9 +1176,9 @@ var Fund = class {
|
|
|
1135
1176
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
|
|
1136
1177
|
decimals = await erc20Contract.decimals();
|
|
1137
1178
|
}
|
|
1138
|
-
let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(
|
|
1179
|
+
let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
|
|
1139
1180
|
const newRecipientInfoList = recipientInfoList.map((i) => {
|
|
1140
|
-
const formatAmount =
|
|
1181
|
+
const formatAmount = parseUnits(i.tokenAmount.toString(), decimals);
|
|
1141
1182
|
return {
|
|
1142
1183
|
idSource: i.socialPlatform,
|
|
1143
1184
|
id: i.userIdentifier,
|
|
@@ -1165,7 +1206,7 @@ var Fund = class {
|
|
|
1165
1206
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1166
1207
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1167
1208
|
const decimals = await erc20Contract.decimals();
|
|
1168
|
-
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(
|
|
1209
|
+
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
|
|
1169
1210
|
if (allowance.lt(requiredAllowance)) {
|
|
1170
1211
|
const tx = await erc20Contract.approve(this.fundContract.address, requiredAllowance);
|
|
1171
1212
|
await tx.wait();
|
|
@@ -1183,7 +1224,7 @@ var Fund = class {
|
|
|
1183
1224
|
}
|
|
1184
1225
|
});
|
|
1185
1226
|
}
|
|
1186
|
-
async attest(
|
|
1227
|
+
async attest(attestParams, genAppSignature) {
|
|
1187
1228
|
return new Promise(async (resolve, reject) => {
|
|
1188
1229
|
if (!this.zkTlsSdk?.padoExtensionVersion) {
|
|
1189
1230
|
return reject(`Uninitialized!`);
|
|
@@ -1191,15 +1232,26 @@ var Fund = class {
|
|
|
1191
1232
|
if (this._attestLoading) {
|
|
1192
1233
|
return reject(`Under proof!`);
|
|
1193
1234
|
}
|
|
1235
|
+
const { socialPlatform, userIdentifier, address } = attestParams;
|
|
1194
1236
|
this._attestLoading = true;
|
|
1195
|
-
const templateId = this._dataSourceTemplateMap[socialPlatform];
|
|
1237
|
+
const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
|
|
1196
1238
|
const attRequest = this.zkTlsSdk.generateRequestParams(
|
|
1197
1239
|
templateId,
|
|
1198
1240
|
address
|
|
1199
1241
|
);
|
|
1242
|
+
attRequest.setAttConditions([
|
|
1243
|
+
[
|
|
1244
|
+
{
|
|
1245
|
+
field,
|
|
1246
|
+
op: "STREQ",
|
|
1247
|
+
value: userIdentifier
|
|
1248
|
+
}
|
|
1249
|
+
]
|
|
1250
|
+
]);
|
|
1200
1251
|
const signParams = attRequest.toJsonString();
|
|
1201
1252
|
const signature = await genAppSignature(signParams);
|
|
1202
1253
|
if (!signature) {
|
|
1254
|
+
this._attestLoading = false;
|
|
1203
1255
|
return reject(`appSignature is empty!`);
|
|
1204
1256
|
}
|
|
1205
1257
|
try {
|
|
@@ -1213,8 +1265,9 @@ var Fund = class {
|
|
|
1213
1265
|
JSON.stringify(formatAttestParams)
|
|
1214
1266
|
);
|
|
1215
1267
|
this._attestLoading = false;
|
|
1216
|
-
resolve(attestation);
|
|
1268
|
+
return resolve(attestation);
|
|
1217
1269
|
} catch (error) {
|
|
1270
|
+
this._attestLoading = false;
|
|
1218
1271
|
return reject(error);
|
|
1219
1272
|
}
|
|
1220
1273
|
});
|
|
@@ -1226,7 +1279,7 @@ var Fund = class {
|
|
|
1226
1279
|
const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: socialPlatform, id: userIdentifier }]);
|
|
1227
1280
|
console.log("fundRecords", fundRecords);
|
|
1228
1281
|
const recordCount = fundRecords.length;
|
|
1229
|
-
if (
|
|
1282
|
+
if (recordCount <= 0) {
|
|
1230
1283
|
return reject(`No fund records.`);
|
|
1231
1284
|
} else {
|
|
1232
1285
|
const totalFee = claimFee.mul(recordCount);
|
|
@@ -1265,6 +1318,53 @@ var Fund = class {
|
|
|
1265
1318
|
}
|
|
1266
1319
|
});
|
|
1267
1320
|
}
|
|
1321
|
+
async getTipRecords(getFundRecordsParams) {
|
|
1322
|
+
return new Promise(async (resolve, reject) => {
|
|
1323
|
+
try {
|
|
1324
|
+
const formatGetFundRecordsParams = getFundRecordsParams.map((item) => {
|
|
1325
|
+
return {
|
|
1326
|
+
idSource: item.socialPlatform,
|
|
1327
|
+
id: item.userIdentifier
|
|
1328
|
+
};
|
|
1329
|
+
});
|
|
1330
|
+
const fundRecords = await this.fundContract.callMethod("getTipRecords", formatGetFundRecordsParams);
|
|
1331
|
+
console.log("fundRecords", fundRecords);
|
|
1332
|
+
let formatRecords = [];
|
|
1333
|
+
for (const record of fundRecords) {
|
|
1334
|
+
const { tipper, timestamp, tipToken: [tokenType, tokenAddress], amount } = record;
|
|
1335
|
+
let decimals = 18;
|
|
1336
|
+
let symbol = "";
|
|
1337
|
+
if (tokenType === 0) {
|
|
1338
|
+
const erc20Contract = new import_ethers2.ethers.Contract(tokenAddress, erc20Abi_default, this.provider);
|
|
1339
|
+
decimals = await erc20Contract.decimals();
|
|
1340
|
+
symbol = await erc20Contract.symbol();
|
|
1341
|
+
} else if (tokenType === 1) {
|
|
1342
|
+
symbol = NATIVETOKENS[this.chainId];
|
|
1343
|
+
}
|
|
1344
|
+
let fundToken = {
|
|
1345
|
+
tokenType,
|
|
1346
|
+
// tokenAmount: formatUnits(amount, decimals),
|
|
1347
|
+
decimals,
|
|
1348
|
+
symbol,
|
|
1349
|
+
chainName: CHAINNAMES[this.chainId]
|
|
1350
|
+
};
|
|
1351
|
+
if (tokenType === 0) {
|
|
1352
|
+
fundToken.tokenAddress = tokenAddress;
|
|
1353
|
+
}
|
|
1354
|
+
formatRecords.push({
|
|
1355
|
+
funder: tipper,
|
|
1356
|
+
fundToken,
|
|
1357
|
+
amount: formatUnits(amount, decimals),
|
|
1358
|
+
timestamp: timestamp.toNumber() * 1e3
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
console.log("formatRecords", formatRecords);
|
|
1362
|
+
return resolve(formatRecords);
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
return reject(error);
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1268
1368
|
};
|
|
1269
1369
|
|
|
1270
1370
|
// src/index.ts
|
|
@@ -1279,7 +1379,13 @@ var PrimusFund = class {
|
|
|
1279
1379
|
return reject("chainId is not supported");
|
|
1280
1380
|
}
|
|
1281
1381
|
this._fund = new Fund();
|
|
1282
|
-
|
|
1382
|
+
let formatProvider;
|
|
1383
|
+
if (provider instanceof import_ethers3.ethers.providers.JsonRpcProvider) {
|
|
1384
|
+
formatProvider = new import_ethers3.ethers.providers.JsonRpcProvider(defaultRpcUrl);
|
|
1385
|
+
} else {
|
|
1386
|
+
formatProvider = new import_ethers3.ethers.providers.Web3Provider(provider);
|
|
1387
|
+
}
|
|
1388
|
+
const result = await this._fund.init(formatProvider, chainId, appId);
|
|
1283
1389
|
return resolve(result);
|
|
1284
1390
|
} catch (error) {
|
|
1285
1391
|
return reject(error);
|
|
@@ -1301,9 +1407,17 @@ var PrimusFund = class {
|
|
|
1301
1407
|
tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
|
|
1302
1408
|
}
|
|
1303
1409
|
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1410
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1411
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1412
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1413
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1414
|
+
}
|
|
1415
|
+
return {
|
|
1416
|
+
nftIds: [],
|
|
1417
|
+
socialPlatform: formatSocialPlatform,
|
|
1418
|
+
userIdentifier: formatUserIdentifier,
|
|
1419
|
+
tokenAmount: i.tokenAmount
|
|
1420
|
+
};
|
|
1307
1421
|
});
|
|
1308
1422
|
if (recipientInfos.length === 1) {
|
|
1309
1423
|
const result = await this._fund?.fund(tokenInfo, newFundRecipientInfos[0]);
|
|
@@ -1325,6 +1439,9 @@ var PrimusFund = class {
|
|
|
1325
1439
|
}
|
|
1326
1440
|
const newRecipients = recipients.map((i) => {
|
|
1327
1441
|
i.socialPlatform = i.socialPlatform.toLowerCase();
|
|
1442
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1443
|
+
i.userIdentifier = i.userIdentifier.slice(1);
|
|
1444
|
+
}
|
|
1328
1445
|
return i;
|
|
1329
1446
|
});
|
|
1330
1447
|
const result = await this._fund?.refund(newRecipients);
|
|
@@ -1334,11 +1451,15 @@ var PrimusFund = class {
|
|
|
1334
1451
|
}
|
|
1335
1452
|
});
|
|
1336
1453
|
}
|
|
1337
|
-
async attest(
|
|
1454
|
+
async attest(attestParams, genAppSignature) {
|
|
1338
1455
|
return new Promise(async (resolve, reject) => {
|
|
1339
1456
|
try {
|
|
1457
|
+
const { socialPlatform } = attestParams;
|
|
1340
1458
|
const lcSocialPlatform = socialPlatform.toLowerCase();
|
|
1341
|
-
const attestation = await this._fund?.attest(
|
|
1459
|
+
const attestation = await this._fund?.attest({
|
|
1460
|
+
...attestParams,
|
|
1461
|
+
socialPlatform: lcSocialPlatform
|
|
1462
|
+
}, genAppSignature);
|
|
1342
1463
|
return resolve(attestation);
|
|
1343
1464
|
} catch (error) {
|
|
1344
1465
|
return reject(error);
|
|
@@ -1359,9 +1480,12 @@ var PrimusFund = class {
|
|
|
1359
1480
|
socialPlatforms[i] = claimParamList[i].socialPlatform.toLowerCase();
|
|
1360
1481
|
attestations[i] = claimParamList[i].attestation;
|
|
1361
1482
|
userIdentifiers[i] = claimParamList[i].userIdentifier;
|
|
1483
|
+
if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
|
|
1484
|
+
claimParamList[i].userIdentifier = claimParamList[i].userIdentifier.slice(1);
|
|
1485
|
+
}
|
|
1362
1486
|
}
|
|
1363
1487
|
if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestations.length) {
|
|
1364
|
-
return reject(`
|
|
1488
|
+
return reject(`claimParams is wrong`);
|
|
1365
1489
|
}
|
|
1366
1490
|
try {
|
|
1367
1491
|
if (socialPlatforms.length === 1) {
|
|
@@ -1376,6 +1500,33 @@ var PrimusFund = class {
|
|
|
1376
1500
|
}
|
|
1377
1501
|
});
|
|
1378
1502
|
}
|
|
1503
|
+
async getFundRecords(getFundRecordsParams) {
|
|
1504
|
+
const queryList = Array.isArray(getFundRecordsParams) ? getFundRecordsParams : [getFundRecordsParams];
|
|
1505
|
+
return new Promise(async (resolve, reject) => {
|
|
1506
|
+
if (!queryList || queryList?.length === 0) {
|
|
1507
|
+
const error = new Error("getFundRecordsParams is empty");
|
|
1508
|
+
return reject(error);
|
|
1509
|
+
}
|
|
1510
|
+
const socialPlatforms = [];
|
|
1511
|
+
const userIdentifiers = [];
|
|
1512
|
+
for (let i = 0; i < queryList.length; i++) {
|
|
1513
|
+
socialPlatforms[i] = queryList[i].socialPlatform.toLowerCase();
|
|
1514
|
+
userIdentifiers[i] = queryList[i].userIdentifier;
|
|
1515
|
+
if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
|
|
1516
|
+
queryList[i].userIdentifier = queryList[i].userIdentifier.slice(1);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
if (socialPlatforms.length !== userIdentifiers.length) {
|
|
1520
|
+
return reject(`getFundRecordsParams is wrong`);
|
|
1521
|
+
}
|
|
1522
|
+
try {
|
|
1523
|
+
const result = await this._fund?.getTipRecords(queryList);
|
|
1524
|
+
resolve(result);
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
return reject(error);
|
|
1527
|
+
}
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1379
1530
|
};
|
|
1380
1531
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1381
1532
|
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
|
|
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";
|
|
@@ -65,7 +101,8 @@ var Contract = class {
|
|
|
65
101
|
resolve(txreceipt);
|
|
66
102
|
} catch (error) {
|
|
67
103
|
console.log("sendTransaction error:", error);
|
|
68
|
-
|
|
104
|
+
const errStr = error?.toString()?.toLowerCase() || "";
|
|
105
|
+
if (error?.code === "ACTION_REJECTED" || errStr.indexOf("user rejected") > -1 || errStr.indexOf("approval denied") > -1) {
|
|
69
106
|
return reject("user rejected transaction");
|
|
70
107
|
}
|
|
71
108
|
if (error?.reason) {
|
|
@@ -74,6 +111,7 @@ var Contract = class {
|
|
|
74
111
|
if (error?.data?.message === "insufficient balance") {
|
|
75
112
|
return reject(error?.data?.message);
|
|
76
113
|
}
|
|
114
|
+
return reject(error);
|
|
77
115
|
}
|
|
78
116
|
});
|
|
79
117
|
}
|
|
@@ -1013,12 +1051,14 @@ var erc20Abi_default = [
|
|
|
1013
1051
|
];
|
|
1014
1052
|
|
|
1015
1053
|
// src/classes/Fund.ts
|
|
1054
|
+
var { parseUnits, formatUnits } = ethers2.utils;
|
|
1016
1055
|
var Fund = class {
|
|
1017
1056
|
_attestLoading;
|
|
1018
1057
|
zkTlsSdk;
|
|
1019
1058
|
fundContract;
|
|
1020
1059
|
provider;
|
|
1021
|
-
|
|
1060
|
+
chainId;
|
|
1061
|
+
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1022
1062
|
constructor() {
|
|
1023
1063
|
this._attestLoading = false;
|
|
1024
1064
|
}
|
|
@@ -1040,6 +1080,7 @@ var Fund = class {
|
|
|
1040
1080
|
}
|
|
1041
1081
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1042
1082
|
this.provider = provider;
|
|
1083
|
+
this.chainId = chainId;
|
|
1043
1084
|
if (appId) {
|
|
1044
1085
|
this.zkTlsSdk = new PrimusZKTLS();
|
|
1045
1086
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
@@ -1066,7 +1107,7 @@ var Fund = class {
|
|
|
1066
1107
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
|
|
1067
1108
|
decimals = await erc20Contract.decimals();
|
|
1068
1109
|
}
|
|
1069
|
-
const tokenAmount =
|
|
1110
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1070
1111
|
const newFundRecipientInfo = {
|
|
1071
1112
|
idSource: recipientInfo.socialPlatform,
|
|
1072
1113
|
id: recipientInfo.userIdentifier,
|
|
@@ -1111,9 +1152,9 @@ var Fund = class {
|
|
|
1111
1152
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, this.provider);
|
|
1112
1153
|
decimals = await erc20Contract.decimals();
|
|
1113
1154
|
}
|
|
1114
|
-
let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(
|
|
1155
|
+
let totalFormatAmount = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
|
|
1115
1156
|
const newRecipientInfoList = recipientInfoList.map((i) => {
|
|
1116
|
-
const formatAmount =
|
|
1157
|
+
const formatAmount = parseUnits(i.tokenAmount.toString(), decimals);
|
|
1117
1158
|
return {
|
|
1118
1159
|
idSource: i.socialPlatform,
|
|
1119
1160
|
id: i.userIdentifier,
|
|
@@ -1141,7 +1182,7 @@ var Fund = class {
|
|
|
1141
1182
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1142
1183
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1143
1184
|
const decimals = await erc20Contract.decimals();
|
|
1144
|
-
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(
|
|
1185
|
+
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
|
|
1145
1186
|
if (allowance.lt(requiredAllowance)) {
|
|
1146
1187
|
const tx = await erc20Contract.approve(this.fundContract.address, requiredAllowance);
|
|
1147
1188
|
await tx.wait();
|
|
@@ -1159,7 +1200,7 @@ var Fund = class {
|
|
|
1159
1200
|
}
|
|
1160
1201
|
});
|
|
1161
1202
|
}
|
|
1162
|
-
async attest(
|
|
1203
|
+
async attest(attestParams, genAppSignature) {
|
|
1163
1204
|
return new Promise(async (resolve, reject) => {
|
|
1164
1205
|
if (!this.zkTlsSdk?.padoExtensionVersion) {
|
|
1165
1206
|
return reject(`Uninitialized!`);
|
|
@@ -1167,15 +1208,26 @@ var Fund = class {
|
|
|
1167
1208
|
if (this._attestLoading) {
|
|
1168
1209
|
return reject(`Under proof!`);
|
|
1169
1210
|
}
|
|
1211
|
+
const { socialPlatform, userIdentifier, address } = attestParams;
|
|
1170
1212
|
this._attestLoading = true;
|
|
1171
|
-
const templateId = this._dataSourceTemplateMap[socialPlatform];
|
|
1213
|
+
const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
|
|
1172
1214
|
const attRequest = this.zkTlsSdk.generateRequestParams(
|
|
1173
1215
|
templateId,
|
|
1174
1216
|
address
|
|
1175
1217
|
);
|
|
1218
|
+
attRequest.setAttConditions([
|
|
1219
|
+
[
|
|
1220
|
+
{
|
|
1221
|
+
field,
|
|
1222
|
+
op: "STREQ",
|
|
1223
|
+
value: userIdentifier
|
|
1224
|
+
}
|
|
1225
|
+
]
|
|
1226
|
+
]);
|
|
1176
1227
|
const signParams = attRequest.toJsonString();
|
|
1177
1228
|
const signature = await genAppSignature(signParams);
|
|
1178
1229
|
if (!signature) {
|
|
1230
|
+
this._attestLoading = false;
|
|
1179
1231
|
return reject(`appSignature is empty!`);
|
|
1180
1232
|
}
|
|
1181
1233
|
try {
|
|
@@ -1189,8 +1241,9 @@ var Fund = class {
|
|
|
1189
1241
|
JSON.stringify(formatAttestParams)
|
|
1190
1242
|
);
|
|
1191
1243
|
this._attestLoading = false;
|
|
1192
|
-
resolve(attestation);
|
|
1244
|
+
return resolve(attestation);
|
|
1193
1245
|
} catch (error) {
|
|
1246
|
+
this._attestLoading = false;
|
|
1194
1247
|
return reject(error);
|
|
1195
1248
|
}
|
|
1196
1249
|
});
|
|
@@ -1202,7 +1255,7 @@ var Fund = class {
|
|
|
1202
1255
|
const fundRecords = await this.fundContract.callMethod("getTipRecords", [{ idSource: socialPlatform, id: userIdentifier }]);
|
|
1203
1256
|
console.log("fundRecords", fundRecords);
|
|
1204
1257
|
const recordCount = fundRecords.length;
|
|
1205
|
-
if (
|
|
1258
|
+
if (recordCount <= 0) {
|
|
1206
1259
|
return reject(`No fund records.`);
|
|
1207
1260
|
} else {
|
|
1208
1261
|
const totalFee = claimFee.mul(recordCount);
|
|
@@ -1241,6 +1294,53 @@ var Fund = class {
|
|
|
1241
1294
|
}
|
|
1242
1295
|
});
|
|
1243
1296
|
}
|
|
1297
|
+
async getTipRecords(getFundRecordsParams) {
|
|
1298
|
+
return new Promise(async (resolve, reject) => {
|
|
1299
|
+
try {
|
|
1300
|
+
const formatGetFundRecordsParams = getFundRecordsParams.map((item) => {
|
|
1301
|
+
return {
|
|
1302
|
+
idSource: item.socialPlatform,
|
|
1303
|
+
id: item.userIdentifier
|
|
1304
|
+
};
|
|
1305
|
+
});
|
|
1306
|
+
const fundRecords = await this.fundContract.callMethod("getTipRecords", formatGetFundRecordsParams);
|
|
1307
|
+
console.log("fundRecords", fundRecords);
|
|
1308
|
+
let formatRecords = [];
|
|
1309
|
+
for (const record of fundRecords) {
|
|
1310
|
+
const { tipper, timestamp, tipToken: [tokenType, tokenAddress], amount } = record;
|
|
1311
|
+
let decimals = 18;
|
|
1312
|
+
let symbol = "";
|
|
1313
|
+
if (tokenType === 0) {
|
|
1314
|
+
const erc20Contract = new ethers2.Contract(tokenAddress, erc20Abi_default, this.provider);
|
|
1315
|
+
decimals = await erc20Contract.decimals();
|
|
1316
|
+
symbol = await erc20Contract.symbol();
|
|
1317
|
+
} else if (tokenType === 1) {
|
|
1318
|
+
symbol = NATIVETOKENS[this.chainId];
|
|
1319
|
+
}
|
|
1320
|
+
let fundToken = {
|
|
1321
|
+
tokenType,
|
|
1322
|
+
// tokenAmount: formatUnits(amount, decimals),
|
|
1323
|
+
decimals,
|
|
1324
|
+
symbol,
|
|
1325
|
+
chainName: CHAINNAMES[this.chainId]
|
|
1326
|
+
};
|
|
1327
|
+
if (tokenType === 0) {
|
|
1328
|
+
fundToken.tokenAddress = tokenAddress;
|
|
1329
|
+
}
|
|
1330
|
+
formatRecords.push({
|
|
1331
|
+
funder: tipper,
|
|
1332
|
+
fundToken,
|
|
1333
|
+
amount: formatUnits(amount, decimals),
|
|
1334
|
+
timestamp: timestamp.toNumber() * 1e3
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
console.log("formatRecords", formatRecords);
|
|
1338
|
+
return resolve(formatRecords);
|
|
1339
|
+
} catch (error) {
|
|
1340
|
+
return reject(error);
|
|
1341
|
+
}
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1244
1344
|
};
|
|
1245
1345
|
|
|
1246
1346
|
// src/index.ts
|
|
@@ -1255,7 +1355,13 @@ var PrimusFund = class {
|
|
|
1255
1355
|
return reject("chainId is not supported");
|
|
1256
1356
|
}
|
|
1257
1357
|
this._fund = new Fund();
|
|
1258
|
-
|
|
1358
|
+
let formatProvider;
|
|
1359
|
+
if (provider instanceof ethers3.providers.JsonRpcProvider) {
|
|
1360
|
+
formatProvider = new ethers3.providers.JsonRpcProvider(defaultRpcUrl);
|
|
1361
|
+
} else {
|
|
1362
|
+
formatProvider = new ethers3.providers.Web3Provider(provider);
|
|
1363
|
+
}
|
|
1364
|
+
const result = await this._fund.init(formatProvider, chainId, appId);
|
|
1259
1365
|
return resolve(result);
|
|
1260
1366
|
} catch (error) {
|
|
1261
1367
|
return reject(error);
|
|
@@ -1277,9 +1383,17 @@ var PrimusFund = class {
|
|
|
1277
1383
|
tokenInfo.tokenAddress = ethers3.constants.AddressZero;
|
|
1278
1384
|
}
|
|
1279
1385
|
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1386
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1387
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1388
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1389
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1390
|
+
}
|
|
1391
|
+
return {
|
|
1392
|
+
nftIds: [],
|
|
1393
|
+
socialPlatform: formatSocialPlatform,
|
|
1394
|
+
userIdentifier: formatUserIdentifier,
|
|
1395
|
+
tokenAmount: i.tokenAmount
|
|
1396
|
+
};
|
|
1283
1397
|
});
|
|
1284
1398
|
if (recipientInfos.length === 1) {
|
|
1285
1399
|
const result = await this._fund?.fund(tokenInfo, newFundRecipientInfos[0]);
|
|
@@ -1301,6 +1415,9 @@ var PrimusFund = class {
|
|
|
1301
1415
|
}
|
|
1302
1416
|
const newRecipients = recipients.map((i) => {
|
|
1303
1417
|
i.socialPlatform = i.socialPlatform.toLowerCase();
|
|
1418
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1419
|
+
i.userIdentifier = i.userIdentifier.slice(1);
|
|
1420
|
+
}
|
|
1304
1421
|
return i;
|
|
1305
1422
|
});
|
|
1306
1423
|
const result = await this._fund?.refund(newRecipients);
|
|
@@ -1310,11 +1427,15 @@ var PrimusFund = class {
|
|
|
1310
1427
|
}
|
|
1311
1428
|
});
|
|
1312
1429
|
}
|
|
1313
|
-
async attest(
|
|
1430
|
+
async attest(attestParams, genAppSignature) {
|
|
1314
1431
|
return new Promise(async (resolve, reject) => {
|
|
1315
1432
|
try {
|
|
1433
|
+
const { socialPlatform } = attestParams;
|
|
1316
1434
|
const lcSocialPlatform = socialPlatform.toLowerCase();
|
|
1317
|
-
const attestation = await this._fund?.attest(
|
|
1435
|
+
const attestation = await this._fund?.attest({
|
|
1436
|
+
...attestParams,
|
|
1437
|
+
socialPlatform: lcSocialPlatform
|
|
1438
|
+
}, genAppSignature);
|
|
1318
1439
|
return resolve(attestation);
|
|
1319
1440
|
} catch (error) {
|
|
1320
1441
|
return reject(error);
|
|
@@ -1335,9 +1456,12 @@ var PrimusFund = class {
|
|
|
1335
1456
|
socialPlatforms[i] = claimParamList[i].socialPlatform.toLowerCase();
|
|
1336
1457
|
attestations[i] = claimParamList[i].attestation;
|
|
1337
1458
|
userIdentifiers[i] = claimParamList[i].userIdentifier;
|
|
1459
|
+
if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
|
|
1460
|
+
claimParamList[i].userIdentifier = claimParamList[i].userIdentifier.slice(1);
|
|
1461
|
+
}
|
|
1338
1462
|
}
|
|
1339
1463
|
if (socialPlatforms.length !== userIdentifiers.length || socialPlatforms.length !== attestations.length) {
|
|
1340
|
-
return reject(`
|
|
1464
|
+
return reject(`claimParams is wrong`);
|
|
1341
1465
|
}
|
|
1342
1466
|
try {
|
|
1343
1467
|
if (socialPlatforms.length === 1) {
|
|
@@ -1352,6 +1476,33 @@ var PrimusFund = class {
|
|
|
1352
1476
|
}
|
|
1353
1477
|
});
|
|
1354
1478
|
}
|
|
1479
|
+
async getFundRecords(getFundRecordsParams) {
|
|
1480
|
+
const queryList = Array.isArray(getFundRecordsParams) ? getFundRecordsParams : [getFundRecordsParams];
|
|
1481
|
+
return new Promise(async (resolve, reject) => {
|
|
1482
|
+
if (!queryList || queryList?.length === 0) {
|
|
1483
|
+
const error = new Error("getFundRecordsParams is empty");
|
|
1484
|
+
return reject(error);
|
|
1485
|
+
}
|
|
1486
|
+
const socialPlatforms = [];
|
|
1487
|
+
const userIdentifiers = [];
|
|
1488
|
+
for (let i = 0; i < queryList.length; i++) {
|
|
1489
|
+
socialPlatforms[i] = queryList[i].socialPlatform.toLowerCase();
|
|
1490
|
+
userIdentifiers[i] = queryList[i].userIdentifier;
|
|
1491
|
+
if (socialPlatforms[i] === "x" && userIdentifiers[i].startsWith("@")) {
|
|
1492
|
+
queryList[i].userIdentifier = queryList[i].userIdentifier.slice(1);
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
if (socialPlatforms.length !== userIdentifiers.length) {
|
|
1496
|
+
return reject(`getFundRecordsParams is wrong`);
|
|
1497
|
+
}
|
|
1498
|
+
try {
|
|
1499
|
+
const result = await this._fund?.getTipRecords(queryList);
|
|
1500
|
+
resolve(result);
|
|
1501
|
+
} catch (error) {
|
|
1502
|
+
return reject(error);
|
|
1503
|
+
}
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1355
1506
|
};
|
|
1356
1507
|
export {
|
|
1357
1508
|
PrimusFund
|