1

Is there a standard for how anchor event logs are displayed in the program logs? There seems to be no standard prefix for me to get the base64 encoded string (event). I'm trying to dynamically extract logs but it seems near impossible to pull out the logs that way as every program has a different prefix. e.g. Program logged: {base64 string} and Program data: {base64 string}

1 Answer 1

2

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);
}

Taken from How to parse event in transaction log with Anchor

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.