@primuslabs/fund-js-sdk 0.1.7 → 0.1.8
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 -12
- package/dist/index.mjs +176 -12
- 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");
|
|
@@ -1368,6 +1368,7 @@ var Fund = class {
|
|
|
1368
1368
|
zkTlsSdk;
|
|
1369
1369
|
fundContract;
|
|
1370
1370
|
provider;
|
|
1371
|
+
formatProvider;
|
|
1371
1372
|
chainId;
|
|
1372
1373
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1373
1374
|
constructor() {
|
|
@@ -1385,9 +1386,13 @@ var Fund = class {
|
|
|
1385
1386
|
} else {
|
|
1386
1387
|
formatProvider = new import_ethers2.ethers.providers.Web3Provider(provider);
|
|
1387
1388
|
}
|
|
1389
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1390
|
+
console.log("getGasPrice=", gasPrice);
|
|
1391
|
+
await formatProvider.ready;
|
|
1388
1392
|
const network = await formatProvider.getNetwork();
|
|
1389
1393
|
const providerChainId = network.chainId;
|
|
1390
1394
|
console.log("init provider", provider, network);
|
|
1395
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1391
1396
|
if (providerChainId !== chainId) {
|
|
1392
1397
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1393
1398
|
}
|
|
@@ -1397,11 +1402,26 @@ var Fund = class {
|
|
|
1397
1402
|
}
|
|
1398
1403
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1399
1404
|
this.provider = provider;
|
|
1405
|
+
this.formatProvider = formatProvider;
|
|
1400
1406
|
this.chainId = chainId;
|
|
1401
1407
|
if (appId) {
|
|
1402
1408
|
this.zkTlsSdk = new import_zktls_js_sdk.PrimusZKTLS();
|
|
1409
|
+
let platformDevice = "pc";
|
|
1410
|
+
const isIpad = () => {
|
|
1411
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
1412
|
+
const isTabletSize = window.innerWidth > 768 && window.innerWidth < 1366;
|
|
1413
|
+
return /ipad/.test(userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 0 && isTabletSize;
|
|
1414
|
+
};
|
|
1415
|
+
if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
|
|
1416
|
+
platformDevice = "android";
|
|
1417
|
+
} else if (navigator.userAgent.toLocaleLowerCase().includes("iphone") || isIpad()) {
|
|
1418
|
+
platformDevice = "ios";
|
|
1419
|
+
}
|
|
1420
|
+
console.log("init appId", appId, platformDevice);
|
|
1403
1421
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1404
|
-
appId
|
|
1422
|
+
appId,
|
|
1423
|
+
"",
|
|
1424
|
+
{ platform: platformDevice }
|
|
1405
1425
|
);
|
|
1406
1426
|
resolve(extensionVersion);
|
|
1407
1427
|
} else {
|
|
@@ -1421,6 +1441,7 @@ var Fund = class {
|
|
|
1421
1441
|
let params = [];
|
|
1422
1442
|
if (tokenInfo.tokenType === 0) {
|
|
1423
1443
|
await this.approve(tokenInfo, recipientInfos);
|
|
1444
|
+
console.log("after approve in fund fn");
|
|
1424
1445
|
const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
|
|
1425
1446
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1426
1447
|
decimals = await erc20Contract.decimals();
|
|
@@ -1441,6 +1462,51 @@ var Fund = class {
|
|
|
1441
1462
|
} else {
|
|
1442
1463
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1443
1464
|
}
|
|
1465
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1466
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1467
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1468
|
+
}
|
|
1469
|
+
console.log("tip-params", params, this.chainId);
|
|
1470
|
+
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1471
|
+
resolve(result);
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
return reject(error);
|
|
1474
|
+
}
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1477
|
+
async onlyFund(tokenInfo, recipientInfo) {
|
|
1478
|
+
return new Promise(async (resolve, reject) => {
|
|
1479
|
+
try {
|
|
1480
|
+
const recipientInfos = [];
|
|
1481
|
+
recipientInfos[0] = recipientInfo;
|
|
1482
|
+
let decimals = 18;
|
|
1483
|
+
let params = [];
|
|
1484
|
+
if (tokenInfo.tokenType === 0) {
|
|
1485
|
+
const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
|
|
1486
|
+
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1487
|
+
decimals = await erc20Contract.decimals();
|
|
1488
|
+
} else if (tokenInfo.tokenType === 2) {
|
|
1489
|
+
decimals = 0;
|
|
1490
|
+
const erc721ContractInstance = new Erc721Contract_default(this.provider, tokenInfo.tokenAddress);
|
|
1491
|
+
await erc721ContractInstance.approve(this.fundContract.address, recipientInfo.nftIds[0]);
|
|
1492
|
+
}
|
|
1493
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1494
|
+
const newFundRecipientInfo = {
|
|
1495
|
+
idSource: recipientInfo.socialPlatform,
|
|
1496
|
+
id: recipientInfo.userIdentifier,
|
|
1497
|
+
amount: tokenAmount,
|
|
1498
|
+
nftIds: recipientInfo.nftIds
|
|
1499
|
+
};
|
|
1500
|
+
if (tokenInfo.tokenType === 1) {
|
|
1501
|
+
params = [tokenInfo, newFundRecipientInfo, { value: tokenAmount }];
|
|
1502
|
+
} else {
|
|
1503
|
+
params = [tokenInfo, newFundRecipientInfo];
|
|
1504
|
+
}
|
|
1505
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1506
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1507
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1508
|
+
}
|
|
1509
|
+
console.log("tip-params", params, this.chainId);
|
|
1444
1510
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1445
1511
|
resolve(result);
|
|
1446
1512
|
} catch (error) {
|
|
@@ -1458,7 +1524,14 @@ var Fund = class {
|
|
|
1458
1524
|
tipTimestamp: i.tipTimestamp
|
|
1459
1525
|
};
|
|
1460
1526
|
});
|
|
1461
|
-
|
|
1527
|
+
let params = [newRecipients];
|
|
1528
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1529
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1530
|
+
params.push({
|
|
1531
|
+
gasPrice
|
|
1532
|
+
});
|
|
1533
|
+
}
|
|
1534
|
+
const result = await this.fundContract.sendTransaction("tipperWithdraw", params);
|
|
1462
1535
|
return resolve(result);
|
|
1463
1536
|
} catch (error) {
|
|
1464
1537
|
return reject(error);
|
|
@@ -1495,6 +1568,10 @@ var Fund = class {
|
|
|
1495
1568
|
} else {
|
|
1496
1569
|
params = [tokenInfo, newRecipientInfoList];
|
|
1497
1570
|
}
|
|
1571
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1572
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1573
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1574
|
+
}
|
|
1498
1575
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1499
1576
|
return resolve(result);
|
|
1500
1577
|
} catch (error) {
|
|
@@ -1502,6 +1579,7 @@ var Fund = class {
|
|
|
1502
1579
|
}
|
|
1503
1580
|
});
|
|
1504
1581
|
}
|
|
1582
|
+
// TODO-nft
|
|
1505
1583
|
async approve(tokenInfo, recipientInfoList) {
|
|
1506
1584
|
return new Promise(async (resolve, reject) => {
|
|
1507
1585
|
try {
|
|
@@ -1511,9 +1589,15 @@ var Fund = class {
|
|
|
1511
1589
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1512
1590
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1513
1591
|
const decimals = await erc20Contract.decimals();
|
|
1592
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1514
1593
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
|
|
1515
1594
|
if (allowance.lt(requiredAllowance)) {
|
|
1516
|
-
|
|
1595
|
+
let params = [this.fundContract.address, requiredAllowance];
|
|
1596
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1597
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1598
|
+
params.push({ gasPrice });
|
|
1599
|
+
}
|
|
1600
|
+
const tx = await erc20Contract.approve(...params);
|
|
1517
1601
|
await tx.wait();
|
|
1518
1602
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1519
1603
|
} else {
|
|
@@ -1529,11 +1613,9 @@ var Fund = class {
|
|
|
1529
1613
|
}
|
|
1530
1614
|
});
|
|
1531
1615
|
}
|
|
1532
|
-
async attest(attestParams, genAppSignature) {
|
|
1616
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1533
1617
|
return new Promise(async (resolve, reject) => {
|
|
1534
|
-
|
|
1535
|
-
return reject(`Uninitialized!`);
|
|
1536
|
-
}
|
|
1618
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1537
1619
|
if (this._attestLoading) {
|
|
1538
1620
|
return reject(`Under proof!`);
|
|
1539
1621
|
}
|
|
@@ -1544,6 +1626,10 @@ var Fund = class {
|
|
|
1544
1626
|
templateId,
|
|
1545
1627
|
address
|
|
1546
1628
|
);
|
|
1629
|
+
if (backUrl) {
|
|
1630
|
+
attRequest.setBackUrl(backUrl);
|
|
1631
|
+
}
|
|
1632
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1547
1633
|
attRequest.setAttConditions([
|
|
1548
1634
|
[
|
|
1549
1635
|
{
|
|
@@ -1588,7 +1674,12 @@ var Fund = class {
|
|
|
1588
1674
|
return reject(`No fund records.`);
|
|
1589
1675
|
} else {
|
|
1590
1676
|
const totalFee = claimFee.mul(recordCount);
|
|
1591
|
-
|
|
1677
|
+
let params = [socialPlatform, attestation, { value: totalFee }];
|
|
1678
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1679
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1680
|
+
params[2].gasPrice = gasPrice;
|
|
1681
|
+
}
|
|
1682
|
+
const txreceipt = await this.fundContract.sendTransaction("claimBySource", params);
|
|
1592
1683
|
return resolve(txreceipt);
|
|
1593
1684
|
}
|
|
1594
1685
|
} catch (error) {
|
|
@@ -1614,7 +1705,12 @@ var Fund = class {
|
|
|
1614
1705
|
return reject(`No fund records.`);
|
|
1615
1706
|
} else {
|
|
1616
1707
|
const totalFee = claimFee.mul(recordCount);
|
|
1617
|
-
|
|
1708
|
+
let params = [socialPlatforms, userIdentifiers, attestationList, { value: totalFee }];
|
|
1709
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1710
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1711
|
+
params[3].gasPrice = gasPrice;
|
|
1712
|
+
}
|
|
1713
|
+
const txreceipt = await this.fundContract.sendTransaction("claimByMultiSource", params);
|
|
1618
1714
|
return resolve(txreceipt);
|
|
1619
1715
|
}
|
|
1620
1716
|
} catch (error) {
|
|
@@ -1747,6 +1843,74 @@ var PrimusFund = class {
|
|
|
1747
1843
|
}
|
|
1748
1844
|
});
|
|
1749
1845
|
}
|
|
1846
|
+
async approve(fundParam) {
|
|
1847
|
+
return new Promise(async (resolve, reject) => {
|
|
1848
|
+
try {
|
|
1849
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1850
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1851
|
+
return reject("recipientInfos is empty");
|
|
1852
|
+
}
|
|
1853
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1854
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1855
|
+
return reject("socialPlatform is not supported");
|
|
1856
|
+
}
|
|
1857
|
+
if (tokenInfo.tokenType === 1) {
|
|
1858
|
+
tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
|
|
1859
|
+
}
|
|
1860
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1861
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1862
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1863
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1864
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1865
|
+
}
|
|
1866
|
+
return {
|
|
1867
|
+
nftIds: i.nftIds ?? [],
|
|
1868
|
+
socialPlatform: formatSocialPlatform,
|
|
1869
|
+
userIdentifier: formatUserIdentifier,
|
|
1870
|
+
tokenAmount: i.tokenAmount
|
|
1871
|
+
};
|
|
1872
|
+
});
|
|
1873
|
+
const result = await this._fund?.approve(tokenInfo, newFundRecipientInfos);
|
|
1874
|
+
return resolve(result);
|
|
1875
|
+
} catch (error) {
|
|
1876
|
+
return reject(error);
|
|
1877
|
+
}
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
async onlyFund(fundParam) {
|
|
1881
|
+
return new Promise(async (resolve, reject) => {
|
|
1882
|
+
try {
|
|
1883
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1884
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1885
|
+
return reject("recipientInfos is empty");
|
|
1886
|
+
}
|
|
1887
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1888
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1889
|
+
return reject("socialPlatform is not supported");
|
|
1890
|
+
}
|
|
1891
|
+
if (tokenInfo.tokenType === 1) {
|
|
1892
|
+
tokenInfo.tokenAddress = import_ethers3.ethers.constants.AddressZero;
|
|
1893
|
+
}
|
|
1894
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1895
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1896
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1897
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1898
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1899
|
+
}
|
|
1900
|
+
return {
|
|
1901
|
+
nftIds: i.nftIds ?? [],
|
|
1902
|
+
socialPlatform: formatSocialPlatform,
|
|
1903
|
+
userIdentifier: formatUserIdentifier,
|
|
1904
|
+
tokenAmount: i.tokenAmount
|
|
1905
|
+
};
|
|
1906
|
+
});
|
|
1907
|
+
const result = await this._fund?.onlyFund(tokenInfo, newFundRecipientInfos[0]);
|
|
1908
|
+
return resolve(result);
|
|
1909
|
+
} catch (error) {
|
|
1910
|
+
return reject(error);
|
|
1911
|
+
}
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1750
1914
|
async refund(recipients) {
|
|
1751
1915
|
return new Promise(async (resolve, reject) => {
|
|
1752
1916
|
try {
|
|
@@ -1767,7 +1931,7 @@ var PrimusFund = class {
|
|
|
1767
1931
|
}
|
|
1768
1932
|
});
|
|
1769
1933
|
}
|
|
1770
|
-
async attest(attestParams, genAppSignature) {
|
|
1934
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1771
1935
|
return new Promise(async (resolve, reject) => {
|
|
1772
1936
|
try {
|
|
1773
1937
|
const { socialPlatform } = attestParams;
|
|
@@ -1775,7 +1939,7 @@ var PrimusFund = class {
|
|
|
1775
1939
|
const attestation = await this._fund?.attest({
|
|
1776
1940
|
...attestParams,
|
|
1777
1941
|
socialPlatform: lcSocialPlatform
|
|
1778
|
-
}, genAppSignature);
|
|
1942
|
+
}, genAppSignature, backUrl);
|
|
1779
1943
|
return resolve(attestation);
|
|
1780
1944
|
} catch (error) {
|
|
1781
1945
|
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");
|
|
@@ -1344,6 +1344,7 @@ var Fund = class {
|
|
|
1344
1344
|
zkTlsSdk;
|
|
1345
1345
|
fundContract;
|
|
1346
1346
|
provider;
|
|
1347
|
+
formatProvider;
|
|
1347
1348
|
chainId;
|
|
1348
1349
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1349
1350
|
constructor() {
|
|
@@ -1361,9 +1362,13 @@ var Fund = class {
|
|
|
1361
1362
|
} else {
|
|
1362
1363
|
formatProvider = new ethers2.providers.Web3Provider(provider);
|
|
1363
1364
|
}
|
|
1365
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1366
|
+
console.log("getGasPrice=", gasPrice);
|
|
1367
|
+
await formatProvider.ready;
|
|
1364
1368
|
const network = await formatProvider.getNetwork();
|
|
1365
1369
|
const providerChainId = network.chainId;
|
|
1366
1370
|
console.log("init provider", provider, network);
|
|
1371
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1367
1372
|
if (providerChainId !== chainId) {
|
|
1368
1373
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1369
1374
|
}
|
|
@@ -1373,11 +1378,26 @@ var Fund = class {
|
|
|
1373
1378
|
}
|
|
1374
1379
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1375
1380
|
this.provider = provider;
|
|
1381
|
+
this.formatProvider = formatProvider;
|
|
1376
1382
|
this.chainId = chainId;
|
|
1377
1383
|
if (appId) {
|
|
1378
1384
|
this.zkTlsSdk = new PrimusZKTLS();
|
|
1385
|
+
let platformDevice = "pc";
|
|
1386
|
+
const isIpad = () => {
|
|
1387
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
1388
|
+
const isTabletSize = window.innerWidth > 768 && window.innerWidth < 1366;
|
|
1389
|
+
return /ipad/.test(userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 0 && isTabletSize;
|
|
1390
|
+
};
|
|
1391
|
+
if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
|
|
1392
|
+
platformDevice = "android";
|
|
1393
|
+
} else if (navigator.userAgent.toLocaleLowerCase().includes("iphone") || isIpad()) {
|
|
1394
|
+
platformDevice = "ios";
|
|
1395
|
+
}
|
|
1396
|
+
console.log("init appId", appId, platformDevice);
|
|
1379
1397
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1380
|
-
appId
|
|
1398
|
+
appId,
|
|
1399
|
+
"",
|
|
1400
|
+
{ platform: platformDevice }
|
|
1381
1401
|
);
|
|
1382
1402
|
resolve(extensionVersion);
|
|
1383
1403
|
} else {
|
|
@@ -1397,6 +1417,7 @@ var Fund = class {
|
|
|
1397
1417
|
let params = [];
|
|
1398
1418
|
if (tokenInfo.tokenType === 0) {
|
|
1399
1419
|
await this.approve(tokenInfo, recipientInfos);
|
|
1420
|
+
console.log("after approve in fund fn");
|
|
1400
1421
|
const web3Provider = new ethers2.providers.Web3Provider(this.provider);
|
|
1401
1422
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1402
1423
|
decimals = await erc20Contract.decimals();
|
|
@@ -1417,6 +1438,51 @@ var Fund = class {
|
|
|
1417
1438
|
} else {
|
|
1418
1439
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1419
1440
|
}
|
|
1441
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1442
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1443
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1444
|
+
}
|
|
1445
|
+
console.log("tip-params", params, this.chainId);
|
|
1446
|
+
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1447
|
+
resolve(result);
|
|
1448
|
+
} catch (error) {
|
|
1449
|
+
return reject(error);
|
|
1450
|
+
}
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
async onlyFund(tokenInfo, recipientInfo) {
|
|
1454
|
+
return new Promise(async (resolve, reject) => {
|
|
1455
|
+
try {
|
|
1456
|
+
const recipientInfos = [];
|
|
1457
|
+
recipientInfos[0] = recipientInfo;
|
|
1458
|
+
let decimals = 18;
|
|
1459
|
+
let params = [];
|
|
1460
|
+
if (tokenInfo.tokenType === 0) {
|
|
1461
|
+
const web3Provider = new ethers2.providers.Web3Provider(this.provider);
|
|
1462
|
+
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1463
|
+
decimals = await erc20Contract.decimals();
|
|
1464
|
+
} else if (tokenInfo.tokenType === 2) {
|
|
1465
|
+
decimals = 0;
|
|
1466
|
+
const erc721ContractInstance = new Erc721Contract_default(this.provider, tokenInfo.tokenAddress);
|
|
1467
|
+
await erc721ContractInstance.approve(this.fundContract.address, recipientInfo.nftIds[0]);
|
|
1468
|
+
}
|
|
1469
|
+
const tokenAmount = parseUnits(recipientInfo.tokenAmount.toString(), decimals);
|
|
1470
|
+
const newFundRecipientInfo = {
|
|
1471
|
+
idSource: recipientInfo.socialPlatform,
|
|
1472
|
+
id: recipientInfo.userIdentifier,
|
|
1473
|
+
amount: tokenAmount,
|
|
1474
|
+
nftIds: recipientInfo.nftIds
|
|
1475
|
+
};
|
|
1476
|
+
if (tokenInfo.tokenType === 1) {
|
|
1477
|
+
params = [tokenInfo, newFundRecipientInfo, { value: tokenAmount }];
|
|
1478
|
+
} else {
|
|
1479
|
+
params = [tokenInfo, newFundRecipientInfo];
|
|
1480
|
+
}
|
|
1481
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1482
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1483
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1484
|
+
}
|
|
1485
|
+
console.log("tip-params", params, this.chainId);
|
|
1420
1486
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1421
1487
|
resolve(result);
|
|
1422
1488
|
} catch (error) {
|
|
@@ -1434,7 +1500,14 @@ var Fund = class {
|
|
|
1434
1500
|
tipTimestamp: i.tipTimestamp
|
|
1435
1501
|
};
|
|
1436
1502
|
});
|
|
1437
|
-
|
|
1503
|
+
let params = [newRecipients];
|
|
1504
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1505
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1506
|
+
params.push({
|
|
1507
|
+
gasPrice
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
const result = await this.fundContract.sendTransaction("tipperWithdraw", params);
|
|
1438
1511
|
return resolve(result);
|
|
1439
1512
|
} catch (error) {
|
|
1440
1513
|
return reject(error);
|
|
@@ -1471,6 +1544,10 @@ var Fund = class {
|
|
|
1471
1544
|
} else {
|
|
1472
1545
|
params = [tokenInfo, newRecipientInfoList];
|
|
1473
1546
|
}
|
|
1547
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1548
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1549
|
+
params[2] = params[2] ? { ...params[2], gasPrice } : { gasPrice };
|
|
1550
|
+
}
|
|
1474
1551
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1475
1552
|
return resolve(result);
|
|
1476
1553
|
} catch (error) {
|
|
@@ -1478,6 +1555,7 @@ var Fund = class {
|
|
|
1478
1555
|
}
|
|
1479
1556
|
});
|
|
1480
1557
|
}
|
|
1558
|
+
// TODO-nft
|
|
1481
1559
|
async approve(tokenInfo, recipientInfoList) {
|
|
1482
1560
|
return new Promise(async (resolve, reject) => {
|
|
1483
1561
|
try {
|
|
@@ -1487,9 +1565,15 @@ var Fund = class {
|
|
|
1487
1565
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1488
1566
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1489
1567
|
const decimals = await erc20Contract.decimals();
|
|
1568
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1490
1569
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
|
|
1491
1570
|
if (allowance.lt(requiredAllowance)) {
|
|
1492
|
-
|
|
1571
|
+
let params = [this.fundContract.address, requiredAllowance];
|
|
1572
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1573
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1574
|
+
params.push({ gasPrice });
|
|
1575
|
+
}
|
|
1576
|
+
const tx = await erc20Contract.approve(...params);
|
|
1493
1577
|
await tx.wait();
|
|
1494
1578
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1495
1579
|
} else {
|
|
@@ -1505,11 +1589,9 @@ var Fund = class {
|
|
|
1505
1589
|
}
|
|
1506
1590
|
});
|
|
1507
1591
|
}
|
|
1508
|
-
async attest(attestParams, genAppSignature) {
|
|
1592
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1509
1593
|
return new Promise(async (resolve, reject) => {
|
|
1510
|
-
|
|
1511
|
-
return reject(`Uninitialized!`);
|
|
1512
|
-
}
|
|
1594
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1513
1595
|
if (this._attestLoading) {
|
|
1514
1596
|
return reject(`Under proof!`);
|
|
1515
1597
|
}
|
|
@@ -1520,6 +1602,10 @@ var Fund = class {
|
|
|
1520
1602
|
templateId,
|
|
1521
1603
|
address
|
|
1522
1604
|
);
|
|
1605
|
+
if (backUrl) {
|
|
1606
|
+
attRequest.setBackUrl(backUrl);
|
|
1607
|
+
}
|
|
1608
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1523
1609
|
attRequest.setAttConditions([
|
|
1524
1610
|
[
|
|
1525
1611
|
{
|
|
@@ -1564,7 +1650,12 @@ var Fund = class {
|
|
|
1564
1650
|
return reject(`No fund records.`);
|
|
1565
1651
|
} else {
|
|
1566
1652
|
const totalFee = claimFee.mul(recordCount);
|
|
1567
|
-
|
|
1653
|
+
let params = [socialPlatform, attestation, { value: totalFee }];
|
|
1654
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1655
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1656
|
+
params[2].gasPrice = gasPrice;
|
|
1657
|
+
}
|
|
1658
|
+
const txreceipt = await this.fundContract.sendTransaction("claimBySource", params);
|
|
1568
1659
|
return resolve(txreceipt);
|
|
1569
1660
|
}
|
|
1570
1661
|
} catch (error) {
|
|
@@ -1590,7 +1681,12 @@ var Fund = class {
|
|
|
1590
1681
|
return reject(`No fund records.`);
|
|
1591
1682
|
} else {
|
|
1592
1683
|
const totalFee = claimFee.mul(recordCount);
|
|
1593
|
-
|
|
1684
|
+
let params = [socialPlatforms, userIdentifiers, attestationList, { value: totalFee }];
|
|
1685
|
+
if ([97, 56].includes(this.chainId)) {
|
|
1686
|
+
const gasPrice = await this.formatProvider.getGasPrice();
|
|
1687
|
+
params[3].gasPrice = gasPrice;
|
|
1688
|
+
}
|
|
1689
|
+
const txreceipt = await this.fundContract.sendTransaction("claimByMultiSource", params);
|
|
1594
1690
|
return resolve(txreceipt);
|
|
1595
1691
|
}
|
|
1596
1692
|
} catch (error) {
|
|
@@ -1723,6 +1819,74 @@ var PrimusFund = class {
|
|
|
1723
1819
|
}
|
|
1724
1820
|
});
|
|
1725
1821
|
}
|
|
1822
|
+
async approve(fundParam) {
|
|
1823
|
+
return new Promise(async (resolve, reject) => {
|
|
1824
|
+
try {
|
|
1825
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1826
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1827
|
+
return reject("recipientInfos is empty");
|
|
1828
|
+
}
|
|
1829
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1830
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1831
|
+
return reject("socialPlatform is not supported");
|
|
1832
|
+
}
|
|
1833
|
+
if (tokenInfo.tokenType === 1) {
|
|
1834
|
+
tokenInfo.tokenAddress = ethers3.constants.AddressZero;
|
|
1835
|
+
}
|
|
1836
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1837
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1838
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1839
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1840
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1841
|
+
}
|
|
1842
|
+
return {
|
|
1843
|
+
nftIds: i.nftIds ?? [],
|
|
1844
|
+
socialPlatform: formatSocialPlatform,
|
|
1845
|
+
userIdentifier: formatUserIdentifier,
|
|
1846
|
+
tokenAmount: i.tokenAmount
|
|
1847
|
+
};
|
|
1848
|
+
});
|
|
1849
|
+
const result = await this._fund?.approve(tokenInfo, newFundRecipientInfos);
|
|
1850
|
+
return resolve(result);
|
|
1851
|
+
} catch (error) {
|
|
1852
|
+
return reject(error);
|
|
1853
|
+
}
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
async onlyFund(fundParam) {
|
|
1857
|
+
return new Promise(async (resolve, reject) => {
|
|
1858
|
+
try {
|
|
1859
|
+
const { tokenInfo, recipientInfos } = fundParam;
|
|
1860
|
+
if (!recipientInfos || recipientInfos.length === 0) {
|
|
1861
|
+
return reject("recipientInfos is empty");
|
|
1862
|
+
}
|
|
1863
|
+
const hasUnsupportedSocialPlatforms = recipientInfos.some((i) => !this.supportedSocialPlatforms.includes(i.socialPlatform.toLowerCase()));
|
|
1864
|
+
if (hasUnsupportedSocialPlatforms) {
|
|
1865
|
+
return reject("socialPlatform is not supported");
|
|
1866
|
+
}
|
|
1867
|
+
if (tokenInfo.tokenType === 1) {
|
|
1868
|
+
tokenInfo.tokenAddress = ethers3.constants.AddressZero;
|
|
1869
|
+
}
|
|
1870
|
+
const newFundRecipientInfos = recipientInfos.map((i) => {
|
|
1871
|
+
const formatSocialPlatform = i.socialPlatform.toLowerCase();
|
|
1872
|
+
let formatUserIdentifier = i.userIdentifier;
|
|
1873
|
+
if (i.socialPlatform === "x" && i.userIdentifier.startsWith("@")) {
|
|
1874
|
+
formatUserIdentifier = i.userIdentifier.slice(1);
|
|
1875
|
+
}
|
|
1876
|
+
return {
|
|
1877
|
+
nftIds: i.nftIds ?? [],
|
|
1878
|
+
socialPlatform: formatSocialPlatform,
|
|
1879
|
+
userIdentifier: formatUserIdentifier,
|
|
1880
|
+
tokenAmount: i.tokenAmount
|
|
1881
|
+
};
|
|
1882
|
+
});
|
|
1883
|
+
const result = await this._fund?.onlyFund(tokenInfo, newFundRecipientInfos[0]);
|
|
1884
|
+
return resolve(result);
|
|
1885
|
+
} catch (error) {
|
|
1886
|
+
return reject(error);
|
|
1887
|
+
}
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1726
1890
|
async refund(recipients) {
|
|
1727
1891
|
return new Promise(async (resolve, reject) => {
|
|
1728
1892
|
try {
|
|
@@ -1743,7 +1907,7 @@ var PrimusFund = class {
|
|
|
1743
1907
|
}
|
|
1744
1908
|
});
|
|
1745
1909
|
}
|
|
1746
|
-
async attest(attestParams, genAppSignature) {
|
|
1910
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1747
1911
|
return new Promise(async (resolve, reject) => {
|
|
1748
1912
|
try {
|
|
1749
1913
|
const { socialPlatform } = attestParams;
|
|
@@ -1751,7 +1915,7 @@ var PrimusFund = class {
|
|
|
1751
1915
|
const attestation = await this._fund?.attest({
|
|
1752
1916
|
...attestParams,
|
|
1753
1917
|
socialPlatform: lcSocialPlatform
|
|
1754
|
-
}, genAppSignature);
|
|
1918
|
+
}, genAppSignature, backUrl);
|
|
1755
1919
|
return resolve(attestation);
|
|
1756
1920
|
} catch (error) {
|
|
1757
1921
|
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.8",
|
|
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
|
}
|