3

Currently, I can do this using a cURL request:

 curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
  {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getMultipleAccounts",
    "params": [
      [
        "AWaJ4oXHWXaNnFJirydYdXdD3Yjmkgh6rYhmwAyB5d8o",
        "yT2bBMrE4AVG3BM8LHjDSr1yketzabmfp3rYimS36C9"
      ],
      {
        "encoding": "jsonParsed"
      }
    ]
  }
'

This will return a response that contains JSON parsed data that is human readable:

{
  "jsonrpc": "2.0",
  "result": {
    "context": {
      "apiVersion": "1.10.29",
      "slot": 150852465
    },
    "value": [
      {
        "data": {
          "parsed": {
            "info": {
              "isNative": false,
              "mint": "4koajw4GkCgVNEjN14xmjCtMVYNASdDj7RtrAA4SZwnK",
              "owner": "GkeDRfHcACap2CM9oaWrb3QUMG7pk6F6EMYca8Lu52t8",
              "state": "initialized",
              "tokenAmount": {
                "amount": "12998020",
                "decimals": 6,
                "uiAmount": 12.99802,
                "uiAmountString": "12.99802"
              }
            },
            "type": "account"
          },
          "program": "spl-token",
          "space": 165
        },
        "executable": false,
        "lamports": 2039280,
        "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
        "rentEpoch": 348
      }
    ]
  },
  "id": 1
}

I want to use the same method by using the @solana/web3.js library:

const accounts = await connection.getMultipleAccountsInfo([new PublicKey("AWaJ4oXHWXaNnFJirydYdXdD3Yjmkgh6rYhmwAyB5d8o"), new PublicKey("yT2bBMrE4AVG3BM8LHjDSr1yketzabmfp3rYimS36C9")]);

This doesn't return JSON parsed account data, but it's using Buffer. I can use the method connection.getParsedAccountInfo() to get the JSON parsed data and it works great, but it's not efficient. I will be parsing multiple token balances from 500+ accounts. Is it possible to convert the Buffer structure to parse token balance from it? I would need the uiAmountString:

enter image description here

Everything works perfectly fine by using a cURL request, but the web3.js library doesn't seem to have this implemented?

2 Answers 2

3

It doesn't look like the web3.js implementation of getMultipleAccounts supports parsed encoding replies. You can either open a feature request issue against the library in the monorepo on github (assuming one doesn't already exist) or better yet, implement it yourself and submit a PR.

This could be implemented either by adding a new encoding field to the GetMultipleAccountsConfig structure or by adding a new method akin to getParsedAccountInfo (as getParsedMultipleAccountsInfo ?)

1

For token accounts specifically, if all the token accounts you want to fetch are owned by the same pubkey, you can use getParsedTokenAccountsByOwner.

Otherwise, I believe web3.js is just a thin wrapper around the JSON RPC API so in the worst case you can probably implement the same thing that you did in CURL in js.

1
  • Yeah, for now I've just used a good old web request for this, it's unfortunate that it's not supported yet.
    – Peroxy
    Jul 28, 2022 at 13:40

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.