|
1 | 1 |
|
2 | | -ethjs-query |
3 | | -new BlockTracker({ provider }) |
| 2 | +# eth-block-tracker |
4 | 3 |
|
| 4 | +This module walks the Ethereum blockchain, keeping track of the latest block. |
| 5 | +It uses a web3 provider as a data source and will continuously poll for the next block. |
5 | 6 |
|
| 7 | +``` |
| 8 | +const HttpProvider = require('ethjs-provider-http') |
| 9 | +const BlockTracker = require('eth-block-tracker') |
6 | 10 |
|
| 11 | +const provider = new HttpProvider('https://mainnet.infura.io') |
| 12 | +const blockTracker = new BlockTracker({ provider }) |
| 13 | +blockTracker.on('block', console.log) |
| 14 | +blockTracker.start() |
| 15 | +``` |
7 | 16 |
|
| 17 | +### methods |
8 | 18 |
|
| 19 | +##### new BlockTracker({ provider, pollingInterval }) |
9 | 20 |
|
| 21 | +creates a new block tracker with `provider` as a data source and |
| 22 | +`pollingInterval` (ms) timeout between polling for the latest block. |
10 | 23 |
|
11 | | -// |
12 | | -// public |
13 | | -// |
| 24 | +##### getCurrentBlock() |
14 | 25 |
|
15 | | -getCurrentBlock() |
16 | | -start() |
17 | | -stop() |
| 26 | +synchronous returns the current block. may be `null`. |
| 27 | + |
| 28 | +```js |
| 29 | +console.log(blockTracker.getCurrentBlock()) |
| 30 | +``` |
| 31 | + |
| 32 | +##### start({ fromBlock }) |
| 33 | + |
| 34 | +Start walking from the `fromBlock` (default: `'latest'`) forward. |
| 35 | +`fromBlock` should be a number as a hex encoded string. |
| 36 | + |
| 37 | +```js |
| 38 | +blockTracker.start() |
| 39 | +``` |
| 40 | + |
| 41 | +```js |
| 42 | +blockTracker.start({ fromBlock: '0x00' }) |
| 43 | +``` |
| 44 | + |
| 45 | +##### stop() |
| 46 | + |
| 47 | +Stop walking the blockchain. |
| 48 | + |
| 49 | +```js |
| 50 | +blockTracker.stop() |
| 51 | +``` |
18 | 52 |
|
19 | 53 | ### EVENTS |
20 | | -block <-- every block in order |
21 | | -fork <-- common root of fork |
22 | | -force <-- latest block was forced |
23 | | -latest <-- the latest block, possibly skipping blocks |
24 | | - |
25 | | -// |
26 | | -// private |
27 | | -// |
28 | | - |
29 | | -async _performSync() { |
30 | | - const latestBlock = await this._fetchLatestBlock() |
31 | | - return this._considerBlock(latestBlock) |
32 | | -} |
33 | | - |
34 | | -async _considerBlock(newBlock) { |
35 | | - const currentBlock = this.getCurrentBlock() |
36 | | - // check if new block should be head |
37 | | - if (!difficultyLessThan(currentBlock, newBlock)) { |
38 | | - return Promise.resolve() |
39 | | - } |
40 | | - const blockPath = await _fetchPathBetweenBlocks(currentBlock, newBlock) |
41 | | -} |
42 | | - |
43 | | -_fetchPathBetweenBlocks (startBlock, endBlock) { |
44 | | - // walk from end to start |
45 | | -} |
46 | | - |
47 | | -_fetchLatestBlock |
48 | | - |
49 | | -_fetchBlockByHash |
50 | | - |
51 | | -_fetchBlockByTag |
52 | | - |
53 | | -poll latest, |
54 | | -walk back to current block, |
55 | | -abort if too far? |
56 | | -ignore if 'latest' if less difficulty |
| 54 | + |
| 55 | +##### block |
| 56 | + |
| 57 | +The `block` event is emitted for every block in order. |
| 58 | +Use this event if you want to operate on every block without missing any. |
| 59 | + |
| 60 | +```js |
| 61 | +blockTracker.on('block',console.log) |
| 62 | +``` |
| 63 | + |
| 64 | +##### latest |
| 65 | + |
| 66 | +The `latest` event is emitted for every that is detected to be the latest block. |
| 67 | +This means skipping a block if there were two created since the last polling period. |
| 68 | +Use this event if you don't care about stale blocks. |
| 69 | + |
| 70 | +```js |
| 71 | +blockTracker.on('latest',console.log) |
| 72 | +``` |
| 73 | + |
| 74 | +### NOTES |
| 75 | + |
| 76 | +Does not currently handle forks. |
0 commit comments