Skip to content

Commit 9801961

Browse files
committed
Updates readme with a full example.
1 parent c70100d commit 9801961

File tree

1 file changed

+85
-38
lines changed

1 file changed

+85
-38
lines changed

README.md

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,101 @@
1-
# ethereumjs-blockstream
2-
31
[![Build Status](https://travis-ci.org/ethereumjs/ethereumjs-blockstream.svg?branch=master)](https://travis-ci.org/ethereumjs/ethereumjs-blockstream) [![Coverage Status](https://coveralls.io/repos/ethereumjs/ethereumjs-blockstream/badge.svg?branch=master&service=github)](https://coveralls.io/github/ethereumjs/ethereumjs-blockstream?branch=master) [![npm version](https://badge.fury.io/js/ethereumjs-blockstream.svg)](https://badge.fury.io/js/ethereumjs-blockstream)
42

5-
A library to turn an unreliable remote source of Ethereum blocks into a reliable stream of blocks. Handles block and log removals on chain reorganizations as well as block and log backfills on skipped blocks.
6-
7-
## Usage
8-
9-
ethereumjs-blockstream can be installed using npm:
10-
3+
A library to turn an unreliable remote source of Ethereum blocks into a reliable stream of blocks. Handles block and log removals on chain reorganization and block and log backfills on skipped blocks.
4+
5+
# Usage
6+
7+
## Full Example
8+
```typescript
9+
// blockRetention is how many blocks of history to keep in memory. it defaults to 100 if not supplied
10+
const configuration = { blockRetention: 100 };
11+
function getBlockByHash(hash: string): Promise<Block|null> {
12+
return fetch("http://localhost:8545", {
13+
method: "POST",
14+
headers: new Headers({"Content-Type": "application/json"}),
15+
body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByHash", params: [hash, false] }
16+
}).then(response => response.json());
17+
}
18+
//function getBlockByHashCallbackStyle(hash: string, callback: (block: Block|null) => void): void {
19+
// fetch("http://localhost:8545", {
20+
// method: "POST",
21+
// headers: new Headers({"Content-Type": "application/json"}),
22+
// body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByHash", params: [hash, false] }
23+
// })
24+
// .then(response => response.json())
25+
// .then(block => callback(undefined, block))
26+
// .catch(error => callback(error, undefined));
27+
//}
28+
function getLogs(filterOptions: FilterOptions): Promise<Log[]> {
29+
return fetch("http://localhost:8545", {
30+
method: "POST",
31+
headers: new Headers({"Content-Type": "application/json"}),
32+
body: { jsonrpc: "2.0", id: 1, method: "eth_getLogs", params: [filterOptions] }
33+
}).then(response => response.json());
34+
}
35+
//function getLogsCallbackStyle(filterOptions: FilterOptions): Promise<Log[]> {
36+
// return fetch("http://localhost:8545", {
37+
// method: "POST",
38+
// headers: new Headers({"Content-Type": "application/json"}),
39+
// body: { jsonrpc: "2.0", id: 1, method: "eth_getLogs", params: [filterOptions] }
40+
// })
41+
// .then(response => response.json())
42+
// .then(logs => callback(undefined, logs)
43+
// .catch(error => callback(error, undefined));
44+
//}
45+
function getLatestBlock(): Promise<Block> {
46+
return fetch("http://localhost:8545", {
47+
method: "POST",
48+
headers: new Headers({"Content-Type": "application/json"}),
49+
body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByNumber", params: ["latest", false] }
50+
}).then(response => response.json());
51+
}
52+
const blockAndLogStreamer = new BlockAndLogStreamer(getBlockByHash, getLogs, configuration);
53+
// const blockAndLogStreamer = BlockAndLogStreamer.createCallbackStyle(getBlockByHashCallbackStyle, getLogsCallbackStyle, configuration);
54+
const onBlockAddedSubscriptionToken = blockAndLogStreamer.subscribeToOnBlockAdded(block => console.log(block));
55+
const onLogAddedSubscriptionToken = blockAndLogStreamer.subscribeToOnLogAdded(log => console.log(log));
56+
const onBlockRemovedSubscriptionToken = blockAndLogStreamer.subscribeToOnBlockRemoved(block => console.log(block));
57+
const onLogRemovedSubscriptionToken = blockAndLogStreamer.subscribeToOnLogRemoved(log => console.log(log));
58+
const logFilterToken = blockAndLogStreamer.addLogFilter({address: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", topics: ["0xbadf00dbadf00dbadf00dbadf00dbadf00dbadf00dbadf00dbadf00dbaadf00d"]});
59+
blockAndLogStreamer.reconcileNewBlock(getLatestBlock());
60+
// you will get a callback for the block and any logs that match the filter here
61+
triggerBlockMining();
62+
triggerBlockMining();
63+
triggerBlockMining();
64+
blockAndLogStreamer.reconcileNewBlock(getLatestBlock());
65+
// you will get a callback for all blocks and logs that match the filter that have been added to the chain since the previous call to reconcileNewBlock
66+
triggerChainReorg();
67+
blockAndLogStreamer.reconcileNewBlock(getLatestBlock());
68+
// you will get a callback for block/log removals that occurred due to the chain re-org, followed by block/log additions
69+
blockAndLogStreamer.unsubscribeFromOnBlockAdded(onBlockAddedSubscriptionToken);
70+
blockAndLogStreamer.unsubscribeFromOnBlockRemoved(onBlockRemovedSubscriptionToken);
71+
blockAndLogStreamer.unsubscribeFromOnLogAdded(onLogAddedSubscriptionToken);
72+
blockAndLogStreamer.unsubscribeFromOnLogRemoved(onLogRemovedSubscriptionToken);
73+
blockAndLogStreamer.removeLogFilter(logFilterToken);
74+
console.log(blockAndLogStreamer.getLatestReconciledBlock());
1175
```
12-
npm install ethereumjs-blockstream
13-
```
14-
15-
### [Instantiate](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/tests/index.ts#L466)
1676

17-
```javascript
18-
new BlockAndLogStreamer(getBlockByHashFunction, getLogsFunction, { blockRetention: 5 });
19-
```
77+
## Signatures
78+
Note: if you have a TypeScript aware editor this will all be available in the tooltip
79+
* [Filter/FilterOptions](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/source/models/filters.ts#L1-L10) - More details at [Parity JSON-RPC Wiki](https://github.com/paritytech/parity/wiki/JSONRPC-eth-module#eth_newfilter)
80+
* [Block](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/source/models/block.ts#L3-L22) - More details at [Parity JSON-RPC Wiki](https://github.com/paritytech/parity/wiki/JSONRPC-eth-module#eth_getblockbyhash)
81+
* [Log](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/source/models/log.ts#L1-L10) - More details at [Parity JSON-RPC Wiki](https://github.com/paritytech/parity/wiki/JSONRPC-eth-module#eth_getfilterchanges)
2082

21-
### [Subscribe](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/tests/index.ts#L467-L470)
22-
23-
```javascript
24-
blockAndLogStreamer.subscribeToOnLogAdded(onLogAddedCallback);
25-
```
26-
27-
### [Reconcile New Blocks](https://github.com/ethereumjs/ethereumjs-blockstream/blob/master/tests/index.ts#L512-L514)
28-
29-
```javascript
30-
blockAndLogStreamer.reconcileNewBlock(blockFromGetLatest);
31-
```
83+
# Development
3284

3385
## Build
34-
35-
```
36-
npm run build
37-
```
38-
To build using Docker:
39-
4086
```
4187
docker build -t blockstream .
4288
```
43-
44-
## Test
45-
89+
or
4690
```
47-
npm run test
91+
npm run build
4892
```
4993

50-
To run tests using Docker:
51-
94+
## Test
5295
```
5396
docker run blockstream
97+
````
98+
or
99+
```
100+
npm run test
54101
```

0 commit comments

Comments
 (0)