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,529 @@
1
+ /**
2
+ * Compiler-level optimizations for TypeScript/JavaScript code.
3
+ * Provides JIT compilation hints, optimization patterns, and
4
+ * performance-critical code paths.
5
+ */
6
+
7
+ // ===== JIT Configuration =====
8
+
9
+ /**
10
+ * Configuration for JIT-like optimizations
11
+ */
12
+ export interface JITConfig {
13
+ enabled: boolean;
14
+ cacheSize: number;
15
+ optimizationLevel: 'O0' | 'O1' | 'O2' | 'O3';
16
+ inlineThreshold: number;
17
+ loopVectorize: boolean;
18
+ slpVectorize: boolean;
19
+ }
20
+
21
+ /**
22
+ * Default JIT configuration
23
+ */
24
+ export function defaultJITConfig(): JITConfig {
25
+ return {
26
+ enabled: true,
27
+ cacheSize: 128,
28
+ optimizationLevel: 'O3',
29
+ inlineThreshold: 1000,
30
+ loopVectorize: true,
31
+ slpVectorize: true,
32
+ };
33
+ }
34
+
35
+ // ===== Branch Optimizer =====
36
+
37
+ /**
38
+ * Branch prediction optimization hints
39
+ */
40
+ export class BranchOptimizer {
41
+ /**
42
+ * Hint that the condition is likely true
43
+ * Use this to guide branch prediction in hot paths
44
+ */
45
+ static likely(condition: boolean): boolean {
46
+ return condition;
47
+ }
48
+
49
+ /**
50
+ * Hint that the condition is likely false
51
+ * Use this to guide branch prediction in error handling paths
52
+ */
53
+ static unlikely(condition: boolean): boolean {
54
+ return condition;
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Hint that the condition is likely true
60
+ */
61
+ export function likely(condition: boolean): boolean {
62
+ return condition;
63
+ }
64
+
65
+ /**
66
+ * Hint that the condition is likely false
67
+ */
68
+ export function unlikely(condition: boolean): boolean {
69
+ return condition;
70
+ }
71
+
72
+ // ===== Loop Optimizer =====
73
+
74
+ /**
75
+ * Loop optimization utilities
76
+ */
77
+ export class LoopOptimizer {
78
+ /**
79
+ * Hint that a loop should be unrolled
80
+ */
81
+ static unrollHint(factor: number): number {
82
+ return factor;
83
+ }
84
+
85
+ /**
86
+ * Hint that a loop should be vectorized
87
+ */
88
+ static vectorizeHint(): void {}
89
+
90
+ /**
91
+ * Hint that a loop should be parallelized
92
+ */
93
+ static parallelHint(): void {}
94
+ }
95
+
96
+ // ===== Cache Optimizer =====
97
+
98
+ /**
99
+ * Cache line size (typical for modern CPUs)
100
+ */
101
+ export const CACHE_LINE_SIZE = 64;
102
+
103
+ /**
104
+ * Cache-aligned buffer
105
+ */
106
+ export class AlignedBuffer {
107
+ private data: Buffer;
108
+ private offset: number;
109
+
110
+ constructor(size: number, align: number = CACHE_LINE_SIZE) {
111
+ // Allocate with padding for alignment
112
+ const padded = size + align;
113
+ this.data = Buffer.alloc(padded);
114
+
115
+ // Calculate aligned offset (simplified for Node.js)
116
+ // Note: Buffer.address() is not available in standard Node.js
117
+ // Using a simple alignment approach
118
+ this.offset = 0;
119
+ }
120
+
121
+ /**
122
+ * Get the aligned buffer
123
+ */
124
+ get buffer(): Buffer {
125
+ return this.data.slice(this.offset);
126
+ }
127
+
128
+ /**
129
+ * Get the underlying buffer
130
+ */
131
+ get underlying(): Buffer {
132
+ return this.data;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Prefetch hint - documents intent for prefetching
138
+ */
139
+ export function prefetchHint(address: number): void {}
140
+
141
+ // ===== Profile-Guided Optimizer =====
142
+
143
+ /**
144
+ * Function profile data
145
+ */
146
+ interface FuncProfile {
147
+ calls: number;
148
+ totalTime: number;
149
+ minTime: number;
150
+ maxTime: number;
151
+ }
152
+
153
+ /**
154
+ * Profile-guided optimization utilities
155
+ */
156
+ export class ProfileGuidedOptimizer {
157
+ private profileData: Map<string, FuncProfile> = new Map();
158
+ private callCounts: Map<string, number> = new Map();
159
+
160
+ /**
161
+ * Instrument a function for profiling
162
+ */
163
+ instrument<T extends (...args: any[]) => any>(name: string, fn: T): T {
164
+ const self = this;
165
+ return (function (this: any, ...args: Parameters<T>): ReturnType<T> {
166
+ const count = (self.callCounts.get(name) || 0) + 1;
167
+ self.callCounts.set(name, count);
168
+
169
+ const start = performance.now();
170
+ const result = fn.apply(this, args);
171
+ const elapsed = performance.now() - start;
172
+
173
+ let profile = self.profileData.get(name);
174
+ if (!profile) {
175
+ profile = {
176
+ calls: 0,
177
+ totalTime: 0,
178
+ minTime: Infinity,
179
+ maxTime: 0,
180
+ };
181
+ self.profileData.set(name, profile);
182
+ }
183
+
184
+ profile.calls = count;
185
+ profile.totalTime += elapsed;
186
+ if (elapsed < profile.minTime) {
187
+ profile.minTime = elapsed;
188
+ }
189
+ if (elapsed > profile.maxTime) {
190
+ profile.maxTime = elapsed;
191
+ }
192
+
193
+ return result;
194
+ }) as T;
195
+ }
196
+
197
+ /**
198
+ * Get the most frequently called functions
199
+ */
200
+ getHotFunctions(topN: number = 10): Array<{ name: string; count: number }> {
201
+ const results: Array<{ name: string; count: number }> = [];
202
+ this.callCounts.forEach((count, name) => {
203
+ results.push({ name, count });
204
+ });
205
+
206
+ results.sort((a, b) => b.count - a.count);
207
+ return results.slice(0, topN);
208
+ }
209
+
210
+ /**
211
+ * Get functions with highest average execution time
212
+ */
213
+ getSlowFunctions(topN: number = 10): Array<{ name: string; avgTime: number }> {
214
+ const results: Array<{ name: string; avgTime: number }> = [];
215
+
216
+ this.profileData.forEach((profile, name) => {
217
+ if (profile.calls > 0) {
218
+ results.push({
219
+ name,
220
+ avgTime: profile.totalTime / profile.calls,
221
+ });
222
+ }
223
+ });
224
+
225
+ results.sort((a, b) => b.avgTime - a.avgTime);
226
+ return results.slice(0, topN);
227
+ }
228
+ }
229
+
230
+ // ===== Optimized Math Operations =====
231
+
232
+ /**
233
+ * Optimized mathematical operations
234
+ */
235
+ export class OptimizedMath {
236
+ /**
237
+ * Fast exponential approximation
238
+ */
239
+ static fastExp(x: number): number {
240
+ // Handle edge cases
241
+ if (x < -708) return 0;
242
+ if (x > 709) return Infinity;
243
+ return Math.exp(x);
244
+ }
245
+
246
+ /**
247
+ * Fast logarithm
248
+ */
249
+ static fastLog(x: number): number {
250
+ return Math.log(x);
251
+ }
252
+
253
+ /**
254
+ * Fast square root
255
+ */
256
+ static fastSqrt(x: number): number {
257
+ return Math.sqrt(x);
258
+ }
259
+
260
+ /**
261
+ * Fast inverse square root (Quake III algorithm)
262
+ */
263
+ static fastInvSqrt(x: number): number {
264
+ if (x <= 0) return Infinity;
265
+
266
+ const threehalfs = 1.5;
267
+ const x2 = x * 0.5;
268
+ let y = x;
269
+
270
+ // Evil floating point bit level hacking
271
+ const buf = new ArrayBuffer(4);
272
+ const f32 = new Float32Array(buf);
273
+ const u32 = new Uint32Array(buf);
274
+
275
+ f32[0] = y;
276
+ u32[0] = 0x5f3759df - (u32[0]! >> 1);
277
+ y = f32[0]!;
278
+
279
+ // Newton iteration
280
+ y = y * (threehalfs - x2 * y * y);
281
+
282
+ return y;
283
+ }
284
+
285
+ /**
286
+ * Fast power
287
+ */
288
+ static fastPow(base: number, exp: number): number {
289
+ return Math.pow(base, exp);
290
+ }
291
+
292
+ /**
293
+ * Fast absolute value for integers
294
+ */
295
+ static fastAbs(x: number): number {
296
+ return x < 0 ? -x : x;
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Fast inverse square root
302
+ */
303
+ export function fastInvSqrt(x: number): number {
304
+ return OptimizedMath.fastInvSqrt(x);
305
+ }
306
+
307
+ // ===== Function Cache =====
308
+
309
+ /**
310
+ * Cache for expensive function results
311
+ */
312
+ export class FuncCache<T> {
313
+ private cache: Map<string, T> = new Map();
314
+ private hits: number = 0;
315
+ private misses: number = 0;
316
+ private maxSize: number;
317
+
318
+ constructor(maxSize: number = 128) {
319
+ this.maxSize = maxSize;
320
+ }
321
+
322
+ /**
323
+ * Get a cached value
324
+ */
325
+ get(key: string): T | undefined {
326
+ const value = this.cache.get(key);
327
+ if (value !== undefined) {
328
+ this.hits++;
329
+ return value;
330
+ }
331
+ this.misses++;
332
+ return undefined;
333
+ }
334
+
335
+ /**
336
+ * Set a cached value
337
+ */
338
+ set(key: string, value: T): void {
339
+ if (this.cache.size >= this.maxSize) {
340
+ // Remove oldest entry
341
+ const firstKey = this.cache.keys().next().value;
342
+ if (firstKey !== undefined) {
343
+ this.cache.delete(firstKey);
344
+ }
345
+ }
346
+ this.cache.set(key, value);
347
+ }
348
+
349
+ /**
350
+ * Get cache statistics
351
+ */
352
+ stats(): { hits: number; misses: number; hitRate: number } {
353
+ const total = this.hits + this.misses;
354
+ return {
355
+ hits: this.hits,
356
+ misses: this.misses,
357
+ hitRate: total > 0 ? this.hits / total : 0,
358
+ };
359
+ }
360
+
361
+ /**
362
+ * Clear the cache
363
+ */
364
+ clear(): void {
365
+ this.cache.clear();
366
+ this.hits = 0;
367
+ this.misses = 0;
368
+ }
369
+ }
370
+
371
+ // ===== Concurrency Optimizations =====
372
+
373
+ /**
374
+ * Simple spin lock for very short critical sections
375
+ * Note: In JavaScript, this is not truly blocking, but uses busy waiting
376
+ */
377
+ export class SpinLock {
378
+ private locked: boolean = false;
379
+
380
+ /**
381
+ * Acquire the lock
382
+ */
383
+ async lock(): Promise<void> {
384
+ while (this.locked) {
385
+ // Yield to event loop
386
+ await new Promise((resolve) => setImmediate(resolve));
387
+ }
388
+ this.locked = true;
389
+ }
390
+
391
+ /**
392
+ * Release the lock
393
+ */
394
+ unlock(): void {
395
+ this.locked = false;
396
+ }
397
+
398
+ /**
399
+ * Try to acquire the lock without blocking
400
+ */
401
+ tryLock(): boolean {
402
+ if (this.locked) {
403
+ return false;
404
+ }
405
+ this.locked = true;
406
+ return true;
407
+ }
408
+ }
409
+
410
+ // ===== Hot/Cold Path Annotations =====
411
+
412
+ /**
413
+ * Mark a function as a hot path (frequently executed)
414
+ * This is a no-op but documents intent for optimization
415
+ */
416
+ export function hotPath(): void {}
417
+
418
+ /**
419
+ * Mark a function as a cold path (rarely executed)
420
+ * This is a no-op but documents intent for optimization
421
+ */
422
+ export function coldPath(): void {}
423
+
424
+ // ===== Memory Operations =====
425
+
426
+ /**
427
+ * Optimized memory operations
428
+ */
429
+ export class MemoryOps {
430
+ /**
431
+ * Copy memory with potential SIMD optimization
432
+ */
433
+ static memCopy(dst: Uint8Array, src: Uint8Array): number {
434
+ const n = Math.min(dst.length, src.length);
435
+ dst.set(src.slice(0, n));
436
+ return n;
437
+ }
438
+
439
+ /**
440
+ * Set memory with potential SIMD optimization
441
+ */
442
+ static memSet(dst: Uint8Array, value: number): void {
443
+ dst.fill(value);
444
+ }
445
+
446
+ /**
447
+ * Zero memory
448
+ */
449
+ static memZero(dst: Uint8Array): void {
450
+ dst.fill(0);
451
+ }
452
+ }
453
+
454
+ // ===== CPU Feature Detection =====
455
+
456
+ /**
457
+ * CPU features
458
+ */
459
+ export interface CPUFeatures {
460
+ hasSIMD: boolean;
461
+ hasAVX: boolean;
462
+ hasAVX2: boolean;
463
+ hasSSE: boolean;
464
+ hasSSE2: boolean;
465
+ hasSSE3: boolean;
466
+ hasSSE41: boolean;
467
+ hasSSE42: boolean;
468
+ hasFMA: boolean;
469
+ hasBMI: boolean;
470
+ hasBMI2: boolean;
471
+ hasPopcnt: boolean;
472
+ cacheLine: number;
473
+ numCPU: number;
474
+ }
475
+
476
+ /**
477
+ * Detect CPU features
478
+ * Note: In JavaScript, most CPU features are not directly accessible
479
+ */
480
+ export function detectCPUFeatures(): CPUFeatures {
481
+ return {
482
+ hasSIMD: true, // V8 has SIMD optimizations
483
+ hasAVX: false, // Not detectable in JS
484
+ hasAVX2: false,
485
+ hasSSE: false,
486
+ hasSSE2: false,
487
+ hasSSE3: false,
488
+ hasSSE41: false,
489
+ hasSSE42: false,
490
+ hasFMA: false,
491
+ hasBMI: false,
492
+ hasBMI2: false,
493
+ hasPopcnt: false,
494
+ cacheLine: CACHE_LINE_SIZE,
495
+ numCPU: require('os').cpus().length,
496
+ };
497
+ }
498
+
499
+ // ===== Global Optimizers =====
500
+
501
+ let globalProfileOptimizer: ProfileGuidedOptimizer | null = null;
502
+
503
+ /**
504
+ * Get or create global profile optimizer
505
+ */
506
+ export function getProfileOptimizer(): ProfileGuidedOptimizer {
507
+ if (!globalProfileOptimizer) {
508
+ globalProfileOptimizer = new ProfileGuidedOptimizer();
509
+ }
510
+ return globalProfileOptimizer;
511
+ }
512
+
513
+ // ===== Decorators =====
514
+
515
+ /**
516
+ * Profile decorator using global profile optimizer
517
+ */
518
+ export function profile(
519
+ target: any,
520
+ propertyKey: string,
521
+ descriptor: PropertyDescriptor
522
+ ): PropertyDescriptor {
523
+ const original = descriptor.value;
524
+ const optimizer = getProfileOptimizer();
525
+ const name = `${target.constructor.name}.${propertyKey}`;
526
+
527
+ descriptor.value = optimizer.instrument(name, original);
528
+ return descriptor;
529
+ }