From 460477293d5ea8d3c86fa68f59f6fcdd7c3b0584 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 26 May 2023 14:09:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96eth?= =?UTF-8?q?=E4=B8=8E=E7=BE=8E=E5=85=83=E5=85=91=E6=8D=A2=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/PayApi.ts | 5 ++++ src/config/cfg_421613_dev.js | 6 ++--- src/index.ts | 16 +++++++++---- src/services/PaySvr.ts | 46 +++++++++++++++++++++++++++++++++++- src/util/chain.util.ts | 4 ++-- 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/api/PayApi.ts b/src/api/PayApi.ts index 2f16894..1d1051b 100644 --- a/src/api/PayApi.ts +++ b/src/api/PayApi.ts @@ -5,3 +5,8 @@ export function reqAlchemyPrePay(data: any) { const url = `${WALLET_API_HOST}/pay/alchemy/buy`; return POST_JSON(url, data); } + +export function queryEthUsdPrice(eth: string, chain: string) { + const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`; + return POST_JSON(url, { token: eth, chain }); +} diff --git a/src/config/cfg_421613_dev.js b/src/config/cfg_421613_dev.js index 6c8a6b6..298fe8c 100644 --- a/src/config/cfg_421613_dev.js +++ b/src/config/cfg_421613_dev.js @@ -8,18 +8,18 @@ module.exports = { }, { type: "erc20", - address: "0xfa513999031dC1DCf86e99d91101e17d07839236", + address: "0xaa34B79A0Ab433eaC900fB3CB9f191F5Cd27501D", symbol: "CEC", decimal: 18, }, { type: "erc20", - address: "0x9f87eCA8F0479383fF11a5AB2336b5A6c383d6F3", + address: "0xaa34B79A0Ab433eaC900fB3CB9f191F5Cd27501D", symbol: "CEG", decimal: 18, }, { - address: "0x3EBF5196dADC8F3F09C808333f98FE8A4b7d1e62", + address: "0xB469331cEC98E52b7Eab07dFB586253bE232BBF7", name: "hero", type: "erc721", }, diff --git a/src/index.ts b/src/index.ts index 81a3d69..8f39ed0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,7 @@ import { JCStandard } from "./standards/JCStandard"; import { WalletType } from "./types/data.enums"; import { IChainData } from "./types/data.types"; import { universalChainCb } from "./util/chain.util"; -import { fromTokenMinimalUnit } from "./util/number.util"; +import { fromTokenMinimalUnit, safeNumberToBN } from "./util/number.util"; import { buildLoginSignMsg, signLogin } from "./util/sign.util"; var global = @@ -289,10 +289,16 @@ export default class JCWallet { } public async generateGasShow(gas: any) { - let price = await this.web3.eth.getGasPrice(); - let ehtBN = this.web3.utils.toBN(price).mul(this.web3.utils.toBN(gas)); + let chainData = this.currentChain; + let price = await new PaySvr().getGasPrice(chainData.id); + let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas)); + let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH"; + let leagelPrice = await new PaySvr().queryEthPrice(chainData.id, ethSymbol); + let priceFloat = parseFloat(leagelPrice) * 100; + let leagelPriceBN = safeNumberToBN(priceFloat); + let usd = fromTokenMinimalUnit(ehtBN.mul(leagelPriceBN), 20); let eth = fromTokenMinimalUnit(ehtBN, 18); - return { gas, price, eth }; + return { gas, price, eth, usd }; } public async sendEth(to: string, amount: number | string, estimate: number) { @@ -301,7 +307,7 @@ export default class JCWallet { let gas = await this.web3.eth.estimateGas({ from, to, - value: amountToSend, + value: "0", }); if (estimate) { return this.generateGasShow(gas); diff --git a/src/services/PaySvr.ts b/src/services/PaySvr.ts index f72e7d6..10a0094 100644 --- a/src/services/PaySvr.ts +++ b/src/services/PaySvr.ts @@ -1,9 +1,10 @@ -import { reqAlchemyPrePay } from "../api/PayApi"; +import { queryEthUsdPrice, reqAlchemyPrePay } from "../api/PayApi"; import { singleton } from "../decorator/singleton.decorator"; import { IPayData } from "../types/data.types"; @singleton export class PaySvr { + private priceMap: Map = new Map(); /** * Calls the alchemyPrePay function with the given data. * @param data - The data to be passed to the alchemyPrePay function. @@ -25,4 +26,47 @@ export class PaySvr { // jsb.openURL(url); return true; } + + public async getGasPrice(chainId: number) { + const key = `gasprice_${chainId}`; + if (!this.priceMap.has(key)) { + let price = await new jc.wallet.web3.eth.getGasPrice(); + this.priceMap.set(key, price + ""); + } else { + setImmediate(async () => { + let price = await new jc.wallet.web3.eth.getGasPrice(); + this.priceMap.set(key, price + ""); + }); + } + console.log("gas price", this.priceMap.get(key)); + return this.priceMap.get(key); + } + + public async queryEthPrice(chainId: number, eth: string) { + const usd = "USD"; + const key = `crypto_usd_${chainId}_${eth}`; + if (!this.priceMap.has(key)) { + try { + let priceData = await queryEthUsdPrice(eth, "ETH"); + console.log("ETH price data", JSON.stringify(priceData)); + let price = priceData.data.price; + this.priceMap.set(key, price + ""); + } catch (err) { + console.log("ETH price err", err); + } + } else { + setImmediate(async () => { + try { + let priceData = await queryEthUsdPrice(eth, "ETH"); + console.log("ETH price data", JSON.stringify(priceData)); + let price = priceData.data.price; + this.priceMap.set(key, price + ""); + } catch (err) { + console.log("ETH price err", err); + } + }); + } + console.log("ETH price", this.priceMap.get(key)); + return this.priceMap.get(key); + } } diff --git a/src/util/chain.util.ts b/src/util/chain.util.ts index f9b770c..01efb44 100644 --- a/src/util/chain.util.ts +++ b/src/util/chain.util.ts @@ -10,7 +10,7 @@ export const UNKNOW = "unknow"; * @param {number} decimals * @return {string} */ -export function parsePrice(price: number, decimals: number) { +export function parsePrice(price: number, decimals: number): string { const n = 19 - decimals; return price + new Array(n).join("0"); } @@ -26,7 +26,7 @@ export function formatPrice( price: number | string, decimals?: number, fixed = 2 -) { +): string | number { if (!decimals) { return price; }