Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 8004

How to infer or find basic types for return values in @solana/web3.js v2 library?

$
0
0

I’m working on a project using the Solana web3.js v2 library, and I’ve run into a challenge with types. Specifically, I’m trying to infer or find the correct type for the return value of a function like this:

async function initializeBlockSubscription(  wssEndpoint: string,  abortSignal: AbortSignal) {  const subscription = createSolanaRpcSubscriptions_UNSTABLE(wssEndpoint);  const notifications = subscription.blockNotifications(    { mentionsAccountOrProgram: TOKEN_PROGRAM_ADDRESS.toString() },    {      encoding: 'base64',      commitment: 'confirmed',      showRewards: false,      transactionDetails: 'full',      maxSupportedTransactionVersion: 0    }  );  const res = await notifications.subscribe({ abortSignal });  return res;}

What I’ve Tried:

  1. Type Inference: My IDE (Jetbrains Webstorm) tries to infer the type of res, but the result is deeply nested and tied to generics that are not exported.
  2. Searching for Exported Types: I couldn’t find a clearly exported or basic type for the return value of notifications.subscribe in the library documentation or type declarations.
  3. Inspecting Source Code: The type seems to be generic or heavily dependent on internal library implementation, which makes it difficult to extract.

My Questions:

  1. Is there a basic or recommended type for the return value of notifications.subscribe that I might have missed?
  2. What’s the best way to deal with return types in libraries like this, where types are deeply nested or undocumented?
  3. Are there any established practices for working with Solana’s web3.js types when they are not clearly exported or straightforward?

Notes:

  • I’m using TypeScript in strict mode, so having a proper type definition is important for my project.
  • I’d prefer not to rely on any or unknown unless absolutely necessary.

Any insights or workarounds would be greatly appreciated!

UPD:

Here’s another use case where I need this type. Even if we successfully infer the return type of the initializeBlockSubscription function, we still need to determine the correct type for the blocks parameter in the processBlocks function:

async function processBlocks(blocks): Promise<void> { // <-- I need to specify a type for the `blocks` parameter  for await (const block of blocks) {    if (!block.value.block?.transactions) continue;    for (const transaction of block.value.block.transactions) {      const message = decodeTransaction(transaction.transaction[0]);      message.instructions.forEach(instruction =>        handleInstruction(instruction, message.staticAccounts)      );    }  }}

Viewing all articles
Browse latest Browse all 8004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>