Created
April 1, 2023 04:14
-
-
Save ZacharyCouchman/930f1036abf3312d72f7cb958b8bcb3b to your computer and use it in GitHub Desktop.
Query events that have been emitted by the IMX contract
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
import { ethers } from 'ethers'; | |
import { formatUnits } from 'ethers/lib/utils.js'; | |
const rpcUrl = `https://eth-mainnet.g.alchemy.com/v2/demo`; | |
const imxERC20TokenAddress = "0xF57e7e7C23978C3cAEC3C3548E3D615c346e79fF"; | |
const contractABI = [ | |
"function name() view returns (string)", | |
"function symbol() view returns (string)", | |
"function decimals() view returns (uint8)", | |
"function balanceOf(address account) view returns (uint256)", | |
"event Transfer(address indexed from, address indexed to, uint256 value)" | |
]; | |
async function queryEvents() { | |
const provider = new ethers.providers.JsonRpcProvider(rpcUrl); | |
const contract = new ethers.Contract(imxERC20TokenAddress, contractABI, provider); | |
const filter = contract.filters.Transfer(); | |
// query all IMX transfer events in the last 200 blocks | |
const events = await contract.queryFilter(filter, provider.blockNumber - 200, provider.blockNumber); | |
const logTransfer = (event) => console.log(`${event.args.from} transferred ${formatUnits(event.args.value, 18)} IMX to ${event.args.to}`); | |
events.forEach(logTransfer); | |
} | |
queryEvents(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment