UNPKG

@ducatus/ducatus-wallet-service-rev

Version:
229 lines 10.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var async = __importStar(require("async")); var lodash_1 = __importDefault(require("lodash")); var request = __importStar(require("request")); var storage_1 = require("./storage"); var $ = require('preconditions').singleton(); var Common = require('./common'); var Defaults = Common.Defaults; var log = require('npmlog'); log.debug = log.verbose; var fiatCodes = { USD: 1, INR: 1, GBP: 1, EUR: 1, CAD: 1, COP: 1, NGN: 1, BRL: 1, ARS: 1, AUD: 1 }; var customCoins = ['duc', 'ducx']; var FiatRateService = (function () { function FiatRateService() { } FiatRateService.prototype.init = function (opts, cb) { var _this = this; opts = opts || {}; this.request = opts.request || request; this.defaultProvider = opts.defaultProvider || Defaults.FIAT_RATE_PROVIDER; async.parallel([ function (done) { if (opts.storage) { _this.storage = opts.storage; done(); } else { _this.storage = new storage_1.Storage(); _this.storage.connect(opts.storageOpts, done); } } ], function (err) { if (err) { log.error(err); } return cb(err); }); }; FiatRateService.prototype.startCron = function (opts, cb) { var _this = this; opts = opts || {}; this.providers = lodash_1.default.values(require('./fiatrateproviders')); var interval = opts.fetchInterval || Defaults.FIAT_RATE_FETCH_INTERVAL; if (interval) { this._fetch(); setInterval(function () { _this._fetch(); }, interval * 60 * 1000); } return cb(); }; FiatRateService.prototype._fetch = function (cb) { var _this = this; cb = cb || function () { }; var coins = ['btc', 'bch', 'eth', 'xrp', 'duc', 'ducx']; async.each(coins, function (coin, next2) { var provider = customCoins.includes(coin) ? _this.providers[1] : _this.providers[0]; if (customCoins.includes(coin)) { console.log("Get " + coin + " from " + provider.url); } _this._retrieve(provider, coin, function (err, res) { if (err) { log.warn('Error retrieving data for ' + provider.name + coin, err); return next2(); } _this.storage.storeFiatRate(coin, res, function (err) { if (err) { log.warn('Error storing data for ' + provider.name, err); } return next2(); }); }); }, cb); }; FiatRateService.prototype._retrieve = function (provider, coin, cb) { log.debug("Fetching data for " + provider.name + " / " + coin + " "); var url = customCoins.includes(coin) ? provider.url : provider.url + coin.toUpperCase(); this.request.get({ url: url, json: true }, function (err, res, body) { if (err || !body) { return cb(err); } var customRates = []; if (customCoins.includes(coin)) { customRates.push({ code: 'USD', name: 'US Dollar', rate: body[coin.toUpperCase()].USD }); } var bodyRates = customCoins.includes(coin) ? customRates : body; log.debug("Data for " + provider.name + " / " + coin + " fetched successfully"); if (!provider.parseFn) { return cb(new Error('No parse function for provider ' + provider.name)); } try { var rates = lodash_1.default.filter(provider.parseFn(bodyRates), function (x) { return fiatCodes[x.code]; }); return cb(null, rates); } catch (e) { return cb(e); } }); }; FiatRateService.prototype.getRate = function (opts, cb) { var _this = this; $.shouldBeFunction(cb); opts = opts || {}; var now = Date.now(); var coin = opts.coin || 'btc'; var ts = lodash_1.default.isNumber(opts.ts) || lodash_1.default.isArray(opts.ts) ? opts.ts : now; async.map([].concat(ts), function (ts, cb) { _this.storage.fetchFiatRate(coin, opts.code, ts, function (err, rate) { if (err) return cb(err); if (rate && ts - rate.ts > Defaults.FIAT_RATE_MAX_LOOK_BACK_TIME * 60 * 1000) rate = null; return cb(null, { ts: +ts, rate: rate ? rate.value : undefined, fetchedOn: rate ? rate.ts : undefined }); }); }, function (err, res) { if (err) return cb(err); if (!lodash_1.default.isArray(ts)) res = res[0]; return cb(null, res); }); }; FiatRateService.prototype.getHistoricalRates = function (opts, cb) { return __awaiter(this, void 0, void 0, function () { var historicalRates, now, ts, coins, customCoins; var _this = this; return __generator(this, function (_a) { $.shouldBeFunction(cb); opts = opts || {}; historicalRates = {}; now = Date.now() - Defaults.FIAT_RATE_FETCH_INTERVAL * 60 * 1000; ts = lodash_1.default.isNumber(opts.ts) ? opts.ts : now; coins = ['btc', 'bch', 'eth', 'xrp', 'duc', 'ducx']; customCoins = ['duc', 'ducx']; async.map(coins, function (coin, cb) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { this.storage.fetchHistoricalRates(coin, opts.code, ts, function (err, rates) { if (err) return cb(err); if (!rates) return cb(); for (var _i = 0, rates_1 = rates; _i < rates_1.length; _i++) { var rate = rates_1[_i]; rate.rate = rate.value; rate.fetchedOn = rate.ts; } historicalRates[coin] = rates; return cb(null, historicalRates); }); return [2]; }); }); }, function (err, res) { if (err) return cb(err); return cb(null, res[0]); }); return [2]; }); }); }; return FiatRateService; }()); exports.FiatRateService = FiatRateService; //# sourceMappingURL=fiatrateservice.js.map