-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: improve AMM and Leverage LP docs (#637)
* docs: improve AMM spec * docs: add additional doc for leverage lp --------- Co-authored-by: Amit Yadav <[email protected]>
- Loading branch information
1 parent
34d1069
commit a294b42
Showing
10 changed files
with
256 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!-- | ||
order: 6 | ||
--> | ||
|
||
# Slippage Model | ||
|
||
In the Oracle Pool, to prevent bots from exploiting slippage by splitting trades into smaller pieces, we introduce the **stacked slippage model**. This model accumulates slippage within a block, making it less advantageous for bots to break down trades. | ||
|
||
## Dynamic Weight Model | ||
|
||
The Oracle Pool employs a dynamic weight model. The weight is updated at the start of the swap transactions execution and remains constant until the end of the block. As a result, slippage is recalculated based on the liquidity changes within the block. Multiple trades within a single block will cumulatively increase slippage, particularly for swaps in the same direction, leading to the "stacked slippage" model. | ||
|
||
## Slippage Calculation | ||
|
||
The slippage amount is calculated as follows: | ||
|
||
$ | ||
SlippageAmount = Max(0, OracleOutAmount - BalancerOutAmount) | ||
$ | ||
|
||
When $BalancerOutAmount$ exceeds $OracleOutAmount$, slippage is considered zero, not negative. | ||
|
||
For trades with a negative slippage tolerance due to numerous opposite side trades, the value is set to 0%, aligning with the oracle price. | ||
|
||
## Dynamic Weight Adjustment Criteria | ||
|
||
To illustrate the dynamic weight adjustment: | ||
|
||
- Suppose the pool contains 1000 ATOM and 10000 USDC, with the oracle providing a price of $11 for 1 ATOM. | ||
- The pool's ratio is updated to 11:10 based on the USD value of each asset. | ||
|
||
## Bot Prevention Mechanism | ||
|
||
The stacked slippage model discourages bots by increasing slippage with consecutive same-direction swaps within a block: | ||
|
||
- Initial swaps have minimal slippage based on the oracle price. | ||
- Subsequent swaps face increased slippage, preventing lower overall slippage even if the trade is split. | ||
|
||
For example, a bot attempting to swap 1000 ATOM for USDC with 1% slippage: | ||
|
||
- Splitting the trade into 1000 swaps of 1 ATOM each would result in increasing slippage for each swap, cumulatively ensuring the overall slippage does not drop below 1%. | ||
|
||
This mechanism ensures that splitting trades does not offer a better deal than a single trade within the block. | ||
|
||
## Role of the Oracle | ||
|
||
The oracle provides price and external liquidity data without additional information. This ensures price accuracy and consistency across trades. | ||
|
||
## Dynamic Weight Model and Slippage | ||
|
||
Oracle-based pools use the balancer slippage model to prevent arbitrage opportunities with other DEXs. The slippage is calculated from the balancer model, considering the weight and liquidity of the pool. When the weight is updated based on the oracle price, the pool's weight aligns with the oracle price, ensuring price consistency. | ||
|
||
## Computational Considerations | ||
|
||
Weights are updated when the first swap transaction is executed within a block, with all swap transactions executed at the end of the block. This does not directly impact users' swap gas fees since the transaction is only scheduled at this stage. The time complexity for executing swap transactions at the end of the block is O(n), where n is the number of swaps within the block. | ||
|
||
## Handling Volatile Markets | ||
|
||
In volatile markets, arbitrage opportunities arise due to price differences across platforms. The oracle provides the average price from these platforms and applies slippage on top, minimizing arbitrage opportunities with other platforms during volatile conditions. | ||
|
||
## User Information on Slippage | ||
|
||
Slippage is determined by liquidity, external liquidity, and oracle price. Users can estimate slippage using the balancer formula and set a minimum acceptable out amount, accounting for stacked slippage within the block. Similar to Uniswap, users can estimate the output amount, though it may vary due to other trades. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!-- | ||
order: 7 | ||
--> | ||
|
||
# Swap Transactions Reordering | ||
|
||
This documentation describes the mechanism used to reorder swap transactions to minimize slippage and prevent MEV (Miner Extractable Value) attacks. | ||
|
||
## Overview | ||
|
||
To achieve the lowest possible slippage within a block, this module reorders swap requests into a specific sequence. This approach mitigates the risk of MEV attacks, which exploit transaction ordering. Instead of executing transactions as they are received from the validator, the requests are stored in a transient store during the endblocker and then executed in an optimal order. | ||
|
||
## Key Considerations | ||
|
||
When designing the reordering algorithm, the following factors are taken into account: | ||
|
||
- Transactions that swap an exact amount in | ||
- Transactions that swap for an exact amount out | ||
- Multihop transactions | ||
- Pools involving two or more tokens | ||
- Algorithm complexity | ||
|
||
## Algorithm | ||
|
||
1. **Grouping Transactions**: Transactions are grouped by pool and token pairs (In -> Out). | ||
- For multihop transactions, only the first pool and the first in/out tokens are considered to reduce complexity. | ||
2. **Selection and Execution**: | ||
|
||
- Select a random swap request. | ||
- Attempt to execute it in a cache context and check the resulting stacked slippage. | ||
- Check for an existing request in the opposite direction (same pool ID with opposite in/out tokens). | ||
- If such an opposite request exists, attempt to execute it in a cache context and check the resulting stacked slippage. | ||
- Apply the swap request that results in lower stacked slippage. | ||
- If either swap fails, discard the swap request without applying any changes. | ||
|
||
3. **Iteration**: | ||
- Repeat the selection and execution process until all swap requests are processed. | ||
|
||
### Algorithm Complexity | ||
|
||
The complexity of the algorithm is `O(n)`, where `n` is the number of swap requests within the block. | ||
|
||
### Algorithm Accuracy | ||
|
||
This algorithm provides a semi-optimized solution, balancing accuracy and complexity. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.