@oobe-protocol-labs/synapse-sap-sdk 0.4.1 → 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 (64) 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/index.js +15 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/parser/client.js +146 -0
  6. package/dist/cjs/parser/client.js.map +1 -0
  7. package/dist/cjs/parser/complete.js +177 -0
  8. package/dist/cjs/parser/complete.js.map +1 -0
  9. package/dist/cjs/parser/index.js +57 -0
  10. package/dist/cjs/parser/index.js.map +1 -0
  11. package/dist/cjs/parser/inner.js +185 -0
  12. package/dist/cjs/parser/inner.js.map +1 -0
  13. package/dist/cjs/parser/instructions.js +114 -0
  14. package/dist/cjs/parser/instructions.js.map +1 -0
  15. package/dist/cjs/parser/transaction.js +153 -0
  16. package/dist/cjs/parser/transaction.js.map +1 -0
  17. package/dist/cjs/parser/types.js +14 -0
  18. package/dist/cjs/parser/types.js.map +1 -0
  19. package/dist/esm/core/client.js +23 -0
  20. package/dist/esm/core/client.js.map +1 -1
  21. package/dist/esm/index.js +3 -0
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/parser/client.js +142 -0
  24. package/dist/esm/parser/client.js.map +1 -0
  25. package/dist/esm/parser/complete.js +173 -0
  26. package/dist/esm/parser/complete.js.map +1 -0
  27. package/dist/esm/parser/index.js +43 -0
  28. package/dist/esm/parser/index.js.map +1 -0
  29. package/dist/esm/parser/inner.js +180 -0
  30. package/dist/esm/parser/inner.js.map +1 -0
  31. package/dist/esm/parser/instructions.js +109 -0
  32. package/dist/esm/parser/instructions.js.map +1 -0
  33. package/dist/esm/parser/transaction.js +149 -0
  34. package/dist/esm/parser/transaction.js.map +1 -0
  35. package/dist/esm/parser/types.js +13 -0
  36. package/dist/esm/parser/types.js.map +1 -0
  37. package/dist/types/core/client.d.ts +19 -0
  38. package/dist/types/core/client.d.ts.map +1 -1
  39. package/dist/types/index.d.ts +3 -0
  40. package/dist/types/index.d.ts.map +1 -1
  41. package/dist/types/parser/client.d.ts +123 -0
  42. package/dist/types/parser/client.d.ts.map +1 -0
  43. package/dist/types/parser/complete.d.ts +90 -0
  44. package/dist/types/parser/complete.d.ts.map +1 -0
  45. package/dist/types/parser/index.d.ts +40 -0
  46. package/dist/types/parser/index.d.ts.map +1 -0
  47. package/dist/types/parser/inner.d.ts +114 -0
  48. package/dist/types/parser/inner.d.ts.map +1 -0
  49. package/dist/types/parser/instructions.d.ts +76 -0
  50. package/dist/types/parser/instructions.d.ts.map +1 -0
  51. package/dist/types/parser/transaction.d.ts +77 -0
  52. package/dist/types/parser/transaction.d.ts.map +1 -0
  53. package/dist/types/parser/types.d.ts +154 -0
  54. package/dist/types/parser/types.d.ts.map +1 -0
  55. package/package.json +6 -1
  56. package/src/core/client.ts +25 -0
  57. package/src/index.ts +25 -0
  58. package/src/parser/client.ts +211 -0
  59. package/src/parser/complete.ts +232 -0
  60. package/src/parser/index.ts +71 -0
  61. package/src/parser/inner.ts +255 -0
  62. package/src/parser/instructions.ts +135 -0
  63. package/src/parser/transaction.ts +200 -0
  64. package/src/parser/types.ts +182 -0
