@oobe-protocol-labs/synapse-sap-sdk 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/cjs/core/client.js +23 -0
  2. package/dist/cjs/core/client.js.map +1 -1
  3. package/dist/cjs/core/connection.js +37 -6
  4. package/dist/cjs/core/connection.js.map +1 -1
  5. package/dist/cjs/core/index.js +2 -1
  6. package/dist/cjs/core/index.js.map +1 -1
  7. package/dist/cjs/index.js +17 -2
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/parser/client.js +146 -0
  10. package/dist/cjs/parser/client.js.map +1 -0
  11. package/dist/cjs/parser/complete.js +177 -0
  12. package/dist/cjs/parser/complete.js.map +1 -0
  13. package/dist/cjs/parser/index.js +57 -0
  14. package/dist/cjs/parser/index.js.map +1 -0
  15. package/dist/cjs/parser/inner.js +185 -0
  16. package/dist/cjs/parser/inner.js.map +1 -0
  17. package/dist/cjs/parser/instructions.js +114 -0
  18. package/dist/cjs/parser/instructions.js.map +1 -0
  19. package/dist/cjs/parser/transaction.js +153 -0
  20. package/dist/cjs/parser/transaction.js.map +1 -0
  21. package/dist/cjs/parser/types.js +14 -0
  22. package/dist/cjs/parser/types.js.map +1 -0
  23. package/dist/esm/core/client.js +23 -0
  24. package/dist/esm/core/client.js.map +1 -1
  25. package/dist/esm/core/connection.js +36 -6
  26. package/dist/esm/core/connection.js.map +1 -1
  27. package/dist/esm/core/index.js +1 -1
  28. package/dist/esm/core/index.js.map +1 -1
  29. package/dist/esm/index.js +4 -1
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/parser/client.js +142 -0
  32. package/dist/esm/parser/client.js.map +1 -0
  33. package/dist/esm/parser/complete.js +173 -0
  34. package/dist/esm/parser/complete.js.map +1 -0
  35. package/dist/esm/parser/index.js +43 -0
  36. package/dist/esm/parser/index.js.map +1 -0
  37. package/dist/esm/parser/inner.js +180 -0
  38. package/dist/esm/parser/inner.js.map +1 -0
  39. package/dist/esm/parser/instructions.js +109 -0
  40. package/dist/esm/parser/instructions.js.map +1 -0
  41. package/dist/esm/parser/transaction.js +149 -0
  42. package/dist/esm/parser/transaction.js.map +1 -0
  43. package/dist/esm/parser/types.js +13 -0
  44. package/dist/esm/parser/types.js.map +1 -0
  45. package/dist/types/core/client.d.ts +19 -0
  46. package/dist/types/core/client.d.ts.map +1 -1
  47. package/dist/types/core/connection.d.ts +32 -6
  48. package/dist/types/core/connection.d.ts.map +1 -1
  49. package/dist/types/core/index.d.ts +2 -2
  50. package/dist/types/core/index.d.ts.map +1 -1
  51. package/dist/types/index.d.ts +5 -2
  52. package/dist/types/index.d.ts.map +1 -1
  53. package/dist/types/parser/client.d.ts +123 -0
  54. package/dist/types/parser/client.d.ts.map +1 -0
  55. package/dist/types/parser/complete.d.ts +90 -0
  56. package/dist/types/parser/complete.d.ts.map +1 -0
  57. package/dist/types/parser/index.d.ts +40 -0
  58. package/dist/types/parser/index.d.ts.map +1 -0
  59. package/dist/types/parser/inner.d.ts +114 -0
  60. package/dist/types/parser/inner.d.ts.map +1 -0
  61. package/dist/types/parser/instructions.d.ts +76 -0
  62. package/dist/types/parser/instructions.d.ts.map +1 -0
  63. package/dist/types/parser/transaction.d.ts +77 -0
  64. package/dist/types/parser/transaction.d.ts.map +1 -0
  65. package/dist/types/parser/types.d.ts +154 -0
  66. package/dist/types/parser/types.d.ts.map +1 -0
  67. package/package.json +6 -1
  68. package/src/core/client.ts +25 -0
  69. package/src/core/connection.ts +59 -7
  70. package/src/core/index.ts +2 -2
  71. package/src/index.ts +27 -2
  72. package/src/parser/client.ts +211 -0
  73. package/src/parser/complete.ts +232 -0
  74. package/src/parser/index.ts +71 -0
  75. package/src/parser/inner.ts +255 -0
  76. package/src/parser/instructions.ts +135 -0
  77. package/src/parser/transaction.ts +200 -0
  78. package/src/parser/types.ts +182 -0
