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.
- package/README.md +390 -0
- package/dist/chunk-MMQAMIKR.mjs +3735 -0
- package/dist/chunk-NEZDFAYA.mjs +7744 -0
- package/dist/clients-VITWK7B6.mjs +1370 -0
- package/dist/index-1BK_FXsW.d.mts +2327 -0
- package/dist/index-1BK_FXsW.d.ts +2327 -0
- package/dist/index.d.mts +2659 -0
- package/dist/index.d.ts +2659 -0
- package/dist/index.js +13265 -0
- package/dist/index.mjs +562 -0
- package/dist/perf/index.d.mts +2 -0
- package/dist/perf/index.d.ts +2 -0
- package/dist/perf/index.js +3742 -0
- package/dist/perf/index.mjs +214 -0
- package/package.json +101 -0
- package/src/__tests__/complete_sdk.test.ts +354 -0
- package/src/__tests__/hotpath.test.ts +486 -0
- package/src/__tests__/nonce.test.ts +45 -0
- package/src/__tests__/sdk.test.ts +425 -0
- package/src/address-lookup/index.ts +197 -0
- package/src/cache/cache.ts +308 -0
- package/src/calc/index.ts +1058 -0
- package/src/calc/pumpfun.ts +124 -0
- package/src/common/bonding_curve.ts +272 -0
- package/src/common/compute-budget.ts +148 -0
- package/src/common/confirm-any-signature.ts +184 -0
- package/src/common/fast-timing.ts +481 -0
- package/src/common/fast_fn.ts +150 -0
- package/src/common/gas-fee-strategy.ts +253 -0
- package/src/common/map-pool.ts +23 -0
- package/src/common/nonce.ts +40 -0
- package/src/common/sdk-log.ts +460 -0
- package/src/common/seed.ts +381 -0
- package/src/common/spl-token.ts +578 -0
- package/src/common/subscription-handle.ts +644 -0
- package/src/common/trading-utils.ts +239 -0
- package/src/common/wsol-manager.ts +325 -0
- package/src/compute/compute_budget_manager.ts +187 -0
- package/src/compute/index.ts +21 -0
- package/src/constants/index.ts +96 -0
- package/src/execution/execution.ts +532 -0
- package/src/execution/index.ts +42 -0
- package/src/hotpath/executor.ts +464 -0
- package/src/hotpath/index.ts +64 -0
- package/src/hotpath/state.ts +435 -0
- package/src/index.ts +2117 -0
- package/src/instruction/bonk_builder.ts +730 -0
- package/src/instruction/index.ts +24 -0
- package/src/instruction/meteora_damm_v2_builder.ts +509 -0
- package/src/instruction/pumpfun_builder.ts +1183 -0
- package/src/instruction/pumpswap.ts +1123 -0
- package/src/instruction/raydium_amm_v4_builder.ts +692 -0
- package/src/instruction/raydium_cpmm_builder.ts +795 -0
- package/src/middleware/traits.ts +407 -0
- package/src/params/index.ts +483 -0
- package/src/perf/compiler-optimization.ts +529 -0
- package/src/perf/hardware.ts +631 -0
- package/src/perf/index.ts +9 -0
- package/src/perf/kernel-bypass.ts +656 -0
- package/src/perf/protocol.ts +682 -0
- package/src/perf/realtime.ts +592 -0
- package/src/perf/simd.ts +668 -0
- package/src/perf/syscall-bypass.ts +331 -0
- package/src/perf/ultra-low-latency.ts +505 -0
- package/src/perf/zero-copy.ts +589 -0
- package/src/pool/pool.ts +294 -0
- package/src/rpc/client.ts +345 -0
- package/src/sdk-errors.ts +13 -0
- package/src/security/index.ts +26 -0
- package/src/security/secure-key.ts +303 -0
- package/src/security/validators.ts +281 -0
- package/src/seed/pda.ts +262 -0
- package/src/serialization/index.ts +28 -0
- package/src/serialization/serialization.ts +288 -0
- package/src/swqos/clients.ts +1754 -0
- package/src/swqos/index.ts +50 -0
- package/src/swqos/providers.ts +1707 -0
- package/src/trading/core/async-executor.ts +702 -0
- package/src/trading/core/confirmation-monitor.ts +711 -0
- package/src/trading/core/index.ts +82 -0
- package/src/trading/core/retry-handler.ts +683 -0
- package/src/trading/core/transaction-pool.ts +780 -0
- package/src/trading/executor.ts +385 -0
- package/src/trading/factory.ts +282 -0
- package/src/trading/index.ts +30 -0
- package/src/types.ts +8 -0
- package/src/utils/index.ts +155 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for Sol Trade SDK
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { PublicKey } from '@solana/web3.js';
|
|
6
|
+
|
|
7
|
+
/// Maximum slippage in basis points (99.99% = 9999 bps)
|
|
8
|
+
/// This prevents the wrap amount from doubling when slippage is 100%
|
|
9
|
+
const MAX_SLIPPAGE_BASIS_POINTS = 9999;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculate amount with slippage for buy operations
|
|
13
|
+
*
|
|
14
|
+
* Note: Basis points are clamped to MAX_SLIPPAGE_BASIS_POINTS (9999 = 99.99%)
|
|
15
|
+
* to prevent the amount from doubling when slippageBasisPoints = 10000.
|
|
16
|
+
*/
|
|
17
|
+
export function calculateWithSlippageBuy(
|
|
18
|
+
amount: bigint,
|
|
19
|
+
slippageBasisPoints: number
|
|
20
|
+
): bigint {
|
|
21
|
+
// Clamp basis points to max 9999 (99.99%) to prevent amount doubling at 100%
|
|
22
|
+
const bps = slippageBasisPoints > MAX_SLIPPAGE_BASIS_POINTS
|
|
23
|
+
? MAX_SLIPPAGE_BASIS_POINTS
|
|
24
|
+
: slippageBasisPoints;
|
|
25
|
+
return (amount * BigInt(10000 + bps)) / BigInt(10000);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Calculate amount with slippage for sell operations
|
|
30
|
+
*/
|
|
31
|
+
export function calculateWithSlippageSell(
|
|
32
|
+
amount: bigint,
|
|
33
|
+
slippageBasisPoints: number
|
|
34
|
+
): bigint {
|
|
35
|
+
return (amount * BigInt(10000 - slippageBasisPoints)) / BigInt(10000);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Calculate buy token amount from SOL amount for PumpFun
|
|
40
|
+
*/
|
|
41
|
+
export function getBuyTokenAmountFromSolAmount(
|
|
42
|
+
virtualTokenReserves: bigint,
|
|
43
|
+
virtualSolReserves: bigint,
|
|
44
|
+
_realTokenReserves: bigint,
|
|
45
|
+
_creatorFee: number,
|
|
46
|
+
solAmount: bigint
|
|
47
|
+
): bigint {
|
|
48
|
+
// Simplified calculation - full implementation requires proper bonding curve math
|
|
49
|
+
const k = virtualTokenReserves * virtualSolReserves;
|
|
50
|
+
const newSolReserves = virtualSolReserves + solAmount;
|
|
51
|
+
const newTokenReserves = k / newSolReserves;
|
|
52
|
+
const tokensOut = virtualTokenReserves - newTokenReserves;
|
|
53
|
+
return tokensOut;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Calculate sell SOL amount from token amount for PumpFun
|
|
58
|
+
*/
|
|
59
|
+
export function getSellSolAmountFromTokenAmount(
|
|
60
|
+
virtualTokenReserves: bigint,
|
|
61
|
+
virtualSolReserves: bigint,
|
|
62
|
+
_creatorFee: number,
|
|
63
|
+
tokenAmount: bigint
|
|
64
|
+
): bigint {
|
|
65
|
+
// Simplified calculation
|
|
66
|
+
const k = virtualTokenReserves * virtualSolReserves;
|
|
67
|
+
const newTokenReserves = virtualTokenReserves + tokenAmount;
|
|
68
|
+
const newSolReserves = k / newTokenReserves;
|
|
69
|
+
const solOut = virtualSolReserves - newSolReserves;
|
|
70
|
+
return solOut;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Convert lamports to SOL
|
|
75
|
+
*/
|
|
76
|
+
export function lamportsToSol(lamports: number | bigint): number {
|
|
77
|
+
return Number(lamports) / 1e9;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Convert SOL to lamports
|
|
82
|
+
*/
|
|
83
|
+
export function solToLamports(sol: number): bigint {
|
|
84
|
+
return BigInt(Math.floor(sol * 1e9));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get current timestamp in microseconds
|
|
89
|
+
*/
|
|
90
|
+
export function nowMicroseconds(): bigint {
|
|
91
|
+
return BigInt(Date.now()) * BigInt(1000);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Validate public key string
|
|
96
|
+
*/
|
|
97
|
+
export function isValidPublicKey(key: string): boolean {
|
|
98
|
+
try {
|
|
99
|
+
new PublicKey(key);
|
|
100
|
+
return true;
|
|
101
|
+
} catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Sleep for specified milliseconds
|
|
108
|
+
*/
|
|
109
|
+
export function sleep(ms: number): Promise<void> {
|
|
110
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Format public key for display (truncated)
|
|
115
|
+
*/
|
|
116
|
+
export function formatPublicKey(key: PublicKey | string, chars: number = 8): string {
|
|
117
|
+
const keyStr = typeof key === 'string' ? key : key.toBase58();
|
|
118
|
+
if (keyStr.length <= chars * 2) return keyStr;
|
|
119
|
+
return `${keyStr.slice(0, chars)}...${keyStr.slice(-chars)}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Calculate price impact percentage
|
|
124
|
+
*/
|
|
125
|
+
export function calculatePriceImpact(
|
|
126
|
+
reserveIn: bigint,
|
|
127
|
+
amountIn: bigint
|
|
128
|
+
): number {
|
|
129
|
+
return Number((amountIn * BigInt(10000)) / reserveIn) / 100;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Retry a function with exponential backoff
|
|
134
|
+
*/
|
|
135
|
+
export async function retryWithBackoff<T>(
|
|
136
|
+
fn: () => Promise<T>,
|
|
137
|
+
maxRetries: number = 3,
|
|
138
|
+
baseDelayMs: number = 1000
|
|
139
|
+
): Promise<T> {
|
|
140
|
+
let lastError: Error | undefined;
|
|
141
|
+
|
|
142
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
143
|
+
try {
|
|
144
|
+
return await fn();
|
|
145
|
+
} catch (error) {
|
|
146
|
+
lastError = error as Error;
|
|
147
|
+
if (i < maxRetries - 1) {
|
|
148
|
+
const delay = baseDelayMs * Math.pow(2, i);
|
|
149
|
+
await sleep(delay);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
throw lastError;
|
|
155
|
+
}
|