Skip to content

Commit

Permalink
Add optional flag to parseLogs to throw an error on decoding failure (#…
Browse files Browse the repository at this point in the history
…2043)

* Add optional flag to parseLogs to throw an error on decoding failure

* update changelog

Co-authored-by: henrye <henry@notanemail>
  • Loading branch information
nicholasgodfreyclarke and henrye committed Dec 5, 2022
1 parent fb714b9 commit 66e4532
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Updates `AccountsClose` to make it safe to call manually ([#2209](https://github.com/coral-xyz/anchor/pull/2209))
- lang: Update rust used in the repo version 1.62 ([#2272](https://github.com/coral-xyz/anchor/pull/2272))
- cli: Allow custom cluster config ([#2271](https://github.com/coral-xyz/anchor/pull/2271)).
- ts: Add optional flag to parseLogs to throw an error on decoding failure ([#2043](https://github.com/coral-xyz/anchor/pull/2043))

### Fixes

Expand Down
20 changes: 15 additions & 5 deletions ts/packages/anchor/src/program/event.ts
Expand Up @@ -176,12 +176,16 @@ export class EventParser {
// its emission, thereby allowing us to know if a given log event was
// emitted by *this* program. If it was, then we parse the raw string and
// emit the event if the string matches the event being subscribed to.
public *parseLogs(logs: string[]) {
public *parseLogs(logs: string[], errorOnDecodeFailure: boolean = false) {
const logScanner = new LogScanner(logs);
const execution = new ExecutionContext();
let log = logScanner.next();
while (log !== null) {
let [event, newProgram, didPop] = this.handleLog(execution, log);
let [event, newProgram, didPop] = this.handleLog(
execution,
log,
errorOnDecodeFailure
);
if (event) {
yield event;
}
Expand All @@ -201,14 +205,15 @@ export class EventParser {
// execution stack).
private handleLog(
execution: ExecutionContext,
log: string
log: string,
errorOnDecodeFailure: boolean
): [Event | null, string | null, boolean] {
// Executing program is this program.
if (
execution.stack.length > 0 &&
execution.program() === this.programId.toString()
) {
return this.handleProgramLog(log);
return this.handleProgramLog(log, errorOnDecodeFailure);
}
// Executing program is not this program.
else {
Expand All @@ -218,14 +223,19 @@ export class EventParser {

// Handles logs from *this* program.
private handleProgramLog(
log: string
log: string,
errorOnDecodeFailure: boolean
): [Event | null, string | null, boolean] {
// This is a `msg!` log or a `sol_log_data` log.
if (log.startsWith(PROGRAM_LOG) || log.startsWith(PROGRAM_DATA)) {
const logStr = log.startsWith(PROGRAM_LOG)
? log.slice(PROGRAM_LOG_START_INDEX)
: log.slice(PROGRAM_DATA_START_INDEX);
const event = this.coder.events.decode(logStr);

if (errorOnDecodeFailure && event === null) {
throw new Error(`Unable to decode event ${logStr}`);
}
return [event, null, false];
}
// System log.
Expand Down

1 comment on commit 66e4532

@vercel
Copy link

@vercel vercel bot commented on 66e4532 Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com
anchor-docs-git-master-200ms.vercel.app

Please sign in to comment.