This recipe shows you how to use the Ethers library with a Chainstack Ethereum node to retrieve transfer logs for an ERC-20 token.
Prerequisites
Environment setup
ethers.js
library.npm install ethers
Initialize a provider instance
index.js
, import the ethers.js library, and initialize a new provider instance using your Chainstack node URL.Paste your Chainstack node URL in the nodeUrl
const.Initialize the ABI and smart contract instance
transfer
event.This specific example retrieves the transfer logs for the APE token, but you can use any ERC-20 token smart contract address.Function to retrieve the logs
getLogs
where we define the range of blocks to query and call the queryFilter
function to retrieve the transfer logs.We query the past 100 blocks in this example and use the getBlockNumber()
method to identify the latest block.Create the main function and parse the response
main()
function used to run the main program.This function will call the getLogs()
function and parse the response extracting only the data that we want; in this case:Run the index.js file
node index.js
The script will retrieve the logs from the past 100 blocks and print the transfer information on the screen.