@oobe-protocol-labs/synapse-sap-sdk 0.6.0 → 0.6.3

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 (70) hide show
  1. package/README.md +107 -3
  2. package/dist/cjs/events/geyser.js +295 -0
  3. package/dist/cjs/events/geyser.js.map +1 -0
  4. package/dist/cjs/index.js +14 -2
  5. package/dist/cjs/index.js.map +1 -1
  6. package/dist/cjs/modules/escrow.js +23 -8
  7. package/dist/cjs/modules/escrow.js.map +1 -1
  8. package/dist/cjs/plugin/index.js +18 -2
  9. package/dist/cjs/plugin/index.js.map +1 -1
  10. package/dist/cjs/plugin/schemas.js +32 -0
  11. package/dist/cjs/plugin/schemas.js.map +1 -1
  12. package/dist/cjs/postgres/sync.js +72 -4
  13. package/dist/cjs/postgres/sync.js.map +1 -1
  14. package/dist/cjs/registries/index.js.map +1 -1
  15. package/dist/cjs/registries/x402.js +51 -8
  16. package/dist/cjs/registries/x402.js.map +1 -1
  17. package/dist/cjs/utils/index.js +10 -1
  18. package/dist/cjs/utils/index.js.map +1 -1
  19. package/dist/cjs/utils/priority-fee.js +163 -0
  20. package/dist/cjs/utils/priority-fee.js.map +1 -0
  21. package/dist/esm/events/geyser.js +258 -0
  22. package/dist/esm/events/geyser.js.map +1 -0
  23. package/dist/esm/index.js +4 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/modules/escrow.js +23 -8
  26. package/dist/esm/modules/escrow.js.map +1 -1
  27. package/dist/esm/plugin/index.js +18 -2
  28. package/dist/esm/plugin/index.js.map +1 -1
  29. package/dist/esm/plugin/schemas.js +32 -0
  30. package/dist/esm/plugin/schemas.js.map +1 -1
  31. package/dist/esm/postgres/sync.js +72 -4
  32. package/dist/esm/postgres/sync.js.map +1 -1
  33. package/dist/esm/registries/index.js.map +1 -1
  34. package/dist/esm/registries/x402.js +51 -8
  35. package/dist/esm/registries/x402.js.map +1 -1
  36. package/dist/esm/utils/index.js +2 -0
  37. package/dist/esm/utils/index.js.map +1 -1
  38. package/dist/esm/utils/priority-fee.js +158 -0
  39. package/dist/esm/utils/priority-fee.js.map +1 -0
  40. package/dist/types/events/geyser.d.ts +150 -0
  41. package/dist/types/events/geyser.d.ts.map +1 -0
  42. package/dist/types/index.d.ts +5 -1
  43. package/dist/types/index.d.ts.map +1 -1
  44. package/dist/types/modules/escrow.d.ts +7 -2
  45. package/dist/types/modules/escrow.d.ts.map +1 -1
  46. package/dist/types/plugin/index.d.ts.map +1 -1
  47. package/dist/types/plugin/schemas.d.ts +6 -0
  48. package/dist/types/plugin/schemas.d.ts.map +1 -1
  49. package/dist/types/postgres/sync.d.ts +26 -2
  50. package/dist/types/postgres/sync.d.ts.map +1 -1
  51. package/dist/types/registries/index.d.ts +1 -1
  52. package/dist/types/registries/index.d.ts.map +1 -1
  53. package/dist/types/registries/x402.d.ts +34 -2
  54. package/dist/types/registries/x402.d.ts.map +1 -1
  55. package/dist/types/utils/index.d.ts +2 -0
  56. package/dist/types/utils/index.d.ts.map +1 -1
  57. package/dist/types/utils/priority-fee.d.ts +185 -0
  58. package/dist/types/utils/priority-fee.d.ts.map +1 -0
  59. package/package.json +5 -1
  60. package/src/events/geyser.ts +384 -0
  61. package/src/events/yellowstone.d.ts +7 -0
  62. package/src/index.ts +19 -0
  63. package/src/modules/escrow.ts +33 -6
  64. package/src/plugin/index.ts +20 -0
  65. package/src/plugin/schemas.ts +32 -0
  66. package/src/postgres/sync.ts +90 -4
  67. package/src/registries/index.ts +1 -0
  68. package/src/registries/x402.ts +63 -6
  69. package/src/utils/index.ts +15 -0
  70. package/src/utils/priority-fee.ts +270 -0
