优化crypto价格查询逻辑

This commit is contained in:
CounterFire2023 2023-07-28 16:00:31 +08:00
parent ad06ad0a65
commit 3d103e2ff9
2 changed files with 8 additions and 38 deletions

View File

@ -99,38 +99,16 @@ class AlchemyController extends BaseController {
}
@router('post /pay/alchemy/crypto_price')
async queryCryptoPrice(req, res) {
let { token, chain, currency, env } = req.params
if (!token || !chain) {
throw new ZError(11, 'token or network not found')
}
// ceg价格固定为0.1
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && token.toLowerCase() === 'ceg') {
return { price: 0.1 }
}
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && token.toLowerCase() === 'agor') {
token = 'ETH'
chain = 'ARBITRUM'
}
let data = {
crypto: token,
network: chain,
fiat: currency || 'USD',
}
let result = await new PriceSvr().fetchPrice(data)
return { price: result }
}
@router('get /pay/alchemy/crypto_price')
async queryCryptoPriceGET(req, res) {
let { crypto, chain, currency, env } = req.params
async queryCryptoPrice(req, res) {
let { token, crypto, chain, currency, env } = req.params
crypto = crypto || token
if (!crypto || !chain) {
throw new ZError(11, 'token or network not found')
}
// ceg价格固定为0.1
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && crypto.toLowerCase() === 'ceg') {
return { price: 0.1 }
if (crypto.toLowerCase() === 'ceg') {
return { price: '0.1' }
}
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && crypto.toLowerCase() === 'agor') {
crypto = 'ETH'

View File

@ -12,7 +12,7 @@ export class PriceSvr {
const apiKey = process.env.CRYPTOCOMPARE_API_KEY
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[fiat] + ''
let price = priceData[fiat] ? priceData[fiat] + '' : ''
return price
}
@ -31,8 +31,8 @@ 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)
let price = await this.queryEthPrice(crypto, fiat)
if (price) {
this.priceMap.set(key, price + '')
} else {
let priceData = await queryPrice(data)
@ -42,14 +42,6 @@ export class PriceSvr {
}
} catch (e) {
logger.debug('update eth price with error: ' + e.message || e)
if (crypto.toLowerCase() === 'eth') {
try {
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)
}
}
}
}