@@ -0,0 +1,76 @@
1
+ /**
2
+ * @module parser/instructions
3
+ * @description Decode SAP instruction names from a pre-built `TransactionInstruction[]`.
4
+ *
5
+ * This is "Case 2B": you already have the decompiled instruction list
6
+ * (for example, from a UI that constructs instructions before sending)
7
+ * and want to identify which ones target the SAP program.
8
+ *
9
+ * @category Parser
10
+ * @since v0.5.0
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { parseSapInstructionsFromList } from "@synapse-sap/sdk/parser";
15
+ * import { SAP_PROGRAM_ID } from "@synapse-sap/sdk";
16
+ *
17
+ * const decoded = parseSapInstructionsFromList(
18
+ * instructions,
19
+ * program.coder.instruction,
20
+ * SAP_PROGRAM_ID,
21
+ * );
22
+ * for (const ix of decoded) {
23
+ * console.log(ix.name, ix.args);
24
+ * }
25
+ * ```
26
+ */
27
+ import type { PublicKey, TransactionInstruction } from "@solana/web3.js";
28
+ import type { DecodedSapInstruction, SapInstructionCoder } from "./types";
29
+ /**
30
+ * Decode an array of `TransactionInstruction` and return only the
31
+ * SAP instructions with their decoded names and arguments.
32
+ *
33
+ * Non-SAP instructions (system, token, other programs) are silently
34
+ * skipped. Instructions whose data cannot be decoded against the IDL
35
+ * are still included with `name: "unknown"` and `args: null` so that
36
+ * consumers can detect IDL mismatches or unsupported instruction
37
+ * variants.
38
+ *
39
+ * @param instructions - The instruction array to inspect.
40
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
41
+ * @param sapProgramId - The SAP program public key to filter by.
42
+ * @returns An array of decoded SAP instructions.
43
+ *
44
+ * @category Parser
45
+ * @since v0.5.0
46
+ */
47
+ export declare function parseSapInstructionsFromList(instructions: TransactionInstruction[], coder: SapInstructionCoder, sapProgramId: PublicKey): DecodedSapInstruction[];
48
+ /**
49
+ * Return only the instruction names for SAP instructions in the list.
50
+ *
51
+ * Convenience wrapper over {@link parseSapInstructionsFromList} for
52
+ * callers that only need the string names.
53
+ *
54
+ * @param instructions - The instruction array to inspect.
55
+ * @param coder - An Anchor instruction coder for the SAP IDL.
56
+ * @param sapProgramId - The SAP program public key.
57
+ * @returns An array of instruction name strings.
58
+ *
59
+ * @category Parser
60
+ * @since v0.5.0
61
+ */
62
+ export declare function parseSapInstructionNamesFromList(instructions: TransactionInstruction[], coder: SapInstructionCoder, sapProgramId: PublicKey): string[];
63
+ /**
64
+ * Check whether any instruction in the list targets the SAP program.
65
+ *
66
+ * Useful as a fast pre-filter before committing to a full decode pass.
67
+ *
68
+ * @param instructions - The instruction array to inspect.
69
+ * @param sapProgramId - The SAP program public key.
70
+ * @returns `true` if at least one instruction targets the SAP program.
71
+ *
72
+ * @category Parser
73
+ * @since v0.5.0
74
+ */
75
+ export declare function containsSapInstruction(instructions: TransactionInstruction[], sapProgramId: PublicKey): boolean;
76
+ //# sourceMappingURL=instructions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instructions.d.ts","sourceRoot":"","sources":["../../../src/parser/instructions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACzE,OAAO,KAAK,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAM1E;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,4BAA4B,CAC1C,YAAY,EAAE,sBAAsB,EAAE,EACtC,KAAK,EAAE,mBAAmB,EAC1B,YAAY,EAAE,SAAS,GACtB,qBAAqB,EAAE,CAgBzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gCAAgC,CAC9C,YAAY,EAAE,sBAAsB,EAAE,EACtC,KAAK,EAAE,mBAAmB,EAC1B,YAAY,EAAE,SAAS,GACtB,MAAM,EAAE,CAIV;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,sBAAsB,EAAE,EACtC,YAAY,EAAE,SAAS,GACtB,OAAO,CAET"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @module parser/transaction
3
+ * @description Decode SAP instruction names from a raw `TransactionResponse`.
4
+ *
5
+ * This is "Case 2A": you have a transaction response object obtained
6
+ * from `connection.getTransaction(signature, ...)` and need to extract
7
+ * the SAP instruction names, arguments, and account keys.
8
+ *
9
+ * The function handles both legacy and versioned (v0) transactions
10
+ * by decompiling the message into `TransactionInstruction[]` and then
11
+ * filtering for instructions whose `programId` matches the SAP program.
12
+ *
13
+ * @category Parser
14
+ * @since v0.5.0
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { parseSapInstructionsFromTransaction } from "@synapse-sap/sdk/parser";
19
+ * import { SAP_PROGRAM_ID } from "@synapse-sap/sdk";
20
+ *
21
+ * const tx = await connection.getTransaction(sig, {
22
+ * commitment: "confirmed",
23
+ * maxSupportedTransactionVersion: 0,
24
+ * });
25
+ * if (!tx) throw new Error("Transaction not found");
26
+ *
27
+ * const decoded = parseSapInstructionsFromTransaction(
28
+ * tx,
29
+ * program.coder.instruction,
30
+ * SAP_PROGRAM_ID,
31
+ * );
32
+ * for (const ix of decoded) {
33
+ * console.log(ix.name, ix.args);
34
+ * }
35
+ * ```
36
+ */
37
+ import { AddressLookupTableAccount, type PublicKey, type VersionedTransactionResponse, type TransactionResponse } from "@solana/web3.js";
38
+ import type { DecodedSapInstruction, SapInstructionCoder } from "./types";
39
+ /**
40
+ * Extract and decode SAP instructions from a transaction response.
41
+ *
42
+ * Supports both legacy (`TransactionResponse`) and versioned
43
+ * (`VersionedTransactionResponse`) formats. For versioned
44
+ * transactions that use address lookup tables, pass the resolved
45
+ * lookup table accounts so that `TransactionMessage.decompile`
46
+ * can reconstruct the full account list.
47
+ *
48
+ * @param tx - The transaction response from `connection.getTransaction`.
49
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
50
+ * @param sapProgramId - The SAP program public key to filter by.
51
+ * @param addressLookupTables - Resolved lookup table accounts for v0 transactions.
52
+ * Required when the transaction uses address lookup tables; omit for legacy txs.
53
+ * @returns An array of decoded SAP instructions found in the transaction.
54
+ *
55
+ * @throws {Error} When the transaction message cannot be decompiled.
56
+ *
57
+ * @category Parser
58
+ * @since v0.5.0
59
+ */
60
+ export declare function parseSapInstructionsFromTransaction(tx: TransactionResponse | VersionedTransactionResponse, coder: SapInstructionCoder, sapProgramId: PublicKey, addressLookupTables?: AddressLookupTableAccount[]): DecodedSapInstruction[];
61
+ /**
62
+ * Extract only the SAP instruction names from a transaction response.
63
+ *
64
+ * Lighter-weight alternative to {@link parseSapInstructionsFromTransaction}
65
+ * when you only need the instruction names without decoded arguments.
66
+ *
67
+ * @param tx - The transaction response.
68
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
69
+ * @param sapProgramId - The SAP program public key.
70
+ * @param addressLookupTables - Resolved lookup table accounts for v0 transactions.
71
+ * @returns An array of instruction name strings.
72
+ *
73
+ * @category Parser
74
+ * @since v0.5.0
75
+ */
76
+ export declare function parseSapInstructionNamesFromTransaction(tx: TransactionResponse | VersionedTransactionResponse, coder: SapInstructionCoder, sapProgramId: PublicKey, addressLookupTables?: AddressLookupTableAccount[]): string[];
77
+ //# sourceMappingURL=transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../../../src/parser/transaction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAEL,yBAAyB,EACzB,KAAK,SAAS,EAEd,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,EACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAM1E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mCAAmC,CACjD,EAAE,EAAE,mBAAmB,GAAG,4BAA4B,EACtD,KAAK,EAAE,mBAAmB,EAC1B,YAAY,EAAE,SAAS,EACvB,mBAAmB,CAAC,EAAE,yBAAyB,EAAE,GAChD,qBAAqB,EAAE,CAGzB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,uCAAuC,CACrD,EAAE,EAAE,mBAAmB,GAAG,4BAA4B,EACtD,KAAK,EAAE,mBAAmB,EAC1B,YAAY,EAAE,SAAS,EACvB,mBAAmB,CAAC,EAAE,yBAAyB,EAAE,GAChD,MAAM,EAAE,CAOV"}
@@ -0,0 +1,154 @@
1
+ /**
2
+ * @module parser/types
3
+ * @description Type definitions for SAP v2 transaction parsing.
4
+ *
5
+ * Provides strongly-typed interfaces for decoded instruction data,
6
+ * account metadata, inner (CPI) instructions, and the full parsed
7
+ * transaction result used by indexers and explorers.
8
+ *
9
+ * @category Parser
10
+ * @since v0.5.0
11
+ */
12
+ import type { PublicKey, TransactionInstruction } from "@solana/web3.js";
13
+ import type { ParsedEvent, SapEventName } from "../events";
14
+ /**
15
+ * A single SAP instruction decoded from on-chain transaction data.
16
+ *
17
+ * Contains the human-readable instruction name (as declared in the
18
+ * Anchor IDL), the decoded argument object, the ordered list of
19
+ * account keys, and the raw instruction for advanced consumers.
20
+ *
21
+ * @interface DecodedSapInstruction
22
+ * @category Parser
23
+ * @since v0.5.0
24
+ */
25
+ export interface DecodedSapInstruction {
26
+ /** Instruction name matching the Anchor IDL method (e.g. `"registerAgent"`). */
27
+ readonly name: string;
28
+ /**
29
+ * Decoded arguments object. Keys match the Anchor IDL argument names.
30
+ * Returns `null` when decoding fails (e.g. IDL mismatch).
31
+ */
32
+ readonly args: Record<string, any> | null;
33
+ /** Ordered list of account public keys passed to the instruction. */
34
+ readonly accounts: PublicKey[];
35
+ /** The original `TransactionInstruction`, useful for re-simulation or forwarding. */
36
+ readonly raw: TransactionInstruction;
37
+ }
38
+ /**
39
+ * An inner instruction produced by cross-program invocation (CPI)
40
+ * within a SAP transaction.
41
+ *
42
+ * Inner instructions are indexed by the outer instruction position
43
+ * that triggered them. Each entry includes the decoded SAP name
44
+ * when the inner call targets the SAP program, or `null` otherwise.
45
+ *
46
+ * @interface DecodedInnerInstruction
47
+ * @category Parser
48
+ * @since v0.5.0
49
+ */
50
+ export interface DecodedInnerInstruction {
51
+ /** Zero-based index of the outer instruction that triggered this CPI call. */
52
+ readonly outerIndex: number;
53
+ /** Zero-based position within the inner instruction set of the parent. */
54
+ readonly innerIndex: number;
55
+ /** SAP instruction name if the CPI targets the SAP program, `null` otherwise. */
56
+ readonly name: string | null;
57
+ /** Decoded arguments when the CPI targets SAP and decoding succeeds. */
58
+ readonly args: Record<string, any> | null;
59
+ /** Ordered account keys for this inner instruction. */
60
+ readonly accounts: PublicKey[];
61
+ /** The program that was invoked by this inner call. */
62
+ readonly programId: PublicKey;
63
+ }
64
+ /**
65
+ * Full parse result for a single SAP transaction.
66
+ *
67
+ * Combines top-level instruction decoding, inner instruction
68
+ * analysis, and event extraction into one unified structure
69
+ * suitable for indexing, analytics dashboards, and protocol
70
+ * explorers.
71
+ *
72
+ * @interface ParsedSapTransaction
73
+ * @category Parser
74
+ * @since v0.5.0
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const result = parseSapTransactionComplete(tx, program, SAP_PROGRAM_ID);
79
+ * for (const ix of result.instructions) {
80
+ * console.log(ix.name, ix.args);
81
+ * }
82
+ * for (const event of result.events) {
83
+ * console.log(event.name, event.data);
84
+ * }
85
+ * ```
86
+ */
87
+ export interface ParsedSapTransaction {
88
+ /** Transaction signature (base-58), extracted from the response when available. */
89
+ readonly signature: string | null;
90
+ /** Slot at which the transaction was confirmed. */
91
+ readonly slot: number | null;
92
+ /** Block timestamp (unix seconds) or `null` if unavailable. */
93
+ readonly blockTime: number | null;
94
+ /** Whether the transaction succeeded (`true`) or failed. */
95
+ readonly success: boolean;
96
+ /** Top-level SAP instructions found in the transaction. */
97
+ readonly instructions: DecodedSapInstruction[];
98
+ /** All inner (CPI) instructions, including non-SAP programs. */
99
+ readonly innerInstructions: DecodedInnerInstruction[];
100
+ /** Decoded SAP events extracted from the transaction logs. */
101
+ readonly events: ParsedEvent[];
102
+ /** Raw log lines from the transaction, for additional debugging. */
103
+ readonly logs: string[];
104
+ }
105
+ /**
106
+ * Options for filtering parsed instruction results.
107
+ *
108
+ * @interface ParseFilterOptions
109
+ * @category Parser
110
+ * @since v0.5.0
111
+ */
112
+ export interface ParseFilterOptions {
113
+ /**
114
+ * When `true`, include inner (CPI) instructions in the parse
115
+ * result. Defaults to `false` because inner instruction
116
+ * reconstruction requires additional account-table lookups.
117
+ */
118
+ readonly includeInner?: boolean;
119
+ /**
120
+ * When `true`, decode and attach SAP events from the
121
+ * transaction logs. Defaults to `true`.
122
+ */
123
+ readonly includeEvents?: boolean;
124
+ /**
125
+ * Restrict the result to instructions matching one of these
126
+ * names. When `undefined` or empty, all SAP instructions are
127
+ * returned.
128
+ */
129
+ readonly instructionFilter?: string[];
130
+ /**
131
+ * Restrict events to those matching one of these names.
132
+ * When `undefined` or empty, all events are returned.
133
+ */
134
+ readonly eventFilter?: SapEventName[];
135
+ }
136
+ /**
137
+ * Minimal interface for the Anchor instruction coder.
138
+ *
139
+ * Extracted so that parser functions can accept either a full
140
+ * `Program` instance or a standalone coder/IDL pair without
141
+ * requiring a live RPC connection.
142
+ *
143
+ * @interface SapInstructionCoder
144
+ * @category Parser
145
+ * @since v0.5.0
146
+ */
147
+ export interface SapInstructionCoder {
148
+ /** Decode raw instruction data into a name and typed argument object. */
149
+ decode(data: Buffer | Uint8Array, encoding?: string): {
150
+ name: string;
151
+ data: Record<string, any>;
152
+ } | null;
153
+ }
154
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/parser/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACzE,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAM3D;;;;;;;;;;GAUG;AACH,MAAM,WAAW,qBAAqB;IACpC,gFAAgF;IAChF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IAEH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;IAC1C,qEAAqE;IACrE,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC;IAC/B,qFAAqF;IACrF,QAAQ,CAAC,GAAG,EAAE,sBAAsB,CAAC;CACtC;AAMD;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,uBAAuB;IACtC,8EAA8E;IAC9E,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,0EAA0E;IAC1E,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,iFAAiF;IACjF,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,wEAAwE;IAExE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;IAC1C,uDAAuD;IACvD,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC;IAC/B,uDAAuD;IACvD,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AAMD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,oBAAoB;IACnC,mFAAmF;IACnF,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,mDAAmD;IACnD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,+DAA+D;IAC/D,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,2DAA2D;IAC3D,QAAQ,CAAC,YAAY,EAAE,qBAAqB,EAAE,CAAC;IAC/C,gEAAgE;IAChE,QAAQ,CAAC,iBAAiB,EAAE,uBAAuB,EAAE,CAAC;IACtD,8DAA8D;IAC9D,QAAQ,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC;IAC/B,oEAAoE;IACpE,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC;CACzB;AAMD;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IACjC;;;;OAIG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IACtC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,YAAY,EAAE,CAAC;CACvC;AAMD;;;;;;;;;;GAUG;AACH,MAAM,WAAW,mBAAmB;IAClC,yEAAyE;IACzE,MAAM,CACJ,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,QAAQ,CAAC,EAAE,MAAM,GAEhB;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,GAAG,IAAI,CAAC;CACvD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oobe-protocol-labs/synapse-sap-sdk",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "TypeScript SDK for the Synapse Agent Protocol (SAP v2) on Solana",
5
5
  "license": "MIT",
