solana_exec/utilities/
get_min_amount_out.rs

1use crate::types::swap_config::SwapConfig;
2use primitive_types::U512;
3use solana_central::PoolTrait;
4use solana_central::SwapDirection;
5use solana_sdk::native_token::LAMPORTS_PER_SOL;
6
7/// Calculate minimum amount out for a swap based on slippage tolerance. Uses the formula: 
8/// min_amount_out = amount_in * price_lp * (10^9 - slippage_lp) / (10^9 * 10^9)`. This calculation
9/// does not account for price impact or swap fees, which prevents execution when pools have 
10/// extreme fees (100%) or zero liquidity. For exact calculations, use protocol-specific functions
11/// that support manual amount specification. Note: 100% slippage means accepting nothing for the
12/// trade (industry standard).
13pub fn get_min_amount_out(swap_config: &SwapConfig, pool: &dyn PoolTrait) -> u64 {
14  // 100% slippage means accepting nothing for the trade as a minimum
15  if swap_config.slippage_lp == LAMPORTS_PER_SOL {
16    return 0;
17  }
18  let lamports = U512::from(LAMPORTS_PER_SOL);
19  let amount_in = U512::from(swap_config.amount_in);
20  let price = U512::from(if swap_config.direction == SwapDirection::AToB {
21    pool.price_b_over_a_lp()
22  } else {
23    pool.price_a_over_b_lp()
24  });
25  let slippage_lp = U512::from(swap_config.slippage_lp);
26  let min_amount_out = amount_in * price * (lamports - slippage_lp) / (lamports.pow(U512::from(2)));
27  min_amount_out.as_u64()
28}