Skip to content

Commit

Permalink
enhance: allow validator find script datum via both DatumDict or Inli…
Browse files Browse the repository at this point in the history
…neDatum & add partial swap (#7)

* allow validator find script datum via both DatumDict or InlineDatum

* add partial swap

* add some safety check

* add some safety check
  • Loading branch information
h2physics authored Oct 24, 2023
1 parent 454504d commit 57fff31
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 230 deletions.
33 changes: 23 additions & 10 deletions lib/amm_dex_v2/math.ak
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ pub fn calculate_amount_out(
numerator / denominator
}

pub fn calculate_max_in_swap(
reserve_in: Int,
reserve_out: Int,
trading_fee_numerator: Int,
trading_fee_denominator: Int,
io_ratio_numerator: Int,
io_ratio_denominator: Int,
) -> Int {
let diff = trading_fee_denominator - trading_fee_numerator
let numerator =
io_ratio_numerator * diff * reserve_out - io_ratio_denominator * trading_fee_denominator * reserve_in
let denominator = io_ratio_denominator * diff
let max_in_swap = numerator / denominator
expect max_in_swap > 0
max_in_swap
}

pub fn calculate_amount_out_fraction(
reserve_in: Int,
reserve_out: Int,
Expand All @@ -83,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
Loading

0 comments on commit 57fff31

Please sign in to comment.