ts: Check if execution context stack is empty in event parser (#524)

This commit is contained in:
aac 2021-07-17 02:56:49 +10:00 committed by GitHub
parent a65665f476
commit ab49478259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 2 deletions

View File

@ -44,6 +44,7 @@ jobs:
- cargo fmt -- --check
- cargo clippy --all-targets -- -D warnings
- cargo test
- pushd ts && yarn && yarn test && popd
- <<: *examples
name: Runs the examples 1
script:

View File

@ -18,7 +18,8 @@
"lint:fix": "prettier src/** -w",
"watch": "tsc -p tsconfig.cjs.json --watch",
"prepublishOnly": "yarn build",
"docs": "typedoc --excludePrivate --includeVersion --out ../docs/src/.vuepress/dist/ts/ --readme none src/index.ts"
"docs": "typedoc --excludePrivate --includeVersion --out ../docs/src/.vuepress/dist/ts/ --readme none src/index.ts",
"test": "jest tests --detectOpenHandles"
},
"dependencies": {
"@project-serum/borsh": "^0.2.2",

View File

@ -58,7 +58,7 @@ export class EventParser {
log: string
): [Event | null, string | null, boolean] {
// Executing program is this program.
if (execution.program() === this.programId.toString()) {
if (execution.stack.length > 0 && execution.program() === this.programId.toString()) {
return this.handleProgramLog(log);
}
// Executing program is not this program.

33
ts/tests/events.spec.ts Normal file
View File

@ -0,0 +1,33 @@
import { PublicKey } from "@solana/web3.js";
import { EventParser } from "../src/program/event";
import { Coder } from "../src";
describe("Events", () => {
it("Parses multiple instructions", async () => {
const logs = [
"Program 11111111111111111111111111111111 invoke [1]",
"Program 11111111111111111111111111111111 success",
"Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
"Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 17867 of 200000 compute units",
"Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
];
const idl = {
version: "0.0.0",
name: "basic_0",
instructions: [
{
name: "initialize",
accounts: [],
args: [],
},
],
};
const coder = new Coder(idl);
const programId = PublicKey.default;
const eventParser = new EventParser(coder, programId);
eventParser.parseLogs(logs, () => {
throw new Error("Should never find logs");
});
});
});