I am trying to filter transfer events using node.js for a contract address between a range of blocks. I am trying to filter the latest 7 transactions which can be shown in etherscan here. My code is as follows:
const Web3 = require('web3');
const fetch = require('node-fetch');
let infura_url = "https://mainnet.infura.io/v3/853346f695b740fe8bd7d8f583bcf55f"
let web3 = new Web3(new Web3.providers.HttpProvider(infura_url))
let fromBlock = web3.utils.toHex(10394853);
let toBlock = web3.utils.toHex(10399444);
let address = "0x683f6e80c378e57fea188d53f4440600e87abc46";
let blockHash = "0xb3b20624f8f0f86eb50dd04688409e5cea4bd02d700bf6e79e9384d47d6a5a35"
let topic = ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"];
let body = {
"jsonrpc": "2.0",
"method": "eth_getLogs",
"params": [{
"toBlock": fromBlock,
"fromBlock": toBlock,
"address": address,
//"blockHash" : blockHash
"topic": topic
}],
"id": 1
};
fetch(infura_url, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then(result => result.json())
.then(response => {
console.log(response);
}).catch(err => res.json({ error: err.message }));
However it returns an empty array as result:
{ jsonrpc: '2.0', id: 1, result: [] }
Also, I want to understand how to pass the fromBlock and toBlock. The documentation says that the blockFrom can be an integer number but when I write it in integer it says the value should be hex "0x". No address is working in the address filter. The only results I can get are with blockHAsh. Kindly help me in figuring out the issue. The contract address, toBlock, fromBlock and topic is mentioned in the code.