Skip to content

Instantly share code, notes, and snippets.

@0xV4L3NT1N3
Created March 17, 2025 17:50
Show Gist options
  • Select an option

  • Save 0xV4L3NT1N3/080fe054bcbcfbbf00f04becb058fa52 to your computer and use it in GitHub Desktop.

Select an option

Save 0xV4L3NT1N3/080fe054bcbcfbbf00f04becb058fa52 to your computer and use it in GitHub Desktop.
Get Uniswap V4 Swaps
const ethers = require("ethers")
async function getUniswapV4Swaps() {
// get abi
const requestAbi = await fetch("https://api.etherscan.io/v2/api?chainid=1&module=contract&action=getabi&address=0xc7bbec68d12a0d1830360f8ec58fa599ba1b0e9b&apikey=YourApiKeyToken")
const abi = await requestAbi.json()
// get logs from ETH/USDC pair
const requestLogs = await fetch("https://api.etherscan.io/v2/api?chainid=1&module=logs&action=getLogs&address=0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640&fromBlock=22065948&toBlock=22065958&topic0=0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67&page=1&offset=1000&apikey=YourApiKeyToken")
const logs = await requestLogs.json()
// define pool contract interface
const pool = new ethers.Interface(abi.result)
// decode
logs.result.forEach((log) => {
const parsedLog = pool.parseLog(log)
console.log(parsedLog)
console.log(`https://etherscan.io/tx/${log.transactionHash}`)
const amount0 = ethers.formatUnits(parsedLog.args[2], 6)
const amount1 = ethers.formatEther(parsedLog.args[3], 6)
// negative amount indicates tokens sent from the pool
// that is the token you bought
if (amount0 < 0) {
console.log(`Sell ${amount1} ETH at ${(amount0 * -1) / amount1}`)
} else {
console.log(`Buy ${amount1 * -1} ETH at ${(amount0 * -1) / amount1}`)
}
})
}
getUniswapV4Swaps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment