paco0x / amm-arbitrageur

An arbitrage bot between Uniswap AMMs
Do What The F*ck You Want To Public License
1.93k stars 826 forks source link

Question on formula #22

Closed ygorgeurts closed 3 years ago

ygorgeurts commented 3 years ago

Hi,

Can you elaborate on where the first set of formula's come from: \delta a_1 = \delta b_1 * a1 / (b_1 - \delta b_1) ?

Thanks!

Ygor

paco0x commented 3 years ago

This comes from the invariant formula of uniswap V2 AMM, which is a*b = k.

So when you calculate the output amount of a swap, that is (a + \delta a) (b - \delta b) = a b. You can calculate the delta a by given a delta b or do a reverse calculation.

For more, you can see the Uniswap source code: https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol#L42-L59

The formula \delta a_1 = \delta b_1 * a1 / (b_1 - \delta b_1) is in the getAmountIn function. And for simplicity, we do not consider the fee calculation in this project.(In uniswap, there's a swap fee of 0.3%).