100x-sdk 1.0.5 → 1.0.6
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/dist/100x-sdk.cjs.js +77 -30
- package/dist/100x-sdk.esm.js +77 -30
- package/dist/100x-sdk.js +77 -30
- package/dist/100x-sdk.js.map +1 -1
- package/dist/index.d.ts +24 -2
- package/package.json +2 -2
- package/src/modules/simulator/buy_sell_token.js +7 -12
- package/src/modules/simulator/long_shrot_stop.js +30 -10
- package/src/modules/simulator/precision.js +32 -0
- package/src/modules/simulator.js +3 -3
- package/src/types/index.d.ts +24 -2
- package/src/utils/constants.js +3 -5
package/dist/100x-sdk.js
CHANGED
|
@@ -36108,11 +36108,46 @@
|
|
|
36108
36108
|
|
|
36109
36109
|
var jsonBigintExports = jsonBigint.exports;
|
|
36110
36110
|
|
|
36111
|
+
/** Format a non-negative integer ratio without converting its operands to Number. */
|
|
36112
|
+
|
|
36113
|
+
function formatRatio$2(numerator, denominator, decimals, multiplier = 1n, rounding = 'down', trim = false) {
|
|
36114
|
+
numerator = BigInt(numerator);
|
|
36115
|
+
denominator = BigInt(denominator);
|
|
36116
|
+
if (numerator < 0n || denominator <= 0n || !Number.isInteger(decimals) || decimals < 0) {
|
|
36117
|
+
throw new RangeError('Invalid ratio');
|
|
36118
|
+
}
|
|
36119
|
+
|
|
36120
|
+
const factor = 10n ** BigInt(decimals);
|
|
36121
|
+
const scaledNumerator = numerator * multiplier * factor;
|
|
36122
|
+
let quotient = scaledNumerator / denominator;
|
|
36123
|
+
if (rounding === 'half-up') {
|
|
36124
|
+
if ((scaledNumerator % denominator) * 2n >= denominator) quotient++;
|
|
36125
|
+
} else if (rounding !== 'down') {
|
|
36126
|
+
throw new RangeError('Invalid rounding mode');
|
|
36127
|
+
}
|
|
36128
|
+
|
|
36129
|
+
if (decimals === 0) return quotient.toString();
|
|
36130
|
+
const integer = quotient / factor;
|
|
36131
|
+
const fraction = (quotient % factor).toString().padStart(decimals, '0');
|
|
36132
|
+
const value = `${integer}.${fraction}`;
|
|
36133
|
+
return trim ? value.replace(/\.?0+$/, '') : value;
|
|
36134
|
+
}
|
|
36135
|
+
|
|
36136
|
+
function ceilDiv$1(numerator, denominator) {
|
|
36137
|
+
numerator = BigInt(numerator);
|
|
36138
|
+
denominator = BigInt(denominator);
|
|
36139
|
+
if (numerator < 0n || denominator <= 0n) throw new RangeError('Invalid division');
|
|
36140
|
+
return (numerator + denominator - 1n) / denominator;
|
|
36141
|
+
}
|
|
36142
|
+
|
|
36143
|
+
var precision = { formatRatio: formatRatio$2, ceilDiv: ceilDiv$1 };
|
|
36144
|
+
|
|
36111
36145
|
const Decimal$1 = decimalExports;
|
|
36112
36146
|
const CurveAMM$6 = curve_amm;
|
|
36113
36147
|
const {transformOrdersData , checkPriceRangeOverlap} = stop_loss_utils;
|
|
36114
36148
|
const { PRICE_ADJUSTMENT_PERCENTAGE, MIN_STOP_LOSS_PERCENT } = utils$2;
|
|
36115
36149
|
jsonBigintExports({ storeAsString: false });
|
|
36150
|
+
const { formatRatio: formatRatio$1, ceilDiv } = precision;
|
|
36116
36151
|
|
|
36117
36152
|
/**
|
|
36118
36153
|
* Simulate long position stop loss calculation
|
|
@@ -36144,10 +36179,11 @@
|
|
|
36144
36179
|
* - For example: 3.5 means the stop loss price is 3.5% lower than the current price
|
|
36145
36180
|
* - For a long position this value should be positive (stop loss price below current price)
|
|
36146
36181
|
*
|
|
36147
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
36182
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
36148
36183
|
* - Formula: currentPrice / (currentPrice - executableStopLossPrice)
|
|
36149
36184
|
* - For example: 28.57 means about 28.57x leverage
|
|
36150
36185
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
36186
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
36151
36187
|
*
|
|
36152
36188
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
36153
36189
|
* - The current token price used in the calculation
|
|
@@ -36381,10 +36417,17 @@
|
|
|
36381
36417
|
// Calculate stop loss percentage
|
|
36382
36418
|
let stopLossPercentage = 0;
|
|
36383
36419
|
let leverage = 1;
|
|
36420
|
+
let leverageDisplay = '1';
|
|
36384
36421
|
|
|
36385
36422
|
if (currentPrice !== executableStopLossPrice) {
|
|
36386
|
-
|
|
36387
|
-
|
|
36423
|
+
const priceDiff = currentPrice - executableStopLossPrice;
|
|
36424
|
+
stopLossPercentage = priceDiff >= 0n
|
|
36425
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
36426
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
36427
|
+
leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
36428
|
+
leverageDisplay = priceDiff > 0n
|
|
36429
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
36430
|
+
: String(leverage);
|
|
36388
36431
|
}
|
|
36389
36432
|
|
|
36390
36433
|
// Calculate margin requirement
|
|
@@ -36420,6 +36463,7 @@
|
|
|
36420
36463
|
tradeAmount: finalTradeAmount, // SOL output amount
|
|
36421
36464
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
36422
36465
|
leverage: leverage, // Leverage ratio
|
|
36466
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
36423
36467
|
currentPrice: currentPrice, // Current price
|
|
36424
36468
|
iterations: iteration, // Number of adjustments
|
|
36425
36469
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -36464,10 +36508,11 @@
|
|
|
36464
36508
|
* - For example: 3.5 means the stop loss price is 3.5% higher than the current price
|
|
36465
36509
|
* - For a short position this value should be positive (stop loss price above current price)
|
|
36466
36510
|
*
|
|
36467
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
36511
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
36468
36512
|
* - Formula: currentPrice / (executableStopLossPrice - currentPrice)
|
|
36469
36513
|
* - For example: 28.57 means about 28.57x leverage
|
|
36470
36514
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
36515
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
36471
36516
|
*
|
|
36472
36517
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
36473
36518
|
* - The current token price used in the calculation
|
|
@@ -36693,11 +36738,17 @@
|
|
|
36693
36738
|
|
|
36694
36739
|
// Calculate stop loss percentage
|
|
36695
36740
|
// For short position, stop loss price is higher than current price, so it's a positive percentage
|
|
36696
|
-
const
|
|
36741
|
+
const priceDiff = executableStopLossPrice - currentPrice;
|
|
36742
|
+
const stopLossPercentage = priceDiff >= 0n
|
|
36743
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
36744
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
36697
36745
|
|
|
36698
36746
|
// Calculate leverage ratio
|
|
36699
36747
|
// For short position, leverage = current price / (stop loss price - current price)
|
|
36700
|
-
const leverage = Number((
|
|
36748
|
+
const leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
36749
|
+
const leverageDisplay = priceDiff > 0n
|
|
36750
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
36751
|
+
: String(leverage);
|
|
36701
36752
|
|
|
36702
36753
|
// Calculate margin requirement
|
|
36703
36754
|
// Consistent with the contract formula (long_short.rs lines 890-894):
|
|
@@ -36737,6 +36788,7 @@
|
|
|
36737
36788
|
tradeAmount: finalTradeAmount, // SOL input amount (SOL needed to buy back tokens at close)
|
|
36738
36789
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
36739
36790
|
leverage: leverage, // Leverage ratio
|
|
36791
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
36740
36792
|
currentPrice: currentPrice, // Current price
|
|
36741
36793
|
iterations: iteration, // Number of adjustments
|
|
36742
36794
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -36864,8 +36916,9 @@
|
|
|
36864
36916
|
// Calculate dynamic binary search upper bound based on leverage
|
|
36865
36917
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
36866
36918
|
const priceDiff = currentPrice - stopLossPriceBigInt;
|
|
36867
|
-
|
|
36868
|
-
const
|
|
36919
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
36920
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
36921
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
36869
36922
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
36870
36923
|
|
|
36871
36924
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than buySolAmount
|
|
@@ -37050,8 +37103,9 @@
|
|
|
37050
37103
|
// Calculate dynamic binary search upper bound based on leverage
|
|
37051
37104
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
37052
37105
|
const priceDiff = stopLossPriceBigInt - currentPrice;
|
|
37053
|
-
|
|
37054
|
-
const
|
|
37106
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
37107
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
37108
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
37055
37109
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
37056
37110
|
|
|
37057
37111
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than sellSolAmount
|
|
@@ -37889,6 +37943,7 @@
|
|
|
37889
37943
|
};
|
|
37890
37944
|
|
|
37891
37945
|
const { calcLiqTokenBuy, calcLiqTokenSell } = calcLiq;
|
|
37946
|
+
const { formatRatio } = precision;
|
|
37892
37947
|
|
|
37893
37948
|
/**
|
|
37894
37949
|
* Simulate token buy transaction - calculate if target token amount can be purchased
|
|
@@ -37983,8 +38038,7 @@
|
|
|
37983
38038
|
if (freeTokenAmount >= buyTokenAmountBig) {
|
|
37984
38039
|
completionPercentage = "100.0";
|
|
37985
38040
|
} else {
|
|
37986
|
-
|
|
37987
|
-
completionPercentage = percentage.toFixed(1);
|
|
38041
|
+
completionPercentage = formatRatio(freeTokenAmount, buyTokenAmountBig, 1, 100n);
|
|
37988
38042
|
}
|
|
37989
38043
|
|
|
37990
38044
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -37995,8 +38049,7 @@
|
|
|
37995
38049
|
if (realSolAmount > 0n) {
|
|
37996
38050
|
// Normal case: calculate slippage
|
|
37997
38051
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
37998
|
-
|
|
37999
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38052
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
38000
38053
|
} else {
|
|
38001
38054
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
38002
38055
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -38021,8 +38074,7 @@
|
|
|
38021
38074
|
finalRealSolAmount = recalcRealSol;
|
|
38022
38075
|
|
|
38023
38076
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
38024
|
-
|
|
38025
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38077
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
38026
38078
|
}
|
|
38027
38079
|
|
|
38028
38080
|
// 3. Calculate suggested liquidity
|
|
@@ -38141,8 +38193,7 @@
|
|
|
38141
38193
|
if (freeTokenAmount >= sellTokenAmountBig) {
|
|
38142
38194
|
completionPercentage = "100.0";
|
|
38143
38195
|
} else {
|
|
38144
|
-
|
|
38145
|
-
completionPercentage = percentage.toFixed(1);
|
|
38196
|
+
completionPercentage = formatRatio(freeTokenAmount, sellTokenAmountBig, 1, 100n);
|
|
38146
38197
|
}
|
|
38147
38198
|
|
|
38148
38199
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -38153,8 +38204,7 @@
|
|
|
38153
38204
|
if (realSolAmount > 0n) {
|
|
38154
38205
|
// Normal case: calculate slippage
|
|
38155
38206
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
38156
|
-
|
|
38157
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38207
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
38158
38208
|
} else {
|
|
38159
38209
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
38160
38210
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -38179,8 +38229,7 @@
|
|
|
38179
38229
|
finalRealSolAmount = recalcRealSol;
|
|
38180
38230
|
|
|
38181
38231
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
38182
|
-
|
|
38183
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38232
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
38184
38233
|
}
|
|
38185
38234
|
|
|
38186
38235
|
// 3. Calculate suggested liquidity
|
|
@@ -39035,9 +39084,9 @@
|
|
|
39035
39084
|
const tokenSellResult = await this.simulateTokenSell(mint, tokenAmountBigInt, null, priceResult, ordersResult);
|
|
39036
39085
|
|
|
39037
39086
|
// Estimate ideal SOL amount
|
|
39038
|
-
|
|
39039
|
-
const
|
|
39040
|
-
const estimatedSolAmount =
|
|
39087
|
+
// Token and SOL both use 9 decimals, so their unit conversions cancel out.
|
|
39088
|
+
const priceScale = BigInt(CurveAMM$3.PRICE_PRECISION_FACTOR_DECIMAL.toFixed(0));
|
|
39089
|
+
const estimatedSolAmount = tokenAmountBigInt * currentPrice / priceScale;
|
|
39041
39090
|
|
|
39042
39091
|
// Transform result to match simulateSell format
|
|
39043
39092
|
return {
|
|
@@ -49064,8 +49113,8 @@
|
|
|
49064
49113
|
network: 'mainnet',
|
|
49065
49114
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
49066
49115
|
defaultDataSource: 'fast',
|
|
49067
|
-
solanaEndpoint: 'https://solana-rpc.
|
|
49068
|
-
fastApiUrl: 'https://api.
|
|
49116
|
+
solanaEndpoint: 'https://solana-rpc.100x.fun',
|
|
49117
|
+
fastApiUrl: 'https://api.100x.fun/',
|
|
49069
49118
|
feeRecipient: 'CmDe8JRAPJ7QpZNCb4ArVEyzyxYoCNL7WZw5qXLePULn',
|
|
49070
49119
|
baseFeeRecipient: '2xhAfEfnH8wg7ZGujSijJi4Zt4ge1ZuwMypo7etntgXA',
|
|
49071
49120
|
paramsAccount: 'CJSn3n4MVCg4qWQ7qb2nxzosYwfcRyBvmwhtM77ugu1V'
|
|
@@ -49076,7 +49125,7 @@
|
|
|
49076
49125
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
49077
49126
|
defaultDataSource: 'fast',
|
|
49078
49127
|
solanaEndpoint: 'https://lu-ura5lv-fast-devnet.helius-rpc.com',
|
|
49079
|
-
fastApiUrl: 'https://devtestapi.
|
|
49128
|
+
fastApiUrl: 'https://devtestapi.100x.fun',
|
|
49080
49129
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
49081
49130
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
49082
49131
|
paramsAccount: 'Ckz5CmbpyKtKmwgw7NDLzFnVACxekWqrX8i6vhCyLkqY'
|
|
@@ -49088,8 +49137,6 @@
|
|
|
49088
49137
|
defaultDataSource: 'fast', // 'fast' or 'chain'
|
|
49089
49138
|
solanaEndpoint: 'http://127.0.0.1:8899',
|
|
49090
49139
|
fastApiUrl: 'http://127.0.0.1:3000',
|
|
49091
|
-
// solanaEndpoint: 'http://216.158.231.58:8899',
|
|
49092
|
-
// fastApiUrl: 'http://216.158.231.58:3000',
|
|
49093
49140
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
49094
49141
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
49095
49142
|
paramsAccount: 'HPuvtLLcgSMPSyRmULPiFe9oAvm1o8mR4weqXZrUhzRM'
|