Introduction
For an introduction to smart contract events, see Getting logs and events of EVM smart contracts.
This article focuses using the web3.js getPastEvents method with a filter to retrieve events for a smart contract through a Chainstack node.
Sample scenario: get all USDT transactions for an address on the Ethereum mainnet.
How-to
Prerequisites
- A node deployed on Chainstack—you will use the node's HTTPS endpoint to retrieve the events data.
- Installed web3.js—you will use the web3.js' getPastEvents method retrieve the events data.
- Contract ABI—you will need the ABI to retrieve the contract events. An easy way to get the contract ABI is to look up the contract on Etherscan and get the ABI on the Contract tab.
Getting the events
Template
To get the events, use the following template:
const Web3 = require('web3');
const web3 = new Web3('HTTPS_ENDPOINT');
const contractAddress = 'CONTRACT_ADDRESS';
const contractAbi = require('./ABI_JSON');
const contract = new web3.eth.Contract(contractAbi, contractAddress);
const PAST_EVENT = async () => {
await contract.getPastEvents('EVENT_NAME',
{
filter: { INDEXED_PARAMETER: VALUE },
fromBlock: BLOCK_NUMBER,
toBlock: BLOCK_NUMBER,
},
(err, events) => {
console.log(events);
});
};
PAST_EVENT();
where
- HTTPS_ENDPOINT — your node's HTTPS endpoint through which you will retrieve the events data. See View node access and credentials.
- CONTRACT_ADDRESS — the contract address whose events you want to track.
- ABI_JSON — the ABI of the contract in JSON format.
- PAST_EVENT — any name you want to assign to print it at the end.
- EVENT_NAME — the name of the event as defined in the smart contract.
- INDEXED_PARAMETER — the indexed parameter of the event.
- VALUE — a value of the indexed parameter.
- BLOCK_NUMBER — the block number to specify the range for the events retrieval.
Example for USDT transfers
The example below does the following:
- Uses a Chainstack Ethereum node endpoint https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d (sample endpoint for illustration).
- Tracks all Ethereum mainnet USDT transfers to and from a Binance address.
- The transfers are tracked using the Transfer event—line 86 in the USDT contract code.
- The transfers are tracked from block 13943000 to block 13943100.
- The contract ABI is taken directly from the USDT contract page on Etherscan.
Example:
const Web3 = require('web3');
const web3 = new Web3('https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d');
const contractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const contractAbi = require('./usdtABI.json');
const contract = new web3.eth.Contract(contractAbi, contractAddress);
const transferFrom = async () => {
await contract.getPastEvents('Transfer',
{
filter: { from: '0x28c6c06298d514db089934071355e5743bf21d60' },
fromBlock: 13943000,
toBlock: 13943100,
},
(err, events) => {
console.log(events);
});
};
const transferTo = async () => {
await contract.getPastEvents('Transfer',
{
filter: { to: '0x28c6c06298d514db089934071355e5743bf21d60' },
fromBlock: 13943000,
toBlock: 13943100,
},
(err, events) => {
console.log(events);
});
};
transferFrom();
transferTo();
Additional information
Under the hood, the getPastEvents method calls eth_getLogs.
Comments
0 comments
Article is closed for comments.