Skip to content

Commit

Permalink
chore: swapping spec (#3127)
Browse files Browse the repository at this point in the history
* swapping spec mvp

* code review

* code review
  • Loading branch information
czarcas7ic authored Oct 24, 2022
1 parent 13e1198 commit e38be99
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/architecture/concentrated-liquidity-adr.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,29 @@ func CreatePosition(

> As a trader, I want to be able to swap over a concentrated liquidity pool so that my trades incur lower slippage
TODO
Unlike balancer pools where liquidity is spread out over an infinite range, concentrated liquidity pools allow for deeper liquidity at the current price, which in turn allows trades the incur less slippage.

Despite this improvement, the liquidity at the current price is still finite and large single trades, times of high volume, and trades against volatile assets are eventually bound to incur some slippage.

In order to determine the depth of liquidity and subsequent amountIn/amountOut values for a given pool, we track the swap's state across multiple swap "steps". You can think of each of these steps as the current price following the original xy=k curve, with the far left bound being the next initialized tick below the current price and the far right bound being the next initialized tick above the current price. It is also important to note that we always view prices of asset1 in terms of asset0, and selling asset1 for asset0 would in turn increase it's spot price. The reciprocal is also true, where if we sell asset0 for asset1 we would decrease the pool's spot price.

When a user swaps asset0 for asset1 (can also be seen as "selling" asset0), we move left along the curve until asset1 reserves in this tick are depleted. If the tick of the current price has enough liquidity to fulfil the order without stepping to the next tick, the order is complete. If we deplete all of asset1 in the current tick, this then marks the end of the first swap "step". Since all liquidity in this tick has been depleted, we search for the next closest tick to the left of the current tick that has liquidity. Once we reach this tick, we determine how much more of asset1 is needed to complete the swap. This process continues until either the entire order is fulfilled or all liquidity is drained from the pool.

The same logic is true for swapping asset1, which is analogous to buying asset0, however instead of moving left along the set of curves, we instead search for liquidity to the right.

The core logic is run by the computeSwapStep function, where we calculate the amountIn, amountOut, and the next sqrtPrice given current price, price target, tick liquidity, and amount available to swap:

```go
func computeSwapStep(
sqrtPriceCurrent sdk.Dec,
sqrtPriceTarget sdk.Dec,
liquidity sdk.Dec,
amountRemaining sdk.Dec,
lte bool) (sqrtPriceNext, amountIn, amountOut sdk.Dec)
{
...
}
```

#### Range Orders

Expand Down

0 comments on commit e38be99

Please sign in to comment.