CarmineOptions / derisk-research

MIT License
4 stars 14 forks source link

Integrate MySwap #67

Open lukaspetrasek opened 3 months ago

lukaspetrasek commented 3 months ago

Compute available liquidity at MySwap CLMM for any given token at any given price level.

Let's choose, for example, the STRK-USDC pair and assume the current price is 2 USDC per 1 STRK. We're interested in the amounts of USDC that we'll be able to swap for STRK at MySwap while not pushing the price by more than 5% in a scenario where the STRK price falls to, e.g., 1.9 USDC, 1.8 USDC, ... 1 USDC, etc.

MarekHauzr commented 3 months ago

Essentially we are asking for an "ordebook projection" of the CL AMM (MySwap).

Part of that is that on some specified price levels we want to know how much liquidity is between this price level and 5% below (or above).

khayss commented 2 months ago

Can I take up this issue?

PhantomOz commented 2 months ago

can i work on this?

lukaspetrasek commented 2 months ago

Hi, can you guys please tell me something about you, what skills/experience do you have and how do you plan to tackle this issue? This task is not simple, so I have to learn more information before I assign anyone 🙏🏼

Adesooye commented 2 months ago

To compute the available liquidity at MySwap for any given token at any given price level, we need to understand the mechanics of the Automated Market Maker (AMM) used by MySwap. One common AMM model is the constant product formula, defined as 𝑥 ⋅ 𝑦

𝑘 x⋅y=k, where 𝑥 x and 𝑦 y are the amounts of two tokens in the pool and 𝑘 k is a constant.

For the STRK-USDC pair with a current price of 2 USDC per 1 STRK, let's assume the pool has 𝑥 x STRK and 𝑦 y USDC such that 𝑥 ⋅ 𝑦

𝑘 x⋅y=k.

Step-by-Step Calculation: Current Pool Balance:

Current price: 2 USDC per 1 STRK 𝑥 x STRK in the pool 𝑦 y USDC in the pool Thus, 𝑦 / 𝑥

2 y/x=2 Therefore, 𝑦

2 𝑥 y=2x Constant Product:

𝑘

𝑥 ⋅ 𝑦 k=x⋅y Substituting 𝑦 y gives 𝑘

𝑥 ⋅ 2 𝑥

2 𝑥 2 k=x⋅2x=2x 2

Liquidity at Various Price Levels: To find the amount of USDC that can be swapped for STRK without pushing the price by more than 5%, we need to consider the price levels 1.9 1.9, 1.8 1.8, ..., 1 1 USDC per STRK.

For a new price 𝑝 ′ p ′ :

New price level 𝑝 ′ p ′ : 𝑝 ′

𝑦 ′ / 𝑥 ′ p ′ =y ′ /x ′

Given 𝑝 ′

1.9 p ′ =1.9, 1.8 1.8, ..., 1 1 USDC per STRK 𝑦 ′

𝑝 ′ ⋅ 𝑥 ′ y ′ =p ′ ⋅x ′

We need to maintain the constant product:

𝑥 ′ ⋅ 𝑦 ′

𝑘 x ′ ⋅y ′ =k Let's compute this step-by-step for each new price level.

Example Calculations: Current Situation:

Let's assume an initial 𝑥 x = 1000 STRK. Thus, 𝑦

2 × 1000

2000 y=2×1000=2000 USDC. 𝑘

1000 × 2000

2 , 000 , 000 k=1000×2000=2,000,000. New Price Level: 1.9 USDC per STRK

New 𝑥 ′ ⋅ 𝑦 ′

2 , 000 , 000 x ′ ⋅y ′ =2,000,000 New price 𝑝 ′

1.9 p ′ =1.9 𝑦 ′

1.9 ⋅ 𝑥 ′ y ′ =1.9⋅x ′

Substitute into the constant product formula: 𝑥 ′ ⋅ ( 1.9 ⋅ 𝑥 ′ )

2 , 000 , 000 x ′ ⋅(1.9⋅x ′ )=2,000,000 1.9 𝑥 ′ 2

2 , 000 , 000 1.9x ′2 =2,000,000 𝑥 ′ 2

2 , 000 , 000 1.9 x ′2 = 1.9 2,000,000 ​

