Search code examples
typescriptsolanasolana-web3jsmetaplex

Solana web3.js parse onProgramAccountChange notifications to JSON


How can I parse the connection.onProgramAccountChange notifications data to JSON?

https://solana-labs.github.io/solana-web3.js/modules.html#AccountInfo

The docs specify the data as type T, which for me has usually been a Buffer.

Example code:

let progKey = new PublicKey("<program key here>");
conn.onProgramAccountChange(progKey, programCallback);
function programCallback(keyedAccountInfo: KeyedAccountInfo, context: Context) {
  let data = keyedAccountInfo.accountInfo.data.toString("hex");
  let ownerId = keyedAccountInfo.accountInfo.owner.toBase58();
  let accId = keyedAccountInfo.accountId.toBase58();
  console.log(`======
  owner: ${ownerId}
  accId: ${accId}
  data: ${data}`);
}

Is the data type literally just a buffer in this specific instance, or is there something extra that I need to do to decode it? Decoding it to hex, utf8, base64, and base58 don't work.


Solution

  • Even though this is programSubscribe, the notification format is the same as getProgramAccounts. Hence, the answer can be found in my other answer: https://stackoverflow.com/a/71028298/7841421