ts: Provide event names when decoding log strs (#297)
This commit is contained in:
parent
24b723e1e1
commit
3a9d05991e
|
@ -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 };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue