eth价格从cryptocompare获取

This commit is contained in:
CounterFire2023 2023-07-19 15:11:21 +08:00
parent 3c7ff25fac
commit 6a5d256ebe

View File

@ -8,12 +8,11 @@ import logger from 'logger/logger'
export class PriceSvr {
private priceMap: Map<string, string> = new Map()
public async queryEthPrice(eth: string) {
const usd = 'USD'
public async queryEthPrice(eth: string, fiat: string = 'USD') {
const apiKey = process.env.CRYPTOCOMPARE_API_KEY
const url = `https://min-api.cryptocompare.com/data/price?fsym=${eth}&tsyms=${usd}&api_key=${apiKey}`
const url = `https://min-api.cryptocompare.com/data/price?fsym=${eth}&tsyms=${fiat}&api_key=${apiKey}`
let priceData = await axios.get(url).then(res => res.data)
let price = priceData[usd] + ''
let price = priceData[fiat] + ''
return price
}
@ -32,18 +31,20 @@ export class PriceSvr {
let { crypto, network, fiat } = data
let key = `${crypto}_${network}_${fiat}`
try {
if (crypto.toLowerCase() === 'eth') {
let price = await this.queryEthPrice(crypto, fiat)
this.priceMap.set(key, price + '')
} else {
let priceData = await queryPrice(data)
if (priceData.success && priceData.data) {
this.priceMap.set(key, priceData.data.cryptoPrice + '')
} else if (crypto.toLowerCase() === 'eth') {
let price = await this.queryEthPrice(crypto)
this.priceMap.set(key, price + '')
}
}
} catch (e) {
logger.debug('update eth price with alchemy error: ' + e.message || e)
logger.debug('update eth price with error: ' + e.message || e)
if (crypto.toLowerCase() === 'eth') {
try {
let price = await this.queryEthPrice(crypto)
let price = await this.queryEthPrice(crypto, fiat)
this.priceMap.set(key, price + '')
} catch (e) {
logger.debug('update eth price with cryptocompare error: ' + e.message || e)