0

For those familiarized with the code in UniswapV2Pair.sol, I don't understand the following line:

uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0

Observing previous lines, I understand that the amount of the pool "_reserve0" is used to extract the quantity "amount0Out", so the final balance of token0 in the pair pool is,

balance0 = _reserve0 - amount0Out

Therefore, I don't understand why balance0 is going to be greater than _reserve0 - amount0Out.

I attach here previous lines of the code,

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
    require(amount0Out > 0 || amount1Out > 0, 'UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT');
    (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
    require(amount0Out < _reserve0 && amount1Out < _reserve1, 'UniswapV2: INSUFFICIENT_LIQUIDITY');

    uint balance0;
    uint balance1;
    { // scope for _token{0,1}, avoids stack too deep errors
    address _token0 = token0;
    address _token1 = token1;
    require(to != _token0 && to != _token1, 'UniswapV2: INVALID_TO');
    if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
    if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
    if (data.length > 0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
    balance0 = IERC20(_token0).balanceOf(address(this));
    balance1 = IERC20(_token1).balanceOf(address(this));
    }
    uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
...
5
  • I don't understand why balance0 is going to be greater than because you can't sell something you don't have
    – Nulik
    Jul 25, 2021 at 20:12
  • exactly like nulik said, but you can also check this post and accepted answer. ethereum.stackexchange.com/questions/102355/… Jul 26, 2021 at 9:34
  • Thank you both for the answers, they help. But probably I am missing an important concept, because I see the following flow of the contract: 1. get the balances of the pool before the swap (reserves) 2. transfer out amountOut from the pool (so the new balance is = reserve-out) 3. query the pool balance after that transferout via IERC20 interface, which is going to be: balance = reserve - amountOut, so I don't know why to query 4. if you calculate from here the inflow amountIn = balance - (reserve-amountOut) = (reserve - amountOut) - (reserve - amountOut) = 0 So.. what am I missing?
    – bitcazador
    Jul 31, 2021 at 17:37
  • maybe the magic is that this function "swap" is called AFTER the user transfer-in the amountIn. So, the user transfer-in to the pool the "amountIn" she wants to sell, and then the "swap" function is invoked, so now "balance" and "reserve" are not the same. the balance is reserve + amountIn and then after the transfer-out amountOut, the balance is = reserve + amountIn - amountOut [1], so now we are in condition to calculate amountIn directly finding from [1], that is: amountIn = balance-reserve+amountOut = balance - (reserve - amountOut)
    – bitcazador
    Jul 31, 2021 at 18:00
  • ok, this fact is confirmed in testing: UniswapV2Pair.spec.ts
    – bitcazador
    Jul 31, 2021 at 18:19

1 Answer 1

1

After finding the solution, that is confirmed in test file UniswapV2Pair.spec.ts, I self-answer the question just in case someone is also interested on it. The magic seems to be that this function swap is called AFTER the user transfer-in the amountIn. So, the user transfer-in to the pool the amountIn she wants to sell, and then the swap function is invoked, so now balance and reserve are not the same (otherwise the final calculation of amountIn would be zero, as explained in previous comments). The balance after the transfer-in is reserve + amountIn (and the fees), and then after the transfer-out amountOut, the balance is = reserve + amountIn - amountOut [1] (and the fees), so now we are in condition to calculate amountIn directly finding from [1], that is: amountIn = balance-reserve+amountOut = balance - (reserve - amountOut)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.