4

I have following transaction logs which contains triggered event and I want to parse it:

'Program log: Instruction: Init',
'Program data: 867fyqeFJJGYZDSk+Vy/7Cpm35d+fps/e3WovaZGp4v+BTlIzV0qngEAAGXNHQAAAAA=',
'Program CBVe4r7UuJtMiBtTUXH8ic7dxqSvNcUWBNX25c2dVAYg consumed 6522 of 571982 compute units',
'Program CBVe4r7UuJtMiBtTUXH8ic7dxqSvNcUWBNX25c2dVAYg success'

This is as far as I get get with parsing:

const tx = await anchor.getProvider().connection.getTransaction(signature, {
            commitment: "confirmed",
        });

const log = tx.meta.logMessages[1];
const message = log.substring(log.indexOf(':')+1);
consle.log(message);

Notes:

  • Event Logs are generated with emit! (which use msq! under hood and it's just a convenience macro for converting an arbitrary struct to a string).

2 Answers 2

8

After some digging to Anchor documentation I found the EventParser which is exactly what I wanted.

import * as anchor from "@project-serum/anchor";
import {BorshCoder, EventParser, Program, web3} from "@project-serum/anchor";
import {Tcf} from "../target/types/tcf";


anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.CoinFlip as Program<Tcf>;

// Get transaction from its signature
const tx = await anchor.getProvider().connection.getTransaction(signature, {
     commitment: "confirmed",
});

const eventParser = new EventParser(program.programId, new BorshCoder(program.idl));
const events = eventParser.parseLogs(tx.meta.logMessages);
for (let event of events) {
    console.log(event);
}
1
  • is there an equivalent for parsing logs via rust?
    – AceGravity
    Jan 30 at 15:12
0

I was struggling with the accepted answer to parse Jupiter perp log events, then dug into how they parse logs: https://github.com/jup-ag/instruction-parser/blob/main/src/lib/get-events.ts

I am not very familiar with solana ecosystem, this might be an alternative way of storing logs by passing it as instruction (?)

Copying the parsing code here in case they make repo private:

    import { Event, Program, utils } from "@coral-xyz/anchor";
    import { JUPITER_V6_PROGRAM_ID } from "../constants";
    import { TransactionWithMeta } from "../types";

    export function getEvents(
      program: Program,
      transactionResponse: TransactionWithMeta
    ) {
      let events: Event[] = [];

      if (transactionResponse && transactionResponse.meta) {
        let { meta } = transactionResponse;

        meta.innerInstructions?.map(async (ix) => {
          ix.instructions.map(async (iix) => {
            if (!iix.programId.equals(JUPITER_V6_PROGRAM_ID)) return;
            if (!("data" in iix)) return; // Guard in case it is a parsed decoded instruction

            const ixData = utils.bytes.bs58.decode(iix.data);
            const eventData = utils.bytes.base64.encode(ixData.subarray(8));
            const event = program.coder.events.decode(eventData);

            if (!event) return;

            events.push(event);
          });
        });
      }

      return events;
    }

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.