client-aftermath-ts-sdk
Version:
Client Aftermath TypeScript SDK
280 lines (279 loc) • 11.1 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 _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pools = void 0;
const pool_1 = require("./pool");
const coin_1 = require("../../packages/coin/coin");
const caller_1 = require("../../general/utils/caller");
const helpers_1 = require("../../general/utils/helpers");
const fixedUtils_1 = require("../../general/utils/fixedUtils");
/**
* @class Pools Provider
*
* @example
* ```
* // Create provider
* const pools = (new Aftermath("MAINNET")).Pools();
* // Call sdk
* const pool = await pools.getPool({ objectId: "0xBEEF" });
* ```
*/
class Pools extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates `Pools` provider to call api.
*
* @param network - The Sui network to interact with
* @returns New `Pools` instance
*/
constructor(config, Provider) {
super(config, "pools");
this.Provider = Provider;
// =========================================================================
// Inspections
// =========================================================================
/**
* Returns the pool object ID for the given LP coin type.
* @param inputs - The request body containing the LP coin type.
* @returns The pool object ID.
* @throws An error if the LP coin type is invalid.
*/
this.getPoolObjectIdForLpCoinType = (inputs) => {
return this.getPoolObjectIdsForLpCoinTypes({
lpCoinTypes: [inputs.lpCoinType],
});
};
this.getPoolObjectIdsForLpCoinTypes = (inputs) => __awaiter(this, void 0, void 0, function* () {
// TODO: handle this case
// if (
// inputs.lpCoinTypes.some(
// (lpCoinType) => !Pools.isPossibleLpCoinType({ lpCoinType })
// )
// )
// throw new Error("invalid lp coin type");
return this.fetchApi("pool-object-ids", inputs);
});
/**
* Checks if the given coin type is an LP coin type.
* @param inputs - An object containing the LP coin type to check.
* @returns A boolean indicating whether the given coin type is an LP coin type.
*/
this.isLpCoinType = (inputs) => __awaiter(this, void 0, void 0, function* () {
try {
yield this.getPoolObjectIdForLpCoinType(inputs);
return true;
}
catch (e) {
return false;
}
});
/**
* Retrieves the total volume in the last 24 hours.
* @returns A Promise that resolves to a number representing the total volume.
*/
this.getTotalVolume24hrs = () => __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("volume-24hrs");
});
// =========================================================================
// Private Helpers
// =========================================================================
this.useProvider = () => {
var _b;
const provider = (_b = this.Provider) === null || _b === void 0 ? void 0 : _b.Pools();
if (!provider)
throw new Error("missing AftermathApi Provider");
return provider;
};
}
// =========================================================================
// Class Objects
// =========================================================================
// =========================================================================
// Pool Class
// =========================================================================
/**
* Creates new `Pool` class from queried pool object
*
* @param objectId - Object id of pool to fetch
* @returns New `Pool` instance
*/
getPool(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const pool = yield this.fetchApi(inputs.objectId);
return new pool_1.Pool(pool, this.config, this.Provider);
});
}
/**
* Creates `Pool[]` of classes from queried pool objects
*
* @param objectIds - Object ids of pools to fetch
* @returns New `Pool[]` instances
*/
getPools(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const pools = yield this.fetchApi("", {
poolIds: inputs.objectIds,
});
return pools.map((pool) => new pool_1.Pool(pool, this.config, this.Provider));
});
}
/**
* Retrieves all pools from the API and returns an array of Pool objects.
* @returns {Promise<Pool[]>} A promise that resolves to an array of Pool objects.
*/
getAllPools() {
return __awaiter(this, void 0, void 0, function* () {
const pools = yield this.fetchApi("", {});
return pools.map((pool) => new pool_1.Pool(pool, this.config, this.Provider));
});
}
getOwnedLpCoins(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("owned-lp-coins", inputs);
});
}
// =========================================================================
// Transactions
// =========================================================================
/**
* Fetches the API transaction for publishing an LP coin.
* @param inputs - The inputs for the transaction.
* @returns A promise that resolves with the API transaction.
*/
getPublishLpCoinTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().buildPublishLpCoinTx(inputs);
});
}
/**
* Fetches the transaction for creating a new pool.
* @param inputs The inputs required for creating a new pool.
* @returns A Promise that resolves to the transaction data.
*/
getCreatePoolTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransaction("transactions/create-pool", inputs);
});
}
getTVL(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("tvl", inputs !== null && inputs !== void 0 ? inputs : {});
});
}
/**
* Fetches statistics for pools.
* @async
* @returns {Promise<PoolStats[]>} The statistics for pools.
*/
getPoolsStats(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("stats", inputs);
});
}
getOwnedDaoFeePoolOwnerCaps(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchOwnedDaoFeePoolOwnerCaps(inputs);
});
}
// =========================================================================
// Events
// =========================================================================
getInteractionEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiIndexerEvents("interaction-events-by-user", inputs);
});
}
}
exports.Pools = Pools;
_a = Pools;
// =========================================================================
// Constants
// =========================================================================
/**
* An object containing various constants used in the pools module.
*/
Pools.constants = {
// TODO: remove this and use fixed class
feePercentages: {
totalProtocol: 0.00005,
// following fees are taked as portions of total protocol fees above
treasury: 0.5,
insuranceFund: 0.3,
devWallet: 0.2, // 20%
},
referralPercentages: {
// these percantages are relative to treasury allocation
discount: 0.05,
rebate: 0.05, // 5%
},
bounds: {
maxCoinsInPool: 8,
maxTradePercentageOfPoolBalance: 0.3,
maxWithdrawPercentageOfPoolBalance: 0.3,
minSwapFee: 0.0001,
maxSwapFee: 0.1,
minWeight: 0.01,
maxWeight: 0.99,
minDaoFee: 0,
maxDaoFee: 1, // 100%
},
defaults: {
lpCoinDecimals: 9,
},
};
// =========================================================================
// Fees
// =========================================================================
Pools.getAmountWithProtocolFees = (inputs) => {
const referralDiscount = inputs.withReferral
? _a.constants.feePercentages.totalProtocol *
_a.constants.feePercentages.treasury *
_a.constants.referralPercentages.discount
: 0;
return BigInt(Math.floor(Number(inputs.amount) *
(1 -
(_a.constants.feePercentages.totalProtocol -
referralDiscount))));
};
Pools.getAmountWithoutProtocolFees = (inputs) => {
const referralDiscount = inputs.withReferral
? _a.constants.feePercentages.totalProtocol *
_a.constants.feePercentages.treasury *
_a.constants.referralPercentages.discount
: 0;
return BigInt(Math.floor(Number(inputs.amount) *
(1 /
(1 -
(_a.constants.feePercentages.totalProtocol -
referralDiscount)))));
};
Pools.normalizeInvertSlippage = (slippage) => fixedUtils_1.FixedUtils.directUncast(1 - slippage);
// =========================================================================
// Display
// =========================================================================
Pools.displayLpCoinType = (lpCoinType) => coin_1.Coin.getCoinTypeSymbol(lpCoinType)
.toLowerCase()
.replace("af_lp_", "")
.split("_")
.map((word) => helpers_1.Helpers.capitalizeOnlyFirstLetter(word))
.join(" ") + " LP";
// =========================================================================
// Helpers
// =========================================================================
Pools.isPossibleLpCoinType = (inputs) => {
const { lpCoinType } = inputs;
return (lpCoinType.split("::").length === 3 &&
lpCoinType.split("::")[1].includes("af_lp") &&
lpCoinType.split("::")[2].includes("AF_LP"));
};