@@ -7,6 +7,14 @@ export default class EthereumBlocks {
77 // 'web3-eth' object
88 public eth : any ;
99
10+ /**
11+ * Gets last block number
12+ * The return value of this function will be cached for `lastBlockNumberDelay` milliseconds
13+ *
14+ * @return blockNumber of the last block
15+ */
16+ public getLastBlockNumber : ( ) => Promise < number > ;
17+
1018 /**
1119 * Cache of the blockTimestamp indexed by blockNumber
1220 * to ask only once the timestamp of a block from a node
@@ -18,15 +26,30 @@ export default class EthereumBlocks {
1826 // Basically, the block where the contract has been created
1927 private firstSignificantBlockNumber : number ;
2028
29+ // The minimum amount of time to wait between fetches of lastBlockNumber
30+ private getLastBlockNumberMinDelay : number ;
31+
2132 /**
2233 * Constructor
2334 * @param eth eth object from web3
2435 * @param firstSignificantBlockNumber all the block before this one will be ignored
36+ * @param getLastBlockNumberMinDelay the minimum delay to wait between fetches of lastBlockNumber
2537 */
26- public constructor ( eth : any , firstSignificantBlockNumber : number ) {
38+ public constructor (
39+ eth : any ,
40+ firstSignificantBlockNumber : number ,
41+ getLastBlockNumberMinDelay : number = 0 ,
42+ ) {
2743 this . eth = eth ;
2844
2945 this . firstSignificantBlockNumber = firstSignificantBlockNumber ;
46+
47+ this . getLastBlockNumberMinDelay = getLastBlockNumberMinDelay ;
48+ // Setup the throttled and retriable getLastBlockNumber function
49+ this . getLastBlockNumber = Utils . cachedThrottle (
50+ ( ) => Utils . retry ( this . eth . getBlockNumber ) ( ) ,
51+ this . getLastBlockNumberMinDelay ,
52+ ) ;
3053 }
3154
3255 /**
@@ -109,15 +132,6 @@ export default class EthereumBlocks {
109132 ) ;
110133 }
111134
112- /**
113- * Gets last block number
114- * @return blockNumber of the last block
115- */
116- public async getLastBlockNumber ( ) : Promise < number > {
117- // Use Utils.retry to rerun if getBlockNumber fails
118- return Utils . retry ( this . eth . getBlockNumber ) ( ) ;
119- }
120-
121135 /**
122136 * Gets second last block number
123137 * @return blockNumber of the second last block
0 commit comments