UNPKG

@axerunners/insight-api

Version:

An AXE blockchain REST and WebSocket API Service

48 lines (41 loc) 1.34 kB
'use strict'; var request = require('request'); function CurrencyController(options) { this.node = options.node; var refresh = options.currencyRefresh || CurrencyController.DEFAULT_CURRENCY_DELAY; this.currencyDelay = refresh * 60000; this.exchange_rates = { axe_usd: 0.00, btc_usd: 0.00, btc_axe: 0.00 }; this.timestamp = Date.now(); } CurrencyController.DEFAULT_CURRENCY_DELAY = 10; CurrencyController.prototype.index = function(req, res) { var self = this; var currentTime = Date.now(); if (self.exchange_rates.axe_usd === 0.00 || currentTime >= (self.timestamp + self.currencyDelay)) { self.timestamp = currentTime; request('https://api.coingecko.com/api/v3/simple/price?ids=axe&vs_currencies=usd&include_24hr_vol=false&include_last_updated_at=false', function(err, response, body) { if (err) { self.node.log.error(err); } if (!err && response.statusCode === 200) { var response = JSON.parse(body); self.exchange_rates = response.axe; self.exchange_rates.bitstamp = response.axe.usd; // backwards compatibility } res.jsonp({ status: 200, data: self.exchange_rates }); }); } else { res.jsonp({ status: 200, data: self.exchange_rates }); } }; module.exports = CurrencyController;