Find equation which gives you the profit for multiple trades

29 Views Asked by At

Automated market makers are protocols where you can exchange crypto tokens, they are based on pools which contain 2 tokens each pool, here is the definitions on how these pools work from Uniswap V2:

Pairs act as automated market makers, standing ready to accept one token for the other as long as the “constant product” formula is preserved. This formula, most simply expressed as x * y = k, states that trades must not change the product (k) of a pair’s reserve balances (x and y). Because k remains unchanged from the reference frame of a trade, it is often referred to as the invariant. This formula has the desirable property that larger trades (relative to reserves) execute at exponentially worse rates than smaller ones. https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/how-uniswap-works

Given two pools with the same tokens you can trade/balance these two pools if they do not have the same ratio of reserves and the following formula calculates how much profit you get:

$$ profit = \frac{a_2·x}{b_2+x}-\frac{a_1·x}{b_1-x} $$

where:

Pair 0 Pair 1
Token 0 reserves a1 a2
Token 1 reserves b1 b2

The first fraction of the previous formula ($\frac{a_2·x}{b_2+x}$), given an input of tokens (x) it will calculate how much you will get of the other tokens given the reserves of the pool, and the second fraction ($\frac{a_1·x}{b_1-x}$), given an output of tokens (x) it will give you how much tokens you need to input to get this output (x) tokens. This formula basically calculates how much profit you get for a given balancing/trading of two pools, it subtracts how much tokens you input to how much tokens you output so you get what is left.

Now my question is how you get the profit formula for multiple(+2) pools, for a triangular arbitrage (3 pools) I do not think would be the same formula because you are not taking into account the pool in he middle..

Pair 0 Pair 1 Pair 2
Token 0 reserves a1 - a3
Token 1 reserves b1 b2 -
Token 2 reserves - c2 c3

Given the above Pairs and tokens how do I get the profit formula if we want to trade:

"token[Pair] ->"

Token0[Pair0] -> Token1[Pair1] -> Token2[Pair2] -> Token 0

I am a bit stuck because it should be something like this but there is something missing in the middle as I think you need to take the pool in the middle into account (how much you input and how much you get from the middle pool)

$$ profit = \frac{a_3·x}{c_3+x} ?...? \frac{a_1·x}{b_1-x} $$