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,308 @@
1
+ /**
2
+ * High-Performance Cache Implementation for Sol Trade SDK
3
+ * Provides LRU, TTL, and sharded caches for optimal performance.
4
+ */
5
+
6
+ // ===== Types =====
7
+
8
+ interface CacheEntry<V> {
9
+ value: V;
10
+ expiration: number;
11
+ accessCount: number;
12
+ }
13
+
14
+ interface CacheStats {
15
+ hits: number;
16
+ misses: number;
17
+ evictions: number;
18
+ size: number;
19
+ hitRate: number;
20
+ }
21
+
22
+ // ===== LRU Cache =====
23
+
24
+ /**
25
+ * Thread-safe LRU cache with TTL support.
26
+ * O(1) get and set operations.
27
+ */
28
+ export class LRUCache<K, V> {
29
+ private cache: Map<K, CacheEntry<V>> = new Map();
30
+ private maxSize: number;
31
+ private ttl: number;
32
+ private hits: number = 0;
33
+ private misses: number = 0;
34
+ private evictions: number = 0;
35
+
36
+ constructor(maxSize: number = 1000, ttl: number = 300000) {
37
+ this.maxSize = maxSize;
38
+ this.ttl = ttl; // milliseconds
39
+ }
40
+
41
+ get(key: K): V | undefined {
42
+ const entry = this.cache.get(key);
43
+ if (!entry) {
44
+ this.misses++;
45
+ return undefined;
46
+ }
47
+
48
+ if (Date.now() > entry.expiration) {
49
+ this.cache.delete(key);
50
+ this.misses++;
51
+ return undefined;
52
+ }
53
+
54
+ // Move to end (most recently used)
55
+ this.cache.delete(key);
56
+ this.cache.set(key, entry);
57
+ entry.accessCount++;
58
+ this.hits++;
59
+ return entry.value;
60
+ }
61
+
62
+ set(key: K, value: V): void {
63
+ const expiration = Date.now() + this.ttl;
64
+
65
+ if (this.cache.has(key)) {
66
+ const entry = this.cache.get(key)!;
67
+ entry.value = value;
68
+ entry.expiration = expiration;
69
+ this.cache.delete(key);
70
+ this.cache.set(key, entry);
71
+ } else {
72
+ this.cache.set(key, {
73
+ value,
74
+ expiration,
75
+ accessCount: 0,
76
+ });
77
+
78
+ // Evict if over capacity
79
+ while (this.cache.size > this.maxSize) {
80
+ const firstKey = this.cache.keys().next().value;
81
+ if (firstKey === undefined) {
82
+ break;
83
+ }
84
+ this.cache.delete(firstKey);
85
+ this.evictions++;
86
+ }
87
+ }
88
+ }
89
+
90
+ delete(key: K): boolean {
91
+ return this.cache.delete(key);
92
+ }
93
+
94
+ clear(): void {
95
+ this.cache.clear();
96
+ this.hits = 0;
97
+ this.misses = 0;
98
+ this.evictions = 0;
99
+ }
100
+
101
+ cleanup(): number {
102
+ let removed = 0;
103
+ const now = Date.now();
104
+ for (const [key, entry] of this.cache.entries()) {
105
+ if (now > entry.expiration) {
106
+ this.cache.delete(key);
107
+ this.evictions++;
108
+ removed++;
109
+ }
110
+ }
111
+ return removed;
112
+ }
113
+
114
+ getStats(): CacheStats {
115
+ const total = this.hits + this.misses;
116
+ return {
117
+ hits: this.hits,
118
+ misses: this.misses,
119
+ evictions: this.evictions,
120
+ size: this.cache.size,
121
+ hitRate: total > 0 ? this.hits / total : 0,
122
+ };
123
+ }
124
+ }
125
+
126
+ // ===== TTL Cache =====
127
+
128
+ /**
129
+ * Simple TTL cache with fast reads.
130
+ */
131
+ export class TTLCache<K, V> {
132
+ private cache: Map<K, CacheEntry<V>> = new Map();
133
+ private ttl: number;
134
+ private hits: number = 0;
135
+ private misses: number = 0;
136
+
137
+ constructor(ttl: number = 300000) {
138
+ this.ttl = ttl;
139
+ }
140
+
141
+ get(key: K): V | undefined {
142
+ const entry = this.cache.get(key);
143
+ if (!entry) {
144
+ this.misses++;
145
+ return undefined;
146
+ }
147
+
148
+ if (Date.now() > entry.expiration) {
149
+ this.cache.delete(key);
150
+ this.misses++;
151
+ return undefined;
152
+ }
153
+
154
+ this.hits++;
155
+ return entry.value;
156
+ }
157
+
158
+ set(key: K, value: V): void {
159
+ this.cache.set(key, {
160
+ value,
161
+ expiration: Date.now() + this.ttl,
162
+ accessCount: 0,
163
+ });
164
+ }
165
+
166
+ delete(key: K): boolean {
167
+ return this.cache.delete(key);
168
+ }
169
+
170
+ cleanup(): number {
171
+ let removed = 0;
172
+ const now = Date.now();
173
+ for (const [key, entry] of this.cache.entries()) {
174
+ if (now > entry.expiration) {
175
+ this.cache.delete(key);
176
+ removed++;
177
+ }
178
+ }
179
+ return removed;
180
+ }
181
+
182
+ getStats(): CacheStats {
183
+ const total = this.hits + this.misses;
184
+ return {
185
+ hits: this.hits,
186
+ misses: this.misses,
187
+ evictions: 0,
188
+ size: this.cache.size,
189
+ hitRate: total > 0 ? this.hits / total : 0,
190
+ };
191
+ }
192
+ }
193
+
194
+ // ===== Sharded Cache =====
195
+
196
+ /**
197
+ * Sharded cache for high concurrency scenarios.
198
+ * Distributes keys across multiple shards to reduce lock contention.
199
+ */
200
+ export class ShardedCache<K, V> {
201
+ private shards: LRUCache<K, V>[];
202
+ private shardMask: number;
203
+
204
+ constructor(shards: number = 16, maxSizePerShard: number = 1000, ttl: number = 300000) {
205
+ this.shards = [];
206
+ for (let i = 0; i < shards; i++) {
207
+ this.shards.push(new LRUCache<K, V>(maxSizePerShard, ttl));
208
+ }
209
+ this.shardMask = shards - 1;
210
+ }
211
+
212
+ private getShard(key: K): LRUCache<K, V> {
213
+ // Simple hash
214
+ const hash = this.hashKey(key);
215
+ return this.shards[hash & this.shardMask]!;
216
+ }
217
+
218
+ private hashKey(key: K): number {
219
+ const str = String(key);
220
+ let hash = 0;
221
+ for (let i = 0; i < str.length; i++) {
222
+ hash = (hash << 5) - hash + str.charCodeAt(i);
223
+ hash = hash & hash;
224
+ }
225
+ return Math.abs(hash);
226
+ }
227
+
228
+ get(key: K): V | undefined {
229
+ return this.getShard(key).get(key);
230
+ }
231
+
232
+ set(key: K, value: V): void {
233
+ this.getShard(key).set(key, value);
234
+ }
235
+
236
+ delete(key: K): boolean {
237
+ return this.getShard(key).delete(key);
238
+ }
239
+
240
+ clear(): void {
241
+ for (const shard of this.shards) {
242
+ shard.clear();
243
+ }
244
+ }
245
+
246
+ getStats(): CacheStats {
247
+ let totalHits = 0;
248
+ let totalMisses = 0;
249
+ let totalEvictions = 0;
250
+ let totalSize = 0;
251
+
252
+ for (const shard of this.shards) {
253
+ const stats = shard.getStats();
254
+ totalHits += stats.hits;
255
+ totalMisses += stats.misses;
256
+ totalEvictions += stats.evictions;
257
+ totalSize += stats.size;
258
+ }
259
+
260
+ const total = totalHits + totalMisses;
261
+ return {
262
+ hits: totalHits,
263
+ misses: totalMisses,
264
+ evictions: totalEvictions,
265
+ size: totalSize,
266
+ hitRate: total > 0 ? totalHits / total : 0,
267
+ };
268
+ }
269
+ }
270
+
271
+ // ===== Function Cache Decorator =====
272
+
273
+ /**
274
+ * Decorator for caching function results.
275
+ */
276
+ export function cached<K extends any[], R>(
277
+ cache: LRUCache<string, R> | TTLCache<string, R>,
278
+ keyFn?: (...args: K) => string
279
+ ) {
280
+ return function (
281
+ target: any,
282
+ propertyKey: string,
283
+ descriptor: TypedPropertyDescriptor<(...args: K) => R>
284
+ ) {
285
+ const originalMethod = descriptor.value!;
286
+
287
+ descriptor.value = function (this: any, ...args: K): R {
288
+ const key = keyFn ? keyFn(...args) : `${propertyKey}:${JSON.stringify(args)}`;
289
+
290
+ const cached = cache.get(key);
291
+ if (cached !== undefined) {
292
+ return cached;
293
+ }
294
+
295
+ const result = originalMethod.apply(this, args);
296
+ cache.set(key, result);
297
+ return result;
298
+ };
299
+
300
+ return descriptor;
301
+ };
302
+ }
303
+
304
+ // ===== Pre-configured Global Caches =====
305
+
306
+ export const blockhashCache = new TTLCache<string, string>(2000); // 2 seconds
307
+ export const accountCache = new ShardedCache<string, Buffer>(16, 500, 10000); // 10 seconds
308
+ export const priceCache = new TTLCache<string, number>(1000); // 1 second