Turbos
  • 📖About Turbos
    • Getting Started
    • About Turbos Finance
    • Why Sui Network
  • 📽️Products
    • Concentrated Liquidity AMM
      • Fee Tier for Token Pairs
      • Oracle
    • Automated Rebalance Vault
      • Step-by-Step Guide
      • Referral Program
    • TurboStar
    • TurbosBoost
  • 💡Protocol Concepts
    • About Concentrated Liquidity
    • Fee Tiers
    • Glossary
  • 🪙Tokenomics
    • TURBOS
    • Token Utility
    • Emission Distribution
  • ⚙️Others
    • Roadmap
    • Audits
    • Brand kit
    • Risks
    • Token Address
  • Developer Docs
    • Dev Overview
    • via SDK
      • Get Started
      • Initialize SDK
      • Clmm
        • getConfig
        • getFeesConfig
        • getPools
        • getPool
        • createPool
        • createAndAddLiquidity
        • increaseLiquidity
        • decreaseLiquidity
        • removeLiquidityAndBurn
        • preSwap
        • swap
        • collect Fees
        • collect Rewards
        • Fee
        • Liquidity and coin amounts calculation
        • APR calculation
      • Vault
        • createAndDepositVault
        • depositVault
        • withdrawVault
        • withdrawAllVault
        • collectClmmRewardDirectReturnVault
        • closeVault
        • computeTokenWithdrawVaultSwapResult
        • getVaultBalanceAmount
        • getMyVaults
  • Turbos Fun
    • Base
    • Create Pool
    • Calculate Token Amount
    • Buy
    • Sell
  • 📱Socials
    • Official website
    • Twitter
    • Discord
    • Medium
    • Zealy
  • Tutorials
    • Supported Wallets
      • SafePal Wallet
    • Move tutorial
    • Sui tutorial
Powered by GitBook
On this page
  1. Turbos Fun

Calculate Token Amount

CalculateAmountOptions

export interface CalculateAmountOptions {
  coinType: string;
  amount: string;
  threshold: string;
  atob: boolean;
  isExact: boolean;
  isInit?: boolean;
}
async calculateTokenAndThresholdAmount(options: CalculateAmountOptions) {
    const { coinType, atob, isExact, threshold, isInit = false } = options;
    const poolInfo = !isInit ? await this.getPoolInfo(coinType) : undefined;

    const virtual_token_reserves = new Decimal(
      poolInfo ? poolInfo.virtual_token_reserves : this.initial_virtual_token_reserves,
    );
    const virtual_sui_reserves = new Decimal(
      poolInfo ? poolInfo.virtual_sui_reserves : this.initial_virtual_sui_reserves,
    );
    const amount = new Decimal(options.amount);

    let _amount: Decimal;
    let _amountThreshold: Decimal;

    if (atob) {
      if (isExact) {
        const _fee = new Decimal(1).sub(this.platform_fee); // 0.005 fee
        _amount = virtual_token_reserves.sub(
          virtual_sui_reserves
            .mul(virtual_token_reserves)
            .div(virtual_sui_reserves.add(amount.mul(_fee))),
        );
        _amountThreshold = new Decimal(100).sub(threshold).div(100).mul(_amount);
      } else {
        const _fee = new Decimal(1).add(this.platform_fee); // 0.005 fee
        const _threshold = new Decimal(100).add(threshold).div(100);
        _amount = virtual_sui_reserves
          .mul(virtual_token_reserves)
          .div(virtual_token_reserves.sub(amount))
          .sub(virtual_sui_reserves)
          .mul(_fee)
          .mul(_threshold);

        _amountThreshold = _amount;
      }
    } else {
      if (isExact) {
        const _fee = new Decimal(1).sub(this.platform_fee); // 0.005 fee
        _amount = virtual_sui_reserves
          .sub(
            virtual_sui_reserves
              .mul(virtual_token_reserves)
              .div(virtual_token_reserves.add(amount)),
          )
          .mul(_fee);

        _amountThreshold = new Decimal(100).sub(threshold).div(100).mul(_amount);
      } else {
        const _fee = new Decimal(1).add(this.platform_fee); // 0.005 fee
        _amount = virtual_token_reserves.sub(
          virtual_sui_reserves
            .mul(virtual_token_reserves)
            .div(virtual_sui_reserves.add(amount.mul(_fee))),
        );

        _amountThreshold = new Decimal(100).add(threshold).div(100).mul(_amount);
      }
    }

    return {
      tokenAmount: _amount.toFixed(0),
      thresholdAmount: _amountThreshold.toFixed(0),
    };
  }
PreviousCreate PoolNextBuy

Last updated 3 days ago