diff --git a/examples/ek-event-parser/.env.example b/examples/ek-event-parser/.env.example index ebecb4c..477129c 100644 --- a/examples/ek-event-parser/.env.example +++ b/examples/ek-event-parser/.env.example @@ -1,2 +1,4 @@ PROGRAM_HASH= -IDL_TYPE= \ No newline at end of file +IDL_TYPE= +TRANSACTION_HASH= +RPC_URL= \ No newline at end of file diff --git a/examples/ek-event-parser/README.md b/examples/ek-event-parser/README.md index 9bb27c7..8624b94 100644 --- a/examples/ek-event-parser/README.md +++ b/examples/ek-event-parser/README.md @@ -1 +1,37 @@ # event-parsing-with-ek + +A sample project that showcases the basic usages and integrations with EK-Parser that decodes the program's event data based on the IDL file + +## About + +- The list of program IDLs identified/indexed/curated by the SolanaFM's team is listed here in this library + + - https://www.npmjs.com/package/@solanafm/ek-idls-repo + +- If your IDL is not part of the identified IDLs list, you can still use the Parser by manually injecting your IDL file into the Parser object + - To manually initialise the Parser with your own IDL file please follow the guide below + +## Populate the project with a .env file to get started + +- Sample env configuration file + +``` +PROGRAM_HASH= +INSTRUCTIONS= +PARSER_TYPE=registry # "manual" | "registry" +STARTING_BLOCK=245000000 +RPC_URL= + +# Loading IDL from local file +IDL_TYPE=shank # "anchor" | "shank" | "kinobi" +``` + +--- + +# Initialising the Parser with a custom IDL file configuration + +1. Ensure that your IDL file is either generated by anchor/shank +2. Create a `idl.json` file in the local directory and populate the json file with your generated IDL file +3. Indicate your `PROGRAM_HASH` in the `.env` file +4. Change your `PARSER_TYPE` to `manual` +5. Indicate your `IDL_TYPE` field in the `.env` file \ No newline at end of file diff --git a/examples/ek-event-parser/src/helpers/loadEnvironmentVariables.ts b/examples/ek-event-parser/src/helpers/loadEnvironmentVariables.ts index 221964e..f0da747 100644 --- a/examples/ek-event-parser/src/helpers/loadEnvironmentVariables.ts +++ b/examples/ek-event-parser/src/helpers/loadEnvironmentVariables.ts @@ -4,14 +4,24 @@ config(); export type Environment = { programHash: string; + transactionHash: string; + rpc_url: string; }; export const getEnvironmentVariables = (): Environment => { if (!process.env.PROGRAM_HASH) { throw new Error("PROGRAM_HASH environment variable is not set"); } + if (!process.env.TRANSACTION_HASH) { + throw new Error("TRANSACTION_HASH environment variable is not set"); + } + if (!process.env.RPC_URL) { + throw new Error("RPC_URL environment variable is not set"); + } return { programHash: process.env.PROGRAM_HASH, + transactionHash: process.env.TRANSACTION_HASH, + rpc_url: process.env.RPC_URL, }; }; diff --git a/examples/ek-event-parser/src/index.ts b/examples/ek-event-parser/src/index.ts index 3473334..2b8ba73 100644 --- a/examples/ek-event-parser/src/index.ts +++ b/examples/ek-event-parser/src/index.ts @@ -2,55 +2,35 @@ import { IdlItem, getProgramIdl } from "@solanafm/explorer-kit-idls"; import { convertLogsToEventsMap } from "./helpers/eventParser"; import { getEnvironmentVariables } from "./helpers/loadEnvironmentVariables"; import fs from "fs"; -import { - EventParserInterface, - Parser, - ParserType, - SolanaFMParser, - checkIfEventParser, -} from "@solanafm/explorer-kit"; +import { EventParserInterface, Parser, ParserType, SolanaFMParser, checkIfEventParser } from "@solanafm/explorer-kit"; +import axios from "axios"; const environment = getEnvironmentVariables(); const app = async () => { - const logs: string[] = [ - "Program ComputeBudget111111111111111111111111111111 invoke [1]", - "Program ComputeBudget111111111111111111111111111111 success", - "Program JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB invoke [1]", - "Program log: Instruction: Route", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc invoke [2]", - "Program log: Instruction: Swap", - "Program log: fee_growth: 1093994082951", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]", - "Program log: Instruction: Transfer", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 1339463 compute units", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]", - "Program log: Instruction: Transfer", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 1331898 compute units", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc consumed 55954 of 1379575 compute units", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc success", - "Program data: UWzjvs3QCsQOA2hfjpCQU+RYEhxm9adq7cdwaqEcgviqlSqPK3h5qcb6evO+2606PWXzaqvJdDGxu+TC0vbg5HymAgNFL11hQEIPAAAAAAAGm4hX/quBhPtof2NGGMA12sQ53BrrO1WYoPAAAAAAARgu5AEAAAAA", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc invoke [2]", - "Program log: Instruction: Swap", - "Program log: fee_growth: 94074010856", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]", - "Program log: Instruction: Transfer", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 1267773 compute units", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]", - "Program log: Instruction: Transfer", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 1260120 compute units", - "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc consumed 54865 of 1306828 compute units", - "Program whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc success", - "Program data: UWzjvs3QCsQOA2hfjpCQU+RYEhxm9adq7cdwaqEcgviqlSqPK3h5qQabiFf+q4GE+2h/Y0YYwDXaxDncGus7VZig8AAAAAABGC7kAQAAAADG+nrzvtutOj1l82qryXQxsbvkwtL24OR8pgIDRS9dYfRFDwAAAAAA", - "Program JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB consumed 150851 of 1400000 compute units", - "Program JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB success", - ]; - const eventMap = convertLogsToEventsMap(logs); const parser = await setupParser(); + + const transactionResponse = await axios.post(environment.rpc_url, { + jsonrpc: "2.0", + id: 1, + method: "getTransaction", + params: [ + environment.transactionHash, + { + encoding: "jsonParsed", + maxSupportedTransactionVersion: 0, + }, + ], + }); + + if (!transactionResponse.data.result) { + throw new Error("Transaction not found"); + } + + const transaction = transactionResponse.data.result; + + const logs: string[] = transaction.meta.logMessages; + const eventMap = convertLogsToEventsMap(logs); const parsedEvents: { [key: string]: any[] } = {}; for (const [prorgamId, eventMapLogs] of eventMap.entries()) { @@ -81,10 +61,7 @@ const setupParser = async () => { eventParser = setupParserFromLocalIdl(); console.log("Successfully loaded parser from local idl file"); } else { - const parser = new SolanaFMParser( - idlItemFromLibrary, - environment.programHash - ); + const parser = new SolanaFMParser(idlItemFromLibrary, environment.programHash); const registryParser = parser.createParser(ParserType.EVENT); if (registryParser && checkIfEventParser(registryParser)) { eventParser = registryParser;