@@ -31,9 +31,11 @@ import {
31
31
  type Commitment,
32
32
  Keypair,
33
33
  type PublicKey,
34
+ type Transaction,
35
+ type VersionedTransaction,
34
36
  LAMPORTS_PER_SOL,
35
37
  } from "@solana/web3.js";
36
- import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
38
+ import { AnchorProvider } from "@coral-xyz/anchor";
37
39
  import {
38
40
  SAP_PROGRAM_ID,
39
41
  MAINNET_SAP_PROGRAM_ID,
@@ -42,6 +44,56 @@ import {
42
44
  } from "../constants";
43
45
  import { SapClient } from "./client";
44
46
 
47
+ // ═══════════════════════════════════════════════════════════════════
48
+ // Wallet interface (replaces Anchor's Wallet which is not
49
+ // exported from the ESM bundle of @coral-xyz/anchor)
50
+ // ═══════════════════════════════════════════════════════════════════
51
+
52
+ /**
53
+ * @interface SapWallet
54
+ * @description Minimal wallet/signer interface compatible with
55
+ * Anchor's `AnchorProvider`. Avoids importing `Wallet` from
56
+ * `@coral-xyz/anchor` which is absent in ESM builds.
57
+ * @category Core
58
+ * @since v0.4.1
59
+ */
60
+ export interface SapWallet {
61
+ readonly publicKey: PublicKey;
62
+ signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
63
+ signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]>;
64
+ }
65
+
66
+ /**
67
+ * @name KeypairWallet
68
+ * @description Simple wallet wrapper around a `Keypair`.
69
+ * Drop-in replacement for Anchor's `NodeWallet` / `Wallet` class.
70
+ * @category Core
71
+ * @since v0.4.1
72
+ */
73
+ export class KeypairWallet implements SapWallet {
74
+ readonly publicKey: PublicKey;
75
+
76
+ constructor(readonly payer: Keypair) {
77
+ this.publicKey = payer.publicKey;
78
+ }
79
+
80
+ async signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T> {
81
+ if ("partialSign" in tx) {
82
+ (tx as Transaction).partialSign(this.payer);
83
+ } else {
84
+ (tx as VersionedTransaction).sign([this.payer]);
85
+ }
86
+ return tx;
87
+ }
88
+
89
+ async signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> {
90
+ for (const tx of txs) {
91
+ await this.signTransaction(tx);
92
+ }
93
+ return txs;
94
+ }
95
+ }
96
+
45
97
  // ═══════════════════════════════════════════════════════════════════
46
98
  // Types
47
99
  // ═══════════════════════════════════════════════════════════════════
@@ -261,7 +313,7 @@ export class SapConnection {
261
313
  commitment: opts?.commitment,
262
314
  cluster: opts?.cluster,
263
315
  });
264
- const client = conn.createClient(new Wallet(keypair));
316
+ const client = conn.createClient(new KeypairWallet(keypair));
265
317
  return Object.assign(conn, { client });
266
318
  }
267
319
 
@@ -270,18 +322,18 @@ export class SapConnection {
270
322
  // ─────────────────────────────────────────────
271
323
 
272
324
  /**
273
- * Create a {@link SapClient} from an Anchor {@link Wallet} (signer).
325
+ * Create a {@link SapClient} from a {@link SapWallet} (signer).
274
326
  *
275
- * @param {Wallet} wallet — An Anchor-compatible wallet/signer.
327
+ * @param {SapWallet} wallet — A wallet/signer implementing {@link SapWallet}.
276
328
  * @returns {SapClient} A fully-configured SAP client.
277
329
  * @since v0.1.0
278
330
  *
279
331
  * @example
280
332
  * ```ts
281
- * const client = conn.createClient(new Wallet(keypair));
333
+ * const client = conn.createClient(new KeypairWallet(keypair));
282
334
  * ```
283
335
  */
284
- createClient(wallet: Wallet): SapClient {
336
+ createClient(wallet: SapWallet): SapClient {
285
337
  const provider = new AnchorProvider(this.connection, wallet, {
286
338
  commitment: this.commitment,
287
339
  });
@@ -301,7 +353,7 @@ export class SapConnection {
301
353
  * ```
302
354
  */
303
355
  fromKeypair(keypair: Keypair): SapClient {
304
- return this.createClient(new Wallet(keypair));
356
+ return this.createClient(new KeypairWallet(keypair));
305
357
  }
306
358
 
307
359
  // ─────────────────────────────────────────────
package/src/core/index.ts CHANGED
@@ -16,5 +16,5 @@
16
16
  */
17
17
 
18
18
  export { SapClient } from "./client";
19
- export { SapConnection } from "./connection";
20
- export type { SapCluster, SapConnectionConfig } from "./connection";
19
+ export { SapConnection, KeypairWallet } from "./connection";
20
+ export type { SapCluster, SapConnectionConfig, SapWallet } from "./connection";
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@
15
15
  * | `modules/` | Low-level per-domain instruction wrappers |
16
16
  * | `registries/` | High-level abstractions (discovery, x402, …) |
17
17
  * | `plugin/` | SynapseAgentKit adapter (52 tools) |
18
+ * | `parser/` | Transaction decode, instruction + event parse |
18
19
  * | `idl/` | Embedded Anchor IDL |
19
20
  *
20
21
  * @example
@@ -44,8 +45,8 @@
44
45
  */
45
46
 
46
47
  // ── Core ─────────────────────────────────────────────
47
- export { SapClient, SapConnection } from "./core";
48
- export type { SapCluster, SapConnectionConfig } from "./core";
48
+ export { SapClient, SapConnection, KeypairWallet } from "./core";
49
+ export type { SapCluster, SapConnectionConfig, SapWallet } from "./core";
49
50
 
50
51
  // ── Types ────────────────────────────────────────────
51
52
  export type {
@@ -220,6 +221,30 @@ export type {
220
221
  SyncOptions,
221
222
  } from "./postgres";
222
223
 
224
+ // ── Parser (transaction decode + instruction + events) ─
225
+ export {
226
+ parseSapInstructionsFromTransaction,
227
+ parseSapInstructionNamesFromTransaction,
228
+ parseSapInstructionsFromList,
229
+ parseSapInstructionNamesFromList,
230
+ containsSapInstruction,
231
+ parseSapTransactionComplete,
232
+ parseSapTransactionBatch,
233
+ decodeInnerInstructions,
234
+ filterSapInnerInstructions,
235
+ extractAccountKeys,
236
+ TransactionParser,
237
+ } from "./parser";
238
+ export type {
239
+ DecodedSapInstruction,
240
+ DecodedInnerInstruction,
241
+ ParsedSapTransaction,
242
+ ParseFilterOptions,
243
+ SapInstructionCoder,
244
+ CompiledInner,
245
+ InnerInstructionGroup,
246
+ } from "./parser";
247
+
223
248
  // ── Registries (high-level developer abstractions) ────
224
249
  export {
225
250
  DiscoveryRegistry,
@@ -0,0 +1,211 @@
1
+ /**
2
+ * @module parser/client
3
+ * @description Object-oriented wrapper for transaction parsing.
4
+ *
5
+ * Binds the Anchor `Program` reference so callers do not need to
6
+ * pass it on every call. Designed as a lazy singleton accessible
7
+ * from {@link SapClient.parser}.
8
+ *
9
+ * @category Parser
10
+ * @since v0.5.0
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const client = SapClient.from(provider);
15
+ *
16
+ * // Parse a full transaction
17
+ * const parsed = client.parser.parseTransaction(txResponse);
18
+ *
19
+ * // Quick instruction names
20
+ * const names = client.parser.instructionNames(txResponse);
21
+ *
22
+ * // From pre-built instructions
23
+ * const decoded = client.parser.fromInstructions(ixList);
24
+ * ```
25
+ */
26
+
27
+ import type { Program } from "@coral-xyz/anchor";
28
+ import type {
29
+ PublicKey,
30
+ TransactionInstruction,
31
+ TransactionResponse,
32
+ VersionedTransactionResponse,
33
+ } from "@solana/web3.js";
34
+ import { AddressLookupTableAccount } from "@solana/web3.js";
35
+
36
+ import type {
37
+ DecodedSapInstruction,
38
+ ParsedSapTransaction,
39
+ ParseFilterOptions,
40
+ SapInstructionCoder,
41
+ } from "./types";
42
+ import { parseSapInstructionsFromTransaction } from "./transaction";
43
+ import { parseSapInstructionsFromList, containsSapInstruction } from "./instructions";
44
+ import { parseSapTransactionComplete, parseSapTransactionBatch } from "./complete";
45
+ import {
46
+ decodeInnerInstructions,
47
+ extractAccountKeys,
48
+ type InnerInstructionGroup,
49
+ } from "./inner";
50
+
51
+ /**
52
+ * Stateful transaction parser bound to a specific Anchor `Program`.
53
+ *
54
+ * Stores the program reference, instruction coder, and program ID
55
+ * internally so that repeated parse calls require only the
56
+ * transaction data as input.
57
+ *
58
+ * @name TransactionParser
59
+ * @category Parser
60
+ * @since v0.5.0
61
+ */
62
+ export class TransactionParser {
63
+ private readonly coder: SapInstructionCoder;
64
+ private readonly programId: PublicKey;
65
+
66
+ /**
67
+ * Create a new TransactionParser.
68
+ *
69
+ * @param program - An Anchor `Program` built from the SAP IDL.
70
+ */
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ constructor(private readonly program: Program<any>) {
73
+ this.coder = program.coder.instruction as unknown as SapInstructionCoder;
74
+ this.programId = program.programId;
75
+ }
76
+
77
+ /**
78
+ * Full parse: instructions + inner calls + events.
79
+ *
80
+ * @param tx - Raw transaction response from `connection.getTransaction`.
81
+ * @param options - Optional filters for instructions, events, and inner calls.
82
+ * @param addressLookupTables - Resolved lookup tables for v0 transactions.
83
+ * @returns Complete parsed transaction, or `null` if the input is nullish.
84
+ *
85
+ * @since v0.5.0
86
+ */
87
+ parseTransaction(
88
+ tx: TransactionResponse | VersionedTransactionResponse | null | undefined,
89
+ options?: ParseFilterOptions,
90
+ addressLookupTables?: AddressLookupTableAccount[],
91
+ ): ParsedSapTransaction | null {
92
+ return parseSapTransactionComplete(
93
+ tx,
94
+ this.program,
95
+ this.programId,
96
+ options,
97
+ addressLookupTables,
98
+ );
99
+ }
100
+
101
+ /**
102
+ * Parse a batch of transactions.
103
+ *
104
+ * @param txs - Array of transaction responses (may contain `null` entries).
105
+ * @param options - Optional parse filters.
106
+ * @param addressLookupTables - Resolved lookup tables for v0 transactions.
107
+ * @returns Non-null parsed transactions.
108
+ *
109
+ * @since v0.5.0
110
+ */
111
+ parseBatch(
112
+ txs: (TransactionResponse | VersionedTransactionResponse | null | undefined)[],
113
+ options?: ParseFilterOptions,
114
+ addressLookupTables?: AddressLookupTableAccount[],
115
+ ): ParsedSapTransaction[] {
116
+ return parseSapTransactionBatch(
117
+ txs,
118
+ this.program,
119
+ this.programId,
120
+ options,
121
+ addressLookupTables,
122
+ );
123
+ }
124
+
125
+ /**
126
+ * Decode top-level SAP instructions from a transaction response.
127
+ *
128
+ * @param tx - Raw transaction response.
129
+ * @param addressLookupTables - Resolved lookup tables for v0 transactions.
130
+ * @returns Decoded SAP instructions.
131
+ *
132
+ * @since v0.5.0
133
+ */
134
+ instructionsFromTransaction(
135
+ tx: TransactionResponse | VersionedTransactionResponse,
136
+ addressLookupTables?: AddressLookupTableAccount[],
137
+ ): DecodedSapInstruction[] {
138
+ return parseSapInstructionsFromTransaction(
139
+ tx,
140
+ this.coder,
141
+ this.programId,
142
+ addressLookupTables,
143
+ );
144
+ }
145
+
146
+ /**
147
+ * Extract only the instruction names from a transaction response.
148
+ *
149
+ * @param tx - Raw transaction response.
150
+ * @param addressLookupTables - Resolved lookup tables for v0 transactions.
151
+ * @returns Instruction name strings.
152
+ *
153
+ * @since v0.5.0
154
+ */
155
+ instructionNames(
156
+ tx: TransactionResponse | VersionedTransactionResponse,
157
+ addressLookupTables?: AddressLookupTableAccount[],
158
+ ): string[] {
159
+ return this.instructionsFromTransaction(tx, addressLookupTables).map(
160
+ (ix) => ix.name,
161
+ );
162
+ }
163
+
164
+ /**
165
+ * Decode SAP instructions from a pre-built instruction array.
166
+ *
167
+ * @param instructions - The instruction list to decode.
168
+ * @returns Decoded SAP instructions.
169
+ *
170
+ * @since v0.5.0
171
+ */
172
+ fromInstructions(
173
+ instructions: TransactionInstruction[],
174
+ ): DecodedSapInstruction[] {
175
+ return parseSapInstructionsFromList(instructions, this.coder, this.programId);
176
+ }
177
+
178
+ /**
179
+ * Check if any instruction in the list targets the SAP program.
180
+ *
181
+ * @param instructions - The instruction array to inspect.
182
+ * @returns `true` if at least one instruction targets SAP.
183
+ *
184
+ * @since v0.5.0
185
+ */
186
+ isSapTransaction(instructions: TransactionInstruction[]): boolean {
187
+ return containsSapInstruction(instructions, this.programId);
188
+ }
189
+
190
+ /**
191
+ * Decode inner (CPI) instructions from transaction metadata.
192
+ *
193
+ * @param innerGroups - The `tx.meta.innerInstructions` array.
194
+ * @param tx - The transaction response (for account key resolution).
195
+ * @returns Decoded inner instructions.
196
+ *
197
+ * @since v0.5.0
198
+ */
199
+ decodeInner(
200
+ innerGroups: InnerInstructionGroup[],
201
+ tx: TransactionResponse | VersionedTransactionResponse,
202
+ ) {
203
+ const accountKeys = extractAccountKeys(tx);
204
+ return decodeInnerInstructions(
205
+ innerGroups,
206
+ accountKeys,
207
+ this.coder,
208
+ this.programId,
209
+ );
210
+ }
211
+ }
@@ -0,0 +1,232 @@
1
+ /**
2
+ * @module parser/complete
3
+ * @description Full SAP transaction parser: instructions + args + accounts + events.
4
+ *
5
+ * This is "Case 2 Complete": given a raw transaction response, produce a
6
+ * single {@link ParsedSapTransaction} containing every decoded instruction,
7
+ * all inner (CPI) calls, and all SAP events extracted from the logs.
8
+ *
9
+ * Designed for indexer pipelines where the decode step must be pure,
10
+ * deterministic, and fully testable without an RPC connection.
11
+ *
12
+ * @category Parser
13
+ * @since v0.5.0
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { parseSapTransactionComplete } from "@synapse-sap/sdk/parser";
18
+ * import { SAP_PROGRAM_ID, SAP_IDL } from "@synapse-sap/sdk";
19
+ * import { Program } from "\@coral-xyz/anchor";
20
+ *
21
+ * const program = new Program(SAP_IDL, provider);
22
+ * const tx = await connection.getTransaction(sig, {
23
+ * commitment: "confirmed",
24
+ * maxSupportedTransactionVersion: 0,
25
+ * });
26
+ *
27
+ * const parsed = parseSapTransactionComplete(tx, program, SAP_PROGRAM_ID);
28
+ * console.log(parsed.instructions.map(i => i.name));
29
+ * console.log(parsed.events.map(e => e.name));
30
+ * ```
31
+ */
32
+
33
+ import {
34
+ AddressLookupTableAccount,
35
+ type PublicKey,
36
+ type TransactionResponse,
37
+ type VersionedTransactionResponse,
38
+ } from "@solana/web3.js";
39
+ import type { Program } from "@coral-xyz/anchor";
40
+
41
+ import type {
42
+ ParsedSapTransaction,
43
+ ParseFilterOptions,
44
+ DecodedInnerInstruction,
45
+ SapInstructionCoder,
46
+ } from "./types";
47
+ import type { ParsedEvent } from "../events";
48
+ import { EventParser } from "../events";
49
+ import { parseSapInstructionsFromTransaction } from "./transaction";
50
+ import {
51
+ decodeInnerInstructions,
52
+ extractAccountKeys,
53
+ type InnerInstructionGroup,
54
+ } from "./inner";
55
+
56
+ // ================================================================
57
+ // Public API
58
+ // ================================================================
59
+
60
+ /**
61
+ * Parse a complete SAP transaction into a unified result.
62
+ *
63
+ * Combines three stages:
64
+ * 1. **Instruction decode** - top-level SAP instructions with args and accounts
65
+ * 2. **Inner instruction decode** - CPI calls with full account reconstruction
66
+ * 3. **Event extraction** - SAP events decoded from the transaction logs
67
+ *
68
+ * All three stages are safe: malformed data produces `null` fields
69
+ * rather than exceptions. This makes the function suitable for
70
+ * batch-processing in indexer workers where a single bad transaction
71
+ * must not halt the pipeline.
72
+ *
73
+ * @param tx - The raw transaction response from `connection.getTransaction`.
74
+ * @param program - An Anchor `Program` instance built from the SAP IDL.
75
+ * The coder and program ID are extracted automatically.
76
+ * @param sapProgramId - The SAP program public key. Passed explicitly so
77
+ * callers can target devnet/localnet deployments independently.
78
+ * @param options - Optional filters for instructions, events, and inner calls.
79
+ * @param addressLookupTables - Resolved lookup table accounts for v0 transactions.
80
+ * @returns A fully parsed transaction, or `null` if the input is `null`/`undefined`.
81
+ *
82
+ * @category Parser
83
+ * @since v0.5.0
84
+ */
85
+ export function parseSapTransactionComplete(
86
+ tx: TransactionResponse | VersionedTransactionResponse | null | undefined,
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+ program: Program<any>,
89
+ sapProgramId: PublicKey,
90
+ options?: ParseFilterOptions,
91
+ addressLookupTables?: AddressLookupTableAccount[],
92
+ ): ParsedSapTransaction | null {
93
+ if (!tx) return null;
94
+
95
+ const opts: Required<ParseFilterOptions> = {
96
+ includeInner: options?.includeInner ?? false,
97
+ includeEvents: options?.includeEvents ?? true,
98
+ instructionFilter: options?.instructionFilter ?? [],
99
+ eventFilter: options?.eventFilter ?? [],
100
+ };
101
+
102
+ // Extract coder from the program
103
+ const coder = program.coder.instruction as unknown as SapInstructionCoder;
104
+
105
+ // 1. Top-level instructions
106
+ let instructions = parseSapInstructionsFromTransaction(
107
+ tx,
108
+ coder,
109
+ sapProgramId,
110
+ addressLookupTables,
111
+ );
112
+
113
+ if (opts.instructionFilter.length > 0) {
114
+ const filterSet = new Set(opts.instructionFilter);
115
+ instructions = instructions.filter((ix) => filterSet.has(ix.name));
116
+ }
117
+
118
+ // 2. Inner (CPI) instructions
119
+ let innerInstructions: DecodedInnerInstruction[] = [];
120
+ if (opts.includeInner && tx.meta?.innerInstructions) {
121
+ const accountKeys = extractAccountKeys(tx);
122
+ innerInstructions = decodeInnerInstructions(
123
+ tx.meta.innerInstructions as unknown as InnerInstructionGroup[],
124
+ accountKeys,
125
+ coder,
126
+ sapProgramId,
127
+ );
128
+ }
129
+
130
+ // 3. Events from logs
131
+ let events: ParsedEvent[] = [];
132
+ const logs = tx.meta?.logMessages ?? [];
133
+
134
+ if (opts.includeEvents && logs.length > 0) {
135
+ const eventParser = new EventParser(program);
136
+ events = eventParser.parseLogs(logs);
137
+
138
+ if (opts.eventFilter.length > 0) {
139
+ const filterSet = new Set(opts.eventFilter);
140
+ events = events.filter((e) => filterSet.has(e.name as never));
141
+ }
142
+ }
143
+
144
+ // 4. Metadata
145
+ const signature = extractSignature(tx);
146
+ const slot = tx.slot ?? null;
147
+ const blockTime = tx.blockTime ?? null;
148
+ const success = tx.meta?.err === null || tx.meta?.err === undefined;
149
+
150
+ return {
151
+ signature,
152
+ slot,
153
+ blockTime,
154
+ success,
155
+ instructions,
156
+ innerInstructions,
157
+ events,
158
+ logs,
159
+ };
160
+ }
161
+
162
+ /**
163
+ * Parse multiple transactions in batch.
164
+ *
165
+ * Convenience wrapper for indexer pipelines that process pages of
166
+ * transactions. Skips `null` entries and failed decodes silently.
167
+ *
168
+ * @param txs - Array of transaction responses (may contain `null` entries).
169
+ * @param program - The Anchor SAP program instance.
170
+ * @param sapProgramId - The SAP program public key.
171
+ * @param options - Optional parse filters applied to every transaction.
172
+ * @param addressLookupTables - Lookup tables for v0 transactions.
173
+ * @returns An array of non-null parsed transactions.
174
+ *
175
+ * @category Parser
176
+ * @since v0.5.0
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * const signatures = await connection.getSignaturesForAddress(agentPda);
181
+ * const txs = await Promise.all(
182
+ * signatures.map(s => connection.getTransaction(s.signature, { ... }))
183
+ * );
184
+ * const parsed = parseSapTransactionBatch(txs, program, SAP_PROGRAM_ID, {
185
+ * includeEvents: true,
186
+ * includeInner: true,
187
+ * });
188
+ * ```
189
+ */
190
+ export function parseSapTransactionBatch(
191
+ txs: (TransactionResponse | VersionedTransactionResponse | null | undefined)[],
192
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
+ program: Program<any>,
194
+ sapProgramId: PublicKey,
195
+ options?: ParseFilterOptions,
196
+ addressLookupTables?: AddressLookupTableAccount[],
197
+ ): ParsedSapTransaction[] {
198
+ const results: ParsedSapTransaction[] = [];
199
+ for (const tx of txs) {
200
+ const parsed = parseSapTransactionComplete(
201
+ tx,
202
+ program,
203
+ sapProgramId,
204
+ options,
205
+ addressLookupTables,
206
+ );
207
+ if (parsed) results.push(parsed);
208
+ }
209
+ return results;
210
+ }
211
+
212
+ // ================================================================
213
+ // Internal
214
+ // ================================================================
215
+
216
+ /**
217
+ * Extract the transaction signature from the response.
218
+ * Different RPC clients expose it in different locations.
219
+ *
220
+ * @internal
221
+ */
222
+ function extractSignature(
223
+ tx: TransactionResponse | VersionedTransactionResponse,
224
+ ): string | null {
225
+ // Some clients expose signatures on the transaction object
226
+ const txObj = tx.transaction;
227
+ if ("signatures" in txObj && Array.isArray(txObj.signatures) && txObj.signatures.length > 0) {
228
+ const first = txObj.signatures[0];
229
+ if (typeof first === "string") return first;
230
+ }
231
+ return null;
232
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @module parser
3
+ * @description Transaction parsing utilities for SAP v2.
4
+ *
5
+ * Provides modular, composable functions for decoding on-chain SAP
6
+ * transactions into typed instruction data, argument objects, account
7
+ * lists, and protocol events.
8
+ *
9
+ * Three levels of parsing are available depending on your use case:
10
+ *
11
+ * | Function | Input | Output |
12
+ * |----------|-------|--------|
13
+ * | `parseSapInstructionsFromTransaction` | `TransactionResponse` (RPC) | Decoded instructions |
14
+ * | `parseSapInstructionsFromList` | `TransactionInstruction[]` | Decoded instructions |
15
+ * | `parseSapTransactionComplete` | `TransactionResponse` (RPC) | Instructions + events + inner calls |
16
+ *
17
+ * All functions are pure and stateless: they accept the Anchor coder
18
+ * (or full Program) as a parameter, making them safe for server-side
19
+ * indexer workers, edge functions (Node.js runtime), and test suites.
20
+ *
21
+ * @category Parser
22
+ * @since v0.5.0
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import {
27
+ * parseSapTransactionComplete,
28
+ * parseSapInstructionsFromList,
29
+ * containsSapInstruction,
30
+ * } from "@synapse-sap/sdk/parser";
31
+ * ```
32
+ */
33
+
34
+ // ── Types ────────────────────────────────────────────
35
+ export type {
36
+ DecodedSapInstruction,
37
+ DecodedInnerInstruction,
38
+ ParsedSapTransaction,
39
+ ParseFilterOptions,
40
+ SapInstructionCoder,
41
+ } from "./types";
42
+
43
+ // ── Case 2A: from TransactionResponse ────────────────
44
+ export {
45
+ parseSapInstructionsFromTransaction,
46
+ parseSapInstructionNamesFromTransaction,
47
+ } from "./transaction";
48
+
49
+ // ── Case 2B: from TransactionInstruction[] ───────────
50
+ export {
51
+ parseSapInstructionsFromList,
52
+ parseSapInstructionNamesFromList,
53
+ containsSapInstruction,
54
+ } from "./instructions";
55
+
56
+ // ── Case 2 Complete: instructions + events + inner ───
57
+ export {
58
+ parseSapTransactionComplete,
59
+ parseSapTransactionBatch,
60
+ } from "./complete";
61
+
62
+ // ── Inner instruction utilities ──────────────────────
63
+ export {
64
+ decodeInnerInstructions,
65
+ filterSapInnerInstructions,
66
+ extractAccountKeys,
67
+ } from "./inner";
68
+ export type { CompiledInner, InnerInstructionGroup } from "./inner";
69
+
70
+ // ── OOP wrapper (for SapClient integration) ──────────
71
+ export { TransactionParser } from "./client";