UNPKG

redstone-api

Version:

Javascript library for fetching trusted token pricing data from Redstone data ecosystem

275 lines (274 loc) 12.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedstoneQueryExecutable = exports.RedstoneQueryForSeveralSymbols = exports.RedstoneQueryForSingleSymbol = exports.RedstoneQueryForSingleOrSeveralSymbols = exports.RedstoneQuery = void 0; var api_1 = __importDefault(require("./api")); var RedstoneQuery = /** @class */ (function () { function RedstoneQuery(params) { if (params === void 0) { params = {}; } this.params = __assign({ symbols: [] }, params); } /** * Sets a token symbol to fetch * @param symbol - Token symbol string * @returns query object */ RedstoneQuery.prototype.symbol = function (symbol) { return new RedstoneQueryForSingleSymbol({ symbols: [symbol], }); }; /** * Sets token symbols to fetch * @param symbols - Array of strings (token symbols) * @returns query object */ RedstoneQuery.prototype.symbols = function (symbols) { return new RedstoneQueryForSeveralSymbols({ symbols: symbols }); }; /** * Configures query to fetch prices for all supported tokens. * It doesn't support any params * @returns query object */ RedstoneQuery.prototype.allSymbols = function () { // return this.getQueryWithUpdatedSymbols<{ [symbol: string]: PriceData }>([]); return new RedstoneQueryForSeveralSymbols({ symbols: [], }); }; return RedstoneQuery; }()); exports.RedstoneQuery = RedstoneQuery; var RedstoneQueryForSingleOrSeveralSymbols = /** @class */ (function () { function RedstoneQueryForSingleOrSeveralSymbols(params) { this.params = params; } // TODO: Maybe improve the type later (replace any with more detailed one) RedstoneQueryForSingleOrSeveralSymbols.prototype.getExecutableQuery = function (update) { return new RedstoneQueryExecutable(__assign(__assign({}, this.params), update)); }; /** * Configures query to fetch the latest price/prices * It doesn't support any params * @returns query object */ RedstoneQueryForSingleOrSeveralSymbols.prototype.latest = function () { return this.getExecutableQuery({}); }; /** * Configures query to fetch the price for X hours ago. * @param hoursCount - Number of hours ago * @returns query object */ RedstoneQueryForSingleOrSeveralSymbols.prototype.hoursAgo = function (hoursCount) { return this.getExecutableQuery({ date: Date.now() - hoursCount * 3600 * 1000, }); }; /** * Configures query to fetch the price for a specific date. * @param date - Date for the historical price (date | timestamp | string) * @returns query object */ RedstoneQueryForSingleOrSeveralSymbols.prototype.atDate = function (date) { return this.getExecutableQuery({ date: date }); }; return RedstoneQueryForSingleOrSeveralSymbols; }()); exports.RedstoneQueryForSingleOrSeveralSymbols = RedstoneQueryForSingleOrSeveralSymbols; var RedstoneQueryForSingleSymbol = /** @class */ (function (_super) { __extends(RedstoneQueryForSingleSymbol, _super); function RedstoneQueryForSingleSymbol(params) { return _super.call(this, params) || this; } /** * Configures query to fetch the price in a time range. It is important to use fromDate with toDate query methods * @param date - Start date/time for the time range * * @returns query object */ RedstoneQueryForSingleSymbol.prototype.fromDate = function (date) { return new RedstoneQueryForSingleSymbol(__assign(__assign({}, this.params), { startDate: date })); }; /** * Configures query to fetch the price in a time range. toDate method should go after the fromDate * @param date - End date/time for the time range * * @returns query object */ RedstoneQueryForSingleSymbol.prototype.toDate = function (date) { if (this.params.startDate === undefined) { throw new Error("Please specify fromDate before using toDate"); } return this.getExecutableQuery({ endDate: date }); }; /** * Configures query to fetch the price for the last few hours * @param hoursCount - Number of hours in the time range * * @returns query object */ RedstoneQueryForSingleSymbol.prototype.forLastHours = function (hoursCount) { var endDate = Date.now(); return this.getExecutableQuery({ endDate: endDate, startDate: endDate - hoursCount * 3600 * 1000, interval: 600 * 1000, }); }; /** * Configures query to fetch the price for the last few days * @param daysCount - Number of days in the time range * * @returns query object */ RedstoneQueryForSingleSymbol.prototype.forLastDays = function (daysCount) { var endDate = Date.now(); return this.getExecutableQuery({ endDate: endDate, startDate: endDate - daysCount * 24 * 3600 * 1000, interval: 3600 * 1000, }); }; return RedstoneQueryForSingleSymbol; }(RedstoneQueryForSingleOrSeveralSymbols)); exports.RedstoneQueryForSingleSymbol = RedstoneQueryForSingleSymbol; var RedstoneQueryForSeveralSymbols = /** @class */ (function (_super) { __extends(RedstoneQueryForSeveralSymbols, _super); function RedstoneQueryForSeveralSymbols(params) { return _super.call(this, params) || this; } return RedstoneQueryForSeveralSymbols; }(RedstoneQueryForSingleOrSeveralSymbols)); exports.RedstoneQueryForSeveralSymbols = RedstoneQueryForSeveralSymbols; var RedstoneQueryExecutable = /** @class */ (function () { function RedstoneQueryExecutable(params) { if (params === void 0) { params = {}; } this.params = __assign({ symbols: [] }, params); } /** * Executes the query * * @returns Promise resolving the query result (result type depends on the query) */ RedstoneQueryExecutable.prototype.exec = function (redstoneApiConfig) { return __awaiter(this, void 0, void 0, function () { var redstone, symbols, symbolOrSymbols, _a, startDate, endDate, date, interval, diff; return __generator(this, function (_b) { switch (_b.label) { case 0: redstone = new api_1.default(__assign(__assign({}, redstoneApiConfig), { defaultProvider: redstoneApiConfig === null || redstoneApiConfig === void 0 ? void 0 : redstoneApiConfig.provider })); symbols = this.params.symbols; if (!(symbols.length > 0)) return [3 /*break*/, 5]; symbolOrSymbols = symbols.length === 1 ? symbols[0] : symbols; _a = this.params, startDate = _a.startDate, endDate = _a.endDate, date = _a.date, interval = _a.interval; if (![startDate, endDate, date].every(function (el) { return el === undefined; })) return [3 /*break*/, 2]; return [4 /*yield*/, redstone.getPrice(symbolOrSymbols, this.params)]; case 1: // Fetch the latest price return [2 /*return*/, (_b.sent())]; case 2: // Fetch the historical price if (startDate !== undefined && endDate !== undefined && interval === undefined) { diff = getTimeDiff(startDate, endDate); if (diff >= 24 * 3600 * 1000) { this.params.interval = 3600 * 1000; } else { this.params.interval = 1; } } return [4 /*yield*/, redstone.getHistoricalPrice(symbolOrSymbols, this.params)]; case 3: return [2 /*return*/, (_b.sent())]; case 4: return [3 /*break*/, 7]; case 5: return [4 /*yield*/, redstone.getAllPrices(this.params)]; case 6: // Fetch prices for all tokens return [2 /*return*/, (_b.sent())]; case 7: return [2 /*return*/]; } }); }); }; return RedstoneQueryExecutable; }()); exports.RedstoneQueryExecutable = RedstoneQueryExecutable; function getTimeDiff(date1, date2) { var timestamp1 = new Date(date1).getTime(); var timestamp2 = new Date(date2).getTime(); return Math.abs(timestamp2 - timestamp1); } /** * Initializes and returns an empty query. * It doesn't support any params * @returns query object */ var query = function () { return new RedstoneQuery(); }; exports.default = query;