robertoschler
over 1 year ago  Karma: 50
Web3.js function(s) to help determine the block numbers inside a date range?

I'm working through Lesson 6, Chapter 9. In that lesson it talks about subscribing to events and also querying for events within a certain block range:

We can even query past events using getPastEvents, and use the filters fromBlock and toBlock to give Solidity a time range for the event logs ("block" in this case referring to the Ethereum block number):
``
cryptoZombies.getPastEvents("NewZombie", { fromBlock: 0, toBlock: "latest" })
.then(function(events) {
//
eventsis an array ofevent` objects that we can iterate, like we did above
// This code will get us a list of every zombie that was ever created
});


Are there functions in Web3.js that can help convert a date range to *from* and *to* block numbers needed to cover that date range when calling *getPastEvents()*?  Or is there another method/technique that works directly with dates instead of block numbers?
blocks
solidity
web3.js
node.js
robertoschler
over 1 year ago  Karma: 50
Web3.js function(s) to help determine the block numbers inside a date range?

I'm working through Lesson 6, Chapter 9. In that lesson it talks about subscribing to events and also querying for events within a certain block range:

We can even query past events using getPastEvents, and use the filters fromBlock and toBlock to give Solidity a time range for the event logs ("block" in this case referring to the Ethereum block number):
``
cryptoZombies.getPastEvents("NewZombie", { fromBlock: 0, toBlock: "latest" })
.then(function(events) {
//
eventsis an array ofevent` objects that we can iterate, like we did above
// This code will get us a list of every zombie that was ever created
});


Are there functions in Web3.js that can help convert a date range to *from* and *to* block numbers needed to cover that date range when calling *getPastEvents()*?  Or is there another method/technique that works directly with dates instead of block numbers?
blocks
solidity
web3.js
node.js

ONE ANSWER
bockpeter
over 1 year ago Karma: 1162

From what I can see in the Web3.js documentation there isn't a function in web3.js that allows you to convert a date / unix timestampt into a block number.

Every block has an associated timestamp, but my searches on the web seem to point to them only being accessible by already having the block number and getting it.

I think that would be a View (given it only gets data and reads it, and modifies nothing), and thus not cost anything, given that it only reads the data, and attempts no modification.

You SHOULD be able to code a makeshift solution in your ethereum contract, that you give a date (that is then converted into a unix timestamp) and that will query blocks, using one of the algorithms that work well with sorted arrays (given each succesive block will be older than the last one).

It'd take time to find the right block, and it depends could be pretty bloated if there's no way to restrict which information you get back on the block (if you get the entire block every time, including contents...)


Do take note that I'm in no way an expert. This is just how I'd approach the situation, if there really is no direct method to get blocks by their timestamp instead of block number.

From what I can see in the Web3.js documentation there isn't a function in web3.js that allows you to convert a date / unix timestampt into a block number.

Every block has an associated timestamp, but my searches on the web seem to point to them only being accessible by already having the block number and getting it.

I think that would be a View (given it only gets data and reads it, and modifies nothing), and thus not cost anything, given that it only reads the data, and attempts no modification.

You SHOULD be able to code a makeshift solution in your ethereum contract, that you give a date (that is then converted into a unix timestamp) and that will query blocks, using one of the algorithms that work well with sorted arrays (given each succesive block will be older than the last one).

It'd take time to find the right block, and it depends could be pretty bloated if there's no way to restrict which information you get back on the block (if you get the entire block every time, including contents...)


Do take note that I'm in no way an expert. This is just how I'd approach the situation, if there really is no direct method to get blocks by their timestamp instead of block number.

gvsrusa

Were you able to find the answer? I'm also in the same situation

Jul 23 '18 at 3:36 pm
gvsrusa

I hope this helps

You can use like this. (await web3.eth.getBlockNumber()). this will give you current block number. if you want past 2 days blocks the expression will look like below - (await web3.eth.getBlockNumber()) - 12343. Remember for every 14sec there is a new block generated. So for 2days it is appx 12343 block. Same way you can do it for between dates as well.

Exampe: last but one day blocks

const events= await MyContract.getPastEvents("Event", {
fromBlock: (await web3.eth.getBlockNumber()) - 12343,
toBlock: (await web3.eth.getBlockNumber()) - 6172
});


Jul 23 '18 at 5:21 pm
Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER