-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateblock.js
More file actions
47 lines (36 loc) · 1.32 KB
/
Copy pathdateblock.js
File metadata and controls
47 lines (36 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class DateBlock {
constructor(web3) {
if (!(this instanceof DateBlock)) {
return new DateBlock(web3);
}
this.web3 = web3;
}
async guessBlock(curBlock, targetDate, iteration) {
let _this=this;
return new Promise((fulfil) => {
let curBlockDate = new Date(curBlock.timestamp * 1000);
let diff = (curBlockDate - targetDate) / 1000;
let guess = Math.floor(curBlock.number - diff / (15+iteration));
_this.web3.eth.getBlock(guess).then(fulfil)
.catch(console.log);
})
.then (function (guess) {
if(guess === null ) {throw "Future date or block not mined for this date";}
if ( Math.abs(guess.number - curBlock.number) > 1 ) { //|| Date(guess.timestamp*1000)>targetDate
return _this.guessBlock(guess, targetDate, ++iteration);
} else {
return(guess);
}
});
}
async getBlock(date) {
let _this=this;
//try to guess the block
return new Promise((fulfil) => {
this.web3.eth.getBlock('latest').then(function (block) {
_this.guessBlock(block, date, 0).then(fulfil).catch(console.log);
}).catch(console.log);
})
}
}
module.exports = DateBlock;