In web3js@1 we could use connection.getTokenAccountsByOwner()
to get the balance details for tokens in a wallet.
In web3js@2 we have getTokenAccountsByOwner function in the rpc object, but I'm failing to use it, and there seems to be no examples anywhere. I've tried:
await rpc.getTokenAccountsByOwner(wallet.address, {
programId: TOKEN_PROGRAM_ID,
}).send()
with TOKEN_PROGRAM_ID = address('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
, but I'm receiving this error:
SolanaError: JSON-RPC error: The JSON sent is not a valid `Request` object (Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.)
So, does anyone have a working example of getting a token balance of a wallet in web3js v2?