UNPKG

client-aftermath-ts-sdk

Version:
163 lines (162 loc) 7.5 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerpetualsMarket = void 0; const __1 = require("../.."); const caller_1 = require("../../general/utils/caller"); const iFixedUtils_1 = require("../../general/utils/iFixedUtils"); const perpetuals_1 = require("./perpetuals"); const utils_1 = require("./utils"); class PerpetualsMarket extends caller_1.Caller { // ========================================================================= // Constructor // ========================================================================= constructor(marketData, config) { super(config, `perpetuals/${marketData.collateralCoinType}/markets/${marketData.objectId}`); this.marketData = marketData; this.getMaxOrderSizeUsd = (inputs) => __awaiter(this, void 0, void 0, function* () { const { side, price, account, indexPrice, leverage } = inputs; const maxSize = yield this.fetchApi("max-order-size", { accountId: account.accountCap.accountId, collateral: account.collateralBalance(), side, price, leverage, }); return Number(maxSize) * this.lotSize() * indexPrice; }); // ========================================================================= // Calculations // ========================================================================= this.timeUntilNextFundingMs = () => { return this.nextFundingTimeMs() - Date.now(); }; this.nextFundingTimeMs = () => { const fundingFrequencyMs = Number(this.marketParams.fundingFrequencyMs); const lastFundingIntervalNumber = Math.floor(this.marketState.fundingLastUpdateMs / fundingFrequencyMs); return (lastFundingIntervalNumber + 1) * fundingFrequencyMs; }; // The funding rate as the difference between book and index TWAPs relative to the index price, // scaled by the funding period adjustment: // (bookTwap - indexTwap) / indexPrice * (fundingFrequency / fundingPeriod) // // To get the rate as a percentage, multiply the output by 100. this.estimatedFundingRate = (inputs) => { const { indexPrice } = inputs; const premiumTwap = iFixedUtils_1.IFixedUtils.numberFromIFixed(this.marketState.premiumTwap); const relativePremium = premiumTwap / indexPrice; const periodAdjustment = Number(this.marketParams.fundingFrequencyMs) / Number(this.marketParams.fundingPeriodMs); return relativePremium * periodAdjustment; }; this.priceToOrderPrice = (inputs) => { const { price } = inputs; const lotSize = this.marketParams.lotSize; const tickSize = this.marketParams.tickSize; return perpetuals_1.Perpetuals.priceToOrderPrice({ price, lotSize, tickSize, }); }; this.orderPriceToPrice = (inputs) => { const { orderPrice } = inputs; const lotSize = this.marketParams.lotSize; const tickSize = this.marketParams.tickSize; return perpetuals_1.Perpetuals.orderPriceToPrice({ orderPrice, lotSize, tickSize, }); }; this.calcCollateralUsedForOrder = (inputs) => { const { leverage, orderData, indexPrice, collateralPrice } = inputs; const imr = 1 / leverage; // const imr = this.initialMarginRatio(); const collateralUsd = Number(orderData.initialSize - orderData.filledSize) * this.lotSize() * indexPrice * imr; const collateral = collateralUsd / collateralPrice; return { collateralUsd, collateral, }; }; this.roundToValidPrice = (inputs) => { return Math.round(inputs.price / this.tickSize()) * this.tickSize(); }; this.roundToValidSize = (inputs) => { const lots = inputs.size / this.lotSize(); return ((inputs.floor ? Math.floor(lots) : inputs.ceil ? Math.ceil(lots) : Math.round(lots)) * this.lotSize()); }; this.marketId = marketData.objectId; this.collateralCoinType = marketData.collateralCoinType; this.marketParams = marketData.marketParams; this.marketState = marketData.marketState; } // ========================================================================= // Inspections // ========================================================================= // NOTE: should this be entirely removed since data already in orderbook function ? getOrderbookPrice() { return this.fetchApi("orderbook-price"); } get24hrVolume() { return this.fetchApi("24hr-volume"); } getPrice24hrsAgo() { return this.fetchApi("price-24hrs-ago"); } getOrderbook() { return this.fetchApi("orderbook"); } // ========================================================================= // Trade History // ========================================================================= getTradeHistory(inputs) { return __awaiter(this, void 0, void 0, function* () { return this.fetchApi(`trade-history`, inputs); }); } // ========================================================================= // Value Conversions // ========================================================================= lotSize() { return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.lotSize); } tickSize() { return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.tickSize); } maxLeverage() { return (1 / __1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioInitial)); } initialMarginRatio() { return __1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioInitial); } maintenanceMarginRatio() { return __1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioMaintenance); } // ========================================================================= // Helpers // ========================================================================= orderPrice(inputs) { const { orderId } = inputs; const orderPrice = utils_1.PerpetualsOrderUtils.price(orderId); return this.orderPriceToPrice({ orderPrice }); } } exports.PerpetualsMarket = PerpetualsMarket;