Skip to content

Commit

Permalink
Make compute_swap function public (#535)
Browse files Browse the repository at this point in the history
* make compute_swap method public

* refine comments, fix build warning
  • Loading branch information
0xh3rman authored Nov 24, 2024
1 parent a0d364f commit db2105a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions rust-sdk/core/src/math/tick_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use super::{

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TickArraySequence<const SIZE: usize> {
tick_arrays: [Option<TickArrayFacade>; SIZE],
tick_spacing: u16,
pub tick_arrays: [Option<TickArrayFacade>; SIZE],
pub tick_spacing: u16,
}

impl<const SIZE: usize> TickArraySequence<SIZE> {
Expand Down
35 changes: 28 additions & 7 deletions rust-sdk/core/src/quote/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,34 @@ pub fn swap_quote_by_output_token(
})
}

// Private functions

struct SwapResult {
token_a: u64,
token_b: u64,
trade_fee: u64,
pub struct SwapResult {
pub token_a: u64,
pub token_b: u64,
pub trade_fee: u64,
}

fn compute_swap<const SIZE: usize>(
/// 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.
pub fn compute_swap<const SIZE: usize>(
token_amount: u64,
sqrt_price_limit: u128,
whirlpool: WhirlpoolFacade,
Expand Down Expand Up @@ -275,6 +294,8 @@ fn compute_swap<const SIZE: usize>(
})
}

// Private functions

fn get_next_liquidity(
current_liquidity: u128,
next_tick: Option<&TickFacade>,
Expand Down

0 comments on commit db2105a

Please sign in to comment.