𝑥 ′

2 , 000 , 000 1.9 x ′ = 1.9 2,000,000 ​

𝑥 ′ ≈ 1024.7 x ′ ≈1024.7 𝑦 ′

1.9 × 1024.7 ≈ 1946.93 y ′ =1.9×1024.7≈1946.93 Available Liquidity at 1.9 USDC per STRK:

Initial USDC: 2000 2000 Remaining USDC: 1946.93 1946.93 Swappable USDC: 2000 − 1946.93 ≈ 53.07 2000−1946.93≈53.07 We can repeat this process for other price levels 1.8 1.8, 1.7 1.7, etc.

Let's calculate this iteratively for a series of price levels using Python.

Python Calculation: python Copy code import math

Function to compute available liquidity at a new price level

def available_liquidity(initial_x, initial_price, new_prices): initial_y = initial_price initial_x k = initial_x initial_y

liquidity_info = []

for p in new_prices:
    x_prime = math.sqrt(k / p)
    y_prime = p * x_prime
    available_usdc = initial_y - y_prime
    liquidity_info.append((p, available_usdc))

return liquidity_info

Initial parameters

initial_x = 1000 # initial amount of STRK initial_price = 2 # current price of 1 STRK in USDC new_prices = [1.9, 1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1, 1.0] # new price levels

Compute available liquidity

liquidity = available_liquidity(initial_x, initial_price, new_prices) liquidity Let's run the Python code to get the exact values.

Here are the available amounts of USDC that can be swapped for STRK at various price levels without pushing the price by more than 5%:

1.9 USDC per STRK: 50.64 USDC 1.8 USDC per STRK: 102.63 USDC 1.7 USDC per STRK: 156.09 USDC 1.6 USDC per STRK: 211.15 USDC 1.5 USDC per STRK: 267.95 USDC 1.4 USDC per STRK: 326.68 USDC 1.3 USDC per STRK: 387.55 USDC 1.2 USDC per STRK: 450.81 USDC 1.1 USDC per STRK: 516.76 USDC 1.0 USDC per STRK: 585.79 USDC These amounts represent the liquidity available in the pool at each specified price level. As the price of STRK decreases, the amount of USDC that can be swapped for STRK increases. ​​

lukaspetrasek commented 2 months ago

Hi @Adesooye , can you pls write a more concise message? Also, mind that this task is to compute liquidity on the MySwap CLMM, not their V2 AMM.

estherbreath commented 2 months ago

@lukaspetrasek, I would like to work on this issue. I am a smart contract developer in Solidity and Cairo languages

Here's my approach; I will compute the available liquidity at MySwap CLMM for the STRK-USDC pair at various price levels without significantly affecting the price

The first thing is to obtain pool data, by getting the current state of the STRK-USDC liquidity pool from MySwap, including the total amounts of STRK and USDC.

Estimate the price impact of swapping USDC for STRK at different price levels. This involves determining how much USDC can be swapped without causing the price to change by more than 5%.

For each price level (e.g., 1.9 USDC, 1.8 USDC,..., 1 USDC), calculate the maximum amount of USDC that can be swapped without exceeding the 5% price impact threshold.

Ensure to document my findings for reference sake

lukaspetrasek commented 2 months ago

Okay, assigning you @estherbreath 👍🏼

@estherbreath Let me know if everything is clear. If you have any questions, please ask here. What is you TG handler please? 🙏🏼

Consider joining our TG group. See also our contributor guidelines.

estherbreath commented 2 months ago

@lukaspetrasek My TG handle: @Bosslady248

lukaspetrasek commented 1 month ago

Hi @estherbreath , any progress on this?

estherbreath commented 1 month ago

Almost done

faurdent commented 1 month ago

Hi, what is the status of this task, I would like to take this one as far as I have already implemented Haiko and also AMM for uniswap V2 and have experience to manage this task. My approach is next:

  1. Fetch pool data
  2. Calculate price per tick for liquidity amount in pool
  3. Transform this data to OrderBook structure
  4. Save it to db
lukaspetrasek commented 1 month ago

Hi @estherbreath , unassigning you because you didn't submit any PR yet and putting @faurdent instead. Good luck @faurdent !