3

I'm experimenting with the RPC methods using websockets from solana.py.

I didn't find a documentation describing precisely all fields in reply. As an example, AccountSubscribe methods should give the following (source):

{
  "jsonrpc": "2.0",
  "method": "accountNotification",
  "params": {
    "result": {
      "context": {
        "slot": 5199307
      },
      "value": {
        "data": [
          "11116bv5nS2h3y12kD1yUKeMZvGcKLSjQgX6BeV7u1FrjeJcKfsHPXHRDEHrBesJhZyqnnq9qJeUuF7WHxiuLuL5twc38w2TXNLxnDbjmuR",
          "base58"
        ],
        "executable": false,
        "lamports": 33594,
        "owner": "11111111111111111111111111111111",
        "rentEpoch": 635,
        "space": 80
      }
    },
    "subscription": 23784
  }
}

For example, the field data is encoded in base58. But the decoded data is not a string.

What is the format of the decoded data field ? How to decode (after base58 decoding) it ? How to interpret it ?

The same documentation says the notification format is the same as getAccountInfo. And getAccountInfo documentation says this:

data: <[string, encoding]|object> - data associated with the account, either as encoded binary data or JSON format {<program>: <state>} - depending on encoding parameter

Any clue ?

1 Answer 1

3

Depends on what data is in the account.

You will get the accountInfo, which has the sol balance, data etc and then you need to figure out how to decode the data in there which depends on the account type. Then you need to decode it depending on the type.

connection.onAccountChange(
      PDAaccountAddress,
      (accountInfo) => {
        // Account info type which has data and lamports and program id etc 

      }

For token accounts this would be this for example:

  const tokenAccountInfo = AccountLayout.decode(e.account.data);

For anchor programs you can use the program.decoder something like this:

connection.onAccountChange(pda, (account) => {
   var gameData program.coder.accounts.decode("playerData", account.data);
});
2
  • 1
    Thank for your answer. I'm using Python with WebSocket. With your input I figured out that the encoding is using borsh with a specific schema layout. For AccountSubscribe case the documentation can be found here . However, trying that example give me that error: construct.core.StreamError: Error in path (parsing) -> map -> items stream read less than specified amount, expected 1785345379, found 99 with the data field from AccountSubscribe (its in base64). Any idea ?
    – microchip
    Commented May 31 at 10:19
  • 1
    I found the solution. The data field for AccountSubscribe is a number of sol own by the account. So the layout for the borsh is just one field U8.
    – microchip
    Commented May 31 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.