123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import { PublicKey } from "@solana/web3.js";
- import { EventParser } from "../src/program/event";
- import { BorshCoder } from "../src";
- describe("Events", () => {
- it("Parses multiple instructions", () => {
- 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 BorshCoder(idl);
- const programId = PublicKey.default;
- const eventParser = new EventParser(programId, coder);
- eventParser.parseLogs(logs, () => {
- throw new Error("Should never find logs");
- });
- });
- it("Upgrade event check", () => {
- const logs = [
- "Upgraded program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54",
- "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 BorshCoder(idl);
- const programId = PublicKey.default;
- const eventParser = new EventParser(programId, coder);
- eventParser.parseLogs(logs, () => {
- throw new Error("Should never find logs");
- });
- });
- it("Find event with different start log.", (done) => {
- const logs = [
- "Upgraded program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54",
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
- "Program log: Instruction: BuyNft",
- "Program 11111111111111111111111111111111 invoke [2]",
- "Program log: UhUxVlc2hGeTBjNPCGmmZjvNSuBOYpfpRPJLfJmTLZueJAmbgEtIMGl9lLKKH6YKy1AQd8lrsdJPPc7joZ6kCkEKlNLKhbUv",
- "Program 11111111111111111111111111111111 success",
- "Program 11111111111111111111111111111111 invoke [2]",
- "Program 11111111111111111111111111111111 success",
- "Program 11111111111111111111111111111111 invoke [2]",
- "Program 11111111111111111111111111111111 success",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: Transfer",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 141128 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: CloseAccount",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 135127 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program log: UhUxVlc2hGeTBjNPCGmmZjvNSuBOYpfpRPJLfJmTLZueJAmbgEtIMGl9lLKKH6YKy1AQd8lrsdJPPc7joZ6kCkEKlNLKhbUv",
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 73106 of 200000 compute units",
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
- ];
- const idl = {
- version: "0.0.0",
- name: "basic_1",
- instructions: [
- {
- name: "initialize",
- accounts: [],
- args: [],
- },
- ],
- events: [
- {
- name: "NftSold",
- fields: [
- {
- name: "nftMintAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- {
- name: "accountAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- ],
- },
- ],
- };
- const coder = new BorshCoder(idl);
- const programId = new PublicKey(
- "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
- );
- const eventParser = new EventParser(programId, coder);
- eventParser.parseLogs(logs, (event) => {
- expect(event.name).toEqual("NftSold");
- done();
- });
- });
- it("Find event from logs", (done) => {
- const logs = [
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
- "Program log: Instruction: CancelListing",
- "Program log: TRANSFERED SOME TOKENS",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: Transfer",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program log: TRANSFERED SOME TOKENS",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: CloseAccount",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 31435 of 200000 compute units",
- "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
- ];
- const idl = {
- version: "0.0.0",
- name: "basic_2",
- instructions: [
- {
- name: "cancelListing",
- accounts: [
- {
- name: "globalState",
- isMut: true,
- isSigner: false,
- },
- {
- name: "nftHolderAccount",
- isMut: true,
- isSigner: false,
- },
- {
- name: "listingAccount",
- isMut: true,
- isSigner: false,
- },
- {
- name: "nftAssociatedAccount",
- isMut: true,
- isSigner: false,
- },
- {
- name: "signer",
- isMut: true,
- isSigner: true,
- },
- {
- name: "tokenProgram",
- isMut: false,
- isSigner: false,
- },
- ],
- args: [],
- },
- ],
- events: [
- {
- name: "ListingClosed",
- fields: [
- {
- name: "initializer",
- type: "publicKey" as "publicKey",
- index: false,
- },
- {
- name: "nftMintAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- {
- name: "accountAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- ],
- },
- ],
- };
- const coder = new BorshCoder(idl);
- const programId = new PublicKey(
- "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
- );
- const eventParser = new EventParser(programId, coder);
- eventParser.parseLogs(logs, (event) => {
- expect(event.name).toEqual("ListingClosed");
- done();
- });
- });
- it("Listen to different program and send other program logs with same name", () => {
- const logs = [
- "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP invoke [1]",
- "Program log: Instruction: CancelListing",
- "Program log: TRANSFERED SOME TOKENS",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: Transfer",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program log: TRANSFERED SOME TOKENS",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
- "Program log: Instruction: CloseAccount",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
- "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
- "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
- "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP consumed 31435 of 200000 compute units",
- "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP success",
- ];
- const idl = {
- version: "0.0.0",
- name: "basic_2",
- instructions: [],
- events: [
- {
- name: "ListingClosed",
- fields: [
- {
- name: "initializer",
- type: "publicKey" as "publicKey",
- index: false,
- },
- {
- name: "nftMintAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- {
- name: "accountAddress",
- type: "publicKey" as "publicKey",
- index: false,
- },
- ],
- },
- ],
- };
- const coder = new BorshCoder(idl);
- const programId = new PublicKey(
- "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
- );
- const eventParser = new EventParser(programId, coder);
- eventParser.parseLogs(logs, () => {
- throw new Error("Should never find logs");
- });
- });
- });
|