0

Consider the following swap event as listed here:

amount0In :4003282810462813624841310246
amount1In :947647544007674247
amount0Out :729688943313804828976594873545
amount1Out :0

To calculate the swap price (token1/token0)( without accounting for potential transfer tax) you would just do amount1/amount0, and depending on buy or sell this would translate to amount1Out/amount0In or amount1In/amount0Out. Please let me know if I have this part wrong.

However, the above event has both amount0In and amount1In as non-zero. What does this even mean (i.e. what happened here) and how to calculate the swap price in this situation?

1 Answer 1

1

Sometimes Uniswap pool reserves as tracked by the smart contract goes out of sync with the token balances in the pool's contract - for example, when someone donates a token in the pool and fails call sync() afterwards.

The result is these weird swap events, where one or both input token amounts are more than what was provided by the swapper. In the general case this means that swap event is not a 100% reliable way how to get the price in the pool.

If the transaction has come through the Uniswap router or some other sensible contract, then its pretty safe to assume that only one token was actually intended to be used for the input. Which one - that depends on the amount0Out or amount1Out not being zero.

In your example amount1Out is zero, so token 1 was probably the input token. To compute the price, you must further subtract the 0.3% fee from the input amount, like this:

amount1InWithFee = 947647544007674247 * 997 / 1000
price = amount1InWithFee / amount0Out

Now the real horror is transactions where both outputs are zero... Luckily with the newer routers they are much less frequent than in the early years of Uniswap.

1
  • If I were to use swap price to calculate PnL for the person who interacted with the pool, I would not want to subtract the fees correct? After all, said person did actually pay for the fees as well
    – Geert-Jan
    Commented Jun 30, 2023 at 21:04

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.