sol-trade-sdk 0.1.0

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 (87) hide show
  1. package/README.md +390 -0
  2. package/dist/chunk-MMQAMIKR.mjs +3735 -0
  3. package/dist/chunk-NEZDFAYA.mjs +7744 -0
  4. package/dist/clients-VITWK7B6.mjs +1370 -0
  5. package/dist/index-1BK_FXsW.d.mts +2327 -0
  6. package/dist/index-1BK_FXsW.d.ts +2327 -0
  7. package/dist/index.d.mts +2659 -0
  8. package/dist/index.d.ts +2659 -0
  9. package/dist/index.js +13265 -0
  10. package/dist/index.mjs +562 -0
  11. package/dist/perf/index.d.mts +2 -0
  12. package/dist/perf/index.d.ts +2 -0
  13. package/dist/perf/index.js +3742 -0
  14. package/dist/perf/index.mjs +214 -0
  15. package/package.json +101 -0
  16. package/src/__tests__/complete_sdk.test.ts +354 -0
  17. package/src/__tests__/hotpath.test.ts +486 -0
  18. package/src/__tests__/nonce.test.ts +45 -0
  19. package/src/__tests__/sdk.test.ts +425 -0
  20. package/src/address-lookup/index.ts +197 -0
  21. package/src/cache/cache.ts +308 -0
  22. package/src/calc/index.ts +1058 -0
  23. package/src/calc/pumpfun.ts +124 -0
  24. package/src/common/bonding_curve.ts +272 -0
  25. package/src/common/compute-budget.ts +148 -0
  26. package/src/common/confirm-any-signature.ts +184 -0
  27. package/src/common/fast-timing.ts +481 -0
  28. package/src/common/fast_fn.ts +150 -0
  29. package/src/common/gas-fee-strategy.ts +253 -0
  30. package/src/common/map-pool.ts +23 -0
  31. package/src/common/nonce.ts +40 -0
  32. package/src/common/sdk-log.ts +460 -0
  33. package/src/common/seed.ts +381 -0
  34. package/src/common/spl-token.ts +578 -0
  35. package/src/common/subscription-handle.ts +644 -0
  36. package/src/common/trading-utils.ts +239 -0
  37. package/src/common/wsol-manager.ts +325 -0
  38. package/src/compute/compute_budget_manager.ts +187 -0
  39. package/src/compute/index.ts +21 -0
  40. package/src/constants/index.ts +96 -0
  41. package/src/execution/execution.ts +532 -0
  42. package/src/execution/index.ts +42 -0
  43. package/src/hotpath/executor.ts +464 -0
  44. package/src/hotpath/index.ts +64 -0
  45. package/src/hotpath/state.ts +435 -0
  46. package/src/index.ts +2117 -0
  47. package/src/instruction/bonk_builder.ts +730 -0
  48. package/src/instruction/index.ts +24 -0
  49. package/src/instruction/meteora_damm_v2_builder.ts +509 -0
  50. package/src/instruction/pumpfun_builder.ts +1183 -0
  51. package/src/instruction/pumpswap.ts +1123 -0
  52. package/src/instruction/raydium_amm_v4_builder.ts +692 -0
  53. package/src/instruction/raydium_cpmm_builder.ts +795 -0
  54. package/src/middleware/traits.ts +407 -0
  55. package/src/params/index.ts +483 -0
  56. package/src/perf/compiler-optimization.ts +529 -0
  57. package/src/perf/hardware.ts +631 -0
  58. package/src/perf/index.ts +9 -0
  59. package/src/perf/kernel-bypass.ts +656 -0
  60. package/src/perf/protocol.ts +682 -0
  61. package/src/perf/realtime.ts +592 -0
  62. package/src/perf/simd.ts +668 -0
  63. package/src/perf/syscall-bypass.ts +331 -0
  64. package/src/perf/ultra-low-latency.ts +505 -0
  65. package/src/perf/zero-copy.ts +589 -0
  66. package/src/pool/pool.ts +294 -0
  67. package/src/rpc/client.ts +345 -0
  68. package/src/sdk-errors.ts +13 -0
  69. package/src/security/index.ts +26 -0
  70. package/src/security/secure-key.ts +303 -0
  71. package/src/security/validators.ts +281 -0
  72. package/src/seed/pda.ts +262 -0
  73. package/src/serialization/index.ts +28 -0
  74. package/src/serialization/serialization.ts +288 -0
  75. package/src/swqos/clients.ts +1754 -0
  76. package/src/swqos/index.ts +50 -0
  77. package/src/swqos/providers.ts +1707 -0
  78. package/src/trading/core/async-executor.ts +702 -0
  79. package/src/trading/core/confirmation-monitor.ts +711 -0
  80. package/src/trading/core/index.ts +82 -0
  81. package/src/trading/core/retry-handler.ts +683 -0
  82. package/src/trading/core/transaction-pool.ts +780 -0
  83. package/src/trading/executor.ts +385 -0
  84. package/src/trading/factory.ts +282 -0
  85. package/src/trading/index.ts +30 -0
  86. package/src/types.ts +8 -0
  87. package/src/utils/index.ts +155 -0
