UNPKG

@orca-so/whirlpool-sdk

Version:

Whirlpool SDK for the Orca protocol.

134 lines (133 loc) 5.17 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrcaZooplankton = void 0; const axios_1 = __importDefault(require("axios")); const CacheKey_Tokens = "tokens"; const CacheKey_Pools = "pools"; /** * Offchain data accessor. */ class OrcaZooplankton { constructor(dataSourceURI) { this._cache = new InternalZPCache(); this._request = axios_1.default.create({ baseURL: dataSourceURI }); } getTokens() { var _a; return __awaiter(this, void 0, void 0, function* () { try { const cachedResponse = this._cache.get(CacheKey_Tokens); if (cachedResponse) { return cachedResponse; } const response = yield this._request.request({ url: "/token/list", method: "get", params: { whitelisted: true }, }); const tokens = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.tokens; if (!tokens) { return null; } const result = {}; tokens.forEach((token) => { result[token.mint] = { mint: token.mint, name: token.name, symbol: token.symbol, logoURI: token.logoURI, whitelisted: token.whitelisted, coingeckoId: token.coingeckoId, }; }); this._cache.set(CacheKey_Tokens, result); return result; } catch (e) { console.error(e); return null; } }); } getPools() { var _a; return __awaiter(this, void 0, void 0, function* () { try { const cachedResponse = this._cache.get(CacheKey_Pools); if (cachedResponse) { return cachedResponse; } const response = yield this._request.request({ url: "/whirlpool/list", method: "get", }); const whirlpools = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.whirlpools; if (!whirlpools) { return null; } const result = {}; whirlpools.forEach((pool) => { result[pool.address] = { address: pool.address, whitelisted: pool.whitelisted, tokenMintA: pool.tokenA.mint, tokenMintB: pool.tokenB.mint, stable: pool.stable, price: pool.price, lpsFeeRate: pool.lpFeeRate, protocolFeeRate: pool.protocolFeeRate, priceHistory: pool.priceRange, tvl: pool.tvl, volume: pool.volume, feeApr: pool.feeApr, reward0Apr: pool.reward0Apr, reward1Apr: pool.reward1Apr, reward2Apr: pool.reward2Apr, totalApr: pool.totalApr, }; }); this._cache.set(CacheKey_Pools, result); return result; } catch (e) { console.error(e); return null; } }); } } exports.OrcaZooplankton = OrcaZooplankton; /*** Internal Cache ***/ class InternalZPCache { constructor() { this._cache = {}; } /** * Retrieves cached response if it exists and has not expired */ get(key) { const content = this._cache[key]; if (!content || Date.now() >= content.eol) { return null; } return content.value; } /** * Stores the response in cache with time-to-live default to 15 seconds. */ set(key, value, ttl = 15000) { this._cache[key] = { value, eol: Date.now() + ttl }; } }