LogoLogo
HomeMetadata APIAPI PROTwitter
Etherscan V2
Etherscan V2
  • Introduction
  • Chef's Pick
  • Rate Limits
  • V1 to V2 API Migration Guide
  • ✨Getting Started
    • Creating an Account
    • Getting an API Key
    • Supported Chains
    • Supported Endpoints
  • 🎯API Endpoints
    • Nametags
    • Accounts
    • Contracts
    • Transactions
    • Blocks
    • Logs
    • Geth/Parity Proxy
    • Tokens
    • Gas Tracker
    • Stats
    • L2 Deposits/Withdrawals
    • Usage
  • 🏆API PRO
    • Etherscan API PRO
    • Metadata/Name Tag API
  • 🍳Cookbook
    • Track Uniswap V4 DEX Trades
  • Get An Address's Full Transaction History
  • 🤝Support
    • FAQ
    • Legacy V1 Docs
    • Checking Usage
    • Common Error Messages
    • Getting Help
  • Visit Etherscan.io
Powered by GitBook
On this page
  • 1. Find Your Token Pair
  • 2. Filter by Swap Events
  • 3. Get Contract ABI
  • 4. Decode
  • 4. Actually Making Sense Of It
  1. Cookbook

Track Uniswap V4 DEX Trades

PreviousEtherscan API PRONextGet An Address's Full Transaction History

Last updated 1 month ago

Sample

1. Find Your Token Pair

This can be as simple as searching for the token pair on .

Let's track the DEX trades of the pool, which represents users buying or selling ETH.

2. Filter by Swap Events

We're interested in Swap events that come from this pool, which denote the exchange of tokens.

That will be specified as topic0for the logs search.

https://api.etherscan.io/v2/api
   ?chainid=1
   &module=logs
   &action=getLogs
   &address=0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640
   &fromBlock=22057075
   &toBlock=22057085
   &topic0=0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67
   &page=1
   &offset=1000
   &apikey=YourApiKeyToken

3. Get Contract ABI

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

const requestAbi = await fetch("https://api.etherscan.io/v2/api?chainid=1&module=contract&action=getabi&address=0xc7bbec68d12a0d1830360f8ec58fa599ba1b0e9b&apikey=VZFDUWB3YGQ1YCDKTCU1D6DDSS6EWI62KV")
const abi = await requestAbi.json()

4. Decode

const pool = new ethers.Interface(abi.result)

logs.result.forEach((log) => {

    const parsedLog = pool.parseLog(log)
    console.log(parsedLog)
        
}

A decoded log looks like this, still looks pretty gnarly.

{
name: 'Swap',
  signature: 'Swap(address,address,int256,int256,uint160,uint128,int24)',
  topic: '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67',
  args: Result(7) [
    '0xE592427A0AEce92De3Edee1F18E0157C05861564',
    '0x69460570c93f9DE5E2edbC3052bf10125f0Ca22d',
    -7613616n, // usdc received
    3990000000000000n, // eth sent
    1813267739337528689462471906224148n,
    18077430532320816861n,
    200776n
  ]
}

4. Actually Making Sense Of It

-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.

if (amount0 < 0) {

   console.log(`Sell ${amount1} ETH at ${(amount0 * -1) / amount1}`)
   
} else {

   console.log(`Buy ${amount1 * -1} ETH at ${(amount0 * -1) / amount1}`)
   
}

This transaction can then be interpreted as selling 0.00399 ETH for 7.613616 USDC at $1908

Using the library <3, define the contract interface using the ABI retrieved earlier.

The token amounts swapped are denoted in the args ( array starts with 0 anon ).

Logs are always a little complicated to work with, if you get stuck anywhere .

🍳
🐙
code gist
Uniswap
ETH/USDC
Ethers
result 2 and 3
✨
let us know