UNPKG

@cryptoalgebra/alm-sdk

Version:

Algebra ALM SDK

159 lines 9.09 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 (g && (g = 0, op[0] && (_ = 0)), _) 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.getLpPriceChange = exports.getLpApr = exports.getLpPriceAt = void 0; var ethers_1 = require("ethers"); var types_1 = require("../types"); // eslint-disable-next-line import/no-cycle var vault_1 = require("./vault"); var _totalBalances_1 = require("./_totalBalances"); var priceFromPool_1 = require("./priceFromPool"); var timestamps_1 = require("../utils/timestamps"); var getPrice_1 = __importDefault(require("../utils/getPrice")); var formatBigInt_1 = __importDefault(require("../utils/formatBigInt")); var getGraphUrls_1 = __importDefault(require("../utils/getGraphUrls")); // eslint-disable-next-line import/no-cycle var _vaultEvents_1 = require("./_vaultEvents"); var cache_1 = __importDefault(require("../utils/cache")); function getLpPriceAt(vaultEvents, daysAgo, isVaultInverted, token0decimals, token1decimals) { var e = (0, _vaultEvents_1.getVaultStateAt)(vaultEvents, daysAgo); if (!e) { return null; } else { var depositTokenDecimals = isVaultInverted ? token1decimals : token0decimals; var scarceTokenDecimals = isVaultInverted ? token0decimals : token1decimals; var totalAmount0 = e.totalAmount0, totalAmount1 = e.totalAmount1, sqrtPrice = e.sqrtPrice; var formattedTotalAmount0 = (0, formatBigInt_1.default)(totalAmount0, token0decimals); var formattedTotalAmount1 = (0, formatBigInt_1.default)(totalAmount1, token1decimals); var price = (0, getPrice_1.default)(isVaultInverted, ethers_1.BigNumber.from(sqrtPrice), depositTokenDecimals, scarceTokenDecimals, 15); var tvl = !isVaultInverted ? Number(formattedTotalAmount0) + Number(formattedTotalAmount1) * price : Number(formattedTotalAmount1) + Number(formattedTotalAmount0) * price; var totalSupply = Number((0, formatBigInt_1.default)(e.totalSupply, types_1.algebraVaultDecimals)); var days = (0, timestamps_1.millisecondsToDays)(Date.now() - Number(e.createdAtTimestamp) * 1000); if (totalSupply === 0) { return null; } return { timeInterval: days, priceChange: tvl / totalSupply }; } } exports.getLpPriceAt = getLpPriceAt; function getLpApr(vaultAddress, jsonProvider, dex, vault, token0Decimals, token1Decimals) { return __awaiter(this, void 0, void 0, function () { var key, cachedData, ttl, isInv, _a, tvl, totalAmounts, vaultAprs, result; return __generator(this, function (_b) { switch (_b.label) { case 0: key = "lpApr-".concat(dex, "-").concat(vaultAddress); cachedData = cache_1.default.get(key); if (cachedData) { return [2 /*return*/, cachedData]; } ttl = 30 * 60 * 1000; isInv = vault.allowTokenB; return [4 /*yield*/, (0, priceFromPool_1.getVaultTvl)(vault, jsonProvider, isInv, token0Decimals, token1Decimals)]; case 1: _a = _b.sent(), tvl = _a.tvl, totalAmounts = _a.totalAmounts; vaultAprs = []; result = { aprs: vaultAprs, vault: vault, tvl: tvl, totalAmounts: totalAmounts, }; cache_1.default.set(key, result, ttl); return [2 /*return*/, result]; } }); }); } exports.getLpApr = getLpApr; function getLpPriceChange(vaultAddress, jsonProvider, dex, timeIntervals) { return __awaiter(this, void 0, void 0, function () { var key, cachedData, ttl, _a, chainId, vault, decimals0, decimals1, isInv, currLpPrice, arrDays, maxDays, result, vaultEvents; return __generator(this, function (_b) { switch (_b.label) { case 0: key = "lpPriceChange-".concat(dex, "-").concat(vaultAddress); cachedData = cache_1.default.get(key); if (cachedData) { return [2 /*return*/, cachedData]; } ttl = 30 * 60 * 1000; return [4 /*yield*/, (0, vault_1.validateVaultData)(vaultAddress, jsonProvider, dex)]; case 1: _a = _b.sent(), chainId = _a.chainId, vault = _a.vault; (0, getGraphUrls_1.default)(chainId, dex, true); return [4 /*yield*/, (0, _totalBalances_1.getTokenDecimals)(vault.tokenA, jsonProvider, chainId)]; case 2: decimals0 = _b.sent(); return [4 /*yield*/, (0, _totalBalances_1.getTokenDecimals)(vault.tokenB, jsonProvider, chainId)]; case 3: decimals1 = _b.sent(); isInv = vault.allowTokenB; return [4 /*yield*/, (0, priceFromPool_1.getCurrLpPrice)(vault, jsonProvider, dex, chainId, isInv, decimals0, decimals1)]; case 4: currLpPrice = _b.sent(); arrDays = timeIntervals && timeIntervals.length > 0 ? timeIntervals : [1, 7, 30]; maxDays = Math.max.apply(Math, arrDays) + 30; result = []; return [4 /*yield*/, (0, _vaultEvents_1._getAllVaultEvents)(vaultAddress, chainId, dex, maxDays)]; case 5: vaultEvents = _b.sent(); arrDays.forEach(function (d) { var objLpPrice = getLpPriceAt(vaultEvents, d, isInv, decimals0, decimals1); var prevLpPrice = objLpPrice === null || objLpPrice === void 0 ? void 0 : objLpPrice.priceChange; if (!prevLpPrice || prevLpPrice === 0) { result.push({ timeInterval: d, priceChange: null }); } else { result.push({ timeInterval: d, priceChange: ((currLpPrice - prevLpPrice) / prevLpPrice) * 100 }); } }); cache_1.default.set(key, result, ttl); return [2 /*return*/, result]; } }); }); } exports.getLpPriceChange = getLpPriceChange; //# sourceMappingURL=calculateApr.js.map