0

Here my code:

const web3 = require('@solana/web3.js');
const connection = new web3.Connection('https://solana-api.projectserum.com');

connection.onAccountChange(
  wallet.publicKey,
  (updatedAccountInfo, context) => {
    let tx = web3.Transaction.from(updatedAccountInfo.data);
    console.log('TX: ', tx);
  },
  'confirmed',
);

When Solana comes to my wallet, or when I send Solana via Solana CLI, the onAccountChange event is triggered, but shows null:

Solana web3.Transaction.from return null

What am I doing wrong and how do I read the transaction data?

Alexandr Kazakov
  • 682
  • 1
  • 7
  • 15

2 Answers2

2

The callback for onAccountChange() returns an AccountInfo not a transaction - So you can read information about the account (Lamports, Owner, ...), but not the info on the transaction that triggered the change. Typescript will help datatype resolution.

https://solana-labs.github.io/solana-web3.js/classes/Connection.html#onAccountChange

Coach Chuck
  • 106
  • 1
  • 3
  • Thank you! But how do I subscribe to transactions then? I want to subscribe to all wallet transactions so that I can get data from them. – Alexandr Kazakov May 10 '22 at 13:53
0

You could use onAccountChange to listen for events when account changes in any way. In the callback that you've provided to the onAccountChange, you can call getConfirmedSignaturesForAddress2 to get transactions signatures and later on you can call getTransactions by providing signatures you've received in previous step.