Nomics: Get the Best Rate by Exchange and Currency

node v10.24.1
version: 1.0.2
endpointsharetweet
const axios = require('axios') const _ = require('lodash') async function endpoint(request, response) { let currency = 'XLM' if (request && response && request.url) currency = _.last(request.url.split('/')).toUpperCase() if (!currency) return response.end('Please append currency symbol to url') try { const exchanges = await axios.get('https://api.nomics.com/v1/exchange-markets/prices', { params: { key: "2018-08-demo-dont-deploy-6eb4ce24acd11f08", currency } }).then(({data}) => data) const prices = await axios.get('https://api.nomics.com/v1/prices', { params: { key: "2018-08-demo-dont-deploy-6eb4ce24acd11f08" } }).then(({data}) => data) const rates = await axios.get('https://api.nomics.com/v1/exchange-rates', { params: { key: "2018-08-demo-dont-deploy-6eb4ce24acd11f08" } }).then(({data}) => data) const usd_price_map = _ .chain(exchanges) .map((exchange) => { let price = _.find(prices, {currency: exchange.quote}) if (!price) price = _.find(rates, {currency: exchange.quote}) if (price) return { exchange: exchange.exchange, currency: exchange.quote, price_usd: exchange.price_quote * (price.price || price.rate) } else return exchange.quote }) .orderBy('price_usd', 'desc') // .groupBy('exchange') .value() if (request && response) response.end(JSON.stringify(usd_price_map)) else console.log(usd_price_map) } catch(err) { throw err } } endpoint() exports.endpoint = endpoint
Loading…

no comments

    sign in to comment