Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental web3js: RPC methods with no inputs are typed unknown #1389

Closed
mcintyre94 opened this issue Jul 10, 2023 · 5 comments · Fixed by #1401
Closed

Experimental web3js: RPC methods with no inputs are typed unknown #1389

mcintyre94 opened this issue Jul 10, 2023 · 5 comments · Fixed by #1401
Labels
bug Something isn't working

Comments

@mcintyre94
Copy link
Collaborator

Overview

I'm trying to use getFirstAvailableBlock, getEpochInfo and getEpochSchedule in a refactor of Explorer. I've found that the return of getFirstAvailableBlock and getEpochSchedule are typed as unknown by TypeScript. However getEpochInfo is typed correctly even when called with no parameters, I think because it has an optional parameter.

Steps to reproduce

See replit: https://replit.com/@mcintyre94/web3js-unknown#index.ts

See how TypeScript types these incorrectly:
Screenshot 2023-07-10 at 17 39 02
Screenshot 2023-07-10 at 17 39 15
Screenshot 2023-07-10 at 17 39 25

We also see this in the autocomplete:
Screenshot 2023-07-10 at 17 39 59
Screenshot 2023-07-10 at 17 40 05

I've also seen this locally using VSCode so it's not a Replit issue

Description of bug

As far as I can tell this only happens with RPC methods that take no inputs, either optional or required

I'm guessing we need to tighten up the proxy a bit to correctly type this case

The full message when autocompleting getEpochSchedule is:

(method) getEpochSchedule(...args: unknown[]): PendingRpcRequest<unknown> (+1 overload)
@steveluscher
Copy link
Collaborator

Shit. This is ringing some {I filed this bug with TypeScript} bells that I hoped never to hear again.

@steveluscher
Copy link
Collaborator

This? microsoft/TypeScript#53508

@mcintyre94
Copy link
Collaborator Author

That looks like the one and doesn't seem to have a fix

A hacky workaround that would probably work would be adding an optional empty config object to these functions. Easy for a caller to ignore when they write the call (and for us to document in typedocs), and then should just work like everything else from there. Think that's worth doing?

@steveluscher
Copy link
Collaborator

steveluscher commented Jul 13, 2023

Yes please! Add it and liberally // FIXME it with a reference back here.

You can make the config object Record<string, never> to express ‘definitely empty object.’

@mcintyre94 mcintyre94 assigned mcintyre94 and unassigned mcintyre94 Jul 14, 2023
mcintyre94 added a commit to mcintyre94/solana-web3.js that referenced this issue Jul 14, 2023
This fixes an issue where TS incorrectly types their returns as `unknown`

See solana-labs#1389
mcintyre94 added a commit to solana-labs/explorer that referenced this issue Jul 14, 2023
This provider makes 3 requests, and makes their responses available to
child components:

- `getFirstAvailableBlock`
- `getEpochInfo`
- `getEpochSchedule`

The first 2 are reasonably straightforward, we have basically the same
structure as the legacy web3js, with just number/bigint changes.

`getEpochSchedule` is a bit more complex. The existing API exposes a
class with a bunch of functionality for finding the epoch for a slot,
and the first/last slot for an epoch. None of this is RPC functionality,
it's all baked into the legacy web3js code. Since the experimental
web3js doesn't do any of that, I've copied these functions into the
Explorer codebase, as pure functions that take an `EpochSchedule` (pure
data returned by the new RPC method) and a slot/epoch (bigint).

See existing web3js code here:
https://github.com/solana-labs/solana-web3.js/blob/9232d2b1019dc50f852ad70aa81624e751d76161/packages/library-legacy/src/epoch-schedule.ts

Also note that I hit a bug in experimental web3js where some of these
functions are incorrectly typed as unknown:
solana-labs/solana-web3.js#1389
This was easy enough to work around for now

I've also moved `localStorageIsAvailable` from `utils/index.ts` to its
own `utils/local-storage`. This lets us import it without pulling in the
web3js dependency in `utils/index.ts`

The result of this PR is that the `ClusterProvider` in the root layout
no longer pulls in the legacy web3js dependency.
mcintyre94 added a commit to mcintyre94/solana-web3.js that referenced this issue Jul 17, 2023
…ds with no params

This fixes an issue where TS incorrectly types their returns as `unknown`

See solana-labs#1389
mcintyre94 added a commit to mcintyre94/solana-web3.js that referenced this issue Jul 18, 2023
…ds with no params

This fixes an issue where TS incorrectly types their returns as `unknown`

See solana-labs#1389
mcintyre94 added a commit that referenced this issue Jul 18, 2023
…ds with no params (#1401)

This fixes an issue where TS incorrectly types their returns as `unknown`

See #1389
@github-actions
Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants