{"id":25353589,"url":"https://github.com/immutal0/solana-transaction-parser","last_synced_at":"2026-04-27T20:32:29.441Z","repository":{"id":277262094,"uuid":"931855871","full_name":"Immutal0/solana-transaction-parser","owner":"Immutal0","description":"Parse the Solana transaction for Arbitrage","archived":false,"fork":false,"pushed_at":"2025-02-13T00:55:54.000Z","size":1478,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-09T00:47:36.496Z","etag":null,"topics":["arbitrage","deserializer","instruction","solana","transaction"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Immutal0.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-13T00:55:11.000Z","updated_at":"2025-03-28T19:09:28.000Z","dependencies_parsed_at":"2025-02-13T02:26:28.329Z","dependency_job_id":"93919449-9f55-4d11-aa95-612f901d3e04","html_url":"https://github.com/Immutal0/solana-transaction-parser","commit_stats":null,"previous_names":["immutal0/solana-transaction-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Immutal0%2Fsolana-transaction-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Immutal0%2Fsolana-transaction-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Immutal0%2Fsolana-transaction-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Immutal0%2Fsolana-transaction-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Immutal0","download_url":"https://codeload.github.com/Immutal0/solana-transaction-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247953091,"owners_count":21023945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arbitrage","deserializer","instruction","solana","transaction"],"created_at":"2025-02-14T19:33:47.368Z","updated_at":"2026-04-27T20:32:29.401Z","avatar_url":"https://github.com/Immutal0.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Generated docs\n[Generated module docs](/docs/README.md)\n\n\n## Table of contents\n  * [Installation](#installation)\n  * [What this tool can be used for](#what-this-tool-can-be-used-for)\n  * [How To Use](#how-to-use)\n    + [Parse by IDL](#parse-by-idl)\n    + [Parse with custom parser](#parse-with-custom-parser)\n- [More details](#more-details)\n  * [Instruction deserialization](#instruction-deserialization)\n  * [CPI flattening](#cpi-flattening)\n  * [Parsing transaction logs](#parsing-transaction-logs)\n  * [Using everything together](#using-everything-together)\n\n### Installation\n- Via NPM:\n`npm i @debridge-finance/solana-transaction-parser`\n\n- Via github:\n`npm i git+https://github.com/Myrmekes-a/solana-tx-parser-public`.\n\nThen run `npm i` \n\n### What this tool can be used for\n- Parse solana instructions using anchor IDL/custom parsers.\n- Parse `SystemProgram`, `TokenProgram`, `AssociatedTokenProgram` instructions out of the box.\n- Parse transaction logs.\n- Convert ParsedTransaction/CompiledTransaction/base64-encoded transaction into a list of TransactionInstructions and parse it.\n- Unfold transaction with CPI into artificial transaction with CPI calls included as usual TransactionInstruction.\n\n### How To Use\n#### Parse by IDL\nFirst step: init parser\n```ts\nimport { Idl } from \"@project-serum/anchor\";\nimport { PublicKey, Connection } from \"@solana/web3.js\";\nimport { SolanaParser } from \"@debridge-finance/solana-transaction-parser\";\nimport { IDL as JupiterIdl, Jupiter } from \"./idl/jupiter\"; // idl and types file generated by Anchor\n\nconst rpcConnection = new Connection(\"https://jupiter.genesysgo.net\");\nconst txParser = new SolanaParser([{ idl: JupiterIdl as unknown as Idl, programId: \"JUP2jxvXaqu7NQY1GmNF4m1vodw12LVXYxbFL2uJvfo\" }]);\n```\nSecond step: parse transaction by tx hash:\n```ts\nconst parsed = await txParser.parseTransaction(\n\trpcConnection,\n\t\"5zgvxQjV6BisU8SfahqasBZGfXy5HJ3YxYseMBG7VbR4iypDdtdymvE1jmEMG7G39bdVBaHhLYUHUejSTtuZEpEj\",\n\tfalse,\n);\n```\nVoila! We have a list of TransactionInstruction with instruction names, args and accounts.\n```ts\n// we can find instruction by name\nconst tokenSwapIx = parsed?.find((pix) =\u003e pix.name === \"tokenSwap\");\n// or just use index\nconst setTokenLedgerIx = parsed[0] as ParsedIdlInstruction\u003cJupiter, \"setTokenLedger\"\u003e;\n```\n#### Parse with custom parser\nWhat if the instructions we want to parse do not belong to Anchor program? \nWe could provide custom instruction parser to SolanaParser!\nCustom parser need to have following interface: `(instruction: TransactionInstruction): ParsedCustomInstruction`.\nWe've implemented a small program for testing purposes which can perform two actions: sum up two numbers (passed as u64 numbers) OR\nprint provided data to log (passed as ASCII codes and as a second account in accounts list).\n\n| First byte | Action                                                                           |\n| ---------- | -------------------------------------------------------------------------------- |\n| 0          | Print remaining data as text + string \"From: \" + second account pubkey as base58 |\n| 1          | Sum up two numbers and print the result to log                                   |\n\nParser will be really simple in such case:\n```ts\nfunction customParser(instruction: TransactionInstruction): ParsedCustomInstruction {\n\tlet args: unknown;\n\tlet keys: ParsedAccount[];\n\tlet name: string;\n\tswitch (instruction.data[0]) {\n\t\tcase 0:\n\t\t\targs = { message: instruction.data.slice(1).toString(\"utf8\") };\n\t\t\tkeys = [instruction.keys[0], { name: \"messageFrom\", ...instruction.keys[1] }];\n\t\t\tname = \"echo\";\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\targs = { a: instruction.data.readBigInt64LE(1), b: instruction.data.readBigInt64LE(9) };\n\t\t\tkeys = instruction.keys;\n\t\t\tname = \"sum\";\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new Error(\"unknown instruction!\");\n\t}\n\n\treturn {\n\t\tprogramId: instruction.programId,\n\t\taccounts: keys,\n\t\targs,\n\t\tname,\n\t};\n}\n```\nNow we need to init SolanaParser object (which contains different parsers that can parse instructions from specified programs)\n```ts\nconst parser = new SolanaParser([]);\nparser.addParser(new PublicKey(\"5wZA8owNKtmfWGBc7rocEXBvTBxMtbpVpkivXNKXNuCV\"), customParser);\n```\n\n*UPD: Since solana devnet transactions become unavailable after some time, here's code that can be used to emit test transactions*\n```ts\nimport { Connection, Keypair, PublicKey, Transaction, TransactionInstruction, LAMPORTS_PER_SOL } from \"@solana/web3.js\";\nimport { Wallet } from \"@project-serum/anchor\";\n\nfunction echoIx(msg: string, from: PublicKey) {\n\treturn new TransactionInstruction({\n\t\tprogramId: new PublicKey(\"5wZA8owNKtmfWGBc7rocEXBvTBxMtbpVpkivXNKXNuCV\"),\n\t\tdata: Buffer.concat([Buffer.from([0]), Buffer.from(msg)]),\n\t\tkeys: [\n\t\t\t{ isSigner: true, isWritable: false, pubkey: from },\n\t\t\t{ isSigner: false, isWritable: false, pubkey: new Keypair().publicKey },\n\t\t],\n\t});\n}\n\nfunction addIx(a: bigint, b: bigint, signer: PublicKey) {\n\tconst data: Buffer = Buffer.alloc(2 * 8 + 1);\n\tdata[0] = 1;\n\tdata.writeBigInt64LE(a, 1);\n\tdata.writeBigInt64LE(b, 9);\n\n\treturn new TransactionInstruction({\n\t\tprogramId: new PublicKey(\"5wZA8owNKtmfWGBc7rocEXBvTBxMtbpVpkivXNKXNuCV\"),\n\t\tdata,\n\t\tkeys: [\n\t\t\t{ isSigner: true, isWritable: false, pubkey: signer },\n\t\t\t{ isSigner: false, isWritable: false, pubkey: new Keypair().publicKey },\n\t\t],\n\t});\n}\n\nconst tx = new Transaction().add(echoIx(\"some test msg\", kp.publicKey), addIx(555n, 1234n, kp.publicKey));\n// send tx using devnet connection and your wallet\n```\n\nTo parse transaction which contains this instructions we only need to call `parseTransaction` method on parser object\n```ts\nconst connection = new Connection(clusterApiUrl(\"devnet\"));\nconst parsed = await parser.parseTransaction(connection, \"5xEeZMdrWVG7i8Fbcbu718FtbgSbXsK9c4GPBv21W2e35vP4DdkVghqH4p8dPKdmroUzNe2mBkctm4RAxaVbo78G\");\n// check if no errors was produced during parsing\nif (!parsed) throw new Error(\"failed to get tx/parse!\");\nconsole.log(parsed[0].name); // will print \"echo\"\n```\n\n## More details\n\nMain functions of this project are:\n- instruction deserialization\n- *flattening* transactions with CPI calls\n- parsing transaction logs\n\n### Instruction deserialization\nThis part is pretty simple: we have some `const mapping = Map\u003cstring, Parser\u003e` where keys are program ids and values are parsers for corresponding programId - function that takes TransactionInstruction as input and returns ParsedInstruction (deserialized data, instruction name and accounts meta [with names]).\nWe can deserialize **TransactionInstruction** using Anchor's IDL or custom parser, hence we can deserialize everything which consists of (or can be converted into) **TransactionInstructions**: Transaction, ParsedTransaction, TransactionResponse, ParsedConfirmedTransaction, Message, CompiledMessage, CompiledInstruction or wire transaction, etc.\nSteps of parsing are following:\n1. Convert input into TransactionInstruction, lets name it **ix** (different functions need to be called for different input formats)\n2. Find `ix.programId` in the mapping. If parser exists pass **ix** to it\n3. Check parsed instruction name, set correct data types using generics\n\n### CPI flattening \nFunction: [flattenTransactionResponse](./src/helpers.ts#L87)  \nCan be only done with TransactionResponse/ParsedTransactionWithMeta objects because we need `transaction.meta.innerInstructions` field.\n`transaction.meta.innerInstructions` is a list of objects of following structure: \n```ts\nexport type CompiledInnerInstruction = {\n  index: number, // index of instruction in Transaction object which produced CPI, \n  instructions: CompiledInstruction[]  // ordered list of instructions which were called after instruction with index *index*\n}\n```\nWe create artificial `const result: Transaction = new Transaction();` object which contains all the instructions from TransactionResponse + CPI calls:\nAdd first TransactionInstruction to result, check if CPI calls with index = 0 exist, add them to result, move to the next instruction, repeat.\nFinally, we check that `result.instructions.length === input.instructions.length + total number of CPI instructions`.  \nWe can call index of result.instructions **callId** - index of call in the whole transaction. Same **callId** will be used in the logs part\n\n### Parsing transaction logs \nFunction: [parseLogs](./src/helpers.ts#L143)  \nWorking with Solana's logs is not a trivial task - to determine which program emitted current log line we have to restore call stack, check call depth and set correct **callId** for each log line. parseLogs function implements all that stuff (with call depth and call id checks): \n1. Iterate over logs\n2. Check log type (invoke/return/error/program log/program data) using regex\n3. According to the log type perform action:\n   - Invoke: init and save new context object which contains program id, call depth of instruction, **callId**, index of instruction in Transaction which produced log (depth == 0) or current CPI call (depth != 0), save call stack (push current callId into stack)\n   - Return success/fail: pop caller id from call stack, current callId = popped callId\n   - program log/program data/program consumed: save log into context object with **callId** == current callId\n4. Return list of context objects\n\n### Using everything together\n```ts\nimport { ourIdl } from \"programIdl\";\n\nconst parser = new SolanaParser([{programId: \"someBase58Address\", idl: ourIdl}]);\nconst flattened = flattenTransactionResponse(response);\nconst logs = parseLogs(response.meta.logs || []);\nconst parsed = flattened.map((ix) =\u003e parser.parse(ix));\n\nconst callId = parsed.findIndex( (parsedIx) =\u003e parsedIx.name === \"someInstruction\" );\nif (callId === -1) return Promise.reject(\"instruction not found\");\n\nconst someInstruction = parsed[callId];\nconst someInstructionLogs = logs.find((context) =\u003e context.id === callId);\n\nconst onlyNeededProgramLogs = logs.filter((context) =\u003e context.programId === \"someBase58Address\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimmutal0%2Fsolana-transaction-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimmutal0%2Fsolana-transaction-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimmutal0%2Fsolana-transaction-parser/lists"}