1

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.

1 Answer 1

1

Not sure where exactly your problem is, but you can do the whole thing directly with web3 (instead of an HTTP request):

const params = {
    address  : "0x683f6e80c378e57fea188d53f4440600e87abc46",
    topics   : ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
    fromBlock: 10394853,
    toBlock  : 10399444
};
web3.eth.getPastLogs(params).then(response => ...

Update - I also know where your problem is:

You are using the address of one of the parties involved in the transfer events (i.e., the from in some cases and the to in other cases), while you should be using the address of the specific token involved, as you can see here for example:

  • Address 0xdceaf1652a131f32a821468dc03a92df0edd86ea
  • Address 0x67cbbb366a51fff9ad869d027e496ba49f5f6d55
1
  • Thankyou so much your comment solved my problem. It is now returning logs.
    – mzaidi
    Jul 6, 2020 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.