aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
155 lines (154 loc) • 7.58 kB
JavaScript
"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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NftAmmMarket = void 0;
const caller_1 = require("../../general/utils/caller");
const pools_1 = require("../pools");
class NftAmmMarket extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
constructor(market, config, Provider) {
super(config, `nft-amm/markets/${market.objectId}`);
this.market = market;
this.Provider = Provider;
// =========================================================================
// Calculations
// =========================================================================
this.getNftSpotPriceInAssetCoin = (inputs) => {
const assetToFractionalizedSpotPrice = this.getAssetCoinToFractionalizeCoinSpotPrice(inputs);
return BigInt(assetToFractionalizedSpotPrice *
Number(this.market.fractionalizedCoinAmount));
};
this.getFractionalizedCoinToAssetCoinSpotPrice = (inputs) => {
return this.pool.getSpotPrice({
coinInType: this.market.fractionalizedCoinType,
coinOutType: this.market.assetCoinType,
withFees: inputs === null || inputs === void 0 ? void 0 : inputs.withFees,
});
};
this.getAssetCoinToFractionalizeCoinSpotPrice = (inputs) => {
return this.pool.getSpotPrice({
coinInType: this.market.assetCoinType,
coinOutType: this.market.fractionalizedCoinType,
withFees: inputs === null || inputs === void 0 ? void 0 : inputs.withFees,
});
};
this.getBuyAssetCoinAmountIn = (inputs) => {
return this.pool.getTradeAmountIn({
coinOutAmount: BigInt(inputs.nftsCount) * this.market.fractionalizedCoinAmount,
coinInType: this.market.assetCoinType,
coinOutType: this.market.fractionalizedCoinType,
referral: inputs.referral,
});
};
this.getSellAssetCoinAmountOut = (inputs) => {
return this.pool.getTradeAmountOut({
coinInAmount: BigInt(inputs.nftsCount) * this.market.fractionalizedCoinAmount,
coinInType: this.market.fractionalizedCoinType,
coinOutType: this.market.assetCoinType,
referral: inputs.referral,
});
};
this.getDepositLpCoinAmountOut = (inputs) => {
return this.pool.getDepositLpAmountOut({
amountsIn: {
[this.market.assetCoinType]: inputs.assetCoinAmountIn,
},
referral: inputs.referral,
});
};
this.getWithdrawFractionalizedCoinAmountOut = (inputs) => {
const lpRatio = this.pool.getMultiCoinWithdrawLpRatio({
lpCoinAmountIn: inputs.lpCoinAmount,
});
const amountsOut = this.pool.getWithdrawAmountsOut({
lpRatio,
amountsOutDirection: {
[this.market.fractionalizedCoinType]: this.market.fractionalizedCoinAmount,
},
referral: inputs.referral,
});
const fractionalizedCoinAmountOut = amountsOut[0];
return fractionalizedCoinAmountOut;
};
this.getWithdrawNftsCountOut = (inputs) => {
const fractionalizedCoinAmountOut = this.getWithdrawFractionalizedCoinAmountOut(inputs);
const minNftsCountOut = fractionalizedCoinAmountOut / this.market.fractionalizedCoinAmount;
return minNftsCountOut;
};
// =========================================================================
// Private Helpers
// =========================================================================
this.useProvider = () => {
var _a;
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.NftAmm();
if (!provider)
throw new Error("missing AftermathApi Provider");
return provider;
};
this.market = market;
this.pool = new pools_1.Pool(market.pool, config);
}
// =========================================================================
// Objects
// =========================================================================
getNfts(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { cursor, limit } = inputs;
return this.useProvider().fetchNftsInMarketTable({
marketTableObjectId: this.market.objectId,
limit: limit !== null && limit !== void 0 ? limit : 25,
cursor,
});
});
}
// =========================================================================
// Transactions
// =========================================================================
getBuyTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildBuyTx(Object.assign(Object.assign({}, inputs), { market: this }));
});
}
getSellTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildSellTx(Object.assign(Object.assign({}, inputs), { market: this }));
});
}
getDepositTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { nftObjectIds: nfts } = inputs, otherInputs = __rest(inputs, ["nftObjectIds"]);
return this.useProvider().fetchBuildDepositTx(Object.assign(Object.assign({}, otherInputs), { nfts, market: this }));
});
}
getWithdrawTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildWithdrawTx(Object.assign(Object.assign({}, inputs), { market: this }));
});
}
}
exports.NftAmmMarket = NftAmmMarket;
// =========================================================================
// Private Constants
// =========================================================================
NftAmmMarket.constants = {};