Skip to content

Commit

Permalink
add some safety check
Browse files Browse the repository at this point in the history
  • Loading branch information
h2physics committed Oct 23, 2023
1 parent e074857 commit c9be21e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
20 changes: 9 additions & 11 deletions lib/amm_dex_v2/math.ak
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ pub fn calculate_max_in_swap(
let numerator =
io_ratio_numerator * diff * reserve_out - io_ratio_denominator * trading_fee_denominator * reserve_in
let denominator = io_ratio_denominator * diff
numerator / denominator
let max_in_swap = numerator / denominator
expect max_in_swap > 0
max_in_swap
}

pub fn calculate_amount_out_fraction(
Expand All @@ -98,16 +100,12 @@ pub fn calculate_amount_in(
amount_out: Int,
trading_fee_numerator: Int,
trading_fee_denominator: Int,
) -> Option<Int> {
if amount_out >= reserve_out {
None
} else {
let diff = trading_fee_denominator - trading_fee_numerator
let numerator = reserve_in * amount_out * trading_fee_denominator
let denominator = ( reserve_out - amount_out ) * diff
let amount_in = numerator / denominator + 1
Some(amount_in)
}
) -> Int {
expect amount_out < reserve_out
let diff = trading_fee_denominator - trading_fee_numerator
let numerator = reserve_in * amount_out * trading_fee_denominator
let denominator = ( reserve_out - amount_out ) * diff
numerator / denominator + 1
}

// Calculate liquidity amount which will be minted for profit sharing
Expand Down
3 changes: 1 addition & 2 deletions lib/amm_dex_v2/order_validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ fn validate_swap_exact_out(
temp_amount_in
}
let has_enough_amount_in = maximum_amount_in > 0
let necessary_amount_in_opt =
let necessary_amount_in =
math.calculate_amount_in(
reserve_in,
reserve_out,
expected_receive,
trading_fee_numerator,
trading_fee_denominator,
)
let necessary_amount_in = utils.must_parse_option(necessary_amount_in_opt)
let not_over_slippage = necessary_amount_in <= maximum_amount_in
let actual_amount_out =
value.quantity_of(
Expand Down
Loading

0 comments on commit c9be21e

Please sign in to comment.