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.cjs.js
CHANGED
|
@@ -47695,11 +47695,46 @@ jsonBigint.exports.stringify = json_stringify;
|
|
|
47695
47695
|
|
|
47696
47696
|
var jsonBigintExports = jsonBigint.exports;
|
|
47697
47697
|
|
|
47698
|
+
/** Format a non-negative integer ratio without converting its operands to Number. */
|
|
47699
|
+
|
|
47700
|
+
function formatRatio$2(numerator, denominator, decimals, multiplier = 1n, rounding = 'down', trim = false) {
|
|
47701
|
+
numerator = BigInt(numerator);
|
|
47702
|
+
denominator = BigInt(denominator);
|
|
47703
|
+
if (numerator < 0n || denominator <= 0n || !Number.isInteger(decimals) || decimals < 0) {
|
|
47704
|
+
throw new RangeError('Invalid ratio');
|
|
47705
|
+
}
|
|
47706
|
+
|
|
47707
|
+
const factor = 10n ** BigInt(decimals);
|
|
47708
|
+
const scaledNumerator = numerator * multiplier * factor;
|
|
47709
|
+
let quotient = scaledNumerator / denominator;
|
|
47710
|
+
if (rounding === 'half-up') {
|
|
47711
|
+
if ((scaledNumerator % denominator) * 2n >= denominator) quotient++;
|
|
47712
|
+
} else if (rounding !== 'down') {
|
|
47713
|
+
throw new RangeError('Invalid rounding mode');
|
|
47714
|
+
}
|
|
47715
|
+
|
|
47716
|
+
if (decimals === 0) return quotient.toString();
|
|
47717
|
+
const integer = quotient / factor;
|
|
47718
|
+
const fraction = (quotient % factor).toString().padStart(decimals, '0');
|
|
47719
|
+
const value = `${integer}.${fraction}`;
|
|
47720
|
+
return trim ? value.replace(/\.?0+$/, '') : value;
|
|
47721
|
+
}
|
|
47722
|
+
|
|
47723
|
+
function ceilDiv$1(numerator, denominator) {
|
|
47724
|
+
numerator = BigInt(numerator);
|
|
47725
|
+
denominator = BigInt(denominator);
|
|
47726
|
+
if (numerator < 0n || denominator <= 0n) throw new RangeError('Invalid division');
|
|
47727
|
+
return (numerator + denominator - 1n) / denominator;
|
|
47728
|
+
}
|
|
47729
|
+
|
|
47730
|
+
var precision = { formatRatio: formatRatio$2, ceilDiv: ceilDiv$1 };
|
|
47731
|
+
|
|
47698
47732
|
const Decimal$1 = decimalExports;
|
|
47699
47733
|
const CurveAMM$6 = curve_amm;
|
|
47700
47734
|
const {transformOrdersData , checkPriceRangeOverlap} = stop_loss_utils;
|
|
47701
47735
|
const { PRICE_ADJUSTMENT_PERCENTAGE, MIN_STOP_LOSS_PERCENT } = utils$2;
|
|
47702
47736
|
jsonBigintExports({ storeAsString: false });
|
|
47737
|
+
const { formatRatio: formatRatio$1, ceilDiv } = precision;
|
|
47703
47738
|
|
|
47704
47739
|
/**
|
|
47705
47740
|
* Simulate long position stop loss calculation
|
|
@@ -47731,10 +47766,11 @@ jsonBigintExports({ storeAsString: false });
|
|
|
47731
47766
|
* - For example: 3.5 means the stop loss price is 3.5% lower than the current price
|
|
47732
47767
|
* - For a long position this value should be positive (stop loss price below current price)
|
|
47733
47768
|
*
|
|
47734
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
47769
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
47735
47770
|
* - Formula: currentPrice / (currentPrice - executableStopLossPrice)
|
|
47736
47771
|
* - For example: 28.57 means about 28.57x leverage
|
|
47737
47772
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
47773
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
47738
47774
|
*
|
|
47739
47775
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
47740
47776
|
* - The current token price used in the calculation
|
|
@@ -47968,10 +48004,17 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
47968
48004
|
// Calculate stop loss percentage
|
|
47969
48005
|
let stopLossPercentage = 0;
|
|
47970
48006
|
let leverage = 1;
|
|
48007
|
+
let leverageDisplay = '1';
|
|
47971
48008
|
|
|
47972
48009
|
if (currentPrice !== executableStopLossPrice) {
|
|
47973
|
-
|
|
47974
|
-
|
|
48010
|
+
const priceDiff = currentPrice - executableStopLossPrice;
|
|
48011
|
+
stopLossPercentage = priceDiff >= 0n
|
|
48012
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
48013
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
48014
|
+
leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
48015
|
+
leverageDisplay = priceDiff > 0n
|
|
48016
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
48017
|
+
: String(leverage);
|
|
47975
48018
|
}
|
|
47976
48019
|
|
|
47977
48020
|
// Calculate margin requirement
|
|
@@ -48007,6 +48050,7 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
48007
48050
|
tradeAmount: finalTradeAmount, // SOL output amount
|
|
48008
48051
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
48009
48052
|
leverage: leverage, // Leverage ratio
|
|
48053
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
48010
48054
|
currentPrice: currentPrice, // Current price
|
|
48011
48055
|
iterations: iteration, // Number of adjustments
|
|
48012
48056
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -48051,10 +48095,11 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
48051
48095
|
* - For example: 3.5 means the stop loss price is 3.5% higher than the current price
|
|
48052
48096
|
* - For a short position this value should be positive (stop loss price above current price)
|
|
48053
48097
|
*
|
|
48054
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
48098
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
48055
48099
|
* - Formula: currentPrice / (executableStopLossPrice - currentPrice)
|
|
48056
48100
|
* - For example: 28.57 means about 28.57x leverage
|
|
48057
48101
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
48102
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
48058
48103
|
*
|
|
48059
48104
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
48060
48105
|
* - The current token price used in the calculation
|
|
@@ -48280,11 +48325,17 @@ async function simulateShortStopLoss$1(mint, sellTokenAmount, stopLossPrice, las
|
|
|
48280
48325
|
|
|
48281
48326
|
// Calculate stop loss percentage
|
|
48282
48327
|
// For short position, stop loss price is higher than current price, so it's a positive percentage
|
|
48283
|
-
const
|
|
48328
|
+
const priceDiff = executableStopLossPrice - currentPrice;
|
|
48329
|
+
const stopLossPercentage = priceDiff >= 0n
|
|
48330
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
48331
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
48284
48332
|
|
|
48285
48333
|
// Calculate leverage ratio
|
|
48286
48334
|
// For short position, leverage = current price / (stop loss price - current price)
|
|
48287
|
-
const leverage = Number((
|
|
48335
|
+
const leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
48336
|
+
const leverageDisplay = priceDiff > 0n
|
|
48337
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
48338
|
+
: String(leverage);
|
|
48288
48339
|
|
|
48289
48340
|
// Calculate margin requirement
|
|
48290
48341
|
// Consistent with the contract formula (long_short.rs lines 890-894):
|
|
@@ -48324,6 +48375,7 @@ async function simulateShortStopLoss$1(mint, sellTokenAmount, stopLossPrice, las
|
|
|
48324
48375
|
tradeAmount: finalTradeAmount, // SOL input amount (SOL needed to buy back tokens at close)
|
|
48325
48376
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
48326
48377
|
leverage: leverage, // Leverage ratio
|
|
48378
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
48327
48379
|
currentPrice: currentPrice, // Current price
|
|
48328
48380
|
iterations: iteration, // Number of adjustments
|
|
48329
48381
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -48451,8 +48503,9 @@ async function simulateLongSolStopLoss$1(mint, buySolAmount, stopLossPrice, last
|
|
|
48451
48503
|
// Calculate dynamic binary search upper bound based on leverage
|
|
48452
48504
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
48453
48505
|
const priceDiff = currentPrice - stopLossPriceBigInt;
|
|
48454
|
-
|
|
48455
|
-
const
|
|
48506
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
48507
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
48508
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
48456
48509
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
48457
48510
|
|
|
48458
48511
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than buySolAmount
|
|
@@ -48637,8 +48690,9 @@ async function simulateShortSolStopLoss$1(mint, sellSolAmount, stopLossPrice, la
|
|
|
48637
48690
|
// Calculate dynamic binary search upper bound based on leverage
|
|
48638
48691
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
48639
48692
|
const priceDiff = stopLossPriceBigInt - currentPrice;
|
|
48640
|
-
|
|
48641
|
-
const
|
|
48693
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
48694
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
48695
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
48642
48696
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
48643
48697
|
|
|
48644
48698
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than sellSolAmount
|
|
@@ -49476,6 +49530,7 @@ var calcLiq = {
|
|
|
49476
49530
|
};
|
|
49477
49531
|
|
|
49478
49532
|
const { calcLiqTokenBuy, calcLiqTokenSell } = calcLiq;
|
|
49533
|
+
const { formatRatio } = precision;
|
|
49479
49534
|
|
|
49480
49535
|
/**
|
|
49481
49536
|
* Simulate token buy transaction - calculate if target token amount can be purchased
|
|
@@ -49570,8 +49625,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
49570
49625
|
if (freeTokenAmount >= buyTokenAmountBig) {
|
|
49571
49626
|
completionPercentage = "100.0";
|
|
49572
49627
|
} else {
|
|
49573
|
-
|
|
49574
|
-
completionPercentage = percentage.toFixed(1);
|
|
49628
|
+
completionPercentage = formatRatio(freeTokenAmount, buyTokenAmountBig, 1, 100n);
|
|
49575
49629
|
}
|
|
49576
49630
|
|
|
49577
49631
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -49582,8 +49636,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
49582
49636
|
if (realSolAmount > 0n) {
|
|
49583
49637
|
// Normal case: calculate slippage
|
|
49584
49638
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
49585
|
-
|
|
49586
|
-
slippagePercentage = slippage.toFixed(1);
|
|
49639
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
49587
49640
|
} else {
|
|
49588
49641
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
49589
49642
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -49608,8 +49661,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
49608
49661
|
finalRealSolAmount = recalcRealSol;
|
|
49609
49662
|
|
|
49610
49663
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
49611
|
-
|
|
49612
|
-
slippagePercentage = slippage.toFixed(1);
|
|
49664
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
49613
49665
|
}
|
|
49614
49666
|
|
|
49615
49667
|
// 3. Calculate suggested liquidity
|
|
@@ -49728,8 +49780,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
49728
49780
|
if (freeTokenAmount >= sellTokenAmountBig) {
|
|
49729
49781
|
completionPercentage = "100.0";
|
|
49730
49782
|
} else {
|
|
49731
|
-
|
|
49732
|
-
completionPercentage = percentage.toFixed(1);
|
|
49783
|
+
completionPercentage = formatRatio(freeTokenAmount, sellTokenAmountBig, 1, 100n);
|
|
49733
49784
|
}
|
|
49734
49785
|
|
|
49735
49786
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -49740,8 +49791,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
49740
49791
|
if (realSolAmount > 0n) {
|
|
49741
49792
|
// Normal case: calculate slippage
|
|
49742
49793
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
49743
|
-
|
|
49744
|
-
slippagePercentage = slippage.toFixed(1);
|
|
49794
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
49745
49795
|
} else {
|
|
49746
49796
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
49747
49797
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -49766,8 +49816,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
49766
49816
|
finalRealSolAmount = recalcRealSol;
|
|
49767
49817
|
|
|
49768
49818
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
49769
|
-
|
|
49770
|
-
slippagePercentage = slippage.toFixed(1);
|
|
49819
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
49771
49820
|
}
|
|
49772
49821
|
|
|
49773
49822
|
// 3. Calculate suggested liquidity
|
|
@@ -50622,9 +50671,9 @@ class SimulatorModule$1 {
|
|
|
50622
50671
|
const tokenSellResult = await this.simulateTokenSell(mint, tokenAmountBigInt, null, priceResult, ordersResult);
|
|
50623
50672
|
|
|
50624
50673
|
// Estimate ideal SOL amount
|
|
50625
|
-
|
|
50626
|
-
const
|
|
50627
|
-
const estimatedSolAmount =
|
|
50674
|
+
// Token and SOL both use 9 decimals, so their unit conversions cancel out.
|
|
50675
|
+
const priceScale = BigInt(CurveAMM$3.PRICE_PRECISION_FACTOR_DECIMAL.toFixed(0));
|
|
50676
|
+
const estimatedSolAmount = tokenAmountBigInt * currentPrice / priceScale;
|
|
50628
50677
|
|
|
50629
50678
|
// Transform result to match simulateSell format
|
|
50630
50679
|
return {
|
|
@@ -60654,8 +60703,8 @@ const DEFAULT_NETWORKS = {
|
|
|
60654
60703
|
network: 'mainnet',
|
|
60655
60704
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
60656
60705
|
defaultDataSource: 'fast',
|
|
60657
|
-
solanaEndpoint: 'https://solana-rpc.
|
|
60658
|
-
fastApiUrl: 'https://api.
|
|
60706
|
+
solanaEndpoint: 'https://solana-rpc.100x.fun',
|
|
60707
|
+
fastApiUrl: 'https://api.100x.fun/',
|
|
60659
60708
|
feeRecipient: 'CmDe8JRAPJ7QpZNCb4ArVEyzyxYoCNL7WZw5qXLePULn',
|
|
60660
60709
|
baseFeeRecipient: '2xhAfEfnH8wg7ZGujSijJi4Zt4ge1ZuwMypo7etntgXA',
|
|
60661
60710
|
paramsAccount: 'CJSn3n4MVCg4qWQ7qb2nxzosYwfcRyBvmwhtM77ugu1V'
|
|
@@ -60666,7 +60715,7 @@ const DEFAULT_NETWORKS = {
|
|
|
60666
60715
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
60667
60716
|
defaultDataSource: 'fast',
|
|
60668
60717
|
solanaEndpoint: 'https://lu-ura5lv-fast-devnet.helius-rpc.com',
|
|
60669
|
-
fastApiUrl: 'https://devtestapi.
|
|
60718
|
+
fastApiUrl: 'https://devtestapi.100x.fun',
|
|
60670
60719
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
60671
60720
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
60672
60721
|
paramsAccount: 'Ckz5CmbpyKtKmwgw7NDLzFnVACxekWqrX8i6vhCyLkqY'
|
|
@@ -60678,8 +60727,6 @@ const DEFAULT_NETWORKS = {
|
|
|
60678
60727
|
defaultDataSource: 'fast', // 'fast' or 'chain'
|
|
60679
60728
|
solanaEndpoint: 'http://127.0.0.1:8899',
|
|
60680
60729
|
fastApiUrl: 'http://127.0.0.1:3000',
|
|
60681
|
-
// solanaEndpoint: 'http://216.158.231.58:8899',
|
|
60682
|
-
// fastApiUrl: 'http://216.158.231.58:3000',
|
|
60683
60730
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
60684
60731
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
60685
60732
|
paramsAccount: 'HPuvtLLcgSMPSyRmULPiFe9oAvm1o8mR4weqXZrUhzRM'
|
package/dist/100x-sdk.esm.js
CHANGED
|
@@ -36100,11 +36100,46 @@ jsonBigint.exports.stringify = json_stringify;
|
|
|
36100
36100
|
|
|
36101
36101
|
var jsonBigintExports = jsonBigint.exports;
|
|
36102
36102
|
|
|
36103
|
+
/** Format a non-negative integer ratio without converting its operands to Number. */
|
|
36104
|
+
|
|
36105
|
+
function formatRatio$2(numerator, denominator, decimals, multiplier = 1n, rounding = 'down', trim = false) {
|
|
36106
|
+
numerator = BigInt(numerator);
|
|
36107
|
+
denominator = BigInt(denominator);
|
|
36108
|
+
if (numerator < 0n || denominator <= 0n || !Number.isInteger(decimals) || decimals < 0) {
|
|
36109
|
+
throw new RangeError('Invalid ratio');
|
|
36110
|
+
}
|
|
36111
|
+
|
|
36112
|
+
const factor = 10n ** BigInt(decimals);
|
|
36113
|
+
const scaledNumerator = numerator * multiplier * factor;
|
|
36114
|
+
let quotient = scaledNumerator / denominator;
|
|
36115
|
+
if (rounding === 'half-up') {
|
|
36116
|
+
if ((scaledNumerator % denominator) * 2n >= denominator) quotient++;
|
|
36117
|
+
} else if (rounding !== 'down') {
|
|
36118
|
+
throw new RangeError('Invalid rounding mode');
|
|
36119
|
+
}
|
|
36120
|
+
|
|
36121
|
+
if (decimals === 0) return quotient.toString();
|
|
36122
|
+
const integer = quotient / factor;
|
|
36123
|
+
const fraction = (quotient % factor).toString().padStart(decimals, '0');
|
|
36124
|
+
const value = `${integer}.${fraction}`;
|
|
36125
|
+
return trim ? value.replace(/\.?0+$/, '') : value;
|
|
36126
|
+
}
|
|
36127
|
+
|
|
36128
|
+
function ceilDiv$1(numerator, denominator) {
|
|
36129
|
+
numerator = BigInt(numerator);
|
|
36130
|
+
denominator = BigInt(denominator);
|
|
36131
|
+
if (numerator < 0n || denominator <= 0n) throw new RangeError('Invalid division');
|
|
36132
|
+
return (numerator + denominator - 1n) / denominator;
|
|
36133
|
+
}
|
|
36134
|
+
|
|
36135
|
+
var precision = { formatRatio: formatRatio$2, ceilDiv: ceilDiv$1 };
|
|
36136
|
+
|
|
36103
36137
|
const Decimal$1 = decimalExports;
|
|
36104
36138
|
const CurveAMM$6 = curve_amm;
|
|
36105
36139
|
const {transformOrdersData , checkPriceRangeOverlap} = stop_loss_utils;
|
|
36106
36140
|
const { PRICE_ADJUSTMENT_PERCENTAGE, MIN_STOP_LOSS_PERCENT } = utils$2;
|
|
36107
36141
|
jsonBigintExports({ storeAsString: false });
|
|
36142
|
+
const { formatRatio: formatRatio$1, ceilDiv } = precision;
|
|
36108
36143
|
|
|
36109
36144
|
/**
|
|
36110
36145
|
* Simulate long position stop loss calculation
|
|
@@ -36136,10 +36171,11 @@ jsonBigintExports({ storeAsString: false });
|
|
|
36136
36171
|
* - For example: 3.5 means the stop loss price is 3.5% lower than the current price
|
|
36137
36172
|
* - For a long position this value should be positive (stop loss price below current price)
|
|
36138
36173
|
*
|
|
36139
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
36174
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
36140
36175
|
* - Formula: currentPrice / (currentPrice - executableStopLossPrice)
|
|
36141
36176
|
* - For example: 28.57 means about 28.57x leverage
|
|
36142
36177
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
36178
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
36143
36179
|
*
|
|
36144
36180
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
36145
36181
|
* - The current token price used in the calculation
|
|
@@ -36373,10 +36409,17 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
36373
36409
|
// Calculate stop loss percentage
|
|
36374
36410
|
let stopLossPercentage = 0;
|
|
36375
36411
|
let leverage = 1;
|
|
36412
|
+
let leverageDisplay = '1';
|
|
36376
36413
|
|
|
36377
36414
|
if (currentPrice !== executableStopLossPrice) {
|
|
36378
|
-
|
|
36379
|
-
|
|
36415
|
+
const priceDiff = currentPrice - executableStopLossPrice;
|
|
36416
|
+
stopLossPercentage = priceDiff >= 0n
|
|
36417
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
36418
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
36419
|
+
leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
36420
|
+
leverageDisplay = priceDiff > 0n
|
|
36421
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
36422
|
+
: String(leverage);
|
|
36380
36423
|
}
|
|
36381
36424
|
|
|
36382
36425
|
// Calculate margin requirement
|
|
@@ -36412,6 +36455,7 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
36412
36455
|
tradeAmount: finalTradeAmount, // SOL output amount
|
|
36413
36456
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
36414
36457
|
leverage: leverage, // Leverage ratio
|
|
36458
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
36415
36459
|
currentPrice: currentPrice, // Current price
|
|
36416
36460
|
iterations: iteration, // Number of adjustments
|
|
36417
36461
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -36456,10 +36500,11 @@ async function simulateLongStopLoss$1(mint, buyTokenAmount, stopLossPrice, lastP
|
|
|
36456
36500
|
* - For example: 3.5 means the stop loss price is 3.5% higher than the current price
|
|
36457
36501
|
* - For a short position this value should be positive (stop loss price above current price)
|
|
36458
36502
|
*
|
|
36459
|
-
* @returns {number} returns.leverage - Leverage ratio
|
|
36503
|
+
* @returns {number} returns.leverage - Leverage ratio (existing four-decimal downward truncation)
|
|
36460
36504
|
* - Formula: currentPrice / (executableStopLossPrice - currentPrice)
|
|
36461
36505
|
* - For example: 28.57 means about 28.57x leverage
|
|
36462
36506
|
* - The higher the leverage, the higher the risk, but also the higher the potential return
|
|
36507
|
+
* @returns {string} returns.leverageDisplay - Rounded display value derived from the executable stop-loss price; not a maximum leverage limit
|
|
36463
36508
|
*
|
|
36464
36509
|
* @returns {bigint} returns.currentPrice - Current price (u128 format)
|
|
36465
36510
|
* - The current token price used in the calculation
|
|
@@ -36685,11 +36730,17 @@ async function simulateShortStopLoss$1(mint, sellTokenAmount, stopLossPrice, las
|
|
|
36685
36730
|
|
|
36686
36731
|
// Calculate stop loss percentage
|
|
36687
36732
|
// For short position, stop loss price is higher than current price, so it's a positive percentage
|
|
36688
|
-
const
|
|
36733
|
+
const priceDiff = executableStopLossPrice - currentPrice;
|
|
36734
|
+
const stopLossPercentage = priceDiff >= 0n
|
|
36735
|
+
? Number(formatRatio$1(priceDiff, currentPrice, 2, 100n))
|
|
36736
|
+
: Number((10000n * priceDiff) / currentPrice) / 100;
|
|
36689
36737
|
|
|
36690
36738
|
// Calculate leverage ratio
|
|
36691
36739
|
// For short position, leverage = current price / (stop loss price - current price)
|
|
36692
|
-
const leverage = Number((
|
|
36740
|
+
const leverage = Number((10000n * currentPrice) / priceDiff) / 10000;
|
|
36741
|
+
const leverageDisplay = priceDiff > 0n
|
|
36742
|
+
? formatRatio$1(currentPrice, priceDiff, 2, 1n, 'half-up', true)
|
|
36743
|
+
: String(leverage);
|
|
36693
36744
|
|
|
36694
36745
|
// Calculate margin requirement
|
|
36695
36746
|
// Consistent with the contract formula (long_short.rs lines 890-894):
|
|
@@ -36729,6 +36780,7 @@ async function simulateShortStopLoss$1(mint, sellTokenAmount, stopLossPrice, las
|
|
|
36729
36780
|
tradeAmount: finalTradeAmount, // SOL input amount (SOL needed to buy back tokens at close)
|
|
36730
36781
|
stopLossPercentage: stopLossPercentage, // Stop loss percentage relative to current price
|
|
36731
36782
|
leverage: leverage, // Leverage ratio
|
|
36783
|
+
leverageDisplay: leverageDisplay, // Rounded display value; leverage keeps its existing meaning
|
|
36732
36784
|
currentPrice: currentPrice, // Current price
|
|
36733
36785
|
iterations: iteration, // Number of adjustments
|
|
36734
36786
|
originalStopLossPrice: BigInt(stopLossPrice), // Original stop loss price
|
|
@@ -36856,8 +36908,9 @@ async function simulateLongSolStopLoss$1(mint, buySolAmount, stopLossPrice, last
|
|
|
36856
36908
|
// Calculate dynamic binary search upper bound based on leverage
|
|
36857
36909
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
36858
36910
|
const priceDiff = currentPrice - stopLossPriceBigInt;
|
|
36859
|
-
|
|
36860
|
-
const
|
|
36911
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
36912
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
36913
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
36861
36914
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
36862
36915
|
|
|
36863
36916
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than buySolAmount
|
|
@@ -37042,8 +37095,9 @@ async function simulateShortSolStopLoss$1(mint, sellSolAmount, stopLossPrice, la
|
|
|
37042
37095
|
// Calculate dynamic binary search upper bound based on leverage
|
|
37043
37096
|
const stopLossPriceBigInt = BigInt(stopLossPrice);
|
|
37044
37097
|
const priceDiff = stopLossPriceBigInt - currentPrice;
|
|
37045
|
-
|
|
37046
|
-
const
|
|
37098
|
+
// Keep the original four-decimal leverage truncation used to size the search range.
|
|
37099
|
+
const scaledLeverage = priceDiff > 0n ? currentPrice * 10000n / priceDiff : 100000n;
|
|
37100
|
+
const safeMultiplier = ceilDiv(scaledLeverage * 3n, 10000n); // 3x safety factor
|
|
37047
37101
|
const multiplier = safeMultiplier > 10n ? safeMultiplier : 10n; // minimum 10x
|
|
37048
37102
|
|
|
37049
37103
|
// Use a binary search algorithm to find the maximum estimatedMargin that is less than sellSolAmount
|
|
@@ -37881,6 +37935,7 @@ var calcLiq = {
|
|
|
37881
37935
|
};
|
|
37882
37936
|
|
|
37883
37937
|
const { calcLiqTokenBuy, calcLiqTokenSell } = calcLiq;
|
|
37938
|
+
const { formatRatio } = precision;
|
|
37884
37939
|
|
|
37885
37940
|
/**
|
|
37886
37941
|
* Simulate token buy transaction - calculate if target token amount can be purchased
|
|
@@ -37975,8 +38030,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
37975
38030
|
if (freeTokenAmount >= buyTokenAmountBig) {
|
|
37976
38031
|
completionPercentage = "100.0";
|
|
37977
38032
|
} else {
|
|
37978
|
-
|
|
37979
|
-
completionPercentage = percentage.toFixed(1);
|
|
38033
|
+
completionPercentage = formatRatio(freeTokenAmount, buyTokenAmountBig, 1, 100n);
|
|
37980
38034
|
}
|
|
37981
38035
|
|
|
37982
38036
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -37987,8 +38041,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
37987
38041
|
if (realSolAmount > 0n) {
|
|
37988
38042
|
// Normal case: calculate slippage
|
|
37989
38043
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
37990
|
-
|
|
37991
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38044
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
37992
38045
|
} else {
|
|
37993
38046
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
37994
38047
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -38013,8 +38066,7 @@ async function simulateTokenBuy$1(mint, buyTokenAmount, passOrder = null, lastPr
|
|
|
38013
38066
|
finalRealSolAmount = recalcRealSol;
|
|
38014
38067
|
|
|
38015
38068
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
38016
|
-
|
|
38017
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38069
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
38018
38070
|
}
|
|
38019
38071
|
|
|
38020
38072
|
// 3. Calculate suggested liquidity
|
|
@@ -38133,8 +38185,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
38133
38185
|
if (freeTokenAmount >= sellTokenAmountBig) {
|
|
38134
38186
|
completionPercentage = "100.0";
|
|
38135
38187
|
} else {
|
|
38136
|
-
|
|
38137
|
-
completionPercentage = percentage.toFixed(1);
|
|
38188
|
+
completionPercentage = formatRatio(freeTokenAmount, sellTokenAmountBig, 1, 100n);
|
|
38138
38189
|
}
|
|
38139
38190
|
|
|
38140
38191
|
// 2. Calculate slippage percentage and get final SOL amount
|
|
@@ -38145,8 +38196,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
38145
38196
|
if (realSolAmount > 0n) {
|
|
38146
38197
|
// Normal case: calculate slippage
|
|
38147
38198
|
const diff = idealSolAmount > realSolAmount ? idealSolAmount - realSolAmount : realSolAmount - idealSolAmount;
|
|
38148
|
-
|
|
38149
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38199
|
+
slippagePercentage = formatRatio(diff, idealSolAmount, 1, 100n);
|
|
38150
38200
|
} else {
|
|
38151
38201
|
// Special case: real SOL amount is 0, need to recalculate with suggested liquidity
|
|
38152
38202
|
const suggestedAmount = (freeTokenAmount * BigInt(this.sdk.SUGGEST_LIQ_RATIO)) / 1000n;
|
|
@@ -38171,8 +38221,7 @@ async function simulateTokenSell$1(mint, sellTokenAmount, passOrder = null, last
|
|
|
38171
38221
|
finalRealSolAmount = recalcRealSol;
|
|
38172
38222
|
|
|
38173
38223
|
const diff = recalcIdealSol > recalcRealSol ? recalcIdealSol - recalcRealSol : recalcRealSol - recalcIdealSol;
|
|
38174
|
-
|
|
38175
|
-
slippagePercentage = slippage.toFixed(1);
|
|
38224
|
+
slippagePercentage = formatRatio(diff, recalcIdealSol, 1, 100n);
|
|
38176
38225
|
}
|
|
38177
38226
|
|
|
38178
38227
|
// 3. Calculate suggested liquidity
|
|
@@ -39027,9 +39076,9 @@ class SimulatorModule$1 {
|
|
|
39027
39076
|
const tokenSellResult = await this.simulateTokenSell(mint, tokenAmountBigInt, null, priceResult, ordersResult);
|
|
39028
39077
|
|
|
39029
39078
|
// Estimate ideal SOL amount
|
|
39030
|
-
|
|
39031
|
-
const
|
|
39032
|
-
const estimatedSolAmount =
|
|
39079
|
+
// Token and SOL both use 9 decimals, so their unit conversions cancel out.
|
|
39080
|
+
const priceScale = BigInt(CurveAMM$3.PRICE_PRECISION_FACTOR_DECIMAL.toFixed(0));
|
|
39081
|
+
const estimatedSolAmount = tokenAmountBigInt * currentPrice / priceScale;
|
|
39033
39082
|
|
|
39034
39083
|
// Transform result to match simulateSell format
|
|
39035
39084
|
return {
|
|
@@ -49056,8 +49105,8 @@ const DEFAULT_NETWORKS = {
|
|
|
49056
49105
|
network: 'mainnet',
|
|
49057
49106
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
49058
49107
|
defaultDataSource: 'fast',
|
|
49059
|
-
solanaEndpoint: 'https://solana-rpc.
|
|
49060
|
-
fastApiUrl: 'https://api.
|
|
49108
|
+
solanaEndpoint: 'https://solana-rpc.100x.fun',
|
|
49109
|
+
fastApiUrl: 'https://api.100x.fun/',
|
|
49061
49110
|
feeRecipient: 'CmDe8JRAPJ7QpZNCb4ArVEyzyxYoCNL7WZw5qXLePULn',
|
|
49062
49111
|
baseFeeRecipient: '2xhAfEfnH8wg7ZGujSijJi4Zt4ge1ZuwMypo7etntgXA',
|
|
49063
49112
|
paramsAccount: 'CJSn3n4MVCg4qWQ7qb2nxzosYwfcRyBvmwhtM77ugu1V'
|
|
@@ -49068,7 +49117,7 @@ const DEFAULT_NETWORKS = {
|
|
|
49068
49117
|
programId: 'sGecRTjTZmnqJBmLK4ZMNCzsaMrgkFfNqEcYk1GhRde',
|
|
49069
49118
|
defaultDataSource: 'fast',
|
|
49070
49119
|
solanaEndpoint: 'https://lu-ura5lv-fast-devnet.helius-rpc.com',
|
|
49071
|
-
fastApiUrl: 'https://devtestapi.
|
|
49120
|
+
fastApiUrl: 'https://devtestapi.100x.fun',
|
|
49072
49121
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
49073
49122
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
49074
49123
|
paramsAccount: 'Ckz5CmbpyKtKmwgw7NDLzFnVACxekWqrX8i6vhCyLkqY'
|
|
@@ -49080,8 +49129,6 @@ const DEFAULT_NETWORKS = {
|
|
|
49080
49129
|
defaultDataSource: 'fast', // 'fast' or 'chain'
|
|
49081
49130
|
solanaEndpoint: 'http://127.0.0.1:8899',
|
|
49082
49131
|
fastApiUrl: 'http://127.0.0.1:3000',
|
|
49083
|
-
// solanaEndpoint: 'http://216.158.231.58:8899',
|
|
49084
|
-
// fastApiUrl: 'http://216.158.231.58:3000',
|
|
49085
49132
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
49086
49133
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
49087
49134
|
paramsAccount: 'HPuvtLLcgSMPSyRmULPiFe9oAvm1o8mR4weqXZrUhzRM'
|