Skip to content

Commit

Permalink
update docs (#36)
Browse files Browse the repository at this point in the history
* update docs

* update docs

* not equal

* format 2
  • Loading branch information
m1n999999 authored Feb 21, 2024
1 parent ec38cd1 commit 7437012
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
40 changes: 20 additions & 20 deletions amm-v2-docs/amm-v2-specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ Transaction structure:
- Step:
- _a_to_b_direction_
- _swap_amount_
- _minimum_receive_ (TODO: Link formula section here)
- _minimum_receive_([Formula](./formula.md#1-swap-exact-in))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
Expand All @@ -552,7 +552,7 @@ Transaction structure:
- Step:
- _a_to_b_direction_
- _swap_amount_
- _stop_loss_receive_ (TODO: Link formula section here)
- _stop_loss_receive_([Formula](./formula.md#1-swap-exact-in))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
Expand All @@ -561,17 +561,17 @@ Transaction structure:
- Step:
- _a_to_b_direction_
- _swap_amount_
- _minimum_receive_ (TODO: Link formula section here)
- _stop_loss_receive_ (TODO: Link formula section here)
- _minimum_receive_([Formula](./formula.md#1-swap-exact-in))
- _stop_loss_receive_([Formula](./formula.md#1-swap-exact-in))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
- Token is swapping
- _SwapExactOut_:
- Step:
- _a_to_b_direction_
- _maximum_swap_amount_
- _expected_receive_ (TODO: Link formula section here)
- _maximum_swap_amount_([Formula](./formula.md#2-swap-exact-out))
- _expected_receive_
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
Expand All @@ -580,15 +580,15 @@ Transaction structure:
- Step:
- _deposit_amount_a_
- _deposit_amount_b_
- _minimum_lp_ (TODO: Link formula section here)
- _minimum_lp_([Formula](./formula.md#3-deposit))
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
- Token A and Token B (amount of token A or B can be zero)
- _Withdraw_:
- Step:
- _withdrawal_lp_amount_
- _minimum_asset_a_ (TODO: Link formula section here)
- _minimum_asset_b_ (TODO: Link formula section here)
- _minimum_asset_a_([Formula](./formula.md#4-withdraw))
- _minimum_asset_b_([Formula](./formula.md#4-withdraw))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
Expand All @@ -597,38 +597,38 @@ Transaction structure:
- Step:
- _a_to_b_direction_
- _withdrawal_lp_amount_
- _minimum_receive_ (TODO: Link formula section here)
- _minimum_receive_([Formula](./formula.md#5-zap-out))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
- LP Asset
- _WithdrawImbalance_:
- Step:
- _withdrawal_lp_amount_
- _ratio_asset_a_ (TODO: Link formula section here)
- _ratio_asset_b_ (TODO: Link formula section here)
- _minimum_asset_a_ (TODO: Link formula section here)
- _ratio_asset_a_
- _ratio_asset_b_
- _minimum_asset_a_([Formula](./formula.md#6-withdraw-imbalance))
- _killable_
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
- LP Asset
- _PartialSwap_:
- Step:
- _a_to_b_direction_ (TODO: Link formula section here)
- _a_to_b_direction_([Formula](./formula.md#7-partial-swap))
- _total_swap_amount_
- _io_ratio_numerator_ (TODO: Link formula section here)
- _io_ratio_denominator_ (TODO: Link formula section here)
- _hops_ (TODO: Link formula section here)
- _minimum_swap_amount_required_ (TODO: Link formula section here)
- _io_ratio_numerator_([Formula](./formula.md#7-partial-swap))
- _io_ratio_denominator_([Formula](./formula.md#7-partial-swap))
- _hops_([Formula](./formula.md#7-partial-swap))
- _minimum_swap_amount_required_([Formula](./formula.md#7-partial-swap))
- _max_batcher_fee_each_time_
- Value:
- _batcher_fee_ * _hops_ + additional ADA to cover output (if need)
- Token is swapping and received Token of previous swap (if you are order's creator, you no need to care about _received Token_)
- _SwapMultiRouting_:
- Step:
- _routings_ (TODO: Link formula section here)
- _routings_([Formula](./formula.md#1-swap-exact-in))
- _swap_amount_
- _minimum_receive_ (TODO: Link formula section here)
- _minimum_receive_([Formula](./formula.md#1-swap-exact-in))
- Value:
- _batcher_fee_ + additional ADA to cover output (if need)
- Token is swapping
Expand Down
10 changes: 8 additions & 2 deletions amm-v2-docs/formula.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Before we go to details, here are some definitions:
## AMM has 2 main formula:
### 1. When the price changes (Swap)
When a Token is swapped, Swap Fee is deducted from the input amount ($\Delta x$)

$$ k = x_{0} * y_{0}$$

$$ k = (x_{0} + (1 - f) * \Delta x) * (y_{0} - \Delta y)$$

### 2. When the price is not changes (Add or Remove Liquidity)
Expand All @@ -34,7 +36,7 @@ $$\Delta x = \frac{x_{0} * \Delta y * f_{d}}{(f_{d} - f_{n}) * (y_{0} - \Delta y
### 3. Deposit
On the V2, we allow users to deposit with any amount A & B, so there are two cases can happen:
- $\frac{\Delta x}{x_{0}} = \frac{\Delta y}{y_{0}} \Rightarrow \Delta L = \frac{\Delta x}{x_{0}} * L$
- $\frac{\Delta x}{x_{0}} \# \frac{\Delta y}{y_{0}}$:
- $\frac{\Delta x}{x_{0}} \frac{\Delta y}{y_{0}}$:
- $\frac{\Delta x}{x_{0}} > \frac{\Delta y}{y_{0}}$: We need to Swap a part of X to Y
- $\frac{\Delta x}{x_{0}} < \frac{\Delta y}{y_{0}}$: We need to Swap a part of Y to X

Expand Down Expand Up @@ -94,8 +96,10 @@ Combination of formula (1) and (2), we have:
$$a * swap_{x} ^ 2 + b * swap_{x} + c = 0$$
where
$$a = (f_{d} - f_{n}) * B$$

$$b = A*(f_{d} - f_{n})*(y_{0}+\Delta y) + B *(f_{d} * x_{0} - (f_{d} - f_{n})*\Delta x)$$
$$ c =f_{d} * x_{0} *(A * \Delta y - B * \Delta x) $$

$$c =f_{d} * x_{0} *(A * \Delta y - B * \Delta x)$$

### 7. Partial Swap
Allow users swap only if price is exactly matched.
Expand All @@ -104,6 +108,8 @@ In case users want to swap with price $A/B$
We have 2 formulas:
$$\frac{\Delta x}{\Delta y} = \frac{A}{B} (1)$$
$$ \Delta y = \frac{(f_{d} - f_{n}) * \Delta x * y_{0}}{x_{0} * f_{d} + (f_{d} - f_{n}) * \Delta x} (2)$$

We can calculate $\Delta x$:

$$\Delta x = \frac{A * (f_{d} - f_{n}) * y_{0} - B * f_{d} * x_{0}}{(f_{d} - f_{n}) * B}$$
where $\Delta x$ is the maximum amount can be swapped to adapt $A/B$ ratio
4 changes: 2 additions & 2 deletions lib/amm_dex_v2/math.ak
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ pub fn calculate_zap_out(

// Base formula:
// ( amount_in - swap_amount_in) / (amount_out + receive_amount_out) = expect_io_ratio_numerator / expect_io_ratio_denominator (1)
// receive_amount_out = fee_diff * swap_amount_in * reserve_out / (fee_denominator) * reserve_in + fee_diff * swap_amount_in (2)
// receive_amount_out = fee_diff * swap_amount_in * reserve_out / (fee_denominator * reserve_in + fee_diff * swap_amount_in) (2)
// (1) & (2) =>>>>>
// a * swap_amount_in ^ 2 + b * swap_amount_in + c = 0
// Where:
// - a = fee_diff * expect_io_ratio_denominator
// - b = expect_io_ratio_numerator * fee_diff ( reserve_out + amount_out ) + expect_io_ratio_denominator * (fee_denominator * reserve_in - fee_diff * amount_in)
// - c = fee_denominator * reserve_in* (expect_io_ratio_numerator * amount_out - expect_io_ratio_denominator * amount_in)
// - c = fee_denominator * reserve_in * (expect_io_ratio_numerator * amount_out - expect_io_ratio_denominator * amount_in)
fn calculate_withdraw_swap_amount(
amount_in: Int,
amount_out: Int,
Expand Down

0 comments on commit 7437012

Please sign in to comment.