UNPKG

leap-node

Version:

[![codecov](https://codecov.io/gh/leapdao/leap-node/branch/master/graph/badge.svg)](https://codecov.io/gh/leapdao/leap-node) [![Docker Repository on Quay](https://quay.io/repository/leapdao/leap-node/status "Docker Repository on Quay")](https://quay.io/re

26 lines (21 loc) 658 B
const getBlockAverageTime = require('./getBlockAverageTime'); module.exports = class BlockTicker { constructor(web3, subscribers = []) { this.web3 = web3; this.subscribers = subscribers; this.latestBlock = this.latestBlock.bind(this); } subscribe(subscriber) { this.subscribers.push(subscriber); } async init() { const interval = Math.max(1, (await getBlockAverageTime(this.web3)) * 0.7); setInterval(this.latestBlock, interval * 1000); } async latestBlock() { const blockNumber = await this.web3.eth.getBlockNumber(); for (const subscriber of this.subscribers) { subscriber(blockNumber); } } };