@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
@@ -0,0 +1,255 @@
1
+ /**
2
+ * @module parser/inner
3
+ * @description Decode inner (CPI) instructions from transaction metadata.
4
+ *
5
+ * When a SAP instruction triggers cross-program invocations, the
6
+ * resulting inner instructions appear in `tx.meta.innerInstructions`.
7
+ * These are stored in a "compiled" format that references account
8
+ * indices rather than full public keys.
9
+ *
10
+ * This module reconstructs the full account keys from the transaction
11
+ * message's account list and decodes any inner calls that target the
12
+ * SAP program.
13
+ *
14
+ * @category Parser
15
+ * @since v0.5.0
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { decodeInnerInstructions } from "@synapse-sap/sdk/parser";
20
+ *
21
+ * const inner = decodeInnerInstructions(
22
+ * tx.meta?.innerInstructions ?? [],
23
+ * accountKeys,
24
+ * program.coder.instruction,
25
+ * SAP_PROGRAM_ID,
26
+ * );
27
+ * for (const cpi of inner) {
28
+ * if (cpi.name) console.log("SAP CPI:", cpi.name);
29
+ * }
30
+ * ```
31
+ */
32
+
33
+ import { PublicKey } from "@solana/web3.js";
34
+ import { utils } from "@coral-xyz/anchor";
35
+ import type { DecodedInnerInstruction, SapInstructionCoder } from "./types";
36
+
37
+ // ================================================================
38
+ // Types matching Solana RPC response shapes
39
+ // ================================================================
40
+
41
+ /**
42
+ * Shape of a single compiled inner instruction from `tx.meta.innerInstructions`.
43
+ * Mirrors the Solana RPC `CompiledInnerInstruction` format.
44
+ *
45
+ * @interface CompiledInner
46
+ * @category Parser
47
+ * @since v0.5.0
48
+ */
49
+ export interface CompiledInner {
50
+ readonly programIdIndex: number;
51
+ readonly accounts: number[];
52
+ readonly data: string;
53
+ }
54
+
55
+ /**
56
+ * Top-level inner instruction group from the transaction metadata.
57
+ * Each group corresponds to one outer instruction by `index`.
58
+ *
59
+ * @interface InnerInstructionGroup
60
+ * @category Parser
61
+ * @since v0.5.0
62
+ */
63
+ export interface InnerInstructionGroup {
64
+ readonly index: number;
65
+ readonly instructions: CompiledInner[];
66
+ }
67
+
68
+ // ================================================================
69
+ // Public API
70
+ // ================================================================
71
+
72
+ /**
73
+ * Decode inner (CPI) instructions from transaction metadata.
74
+ *
75
+ * Reconstructs full public keys from the compiled account indices
76
+ * and attempts to decode each inner instruction that targets the
77
+ * SAP program. Non-SAP inner instructions are included in the
78
+ * result with `name: null` and `args: null`.
79
+ *
80
+ * @param innerInstructionGroups - The `tx.meta.innerInstructions` array.
81
+ * @param accountKeys - Ordered list of all account public keys from the
82
+ * transaction message (`staticAccountKeys` for versioned, or
83
+ * `accountKeys` for legacy).
84
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
85
+ * @param sapProgramId - The SAP program public key.
86
+ * @returns An array of decoded inner instructions.
87
+ *
88
+ * @category Parser
89
+ * @since v0.5.0
90
+ */
91
+ export function decodeInnerInstructions(
92
+ innerInstructionGroups: InnerInstructionGroup[],
93
+ accountKeys: PublicKey[],
94
+ coder: SapInstructionCoder,
95
+ sapProgramId: PublicKey,
96
+ ): DecodedInnerInstruction[] {
97
+ const results: DecodedInnerInstruction[] = [];
98
+
99
+ for (const group of innerInstructionGroups) {
100
+ for (let innerIdx = 0; innerIdx < group.instructions.length; innerIdx++) {
101
+ const compiled = group.instructions[innerIdx];
102
+ if (!compiled) continue;
103
+
104
+ const programId = resolveAccountKey(
105
+ accountKeys,
106
+ compiled.programIdIndex,
107
+ );
108
+ const accounts = compiled.accounts.map((idx) =>
109
+ resolveAccountKey(accountKeys, idx),
110
+ );
111
+
112
+ let name: string | null = null;
113
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
114
+ let args: Record<string, any> | null = null;
115
+
116
+ if (programId.equals(sapProgramId)) {
117
+ const decoded = safeDecodeInstruction(coder, compiled.data);
118
+ if (decoded) {
119
+ name = decoded.name;
120
+ args = decoded.data;
121
+ }
122
+ }
123
+
124
+ results.push({
125
+ outerIndex: group.index,
126
+ innerIndex: innerIdx,
127
+ name,
128
+ args,
129
+ accounts,
130
+ programId,
131
+ });
132
+ }
133
+ }
134
+
135
+ return results;
136
+ }
137
+
138
+ /**
139
+ * Filter decoded inner instructions to only those targeting the SAP program.
140
+ *
141
+ * @param inner - The full inner instruction list from {@link decodeInnerInstructions}.
142
+ * @returns Only inner instructions where `name` is not `null`.
143
+ *
144
+ * @category Parser
145
+ * @since v0.5.0
146
+ */
147
+ export function filterSapInnerInstructions(
148
+ inner: DecodedInnerInstruction[],
149
+ ): DecodedInnerInstruction[] {
150
+ return inner.filter((i) => i.name !== null);
151
+ }
152
+
153
+ // ================================================================
154
+ // Helpers: extract account keys from various tx formats
155
+ // ================================================================
156
+
157
+ /**
158
+ * Extract the full ordered list of account keys from a transaction
159
+ * response, handling both legacy and versioned formats.
160
+ *
161
+ * For versioned transactions that include loaded addresses (from
162
+ * address lookup tables), these are appended after the static keys
163
+ * in the order: static, writable loaded, readonly loaded.
164
+ *
165
+ * @param tx - The raw transaction response from RPC.
166
+ * @returns An ordered array of all account public keys.
167
+ *
168
+ * @category Parser
169
+ * @since v0.5.0
170
+ */
171
+ export function extractAccountKeys(
172
+ tx: {
173
+ transaction: {
174
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
175
+ message: any;
176
+ };
177
+ meta?: {
178
+ loadedAddresses?: {
179
+ writable: PublicKey[];
180
+ readonly: PublicKey[];
181
+ } | null;
182
+ } | null;
183
+ },
184
+ ): PublicKey[] {
185
+ const message = tx.transaction.message;
186
+
187
+ // Versioned messages expose `staticAccountKeys`
188
+ if ("staticAccountKeys" in message && Array.isArray(message.staticAccountKeys)) {
189
+ const staticKeys: PublicKey[] = message.staticAccountKeys.map(toPubkey);
190
+ const loaded = tx.meta?.loadedAddresses;
191
+ if (loaded) {
192
+ return [
193
+ ...staticKeys,
194
+ ...loaded.writable.map(toPubkey),
195
+ ...loaded.readonly.map(toPubkey),
196
+ ];
197
+ }
198
+ return staticKeys;
199
+ }
200
+
201
+ // Legacy messages expose `accountKeys`
202
+ if ("accountKeys" in message && Array.isArray(message.accountKeys)) {
203
+ return message.accountKeys.map(toPubkey);
204
+ }
205
+
206
+ return [];
207
+ }
208
+
209
+ // ================================================================
210
+ // Internal
211
+ // ================================================================
212
+
213
+ /**
214
+ * Safely resolve an account index to a public key, returning
215
+ * `PublicKey.default` for out-of-bounds indices instead of throwing.
216
+ *
217
+ * @internal
218
+ */
219
+ function resolveAccountKey(keys: PublicKey[], index: number): PublicKey {
220
+ const key = keys[index];
221
+ return key ?? PublicKey.default;
222
+ }
223
+
224
+ /**
225
+ * Coerce a value to PublicKey. Handles both string base58 and
226
+ * PublicKey instances returned by different RPC client versions.
227
+ *
228
+ * @internal
229
+ */
230
+ function toPubkey(value: string | PublicKey): PublicKey {
231
+ if (typeof value === "string") return new PublicKey(value);
232
+ return value;
233
+ }
234
+
235
+ /**
236
+ * @internal
237
+ */
238
+ function safeDecodeInstruction(
239
+ coder: SapInstructionCoder,
240
+ data: string,
241
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
242
+ ): { name: string; data: Record<string, any> } | null {
243
+ try {
244
+ // Inner instruction data from RPC is base58-encoded
245
+ const buffer = Buffer.from(utils.bytes.bs58.decode(data));
246
+ return coder.decode(buffer);
247
+ } catch {
248
+ // Fallback: try base64 encoding (some RPC responses use base64)
249
+ try {
250
+ return coder.decode(Buffer.from(data, "base64"));
251
+ } catch {
252
+ return null;
253
+ }
254
+ }
255
+ }
@@ -0,0 +1,135 @@
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
+
28
+ import type { PublicKey, TransactionInstruction } from "@solana/web3.js";
29
+ import type { DecodedSapInstruction, SapInstructionCoder } from "./types";
30
+
31
+ // ================================================================
32
+ // Public API
33
+ // ================================================================
34
+
35
+ /**
36
+ * Decode an array of `TransactionInstruction` and return only the
37
+ * SAP instructions with their decoded names and arguments.
38
+ *
39
+ * Non-SAP instructions (system, token, other programs) are silently
40
+ * skipped. Instructions whose data cannot be decoded against the IDL
41
+ * are still included with `name: "unknown"` and `args: null` so that
42
+ * consumers can detect IDL mismatches or unsupported instruction
43
+ * variants.
44
+ *
45
+ * @param instructions - The instruction array to inspect.
46
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
47
+ * @param sapProgramId - The SAP program public key to filter by.
48
+ * @returns An array of decoded SAP instructions.
49
+ *
50
+ * @category Parser
51
+ * @since v0.5.0
52
+ */
53
+ export function parseSapInstructionsFromList(
54
+ instructions: TransactionInstruction[],
55
+ coder: SapInstructionCoder,
56
+ sapProgramId: PublicKey,
57
+ ): DecodedSapInstruction[] {
58
+ const results: DecodedSapInstruction[] = [];
59
+
60
+ for (const ix of instructions) {
61
+ if (!ix.programId.equals(sapProgramId)) continue;
62
+
63
+ const decoded = safeDecodeInstruction(coder, ix.data);
64
+ results.push({
65
+ name: decoded?.name ?? "unknown",
66
+ args: decoded?.data ?? null,
67
+ accounts: ix.keys.map((k) => k.pubkey),
68
+ raw: ix,
69
+ });
70
+ }
71
+
72
+ return results;
73
+ }
74
+
75
+ /**
76
+ * Return only the instruction names for SAP instructions in the list.
77
+ *
78
+ * Convenience wrapper over {@link parseSapInstructionsFromList} for
79
+ * callers that only need the string names.
80
+ *
81
+ * @param instructions - The instruction array to inspect.
82
+ * @param coder - An Anchor instruction coder for the SAP IDL.
83
+ * @param sapProgramId - The SAP program public key.
84
+ * @returns An array of instruction name strings.
85
+ *
86
+ * @category Parser
87
+ * @since v0.5.0
88
+ */
89
+ export function parseSapInstructionNamesFromList(
90
+ instructions: TransactionInstruction[],
91
+ coder: SapInstructionCoder,
92
+ sapProgramId: PublicKey,
93
+ ): string[] {
94
+ return parseSapInstructionsFromList(instructions, coder, sapProgramId).map(
95
+ (ix) => ix.name,
96
+ );
97
+ }
98
+
99
+ /**
100
+ * Check whether any instruction in the list targets the SAP program.
101
+ *
102
+ * Useful as a fast pre-filter before committing to a full decode pass.
103
+ *
104
+ * @param instructions - The instruction array to inspect.
105
+ * @param sapProgramId - The SAP program public key.
106
+ * @returns `true` if at least one instruction targets the SAP program.
107
+ *
108
+ * @category Parser
109
+ * @since v0.5.0
110
+ */
111
+ export function containsSapInstruction(
112
+ instructions: TransactionInstruction[],
113
+ sapProgramId: PublicKey,
114
+ ): boolean {
115
+ return instructions.some((ix) => ix.programId.equals(sapProgramId));
116
+ }
117
+
118
+ // ================================================================
119
+ // Internal
120
+ // ================================================================
121
+
122
+ /**
123
+ * @internal
124
+ */
125
+ function safeDecodeInstruction(
126
+ coder: SapInstructionCoder,
127
+ data: Buffer | Uint8Array,
128
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
+ ): { name: string; data: Record<string, any> } | null {
130
+ try {
131
+ return coder.decode(Buffer.from(data));
132
+ } catch {
133
+ return null;
134
+ }
135
+ }
@@ -0,0 +1,200 @@
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
+
38
+ import {
39
+ TransactionMessage,
40
+ AddressLookupTableAccount,
41
+ type PublicKey,
42
+ type TransactionInstruction,
43
+ type VersionedTransactionResponse,
44
+ type TransactionResponse,
45
+ } from "@solana/web3.js";
46
+ import type { DecodedSapInstruction, SapInstructionCoder } from "./types";
47
+
48
+ // ================================================================
49
+ // Public API
50
+ // ================================================================
51
+
52
+ /**
53
+ * Extract and decode SAP instructions from a transaction response.
54
+ *
55
+ * Supports both legacy (`TransactionResponse`) and versioned
56
+ * (`VersionedTransactionResponse`) formats. For versioned
57
+ * transactions that use address lookup tables, pass the resolved
58
+ * lookup table accounts so that `TransactionMessage.decompile`
59
+ * can reconstruct the full account list.
60
+ *
61
+ * @param tx - The transaction response from `connection.getTransaction`.
62
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
63
+ * @param sapProgramId - The SAP program public key to filter by.
64
+ * @param addressLookupTables - Resolved lookup table accounts for v0 transactions.
65
+ * Required when the transaction uses address lookup tables; omit for legacy txs.
66
+ * @returns An array of decoded SAP instructions found in the transaction.
67
+ *
68
+ * @throws {Error} When the transaction message cannot be decompiled.
69
+ *
70
+ * @category Parser
71
+ * @since v0.5.0
72
+ */
73
+ export function parseSapInstructionsFromTransaction(
74
+ tx: TransactionResponse | VersionedTransactionResponse,
75
+ coder: SapInstructionCoder,
76
+ sapProgramId: PublicKey,
77
+ addressLookupTables?: AddressLookupTableAccount[],
78
+ ): DecodedSapInstruction[] {
79
+ const instructions = decompileTransaction(tx, addressLookupTables);
80
+ return decodeSapInstructions(instructions, coder, sapProgramId);
81
+ }
82
+
83
+ /**
84
+ * Extract only the SAP instruction names from a transaction response.
85
+ *
86
+ * Lighter-weight alternative to {@link parseSapInstructionsFromTransaction}
87
+ * when you only need the instruction names without decoded arguments.
88
+ *
89
+ * @param tx - The transaction response.
90
+ * @param coder - An Anchor instruction coder built from the SAP IDL.
91
+ * @param sapProgramId - The SAP program public key.
92
+ * @param addressLookupTables - Resolved lookup table accounts for v0 transactions.
93
+ * @returns An array of instruction name strings.
94
+ *
95
+ * @category Parser
96
+ * @since v0.5.0
97
+ */
98
+ export function parseSapInstructionNamesFromTransaction(
99
+ tx: TransactionResponse | VersionedTransactionResponse,
100
+ coder: SapInstructionCoder,
101
+ sapProgramId: PublicKey,
102
+ addressLookupTables?: AddressLookupTableAccount[],
103
+ ): string[] {
104
+ return parseSapInstructionsFromTransaction(
105
+ tx,
106
+ coder,
107
+ sapProgramId,
108
+ addressLookupTables,
109
+ ).map((ix) => ix.name);
110
+ }
111
+
112
+ // ================================================================
113
+ // Internal helpers
114
+ // ================================================================
115
+
116
+ /**
117
+ * Decompile a transaction response into an array of `TransactionInstruction`.
118
+ *
119
+ * Handles both legacy messages (which already contain full account keys)
120
+ * and versioned v0 messages (which require address lookup table resolution).
121
+ *
122
+ * @internal
123
+ */
124
+ function decompileTransaction(
125
+ tx: TransactionResponse | VersionedTransactionResponse,
126
+ addressLookupTables?: AddressLookupTableAccount[],
127
+ ): TransactionInstruction[] {
128
+ const message = tx.transaction.message;
129
+
130
+ // Versioned transactions expose `version` on the response object.
131
+ // Legacy transactions have either `version = "legacy"` or no field at all.
132
+ const isVersioned =
133
+ "version" in tx && tx.version !== undefined && tx.version !== "legacy";
134
+
135
+ if (isVersioned) {
136
+ // VersionedMessage requires decompile with optional lookup tables
137
+ const decompiledMessage = TransactionMessage.decompile(
138
+ message as import("@solana/web3.js").VersionedMessage,
139
+ addressLookupTables?.length
140
+ ? { addressLookupTableAccounts: addressLookupTables }
141
+ : undefined,
142
+ );
143
+ return decompiledMessage.instructions;
144
+ }
145
+
146
+ // Legacy message: decompile directly
147
+ const decompiledMessage = TransactionMessage.decompile(
148
+ message as import("@solana/web3.js").VersionedMessage,
149
+ );
150
+ return decompiledMessage.instructions;
151
+ }
152
+
153
+ /**
154
+ * Decode a list of raw `TransactionInstruction` into typed SAP results.
155
+ *
156
+ * Filters for instructions whose `programId` matches the SAP program,
157
+ * then runs each through the Anchor instruction coder.
158
+ *
159
+ * @internal
160
+ */
161
+ function decodeSapInstructions(
162
+ instructions: TransactionInstruction[],
163
+ coder: SapInstructionCoder,
164
+ sapProgramId: PublicKey,
165
+ ): DecodedSapInstruction[] {
166
+ const results: DecodedSapInstruction[] = [];
167
+
168
+ for (const ix of instructions) {
169
+ if (!ix.programId.equals(sapProgramId)) continue;
170
+
171
+ const decoded = safeDecodeInstruction(coder, ix.data);
172
+ results.push({
173
+ name: decoded?.name ?? "unknown",
174
+ args: decoded?.data ?? null,
175
+ accounts: ix.keys.map((k) => k.pubkey),
176
+ raw: ix,
177
+ });
178
+ }
179
+
180
+ return results;
181
+ }
182
+
183
+ /**
184
+ * Attempt to decode instruction data, returning `null` on failure
185
+ * instead of throwing. This prevents a single malformed instruction
186
+ * from breaking the entire parse pipeline.
187
+ *
188
+ * @internal
189
+ */
190
+ function safeDecodeInstruction(
191
+ coder: SapInstructionCoder,
192
+ data: Buffer | Uint8Array,
193
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
194
+ ): { name: string; data: Record<string, any> } | null {
195
+ try {
196
+ return coder.decode(Buffer.from(data));
197
+ } catch {
198
+ return null;
199
+ }
200
+ }