ts: Provide event names when decoding log strs (#297)

This commit is contained in:
Armani Ferrante 2021-05-20 13:14:46 -07:00 committed by GitHub
parent 24b723e1e1
commit 3a9d05991e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import {
IdlStateMethod,
} from "./idl";
import { IdlError } from "./error";
import { Event } from "./program/event";
/**
* Number of bytes of the account discriminator.
@ -249,18 +250,19 @@ class EventCoder {
);
}
public decode<T = any>(log: string): T | null {
public decode(log: string): Event | null {
const logArr = Buffer.from(base64.toByteArray(log));
const disc = base64.fromByteArray(logArr.slice(0, 8));
// Only deserialize if the discriminator implies a proper event.
const eventName = this.discriminators.get(disc);
if (eventName === undefined) {
return undefined;
return null;
}
const layout = this.layouts.get(eventName);
return layout.decode(logArr.slice(8));
const data = layout.decode(logArr.slice(8));
return { data, name: eventName };
}
}

View File

@ -1,5 +1,4 @@
import { PublicKey } from "@solana/web3.js";
import * as base64 from "base64-js";
import * as assert from "assert";
import Coder from "../coder";

View File

@ -38,7 +38,7 @@ export default class SimulateFactory {
): SimulateFn {
const simulate = async (...args: any[]): Promise<SimulateResponse> => {
const tx = txFn(...args);
const [_, ctx] = splitArgsAndCtx(idlIx, [...args]);
const [, ctx] = splitArgsAndCtx(idlIx, [...args]);
let resp = undefined;
try {
resp = await provider.simulate(tx, ctx.signers, ctx.options);