Track Uniswap V4 DEX Trades
1. Find Your Token Pair
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.
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 topic0
for 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
Using the Ethers library <3, define the contract interface using the ABI retrieved earlier.
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
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.
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
Logs are always a little complicated to work with, let us know if you get stuck anywhere ✨.
Last updated