@@ -0,0 +1,331 @@
1
+ /**
2
+ * Syscall Bypass Module for Sol Trade SDK
3
+ * Provides high-performance time and system call optimizations.
4
+ */
5
+
6
+ // ===== Types =====
7
+
8
+ /**
9
+ * Configuration for syscall bypass optimizations
10
+ */
11
+ export interface SyscallBypassConfig {
12
+ /** Enable fast time provider using cached time */
13
+ enableFastTime: boolean;
14
+ /** Time cache interval in microseconds */
15
+ timeCacheIntervalUs: number;
16
+ /** Enable syscall batching */
17
+ enableBatching: boolean;
18
+ /** Maximum batch size for syscalls */
19
+ maxBatchSize: number;
20
+ /** Enable vDSO (virtual dynamic shared object) time */
21
+ useVdsoTime: boolean;
22
+ }
23
+
24
+ /**
25
+ * Default syscall bypass configuration
26
+ */
27
+ export function defaultSyscallBypassConfig(): SyscallBypassConfig {
28
+ return {
29
+ enableFastTime: true,
30
+ timeCacheIntervalUs: 1000, // 1ms
31
+ enableBatching: true,
32
+ maxBatchSize: 64,
33
+ useVdsoTime: true,
34
+ };
35
+ }
36
+
37
+ // ===== Fast Time Provider =====
38
+
39
+ /**
40
+ * High-performance time provider with caching to reduce syscalls.
41
+ * Uses a background update mechanism to cache current time.
42
+ */
43
+ export class FastTimeProvider {
44
+ private cachedTimeNs: bigint = BigInt(0);
45
+ private cachedTimeMs: number = 0;
46
+ private lastUpdateNs: bigint = BigInt(0);
47
+ private updateIntervalNs: bigint;
48
+ private running: boolean = false;
49
+ private updateTimer: NodeJS.Timeout | null = null;
50
+ private useHighRes: boolean;
51
+
52
+ constructor(updateIntervalUs: number = 1000, useHighRes: boolean = true) {
53
+ this.updateIntervalNs = BigInt(updateIntervalUs * 1000);
54
+ this.useHighRes = useHighRes;
55
+ this.updateTime();
56
+ }
57
+
58
+ /**
59
+ * Start the background time update loop
60
+ */
61
+ start(): void {
62
+ if (this.running) return;
63
+ this.running = true;
64
+
65
+ // Update time periodically
66
+ const intervalMs = Number(this.updateIntervalNs) / 1_000_000;
67
+ this.updateTimer = setInterval(() => {
68
+ this.updateTime();
69
+ }, Math.max(1, intervalMs));
70
+ }
71
+
72
+ /**
73
+ * Stop the background time update loop
74
+ */
75
+ stop(): void {
76
+ this.running = false;
77
+ if (this.updateTimer) {
78
+ clearInterval(this.updateTimer);
79
+ this.updateTimer = null;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Get cached time in nanoseconds (fast, no syscall)
85
+ */
86
+ nowNs(): bigint {
87
+ return this.cachedTimeNs;
88
+ }
89
+
90
+ /**
91
+ * Get cached time in microseconds (fast, no syscall)
92
+ */
93
+ nowUs(): bigint {
94
+ return this.cachedTimeNs / BigInt(1000);
95
+ }
96
+
97
+ /**
98
+ * Get cached time in milliseconds (fast, no syscall)
99
+ */
100
+ nowMs(): number {
101
+ return this.cachedTimeMs;
102
+ }
103
+
104
+ /**
105
+ * Get precise time (may involve syscall)
106
+ */
107
+ preciseNowNs(): bigint {
108
+ if (this.useHighRes && typeof process !== 'undefined' && process.hrtime) {
109
+ const [seconds, nanoseconds] = process.hrtime();
110
+ return BigInt(seconds) * BigInt(1e9) + BigInt(nanoseconds);
111
+ }
112
+ return BigInt(Date.now()) * BigInt(1e6);
113
+ }
114
+
115
+ /**
116
+ * Get precise time in microseconds
117
+ */
118
+ preciseNowUs(): bigint {
119
+ return this.preciseNowNs() / BigInt(1000);
120
+ }
121
+
122
+ private updateTime(): void {
123
+ this.cachedTimeNs = this.preciseNowNs();
124
+ this.cachedTimeMs = Date.now();
125
+ this.lastUpdateNs = this.cachedTimeNs;
126
+ }
127
+ }
128
+
129
+ // ===== Syscall Bypass Manager =====
130
+
131
+ /**
132
+ * Manages syscall bypass optimizations for high-frequency trading.
133
+ * Reduces system call overhead through caching and batching.
134
+ */
135
+ export class SyscallBypassManager {
136
+ private config: SyscallBypassConfig;
137
+ private timeProvider: FastTimeProvider;
138
+ private syscallQueue: Array<() => void> = [];
139
+ private batchTimer: NodeJS.Timeout | null = null;
140
+ private stats: SyscallStats;
141
+
142
+ constructor(config: SyscallBypassConfig = defaultSyscallBypassConfig()) {
143
+ this.config = config;
144
+ this.timeProvider = new FastTimeProvider(config.timeCacheIntervalUs);
145
+ this.stats = {
146
+ totalCalls: 0,
147
+ bypassedCalls: 0,
148
+ batchedCalls: 0,
149
+ timeSavedNs: BigInt(0),
150
+ };
151
+ }
152
+
153
+ /**
154
+ * Initialize the syscall bypass manager
155
+ */
156
+ initialize(): void {
157
+ if (this.config.enableFastTime) {
158
+ this.timeProvider.start();
159
+ }
160
+
161
+ if (this.config.enableBatching) {
162
+ this.startBatching();
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Shutdown the syscall bypass manager
168
+ */
169
+ shutdown(): void {
170
+ this.timeProvider.stop();
171
+
172
+ if (this.batchTimer) {
173
+ clearInterval(this.batchTimer);
174
+ this.batchTimer = null;
175
+ }
176
+
177
+ // Flush remaining batched syscalls
178
+ this.flushBatch();
179
+ }
180
+
181
+ /**
182
+ * Get fast cached time in nanoseconds
183
+ */
184
+ fastTimeNs(): bigint {
185
+ this.stats.totalCalls++;
186
+ this.stats.bypassedCalls++;
187
+ return this.timeProvider.nowNs();
188
+ }
189
+
190
+ /**
191
+ * Get fast cached time in microseconds
192
+ */
193
+ fastTimeUs(): bigint {
194
+ return this.timeProvider.nowUs();
195
+ }
196
+
197
+ /**
198
+ * Get fast cached time in milliseconds
199
+ */
200
+ fastTimeMs(): number {
201
+ return this.timeProvider.nowMs();
202
+ }
203
+
204
+ /**
205
+ * Queue a syscall for batching
206
+ */
207
+ queueSyscall(syscall: () => void): void {
208
+ if (!this.config.enableBatching) {
209
+ syscall();
210
+ return;
211
+ }
212
+
213
+ this.syscallQueue.push(syscall);
214
+ this.stats.batchedCalls++;
215
+
216
+ if (this.syscallQueue.length >= this.config.maxBatchSize) {
217
+ this.flushBatch();
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Execute a function with syscall bypass timing
223
+ */
224
+ measure<T>(fn: () => T): { result: T; elapsedNs: bigint } {
225
+ const start = this.timeProvider.preciseNowNs();
226
+ const result = fn();
227
+ const elapsed = this.timeProvider.preciseNowNs() - start;
228
+ return { result, elapsedNs: elapsed };
229
+ }
230
+
231
+ /**
232
+ * Get syscall bypass statistics
233
+ */
234
+ getStats(): SyscallStats {
235
+ const bypassRate = this.stats.totalCalls > 0
236
+ ? this.stats.bypassedCalls / this.stats.totalCalls
237
+ : 0;
238
+
239
+ return {
240
+ ...this.stats,
241
+ bypassRate,
242
+ };
243
+ }
244
+
245
+ /**
246
+ * Reset statistics
247
+ */
248
+ resetStats(): void {
249
+ this.stats = {
250
+ totalCalls: 0,
251
+ bypassedCalls: 0,
252
+ batchedCalls: 0,
253
+ timeSavedNs: BigInt(0),
254
+ };
255
+ }
256
+
257
+ private startBatching(): void {
258
+ // Flush batch periodically
259
+ this.batchTimer = setInterval(() => {
260
+ this.flushBatch();
261
+ }, 1); // 1ms batch window
262
+ }
263
+
264
+ private flushBatch(): void {
265
+ while (this.syscallQueue.length > 0) {
266
+ const syscall = this.syscallQueue.shift();
267
+ if (syscall) {
268
+ try {
269
+ syscall();
270
+ } catch (error) {
271
+ // Log but don't throw to prevent batch disruption
272
+ console.error('Batched syscall error:', error);
273
+ }
274
+ }
275
+ }
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Syscall bypass statistics
281
+ */
282
+ interface SyscallStats {
283
+ totalCalls: number;
284
+ bypassedCalls: number;
285
+ batchedCalls: number;
286
+ timeSavedNs: bigint;
287
+ bypassRate?: number;
288
+ }
289
+
290
+ // ===== Convenience Functions =====
291
+
292
+ /**
293
+ * Create a new syscall bypass manager with default configuration
294
+ */
295
+ export function createSyscallBypassManager(
296
+ config?: Partial<SyscallBypassConfig>
297
+ ): SyscallBypassManager {
298
+ const fullConfig = { ...defaultSyscallBypassConfig(), ...config };
299
+ return new SyscallBypassManager(fullConfig);
300
+ }
301
+
302
+ /**
303
+ * Global fast time provider instance
304
+ */
305
+ let globalTimeProvider: FastTimeProvider | null = null;
306
+
307
+ /**
308
+ * Get the global fast time provider (creates if needed)
309
+ */
310
+ export function getGlobalTimeProvider(): FastTimeProvider {
311
+ if (!globalTimeProvider) {
312
+ globalTimeProvider = new FastTimeProvider();
313
+ globalTimeProvider.start();
314
+ }
315
+ return globalTimeProvider;
316
+ }
317
+
318
+ /**
319
+ * Fast time access using global provider
320
+ */
321
+ export function fastNowNs(): bigint {
322
+ return getGlobalTimeProvider().nowNs();
323
+ }
324
+
325
+ export function fastNowUs(): bigint {
326
+ return getGlobalTimeProvider().nowUs();
327
+ }
328
+
329
+ export function fastNowMs(): number {
330
+ return getGlobalTimeProvider().nowMs();
331
+ }