Browse Source

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

Armani Ferrante 4 years ago
parent
commit
3a9d05991e
3 changed files with 6 additions and 5 deletions
  1. 5 3
      ts/src/coder.ts
  2. 0 1
      ts/src/program/event.ts
  3. 1 1
      ts/src/program/namespace/simulate.ts

+ 5 - 3
ts/src/coder.ts

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

+ 0 - 1
ts/src/program/event.ts

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

+ 1 - 1
ts/src/program/namespace/simulate.ts

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