100x-sdk 1.0.2 → 1.0.4
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 +7037 -3901
- package/dist/100x-sdk.esm.js +6240 -3341
- package/dist/100x-sdk.js +6240 -3341
- package/dist/100x-sdk.js.map +1 -1
- package/dist/index.d.ts +19 -19
- package/package.json +1 -1
- package/src/modules/chain.js +26 -26
- package/src/modules/fast.js +88 -88
- package/src/modules/param.js +6 -6
- package/src/modules/simulator/buy_sell_token.js +38 -39
- package/src/modules/simulator/calcLiq.js +176 -198
- package/src/modules/simulator/calc_sol_liq.js +40 -40
- package/src/modules/simulator/close_indices.js +51 -54
- package/src/modules/simulator/long_shrot_stop.js +332 -332
- package/src/modules/simulator/stop_loss_utils.js +125 -125
- package/src/modules/simulator/utils.js +1 -2
- package/src/modules/simulator.js +14 -19
- package/src/modules/token.js +69 -69
- package/src/modules/tools.js +1 -1
- package/src/modules/trading.js +97 -97
- package/src/sdk.js +22 -23
- package/src/types/index.d.ts +19 -19
- package/src/utils/constants.js +2 -2
- package/src/utils/curve_amm.js +54 -54
- package/src/utils/orderUtils.js +1 -3
package/src/types/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Connection, PublicKey, Transaction, Keypair } from '@solana/web3.js';
|
|
2
2
|
import { BN, Wallet, Program } from '@coral-xyz/anchor';
|
|
3
3
|
|
|
4
|
-
// =========================
|
|
4
|
+
// ========================= Basic Type Definitions =========================
|
|
5
5
|
|
|
6
6
|
export type DataSourceType = 'fast' | 'chain';
|
|
7
7
|
|
|
@@ -29,7 +29,7 @@ export interface Fun100xSdkOptions {
|
|
|
29
29
|
paramsAccount?: string;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
// =========================
|
|
32
|
+
// ========================= Order and Trading Related Types =========================
|
|
33
33
|
|
|
34
34
|
export interface OrderData {
|
|
35
35
|
order_pda: string;
|
|
@@ -89,7 +89,7 @@ export interface MintInfo {
|
|
|
89
89
|
[key: string]: any;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
// =========================
|
|
92
|
+
// ========================= Trading Parameter Types =========================
|
|
93
93
|
|
|
94
94
|
export interface BuyParams {
|
|
95
95
|
mintAccount: string | PublicKey;
|
|
@@ -147,7 +147,7 @@ export interface TransactionOptions {
|
|
|
147
147
|
computeUnits?: number;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
// =========================
|
|
150
|
+
// ========================= Query Parameter Types =========================
|
|
151
151
|
|
|
152
152
|
export interface OrdersQueryOptions {
|
|
153
153
|
type?: 'up_orders' | 'down_orders';
|
|
@@ -167,7 +167,7 @@ export interface UserOrdersQueryOptions {
|
|
|
167
167
|
dataSource?: DataSourceType;
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
-
// =========================
|
|
170
|
+
// ========================= Simulator Related Types =========================
|
|
171
171
|
|
|
172
172
|
export interface SimulationResult {
|
|
173
173
|
liqResult: {
|
|
@@ -187,7 +187,7 @@ export interface SimulationResult {
|
|
|
187
187
|
suggestedSolAmount: string;
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
// =========================
|
|
190
|
+
// ========================= Utility Related Types =========================
|
|
191
191
|
|
|
192
192
|
export interface FindPrevNextResult {
|
|
193
193
|
prevOrder: OrderData | null;
|
|
@@ -200,7 +200,7 @@ export interface ValidationResult {
|
|
|
200
200
|
warnings: string[];
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
// =========================
|
|
203
|
+
// ========================= Module Interface Definitions =========================
|
|
204
204
|
|
|
205
205
|
export interface TradingModule {
|
|
206
206
|
buy(params: BuyParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
@@ -242,14 +242,14 @@ export interface SimulatorModule {
|
|
|
242
242
|
simulateSellStopLoss(mint: string, sellTokenAmount: bigint | string | number, stopLossPrice: bigint | string | number, lastPrice?: any, ordersData?: any): Promise<any>;
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
-
// =========================
|
|
245
|
+
// ========================= Data Interface Types =========================
|
|
246
246
|
|
|
247
247
|
export interface DataInterface {
|
|
248
248
|
orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
|
|
249
249
|
price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
-
// =========================
|
|
252
|
+
// ========================= Main SDK Type Definition =========================
|
|
253
253
|
|
|
254
254
|
export declare class Fun100xSdk {
|
|
255
255
|
connection: Connection;
|
|
@@ -262,12 +262,12 @@ export declare class Fun100xSdk {
|
|
|
262
262
|
paramsAccount: PublicKey;
|
|
263
263
|
fastApiUrl: string;
|
|
264
264
|
|
|
265
|
-
//
|
|
265
|
+
// Constants
|
|
266
266
|
readonly MAX_ORDERS_COUNT: number;
|
|
267
267
|
readonly FIND_MAX_ORDERS_COUNT: number;
|
|
268
268
|
readonly SUGGEST_LIQ_RATIO: number;
|
|
269
269
|
|
|
270
|
-
//
|
|
270
|
+
// Modules
|
|
271
271
|
trading: TradingModule;
|
|
272
272
|
fast: FastModule;
|
|
273
273
|
chain: ChainModule;
|
|
@@ -276,7 +276,7 @@ export declare class Fun100xSdk {
|
|
|
276
276
|
simulator: SimulatorModule;
|
|
277
277
|
data: DataInterface;
|
|
278
278
|
|
|
279
|
-
//
|
|
279
|
+
// Static utility class references
|
|
280
280
|
static CurveAMM: typeof CurveAMM;
|
|
281
281
|
static OrderUtils: typeof OrderUtils;
|
|
282
282
|
|
|
@@ -286,14 +286,14 @@ export declare class Fun100xSdk {
|
|
|
286
286
|
options?: Fun100xSdkOptions
|
|
287
287
|
);
|
|
288
288
|
|
|
289
|
-
// OrderUtils
|
|
289
|
+
// OrderUtils shortcut methods
|
|
290
290
|
buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
|
|
291
291
|
buildOrderAccounts(orders: OrderData[], maxCount?: number): (string | null)[];
|
|
292
292
|
findPrevNext(orders: OrderData[], findOrderPda: string): FindPrevNextResult;
|
|
293
293
|
findOrderIndex(orders: OrderData[], targetOrderPda: string | PublicKey | null): number;
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
-
// =========================
|
|
296
|
+
// ========================= Utility Class Exports =========================
|
|
297
297
|
|
|
298
298
|
export declare class OrderUtils {
|
|
299
299
|
static buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
|
|
@@ -336,7 +336,7 @@ export declare class CurveAMM {
|
|
|
336
336
|
static calculateInitialK(): any;
|
|
337
337
|
static getInitialPrice(): bigint | null;
|
|
338
338
|
|
|
339
|
-
// Custom pool parameters calculation (
|
|
339
|
+
// Custom pool parameters calculation (dynamic liquidity pool)
|
|
340
340
|
static calculateK(initialVirtualSol: any, initialVirtualToken: any): any;
|
|
341
341
|
static getInitialPriceWithParams(initialVirtualSol: any, initialVirtualToken: any): bigint | null;
|
|
342
342
|
|
|
@@ -349,7 +349,7 @@ export declare class CurveAMM {
|
|
|
349
349
|
static buyFromPriceWithTokenOutput(startLowPrice: bigint | string | number, tokenOutputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
350
350
|
static sellFromPriceWithSolOutput(startHighPrice: bigint | string | number, solOutputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
351
351
|
|
|
352
|
-
// AMM calculation methods with custom pool parameters (
|
|
352
|
+
// AMM calculation methods with custom pool parameters (dynamic liquidity pool)
|
|
353
353
|
static priceToReservesWithParams(price: any, initialVirtualSol: any, initialVirtualToken: any): [any, any] | null;
|
|
354
354
|
static buyFromPriceToPriceWithParams(startLowPrice: bigint | string | number, endHighPrice: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
|
|
355
355
|
static sellFromPriceToPriceWithParams(startHighPrice: bigint | string | number, endLowPrice: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
|
|
@@ -365,7 +365,7 @@ export declare class CurveAMM {
|
|
|
365
365
|
static calculatePoolPrice(lpTokenReserve: bigint | string | number | BN, lpSolReserve: bigint | string | number | BN): string | null;
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
-
// =========================
|
|
368
|
+
// ========================= Constant and Function Exports =========================
|
|
369
369
|
|
|
370
370
|
export declare const FUN100X_PROGRAM_ID: PublicKey;
|
|
371
371
|
|
|
@@ -373,7 +373,7 @@ export declare function getProgramId(network?: NetworkType): PublicKey;
|
|
|
373
373
|
|
|
374
374
|
export declare function getDefaultOptions(networkName?: 'MAINNET' | 'DEVNET' | 'LOCALNET'): NetworkConfig;
|
|
375
375
|
|
|
376
|
-
// =========================
|
|
376
|
+
// ========================= Module Class Exports =========================
|
|
377
377
|
|
|
378
378
|
export declare class TradingModule implements TradingModule {}
|
|
379
379
|
export declare class FastModule implements FastModule {}
|
|
@@ -382,5 +382,5 @@ export declare class TokenModule implements TokenModule {}
|
|
|
382
382
|
export declare class ParamModule implements ParamModule {}
|
|
383
383
|
export declare class SimulatorModule implements SimulatorModule {}
|
|
384
384
|
|
|
385
|
-
//
|
|
385
|
+
// Default export
|
|
386
386
|
export default Fun100xSdk;
|
package/src/utils/constants.js
CHANGED
|
@@ -29,7 +29,7 @@ const DEFAULT_NETWORKS = {
|
|
|
29
29
|
LOCALNET: {
|
|
30
30
|
name: 'localnet',
|
|
31
31
|
network: 'localnet',
|
|
32
|
-
programId: '
|
|
32
|
+
programId: 'EVNaaiyg9z876PUmLCVQcdc5L5eJukT4pni5GtVJ8P37',
|
|
33
33
|
defaultDataSource: 'fast', // 'fast' or 'chain'
|
|
34
34
|
solanaEndpoint: 'http://127.0.0.1:8899',
|
|
35
35
|
fastApiUrl: 'http://127.0.0.1:3000',
|
|
@@ -37,7 +37,7 @@ const DEFAULT_NETWORKS = {
|
|
|
37
37
|
// fastApiUrl: 'http://216.158.231.58:3000',
|
|
38
38
|
feeRecipient: 'GesAj2dTn2wdNcxj4x8qsqS9aNRVPBPkE76aaqg7skxu',
|
|
39
39
|
baseFeeRecipient: '5YHi1HsxobLiTD6NQfHJQpoPoRjMuNyXp4RroTvR6dKi',
|
|
40
|
-
paramsAccount: '
|
|
40
|
+
paramsAccount: 'HPuvtLLcgSMPSyRmULPiFe9oAvm1o8mR4weqXZrUhzRM'
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
|
package/src/utils/curve_amm.js
CHANGED
|
@@ -286,16 +286,16 @@ class CurveAMM {
|
|
|
286
286
|
}
|
|
287
287
|
|
|
288
288
|
/**
|
|
289
|
-
*
|
|
290
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
291
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
292
|
-
* @returns {Decimal} k
|
|
289
|
+
* Calculate the k value based on custom initial reserves
|
|
290
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve (actual value, not lamports)
|
|
291
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve (actual value, not smallest unit)
|
|
292
|
+
* @returns {Decimal} k value
|
|
293
293
|
*
|
|
294
294
|
* @example
|
|
295
|
-
* //
|
|
295
|
+
* // Use default parameters (30 SOL, 1.073 billion Token)
|
|
296
296
|
* const k1 = CurveAMM.calculateK(30, 1073000000);
|
|
297
297
|
*
|
|
298
|
-
* //
|
|
298
|
+
* // Use custom parameters (60 SOL, 1.073 billion Token)
|
|
299
299
|
* const k2 = CurveAMM.calculateK(60, 1073000000);
|
|
300
300
|
*/
|
|
301
301
|
static calculateK(initialVirtualSol, initialVirtualToken) {
|
|
@@ -305,7 +305,7 @@ class CurveAMM {
|
|
|
305
305
|
}
|
|
306
306
|
|
|
307
307
|
/**
|
|
308
|
-
*
|
|
308
|
+
* Calculate the initial k value using default parameters (backward compatible)
|
|
309
309
|
* @returns {Decimal} Product k value of initial reserves
|
|
310
310
|
*/
|
|
311
311
|
static calculateInitialK() {
|
|
@@ -316,10 +316,10 @@ class CurveAMM {
|
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
/**
|
|
319
|
-
*
|
|
320
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
321
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
322
|
-
* @returns {bigint|null} u128
|
|
319
|
+
* Get the initial price based on custom initial reserves
|
|
320
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
321
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
322
|
+
* @returns {bigint|null} Initial price in u128 format
|
|
323
323
|
*/
|
|
324
324
|
static getInitialPriceWithParams(initialVirtualSol, initialVirtualToken) {
|
|
325
325
|
const sol = new Decimal(initialVirtualSol);
|
|
@@ -329,7 +329,7 @@ class CurveAMM {
|
|
|
329
329
|
}
|
|
330
330
|
|
|
331
331
|
/**
|
|
332
|
-
*
|
|
332
|
+
* Get the initial price using default parameters (backward compatible)
|
|
333
333
|
* Get initial price (SOL amount for 1 token)
|
|
334
334
|
*
|
|
335
335
|
* @returns {bigint|null} Initial price in u128 format, returns null if calculation fails
|
|
@@ -342,12 +342,12 @@ class CurveAMM {
|
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
/**
|
|
345
|
-
*
|
|
346
|
-
* @param {bigint|string|number} startLowPrice -
|
|
347
|
-
* @param {bigint|string|number} endHighPrice -
|
|
348
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
349
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
350
|
-
* @returns {[bigint, bigint]|null}
|
|
345
|
+
* Calculate the SOL required and Token obtained when buying from a low price up to a high price, based on custom initial reserves
|
|
346
|
+
* @param {bigint|string|number} startLowPrice - Starting price (lower)
|
|
347
|
+
* @param {bigint|string|number} endHighPrice - Target price (higher)
|
|
348
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
349
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
350
|
+
* @returns {[bigint, bigint]|null} Returns [SOL amount to invest, token amount obtained] on success, null on failure
|
|
351
351
|
*/
|
|
352
352
|
static buyFromPriceToPriceWithParams(startLowPrice, endHighPrice, initialVirtualSol, initialVirtualToken) {
|
|
353
353
|
// Convert to Decimal for calculation
|
|
@@ -397,7 +397,7 @@ class CurveAMM {
|
|
|
397
397
|
}
|
|
398
398
|
|
|
399
399
|
/**
|
|
400
|
-
*
|
|
400
|
+
* Calculate buying from a low price up to a high price using default parameters (backward compatible)
|
|
401
401
|
* Calculate SOL required and token amount obtained when buying tokens from low to high price
|
|
402
402
|
*
|
|
403
403
|
* @param {bigint|string|number} startLowPrice - Starting price (lower)
|
|
@@ -415,12 +415,12 @@ class CurveAMM {
|
|
|
415
415
|
}
|
|
416
416
|
|
|
417
417
|
/**
|
|
418
|
-
*
|
|
419
|
-
* @param {bigint|string|number} startHighPrice -
|
|
420
|
-
* @param {bigint|string|number} endLowPrice -
|
|
421
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
422
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
423
|
-
* @returns {[bigint, bigint]|null}
|
|
418
|
+
* Calculate the Token required and SOL obtained when selling from a high price down to a low price, based on custom initial reserves
|
|
419
|
+
* @param {bigint|string|number} startHighPrice - Starting price (higher)
|
|
420
|
+
* @param {bigint|string|number} endLowPrice - Target price (lower)
|
|
421
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
422
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
423
|
+
* @returns {[bigint, bigint]|null} Returns [token amount to sell, SOL amount obtained] on success, null on failure
|
|
424
424
|
*/
|
|
425
425
|
static sellFromPriceToPriceWithParams(startHighPrice, endLowPrice, initialVirtualSol, initialVirtualToken) {
|
|
426
426
|
// Convert to Decimal for calculation
|
|
@@ -470,7 +470,7 @@ class CurveAMM {
|
|
|
470
470
|
}
|
|
471
471
|
|
|
472
472
|
/**
|
|
473
|
-
*
|
|
473
|
+
* Calculate selling from a high price down to a low price using default parameters (backward compatible)
|
|
474
474
|
* Calculate SOL amount obtained when selling tokens from high to low price
|
|
475
475
|
*
|
|
476
476
|
* @param {bigint|string|number} startHighPrice - Starting price (higher)
|
|
@@ -488,9 +488,9 @@ class CurveAMM {
|
|
|
488
488
|
}
|
|
489
489
|
|
|
490
490
|
/**
|
|
491
|
-
*
|
|
492
|
-
* @param {bigint|string} price - u128
|
|
493
|
-
* @param {Decimal} k -
|
|
491
|
+
* Calculate reserves based on a price and a custom k value
|
|
492
|
+
* @param {bigint|string} price - Price in u128 format
|
|
493
|
+
* @param {Decimal} k - Custom k value
|
|
494
494
|
* @returns {{solReserve: Decimal, tokenReserve: Decimal}|null}
|
|
495
495
|
*/
|
|
496
496
|
static priceToReservesWithK(price, k) {
|
|
@@ -523,10 +523,10 @@ class CurveAMM {
|
|
|
523
523
|
}
|
|
524
524
|
|
|
525
525
|
/**
|
|
526
|
-
*
|
|
527
|
-
* @param {bigint|string} price - u128
|
|
528
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
529
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
526
|
+
* Calculate reserves based on a price and custom initial reserves
|
|
527
|
+
* @param {bigint|string} price - Price in u128 format
|
|
528
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
529
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
530
530
|
* @returns {{solReserve: Decimal, tokenReserve: Decimal}|null}
|
|
531
531
|
*/
|
|
532
532
|
static priceToReservesWithParams(price, initialVirtualSol, initialVirtualToken) {
|
|
@@ -535,8 +535,8 @@ class CurveAMM {
|
|
|
535
535
|
}
|
|
536
536
|
|
|
537
537
|
/**
|
|
538
|
-
*
|
|
539
|
-
* @param {bigint|string} price - u128
|
|
538
|
+
* Calculate reserves based on a price using default parameters (backward compatible)
|
|
539
|
+
* @param {bigint|string} price - Price in u128 format
|
|
540
540
|
* @returns {{solReserve: Decimal, tokenReserve: Decimal}|null}
|
|
541
541
|
*/
|
|
542
542
|
static priceToReserves(price) {
|
|
@@ -617,12 +617,12 @@ class CurveAMM {
|
|
|
617
617
|
}
|
|
618
618
|
|
|
619
619
|
/**
|
|
620
|
-
*
|
|
621
|
-
* @param {bigint|string|number} startLowPrice -
|
|
622
|
-
* @param {bigint|string|number} solInputAmount -
|
|
623
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
624
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
625
|
-
* @returns {[bigint, bigint]|null}
|
|
620
|
+
* Based on custom initial reserves, calculate the token output amount and ending price from a starting price and SOL input amount
|
|
621
|
+
* @param {bigint|string|number} startLowPrice - Starting price
|
|
622
|
+
* @param {bigint|string|number} solInputAmount - SOL amount used for buying
|
|
623
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
624
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
625
|
+
* @returns {[bigint, bigint]|null} Returns [price after transaction, token amount obtained] on success, null on failure
|
|
626
626
|
*/
|
|
627
627
|
static buyFromPriceWithSolInputWithParams(startLowPrice, solInputAmount, initialVirtualSol, initialVirtualToken) {
|
|
628
628
|
// Convert to Decimal for calculation
|
|
@@ -683,7 +683,7 @@ class CurveAMM {
|
|
|
683
683
|
}
|
|
684
684
|
|
|
685
685
|
/**
|
|
686
|
-
*
|
|
686
|
+
* Calculate the token output amount and ending price using default parameters (backward compatible)
|
|
687
687
|
* Calculate token output amount and ending price based on starting price and SOL input amount
|
|
688
688
|
*
|
|
689
689
|
* @param {bigint|string|number} startLowPrice - Starting price
|
|
@@ -701,12 +701,12 @@ class CurveAMM {
|
|
|
701
701
|
}
|
|
702
702
|
|
|
703
703
|
/**
|
|
704
|
-
*
|
|
705
|
-
* @param {bigint|string|number} startHighPrice -
|
|
706
|
-
* @param {bigint|string|number} tokenInputAmount -
|
|
707
|
-
* @param {string|number|Decimal} initialVirtualSol -
|
|
708
|
-
* @param {string|number|Decimal} initialVirtualToken -
|
|
709
|
-
* @returns {[bigint, bigint]|null}
|
|
704
|
+
* Based on custom initial reserves, calculate the SOL output amount and ending price from a starting price and token input amount
|
|
705
|
+
* @param {bigint|string|number} startHighPrice - Starting price
|
|
706
|
+
* @param {bigint|string|number} tokenInputAmount - Token amount to sell
|
|
707
|
+
* @param {string|number|Decimal} initialVirtualSol - Initial virtual SOL reserve
|
|
708
|
+
* @param {string|number|Decimal} initialVirtualToken - Initial virtual Token reserve
|
|
709
|
+
* @returns {[bigint, bigint]|null} Returns [price after transaction, SOL amount obtained] on success, null on failure
|
|
710
710
|
*/
|
|
711
711
|
static sellFromPriceWithTokenInputWithParams(startHighPrice, tokenInputAmount, initialVirtualSol, initialVirtualToken) {
|
|
712
712
|
// Convert to Decimal for calculation
|
|
@@ -741,7 +741,7 @@ class CurveAMM {
|
|
|
741
741
|
// Calculate token reserves for ending state
|
|
742
742
|
const endTokenReserve = startTokenReserve.add(tokenInputDec);
|
|
743
743
|
|
|
744
|
-
//
|
|
744
|
+
// Calculate the SOL reserve for the ending state according to the AMM formula
|
|
745
745
|
const endSolReserve = k.div(endTokenReserve);
|
|
746
746
|
|
|
747
747
|
// Calculate SOL output amount
|
|
@@ -767,7 +767,7 @@ class CurveAMM {
|
|
|
767
767
|
}
|
|
768
768
|
|
|
769
769
|
/**
|
|
770
|
-
*
|
|
770
|
+
* Calculate the SOL output amount and ending price using default parameters (backward compatible)
|
|
771
771
|
* Calculate SOL output amount and ending price based on starting price and token input amount
|
|
772
772
|
*
|
|
773
773
|
* @param {bigint|string|number} startHighPrice - Starting price
|
|
@@ -825,14 +825,14 @@ class CurveAMM {
|
|
|
825
825
|
// Calculate token reserves for ending state
|
|
826
826
|
const endTokenReserve = startTokenReserve.sub(tokenOutputDec);
|
|
827
827
|
|
|
828
|
-
//console.log('buyFromPriceWithTokenOutput
|
|
828
|
+
//console.log('buyFromPriceWithTokenOutput ending token reserve = starting token reserve - token output amount:', endTokenReserve.toString());
|
|
829
829
|
|
|
830
830
|
// Check if token reserves are sufficient
|
|
831
831
|
if (endTokenReserve.lte(0)) {
|
|
832
832
|
return null;
|
|
833
833
|
}
|
|
834
834
|
|
|
835
|
-
//
|
|
835
|
+
// Calculate the SOL reserve for the ending state according to the AMM formula
|
|
836
836
|
const endSolReserve = k.div(endTokenReserve);
|
|
837
837
|
|
|
838
838
|
// Calculate required SOL input amount
|
|
@@ -858,7 +858,7 @@ class CurveAMM {
|
|
|
858
858
|
}
|
|
859
859
|
|
|
860
860
|
/**
|
|
861
|
-
*
|
|
861
|
+
* Based on a starting price and desired token output amount, calculate the required SOL input amount and ending price (with custom liquidity pool parameters)
|
|
862
862
|
* Based on starting price and desired token output, calculate required SOL input and ending price (with custom pool params)
|
|
863
863
|
*
|
|
864
864
|
* @param {bigint|string|number} startLowPrice - Starting price (lower)
|
|
@@ -1003,7 +1003,7 @@ class CurveAMM {
|
|
|
1003
1003
|
}
|
|
1004
1004
|
|
|
1005
1005
|
/**
|
|
1006
|
-
*
|
|
1006
|
+
* Based on a starting price and desired SOL output amount, calculate the required token input amount and ending price (with custom liquidity pool parameters)
|
|
1007
1007
|
* Based on starting price and desired SOL output, calculate required token input and ending price (with custom pool params)
|
|
1008
1008
|
*
|
|
1009
1009
|
* @param {bigint|string|number} startHighPrice - Starting price (higher)
|
package/src/utils/orderUtils.js
CHANGED