🛠 AlphaX Arbitrageurs Docs 💰

AlphaX Contract Interfaces: https://hackmd.io/RfH7bNirRMCP8y9rIN066g

This doc will go into details of how to effectively arbitrage on AlphaX, including some PoCs.

AlphaX Recap

To recap, AlphaX has 2 main types of tokens: Long Strike Tokens & Short Strike Tokens.

Long Strike Tokens come in the form ETH-X, e.g., ETH-3000, ETH-2500.
Short Strike Tokens come in the form of Y-ETH, e.g., 6450-ETH, 7100-ETH.

The price of ETH-X should be pegged to (ETH price - X).
The price of Y-ETH should be pegged to (Y - ETH price).
This is regarded as the Oracle Price \(P_O\).

However, the actual ETH-X & Y-ETH token price might be trading on DEXes at another price. This price is the Market Price \(P_M\).


There are multiple ways to arbitrage the gap between \(P_M\) and \(P_O\). Here, we present one way.

Two main arbitrage cases:

Case 1. Market Price too low (\(P_M < P_O\))

TL;DR Buy XToken from the market.

How much XTokens to buy depends on trading pool reserves, market price, and oracle price.

Here are some calculations:

Let
\(R_b\) = trading pool's base reserve (obtained from pair.getReserves())
\(R_q\) = trading pool's quote reserve (obtained from pair.getReserves())
\(f\) = trading pool's swap fee (0.3% = 0.003 in this case).

Let \(x\) be the optimal amount of XTokens to buy to bring \(P_M\) up to \(P_O\). We want to solve for \(x\).

After the swap, the new reserves become:
\begin{align*} R'_b &= R_b + x \\ R'_q &= \frac{R_b \cdot R_q}{R_b + (1-f) \cdot x} \end{align*}

The new market price becomes:
\begin{align*} P_O = P'_M = \frac{R'_q}{R'_b} = \frac{R_b \cdot R_q}{(R_b + (1-f) \cdot x)\cdot(R_b+x)} \end{align*}

Rearranging the equation gives a solvable quadratic equation:

\begin{align*} P_O \cdot (1-f) \cdot x^2 + (P_O\cdot(2-f)\cdot R_b)\cdot x + P_O\cdot R_b^2 - R_b \cdot R_q = 0 \end{align*}

Calculation Pseudocode:

def solve_buy_amt(pair_addr, oracle_price):
    pair = interface.IUniswapV2Pair(pair_addr)
    f = 0.003 # 0.3% swap fee
    p = oracle_price 

    r_0, r_1, _ = pair.getReserves()
    r_b, r_q = (r_0, r_1) if pair.token1() == USDC else (r_1, r_0) # check ordering

    # quadratic equation: ax^2 + bx + c = 0
    a = p * (1-f)
    b = p * (2-f) * r_b
    c = p * r_b * r_b - r_b * r_q

    # solve x
    x = int((-b + sqrt(b*b - 4*a*c))/(2*a))
    
    return x

NOTE: You can swap directly in the trading pool using USDC.e.

Case 2. Market Price too high (\(P_M > P_O\))

TL;DR Sell XToken to the market. (Mint XToken pair if necessary).

How much XTokens to sell depends on trading pool reserves, market price, and oracle price.

Here are some calculations:

Let
\(R_b\) = trading pool's base reserve (obtained from pair.getReserves())
\(R_q\) = trading pool's quote reserve (obtained from pair.getReserves())
\(f\) = trading pool's swap fee (0.3% = 0.003 in this case).

Let \(x\) be the optimal amount of XTokens to sell to bring \(P_M\) down to \(P_O\). We want to solve for \(x\).

After the swap, the new reserves become:
\begin{align*} R'_b &= R_b - x \\ R'_q &= \frac{(1-f)\cdot R_b \cdot R_q + f\cdot R_q \cdot x}{(1-f)\cdot (R_b - x)} \end{align*}

The new market price becomes:
\begin{align*} P_O = P'_M = \frac{(1-f)\cdot R_b \cdot R_q + f\cdot R_q \cdot x}{(1-f)\cdot (R_b - x) \cdot(R_b -x)} \end{align*}

Rearranging the equation gives a solvable quadratic equation:

\begin{align*} P_O \cdot (1-f) \cdot x^2 - (2\cdot P_O \cdot(1-f)\cdot R_b + f \cdot R_q) \cdot x + (P_O \cdot (1-f)\cdot R_b^2 - (1-f)\cdot R_b \cdot R_q) = 0 \end{align*}

Calculation Pseudocode:

def solve_sell_amt(pair_addr, oracle_price):
    pair = interface.IUniswapV2Pair(pair_addr)
    f = 0.003 # 0.3% swap fee
    p = oracle_price 

    r_0, r_1, _ = pair.getReserves()
    r_b, r_q = (r_0, r_1) if pair.token1() == USDC else (r_1, r_0) # check ordering

    # quadratic equation: ax^2 + bx + c = 0
    a = p * (1-f)
    b = -(2 * p * (1-f) * r_b + f * r_q)
    c = p * (1-f) * r_b * r_b - (1-f) * r_b * r_q

    # solve x
    x = int((-b + sqrt(b*b - 4*a*c))/(2*a))
    
    return x

NOTE: You can swap directly in the trading pool using XTokens. If you do not have sufficient XTokens, you can mint XTokens using the interfaces found here: https://hackmd.io/RfH7bNirRMCP8y9rIN066g

Select a repo