solana-web3.js icon indicating copy to clipboard operation
solana-web3.js copied to clipboard

Experimental: type errors in `getTransaction`

Open mcintyre94 opened this issue 2 years ago • 3 comments
trafficstars

Overview

We've come across this issue a few times where using the pattern like TSomething extends true ? {extraField: X} : Record<string, never> to conditionally add fields to a response, doesn't work correctly in the experimental RPC. This comes down to https://github.com/solana-labs/solana-web3.js/pull/1531

I noticed we use that pattern in getTransaction and wanted to figure out why it works there. Unfortunately I don't think it does

Steps to reproduce

Replit: https://replit.com/@mcintyre94/tx-transaction-version#index.ts

Code:

const address = 'abc' as Base58EncodedAddress;
const tx1 = await rpc
    .getTransaction(address, {encoding: 'json'}) // no maxSupportedTransactionVersion
    .send();

tx1?.version; // type checks as TransactionVersion | undefined, should be type error I think?
tx1?.meta!.loadedAddresses // type checks as {writable: [...], readable: [...]} | undefined, should be type error I think?
tx1?.transaction.message.addressTableLookups; // correctly type error

const tx2 = await rpc
    .getTransaction(address, {encoding: 'json', maxSupportedTransactionVersion: 0}) // with maxSupportedTransactionVersion
    .send();

tx2?.version; // type checks as TransactionVersion | undefined, should be TransactionVersion
tx2?.meta!.loadedAddresses // correctly type checks (can be undefined for legacy tx)
tx2?.transaction.message.addressTableLookups; // missing incorrectly (defined for v0 tx)

Description of bug

  • top-level version field:

    • should not be set if maxSupportedTransactionVersion is unset. But is being added as TransactionVersion | undefined
    • should be set if maxSupportedTransactionVersion is set, and typed as TransactionVersion. But is being added as TransactionVersion | undefined
  • meta loadedAddresses field:

    • should not be set if maxSupportedTransactionVersion is unset. But is being added as {writable: [...], readable: [...]} | undefined
    • should be set if maxSupportedTransactionVersion is set, and typed as {writable: [...], readable: [...]} | undefined (it can be undefined if the requested transaction is legacy). This is working correctly
  • transaction message addressTableLookups field:

    • should not be set if maxSupportedTransactionVersion is unset. This is correct
    • should be set if maxSupportedTransactionVersion is set, and typed as AddressTableLookup[] | undefined (it can be undefined if the requested transaction is legacy). This is not working correctly, it never seems to be defined

mcintyre94 avatar Sep 06 '23 11:09 mcintyre94

Crap. I'll fix this.

steveluscher avatar Sep 06 '23 18:09 steveluscher

is this issue/bug still existing? I wish to work on this.

r0ushann avatar Sep 26 '23 18:09 r0ushann

is this issue/bug still existing? I wish to work on this.

Go right ahead! You can use getBlock as reference if you need!

buffalojoec avatar Sep 26 '23 19:09 buffalojoec

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[bot] avatar Oct 11 '24 08:10 github-actions[bot]