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

improve pool routing to support N Pools #5

Merged
merged 5 commits into from
Oct 16, 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
16 changes: 10 additions & 6 deletions lib/amm_dex_v2/math.ak
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ pub fn calculate_profit_sharing(

// Calculate which amount will be swapped to supply enough amount for depositing
// Refer: https://blog.alphafinance.io/onesideduniswap/
// Final formular:
// swap_amount ^ 2 * ( 1 - f ) * ( amount_out + reserve_out )
// + swap_amount * ( 2 - f ) * reserve_in * ( amount_out + reserve_out )
// + ( reserve_in ^ 2 * amount_out + reserve_in * reserve_out * amount_out )
// = 0
pub fn calculate_swap_amount(
amount_in: Int,
amount_out: Int,
Expand All @@ -142,13 +147,11 @@ pub fn calculate_swap_amount(
amount_out * calculate_pow(reserve_in) - amount_in * reserve_in * reserve_out
)
let z = 2 * ( amount_out + reserve_out )
let b = ( 2 * trading_fee_denominator - trading_fee_numerator ) * x
let a =
calculate_pow(x) * calculate_pow(
2 * trading_fee_denominator - trading_fee_numerator,
) - y * trading_fee_denominator * (
calculate_pow(b) - y * trading_fee_denominator * (
trading_fee_denominator - trading_fee_numerator
)
let b = ( 2 * trading_fee_denominator - trading_fee_numerator ) * x
let numerator = calculate_sqrt(a) - b
let denominator = z * ( trading_fee_denominator - trading_fee_numerator )
(numerator, denominator)
Expand Down Expand Up @@ -185,8 +188,9 @@ pub fn calculate_deposit_amount(
trading_fee_numerator: trading_fee_numerator,
trading_fee_denominator: trading_fee_denominator,
)
// TODO: should +1?
let reserve_product_after_swap =
( reserve_a * swap_amount_a_denominator - swap_amount_a_numerator ) * (
( reserve_a * swap_amount_a_denominator + swap_amount_a_numerator ) * (
reserve_b * receive_amount_b_denominator - receive_amount_b_numerator
) / ( swap_amount_a_denominator * receive_amount_b_denominator ) + 1
let lp_amount =
Expand Down Expand Up @@ -215,7 +219,7 @@ pub fn calculate_deposit_amount(
trading_fee_denominator: trading_fee_denominator,
)
let reserve_product_after_swap =
( reserve_b * swap_amount_b_denominator - swap_amount_b_numerator ) * (
( reserve_b * swap_amount_b_denominator + swap_amount_b_numerator ) * (
reserve_a * receive_amount_a_denominator - receive_amount_a_numerator
) / ( swap_amount_b_denominator * receive_amount_a_denominator ) + 1
let lp_amount =
Expand Down
Loading