6
6
  "main": "dist/cjs/index.js",
@@ -131,6 +131,11 @@
131
131
  "import": "./dist/esm/postgres/index.js",
132
132
  "require": "./dist/cjs/postgres/index.js",
133
133
  "types": "./dist/types/postgres/index.d.ts"
134
+ },
135
+ "./parser": {
136
+ "import": "./dist/esm/parser/index.js",
137
+ "require": "./dist/cjs/parser/index.js",
138
+ "types": "./dist/types/parser/index.d.ts"
134
139
  }
135
140
  },
136
141
  "files": [
@@ -36,6 +36,7 @@ import { EscrowModule } from "../modules/escrow";
36
36
  import { AttestationModule } from "../modules/attestation";
37
37
  import { LedgerModule } from "../modules/ledger";
38
38
  import { EventParser } from "../events";
39
+ import { TransactionParser } from "../parser/client";
39
40
  import { DiscoveryRegistry } from "../registries/discovery";
40
41
  import { X402Registry } from "../registries/x402";
41
42
  import { SessionManager } from "../registries/session";
@@ -116,6 +117,9 @@ export class SapClient {
116
117
  #ledger?: LedgerModule;
117
118
  #events?: EventParser;
118
119
 
120
+ // ── Lazy parser singleton ─────────────────────
121
+ #parser?: TransactionParser;
122
+
119
123
  // ── Lazy registry singletons ──────────────────────
120
124
  #discovery?: DiscoveryRegistry;
121
125
  #x402?: X402Registry;
@@ -318,6 +322,27 @@ export class SapClient {
318
322
  return (this.#events ??= new EventParser(this.program));
319
323
  }
320
324
 
325
+ /**
326
+ * @name parser
327
+ * @description Transaction parser: decode instruction names, arguments,
328
+ * accounts, inner CPI calls, and protocol events from raw transaction
329
+ * responses. Designed for indexers and explorers.
330
+ * @returns {TransactionParser} The lazily-instantiated `TransactionParser` singleton.
331
+ * @category Modules
332
+ * @since v0.5.0
333
+ * @see {@link TransactionParser}
334
+ *
335
+ * @example
336
+ * ```ts
337
+ * const tx = await connection.getTransaction(sig, { ... });
338
+ * const parsed = client.parser.parseTransaction(tx);
339
+ * console.log(parsed?.instructions.map(i => i.name));
340
+ * ```
341
+ */
342
+ get parser(): TransactionParser {
343
+ return (this.#parser ??= new TransactionParser(this.program));
344
+ }
345
+
321
346
  // ═════════════════════════════════════════════
322
347
  // Registry Accessors (high-level abstractions)
323
348
  // ═════════════════════════════════════════════
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
@@ -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
+ }