Skip to content

Commit

Permalink
Udpated env example and support retrieving transaction through rpc in…
Browse files Browse the repository at this point in the history
…stead of a hard coded string[]

Signed-off-by: linsyhen99 <linsyhen99@gmail.com>
  • Loading branch information
linsyhen99 committed Oct 29, 2023
1 parent 58a98d9 commit 3774014
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
4 changes: 3 additions & 1 deletion examples/ek-event-parser/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
PROGRAM_HASH=
IDL_TYPE=
IDL_TYPE=
TRANSACTION_HASH=
RPC_URL=
36 changes: 36 additions & 0 deletions examples/ek-event-parser/README.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions examples/ek-event-parser/src/helpers/loadEnvironmentVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
73 changes: 25 additions & 48 deletions examples/ek-event-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3774014

Please sign in to comment.