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,187 @@
1
+ /**
2
+ * Compute Budget Manager - Caching compute budget instructions.
3
+ * Based on sol-trade-sdk Rust implementation patterns.
4
+ */
5
+
6
+ // ===== Constants =====
7
+
8
+ /**
9
+ * ComputeBudgetProgram is the Solana compute budget program ID
10
+ */
11
+ export const COMPUTE_BUDGET_PROGRAM = new Uint8Array([
12
+ 0x43, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x42, 0x75, 0x64, 0x67, 0x65,
13
+ 0x74, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
14
+ 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
15
+ ]);
16
+
17
+ /**
18
+ * Instruction discriminators
19
+ */
20
+ export const SET_COMPUTE_UNIT_PRICE_DISCRIMINATOR = new Uint8Array([
21
+ 0x02, 0x00, 0x00, 0x00,
22
+ ]);
23
+ export const SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR = new Uint8Array([
24
+ 0x00, 0x00, 0x00, 0x00,
25
+ ]);
26
+
27
+ // ===== Cache Key =====
28
+
29
+ /**
30
+ * Cache key for compute budget instructions
31
+ */
32
+ interface ComputeBudgetCacheKey {
33
+ unitPrice: bigint;
34
+ unitLimit: number;
35
+ }
36
+
37
+ function cacheKeyToString(key: ComputeBudgetCacheKey): string {
38
+ return `${key.unitPrice}:${key.unitLimit}`;
39
+ }
40
+
41
+ // ===== Cache =====
42
+
43
+ /**
44
+ * Stores compute budget instructions.
45
+ * Uses Map for high-performance access.
46
+ */
47
+ class ComputeBudgetCache {
48
+ private cache = new Map<string, Uint8Array[]>();
49
+
50
+ get(key: ComputeBudgetCacheKey): Uint8Array[] | undefined {
51
+ return this.cache.get(cacheKeyToString(key));
52
+ }
53
+
54
+ set(key: ComputeBudgetCacheKey, value: Uint8Array[]): void {
55
+ this.cache.set(cacheKeyToString(key), value);
56
+ }
57
+
58
+ has(key: ComputeBudgetCacheKey): boolean {
59
+ return this.cache.has(cacheKeyToString(key));
60
+ }
61
+
62
+ get size(): number {
63
+ return this.cache.size;
64
+ }
65
+
66
+ clear(): void {
67
+ this.cache.clear();
68
+ }
69
+ }
70
+
71
+ // Global cache instance
72
+ const globalCache = new ComputeBudgetCache();
73
+
74
+ // ===== Instruction Builders =====
75
+
76
+ /**
77
+ * Create set compute unit price instruction
78
+ */
79
+ export function setComputeUnitPrice(price: bigint): Uint8Array {
80
+ // Instruction: [discriminator (4 bytes) | price (8 bytes)]
81
+ const data = new Uint8Array(12);
82
+ data.set(SET_COMPUTE_UNIT_PRICE_DISCRIMINATOR, 0);
83
+
84
+ // Little-endian price (8 bytes)
85
+ const view = new DataView(data.buffer, 4, 8);
86
+ view.setBigUint64(0, price, true); // little-endian
87
+
88
+ return data;
89
+ }
90
+
91
+ /**
92
+ * Create set compute unit limit instruction
93
+ */
94
+ export function setComputeUnitLimit(limit: number): Uint8Array {
95
+ // Instruction: [discriminator (4 bytes) | limit (4 bytes)]
96
+ const data = new Uint8Array(8);
97
+ data.set(SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR, 0);
98
+
99
+ // Little-endian limit (4 bytes)
100
+ const view = new DataView(data.buffer, 4, 4);
101
+ view.setUint32(0, limit, true); // little-endian
102
+
103
+ return data;
104
+ }
105
+
106
+ // ===== Cached Instruction Functions =====
107
+
108
+ /**
109
+ * Extend instructions with compute budget instructions.
110
+ * On cache hit, extends from cached array (no allocation).
111
+ */
112
+ export function extendComputeBudgetInstructions(
113
+ instructions: Uint8Array[],
114
+ unitPrice: bigint,
115
+ unitLimit: number
116
+ ): Uint8Array[] {
117
+ const cacheKey: ComputeBudgetCacheKey = { unitPrice, unitLimit };
118
+
119
+ // Check cache
120
+ const cached = globalCache.get(cacheKey);
121
+ if (cached) {
122
+ instructions.push(...cached);
123
+ return instructions;
124
+ }
125
+
126
+ // Build new instructions
127
+ const insts: Uint8Array[] = [];
128
+ if (unitPrice > 0n) {
129
+ insts.push(setComputeUnitPrice(unitPrice));
130
+ }
131
+ if (unitLimit > 0) {
132
+ insts.push(setComputeUnitLimit(unitLimit));
133
+ }
134
+
135
+ // Store in cache
136
+ globalCache.set(cacheKey, insts);
137
+
138
+ instructions.push(...insts);
139
+ return instructions;
140
+ }
141
+
142
+ /**
143
+ * Returns compute budget instructions.
144
+ * Note: prefer extendComputeBudgetInstructions on hot path.
145
+ */
146
+ export function computeBudgetInstructions(
147
+ unitPrice: bigint,
148
+ unitLimit: number
149
+ ): Uint8Array[] {
150
+ const cacheKey: ComputeBudgetCacheKey = { unitPrice, unitLimit };
151
+
152
+ // Check cache
153
+ const cached = globalCache.get(cacheKey);
154
+ if (cached) {
155
+ return [...cached]; // Return copy
156
+ }
157
+
158
+ // Build new instructions
159
+ const insts: Uint8Array[] = [];
160
+ if (unitPrice > 0n) {
161
+ insts.push(setComputeUnitPrice(unitPrice));
162
+ }
163
+ if (unitLimit > 0) {
164
+ insts.push(setComputeUnitLimit(unitLimit));
165
+ }
166
+
167
+ // Store in cache
168
+ globalCache.set(cacheKey, insts);
169
+
170
+ return [...insts];
171
+ }
172
+
173
+ // ===== Cache Statistics =====
174
+
175
+ /**
176
+ * Get cache size
177
+ */
178
+ export function getCacheStats(): number {
179
+ return globalCache.size;
180
+ }
181
+
182
+ /**
183
+ * Clear the cache (for testing)
184
+ */
185
+ export function clearCache(): void {
186
+ globalCache.clear();
187
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Compute Budget Module for Sol Trade SDK
3
+ *
4
+ * Provides cached compute budget instructions for low-latency transaction building.
5
+ */
6
+
7
+ export {
8
+ // Constants
9
+ COMPUTE_BUDGET_PROGRAM,
10
+ SET_COMPUTE_UNIT_PRICE_DISCRIMINATOR,
11
+ SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR,
12
+ // Instruction builders
13
+ setComputeUnitPrice,
14
+ setComputeUnitLimit,
15
+ // Cached functions
16
+ extendComputeBudgetInstructions,
17
+ computeBudgetInstructions,
18
+ // Cache stats
19
+ getCacheStats,
20
+ clearCache,
21
+ } from './compute_budget_manager';
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Constants for Sol Trade SDK
3
+ */
4
+
5
+ import { PublicKey } from '@solana/web3.js';
6
+
7
+ // System programs
8
+ export const SYSTEM_PROGRAM = new PublicKey('11111111111111111111111111111111');
9
+
10
+ // Token programs
11
+ export const TOKEN_PROGRAM = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
12
+ export const TOKEN_PROGRAM_2022 = new PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
13
+
14
+ // Token mints
15
+ export const SOL_TOKEN_ACCOUNT = new PublicKey('So11111111111111111111111111111111111111111');
16
+ export const WSOL_TOKEN_ACCOUNT = new PublicKey('So11111111111111111111111111111111111111112');
17
+ export const USD1_TOKEN_ACCOUNT = new PublicKey('USD1ttGY1N17NEEHLmELoaybftRBUSErhqYiQzvEmuB');
18
+ export const USDC_TOKEN_ACCOUNT = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');
19
+
20
+ // Associated token program
21
+ export const ASSOCIATED_TOKEN_PROGRAM = new PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
22
+
23
+ // Rent sysvar
24
+ export const RENT = new PublicKey('SysvarRent111111111111111111111111111111111');
25
+
26
+ // DEX Programs — aligned with sol-trade-sdk Rust (mainnet)
27
+ export const PUMPFUN_PROGRAM = new PublicKey('6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P');
28
+ /** PumpSwap AMM program (same as `instruction/pumpswap` PUMPSWAP_PROGRAM) */
29
+ export const PUMPSWAP_PROGRAM_ID = new PublicKey('pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA');
30
+ export const BONK_PROGRAM = new PublicKey('LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj');
31
+ export const RAYDIUM_CPMM_PROGRAM = new PublicKey('CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C');
32
+ export const RAYDIUM_AMM_V4_PROGRAM = new PublicKey('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8');
33
+ export const METEORA_DAMM_V2_PROGRAM = new PublicKey('cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG');
34
+
35
+ // Fee recipients (generic SDK defaults)
36
+ export const SDK_FEE_RECIPIENT = new PublicKey('CebN5WGQ4jvEPvsVU4EoHEpgzq1VV7AbicfhtW4Cs9tM');
37
+
38
+ export const SDK_MAYHEM_FEE_RECIPIENTS: PublicKey[] = [
39
+ new PublicKey('7VtWHe8WJeU9Sy5j1XF5n8qPzDtJjWxMgYVtJ89AQrVj'),
40
+ new PublicKey('82jN8eGgPvMSW1KP9W6GdW4bQ3YbB7sGgC6BhZnLVQvR'),
41
+ ];
42
+
43
+ // Instruction discriminators — aligned with `instruction/pumpfun_builder` / Rust
44
+ export const PUMPFUN_DISCRIMINATORS = {
45
+ BUY: Buffer.from([102, 6, 61, 18, 1, 218, 235, 234]),
46
+ SELL: Buffer.from([51, 230, 133, 164, 1, 127, 131, 173]),
47
+ BUY_EXACT_SOL_IN: Buffer.from([56, 252, 116, 8, 158, 223, 205, 95]),
48
+ CLAIM_CASHBACK: Buffer.from([37, 58, 35, 126, 190, 53, 228, 197]),
49
+ };
50
+
51
+ export const PUMPSWAP_DISCRIMINATORS = {
52
+ SWAP: Buffer.from([43, 4, 237, 11, 26, 201, 30, 98]),
53
+ DEPOSIT: Buffer.from([242, 35, 198, 137, 82, 225, 242, 178]),
54
+ WITHDRAW: Buffer.from([183, 18, 178, 128, 70, 157, 46, 34]),
55
+ };
56
+
57
+ // Default values
58
+ export const DEFAULT_SLIPPAGE = 500; // 5%
59
+ export const DEFAULT_COMPUTE_UNITS = 200000;
60
+ export const DEFAULT_PRIORITY_FEE = 100000;
61
+ export const DEFAULT_TIP_LAMPORTS = 100000;
62
+
63
+ /** Aggregate for consumers expecting a single `CONSTANTS` object (matches historical `index` export). */
64
+ export const CONSTANTS = {
65
+ SYSTEM_PROGRAM,
66
+ TOKEN_PROGRAM,
67
+ TOKEN_PROGRAM_2022,
68
+ SOL_TOKEN_ACCOUNT,
69
+ WSOL_TOKEN_ACCOUNT,
70
+ USD1_TOKEN_ACCOUNT,
71
+ USDC_TOKEN_ACCOUNT,
72
+ ASSOCIATED_TOKEN_PROGRAM,
73
+ RENT,
74
+ PUMPFUN_PROGRAM,
75
+ PUMPSWAP_PROGRAM: PUMPSWAP_PROGRAM_ID,
76
+ BONK_PROGRAM,
77
+ RAYDIUM_CPMM_PROGRAM,
78
+ RAYDIUM_AMM_V4_PROGRAM,
79
+ METEORA_DAMM_V2_PROGRAM,
80
+ DEFAULT_SLIPPAGE,
81
+ DEFAULT_COMPUTE_UNITS,
82
+ DEFAULT_PRIORITY_FEE,
83
+ DEFAULT_TIP_LAMPORTS,
84
+ } as const;
85
+
86
+ // SWQOS endpoints
87
+ export const SWQOS_ENDPOINTS: Record<string, Record<string, string>> = {
88
+ Jito: {
89
+ Frankfurt: 'frankfurt.mainnet.block-engine.jito.wtf',
90
+ NewYork: 'amsterdam.mainnet.block-engine.jito.wtf',
91
+ Amsterdam: 'amsterdam.mainnet.block-engine.jito.wtf',
92
+ Tokyo: 'tokyo.mainnet.block-engine.jito.wtf',
93
+ Singapore: 'singapore.mainnet.block-engine.jito.wtf',
94
+ },
95
+ // Add more SWQOS endpoints as needed
96
+ };