UNPKG

@onekeyfe/blockchain-libs

Version:
134 lines 4.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Coingecko = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const array_plus_1 = require("../../basic/array-plus"); const restful_1 = require("../../basic/request/restful"); const PRESET_COINGECKO_IDS = { ethereum: 'eth', binancecoin: 'bsc', 'huobi-token': 'heco', okexchain: 'okt', }; class Coingecko { constructor() { this.lastIndex = 0; this.restfuls = [ new restful_1.RestfulRequest('https://cdn.trezor.io/dynamic/coingecko'), new restful_1.RestfulRequest('https://api.coingecko.com'), ]; } async fetchApi(path, params) { const errors = []; for (let i = 0, count = this.restfuls.length; i < count; ++i) { try { const index = (this.lastIndex + i) % count; const restful = this.restfuls[index]; const result = await restful.get(path, params); this.lastIndex = index; return result; } catch (e) { errors.push(e); } } throw errors; } async fetchBTCPrices() { const resp = await this.fetchApi('/api/v3/exchange_rates').then((i) => i.json()); const rates = (resp === null || resp === void 0 ? void 0 : resp.rates) || {}; return Object.entries(rates) .filter(([unit, rate]) => typeof rate === 'object' && rate.type === 'fiat' && Coingecko.FIATS.has(unit)) .map(([unit, rate]) => ({ coin: 'btc', unit, value: new bignumber_js_1.default(rate.value), })); } async fetchCGKIdsPrices(cgkIds, unit = 'btc') { const resp = (await this.fetchApi('/api/v3/coins/markets', { ids: Object.keys(cgkIds).join(','), vs_currency: unit, }).then((i) => i.json())) || []; return resp .filter((i) => typeof i === 'object' && i.id && i.id in cgkIds) .map((i) => ({ coin: cgkIds[i.id], unit, value: new bignumber_js_1.default(i.current_price), })); } async fetchERC20Prices(coins, unit = 'btc') { const tokenAddress2Code = coins.reduce((acc, cur) => { acc[cur.tokenAddress.toLowerCase()] = cur.code; return acc; }, {}); const prices = []; for (const tokenAddresses of (0, array_plus_1.chunked)(Object.keys(tokenAddress2Code), 100)) { const resp = (await this.fetchApi('/api/v3/simple/token_price/ethereum', { contract_addresses: tokenAddresses.join(','), vs_currencies: unit, }).then((i) => i.json())) || {}; prices.push(...Object.entries(resp) .filter(([address, rate]) => typeof address && typeof rate === 'object' && address.toLowerCase() in tokenAddress2Code) .map(([address, rate]) => ({ coin: tokenAddress2Code[address.toLowerCase()], unit, value: new bignumber_js_1.default(rate[unit]), }))); } return prices; } async pricing(coins) { const prices = []; try { const btcPrices = await this.fetchBTCPrices(); prices.push(...btcPrices); } catch (e) { console.error('Error in fetching prices of btc. error: ', e); } const cgkIds = PRESET_COINGECKO_IDS; if (cgkIds) { try { const cgkIdsPrices = await this.fetchCGKIdsPrices(cgkIds); prices.push(...cgkIdsPrices); } catch (e) { console.error('Error in fetching prices of preset ckg ids. error: ', e); } } const erc20Coins = coins.filter((i) => i.chainCode === 'eth' && i.tokenAddress); if (erc20Coins.length > 0) { try { const erc20Prices = await this.fetchERC20Prices(erc20Coins); prices.push(...erc20Prices); } catch (e) { console.error('Error in fetching prices of ERC20. error: ', e); } } return prices.filter((i) => i.value.isFinite() && i.value.gt(0)); } } exports.Coingecko = Coingecko; Coingecko.FIATS = new Set([ 'cny', 'usd', 'jpy', 'krw', 'gbp', 'eur', 'hkd', 'myr', 'aud', 'inr', ]); //# sourceMappingURL=coingecko.js.map