I want to get logs of all transfer methods in the block.

I am using web3.js or ether.js.

I have to set fromBlock and toBlock.

This is my sample code.

But I can't set fromBlock and toBlock.

Now, I get only logs of latest block.

web3.eth.subscribe('logs', {
                fromBlock: 10150899,
                toBlock: 'latest',
                topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] // transfer
            }, function(error, result){
                //   if (!error)
                //       console.log(result);
            .on("connected", function(subscriptionId){
                // console.log("subcriptionID === " + subscriptionId);
            .on("data", function(log){
                // console.log(log);

How to set fromBlock and toBlock in this sample?

2 Answers 2


You should ideally have a function to fetch all historical logs. Then have the subscribe function which fetches the latest transactions and you also need a third function that fetches logs that have been missed in case your WebSocket connection drops. (backup)


If this is helpful, I created a video showing how to do this here:


You can take out the filter if you want, but I am using Alchemy. You can do this without a provider like Alchemy as well, of course, but this should give you an idea of the setup.

Your Answer

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

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