@primuslabs/fund-js-sdk 0.1.7 → 0.1.9
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.js +176 -21
- package/dist/index.mjs +176 -21
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -178,7 +178,7 @@ var Contract = class {
|
|
|
178
178
|
if (isNoPendingWithdrawals) {
|
|
179
179
|
return reject("no pending withdrawals");
|
|
180
180
|
}
|
|
181
|
-
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
|
|
181
|
+
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit", "INSUFFICIENT_FUNDS"];
|
|
182
182
|
const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
|
|
183
183
|
if (isInsufficientBalance) {
|
|
184
184
|
return reject("insufficient balance");
|
|
@@ -1364,14 +1364,13 @@ var Erc721Contract_default = Erc721Contract;
|
|
|
1364
1364
|
// src/classes/Fund.ts
|
|
1365
1365
|
var { parseUnits, formatUnits } = import_ethers2.ethers.utils;
|
|
1366
1366
|
var Fund = class {
|
|
1367
|
-
_attestLoading;
|
|
1368
1367
|
zkTlsSdk;
|
|
1369
1368
|
fundContract;
|
|
1370
1369
|
provider;
|
|
1370
|
+
formatProvider;
|
|
1371
1371
|
chainId;
|
|
1372
1372
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1373
1373
|
constructor() {
|
|
1374
|
-
this._attestLoading = false;
|
|
1375
1374
|
}
|
|
1376
1375
|
getZkTlsSdk() {
|
|
1377
1376
|
return this.zkTlsSdk;
|
|
@@ -1385,9 +1384,13 @@ var Fund = class {
|
|
|
1385
1384
|
} else {
|
|
1386
1385
|
formatProvider = new import_ethers2.ethers.providers.Web3Provider(provider);
|
|
1387
1386
|
}
|
|
1387
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1388
|
+
console.log("getGasPrice=", gasPrice);
|
|
1389
|
+
await formatProvider.ready;
|
|
1388
1390
|
const network = await formatProvider.getNetwork();
|
|
1389
1391
|
const providerChainId = network.chainId;
|
|
1390
1392
|
console.log("init provider", provider, network);
|
|
1393
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1391
1394
|
if (providerChainId !== chainId) {
|
|
1392
1395
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1393
1396
|
}
|
|
@@ -1397,11 +1400,26 @@ var Fund = class {
|
|
|
1397
1400
|
}
|
|
1398
1401
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1399
1402
|
this.provider = provider;
|
|
1403
|
+
this.formatProvider = formatProvider;
|
|
1400
1404
|
this.chainId = chainId;
|
|
1401
1405
|
if (appId) {
|
|
1402
1406
|
this.zkTlsSdk = new import_zktls_js_sdk.PrimusZKTLS();
|
|
1407
|
+
let platformDevice = "pc";
|
|
1408
|
+
const isIpad = () => {
|
|
1409
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
1410
|
+
const isTabletSize = window.innerWidth > 768 && window.innerWidth < 1366;
|
|
1411
|
+
return /ipad/.test(userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 0 && isTabletSize;
|
|
1412
|
+
};
|
|
1413
|
+
if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
|
|
1414
|
+
platformDevice = "android";
|
|
1415
|
+
} else if (navigator.userAgent.toLocaleLowerCase().includes("iphone") || isIpad()) {
|
|
1416
|
+
platformDevice = "ios";
|
|
1417
|
+
}
|
|
1418
|
+
console.log("init appId", appId, platformDevice);
|
|
1403
1419
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1404
|
-
appId
|
|
1420
|
+
appId,
|
|
1421
|
+
"",
|
|
1422
|
+
{ platform: platformDevice }
|
|
1405
1423
|
);
|
|
1406
1424
|
resolve(extensionVersion);
|
|
1407
1425
|
} else {
|
|
@@ -1421,6 +1439,47 @@ var Fund = class {
|
|
|
1421
1439
|
let params = [];
|
|
1422
1440
|
if (tokenInfo.tokenType === 0) {
|
|
1423
1441
|
await this.approve(tokenInfo, recipientInfos);
|
|
1442
|
+
console.log("after approve in fund fn");
|
|
1443
|
+
const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
|
|
1444
|
+
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1445
|
+
decimals = await erc20Contract.decimals();
|
|
1446
|
+
} else if (tokenInfo.tokenType === 2) {
|
|
1447
|
+
decimals = 0;
|
|
1448
|
+
const erc721ContractInstance = new Erc721Contract_default(this.provider, tokenInfo.tokenAddress);
|
|
1449
|
+
await erc721ContractInstance.approve(this.fundContract.address, recipientInfo.nftIds[0]);
|
|
1450
|
+
}
|
|
1451
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1452
|
+
const newFundRecipientInfo = {
|
|
1453
|
+
idSource: recipientInfo.socialPlatform,
|
|
1454
|
+
id: recipientInfo.userIdentifier,
|
|
1455
|
+
amount: tokenAmount,
|
|
1456
|
+
nftIds: recipientInfo.nftIds
|
|
1457
|
+
};
|
|
1458
|
+
if (tokenInfo.tokenType === 1) {
|
|
1459
|
+
params = [tokenInfo, newFundRecipientInfo, { value: tokenAmount }];
|
|
1460
|
+
} else {
|
|
1461
|
+
params = [tokenInfo, newFundRecipientInfo];
|
|
1462
|
+
}
|
|
1463
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1464
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1465
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1466
|
+
}
|
|
1467
|
+
console.log("tip-params", params, this.chainId);
|
|
1468
|
+
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1469
|
+
resolve(result);
|
|
1470
|
+
} catch (error) {
|
|
1471
|
+
return reject(error);
|
|
1472
|
+
}
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
async onlyFund(tokenInfo, recipientInfo) {
|
|
1476
|
+
return new Promise(async (resolve, reject) => {
|
|
1477
|
+
try {
|
|
1478
|
+
const recipientInfos = [];
|
|
1479
|
+
recipientInfos[0] = recipientInfo;
|
|
1480
|
+
let decimals = 18;
|
|
1481
|
+
let params = [];
|
|
1482
|
+
if (tokenInfo.tokenType === 0) {
|
|
1424
1483
|
const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
|
|
1425
1484
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1426
1485
|
decimals = await erc20Contract.decimals();
|
|
@@ -1441,6 +1500,11 @@ var Fund = class {
|
|
|
1441
1500
|
} else {
|
|
1442
1501
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1443
1502
|
}
|
|
1503
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1504
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1505
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1506
|
+
}
|
|
1507
|
+
console.log("tip-params", params, this.chainId);
|
|
1444
1508
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1445
1509
|
resolve(result);
|
|
1446
1510
|
} catch (error) {
|
|
@@ -1458,7 +1522,14 @@ var Fund = class {
|
|
|
1458
1522
|
tipTimestamp: i.tipTimestamp
|
|
1459
1523
|
};
|
|
1460
1524
|
});
|
|
1461
|
-
|
|
1525
|
+
let params = [newRecipients];
|
|
1526
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1527
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1528
|
+
params.push({
|
|
1529
|
+
gasPrice
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
const result = await this.fundContract.sendTransaction("tipperWithdraw", params);
|
|
1462
1533
|
return resolve(result);
|
|
1463
1534
|
} catch (error) {
|
|
1464
1535
|
return reject(error);
|
|
@@ -1495,6 +1566,10 @@ var Fund = class {
|
|
|
1495
1566
|
} else {
|
|
1496
1567
|
params = [tokenInfo, newRecipientInfoList];
|
|
1497
1568
|
}
|
|
1569
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1570
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1571
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1572
|
+
}
|
|
1498
1573
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1499
1574
|
return resolve(result);
|
|
1500
1575
|
} catch (error) {
|
|
@@ -1502,6 +1577,7 @@ var Fund = class {
|
|
|
1502
1577
|
}
|
|
1503
1578
|
});
|
|
1504
1579
|
}
|
|
1580
|
+
// TODO-nft
|
|
1505
1581
|
async approve(tokenInfo, recipientInfoList) {
|
|
1506
1582
|
return new Promise(async (resolve, reject) => {
|
|
1507
1583
|
try {
|
|
@@ -1511,9 +1587,15 @@ var Fund = class {
|
|
|
1511
1587
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1512
1588
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1513
1589
|
const decimals = await erc20Contract.decimals();
|
|
1590
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1514
1591
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
|
|
1515
1592
|
if (allowance.lt(requiredAllowance)) {
|
|
1516
|
-
|
|
1593
|
+
let params = [this.fundContract.address, requiredAllowance];
|
|
1594
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1595
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1596
|
+
params.push({ gasPrice });
|
|
1597
|
+
}
|
|
1598
|
+
const tx = await erc20Contract.approve(...params);
|
|
1517
1599
|
await tx.wait();
|
|
1518
1600
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1519
1601
|
} else {
|
|
@@ -1529,21 +1611,19 @@ var Fund = class {
|
|
|
1529
1611
|
}
|
|
1530
1612
|
});
|
|
1531
1613
|
}
|
|
1532
|
-
async attest(attestParams, genAppSignature) {
|
|
1614
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1533
1615
|
return new Promise(async (resolve, reject) => {
|
|
1534
|
-
|
|
1535
|
-
return reject(`Uninitialized!`);
|
|
1536
|
-
}
|
|
1537
|
-
if (this._attestLoading) {
|
|
1538
|
-
return reject(`Under proof!`);
|
|
1539
|
-
}
|
|
1616
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1540
1617
|
const { socialPlatform, userIdentifier, address } = attestParams;
|
|
1541
|
-
this._attestLoading = true;
|
|
1542
1618
|
const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
|
|
1543
1619
|
const attRequest = this.zkTlsSdk.generateRequestParams(
|
|
1544
1620
|
templateId,
|
|
1545
1621
|
address
|
|
1546
1622
|
);
|
|
1623
|
+
if (backUrl) {
|
|
1624
|
+
attRequest.setBackUrl(backUrl);
|
|
1625
|
+
}
|
|
1626
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1547
1627
|
attRequest.setAttConditions([
|
|
1548
1628
|
[
|
|
1549
1629
|
{
|
|
@@ -1556,7 +1636,6 @@ var Fund = class {
|
|
|
1556
1636
|
const signParams = attRequest.toJsonString();
|
|
1557
1637
|
const signature = await genAppSignature(signParams);
|
|
1558
1638
|
if (!signature) {
|
|
1559
|
-
this._attestLoading = false;
|
|
1560
1639
|
return reject(`appSignature is empty!`);
|
|
1561
1640
|
}
|
|
1562
1641
|
try {
|
|
@@ -1569,10 +1648,8 @@ var Fund = class {
|
|
|
1569
1648
|
const attestation = await this.zkTlsSdk.startAttestation(
|
|
1570
1649
|
JSON.stringify(formatAttestParams)
|
|
1571
1650
|
);
|
|
1572
|
-
this._attestLoading = false;
|
|
1573
1651
|
return resolve(attestation);
|
|
1574
1652
|
} catch (error) {
|
|
1575
|
-
this._attestLoading = false;
|
|
1576
1653
|
return reject(error);
|
|
1577
1654
|
}
|
|
1578
1655
|
});
|
|
@@ -1588,7 +1665,12 @@ var Fund = class {
|
|
|
1588
1665
|
return reject(`No fund records.`);
|
|
1589
1666
|
} else {
|
|
1590
1667
|
const totalFee = claimFee.mul(recordCount);
|
|
1591
|
-
|
|
1668
|
+
let params = [socialPlatform, attestation, { value: totalFee }];
|
|
1669
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1670
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1671
|
+
params[2].gasPrice = gasPrice;
|
|
1672
|
+
}
|
|
1673
|
+
const txreceipt = await this.fundContract.sendTransaction("claimBySource", params);
|
|
1592
1674
|
return resolve(txreceipt);
|
|
1593
1675
|
}
|
|
1594
1676
|
} catch (error) {
|
|
@@ -1614,7 +1696,12 @@ var Fund = class {
|
|
|
1614
1696
|
return reject(`No fund records.`);
|
|
1615
1697
|
} else {
|
|
1616
1698
|
const totalFee = claimFee.mul(recordCount);
|
|
1617
|
-
|
|
1699
|
+
let params = [socialPlatforms, userIdentifiers, attestationList, { value: totalFee }];
|
|
1700
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1701
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1702
|
+
params[3].gasPrice = gasPrice;
|
|
1703
|
+
}
|
|
1704
|
+
const txreceipt = await this.fundContract.sendTransaction("claimByMultiSource", params);
|
|
1618
1705
|
return resolve(txreceipt);
|
|
1619
1706
|
}
|
|
1620
1707
|
} catch (error) {
|
|
@@ -1747,6 +1834,74 @@ var PrimusFund = class {
|
|
|
1747
1834
|
}
|
|
1748
1835
|
});
|
|
1749
1836
|
}
|
|
1837
|
+
async approve(fundParam) {
|
|
1838
|
+
return new Promise(async (resolve, reject) => {
|
|
1839
|
+
try {
|
|
1840
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1841
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1842
|
+
return reject("recipientInfos is empty");
|
|
1843
|
+
}
|
|
1844
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1845
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1846
|
+
return reject("socialPlatform is not supported");
|
|
1847
|
+
}
|
|
1848
|
+
if (tokenInfo.tokenType === 1) {
|
|
1849
|
+
tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
|
|
1850
|
+
}
|
|
1851
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1852
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1853
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1854
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1855
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1856
|
+
}
|
|
1857
|
+
return {
|
|
1858
|
+
nftIds: i.nftIds ?? [],
|
|
1859
|
+
socialPlatform: formatSocialPlatform,
|
|
1860
|
+
userIdentifier: formatUserIdentifier,
|
|
1861
|
+
tokenAmount: i.tokenAmount
|
|
1862
|
+
};
|
|
1863
|
+
});
|
|
1864
|
+
const result = await this._fund?.approve(tokenInfo, newFundRecipientInfos);
|
|
1865
|
+
return resolve(result);
|
|
1866
|
+
} catch (error) {
|
|
1867
|
+
return reject(error);
|
|
1868
|
+
}
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
async onlyFund(fundParam) {
|
|
1872
|
+
return new Promise(async (resolve, reject) => {
|
|
1873
|
+
try {
|
|
1874
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1875
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1876
|
+
return reject("recipientInfos is empty");
|
|
1877
|
+
}
|
|
1878
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1879
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1880
|
+
return reject("socialPlatform is not supported");
|
|
1881
|
+
}
|
|
1882
|
+
if (tokenInfo.tokenType === 1) {
|
|
1883
|
+
tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
|
|
1884
|
+
}
|
|
1885
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1886
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1887
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1888
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1889
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1890
|
+
}
|
|
1891
|
+
return {
|
|
1892
|
+
nftIds: i.nftIds ?? [],
|
|
1893
|
+
socialPlatform: formatSocialPlatform,
|
|
1894
|
+
userIdentifier: formatUserIdentifier,
|
|
1895
|
+
tokenAmount: i.tokenAmount
|
|
1896
|
+
};
|
|
1897
|
+
});
|
|
1898
|
+
const result = await this._fund?.onlyFund(tokenInfo, newFundRecipientInfos[0]);
|
|
1899
|
+
return resolve(result);
|
|
1900
|
+
} catch (error) {
|
|
1901
|
+
return reject(error);
|
|
1902
|
+
}
|
|
1903
|
+
});
|
|
1904
|
+
}
|
|
1750
1905
|
async refund(recipients) {
|
|
1751
1906
|
return new Promise(async (resolve, reject) => {
|
|
1752
1907
|
try {
|
|
@@ -1767,7 +1922,7 @@ var PrimusFund = class {
|
|
|
1767
1922
|
}
|
|
1768
1923
|
});
|
|
1769
1924
|
}
|
|
1770
|
-
async attest(attestParams, genAppSignature) {
|
|
1925
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1771
1926
|
return new Promise(async (resolve, reject) => {
|
|
1772
1927
|
try {
|
|
1773
1928
|
const { socialPlatform } = attestParams;
|
|
@@ -1775,7 +1930,7 @@ var PrimusFund = class {
|
|
|
1775
1930
|
const attestation = await this._fund?.attest({
|
|
1776
1931
|
...attestParams,
|
|
1777
1932
|
socialPlatform: lcSocialPlatform
|
|
1778
|
-
}, genAppSignature);
|
|
1933
|
+
}, genAppSignature, backUrl);
|
|
1779
1934
|
return resolve(attestation);
|
|
1780
1935
|
} catch (error) {
|
|
1781
1936
|
return reject(error);
|
package/dist/index.mjs
CHANGED
|
@@ -154,7 +154,7 @@ var Contract = class {
|
|
|
154
154
|
if (isNoPendingWithdrawals) {
|
|
155
155
|
return reject("no pending withdrawals");
|
|
156
156
|
}
|
|
157
|
-
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
|
|
157
|
+
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit", "INSUFFICIENT_FUNDS"];
|
|
158
158
|
const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
|
|
159
159
|
if (isInsufficientBalance) {
|
|
160
160
|
return reject("insufficient balance");
|
|
@@ -1340,14 +1340,13 @@ var Erc721Contract_default = Erc721Contract;
|
|
|
1340
1340
|
// src/classes/Fund.ts
|
|
1341
1341
|
var { parseUnits, formatUnits } = ethers2.utils;
|
|
1342
1342
|
var Fund = class {
|
|
1343
|
-
_attestLoading;
|
|
1344
1343
|
zkTlsSdk;
|
|
1345
1344
|
fundContract;
|
|
1346
1345
|
provider;
|
|
1346
|
+
formatProvider;
|
|
1347
1347
|
chainId;
|
|
1348
1348
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1349
1349
|
constructor() {
|
|
1350
|
-
this._attestLoading = false;
|
|
1351
1350
|
}
|
|
1352
1351
|
getZkTlsSdk() {
|
|
1353
1352
|
return this.zkTlsSdk;
|
|
@@ -1361,9 +1360,13 @@ var Fund = class {
|
|
|
1361
1360
|
} else {
|
|
1362
1361
|
formatProvider = new ethers2.providers.Web3Provider(provider);
|
|
1363
1362
|
}
|
|
1363
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1364
|
+
console.log("getGasPrice=", gasPrice);
|
|
1365
|
+
await formatProvider.ready;
|
|
1364
1366
|
const network = await formatProvider.getNetwork();
|
|
1365
1367
|
const providerChainId = network.chainId;
|
|
1366
1368
|
console.log("init provider", provider, network);
|
|
1369
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1367
1370
|
if (providerChainId !== chainId) {
|
|
1368
1371
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1369
1372
|
}
|
|
@@ -1373,11 +1376,26 @@ var Fund = class {
|
|
|
1373
1376
|
}
|
|
1374
1377
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1375
1378
|
this.provider = provider;
|
|
1379
|
+
this.formatProvider = formatProvider;
|
|
1376
1380
|
this.chainId = chainId;
|
|
1377
1381
|
if (appId) {
|
|
1378
1382
|
this.zkTlsSdk = new PrimusZKTLS();
|
|
1383
|
+
let platformDevice = "pc";
|
|
1384
|
+
const isIpad = () => {
|
|
1385
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
1386
|
+
const isTabletSize = window.innerWidth > 768 && window.innerWidth < 1366;
|
|
1387
|
+
return /ipad/.test(userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 0 && isTabletSize;
|
|
1388
|
+
};
|
|
1389
|
+
if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
|
|
1390
|
+
platformDevice = "android";
|
|
1391
|
+
} else if (navigator.userAgent.toLocaleLowerCase().includes("iphone") || isIpad()) {
|
|
1392
|
+
platformDevice = "ios";
|
|
1393
|
+
}
|
|
1394
|
+
console.log("init appId", appId, platformDevice);
|
|
1379
1395
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1380
|
-
appId
|
|
1396
|
+
appId,
|
|
1397
|
+
"",
|
|
1398
|
+
{ platform: platformDevice }
|
|
1381
1399
|
);
|
|
1382
1400
|
resolve(extensionVersion);
|
|
1383
1401
|
} else {
|
|
@@ -1397,6 +1415,47 @@ var Fund = class {
|
|
|
1397
1415
|
let params = [];
|
|
1398
1416
|
if (tokenInfo.tokenType === 0) {
|
|
1399
1417
|
await this.approve(tokenInfo, recipientInfos);
|
|
1418
|
+
console.log("after approve in fund fn");
|
|
1419
|
+
const web3Provider = new ethers2.providers.Web3Provider(this.provider);
|
|
1420
|
+
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1421
|
+
decimals = await erc20Contract.decimals();
|
|
1422
|
+
} else if (tokenInfo.tokenType === 2) {
|
|
1423
|
+
decimals = 0;
|
|
1424
|
+
const erc721ContractInstance = new Erc721Contract_default(this.provider, tokenInfo.tokenAddress);
|
|
1425
|
+
await erc721ContractInstance.approve(this.fundContract.address, recipientInfo.nftIds[0]);
|
|
1426
|
+
}
|
|
1427
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1428
|
+
const newFundRecipientInfo = {
|
|
1429
|
+
idSource: recipientInfo.socialPlatform,
|
|
1430
|
+
id: recipientInfo.userIdentifier,
|
|
1431
|
+
amount: tokenAmount,
|
|
1432
|
+
nftIds: recipientInfo.nftIds
|
|
1433
|
+
};
|
|
1434
|
+
if (tokenInfo.tokenType === 1) {
|
|
1435
|
+
params = [tokenInfo, newFundRecipientInfo, { value: tokenAmount }];
|
|
1436
|
+
} else {
|
|
1437
|
+
params = [tokenInfo, newFundRecipientInfo];
|
|
1438
|
+
}
|
|
1439
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1440
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1441
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1442
|
+
}
|
|
1443
|
+
console.log("tip-params", params, this.chainId);
|
|
1444
|
+
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1445
|
+
resolve(result);
|
|
1446
|
+
} catch (error) {
|
|
1447
|
+
return reject(error);
|
|
1448
|
+
}
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
async onlyFund(tokenInfo, recipientInfo) {
|
|
1452
|
+
return new Promise(async (resolve, reject) => {
|
|
1453
|
+
try {
|
|
1454
|
+
const recipientInfos = [];
|
|
1455
|
+
recipientInfos[0] = recipientInfo;
|
|
1456
|
+
let decimals = 18;
|
|
1457
|
+
let params = [];
|
|
1458
|
+
if (tokenInfo.tokenType === 0) {
|
|
1400
1459
|
const web3Provider = new ethers2.providers.Web3Provider(this.provider);
|
|
1401
1460
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1402
1461
|
decimals = await erc20Contract.decimals();
|
|
@@ -1417,6 +1476,11 @@ var Fund = class {
|
|
|
1417
1476
|
} else {
|
|
1418
1477
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1419
1478
|
}
|
|
1479
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1480
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1481
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1482
|
+
}
|
|
1483
|
+
console.log("tip-params", params, this.chainId);
|
|
1420
1484
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1421
1485
|
resolve(result);
|
|
1422
1486
|
} catch (error) {
|
|
@@ -1434,7 +1498,14 @@ var Fund = class {
|
|
|
1434
1498
|
tipTimestamp: i.tipTimestamp
|
|
1435
1499
|
};
|
|
1436
1500
|
});
|
|
1437
|
-
|
|
1501
|
+
let params = [newRecipients];
|
|
1502
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1503
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1504
|
+
params.push({
|
|
1505
|
+
gasPrice
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
const result = await this.fundContract.sendTransaction("tipperWithdraw", params);
|
|
1438
1509
|
return resolve(result);
|
|
1439
1510
|
} catch (error) {
|
|
1440
1511
|
return reject(error);
|
|
@@ -1471,6 +1542,10 @@ var Fund = class {
|
|
|
1471
1542
|
} else {
|
|
1472
1543
|
params = [tokenInfo, newRecipientInfoList];
|
|
1473
1544
|
}
|
|
1545
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1546
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1547
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1548
|
+
}
|
|
1474
1549
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1475
1550
|
return resolve(result);
|
|
1476
1551
|
} catch (error) {
|
|
@@ -1478,6 +1553,7 @@ var Fund = class {
|
|
|
1478
1553
|
}
|
|
1479
1554
|
});
|
|
1480
1555
|
}
|
|
1556
|
+
// TODO-nft
|
|
1481
1557
|
async approve(tokenInfo, recipientInfoList) {
|
|
1482
1558
|
return new Promise(async (resolve, reject) => {
|
|
1483
1559
|
try {
|
|
@@ -1487,9 +1563,15 @@ var Fund = class {
|
|
|
1487
1563
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1488
1564
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1489
1565
|
const decimals = await erc20Contract.decimals();
|
|
1566
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1490
1567
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
|
|
1491
1568
|
if (allowance.lt(requiredAllowance)) {
|
|
1492
|
-
|
|
1569
|
+
let params = [this.fundContract.address, requiredAllowance];
|
|
1570
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1571
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1572
|
+
params.push({ gasPrice });
|
|
1573
|
+
}
|
|
1574
|
+
const tx = await erc20Contract.approve(...params);
|
|
1493
1575
|
await tx.wait();
|
|
1494
1576
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1495
1577
|
} else {
|
|
@@ -1505,21 +1587,19 @@ var Fund = class {
|
|
|
1505
1587
|
}
|
|
1506
1588
|
});
|
|
1507
1589
|
}
|
|
1508
|
-
async attest(attestParams, genAppSignature) {
|
|
1590
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1509
1591
|
return new Promise(async (resolve, reject) => {
|
|
1510
|
-
|
|
1511
|
-
return reject(`Uninitialized!`);
|
|
1512
|
-
}
|
|
1513
|
-
if (this._attestLoading) {
|
|
1514
|
-
return reject(`Under proof!`);
|
|
1515
|
-
}
|
|
1592
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1516
1593
|
const { socialPlatform, userIdentifier, address } = attestParams;
|
|
1517
|
-
this._attestLoading = true;
|
|
1518
1594
|
const { id: templateId, field } = this._dataSourceTemplateMap[socialPlatform];
|
|
1519
1595
|
const attRequest = this.zkTlsSdk.generateRequestParams(
|
|
1520
1596
|
templateId,
|
|
1521
1597
|
address
|
|
1522
1598
|
);
|
|
1599
|
+
if (backUrl) {
|
|
1600
|
+
attRequest.setBackUrl(backUrl);
|
|
1601
|
+
}
|
|
1602
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1523
1603
|
attRequest.setAttConditions([
|
|
1524
1604
|
[
|
|
1525
1605
|
{
|
|
@@ -1532,7 +1612,6 @@ var Fund = class {
|
|
|
1532
1612
|
const signParams = attRequest.toJsonString();
|
|
1533
1613
|
const signature = await genAppSignature(signParams);
|
|
1534
1614
|
if (!signature) {
|
|
1535
|
-
this._attestLoading = false;
|
|
1536
1615
|
return reject(`appSignature is empty!`);
|
|
1537
1616
|
}
|
|
1538
1617
|
try {
|
|
@@ -1545,10 +1624,8 @@ var Fund = class {
|
|
|
1545
1624
|
const attestation = await this.zkTlsSdk.startAttestation(
|
|
1546
1625
|
JSON.stringify(formatAttestParams)
|
|
1547
1626
|
);
|
|
1548
|
-
this._attestLoading = false;
|
|
1549
1627
|
return resolve(attestation);
|
|
1550
1628
|
} catch (error) {
|
|
1551
|
-
this._attestLoading = false;
|
|
1552
1629
|
return reject(error);
|
|
1553
1630
|
}
|
|
1554
1631
|
});
|
|
@@ -1564,7 +1641,12 @@ var Fund = class {
|
|
|
1564
1641
|
return reject(`No fund records.`);
|
|
1565
1642
|
} else {
|
|
1566
1643
|
const totalFee = claimFee.mul(recordCount);
|
|
1567
|
-
|
|
1644
|
+
let params = [socialPlatform, attestation, { value: totalFee }];
|
|
1645
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1646
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1647
|
+
params[2].gasPrice = gasPrice;
|
|
1648
|
+
}
|
|
1649
|
+
const txreceipt = await this.fundContract.sendTransaction("claimBySource", params);
|
|
1568
1650
|
return resolve(txreceipt);
|
|
1569
1651
|
}
|
|
1570
1652
|
} catch (error) {
|
|
@@ -1590,7 +1672,12 @@ var Fund = class {
|
|
|
1590
1672
|
return reject(`No fund records.`);
|
|
1591
1673
|
} else {
|
|
1592
1674
|
const totalFee = claimFee.mul(recordCount);
|
|
1593
|
-
|
|
1675
|
+
let params = [socialPlatforms, userIdentifiers, attestationList, { value: totalFee }];
|
|
1676
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1677
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1678
|
+
params[3].gasPrice = gasPrice;
|
|
1679
|
+
}
|
|
1680
|
+
const txreceipt = await this.fundContract.sendTransaction("claimByMultiSource", params);
|
|
1594
1681
|
return resolve(txreceipt);
|
|
1595
1682
|
}
|
|
1596
1683
|
} catch (error) {
|
|
@@ -1723,6 +1810,74 @@ var PrimusFund = class {
|
|
|
1723
1810
|
}
|
|
1724
1811
|
});
|
|
1725
1812
|
}
|
|
1813
|
+
async approve(fundParam) {
|
|
1814
|
+
return new Promise(async (resolve, reject) => {
|
|
1815
|
+
try {
|
|
1816
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1817
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1818
|
+
return reject("recipientInfos is empty");
|
|
1819
|
+
}
|
|
1820
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1821
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1822
|
+
return reject("socialPlatform is not supported");
|
|
1823
|
+
}
|
|
1824
|
+
if (tokenInfo.tokenType === 1) {
|
|
1825
|
+
tokenInfo.tokenAddress = ethers3.constants.AddressZero;
|
|
1826
|
+
}
|
|
1827
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1828
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1829
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1830
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1831
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1832
|
+
}
|
|
1833
|
+
return {
|
|
1834
|
+
nftIds: i.nftIds ?? [],
|
|
1835
|
+
socialPlatform: formatSocialPlatform,
|
|
1836
|
+
userIdentifier: formatUserIdentifier,
|
|
1837
|
+
tokenAmount: i.tokenAmount
|
|
1838
|
+
};
|
|
1839
|
+
});
|
|
1840
|
+
const result = await this._fund?.approve(tokenInfo, newFundRecipientInfos);
|
|
1841
|
+
return resolve(result);
|
|
1842
|
+
} catch (error) {
|
|
1843
|
+
return reject(error);
|
|
1844
|
+
}
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
async onlyFund(fundParam) {
|
|
1848
|
+
return new Promise(async (resolve, reject) => {
|
|
1849
|
+
try {
|
|
1850
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1851
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1852
|
+
return reject("recipientInfos is empty");
|
|
1853
|
+
}
|
|
1854
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1855
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1856
|
+
return reject("socialPlatform is not supported");
|
|
1857
|
+
}
|
|
1858
|
+
if (tokenInfo.tokenType === 1) {
|
|
1859
|
+
tokenInfo.tokenAddress = ethers3.constants.AddressZero;
|
|
1860
|
+
}
|
|
1861
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1862
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1863
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1864
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1865
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1866
|
+
}
|
|
1867
|
+
return {
|
|
1868
|
+
nftIds: i.nftIds ?? [],
|
|
1869
|
+
socialPlatform: formatSocialPlatform,
|
|
1870
|
+
userIdentifier: formatUserIdentifier,
|
|
1871
|
+
tokenAmount: i.tokenAmount
|
|
1872
|
+
};
|
|
1873
|
+
});
|
|
1874
|
+
const result = await this._fund?.onlyFund(tokenInfo, newFundRecipientInfos[0]);
|
|
1875
|
+
return resolve(result);
|
|
1876
|
+
} catch (error) {
|
|
1877
|
+
return reject(error);
|
|
1878
|
+
}
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1726
1881
|
async refund(recipients) {
|
|
1727
1882
|
return new Promise(async (resolve, reject) => {
|
|
1728
1883
|
try {
|
|
@@ -1743,7 +1898,7 @@ var PrimusFund = class {
|
|
|
1743
1898
|
}
|
|
1744
1899
|
});
|
|
1745
1900
|
}
|
|
1746
|
-
async attest(attestParams, genAppSignature) {
|
|
1901
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1747
1902
|
return new Promise(async (resolve, reject) => {
|
|
1748
1903
|
try {
|
|
1749
1904
|
const { socialPlatform } = attestParams;
|
|
@@ -1751,7 +1906,7 @@ var PrimusFund = class {
|
|
|
1751
1906
|
const attestation = await this._fund?.attest({
|
|
1752
1907
|
...attestParams,
|
|
1753
1908
|
socialPlatform: lcSocialPlatform
|
|
1754
|
-
}, genAppSignature);
|
|
1909
|
+
}, genAppSignature, backUrl);
|
|
1755
1910
|
return resolve(attestation);
|
|
1756
1911
|
} catch (error) {
|
|
1757
1912
|
return reject(error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primuslabs/fund-js-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"author": "Primus Labs <dev@primuslabs.org>",
|
|
5
5
|
"description": "Primus fund js sdk",
|
|
6
6
|
"repository": {
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
]
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@primuslabs/zktls-js-sdk": "^0.2
|
|
58
|
+
"@primuslabs/zktls-js-sdk": "^0.3.2",
|
|
59
59
|
"axios": "^1.8.1",
|
|
60
60
|
"ethers": "^5.7.2"
|
|
61
61
|
}
|