@primuslabs/fund-js-sdk 0.1.6 → 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 +199 -14
- package/dist/index.mjs +199 -14
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -59,8 +59,28 @@ var SUPPORTEDCHAINIDSMAP = {
|
|
|
59
59
|
decimals: 18
|
|
60
60
|
},
|
|
61
61
|
contractAddress: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
|
|
62
|
-
}
|
|
62
|
+
},
|
|
63
63
|
// monad testnet
|
|
64
|
+
97: {
|
|
65
|
+
chainId: 97,
|
|
66
|
+
chainName: "BNB Testnet",
|
|
67
|
+
nativeCurrency: {
|
|
68
|
+
decimals: 18,
|
|
69
|
+
name: "tBNB",
|
|
70
|
+
symbol: "tBNB"
|
|
71
|
+
},
|
|
72
|
+
contractAddress: "0x1C5bfc91789DB3130A07a06407E02745945C3218"
|
|
73
|
+
},
|
|
74
|
+
56: {
|
|
75
|
+
chainId: 56,
|
|
76
|
+
chainName: "BNB Chain",
|
|
77
|
+
nativeCurrency: {
|
|
78
|
+
decimals: 18,
|
|
79
|
+
name: "BNB",
|
|
80
|
+
symbol: "BNB"
|
|
81
|
+
},
|
|
82
|
+
contractAddress: "0x1fb86db904caF7c12100EA64024E5dfd7505E484"
|
|
83
|
+
}
|
|
64
84
|
};
|
|
65
85
|
var NATIVETOKENS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
|
|
66
86
|
return {
|
|
@@ -158,7 +178,7 @@ var Contract = class {
|
|
|
158
178
|
if (isNoPendingWithdrawals) {
|
|
159
179
|
return reject("no pending withdrawals");
|
|
160
180
|
}
|
|
161
|
-
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
|
|
181
|
+
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit", "INSUFFICIENT_FUNDS"];
|
|
162
182
|
const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
|
|
163
183
|
if (isInsufficientBalance) {
|
|
164
184
|
return reject("insufficient balance");
|
|
@@ -1348,6 +1368,7 @@ var Fund = class {
|
|
|
1348
1368
|
zkTlsSdk;
|
|
1349
1369
|
fundContract;
|
|
1350
1370
|
provider;
|
|
1371
|
+
formatProvider;
|
|
1351
1372
|
chainId;
|
|
1352
1373
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1353
1374
|
constructor() {
|
|
@@ -1365,9 +1386,13 @@ var Fund = class {
|
|
|
1365
1386
|
} else {
|
|
1366
1387
|
formatProvider = new import_ethers2.ethers.providers.Web3Provider(provider);
|
|
1367
1388
|
}
|
|
1389
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1390
|
+
console.log("getGasPrice=", gasPrice);
|
|
1391
|
+
await formatProvider.ready;
|
|
1368
1392
|
const network = await formatProvider.getNetwork();
|
|
1369
1393
|
const providerChainId = network.chainId;
|
|
1370
1394
|
console.log("init provider", provider, network);
|
|
1395
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1371
1396
|
if (providerChainId !== chainId) {
|
|
1372
1397
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1373
1398
|
}
|
|
@@ -1377,11 +1402,26 @@ var Fund = class {
|
|
|
1377
1402
|
}
|
|
1378
1403
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1379
1404
|
this.provider = provider;
|
|
1405
|
+
this.formatProvider = formatProvider;
|
|
1380
1406
|
this.chainId = chainId;
|
|
1381
1407
|
if (appId) {
|
|
1382
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);
|
|
1383
1421
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1384
|
-
appId
|
|
1422
|
+
appId,
|
|
1423
|
+
"",
|
|
1424
|
+
{ platform: platformDevice }
|
|
1385
1425
|
);
|
|
1386
1426
|
resolve(extensionVersion);
|
|
1387
1427
|
} else {
|
|
@@ -1401,6 +1441,7 @@ var Fund = class {
|
|
|
1401
1441
|
let params = [];
|
|
1402
1442
|
if (tokenInfo.tokenType === 0) {
|
|
1403
1443
|
await this.approve(tokenInfo, recipientInfos);
|
|
1444
|
+
console.log("after approve in fund fn");
|
|
1404
1445
|
const web3Provider = new import_ethers2.ethers.providers.Web3Provider(this.provider);
|
|
1405
1446
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1406
1447
|
decimals = await erc20Contract.decimals();
|
|
@@ -1421,6 +1462,51 @@ var Fund = class {
|
|
|
1421
1462
|
} else {
|
|
1422
1463
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1423
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);
|
|
1424
1510
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1425
1511
|
resolve(result);
|
|
1426
1512
|
} catch (error) {
|
|
@@ -1438,7 +1524,14 @@ var Fund = class {
|
|
|
1438
1524
|
tipTimestamp: i.tipTimestamp
|
|
1439
1525
|
};
|
|
1440
1526
|
});
|
|
1441
|
-
|
|
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);
|
|
1442
1535
|
return resolve(result);
|
|
1443
1536
|
} catch (error) {
|
|
1444
1537
|
return reject(error);
|
|
@@ -1475,6 +1568,10 @@ var Fund = class {
|
|
|
1475
1568
|
} else {
|
|
1476
1569
|
params = [tokenInfo, newRecipientInfoList];
|
|
1477
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
|
+
}
|
|
1478
1575
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1479
1576
|
return resolve(result);
|
|
1480
1577
|
} catch (error) {
|
|
@@ -1482,6 +1579,7 @@ var Fund = class {
|
|
|
1482
1579
|
}
|
|
1483
1580
|
});
|
|
1484
1581
|
}
|
|
1582
|
+
// TODO-nft
|
|
1485
1583
|
async approve(tokenInfo, recipientInfoList) {
|
|
1486
1584
|
return new Promise(async (resolve, reject) => {
|
|
1487
1585
|
try {
|
|
@@ -1491,9 +1589,15 @@ var Fund = class {
|
|
|
1491
1589
|
const erc20Contract = new import_ethers2.ethers.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1492
1590
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1493
1591
|
const decimals = await erc20Contract.decimals();
|
|
1592
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1494
1593
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), import_ethers2.ethers.BigNumber.from(0));
|
|
1495
1594
|
if (allowance.lt(requiredAllowance)) {
|
|
1496
|
-
|
|
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);
|
|
1497
1601
|
await tx.wait();
|
|
1498
1602
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1499
1603
|
} else {
|
|
@@ -1509,11 +1613,9 @@ var Fund = class {
|
|
|
1509
1613
|
}
|
|
1510
1614
|
});
|
|
1511
1615
|
}
|
|
1512
|
-
async attest(attestParams, genAppSignature) {
|
|
1616
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1513
1617
|
return new Promise(async (resolve, reject) => {
|
|
1514
|
-
|
|
1515
|
-
return reject(`Uninitialized!`);
|
|
1516
|
-
}
|
|
1618
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1517
1619
|
if (this._attestLoading) {
|
|
1518
1620
|
return reject(`Under proof!`);
|
|
1519
1621
|
}
|
|
@@ -1524,6 +1626,10 @@ var Fund = class {
|
|
|
1524
1626
|
templateId,
|
|
1525
1627
|
address
|
|
1526
1628
|
);
|
|
1629
|
+
if (backUrl) {
|
|
1630
|
+
attRequest.setBackUrl(backUrl);
|
|
1631
|
+
}
|
|
1632
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1527
1633
|
attRequest.setAttConditions([
|
|
1528
1634
|
[
|
|
1529
1635
|
{
|
|
@@ -1568,7 +1674,12 @@ var Fund = class {
|
|
|
1568
1674
|
return reject(`No fund records.`);
|
|
1569
1675
|
} else {
|
|
1570
1676
|
const totalFee = claimFee.mul(recordCount);
|
|
1571
|
-
|
|
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);
|
|
1572
1683
|
return resolve(txreceipt);
|
|
1573
1684
|
}
|
|
1574
1685
|
} catch (error) {
|
|
@@ -1594,7 +1705,12 @@ var Fund = class {
|
|
|
1594
1705
|
return reject(`No fund records.`);
|
|
1595
1706
|
} else {
|
|
1596
1707
|
const totalFee = claimFee.mul(recordCount);
|
|
1597
|
-
|
|
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);
|
|
1598
1714
|
return resolve(txreceipt);
|
|
1599
1715
|
}
|
|
1600
1716
|
} catch (error) {
|
|
@@ -1644,7 +1760,8 @@ var Fund = class {
|
|
|
1644
1760
|
// tokenAmount: formatUnits(amount, decimals),
|
|
1645
1761
|
decimals,
|
|
1646
1762
|
symbol,
|
|
1647
|
-
chainName: CHAINNAMES[this.chainId]
|
|
1763
|
+
chainName: CHAINNAMES[this.chainId],
|
|
1764
|
+
chainId: this.chainId
|
|
1648
1765
|
};
|
|
1649
1766
|
if (tokenType === 0) {
|
|
1650
1767
|
fundToken.tokenAddress = tokenAddress;
|
|
@@ -1726,6 +1843,74 @@ var PrimusFund = class {
|
|
|
1726
1843
|
}
|
|
1727
1844
|
});
|
|
1728
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
|
+
}
|
|
1729
1914
|
async refund(recipients) {
|
|
1730
1915
|
return new Promise(async (resolve, reject) => {
|
|
1731
1916
|
try {
|
|
@@ -1746,7 +1931,7 @@ var PrimusFund = class {
|
|
|
1746
1931
|
}
|
|
1747
1932
|
});
|
|
1748
1933
|
}
|
|
1749
|
-
async attest(attestParams, genAppSignature) {
|
|
1934
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1750
1935
|
return new Promise(async (resolve, reject) => {
|
|
1751
1936
|
try {
|
|
1752
1937
|
const { socialPlatform } = attestParams;
|
|
@@ -1754,7 +1939,7 @@ var PrimusFund = class {
|
|
|
1754
1939
|
const attestation = await this._fund?.attest({
|
|
1755
1940
|
...attestParams,
|
|
1756
1941
|
socialPlatform: lcSocialPlatform
|
|
1757
|
-
}, genAppSignature);
|
|
1942
|
+
}, genAppSignature, backUrl);
|
|
1758
1943
|
return resolve(attestation);
|
|
1759
1944
|
} catch (error) {
|
|
1760
1945
|
return reject(error);
|
package/dist/index.mjs
CHANGED
|
@@ -35,8 +35,28 @@ var SUPPORTEDCHAINIDSMAP = {
|
|
|
35
35
|
decimals: 18
|
|
36
36
|
},
|
|
37
37
|
contractAddress: "0xcd1Ed9C1595A7e9DADe76808dd5e66aA95940A92"
|
|
38
|
-
}
|
|
38
|
+
},
|
|
39
39
|
// monad testnet
|
|
40
|
+
97: {
|
|
41
|
+
chainId: 97,
|
|
42
|
+
chainName: "BNB Testnet",
|
|
43
|
+
nativeCurrency: {
|
|
44
|
+
decimals: 18,
|
|
45
|
+
name: "tBNB",
|
|
46
|
+
symbol: "tBNB"
|
|
47
|
+
},
|
|
48
|
+
contractAddress: "0x1C5bfc91789DB3130A07a06407E02745945C3218"
|
|
49
|
+
},
|
|
50
|
+
56: {
|
|
51
|
+
chainId: 56,
|
|
52
|
+
chainName: "BNB Chain",
|
|
53
|
+
nativeCurrency: {
|
|
54
|
+
decimals: 18,
|
|
55
|
+
name: "BNB",
|
|
56
|
+
symbol: "BNB"
|
|
57
|
+
},
|
|
58
|
+
contractAddress: "0x1fb86db904caF7c12100EA64024E5dfd7505E484"
|
|
59
|
+
}
|
|
40
60
|
};
|
|
41
61
|
var NATIVETOKENS = Object.values(SUPPORTEDCHAINIDSMAP).reduce((prev, curr) => {
|
|
42
62
|
return {
|
|
@@ -134,7 +154,7 @@ var Contract = class {
|
|
|
134
154
|
if (isNoPendingWithdrawals) {
|
|
135
155
|
return reject("no pending withdrawals");
|
|
136
156
|
}
|
|
137
|
-
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit"];
|
|
157
|
+
const insufficientBalanceErrStrArr = ["insufficient balance", "unpredictable_gas_limit", "INSUFFICIENT_FUNDS"];
|
|
138
158
|
const isInsufficientBalance = hasErrorFlagFn(curErrorStrArr, insufficientBalanceErrStrArr);
|
|
139
159
|
if (isInsufficientBalance) {
|
|
140
160
|
return reject("insufficient balance");
|
|
@@ -1324,6 +1344,7 @@ var Fund = class {
|
|
|
1324
1344
|
zkTlsSdk;
|
|
1325
1345
|
fundContract;
|
|
1326
1346
|
provider;
|
|
1347
|
+
formatProvider;
|
|
1327
1348
|
chainId;
|
|
1328
1349
|
_dataSourceTemplateMap = DATASOURCETEMPLATESMAP;
|
|
1329
1350
|
constructor() {
|
|
@@ -1341,9 +1362,13 @@ var Fund = class {
|
|
|
1341
1362
|
} else {
|
|
1342
1363
|
formatProvider = new ethers2.providers.Web3Provider(provider);
|
|
1343
1364
|
}
|
|
1365
|
+
const gasPrice = await formatProvider.getGasPrice();
|
|
1366
|
+
console.log("getGasPrice=", gasPrice);
|
|
1367
|
+
await formatProvider.ready;
|
|
1344
1368
|
const network = await formatProvider.getNetwork();
|
|
1345
1369
|
const providerChainId = network.chainId;
|
|
1346
1370
|
console.log("init provider", provider, network);
|
|
1371
|
+
console.log("init providerChainId", providerChainId, chainId);
|
|
1347
1372
|
if (providerChainId !== chainId) {
|
|
1348
1373
|
return reject(`Please connect to the chain with ID ${chainId} first.`);
|
|
1349
1374
|
}
|
|
@@ -1353,11 +1378,26 @@ var Fund = class {
|
|
|
1353
1378
|
}
|
|
1354
1379
|
this.fundContract = new Contract_default(provider, fundContractAddress, abi_default);
|
|
1355
1380
|
this.provider = provider;
|
|
1381
|
+
this.formatProvider = formatProvider;
|
|
1356
1382
|
this.chainId = chainId;
|
|
1357
1383
|
if (appId) {
|
|
1358
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);
|
|
1359
1397
|
const extensionVersion = await this.zkTlsSdk.init(
|
|
1360
|
-
appId
|
|
1398
|
+
appId,
|
|
1399
|
+
"",
|
|
1400
|
+
{ platform: platformDevice }
|
|
1361
1401
|
);
|
|
1362
1402
|
resolve(extensionVersion);
|
|
1363
1403
|
} else {
|
|
@@ -1377,6 +1417,7 @@ var Fund = class {
|
|
|
1377
1417
|
let params = [];
|
|
1378
1418
|
if (tokenInfo.tokenType === 0) {
|
|
1379
1419
|
await this.approve(tokenInfo, recipientInfos);
|
|
1420
|
+
console.log("after approve in fund fn");
|
|
1380
1421
|
const web3Provider = new ethers2.providers.Web3Provider(this.provider);
|
|
1381
1422
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, web3Provider);
|
|
1382
1423
|
decimals = await erc20Contract.decimals();
|
|
@@ -1397,6 +1438,51 @@ var Fund = class {
|
|
|
1397
1438
|
} else {
|
|
1398
1439
|
params = [tokenInfo, newFundRecipientInfo];
|
|
1399
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);
|
|
1400
1486
|
const result = await this.fundContract.sendTransaction("tip", params);
|
|
1401
1487
|
resolve(result);
|
|
1402
1488
|
} catch (error) {
|
|
@@ -1414,7 +1500,14 @@ var Fund = class {
|
|
|
1414
1500
|
tipTimestamp: i.tipTimestamp
|
|
1415
1501
|
};
|
|
1416
1502
|
});
|
|
1417
|
-
|
|
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);
|
|
1418
1511
|
return resolve(result);
|
|
1419
1512
|
} catch (error) {
|
|
1420
1513
|
return reject(error);
|
|
@@ -1451,6 +1544,10 @@ var Fund = class {
|
|
|
1451
1544
|
} else {
|
|
1452
1545
|
params = [tokenInfo, newRecipientInfoList];
|
|
1453
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
|
+
}
|
|
1454
1551
|
const result = await this.fundContract.sendTransaction("tipBatch", params);
|
|
1455
1552
|
return resolve(result);
|
|
1456
1553
|
} catch (error) {
|
|
@@ -1458,6 +1555,7 @@ var Fund = class {
|
|
|
1458
1555
|
}
|
|
1459
1556
|
});
|
|
1460
1557
|
}
|
|
1558
|
+
// TODO-nft
|
|
1461
1559
|
async approve(tokenInfo, recipientInfoList) {
|
|
1462
1560
|
return new Promise(async (resolve, reject) => {
|
|
1463
1561
|
try {
|
|
@@ -1467,9 +1565,15 @@ var Fund = class {
|
|
|
1467
1565
|
const erc20Contract = new ethers2.Contract(tokenInfo.tokenAddress, erc20Abi_default, signer);
|
|
1468
1566
|
const allowance = await erc20Contract.allowance(address, this.fundContract.address);
|
|
1469
1567
|
const decimals = await erc20Contract.decimals();
|
|
1568
|
+
console.log("allowance", formatUnits(allowance.toString(), decimals));
|
|
1470
1569
|
const requiredAllowance = recipientInfoList.reduce((acc, cur) => acc.add(parseUnits(cur.tokenAmount.toString(), decimals)), ethers2.BigNumber.from(0));
|
|
1471
1570
|
if (allowance.lt(requiredAllowance)) {
|
|
1472
|
-
|
|
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);
|
|
1473
1577
|
await tx.wait();
|
|
1474
1578
|
console.log(`Approved: ${requiredAllowance.toString()}`);
|
|
1475
1579
|
} else {
|
|
@@ -1485,11 +1589,9 @@ var Fund = class {
|
|
|
1485
1589
|
}
|
|
1486
1590
|
});
|
|
1487
1591
|
}
|
|
1488
|
-
async attest(attestParams, genAppSignature) {
|
|
1592
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1489
1593
|
return new Promise(async (resolve, reject) => {
|
|
1490
|
-
|
|
1491
|
-
return reject(`Uninitialized!`);
|
|
1492
|
-
}
|
|
1594
|
+
console.log("this.zkTlsSdk", this.zkTlsSdk);
|
|
1493
1595
|
if (this._attestLoading) {
|
|
1494
1596
|
return reject(`Under proof!`);
|
|
1495
1597
|
}
|
|
@@ -1500,6 +1602,10 @@ var Fund = class {
|
|
|
1500
1602
|
templateId,
|
|
1501
1603
|
address
|
|
1502
1604
|
);
|
|
1605
|
+
if (backUrl) {
|
|
1606
|
+
attRequest.setBackUrl(backUrl);
|
|
1607
|
+
}
|
|
1608
|
+
console.log(`attRequest: ${JSON.stringify(attRequest)}`);
|
|
1503
1609
|
attRequest.setAttConditions([
|
|
1504
1610
|
[
|
|
1505
1611
|
{
|
|
@@ -1544,7 +1650,12 @@ var Fund = class {
|
|
|
1544
1650
|
return reject(`No fund records.`);
|
|
1545
1651
|
} else {
|
|
1546
1652
|
const totalFee = claimFee.mul(recordCount);
|
|
1547
|
-
|
|
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);
|
|
1548
1659
|
return resolve(txreceipt);
|
|
1549
1660
|
}
|
|
1550
1661
|
} catch (error) {
|
|
@@ -1570,7 +1681,12 @@ var Fund = class {
|
|
|
1570
1681
|
return reject(`No fund records.`);
|
|
1571
1682
|
} else {
|
|
1572
1683
|
const totalFee = claimFee.mul(recordCount);
|
|
1573
|
-
|
|
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);
|
|
1574
1690
|
return resolve(txreceipt);
|
|
1575
1691
|
}
|
|
1576
1692
|
} catch (error) {
|
|
@@ -1620,7 +1736,8 @@ var Fund = class {
|
|
|
1620
1736
|
// tokenAmount: formatUnits(amount, decimals),
|
|
1621
1737
|
decimals,
|
|
1622
1738
|
symbol,
|
|
1623
|
-
chainName: CHAINNAMES[this.chainId]
|
|
1739
|
+
chainName: CHAINNAMES[this.chainId],
|
|
1740
|
+
chainId: this.chainId
|
|
1624
1741
|
};
|
|
1625
1742
|
if (tokenType === 0) {
|
|
1626
1743
|
fundToken.tokenAddress = tokenAddress;
|
|
@@ -1702,6 +1819,74 @@ var PrimusFund = class {
|
|
|
1702
1819
|
}
|
|
1703
1820
|
});
|
|
1704
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
|
+
}
|
|
1705
1890
|
async refund(recipients) {
|
|
1706
1891
|
return new Promise(async (resolve, reject) => {
|
|
1707
1892
|
try {
|
|
@@ -1722,7 +1907,7 @@ var PrimusFund = class {
|
|
|
1722
1907
|
}
|
|
1723
1908
|
});
|
|
1724
1909
|
}
|
|
1725
|
-
async attest(attestParams, genAppSignature) {
|
|
1910
|
+
async attest(attestParams, genAppSignature, backUrl) {
|
|
1726
1911
|
return new Promise(async (resolve, reject) => {
|
|
1727
1912
|
try {
|
|
1728
1913
|
const { socialPlatform } = attestParams;
|
|
@@ -1730,7 +1915,7 @@ var PrimusFund = class {
|
|
|
1730
1915
|
const attestation = await this._fund?.attest({
|
|
1731
1916
|
...attestParams,
|
|
1732
1917
|
socialPlatform: lcSocialPlatform
|
|
1733
|
-
}, genAppSignature);
|
|
1918
|
+
}, genAppSignature, backUrl);
|
|
1734
1919
|
return resolve(attestation);
|
|
1735
1920
|
} catch (error) {
|
|
1736
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
|
}
|