diff --git a/src/api/PayApi.ts b/src/api/PayApi.ts index 1d1051b..a972de1 100644 --- a/src/api/PayApi.ts +++ b/src/api/PayApi.ts @@ -1,5 +1,5 @@ import { WALLET_API_HOST } from "../config/constants"; -import { POST_JSON } from "../lib/Http"; +import { GET_JSON, POST_JSON } from "../lib/Http"; export function reqAlchemyPrePay(data: any) { const url = `${WALLET_API_HOST}/pay/alchemy/buy`; @@ -10,3 +10,8 @@ export function queryEthUsdPrice(eth: string, chain: string) { const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`; return POST_JSON(url, { token: eth, chain }); } + +export function queryFiatList() { + const url = `${WALLET_API_HOST}/pay/alchemy/fait_list`; + return GET_JSON(url); +} diff --git a/src/index.ts b/src/index.ts index 8f39ed0..cafaa8a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,11 @@ 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, safeNumberToBN } from "./util/number.util"; +import { + fromTokenMinimalUnit, + renderFromTokenMinimalUnit, + safeNumberToBN, +} from "./util/number.util"; import { buildLoginSignMsg, signLogin } from "./util/sign.util"; var global = @@ -293,13 +297,27 @@ 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 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 fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, ethSymbol); + let priceFloat = parseFloat(fiatPrice) * 100; + let fiatPriceBN = safeNumberToBN(priceFloat); + let usd = fromTokenMinimalUnit(ehtBN.mul(fiatPriceBN), 20); let eth = fromTokenMinimalUnit(ehtBN, 18); return { gas, price, eth, usd }; } + /** + * 查询token的法币价格 + * token 必须已经在去中心化交易所上架 + * @param tokenName token名称 + * @param amount token数量 + * @returns + */ + 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 priceFloat = parseFloat(fiatPrice) * 100; + return priceFloat * parseFloat(amount); + } public async sendEth(to: string, amount: number | string, estimate: number) { let from = this.currentAccAddr; @@ -386,6 +404,10 @@ export default class JCWallet { return icon.init(msg, diameter); } + public formatPrice(value: string | number, decimal: number, fixed: number) { + return renderFromTokenMinimalUnit(value, decimal, fixed); + } + public async erc20Info(address: string) { let symbol = await this.erc20Standard.getTokenSymbol(address); let decimal = await this.erc20Standard.getTokenDecimals(address); diff --git a/src/services/PaySvr.ts b/src/services/PaySvr.ts index 10a0094..feca2d7 100644 --- a/src/services/PaySvr.ts +++ b/src/services/PaySvr.ts @@ -1,4 +1,8 @@ -import { queryEthUsdPrice, reqAlchemyPrePay } from "../api/PayApi"; +import { + queryEthUsdPrice, + queryFiatList, + reqAlchemyPrePay, +} from "../api/PayApi"; import { singleton } from "../decorator/singleton.decorator"; import { IPayData } from "../types/data.types"; @@ -42,25 +46,28 @@ export class PaySvr { return this.priceMap.get(key); } - public async queryEthPrice(chainId: number, eth: string) { - const usd = "USD"; - const key = `crypto_usd_${chainId}_${eth}`; + private async updateTokenPrice(chain: string, tokenName: string) { + const key = `crypto_usd_${chain}_${tokenName}`; + let priceData = await queryEthUsdPrice(tokenName, chain); + 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) { + chain = chain.toUpperCase(); + tokenName = tokenName.toUpperCase(); + const key = `crypto_usd_${chain}_${tokenName}`; 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 + ""); + await this.updateTokenPrice(chain, tokenName); } 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 + ""); + await this.updateTokenPrice(chain, tokenName); } catch (err) { console.log("ETH price err", err); } @@ -69,4 +76,8 @@ export class PaySvr { console.log("ETH price", this.priceMap.get(key)); return this.priceMap.get(key); } + + public async fetchFiatList() { + return queryFiatList(); + } }