Created
July 7, 2023 16:44
-
-
Save 10dimensions/af75ca44eeff35205f6d1b8c739bc176 to your computer and use it in GitHub Desktop.
Simple UniswapV2 Event Indexer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ethers = require("ethers"); | |
const provider = new ethers.providers.WebSocketProvider( | |
"wss://mainnet.infura.io/ws/v3/<RPC-KEY-HERE>" | |
) | |
const swapHistory = {} | |
filter = { | |
address: ["0xE1De07566cEb19D2e45B60B491776aB74C9b8d7A"], //token address | |
topics: [ | |
"0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822" //event address | |
] | |
} | |
const decodeData = (data) => { | |
return ethers.utils.defaultAbiCoder.decode( | |
['address', 'uint256', 'uint256', 'uint256', 'uint256', 'address'], | |
ethers.utils.hexDataSlice(data, 4) | |
) | |
} | |
const decodeIface = (data) => { | |
const iface = new ethers.utils.Interface([ "event Swap(address indexed sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, address indexed to)" ]); | |
const event = iface.parseLog(data); | |
return event; | |
} | |
provider.on(filter, (e) => { | |
//console.log(e) | |
let swapDat = decodeIface(e) | |
//console.log(swapDat) | |
let swapObj = { | |
"txnHash": e.transactionHash, | |
"sender": swapDat.args[0], | |
"amount0In": ethers.utils.formatEther(parseInt(swapDat.args[1]._hex).toString()), | |
"amount1In": ethers.utils.formatEther(parseInt(swapDat.args[2]._hex).toString()), | |
"amount0Out": ethers.utils.formatEther(parseInt(swapDat.args[3]._hex).toString()), | |
"amount1Out": ethers.utils.formatEther(parseInt(swapDat.args[4]._hex).toString()), | |
"to": swapDat.args[5], | |
} | |
//console.log(swapObj); | |
swapHistory[e.transactionHash] = swapObj; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment