Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: allow validator find script datum via both DatumDict or InlineDatum & add partial swap #7

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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