pub fn compute_swap<const SIZE: usize>(
    token_amount: u64,
    sqrt_price_limit: u128,
    whirlpool: WhirlpoolFacade,
    tick_sequence: TickArraySequence<SIZE>,
    a_to_b: bool,
    specified_input: bool,
    _timestamp: u64,
) -> Result<SwapResult, &'static str>
Expand description

Computes the amounts of tokens A and B based on the current Whirlpool state and tick sequence.

§Arguments

  • token_amount: The input or output amount specified for the swap. Must be non-zero.
  • sqrt_price_limit: The price limit for the swap represented as a square root. If set to 0, it defaults to the minimum or maximum sqrt price based on the direction of the swap.
  • whirlpool: The current state of the Whirlpool AMM, including liquidity, price, and tick information.
  • tick_sequence: A sequence of ticks used to determine price levels during the swap process.
  • a_to_b: Indicates the direction of the swap:
    • true: Swap from token A to token B.
    • false: Swap from token B to token A.
  • specified_input: Determines if the input amount is specified:
    • true: token_amount represents the input amount.
    • false: token_amount represents the output amount.
  • _timestamp: A placeholder for future full swap logic, currently ignored.

§Returns

A Result containing a SwapResult struct if the swap is successful, or an ErrorCode if the computation fails.

§Notes

  • This function doesn’t take into account slippage tolerance.
  • This function doesn’t take into account transfer fee extension.