Track Uniswap V4 DEX Trades
Last updated
Last updated
Sample code gist
This can be as simple as searching for the token pair on Uniswap.
Let's track the DEX trades of the ETH/USDC pool, which represents users buying or selling ETH.
We're interested in Swap events that come from this pool, which denote the exchange of tokens.
That will be specified as topic0
for the logs search.
You can think of a contract's Application Binary Interface ( ABI ) as an instruction manual, it details the function names, format and units to decode the logs.
If the contract is verified on Etherscan, it can be retrieved via API too
Using the Ethers library <3, define the contract interface using the ABI retrieved earlier.
A decoded log looks like this, still looks pretty gnarly.
The token amounts swapped are denoted in the args
result 2 and 3 ( array starts with 0 anon ).
-7613616n
( 7.613616 in decimals ) USDC
3990000000000000n
( 0.00399 in decimals ) ETH
A negative number indicates tokens were sent from the pool to the user address, this is the token you receive.
Conventionally we denote receiving USDC ( amount0
being negative ) as a sell transaction and receiving ETH ( amount1
being negative ) as a buy transaction.
This transaction can then be interpreted as selling 0.00399 ETH for 7.613616 USDC at $1908
Logs are always a little complicated to work with, let us know if you get stuck anywhere .