UNPKG

fsl-js-sdk

Version:
192 lines (191 loc) 9.68 kB
import { u64 } from '@solana/spl-token-v0'; import { Keypair, PublicKey } from '@solana/web3.js'; import Decimal from 'decimal.js'; import { DooarU64 } from '..'; import { TransactionPayload } from '../utils'; export type DepositQuote = { minPoolTokenAmountOut: DooarU64; maxTokenAIn: DooarU64; maxTokenBIn: DooarU64; }; export type WithdrawQuote = { minTokenAOut: DooarU64; minTokenBOut: DooarU64; maxPoolTokenAmountIn: DooarU64; }; /** * Allows interactions with an Dooar liquidity pool. */ export type DooarPool = { /** * Query the token of tokenA in this pool. * @returns Returns the token id of tokenA in this pool */ getTokenA: () => DooarPoolToken; /** * Query the token of tokenB in this pool. * @returns Returns the token id of tokenB in this pool */ getTokenB: () => DooarPoolToken; /** * Query the mint public key for the pool token of this pool. * @returns Returns the tokenMint public key of this pool */ getPoolTokenMint: () => PublicKey; /** * Query the balance for an user address * @param wallet The public key for the user. * @return Returns the amount of LP token the user owns for this pool. */ getLPBalance: (owner: PublicKey) => Promise<DooarU64>; /** * Query the supply of LP tokens for this pool. * @return Returns the supply of LP tokens for this pool */ getLPSupply: () => Promise<DooarU64>; /** * Get the latest quote to trade one token to another in this pool * * Note: slippage supports a maximum scale of 1 (ex. 0.1%). Additional decimal places will be floored. * * @param inputTokenId The token you want to trade from * @param inputAmount The amount of token you would to trade * @param slippage An optional slippage in percentage you are willing to take in this trade (default: 0.1%) * @return Returns a quote on the exchanged token based on the input token amount */ getQuote: (inputToken: DooarToken, inputAmount: Decimal | DooarU64, slippage?: Decimal) => Promise<Quote>; /** * Get the latest quote to trade on token to another in this pool using user provided pool amounts * * Note: slippage supports a maximum scale of 1 (ex. 0.1%). Additional decimal places will be floored. * * @param inputTokenId The token you want to trade from * @param inputAmount The amount of token you would to trade * @param inputTokenPoolAmount The amount of input tokens in the pool * @param outputTokenPoolAmount The amount of output tokens in the pool * @param slippage An optional slippage in percentage you are willing to take in this trade (default: 0.1%) * @return Returns a quote on the exchanged token based on the input token amount */ getQuoteWithPoolAmounts: (inputToken: DooarToken, inputAmount: Decimal | DooarU64, inputTokenPoolAmount: u64, outputTokenPoolAmount: u64, slippage?: Decimal) => Promise<Quote>; /** * Perform a swap from the input type to the other token in the pool. * Fee for the transaction will be paid by the owner's wallet. * * NOTE: * 1. Associated Token Address initialization instructions will be appended if the ATA of the specified token does not exist in the user's wallet * 2. DooarU64 must have the same scale as the corresponding token scale value * * @param owner The keypair for the user's wallet or just the user's public key * @param inputToken An Dooar supported token in the user's wallet to swap from * @param amountIn The amount of inputToken to swap from * @param minimumAmountOut The minimum amount of outputToken to receive from this swap * @return The transaction signature of the swap instruction */ swap: (owner: Keypair | PublicKey, inputToken: DooarToken, amountIn: Decimal | DooarU64, minimumAmountOut: Decimal | DooarU64) => Promise<TransactionPayload>; /** * Get suggested pool token deposit amount based on required constraints on maximum tokenA amount and maximum tokenB amount * * Note: * 1. minPoolTokenAmountOut in the output type is a misnomer, and it represents the _exact_ poolTokenAmountOut value * * @param maxTokenAIn The maximum amount of tokenA to deposit in exchange for pool token * @param maxTokenBIn The maximum amount of tokenB to deposit in exchange for pool token * @param slippage An optional slippage in percentage you are willing to take in deposit (default: 0.1%) * @return Returns the input for deposit */ getDepositQuote: (maxTokenAIn: Decimal | DooarU64, maxTokenBIn: Decimal | DooarU64, slippage?: Decimal) => Promise<DepositQuote>; /** * Perform a deposit: send tokenA and tokenB, and receive a poolToken in return. * Fee for the transaction will be paid by the owner's wallet. * * NOTE: * 1. Associated Token Address initialization instructions will be appended if the ATA of the specified token does not exist in the user's wallet * 2. DooarU64 must have the same scale as the corresponding token scale value * * @param owner The keypair for the user's wallet or just the user's public key * @param maxTokenAIn The maximum amount of tokenA to send * @param maxTokenBIn The maximum amount of tokenB to send * @param minPoolTokenAmountOut The amount of poolToken to receive * @return The transaction signature of the deposit instruction */ deposit: (owner: Keypair | PublicKey, maxTokenAIn: Decimal | DooarU64, maxTokenBIn: Decimal | DooarU64, minPoolTokenAmountOut: Decimal | DooarU64) => Promise<TransactionPayload>; /** * Get suggested withdraw token amounts based on required withdraw amount of the pool token / one of the paired tokens * * Throws error if withdrawTokenMint does not equal tokenMint of tokenA, tokenB, or poolToken of this pool * * Note: * 1. maxPoolTokenAmountIn in the output type is a misnomer, and it represents the _exact_ poolTokenAmountIn value * * @param quoteTokenAmount The amount of tokens to withdraw in terms of tokenA amount, tokenB amount, or poolToken amount * @param quoteTokenMint The token mint public key of tied to quoteTokenAmount. It should be the mint of tokenA, tokenB, or poolToken * @param poolTokenAccount the account of the poolToken to withdraw from * @param slippage An optional slippage in percentage you are willing to take in withdraw (default: 0.1%) * @return Returns the input for withdraw */ getWithdrawQuote: (quoteTokenAmount: Decimal | DooarU64, quoteTokenMint: PublicKey, poolTokenAccount: PublicKey, slippage?: Decimal) => Promise<WithdrawQuote>; /** * Perform a withdraw: send poolToken, and receive tokenA and tokenB in return. * Fee for the transaction will be paid by the owner's wallet. * * NOTE: * 1. Associated Token Address initialization instructions will be appended if the ATA of the specified token does not exist in the user's wallet * 2. DooarU64 must have the same scale as the corresponding token scale value * * @param owner The keypair for the user's wallet or just the user's public key * @param poolTokenAmountIn The amount of poolToken to send * @param minTokenAOut The minimum amount of tokenA to receive * @param minTokenBOut The minimum amount of tokenB to receive * @return The transaction signature of the withdraw instruction */ withdraw: (owner: Keypair | PublicKey, poolTokenAmountIn: Decimal | DooarU64, minTokenAOut: Decimal | DooarU64, minTokenBOut: Decimal | DooarU64) => Promise<TransactionPayload>; loadFeeAccount: () => Promise<void>; }; /** * An Dooar Token * @param tag The tag of the token * @param name The presentable name of the token * @param mint The mint public key for the token * @param scale The scale of the u64 return type */ export type DooarToken = { tag: string; name: string; mint: PublicKey; scale: number; }; /** * An Dooar Token within an DooarPool * @param addr The public key for this token for this Dooar Pool */ export type DooarPoolToken = DooarToken & { addr: PublicKey; }; export type Quote = { /** * Returns the rate of exchange given the trade amount. Fees are included. * Rate is zero if the input trade amount, input or output token balance in pool is zero. * @returns a function that returns the rate of exchange when the quote was built (denominated by output token) */ getRate: () => Decimal; /** * Returns the fee that will be charged in this exchange. * @return a function that returns the fee (denominated by input token) that will be charged in this exchange. */ getLPFees: () => DooarU64; /** * Returns the % impact to the rate if this transaction goes through. * @return a function to return the % impact to the rate if this transaction goes through. Zero if input or output token balance in pool is zero. */ getPriceImpact: () => Decimal; /** * Returns the expected amount of output tokens returned if this exchange is transacted. Fees applied. * @return a function to return the expected amount of output tokens returned if this exchange is transacted */ getExpectedOutputAmount: () => DooarU64; /** * Returns the minimum amount of output tokens returned if this exchange is transacted. Fees & maximum slippage applied. * @return a function to return the minimum amount of output tokens returned if this exchange is transacted */ getMinOutputAmount: () => DooarU64; };