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,381 @@
1
+ /**
2
+ * Seed and key derivation utilities for Sol Trade SDK
3
+ * Provides mnemonic generation, seed derivation, and key pair management.
4
+ */
5
+
6
+ import { PublicKey, Keypair } from '@solana/web3.js';
7
+ import * as crypto from 'crypto';
8
+
9
+ // ===== Constants =====
10
+
11
+ const BIP39_WORDLIST_SIZE = 2048;
12
+ const SEED_LENGTH = 64;
13
+
14
+ // ===== BIP39 Wordlist (simplified - first 256 words for demo, full implementation would have all 2048)
15
+ const BIP39_WORDS: string[] = [
16
+ 'abandon', 'ability', 'able', 'about', 'above', 'absent', 'absorb', 'abstract', 'absurd', 'abuse',
17
+ 'access', 'accident', 'account', 'accuse', 'achieve', 'acid', 'acoustic', 'acquire', 'across', 'act',
18
+ 'action', 'actor', 'actress', 'actual', 'adapt', 'add', 'addict', 'address', 'adjust', 'admit',
19
+ 'adult', 'advance', 'advice', 'aerobic', 'affair', 'afford', 'afraid', 'again', 'age', 'agent',
20
+ 'agree', 'ahead', 'aim', 'air', 'airport', 'aisle', 'alarm', 'album', 'alcohol', 'alert',
21
+ 'alien', 'all', 'alley', 'allow', 'almost', 'alone', 'alpha', 'already', 'also', 'alter',
22
+ 'always', 'amateur', 'amazing', 'among', 'amount', 'amused', 'analyst', 'anchor', 'ancient', 'anger',
23
+ 'angle', 'angry', 'animal', 'ankle', 'announce', 'annual', 'another', 'answer', 'antenna', 'antique',
24
+ 'anxiety', 'any', 'apart', 'apology', 'appear', 'apple', 'approve', 'april', 'arch', 'arctic',
25
+ 'area', 'arena', 'argue', 'arm', 'armed', 'armor', 'army', 'around', 'arrange', 'arrest',
26
+ 'arrive', 'arrow', 'art', 'artefact', 'artist', 'artwork', 'ask', 'aspect', 'assault', 'asset',
27
+ 'assist', 'assume', 'asthma', 'athlete', 'atom', 'attack', 'attend', 'attitude', 'attract', 'auction',
28
+ 'audit', 'august', 'aunt', 'author', 'auto', 'autumn', 'average', 'avocado', 'avoid', 'awake',
29
+ 'aware', 'away', 'awesome', 'awful', 'awkward', 'axis', 'baby', 'bachelor', 'bacon', 'badge',
30
+ 'bag', 'balance', 'balcony', 'ball', 'bamboo', 'banana', 'banner', 'bar', 'barely', 'bargain',
31
+ 'barrel', 'base', 'basic', 'basket', 'battle', 'beach', 'bean', 'beauty', 'because', 'become',
32
+ 'beef', 'before', 'begin', 'behave', 'behind', 'believe', 'below', 'belt', 'bench', 'benefit',
33
+ 'best', 'betray', 'better', 'between', 'beyond', 'bicycle', 'bid', 'bike', 'bind', 'biology',
34
+ 'bird', 'birth', 'bitter', 'black', 'blade', 'blame', 'blanket', 'blast', 'bleak', 'bless',
35
+ 'blind', 'blood', 'blossom', 'blouse', 'blue', 'blur', 'blush', 'board', 'boat', 'body',
36
+ 'boil', 'bomb', 'bone', 'bonus', 'book', 'boost', 'border', 'boring', 'borrow', 'boss',
37
+ 'bottom', 'bounce', 'box', 'boy', 'bracket', 'brain', 'brand', 'brass', 'brave', 'bread',
38
+ 'breeze', 'brick', 'bridge', 'brief', 'bright', 'brilliant', 'bring', 'british', 'broad', 'broken',
39
+ 'bronze', 'brother', 'brown', 'brush', 'bubble', 'buddy', 'budget', 'buffalo', 'build', 'bulb',
40
+ 'bulk', 'bullet', 'bundle', 'bunker', 'burden', 'burger', 'burst', 'bus', 'business', 'busy',
41
+ 'butter', 'buyer', 'buzz', 'cabbage', 'cabin', 'cable', 'cactus', 'cage', 'cake', 'call',
42
+ 'calm', 'camera', 'camp', 'can', 'canal', 'cancel', 'candy', 'cannon', 'canoe', 'canvas',
43
+ ];
44
+
45
+ // ===== Derivation Path Types =====
46
+
47
+ /**
48
+ * BIP44 derivation path components
49
+ */
50
+ export interface DerivationPath {
51
+ purpose: number;
52
+ coinType: number;
53
+ account: number;
54
+ change: number;
55
+ addressIndex: number;
56
+ }
57
+
58
+ /**
59
+ * Parse a derivation path string
60
+ * Format: m/purpose'/coin_type'/account'/change/address_index
61
+ */
62
+ export function parseDerivationPath(path: string): DerivationPath {
63
+ const parts = path.replace(/'/g, '').split('/').slice(1);
64
+
65
+ if (parts.length < 2) {
66
+ throw new Error('Invalid derivation path: must have at least purpose and coin_type');
67
+ }
68
+
69
+ return {
70
+ purpose: parseInt(parts[0] ?? '44', 10),
71
+ coinType: parseInt(parts[1] ?? '501', 10),
72
+ account: parseInt(parts[2] || '0', 10),
73
+ change: parseInt(parts[3] || '0', 10),
74
+ addressIndex: parseInt(parts[4] || '0', 10),
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Convert derivation path to string
80
+ */
81
+ export function derivationPathToString(path: DerivationPath): string {
82
+ return `m/${path.purpose}'/${path.coinType}'/${path.account}'/${path.change}/${path.addressIndex}`;
83
+ }
84
+
85
+ /**
86
+ * Create a Solana derivation path
87
+ */
88
+ export function createSolanaDerivationPath(
89
+ account: number = 0,
90
+ change: number = 0,
91
+ addressIndex: number = 0
92
+ ): DerivationPath {
93
+ return {
94
+ purpose: 44,
95
+ coinType: 501, // Solana coin type
96
+ account,
97
+ change,
98
+ addressIndex,
99
+ };
100
+ }
101
+
102
+ // ===== Mnemonic Generation =====
103
+
104
+ /**
105
+ * Generate a random mnemonic phrase
106
+ * @param wordCount Number of words (12, 15, 18, 21, or 24)
107
+ */
108
+ export function generateMnemonic(wordCount: number = 12): string {
109
+ if (![12, 15, 18, 21, 24].includes(wordCount)) {
110
+ throw new Error('Word count must be 12, 15, 18, 21, or 24');
111
+ }
112
+
113
+ const entropyBits = (wordCount * 32) / 3;
114
+ const entropyBytes = entropyBits / 8;
115
+
116
+ // Generate random entropy
117
+ const entropy = crypto.randomBytes(entropyBytes);
118
+
119
+ // Calculate checksum
120
+ const hash = crypto.createHash('sha256').update(entropy).digest();
121
+ const checksumBits = entropyBits / 32;
122
+ const checksum = (hash[0] ?? 0) >> (8 - checksumBits);
123
+
124
+ // Combine entropy and checksum
125
+ const combined = Buffer.concat([entropy, Buffer.from([checksum])]);
126
+
127
+ // Convert to words
128
+ const words: string[] = [];
129
+ const totalBits = entropyBits + checksumBits;
130
+
131
+ for (let i = 0; i < totalBits; i += 11) {
132
+ const byteIndex = Math.floor(i / 8);
133
+ const bitOffset = i % 8;
134
+
135
+ let value = 0;
136
+ for (let j = 0; j < 11; j++) {
137
+ const currentByteIndex = byteIndex + Math.floor((bitOffset + j) / 8);
138
+ const currentBitOffset = (bitOffset + j) % 8;
139
+
140
+ if (currentByteIndex < combined.length) {
141
+ const byte = combined[currentByteIndex] ?? 0;
142
+ const bit = (byte >> (7 - currentBitOffset)) & 1;
143
+ value = (value << 1) | bit;
144
+ }
145
+ }
146
+
147
+ const word = BIP39_WORDS[value % BIP39_WORDLIST_SIZE];
148
+ if (word) {
149
+ words.push(word);
150
+ }
151
+ }
152
+
153
+ return words.join(' ');
154
+ }
155
+
156
+ /**
157
+ * Validate a mnemonic phrase
158
+ */
159
+ export function validateMnemonic(mnemonic: string): boolean {
160
+ const words = mnemonic.trim().toLowerCase().split(/\s+/);
161
+
162
+ if (![12, 15, 18, 21, 24].includes(words.length)) {
163
+ return false;
164
+ }
165
+
166
+ // Check all words are in the wordlist
167
+ for (const word of words) {
168
+ if (!BIP39_WORDS.includes(word)) {
169
+ return false;
170
+ }
171
+ }
172
+
173
+ return true;
174
+ }
175
+
176
+ /**
177
+ * Convert mnemonic to seed
178
+ */
179
+ export function mnemonicToSeed(mnemonic: string, password: string = ''): Buffer {
180
+ const salt = 'mnemonic' + password;
181
+ return crypto.pbkdf2Sync(mnemonic, salt, 2048, SEED_LENGTH, 'sha512');
182
+ }
183
+
184
+ // ===== Seed Generation =====
185
+
186
+ /**
187
+ * Seed generator for deterministic key derivation
188
+ */
189
+ export class SeedGenerator {
190
+ private seed: Buffer;
191
+
192
+ constructor(seed: Buffer | string) {
193
+ this.seed = typeof seed === 'string' ? Buffer.from(seed, 'hex') : seed;
194
+
195
+ if (this.seed.length !== SEED_LENGTH) {
196
+ throw new Error(`Seed must be ${SEED_LENGTH} bytes`);
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Create from mnemonic
202
+ */
203
+ static fromMnemonic(mnemonic: string, password: string = ''): SeedGenerator {
204
+ const seed = mnemonicToSeed(mnemonic, password);
205
+ return new SeedGenerator(seed);
206
+ }
207
+
208
+ /**
209
+ * Create from random bytes
210
+ */
211
+ static random(): SeedGenerator {
212
+ const seed = crypto.randomBytes(SEED_LENGTH);
213
+ return new SeedGenerator(seed);
214
+ }
215
+
216
+ /**
217
+ * Get the seed bytes
218
+ */
219
+ getSeed(): Buffer {
220
+ return Buffer.from(this.seed);
221
+ }
222
+
223
+ /**
224
+ * Get seed as hex string
225
+ */
226
+ toHex(): string {
227
+ return this.seed.toString('hex');
228
+ }
229
+ }
230
+
231
+ // ===== Key Pair Types =====
232
+
233
+ /**
234
+ * Extended key pair with derivation info
235
+ */
236
+ export interface KeyPair {
237
+ keypair: Keypair;
238
+ publicKey: PublicKey;
239
+ secretKey: Uint8Array;
240
+ derivationPath?: DerivationPath;
241
+ }
242
+
243
+ /**
244
+ * Create a KeyPair from a Solana Keypair
245
+ */
246
+ export function createKeyPair(keypair: Keypair, derivationPath?: DerivationPath): KeyPair {
247
+ return {
248
+ keypair,
249
+ publicKey: keypair.publicKey,
250
+ secretKey: keypair.secretKey,
251
+ derivationPath,
252
+ };
253
+ }
254
+
255
+ /**
256
+ * Generate a new random keypair
257
+ */
258
+ export function generateKeyPair(): KeyPair {
259
+ const keypair = Keypair.generate();
260
+ return createKeyPair(keypair);
261
+ }
262
+
263
+ /**
264
+ * Create keypair from seed and derivation path
265
+ * Note: This is a simplified implementation. Full BIP44 derivation requires
266
+ * proper hierarchical deterministic key derivation.
267
+ */
268
+ export function deriveKeyPair(seed: Buffer, path: DerivationPath | string): KeyPair {
269
+ const parsedPath = typeof path === 'string' ? parseDerivationPath(path) : path;
270
+
271
+ // Simplified derivation - in production, use proper BIP44 derivation
272
+ // This creates a deterministic keypair based on the seed and path
273
+ const pathString = derivationPathToString(parsedPath);
274
+ const derivedSeed = crypto
275
+ .createHmac('sha512', seed)
276
+ .update(pathString)
277
+ .digest();
278
+
279
+ // Use the first 32 bytes as the secret key
280
+ const secretKey = derivedSeed.slice(0, 32);
281
+
282
+ // Generate keypair from secret key (this is simplified)
283
+ // In practice, you'd use proper Ed25519 key derivation
284
+ const keypair = Keypair.fromSeed(secretKey);
285
+
286
+ return createKeyPair(keypair, parsedPath);
287
+ }
288
+
289
+ /**
290
+ * Derive multiple keypairs from a seed
291
+ */
292
+ export function deriveKeyPairs(
293
+ seed: Buffer,
294
+ count: number,
295
+ accountStart: number = 0
296
+ ): KeyPair[] {
297
+ const keypairs: KeyPair[] = [];
298
+
299
+ for (let i = 0; i < count; i++) {
300
+ const path = createSolanaDerivationPath(accountStart + i);
301
+ keypairs.push(deriveKeyPair(seed, path));
302
+ }
303
+
304
+ return keypairs;
305
+ }
306
+
307
+ // ===== Wallet Utilities =====
308
+
309
+ /**
310
+ * Wallet from mnemonic
311
+ */
312
+ export interface Wallet {
313
+ mnemonic: string;
314
+ seed: Buffer;
315
+ masterKey: KeyPair;
316
+ accounts: KeyPair[];
317
+ }
318
+
319
+ /**
320
+ * Create a wallet from mnemonic
321
+ */
322
+ export function createWalletFromMnemonic(
323
+ mnemonic: string,
324
+ accountCount: number = 1
325
+ ): Wallet {
326
+ const seed = mnemonicToSeed(mnemonic);
327
+ const masterKey = deriveKeyPair(seed, createSolanaDerivationPath(0));
328
+ const accounts = deriveKeyPairs(seed, accountCount);
329
+
330
+ return {
331
+ mnemonic,
332
+ seed,
333
+ masterKey,
334
+ accounts,
335
+ };
336
+ }
337
+
338
+ /**
339
+ * Generate a new wallet with random mnemonic
340
+ */
341
+ export function generateWallet(wordCount: number = 12, accountCount: number = 1): Wallet {
342
+ const mnemonic = generateMnemonic(wordCount);
343
+ return createWalletFromMnemonic(mnemonic, accountCount);
344
+ }
345
+
346
+ // ===== Convenience Functions =====
347
+
348
+ /**
349
+ * Load keypair from secret key bytes
350
+ */
351
+ export function loadKeyPair(secretKey: Uint8Array | number[]): KeyPair {
352
+ const keypair = Keypair.fromSecretKey(Uint8Array.from(secretKey));
353
+ return createKeyPair(keypair);
354
+ }
355
+
356
+ /**
357
+ * Load keypair from base58 encoded secret key
358
+ */
359
+ export function loadKeyPairFromBase58(secretKeyBase58: string): KeyPair {
360
+ // Note: This requires bs58 package
361
+ // For now, we assume it's available
362
+ try {
363
+ const bs58 = require('bs58');
364
+ const secretKey = bs58.decode(secretKeyBase58);
365
+ return loadKeyPair(secretKey);
366
+ } catch {
367
+ throw new Error('bs58 package required for base58 decoding');
368
+ }
369
+ }
370
+
371
+ /**
372
+ * Export keypair to base58 encoded secret key
373
+ */
374
+ export function exportKeyPairToBase58(keyPair: KeyPair): string {
375
+ try {
376
+ const bs58 = require('bs58');
377
+ return bs58.encode(keyPair.secretKey);
378
+ } catch {
379
+ throw new Error('bs58 package required for base58 encoding');
380
+ }
381
+ }