kevinheavey / solders

A high-performance Python toolkit for Solana, written in Rust
https://kevinheavey.github.io/solders/
Apache License 2.0
205 stars 23 forks source link

Types for Structs with Serde Field Attributes #40

Closed thekeviv closed 1 year ago

thekeviv commented 1 year ago

Hi, I am trying to port over some code from Typescript to Python and I want to use the types provided by this library for it (the Solana RPC response types). In web3.js, there is a ParsedTransactionWithMeta type which is used in methods like getParsedTransaction. This is the getTransaction rpc method with encoding set to jsonParsed. I wanted a type corresponding to it in Python.

This library has a GetTransactionResp type however that is a bit different from the data I get back.

I took a look at the Solana code which that method calls to fetch data. The type for the data returned for jsonParsed encoding is EncodedConfirmedTransactionWithStatusMeta as also in the value param in the library here. However, that Rust struct has #[serde(flatten)] field attribute on transaction and so in the data returned from rpc (or web3), that type gets flattened. This unfortunately leads to a difference between the type provided in this library and the one corresponding to the data. I think I've seem code in the past here where they rename certain fields too so this could have impact there too.

Can I do anything about this? Any help will be very appreciated.

Thank you for your hard work!

kevinheavey commented 1 year ago

Do you have a reproducible example?

thekeviv commented 1 year ago

Hi, here's a quick example I wrote to show this:

  1. First I make a call using the regular RPC API and I get this response. This is the same output I get from the web3.js library in Typescript too.

    Screenshot 2023-01-27 at 11 37 53 AM
  2. Then I make the same call using your Solana library and notice how the output is slightly different despite passing the same params. In the output, transaction isn't flattened with meta, version and transaction being nested inside another transaction key.

    Screenshot 2023-01-27 at 11 11 51 AM

I was expecting similar outputs between RPC, Web3.js and the Python library.

In web3.js, what they have done is to create 2 different methods, 1 for getTransaction which runs with regular encoding and returns a transaction like it did in 2. Then there's a getParsedTransaction which returns it in a format similar to 1 (with the parsing applied).

kevinheavey commented 1 year ago

Ah I see, thanks. We parse the getTransaction response the same way the Solana Rust client does. This is really about solana_client and web3.js differing.

thekeviv commented 1 year ago

I see. I think I'll create a type myself with the flattened structure reusing the other types provided by solders. I'll close this issue accordingly.

Thank you for your response.