1

EVM based blockchains offer eth_getLogs() RPC call and provide filters (topics argument in eth_getLogs function call) to filter out the results.

Is there an equivalent in Solana RPC calls?

I am using getSignaturesForAddress() RPC call along with getTransactions() RPC to get all transaction info related to an address.

I want to filter transactions based upon which instruction handler is called from the Solana program.

1 Answer 1

1

There is no way to filter transactions based upon which function is called on a given program. The best you can do is to subscribe to logs involving a program, then to filter the logs for any mention of your target instruction.

const PROGRAM_ADDRESS = '...';
const FOREIGN_PROGRAM_INVOKE_LOG_REGEX = new RegExp(
  `^Program ((?!${PROGRAM_ADDRESS})\\w)+ invoke \\[\\d+\\]$`,
);
const TARGET_PROGRAM_INVOKE_LOG_REGEX = new RegExp(
  `^Program ${PROGRAM_ADDRESS} invoke \\[\\d+\\]$`,
);
connection.onLogs(new PublicKey(PROGRAM_ADDRESS), ({logs}) => {
  let inTargetProgramLogSection = false;
  for (const line in logs) {
    if (inTargetProgramLogSection) {
      if (FOREIGN_PROGRAM_INVOKE_LOG_REGEX.test(line)) {
        inTargetProgramLogSection = false;
      } else if (line === 'Program log: Instruction: MyInstruction') {
        // Found the target instruction. Run your code here.
        break;
      }
    } else if (TARGET_PROGRAM_INVOKE_LOG_REGEX.test(line)) {
      inTargetProgramLogSection === true;
    }
  }
});

Note that this is predicated upon the program outputting a log when your target instruction is invoked.

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.