aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
459 lines • 17.2 kB
TypeScript
import { ApiPoolDepositBody, ApiPoolTradeBody, ApiPoolWithdrawBody, Balance, CoinType, CoinsToBalance, PoolDataPoint, PoolGraphDataTimeframeKey, PoolObject, PoolStats, PoolDepositEvent, PoolWithdrawEvent, ApiPoolAllCoinWithdrawBody, ApiIndexerEventsBody, IndexerEventsWithCursor, Percentage, SuiAddress, ObjectId, PoolCoin, CallerConfig } from "../../types";
import { Caller } from "../../general/utils/caller";
import { Transaction } from "@mysten/sui/transactions";
import { AftermathApi } from "../../general/providers";
/**
* The `Pool` class encapsulates all the functionality needed to interact
* with a specific AMM pool on the Aftermath platform. It allows you to
* calculate trade amounts, deposit/withdraw amounts, fetch transactions,
* and retrieve on-chain statistics and event data.
*
* @example
* ```typescript
* const afSdk = new Aftermath("MAINNET");
* await afSdk.init(); // initialize provider
*
* const pools = afSdk.Pools();
* const pool = await pools.getPool({ objectId: "0x..." });
*
* const stats = await pool.getStats();
* const tradeTx = await pool.getTradeTransaction({
* walletAddress: "0x...",
* coinInType: "0x2::sui::SUI",
* coinInAmount: BigInt(1e9),
* coinOutType: "0x<yourCoin>",
* slippage: 0.01,
* });
* ```
*/
export declare class Pool extends Caller {
readonly pool: PoolObject;
private readonly Provider?;
/**
* Internal margin of error used in trade calculations to prevent
* exceeding maximum allowed percentages of pool balances.
*/
private static readonly constants;
/**
* An optional cached object containing statistical data about the pool
* (volume, fees, APR, etc.).
*/
stats: PoolStats | undefined;
/**
* Creates a new instance of the `Pool` class for on-chain interaction.
*
* @param pool - The fetched `PoolObject` from Aftermath API or on-chain query.
* @param config - Optional caller configuration (e.g., network, access token).
* @param Provider - An optional `AftermathApi` instance for advanced transaction usage.
*/
constructor(pool: PoolObject, config?: CallerConfig, Provider?: AftermathApi | undefined);
/**
* Builds or fetches a deposit transaction to add liquidity to this pool.
* The resulting `Transaction` can be signed and submitted by the user.
*
* @param inputs - The deposit parameters including coin amounts, slippage, etc.
* @returns A `Transaction` to deposit funds into the pool.
*
* @example
* ```typescript
* const depositTx = await pool.getDepositTransaction({
* walletAddress: "0x...",
* amountsIn: { "0x<coin>": BigInt(1000000) },
* slippage: 0.01,
* });
* ```
*/
getDepositTransaction(inputs: ApiPoolDepositBody): Promise<Transaction>;
/**
* Builds or fetches a withdrawal transaction to remove liquidity from this pool.
*
* @param inputs - The parameters specifying how much LP is burned, desired coins out, slippage, etc.
* @returns A `Transaction` to withdraw funds from the pool.
*
* @example
* ```typescript
* const withdrawTx = await pool.getWithdrawTransaction({
* walletAddress: "0x...",
* amountsOutDirection: {
* "0x<coin>": BigInt(500000),
* },
* lpCoinAmount: BigInt(1000000),
* slippage: 0.01,
* });
* ```
*/
getWithdrawTransaction(inputs: ApiPoolWithdrawBody): Promise<Transaction>;
/**
* Builds or fetches a transaction to withdraw all coin types from this pool,
* effectively "burning" an LP position in exchange for multiple coin outputs.
*
* @param inputs - The parameters specifying how much LP to burn.
* @returns A `Transaction` to withdraw all coins from the pool in proportion.
*
* @example
* ```typescript
* const allCoinWithdrawTx = await pool.getAllCoinWithdrawTransaction({
* walletAddress: "0x...",
* lpCoinAmount: BigInt(500000),
* });
* ```
*/
getAllCoinWithdrawTransaction(inputs: ApiPoolAllCoinWithdrawBody): Promise<Transaction>;
/**
* Builds or fetches a trade transaction to swap between two coin types in this pool.
*
* @param inputs - The trade parameters including coin in/out, amounts, slippage, etc.
* @returns A `Transaction` that can be signed and executed for the swap.
*
* @example
* ```typescript
* const tradeTx = await pool.getTradeTransaction({
* walletAddress: "0x...",
* coinInType: "0x<coinA>",
* coinInAmount: BigInt(1000000),
* coinOutType: "0x<coinB>",
* slippage: 0.005,
* });
* ```
*/
getTradeTransaction(inputs: ApiPoolTradeBody): Promise<Transaction>;
/**
* Builds a transaction to update the DAO fee percentage for this pool,
* if it has a DAO fee configured. The user must own the appropriate
* `daoFeePoolOwnerCap`.
*
* @param inputs - Includes user wallet, `daoFeePoolOwnerCapId`, and the new fee percentage.
* @returns A `Transaction` that can be signed to update the DAO fee on chain.
* @throws If this pool has no DAO fee configuration.
*
* @example
* ```typescript
* const tx = await pool.getUpdateDaoFeeTransaction({
* walletAddress: "0x...",
* daoFeePoolOwnerCapId: "0x<capId>",
* newFeePercentage: 0.01, // 1%
* });
* ```
*/
getUpdateDaoFeeTransaction(inputs: {
walletAddress: SuiAddress;
daoFeePoolOwnerCapId: ObjectId;
newFeePercentage: Percentage;
}): Promise<Transaction>;
/**
* Builds a transaction to update the DAO fee recipient for this pool,
* if it has a DAO fee configured. The user must own the appropriate
* `daoFeePoolOwnerCap`.
*
* @param inputs - Includes user wallet, `daoFeePoolOwnerCapId`, and the new fee recipient.
* @returns A `Transaction` that can be signed to update the DAO fee recipient on chain.
* @throws If this pool has no DAO fee configuration.
*
* @example
* ```typescript
* const tx = await pool.getUpdateDaoFeeRecipientTransaction({
* walletAddress: "0x...",
* daoFeePoolOwnerCapId: "0x<capId>",
* newFeeRecipient: "0x<recipient>",
* });
* ```
*/
getUpdateDaoFeeRecipientTransaction(inputs: {
walletAddress: SuiAddress;
daoFeePoolOwnerCapId: ObjectId;
newFeeRecipient: SuiAddress;
}): Promise<Transaction>;
/**
* Fetches comprehensive pool statistics (volume, TVL, fees, APR, etc.) from the Aftermath API.
* Also caches the result in `this.stats`.
*
* @returns A promise resolving to `PoolStats` object.
*
* @example
* ```typescript
* const stats = await pool.getStats();
* console.log(stats.volume, stats.fees, stats.apr);
* ```
*/
getStats(): Promise<PoolStats>;
/**
* Caches the provided stats object into `this.stats`.
*
* @param stats - The `PoolStats` object to store.
*/
setStats(stats: PoolStats): void;
/**
* Fetches an array of volume data points for a specified timeframe.
* This is often used for charting or historical references.
*
* @param inputs - Contains a `timeframe` key, such as `"1D"` or `"1W"`.
* @returns A promise resolving to an array of `PoolDataPoint`.
*
* @example
* ```typescript
* const volumeData = await pool.getVolumeData({ timeframe: "1D" });
* console.log(volumeData); // e.g. [{ time: 1686000000, value: 123.45 }, ...]
* ```
*/
getVolumeData(inputs: {
timeframe: PoolGraphDataTimeframeKey;
}): Promise<PoolDataPoint[]>;
/**
* Fetches an array of fee data points for a specified timeframe.
*
* @param inputs - Contains a `timeframe` key, e.g., `"1D"` or `"1W"`.
* @returns A promise resolving to an array of `PoolDataPoint`.
*
* @example
* ```typescript
* const feeData = await pool.getFeeData({ timeframe: "1D" });
* console.log(feeData);
* ```
*/
getFeeData(inputs: {
timeframe: PoolGraphDataTimeframeKey;
}): Promise<PoolDataPoint[]>;
/**
* Retrieves the 24-hour volume for this specific pool.
*
* @returns A promise resolving to a number (volume in 24h).
*
* @example
* ```typescript
* const vol24h = await pool.getVolume24hrs();
* console.log("Pool 24h Volume:", vol24h);
* ```
*/
getVolume24hrs: () => Promise<number>;
/**
* Fetches user interaction events (deposit/withdraw) with this pool, optionally paginated.
*
* @param inputs - Includes user `walletAddress` and optional pagination fields.
* @returns A promise that resolves to `PoolDepositEvent | PoolWithdrawEvent` objects with a cursor if more exist.
*
* @example
* ```typescript
* const events = await pool.getInteractionEvents({ walletAddress: "0x...", limit: 10 });
* console.log(events.events, events.nextCursor);
* ```
*/
getInteractionEvents(inputs: ApiIndexerEventsBody & {
walletAddress: SuiAddress;
}): Promise<IndexerEventsWithCursor<PoolDepositEvent | PoolWithdrawEvent>>;
/**
* Calculates the instantaneous spot price for swapping from `coinInType`
* to `coinOutType` within this pool. Optionally includes fees in the price.
*
* @param inputs - Object specifying input coin, output coin, and a boolean for `withFees`.
* @returns The numerical spot price (float).
*
* @example
* ```typescript
* const price = pool.getSpotPrice({
* coinInType: "0x<coinA>",
* coinOutType: "0x<coinB>",
* withFees: true,
* });
* console.log("Spot Price:", price);
* ```
*/
getSpotPrice: (inputs: {
coinInType: CoinType;
coinOutType: CoinType;
withFees?: boolean;
}) => number;
/**
* Calculates how much output coin you would receive when trading
* a given input coin and amount in this pool, factoring in protocol
* and optional DAO fees.
*
* @param inputs - Includes `coinInType`, `coinInAmount`, and `coinOutType`.
* @returns A bigint representing how many output coins you'd get.
* @throws Error if the trade amount is too large relative to the pool balance.
*
* @example
* ```typescript
* const amountOut = pool.getTradeAmountOut({
* coinInType: "0x<coinA>",
* coinInAmount: BigInt(1000000),
* coinOutType: "0x<coinB>",
* });
* ```
*/
getTradeAmountOut: (inputs: {
coinInType: CoinType;
coinInAmount: Balance;
coinOutType: CoinType;
referral?: boolean;
}) => Balance;
/**
* Calculates how much input coin is required to obtain a certain output coin amount
* from this pool, factoring in fees.
*
* @param inputs - Includes `coinInType`, desired `coinOutAmount`, and `coinOutType`.
* @returns A bigint representing the needed input amount.
* @throws Error if the desired output is too large relative to pool balances.
*
* @example
* ```typescript
* const amountIn = pool.getTradeAmountIn({
* coinInType: "0x<coinA>",
* coinOutAmount: BigInt(1000000),
* coinOutType: "0x<coinB>"
* });
* ```
*/
getTradeAmountIn: (inputs: {
coinInType: CoinType;
coinOutAmount: Balance;
coinOutType: CoinType;
referral?: boolean;
}) => Balance;
/**
* Calculates how many LP tokens you receive for providing liquidity
* in specific coin amounts. Also returns a ratio for reference.
*
* @param inputs - Contains the amounts in for each coin in the pool.
* @returns An object with `lpAmountOut` and `lpRatio`.
*
* @example
* ```typescript
* const depositCalc = pool.getDepositLpAmountOut({
* amountsIn: { "0x<coinA>": BigInt(1000000), "0x<coinB>": BigInt(500000) },
* });
* console.log(depositCalc.lpAmountOut, depositCalc.lpRatio);
* ```
*/
getDepositLpAmountOut: (inputs: {
amountsIn: CoinsToBalance;
referral?: boolean;
}) => {
lpAmountOut: Balance;
lpRatio: number;
};
/**
* Calculates how many coins a user will receive when withdrawing a specific ratio or LP amount.
* This method is used in multi-coin withdrawals where you specify how much of each coin you want.
*
* @param inputs - The LP ratio and an object specifying direction amounts for each coin.
* @returns A `CoinsToBalance` object with final amounts out, factoring in DAO fees.
*
* @example
* ```typescript
* const outAmounts = pool.getWithdrawAmountsOut({
* lpRatio: 0.1,
* amountsOutDirection: { "0x<coinA>": BigInt(500000) },
* });
* console.log(outAmounts);
* ```
*/
getWithdrawAmountsOut: (inputs: {
lpRatio: number;
amountsOutDirection: CoinsToBalance;
referral?: boolean;
}) => CoinsToBalance;
/**
* A simplified multi-coin withdraw approach: calculates all outputs by proportion of the
* user's LP share among selected coin types. Useful for approximate or "blind" all-coin out logic.
*
* @param inputs - Contains the `lpCoinAmountIn` to burn, and which coin types to receive.
* @returns A record mapping coin type => final amounts out.
*/
getWithdrawAmountsOutSimple: (inputs: {
lpCoinAmountIn: Balance;
coinTypesOut: CoinType[];
referral?: boolean;
}) => CoinsToBalance;
/**
* Calculates how many coins you get when withdrawing **all** coin types from the pool,
* given a ratio. This is typically used for proportionate withdrawal.
*
* @param inputs - Includes `lpRatio`, the portion of your LP to burn (0 < ratio < 1).
* @returns A record of coin type => amounts out, after factoring in any fees.
*
* @example
* ```typescript
* const allOut = pool.getAllCoinWithdrawAmountsOut({ lpRatio: 0.1 });
* console.log(allOut); // amounts for each coin
* ```
*/
getAllCoinWithdrawAmountsOut: (inputs: {
lpRatio: number;
referral?: boolean;
}) => CoinsToBalance;
/**
* For multi-coin withdraw, calculates the ratio of how much LP you are burning
* relative to the total supply. e.g. if user burns 100 of 1000 supply => ratio 0.1.
*
* @param inputs - Contains the `lpCoinAmountIn` to burn.
* @returns A float ratio (0 < ratio < 1).
*/
getMultiCoinWithdrawLpRatio: (inputs: {
lpCoinAmountIn: bigint;
}) => number;
/**
* For an all-coin withdraw, calculates the ratio of how much LP is burned
* relative to total supply. e.g. if user burns 50 of 200 supply => ratio 0.25.
*
* @param inputs - Contains the `lpCoinAmountIn`.
* @returns A float ratio, typically 0 < ratio < 1.
*/
getAllCoinWithdrawLpRatio: (inputs: {
lpCoinAmountIn: bigint;
}) => number;
/**
* Returns an array of coin types in ascending lexicographic order
* for the coins contained in this pool.
*
* @returns An array of coin type strings.
*/
coins: () => CoinType[];
/**
* Returns an array of `PoolCoin` objects, one for each coin in this pool,
* sorted lexicographically by coin type.
*
* @returns An array of `PoolCoin`.
*/
poolCoins: () => PoolCoin[];
/**
* Returns an array of `[CoinType, PoolCoin]` pairs, sorted by coin type.
*
* @returns An array of coin-type => `PoolCoin` pairs.
*/
poolCoinEntries: () => [CoinType, PoolCoin][];
/**
* Returns the current DAO fee percentage, if configured (0 < fee <= 100%).
*
* @returns A decimal fraction representing the fee (e.g., 0.01 = 1%) or `undefined`.
*/
daoFeePercentage: () => Percentage | undefined;
/**
* Returns the Sui address that currently receives the DAO fee portion of
* pool trades, or `undefined` if no DAO fee is configured.
*
* @returns The DAO fee recipient address.
*/
daoFeeRecipient: () => SuiAddress | undefined;
/**
* Applies the DAO fee (if present) to a given `amount`, effectively reducing
* that amount by the fee fraction. e.g. if fee is 2%, it returns 98% of the input.
*
* @param inputs - Contains `amount` as a bigint.
* @returns The post-fee amount as a bigint.
*/
private getAmountWithDAOFee;
/**
* The inverse operation of `getAmountWithDAOFee`, used in internal calculations
* when we need to back out how much input was needed prior to the fee cut.
*
* @param inputs - Contains `amount` as a bigint.
* @returns The pre-fee amount as a bigint.
*/
private getAmountWithoutDAOFee;
/**
* Provides an instance of the Pools provider from `AftermathApi`.
* Throws an error if not defined.
*/
private useProvider;
}
//# sourceMappingURL=pool.d.ts.map