增加获取支持的法币列表的接口
This commit is contained in:
parent
460477293d
commit
6cc9b2ee3a
@ -1,5 +1,5 @@
|
|||||||
import { WALLET_API_HOST } from "../config/constants";
|
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) {
|
export function reqAlchemyPrePay(data: any) {
|
||||||
const url = `${WALLET_API_HOST}/pay/alchemy/buy`;
|
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`;
|
const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`;
|
||||||
return POST_JSON(url, { token: eth, chain });
|
return POST_JSON(url, { token: eth, chain });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function queryFiatList() {
|
||||||
|
const url = `${WALLET_API_HOST}/pay/alchemy/fait_list`;
|
||||||
|
return GET_JSON(url);
|
||||||
|
}
|
||||||
|
32
src/index.ts
32
src/index.ts
@ -40,7 +40,11 @@ import { JCStandard } from "./standards/JCStandard";
|
|||||||
import { WalletType } from "./types/data.enums";
|
import { WalletType } from "./types/data.enums";
|
||||||
import { IChainData } from "./types/data.types";
|
import { IChainData } from "./types/data.types";
|
||||||
import { universalChainCb } from "./util/chain.util";
|
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";
|
import { buildLoginSignMsg, signLogin } from "./util/sign.util";
|
||||||
|
|
||||||
var global =
|
var global =
|
||||||
@ -293,13 +297,27 @@ export default class JCWallet {
|
|||||||
let price = await new PaySvr().getGasPrice(chainData.id);
|
let price = await new PaySvr().getGasPrice(chainData.id);
|
||||||
let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas));
|
let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas));
|
||||||
let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH";
|
let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH";
|
||||||
let leagelPrice = await new PaySvr().queryEthPrice(chainData.id, ethSymbol);
|
let fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, ethSymbol);
|
||||||
let priceFloat = parseFloat(leagelPrice) * 100;
|
let priceFloat = parseFloat(fiatPrice) * 100;
|
||||||
let leagelPriceBN = safeNumberToBN(priceFloat);
|
let fiatPriceBN = safeNumberToBN(priceFloat);
|
||||||
let usd = fromTokenMinimalUnit(ehtBN.mul(leagelPriceBN), 20);
|
let usd = fromTokenMinimalUnit(ehtBN.mul(fiatPriceBN), 20);
|
||||||
let eth = fromTokenMinimalUnit(ehtBN, 18);
|
let eth = fromTokenMinimalUnit(ehtBN, 18);
|
||||||
return { gas, price, eth, usd };
|
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) {
|
public async sendEth(to: string, amount: number | string, estimate: number) {
|
||||||
let from = this.currentAccAddr;
|
let from = this.currentAccAddr;
|
||||||
@ -386,6 +404,10 @@ export default class JCWallet {
|
|||||||
return icon.init(msg, diameter);
|
return icon.init(msg, diameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public formatPrice(value: string | number, decimal: number, fixed: number) {
|
||||||
|
return renderFromTokenMinimalUnit(value, decimal, fixed);
|
||||||
|
}
|
||||||
|
|
||||||
public async erc20Info(address: string) {
|
public async erc20Info(address: string) {
|
||||||
let symbol = await this.erc20Standard.getTokenSymbol(address);
|
let symbol = await this.erc20Standard.getTokenSymbol(address);
|
||||||
let decimal = await this.erc20Standard.getTokenDecimals(address);
|
let decimal = await this.erc20Standard.getTokenDecimals(address);
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
import { queryEthUsdPrice, reqAlchemyPrePay } from "../api/PayApi";
|
import {
|
||||||
|
queryEthUsdPrice,
|
||||||
|
queryFiatList,
|
||||||
|
reqAlchemyPrePay,
|
||||||
|
} from "../api/PayApi";
|
||||||
import { singleton } from "../decorator/singleton.decorator";
|
import { singleton } from "../decorator/singleton.decorator";
|
||||||
import { IPayData } from "../types/data.types";
|
import { IPayData } from "../types/data.types";
|
||||||
|
|
||||||
@ -42,25 +46,28 @@ export class PaySvr {
|
|||||||
return this.priceMap.get(key);
|
return this.priceMap.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async queryEthPrice(chainId: number, eth: string) {
|
private async updateTokenPrice(chain: string, tokenName: string) {
|
||||||
const usd = "USD";
|
const key = `crypto_usd_${chain}_${tokenName}`;
|
||||||
const key = `crypto_usd_${chainId}_${eth}`;
|
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)) {
|
if (!this.priceMap.has(key)) {
|
||||||
try {
|
try {
|
||||||
let priceData = await queryEthUsdPrice(eth, "ETH");
|
await this.updateTokenPrice(chain, tokenName);
|
||||||
console.log("ETH price data", JSON.stringify(priceData));
|
|
||||||
let price = priceData.data.price;
|
|
||||||
this.priceMap.set(key, price + "");
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("ETH price err", err);
|
console.log("ETH price err", err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setImmediate(async () => {
|
setImmediate(async () => {
|
||||||
try {
|
try {
|
||||||
let priceData = await queryEthUsdPrice(eth, "ETH");
|
await this.updateTokenPrice(chain, tokenName);
|
||||||
console.log("ETH price data", JSON.stringify(priceData));
|
|
||||||
let price = priceData.data.price;
|
|
||||||
this.priceMap.set(key, price + "");
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("ETH price err", err);
|
console.log("ETH price err", err);
|
||||||
}
|
}
|
||||||
@ -69,4 +76,8 @@ export class PaySvr {
|
|||||||
console.log("ETH price", this.priceMap.get(key));
|
console.log("ETH price", this.priceMap.get(key));
|
||||||
return this.priceMap.get(key);
|
return this.priceMap.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async fetchFiatList() {
|
||||||
|
return queryFiatList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user