Skip to content

Commit

Permalink
Merge pull request #14 from ethereumjs/micah
Browse files Browse the repository at this point in the history
Deprecates callback style interface.
  • Loading branch information
MicahZoltu authored Feb 19, 2018
2 parents 6580081 + 91bec2b commit 6d6262e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
42 changes: 12 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,31 @@ A library to turn an unreliable remote source of Ethereum blocks into a reliable
```typescript
// blockRetention is how many blocks of history to keep in memory. it defaults to 100 if not supplied
const configuration = { blockRetention: 100 };
function getBlockByHash(hash: string): Promise<Block|null> {
return fetch("http://localhost:8545", {
async function getBlockByHash(hash: string): Promise<Block|null> {
const response = await fetch("http://localhost:8545", {
method: "POST",
headers: new Headers({"Content-Type": "application/json"}),
body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByHash", params: [hash, false] }
}).then(response => response.json());
});
return await response.json();
}
//function getBlockByHashCallbackStyle(hash: string, callback: (error?: Error, block?: Block|null) => void): void {
// fetch("http://localhost:8545", {
// method: "POST",
// headers: new Headers({"Content-Type": "application/json"}),
// body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByHash", params: [hash, false] }
// })
// .then(response => response.json())
// .then(block => callback(undefined, block))
// .catch(error => callback(error, undefined));
//}
function getLogs(filterOptions: FilterOptions): Promise<Log[]> {
return fetch("http://localhost:8545", {
async function getLogs(filterOptions: FilterOptions): Promise<Log[]> {
const response = await fetch("http://localhost:8545", {
method: "POST",
headers: new Headers({"Content-Type": "application/json"}),
body: { jsonrpc: "2.0", id: 1, method: "eth_getLogs", params: [filterOptions] }
}).then(response => response.json());
});
return await response.json();
}
//function getLogsCallbackStyle(filterOptions: FilterOptions, callback: (error?: Error, logs?: Log[]) => void): void {
// return fetch("http://localhost:8545", {
// method: "POST",
// headers: new Headers({"Content-Type": "application/json"}),
// body: { jsonrpc: "2.0", id: 1, method: "eth_getLogs", params: [filterOptions] }
// })
// .then(response => response.json())
// .then(logs => callback(undefined, logs)
// .catch(error => callback(error, undefined));
//}
function getLatestBlock(): Promise<Block> {
return fetch("http://localhost:8545", {
async function getLatestBlock(): Promise<Block> {
const response = await fetch("http://localhost:8545", {
method: "POST",
headers: new Headers({"Content-Type": "application/json"}),
body: { jsonrpc: "2.0", id: 1, method: "eth_getBlockByNumber", params: ["latest", false] }
}).then(response => response.json());
});
return await response.json();
}
const blockAndLogStreamer = new BlockAndLogStreamer(getBlockByHash, getLogs, configuration);
// const blockAndLogStreamer = BlockAndLogStreamer.createCallbackStyle(getBlockByHashCallbackStyle, getLogsCallbackStyle, configuration);
const onBlockAddedSubscriptionToken = blockAndLogStreamer.subscribeToOnBlockAdded(block => console.log(block));
const onLogAddedSubscriptionToken = blockAndLogStreamer.subscribeToOnLogAdded(log => console.log(log));
const onBlockRemovedSubscriptionToken = blockAndLogStreamer.subscribeToOnBlockRemoved(block => console.log(block));
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereumjs-blockstream",
"version": "4.0.0",
"version": "4.0.1",
"description": "A library to turn an unreliable remote source of Ethereum blocks into a reliable stream of blocks with removals on re-orgs and backfills on skips.",
"main": "output/source/index.js",
"types": "output/source/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions source/block-and-log-streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class BlockAndLogStreamer<TBlock extends Block, TLog extends Log> {
getLogs: (filterOptions: FilterOptions, callback: (error?: Error, logs?: TLog[]) => void) => void,
configuration?: { blockRetention?: number },
): BlockAndLogStreamer<TBlock, TLog> => {
console.warn(`Deprecation Warning: The callback interface for ethereumjs-blockstream is deprecated and will be removed in a future version of this library. Use BlockAndLogStreamer constructor instead.`);
const wrappedGetBlockByHash = (hash: string): Promise<TBlock | null> => {
return new Promise<TBlock | null>((resolve, reject) => {
getBlockByHash(hash, (error, block) => {
Expand All @@ -65,6 +66,7 @@ export class BlockAndLogStreamer<TBlock extends Block, TLog extends Log> {
};

public readonly reconcileNewBlockCallbackStyle = async (block: TBlock, callback: (error?: Error) => void): Promise<void> => {
console.warn(`Deprecation Warning: The callback interface for ethereumjs-blockstream is deprecated and will be removed in a future version of this library. Use BlockAndLogStreamer.reconcileNewBlock instead.`);
this.reconcileNewBlock(block)
.then(() => callback(undefined))
.catch(error => callback(error));
Expand Down

0 comments on commit 6d6262e

Please sign in to comment.