2

I come from an EVM background and wonder how to listen to Solana events in my dapp such as token/nft deposits/withdrawals to/from a specific user account, creation of new pools, swaps etc.

I tried solanaConnection.onAccountChange() to catch balance changes. but for non-native token balances, I assume the changes are logged with the AccountInfo.data field but this is a hex buffer and I'm not sure how to convert to a meaningful json.

Another way I came across is https://docs.solana.com/api/websocket#accountsubscribe which seems similar to getAccountInfo() but again facing the conversion of data field from base58 encoding to meaningful json.

i have already tried JSON.parse(buf.toString());

1 Answer 1

2

so the easy way is using:

var tokenAccount = new PublicKey("9ursCuiPyhDtD6oNcxPjubev7iSABbqGhvJkMvjncrxE");
getAccount(connection, tokenAccount).then((result) => {
  console.log("getAccountResult: " + JSON.stringify(result));
});

Is you want to parse the data directly when you get it back from onAccountChange web socket connection you can do this:

import { unpackAccount, TOKEN_PROGRAM_ID } from "@solana/spl-token";

        var tokenAccount = new PublicKey("9ursCuiPyhDtD6oNcxPjubev7iSABbqGhvJkMvjncrxE");
connection.onAccountChange(tokenAccount, (account) => {
  var parsedAccount = unpackAccount(tokenAccount, account, TOKEN_PROGRAM_ID);
  console.log("ParsedTokenAccount: " + JSON.stringify(parsedAccount));
}, "confirmed")

This will be triggered every time the token account changes. So when the account increases or decreases for example

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.