增加获取eth与美元兑换价格的接口

This commit is contained in:
zhl 2023-05-26 14:09:02 +08:00
parent a316fc750f
commit 460477293d
5 changed files with 66 additions and 11 deletions

View File

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

View File

@ -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",
},

View File

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

View File

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

View File

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