youwenti
youwenti

Reputation: 99

How to understand uniswap v3 swap event?

https://docs.uniswap.org/protocol/reference/core/interfaces/pool/IUniswapV3PoolEvents

In the uniswap v3 doc, there is some description.

  event Swap(
    address sender,
    address recipient,
    int256 amount0,
    int256 amount1,
    uint160 sqrtPriceX96,
    uint128 liquidity,
    int24 tick
  )

enter image description here

But I can hardly understand what amount0 and amount1 are. Take this transaction for example, Swap 6,767.400346701675410697 LOOKS For 2.681 Ether On Uniswap V3. https://etherscan.io/tx/0xeb4a0eeecd6e19a1204b37339b9b7236b5219419fc7174e559edf27afc37937c#eventlog

The last is the swap event. enter image description here

Where is the output eth amount?

Thanks!

Upvotes: 2

Views: 4882

Answers (3)

TechGeorgii
TechGeorgii

Reputation: 417

The transaction you are talking about is calling swap method on Uniswap V3 WETH/LOOKS pool:

  • token0 is WETH (0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)
  • token1 is LOOKS (0xf4d2888d29D722226FafA5d9B24F9164c092421E)

amount0 and amount1 from Swap event, if you switch to HEX format and cast to int256 datatype, are -2681000000000000000 and 6767400346701675410697 respectively.

LOOKS were swapped to ETH, so it means the pool acquired 6767400346701675410697 LOOKS (someone put them into the pool) and lost 2681000000000000000 ETH (hence the minus sign). These numbers are fixed-point numbers, so decimals property of each token must be taken into account (18 for both tokens here).

Upvotes: 0

Fiduro
Fiduro

Reputation: 108

This might be a little bit of a late answer, but I just stumbled across the same problem and thought it might help some others.

Unfortunately, Etherscan is bad at displaying numbers and doesn't currently know about the concept of negative numbers. Here's how to decode the given log:

On e.g. beaconcha.in, get the raw data of the emitted log:

0xffffffffffffffffffffffffffffffffffffffffffffffffdacb2cb45d2d800000000000000000000000000000000000000000000000016edc8bc819f8b8b50900000000000000000000000000000000000000324b97f19a3936459e285115b700000000000000000000000000000000000000000000053f31c5c8cac5d23340000000000000000000000000000000000000000000000000000000000001321a

As you already know the event's arguments, these can be used to decode these data. There's one thing missing from the documentation: The first two arguments (sender&recipient) are indexed. Indexed arguments are not part of the log data. You can find this in the source code itself:

    event Swap(
        address indexed sender,
        address indexed recipient,
        int256 amount0,
        int256 amount1,
        uint160 sqrtPriceX96,
        uint128 liquidity,
        int24 tick
    );

Or alternatively as part of the pool's contract ABI on Etherscan.

Given the non-indexed argument types int256,int256,uint160,uint128,int24 you can decode these online using an online ABI decoder:

Example of how to input the data

This yields the decoded arguments in their given order:

-2681000000000000000,6767400346701675410697,3984803190183823086827191997879,24777563784443426124608,78362

To further understand them, you have to know what the token0 and token1 of the pool are. This has to be checked carefully as they're just sorted alphabetically for UniswapV3. For the given pool these are:

token0 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 = WETH (18 decimals)
token1 = 0xf4d2888d29D722226FafA5d9B24F9164c092421E = LOOKS (18 decimals)

Applying the decimals yields the following amounts:

amount0 = -2.681000000000000000 WETH
amount1 = 6767.400346701675410697 LOOKS

As defined in the documentation, both are the "delta of the balance of the pool", worded differently this can be read as:

amount0: how many of token0 the pool received during this swap
amount1: how many of token1 the pool received during this swap

As amount0 is negative (one of amount0 and amount1 always has to be negative) this means that the pools "lost" 2.681 ETH and "gained" ~6767.4 LOOKS. For the initiator of the swap it's of course the opposite, they spent ~6767.4 LOOKS and received 2.681 ETH.

Or, as Etherscan puts it, they swapped ~6767.4 LOOKS for 2.681 ETH.

Upvotes: 3

Matthew Segura
Matthew Segura

Reputation: 195

amount0 and amount1 are the same, kind of... amount 0 shows a fixed amount, but amount1 shows numbers in notation. Same principle but different format

Upvotes: 0

Related Questions