aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
160 lines (159 loc) • 7.54 kB
JavaScript
;
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 _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Perpetuals = void 0;
const caller_1 = require("../../general/utils/caller");
const types_1 = require("../../types");
const perpetualsMarket_1 = require("./perpetualsMarket");
const perpetualsAccount_1 = require("./perpetualsAccount");
const iFixedUtils_1 = require("../../general/utils/iFixedUtils");
const fixedUtils_1 = require("../../general/utils/fixedUtils");
const utils_1 = require("../../general/utils");
const utils_2 = require("./utils");
class Perpetuals extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
constructor(config) {
super(config, "perpetuals");
}
// =========================================================================
// Class Objects
// =========================================================================
getAllMarkets(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { collateralCoinType } = inputs;
const marketDatas = yield this.fetchApi(`${collateralCoinType}/markets`);
return marketDatas.map((marketData) => new perpetualsMarket_1.PerpetualsMarket(marketData, this.config));
});
}
getMarket(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const marketData = yield this.fetchApi(`${inputs.collateralCoinType}/markets/${inputs.marketId}`);
return new perpetualsMarket_1.PerpetualsMarket(marketData, this.config);
});
}
getMarkets(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { collateralCoinType } = inputs;
return Promise.all(inputs.marketIds.map((marketId) => this.getMarket({
marketId,
collateralCoinType,
})));
});
}
getAccount(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { accountCap } = inputs;
const account = yield this.fetchApi(`${accountCap.collateralCoinType}/accounts/${accountCap.accountId}`);
return new perpetualsAccount_1.PerpetualsAccount(account, accountCap, this.config);
});
}
getUserAccountCaps(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { collateralCoinType, walletAddress } = inputs;
return this.fetchApi(`${collateralCoinType}/accounts`, {
walletAddress,
});
});
}
// =========================================================================
// Data
// =========================================================================
getMarketHistoricalData(inputs) {
const { collateralCoinType, marketId, fromTimestamp, toTimestamp, intervalMs, } = inputs;
return this.fetchApi(`${collateralCoinType}/markets/${marketId}/historical-data/${fromTimestamp}/${toTimestamp}/${intervalMs}`);
}
// =========================================================================
// Transactions
// =========================================================================
getCreateAccountTx(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransaction("transactions/create-account", inputs);
});
}
// =========================================================================
// Public Static Functions
// =========================================================================
// =========================================================================
// Helpers
// =========================================================================
static positionSide(inputs) {
const baseAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.baseAssetAmount);
const isLong = Math.sign(baseAmount);
const side = isLong >= 0 ? types_1.PerpetualsOrderSide.Bid : types_1.PerpetualsOrderSide.Ask;
return side;
}
static orderPrice(inputs) {
const { orderEvent } = inputs;
return (iFixedUtils_1.IFixedUtils.numberFromIFixed(orderEvent.quoteAssetDelta) /
iFixedUtils_1.IFixedUtils.numberFromIFixed(orderEvent.baseAssetDelta));
}
static lotOrTickSizeToNumber(lotOrTickSize) {
return Number(lotOrTickSize) / fixedUtils_1.FixedUtils.fixedOneN9;
}
static lotOrTickSizeToBigInt(lotOrTickSize) {
return BigInt(Math.round(lotOrTickSize * fixedUtils_1.FixedUtils.fixedOneN9));
}
// =========================================================================
// Calculations
// =========================================================================
static calcEntryPrice(inputs) {
const { baseAssetAmount, quoteAssetNotionalAmount } = inputs;
const denominator = utils_1.Casting.IFixed.numberFromIFixed(baseAssetAmount);
if (!denominator)
return 0;
return Math.abs(utils_1.Casting.IFixed.numberFromIFixed(quoteAssetNotionalAmount) /
denominator);
}
}
exports.Perpetuals = Perpetuals;
_a = Perpetuals;
// =========================================================================
// Constants
// =========================================================================
Perpetuals.OrderUtils = utils_2.PerpetualsOrderUtils;
Perpetuals.priceToOrderPrice = (inputs) => {
const { price, lotSize, tickSize } = inputs;
const priceFixed = fixedUtils_1.FixedUtils.directUncast(price);
// convert f18 to b9 (assuming the former is positive)
const price9 = priceFixed / fixedUtils_1.FixedUtils.fixedOneB9;
const denominator = fixedUtils_1.FixedUtils.fixedOneB9 /
(typeof lotSize === "number"
? _a.lotOrTickSizeToBigInt(lotSize)
: lotSize);
if (denominator <= BigInt(0))
return BigInt(0);
return (price9 /
(typeof tickSize === "number"
? _a.lotOrTickSizeToBigInt(tickSize)
: tickSize) /
denominator);
};
Perpetuals.orderPriceToPrice = (inputs) => {
const { orderPrice, lotSize, tickSize } = inputs;
const temp = fixedUtils_1.FixedUtils.fixedOneB9 /
(typeof lotSize === "number"
? _a.lotOrTickSizeToBigInt(lotSize)
: lotSize);
return fixedUtils_1.FixedUtils.directCast(orderPrice *
(typeof tickSize === "number"
? _a.lotOrTickSizeToBigInt(tickSize)
: tickSize) *
temp *
fixedUtils_1.FixedUtils.fixedOneB9);
};
Perpetuals.orderIdToSide = (orderId) => {
return _a.OrderUtils.isAsk(orderId)
? types_1.PerpetualsOrderSide.Ask
: types_1.PerpetualsOrderSide.Bid;
};