增加获取支持的法币列表的接口

This commit is contained in:
zhl 2023-05-30 14:54:15 +08:00
parent 460477293d
commit 6cc9b2ee3a
3 changed files with 56 additions and 18 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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();
}
}