Skip to content
This repository was archived by the owner on Oct 16, 2025. It is now read-only.

Commit afa7351

Browse files
committed
Align Provider type w/ other packages & re-expose
The Provider type which is used internally by this package is not compatible with the SafeEventEmitterProvider type used by `eth-json-rpc-middleware`. This means that whenever using this package with `eth-json-rpc-middleware` we have to resort to the following hackery: ``` import { PollingBlockTracker, Provider } from 'eth-block-tracker'; import { JsonRpcEngine } from 'json-rpc-engine'; import { providerFromEngine } from '../src'; const engine = new JsonRpcEngine(); const provider = providerFromEngine(engine); const blockTracker = new PollingBlockTracker({ // Notice the typecasting here provider: provider as Provider, }); ``` SafeEventEmitterProvider is the more accurate type here in this case, because it correctly types the `error` argument for the callback that `sendAsync` calls as `unknown` rather than `Error`, so we follow suit here. Additionally, this package used to expose the Provider type and that was changed in 6.0.0 without being documented, probably because it was located in the same file as BaseBlockTracker. So here we move the type to a more obvious place and re-expose it.
1 parent 3f6b456 commit afa7351

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

src/BaseBlockTracker.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import SafeEventEmitter from '@metamask/safe-event-emitter';
2-
import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine';
32

43
const sec = 1000;
54

65
const calculateSum = (accumulator: number, currentValue: number) =>
76
accumulator + currentValue;
87
const blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];
98

10-
export interface Provider extends SafeEventEmitter {
11-
sendAsync: <T, U>(
12-
req: JsonRpcRequest<T>,
13-
cb: (err: Error, response: JsonRpcResponse<U>) => void,
14-
) => void;
15-
}
16-
179
interface BaseBlockTrackerArgs {
1810
blockResetDuration?: number;
1911
}

src/PollingBlockTracker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import getCreateRandomId from 'json-rpc-random-id';
22
import pify from 'pify';
33
import { JsonRpcRequest } from 'json-rpc-engine';
4-
import { BaseBlockTracker, Provider } from './BaseBlockTracker';
4+
import { BaseBlockTracker } from './BaseBlockTracker';
55
import { projectLogger, createModuleLogger } from './logging-utils';
6+
import { Provider } from './types';
67

78
const log = createModuleLogger(projectLogger, 'polling-block-tracker');
89
const createRandomId = getCreateRandomId();

src/SubscribeBlockTracker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import getCreateRandomId from 'json-rpc-random-id';
22
import { JsonRpcNotification, JsonRpcSuccess } from 'json-rpc-engine';
3-
import { BaseBlockTracker, Provider } from './BaseBlockTracker';
3+
import { BaseBlockTracker } from './BaseBlockTracker';
4+
import { Provider } from './types';
45

56
const createRandomId = getCreateRandomId();
67

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './PollingBlockTracker';
22
export * from './SubscribeBlockTracker';
3+
export * from './types';

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SafeEventEmitter from '@metamask/safe-event-emitter';
2+
import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine';
3+
4+
// This type should be compatible with the one defined in
5+
// `eth-json-rpc-middleware`.
6+
export interface Provider extends SafeEventEmitter {
7+
sendAsync: <T, U>(
8+
req: JsonRpcRequest<T>,
9+
cb: (err: unknown, response: JsonRpcResponse<U>) => void,
10+
) => void;
11+
}

tests/withBlockTracker.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import util from 'util';
22
import SafeEventEmitter from '@metamask/safe-event-emitter';
33
import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine';
4-
import { Provider } from '../src/BaseBlockTracker';
5-
import {
6-
SubscribeBlockTracker,
7-
SubscribeBlockTrackerOptions,
8-
} from '../src/SubscribeBlockTracker';
94
import {
105
PollingBlockTracker,
116
PollingBlockTrackerOptions,
12-
} from '../src/PollingBlockTracker';
7+
Provider,
8+
SubscribeBlockTracker,
9+
SubscribeBlockTrackerOptions,
10+
} from '../src';
1311

1412
interface WithPollingBlockTrackerOptions {
1513
provider?: FakeProviderOptions;

0 commit comments

Comments
 (0)