diff --git a/src/api/PayApi.ts b/src/api/PayApi.ts index a972de1..5b31ff6 100644 --- a/src/api/PayApi.ts +++ b/src/api/PayApi.ts @@ -6,9 +6,9 @@ export function reqAlchemyPrePay(data: any) { return POST_JSON(url, data); } -export function queryEthUsdPrice(eth: string, chain: string) { +export function queryTokenUsdPrice(eth: string, chain: string, env: string) { const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`; - return POST_JSON(url, { token: eth, chain }); + return POST_JSON(url, { token: eth, chain, env }); } export function queryFiatList() { diff --git a/src/config/cfg_421613_dev.js b/src/config/cfg_421613_dev.js index 298fe8c..b5b6628 100644 --- a/src/config/cfg_421613_dev.js +++ b/src/config/cfg_421613_dev.js @@ -3,7 +3,7 @@ module.exports = { { type: "eth", address: "eth", - symbol: "MATIC", + symbol: "AGOR", decimal: 18, }, { diff --git a/src/data/allchain.ts b/src/data/allchain.ts index 5eb39f1..2b4b0de 100644 --- a/src/data/allchain.ts +++ b/src/data/allchain.ts @@ -212,9 +212,19 @@ export const AllChains = [ type: "Mainnet", rpc: "https://rpc.ankr.com/arbitrum", id: 42161, + network: "ARBITRUM", symbol: "ETH", explorerurl: "https://arbiscan.io/", }, + { + name: "Arbitrum Goerli", + type: "Testnet", + rpc: "https://endpoints.omniatech.io/v1/arbitrum/goerli/public", + id: 421613, + network: "AGOR", + symbol: "AGOR", + explorerurl: "https://goerli-rollup-explorer.arbitrum.io", + }, { name: "Celo Mainnet RPC", type: "Mainnet", @@ -255,14 +265,7 @@ export const AllChains = [ symbol: "MATIC", explorerurl: "https://mumbai.polygonscan.com/", }, - { - name: "Arbitrum Goerli", - type: "Testnet", - rpc: "https://endpoints.omniatech.io/v1/arbitrum/goerli/public", - id: 421613, - symbol: "AGOR", - explorerurl: "https://goerli-rollup-explorer.arbitrum.io", - }, + { name: "Harmony Mainnet RPC", type: "Mainnet", diff --git a/src/index.ts b/src/index.ts index cafaa8a..ead2a0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -297,7 +297,11 @@ export default class JCWallet { let price = await new PaySvr().getGasPrice(chainData.id); let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas)); let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH"; - let fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, ethSymbol); + let network = + chainData.type !== "Testnet" + ? chainData.network || chainData.symbol + : "ETH"; + let fiatPrice = await new PaySvr().queryTokenPrice(network, ethSymbol); let priceFloat = parseFloat(fiatPrice) * 100; let fiatPriceBN = safeNumberToBN(priceFloat); let usd = fromTokenMinimalUnit(ehtBN.mul(fiatPriceBN), 20); @@ -313,8 +317,12 @@ export default class JCWallet { */ public async calcTokenPrice(tokenName: string, amount: string) { let chainData = this.currentChain; - let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH"; - let fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, tokenName); + let network = + chainData.type !== "Testnet" + ? chainData.network || chainData.symbol + : "ETH"; + let env = this.env; + let fiatPrice = await new PaySvr().queryTokenPrice(network, tokenName, env); let priceFloat = parseFloat(fiatPrice) * 100; return priceFloat * parseFloat(amount); } diff --git a/src/services/PaySvr.ts b/src/services/PaySvr.ts index feca2d7..cc07f3c 100644 --- a/src/services/PaySvr.ts +++ b/src/services/PaySvr.ts @@ -1,6 +1,6 @@ import { - queryEthUsdPrice, queryFiatList, + queryTokenUsdPrice, reqAlchemyPrePay, } from "../api/PayApi"; import { singleton } from "../decorator/singleton.decorator"; @@ -46,28 +46,36 @@ export class PaySvr { return this.priceMap.get(key); } - private async updateTokenPrice(chain: string, tokenName: string) { - const key = `crypto_usd_${chain}_${tokenName}`; - let priceData = await queryEthUsdPrice(tokenName, chain); + private async updateTokenPrice( + chain: string, + tokenName: string, + env: string + ) { + const key = `crypto_usd_${chain}_${tokenName}_${env}`; + let priceData = await queryTokenUsdPrice(tokenName, chain, env); console.log("ETH price data", JSON.stringify(priceData)); let price = priceData.data.price; this.priceMap.set(key, price + ""); } - public async queryTokenPrice(chain: string, tokenName: string) { + public async queryTokenPrice( + chain: string, + tokenName: string, + env: string = "release" + ) { chain = chain.toUpperCase(); tokenName = tokenName.toUpperCase(); - const key = `crypto_usd_${chain}_${tokenName}`; + const key = `crypto_usd_${chain}_${tokenName}_${env}`; if (!this.priceMap.has(key)) { try { - await this.updateTokenPrice(chain, tokenName); + await this.updateTokenPrice(chain, tokenName, env); } catch (err) { console.log("ETH price err", err); } } else { setImmediate(async () => { try { - await this.updateTokenPrice(chain, tokenName); + await this.updateTokenPrice(chain, tokenName, env); } catch (err) { console.log("ETH price err", err); } diff --git a/src/standards/ERC1155Standard.ts b/src/standards/ERC1155Standard.ts index 50bbb64..d57807b 100644 --- a/src/standards/ERC1155Standard.ts +++ b/src/standards/ERC1155Standard.ts @@ -209,7 +209,7 @@ export class ERC1155Standard { if (!gas) { gas = await contract.methods .safeBatchTransferFrom(from, to, tokenIds, amounts, []) - .estimateGas({ gas: 1000000 }); + .estimateGas(); } gas = (gas * 1.1) | 1; if (estimate) { diff --git a/src/standards/ERC20Standard.ts b/src/standards/ERC20Standard.ts index 7550d7e..e0e0349 100644 --- a/src/standards/ERC20Standard.ts +++ b/src/standards/ERC20Standard.ts @@ -159,9 +159,7 @@ export class ERC20Standard { const contract = new this.web3.eth.Contract(abiERC20, address); const amountBN = Web3.utils.toBN(Web3.utils.toWei(amount + "")); if (!gas) { - gas = await contract.methods - .transfer(to, amountBN) - .estimateGas({ gas: 1000000 }); + gas = await contract.methods.transfer(to, "0").estimateGas(); } gas = (gas * 1.1) | 1; if (estimate) { diff --git a/src/standards/ERC721Standard.ts b/src/standards/ERC721Standard.ts index e8c867a..7d3c9be 100644 --- a/src/standards/ERC721Standard.ts +++ b/src/standards/ERC721Standard.ts @@ -306,7 +306,7 @@ export class ERC721Standard { if (!gas) { gas = await contract.methods .safeTransferFrom(from, to, tokenId) - .estimateGas({ gas: 1000000 }); + .estimateGas(); } gas = (gas * 1.1) | 1; if (estimate) { diff --git a/src/standards/JCStandard.ts b/src/standards/JCStandard.ts index e7cc9f3..37a6963 100644 --- a/src/standards/JCStandard.ts +++ b/src/standards/JCStandard.ts @@ -38,7 +38,7 @@ export class JCStandard { try { gas = await contract.methods .buy721NFT(addresses, values, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -115,7 +115,7 @@ export class JCStandard { try { gas = await contract.methods .buy1155NFT(addresses, values, ids, amounts, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -197,7 +197,7 @@ export class JCStandard { try { gas = await contract.methods .evolve721NFT(nftAddress, tokenIds, startTime, nonce, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -272,7 +272,7 @@ export class JCStandard { try { gas = await contract.methods .evolveChip(tokenIds, startTime, nonce, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -356,7 +356,7 @@ export class JCStandard { try { gas = await contract.methods .mintShardBatchUser(tokenIds, amounts, startTime, nonce, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -450,7 +450,7 @@ export class JCStandard { nonce, signature ) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; } @@ -565,10 +565,10 @@ export class JCStandard { try { gas1 = await chipInstance.methods .setApprovalForAll(lockerAddress, true) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); gas0 = await contract.methods .pluginChip(addresses, values, chipIds, slots, signature) - .estimateGas({ gas: SAMPLE_GAS }); + .estimateGas(); } catch (err) {} if (!gas) { gas1 = gas1 ? (gas1 * 1.1) | 1 : SAMPLE_GAS; @@ -663,7 +663,7 @@ export class JCStandard { try { gas = await contract.methods .unplugChip(addresses, values, chipIds, slots, signature) - .estimateGas({ gas: 1000000 }); + .estimateGas(); } catch (err) { console.log(err); } diff --git a/src/types/data.types.ts b/src/types/data.types.ts index cbec1f8..b33443f 100644 --- a/src/types/data.types.ts +++ b/src/types/data.types.ts @@ -3,6 +3,7 @@ export interface IChainData { type: string; rpc: string; id: number; + network?: string; symbol: string; explorerurl: string; decimals?: number;