@@ -0,0 +1,270 @@
1
+ /**
2
+ * @module utils/priority-fee
3
+ * @description Compute budget and priority fee utilities for SAP transactions.
4
+ *
5
+ * Solana transactions that include a priority fee (via the Compute Budget program)
6
+ * land faster because validators prefer higher-fee transactions. This module
7
+ * provides a clean, composable API for building priority-fee instructions
8
+ * that can be prepended to any Anchor method builder via `.preInstructions()`.
9
+ *
10
+ * Typical use: x402 settlement transactions where the receiving agent's RPC
11
+ * has a short confirmation window (e.g., 30 seconds).
12
+ *
13
+ * @category Utils
14
+ * @since v0.6.2
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { buildPriorityFeeIxs, DEFAULT_SETTLE_PRIORITY } from "@synapse-sap/sdk";
19
+ *
20
+ * // Append to any Anchor method builder:
21
+ * await program.methods
22
+ * .settleCalls(calls, hash)
23
+ * .accounts({ ... })
24
+ * .preInstructions(buildPriorityFeeIxs({ priorityFeeMicroLamports: 5000 }))
25
+ * .rpc({ skipPreflight: true });
26
+ * ```
27
+ */
28
+
29
+ import {
30
+ ComputeBudgetProgram,
31
+ type TransactionInstruction,
32
+ } from "@solana/web3.js";
33
+
34
+ // ═══════════════════════════════════════════════════════════════════
35
+ // Constants
36
+ // ═══════════════════════════════════════════════════════════════════
37
+
38
+ /**
39
+ * Default priority fee for settlement transactions (in microlamports).
40
+ * 5000 µL ≈ 0.0005 SOL per 200k CU — fast enough for most agent RPCs.
41
+ *
42
+ * @since v0.6.2
43
+ */
44
+ export const DEFAULT_SETTLE_PRIORITY_FEE = 5_000;
45
+
46
+ /**
47
+ * Default compute unit limit for settlement transactions.
48
+ * `settle_calls` uses ~60k CU; 100k provides a safe margin.
49
+ *
50
+ * @since v0.6.2
51
+ */
52
+ export const DEFAULT_SETTLE_COMPUTE_UNITS = 100_000;
53
+
54
+ /**
55
+ * Default compute unit limit for batch settlement transactions.
56
+ * `settle_batch` with 10 entries uses ~200k CU; 300k provides margin.
57
+ *
58
+ * @since v0.6.2
59
+ */
60
+ export const DEFAULT_BATCH_SETTLE_COMPUTE_UNITS = 300_000;
61
+
62
+ // ═══════════════════════════════════════════════════════════════════
63
+ // Types
64
+ // ═══════════════════════════════════════════════════════════════════
65
+
66
+ /**
67
+ * @interface PriorityFeeConfig
68
+ * @description Configuration for building compute budget instructions.
69
+ * @category Utils
70
+ * @since v0.6.2
71
+ */
72
+ export interface PriorityFeeConfig {
73
+ /**
74
+ * Priority fee in microlamports per compute unit.
75
+ * Higher values = faster confirmation.
76
+ *
77
+ * Common values:
78
+ * - `1000` — low priority (~0.0001 SOL)
79
+ * - `5000` — medium priority (~0.0005 SOL) ← recommended for settle
80
+ * - `50000` — high priority (~0.005 SOL)
81
+ * - `0` — no priority fee (default Solana behavior)
82
+ *
83
+ * @default 0
84
+ */
85
+ readonly priorityFeeMicroLamports?: number;
86
+
87
+ /**
88
+ * Maximum compute units the transaction may consume.
89
+ * Setting an explicit limit avoids overpaying for unused CU.
90
+ *
91
+ * @default 200_000 (Solana default)
92
+ */
93
+ readonly computeUnits?: number;
94
+ }
95
+
96
+ /**
97
+ * @interface SettleOptions
98
+ * @description Options for x402 settlement transactions.
99
+ * Controls priority fees, compute budget, and RPC behavior
100
+ * to optimize confirmation speed for time-sensitive settlements.
101
+ *
102
+ * @category Registries
103
+ * @since v0.6.2
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * // Fast settlement with priority fee
108
+ * const receipt = await x402.settle(depositor, 1, "data", {
109
+ * priorityFeeMicroLamports: 5000,
110
+ * computeUnits: 100_000,
111
+ * skipPreflight: true,
112
+ * });
113
+ *
114
+ * // Use the convenience preset
115
+ * import { FAST_SETTLE_OPTIONS } from "@synapse-sap/sdk";
116
+ * const receipt = await x402.settle(depositor, 1, "data", FAST_SETTLE_OPTIONS);
117
+ * ```
118
+ */
119
+ export interface SettleOptions extends PriorityFeeConfig {
120
+ /**
121
+ * Skip Solana simulation before submitting.
122
+ * Saves ~400ms but loses pre-flight error detection.
123
+ *
124
+ * Recommended for settlements where the escrow state
125
+ * has already been pre-fetched and validated.
126
+ *
127
+ * @default false
128
+ */
129
+ readonly skipPreflight?: boolean;
130
+
131
+ /**
132
+ * Transaction commitment level override.
133
+ * Uses the provider's default when omitted.
134
+ *
135
+ * - `"processed"` — fastest, least reliable
136
+ * - `"confirmed"` — balanced (recommended)
137
+ * - `"finalized"` — slowest, most reliable
138
+ */
139
+ readonly commitment?: "processed" | "confirmed" | "finalized";
140
+
141
+ /**
142
+ * Maximum number of RPC retry attempts.
143
+ * @default provider default (usually 3)
144
+ */
145
+ readonly maxRetries?: number;
146
+ }
147
+
148
+ // ═══════════════════════════════════════════════════════════════════
149
+ // Presets
150
+ // ═══════════════════════════════════════════════════════════════════
151
+
152
+ /**
153
+ * Recommended preset for fast x402 settlements.
154
+ * Priority fee 5000 µL, 100k CU, skip preflight, confirmed commitment.
155
+ *
156
+ * @since v0.6.2
157
+ */
158
+ export const FAST_SETTLE_OPTIONS: Readonly<SettleOptions> = Object.freeze({
159
+ priorityFeeMicroLamports: DEFAULT_SETTLE_PRIORITY_FEE,
160
+ computeUnits: DEFAULT_SETTLE_COMPUTE_UNITS,
161
+ skipPreflight: true,
162
+ commitment: "confirmed",
163
+ });
164
+
165
+ /**
166
+ * Recommended preset for fast batch settlements.
167
+ * Priority fee 5000 µL, 300k CU, skip preflight, confirmed commitment.
168
+ *
169
+ * @since v0.6.2
170
+ */
171
+ export const FAST_BATCH_SETTLE_OPTIONS: Readonly<SettleOptions> = Object.freeze({
172
+ priorityFeeMicroLamports: DEFAULT_SETTLE_PRIORITY_FEE,
173
+ computeUnits: DEFAULT_BATCH_SETTLE_COMPUTE_UNITS,
174
+ skipPreflight: true,
175
+ commitment: "confirmed",
176
+ });
177
+
178
+ // ═══════════════════════════════════════════════════════════════════
179
+ // Builder
180
+ // ═══════════════════════════════════════════════════════════════════
181
+
182
+ /**
183
+ * @name buildPriorityFeeIxs
184
+ * @description Build compute budget instructions for priority fee transactions.
185
+ *
186
+ * Returns an array of 0–2 `TransactionInstruction`s:
187
+ * - `SetComputeUnitPrice` (if `priorityFeeMicroLamports > 0`)
188
+ * - `SetComputeUnitLimit` (if `computeUnits` provided)
189
+ *
190
+ * The returned array is designed to be passed directly to
191
+ * Anchor's `.preInstructions()` builder method.
192
+ *
193
+ * @param config - Priority fee configuration.
194
+ * @returns Array of compute budget instructions (may be empty).
195
+ *
196
+ * @category Utils
197
+ * @since v0.6.2
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * const ixs = buildPriorityFeeIxs({
202
+ * priorityFeeMicroLamports: 5000,
203
+ * computeUnits: 100_000,
204
+ * });
205
+ *
206
+ * await program.methods
207
+ * .settleCalls(calls, hash)
208
+ * .accounts({ ... })
209
+ * .preInstructions(ixs)
210
+ * .rpc({ skipPreflight: true });
211
+ * ```
212
+ */
213
+ export function buildPriorityFeeIxs(
214
+ config?: PriorityFeeConfig,
215
+ ): TransactionInstruction[] {
216
+ if (!config) return [];
217
+
218
+ const ixs: TransactionInstruction[] = [];
219
+
220
+ const fee = config.priorityFeeMicroLamports ?? 0;
221
+ if (fee > 0) {
222
+ ixs.push(
223
+ ComputeBudgetProgram.setComputeUnitPrice({
224
+ microLamports: fee,
225
+ }),
226
+ );
227
+ }
228
+
229
+ const cu = config.computeUnits;
230
+ if (cu !== undefined && cu > 0) {
231
+ ixs.push(
232
+ ComputeBudgetProgram.setComputeUnitLimit({
233
+ units: cu,
234
+ }),
235
+ );
236
+ }
237
+
238
+ return ixs;
239
+ }
240
+
241
+ /**
242
+ * @name buildRpcOptions
243
+ * @description Build Anchor `.rpc()` options from {@link SettleOptions}.
244
+ *
245
+ * @param opts - Settle options.
246
+ * @returns Options object suitable for Anchor `.rpc(opts)`.
247
+ *
248
+ * @category Utils
249
+ * @since v0.6.2
250
+ * @internal
251
+ */
252
+ export function buildRpcOptions(
253
+ opts?: SettleOptions,
254
+ ): Record<string, unknown> | undefined {
255
+ if (!opts) return undefined;
256
+
257
+ const rpcOpts: Record<string, unknown> = {};
258
+
259
+ if (opts.skipPreflight !== undefined) {
260
+ rpcOpts.skipPreflight = opts.skipPreflight;
261
+ }
262
+ if (opts.commitment !== undefined) {
263
+ rpcOpts.commitment = opts.commitment;
264
+ }
265
+ if (opts.maxRetries !== undefined) {
266
+ rpcOpts.maxRetries = opts.maxRetries;
267
+ }
268
+
269
+ return Object.keys(rpcOpts).length > 0 ? rpcOpts : undefined;
270
+ }