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

Conversation

@mcmire
Copy link
Contributor

@mcmire mcmire commented Sep 30, 2022

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.


Fixes #114.

@mcmire mcmire requested a review from a team as a code owner September 30, 2022 22:21
@mcmire
Copy link
Contributor Author

mcmire commented Sep 30, 2022

The SafeEventEmitterProvider type from eth-json-rpc-middleware for reference: https://github.com/MetaMask/eth-json-rpc-middleware/blob/main/src/types.ts#L28

@mcmire
Copy link
Contributor Author

mcmire commented Oct 5, 2022

Also — and we've discussed this on Slack — in the future we should extract SafeEventEmitterProvider to another package so that both json-rpc-engine and this package can use it, but we also want to make a fully EIP-1193-compatible provider, at which point our provider type would be completely outdated anyway, so...

legobeat
legobeat previously approved these changes Nov 27, 2022
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.
@mcmire
Copy link
Contributor Author

mcmire commented Nov 29, 2022

Tested this with eth-json-rpc-middleware (there are some test files that contain the code I provided in the PR description, such as this one) and it seems to work — no need for a typecast.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mcmire mcmire merged commit cd9c8c8 into main Dec 5, 2022
@mcmire mcmire deleted the fix-provider-type branch December 5, 2022 16:40
This was referenced Dec 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provider type exported by this package should be compatible with provider type exported by eth-json-rpc-middleware

3 participants