100x-sdk 1.0.3 → 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 +6450 -3903
- package/dist/100x-sdk.esm.js +5683 -3373
- package/dist/100x-sdk.js +5683 -3373
- 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/dist/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/package.json
CHANGED
package/src/modules/chain.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const { PublicKey } = require('@solana/web3.js');
|
|
3
3
|
const anchor = require('@coral-xyz/anchor');
|
|
4
4
|
const CurveAMM = require('../utils/curve_amm');
|
|
5
|
-
//
|
|
5
|
+
// Use the buffer package uniformly, consistent across all platforms
|
|
6
6
|
const { Buffer } = require('buffer');
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -14,14 +14,14 @@ const { Buffer } = require('buffer');
|
|
|
14
14
|
class ChainModule {
|
|
15
15
|
constructor(sdk) {
|
|
16
16
|
this.sdk = sdk;
|
|
17
|
-
// getCurveAccount
|
|
17
|
+
// getCurveAccount cache: key = mint string, value = { data, timestamp }
|
|
18
18
|
this._curveAccountCache = new Map();
|
|
19
|
-
this._CACHE_TTL = 10000; // 10
|
|
19
|
+
this._CACHE_TTL = 10000; // 10 second TTL
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param {string} [mint] -
|
|
23
|
+
* Clear the curveAccount cache for a specified mint (called after a transaction)
|
|
24
|
+
* @param {string} [mint] - Specified mint address; clears all if not provided
|
|
25
25
|
*/
|
|
26
26
|
invalidateCurveCache(mint) {
|
|
27
27
|
if (mint) {
|
|
@@ -178,12 +178,12 @@ class ChainModule {
|
|
|
178
178
|
const mintPubkey = typeof mint === 'string' ? new PublicKey(mint) : mint;
|
|
179
179
|
const mintKey = mintPubkey.toString();
|
|
180
180
|
|
|
181
|
-
//
|
|
181
|
+
// Check cache
|
|
182
182
|
const cached = this._curveAccountCache.get(mintKey);
|
|
183
183
|
if (cached && (Date.now() - cached.timestamp < this._CACHE_TTL)) {
|
|
184
|
-
//
|
|
184
|
+
// Cache hit: if the request includes balances but the cache doesn't, re-fetch the balance part
|
|
185
185
|
if (!skipBalances && cached.skipBalances) {
|
|
186
|
-
//
|
|
186
|
+
// The cache is from skipBalances, but balances are now needed, so a supplementary query is required
|
|
187
187
|
} else {
|
|
188
188
|
return cached.data;
|
|
189
189
|
}
|
|
@@ -237,7 +237,7 @@ class ChainModule {
|
|
|
237
237
|
this.sdk.programId
|
|
238
238
|
);
|
|
239
239
|
|
|
240
|
-
//
|
|
240
|
+
// Dedicated token account PDA for the borrow pool (added in the contract, seed = "pool_borrow_token")
|
|
241
241
|
const [poolBorrowTokenAccountPDA] = PublicKey.findProgramAddressSync(
|
|
242
242
|
[
|
|
243
243
|
Buffer.from("pool_borrow_token"),
|
|
@@ -309,22 +309,22 @@ class ChainModule {
|
|
|
309
309
|
// Creator address
|
|
310
310
|
creator: decodedData.creator.toString(),
|
|
311
311
|
|
|
312
|
-
// ===
|
|
312
|
+
// === Phase 1 new fields: dynamic liquidity pool parameters ===
|
|
313
313
|
initialVirtualSol: BigInt(decodedData.initialVirtualSol.toString()), // u64, lamports
|
|
314
|
-
initialVirtualToken: BigInt(decodedData.initialVirtualToken.toString()), // u64,
|
|
314
|
+
initialVirtualToken: BigInt(decodedData.initialVirtualToken.toString()), // u64, smallest unit
|
|
315
315
|
|
|
316
|
-
//
|
|
317
|
-
//
|
|
318
|
-
initialBorrowTokenReserve: BigInt(decodedData.initialBorrowTokenReserve.toString()), // u64,
|
|
316
|
+
// Borrow pool initial funding (added in the contract), never changes after creation
|
|
317
|
+
// Borrowed out amount = initialBorrowTokenReserve - borrowTokenReserve
|
|
318
|
+
initialBorrowTokenReserve: BigInt(decodedData.initialBorrowTokenReserve.toString()), // u64, smallest unit
|
|
319
319
|
|
|
320
|
-
// ===
|
|
321
|
-
poolType: decodedData.poolType, // u8, 0
|
|
322
|
-
borrowPoolRatio: decodedData.borrowPoolRatio, // u8, 5-30
|
|
320
|
+
// === Phase 2 new fields: advanced version pool parameters ===
|
|
321
|
+
poolType: decodedData.poolType, // u8, 0=basic version, 1=advanced version
|
|
322
|
+
borrowPoolRatio: decodedData.borrowPoolRatio, // u8, 5-30 means 5%-30%
|
|
323
323
|
|
|
324
|
-
// Token Supply
|
|
324
|
+
// Token Supply information
|
|
325
325
|
totalSupply: BigInt(tokenSupply.value.amount),
|
|
326
326
|
decimals: tokenSupply.value.decimals,
|
|
327
|
-
//
|
|
327
|
+
// Borrow pool initial total, taken directly from the on-chain field (old formula totalSupply - initialVirtualToken is deprecated)
|
|
328
328
|
initialBorrowPool: BigInt(decodedData.initialBorrowTokenReserve.toString()),
|
|
329
329
|
circulatingSupply: BigInt(decodedData.initialVirtualToken.toString())
|
|
330
330
|
+ (BigInt(tokenSupply.value.amount) - BigInt(decodedData.initialVirtualToken.toString()) - BigInt(decodedData.borrowTokenReserve.toString())),
|
|
@@ -335,10 +335,10 @@ class ChainModule {
|
|
|
335
335
|
|
|
336
336
|
// Pool account information
|
|
337
337
|
poolTokenAccount: poolTokenAccountPDA.toString(), // Pool token account address
|
|
338
|
-
poolBorrowTokenAccount: poolBorrowTokenAccountPDA.toString(), // Pool borrow token account address (
|
|
338
|
+
poolBorrowTokenAccount: poolBorrowTokenAccountPDA.toString(), // Pool borrow token account address (added)
|
|
339
339
|
poolSolAccount: poolSolAccountPDA.toString(), // Pool SOL account address
|
|
340
340
|
poolTokenBalance: BigInt(poolTokenBalance.value.amount), // Pool token balance
|
|
341
|
-
poolBorrowTokenBalance: BigInt(poolBorrowTokenBalance.value.amount), // Pool borrow token balance (= borrowTokenReserve,
|
|
341
|
+
poolBorrowTokenBalance: BigInt(poolBorrowTokenBalance.value.amount), // Pool borrow token balance (= borrowTokenReserve, added)
|
|
342
342
|
poolSolBalance: poolSolBalance, // Pool SOL balance (lamports)
|
|
343
343
|
|
|
344
344
|
// Additional metadata
|
|
@@ -348,7 +348,7 @@ class ChainModule {
|
|
|
348
348
|
}
|
|
349
349
|
};
|
|
350
350
|
|
|
351
|
-
//
|
|
351
|
+
// Write to cache
|
|
352
352
|
this._curveAccountCache.set(mintKey, {
|
|
353
353
|
data: convertedData,
|
|
354
354
|
timestamp: Date.now(),
|
|
@@ -411,7 +411,7 @@ class ChainModule {
|
|
|
411
411
|
}
|
|
412
412
|
|
|
413
413
|
try {
|
|
414
|
-
//
|
|
414
|
+
// Reuse the getCurveAccount cache to avoid re-fetching the same account
|
|
415
415
|
const curveData = await this.getCurveAccount(mint, { skipBalances: true });
|
|
416
416
|
const price = curveData.price;
|
|
417
417
|
|
|
@@ -515,7 +515,7 @@ class ChainModule {
|
|
|
515
515
|
// "down_orders" = long orders = downOrderbook (orderType=1)
|
|
516
516
|
const seed = orderType === 'up_orders' ? 'up_orderbook' : 'down_orderbook';
|
|
517
517
|
|
|
518
|
-
//
|
|
518
|
+
// Compute the orderbook PDA locally to avoid calling getCurveAccount (saves 5 RPCs)
|
|
519
519
|
const mintPubkey = new PublicKey(mint);
|
|
520
520
|
const [orderbookPubkey] = PublicKey.findProgramAddressSync(
|
|
521
521
|
[Buffer.from(seed), mintPubkey.toBuffer()],
|
|
@@ -973,7 +973,7 @@ class ChainModule {
|
|
|
973
973
|
throw new Error('debug_orders: order type must be "up_orders" or "down_orders"');
|
|
974
974
|
}
|
|
975
975
|
|
|
976
|
-
//
|
|
976
|
+
// Compute the orderbook PDA locally to avoid calling getCurveAccount (saves 5 RPCs)
|
|
977
977
|
const seed = orderType === 'up_orders' ? 'up_orderbook' : 'down_orderbook';
|
|
978
978
|
const mintPubkey = new PublicKey(mint);
|
|
979
979
|
const [orderbookPubkey] = PublicKey.findProgramAddressSync(
|
|
@@ -1184,7 +1184,7 @@ class ChainModule {
|
|
|
1184
1184
|
const page = 1; // Always return page 1
|
|
1185
1185
|
const orderBy = options.order_by || 'start_time_desc';
|
|
1186
1186
|
|
|
1187
|
-
//
|
|
1187
|
+
// Compute the orderbook PDA locally to avoid calling getCurveAccount (saves 5 RPCs)
|
|
1188
1188
|
const mintPubkey = new PublicKey(mint);
|
|
1189
1189
|
const [upOrderbookPubkey] = PublicKey.findProgramAddressSync(
|
|
1190
1190
|
[Buffer.from('up_orderbook'), mintPubkey.toBuffer()],
|
package/src/modules/fast.js
CHANGED
|
@@ -39,10 +39,10 @@ class FastModule {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* Get token list
|
|
42
|
+
* Get token list
|
|
43
43
|
* @param {Object} options - Query parameters
|
|
44
|
-
* @param {number} options.limit - Items per page, default 10
|
|
45
|
-
* @param {number} options.before_timestamp - Cursor for pagination (
|
|
44
|
+
* @param {number} options.limit - Items per page, default 10
|
|
45
|
+
* @param {number} options.before_timestamp - Cursor for pagination (Unix timestamp)
|
|
46
46
|
* @returns {Promise<Object>} Token list data
|
|
47
47
|
*
|
|
48
48
|
* @example
|
|
@@ -88,8 +88,8 @@ class FastModule {
|
|
|
88
88
|
* const page2 = await sdk.fast.mints({ limit: 5, before_timestamp: page1.data.next_cursor });
|
|
89
89
|
*
|
|
90
90
|
* // Access token data:
|
|
91
|
-
* const tokens = result.data.tokens; //
|
|
92
|
-
* const mintAddresses = tokens.map(t => t.mint_account); //
|
|
91
|
+
* const tokens = result.data.tokens; // Full array of token objects
|
|
92
|
+
* const mintAddresses = tokens.map(t => t.mint_account); // Extract token addresses
|
|
93
93
|
*/
|
|
94
94
|
async mints(options = {}) {
|
|
95
95
|
const params = {
|
|
@@ -108,8 +108,8 @@ class FastModule {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* Get token details
|
|
112
|
-
* @param {string} mint - Token mint address
|
|
111
|
+
* Get token details
|
|
112
|
+
* @param {string} mint - Token mint address
|
|
113
113
|
* @returns {Promise<Object>} Token details data
|
|
114
114
|
*
|
|
115
115
|
* @example
|
|
@@ -163,11 +163,11 @@ class FastModule {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
/**
|
|
166
|
-
*
|
|
166
|
+
* Direct API call method
|
|
167
167
|
* @private
|
|
168
|
-
* @param {Object} config -
|
|
169
|
-
* @param {Object} params -
|
|
170
|
-
* @returns {Promise<Object>} API
|
|
168
|
+
* @param {Object} config - Data configuration
|
|
169
|
+
* @param {Object} params - Request parameters
|
|
170
|
+
* @returns {Promise<Object>} API response data
|
|
171
171
|
*/
|
|
172
172
|
async _directApiCall(config, params = {}) {
|
|
173
173
|
// Ensure FastModule is properly configured before making API calls
|
|
@@ -188,33 +188,33 @@ class FastModule {
|
|
|
188
188
|
url: url
|
|
189
189
|
};
|
|
190
190
|
|
|
191
|
-
//
|
|
191
|
+
// Set parameters based on request method
|
|
192
192
|
if (config.method === 'POST') {
|
|
193
|
-
requestConfig.data = queryParams; // POST
|
|
193
|
+
requestConfig.data = queryParams; // POST request uses data
|
|
194
194
|
} else {
|
|
195
|
-
requestConfig.params = queryParams; // GET
|
|
195
|
+
requestConfig.params = queryParams; // GET request uses params
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
const response = await this.httpClient.request(requestConfig);
|
|
199
199
|
|
|
200
|
-
//
|
|
201
|
-
//
|
|
202
|
-
//
|
|
200
|
+
// Check new and old API response formats
|
|
201
|
+
// New format: { code: 200, msg: "success", data: {...} }
|
|
202
|
+
// Old format: { success: true, data: {...}, message: "..." }
|
|
203
203
|
const isNewFormat = response.data && typeof response.data.code === 'number';
|
|
204
204
|
const isOldFormat = response.data && typeof response.data.success === 'boolean';
|
|
205
205
|
|
|
206
206
|
if (isNewFormat) {
|
|
207
|
-
//
|
|
207
|
+
// New API format
|
|
208
208
|
if (response.data.code !== 200) {
|
|
209
209
|
throw new Error(`API请求失败: ${response.data.msg || '未知错误'}`);
|
|
210
210
|
}
|
|
211
211
|
} else if (isOldFormat) {
|
|
212
|
-
//
|
|
212
|
+
// Old API format (backward compatible)
|
|
213
213
|
if (!response.data.success) {
|
|
214
214
|
throw new Error(`API请求失败: ${response.data.message || '未知错误'}`);
|
|
215
215
|
}
|
|
216
216
|
} else {
|
|
217
|
-
//
|
|
217
|
+
// Unknown format
|
|
218
218
|
throw new Error('API返回格式无法识别');
|
|
219
219
|
}
|
|
220
220
|
|
|
@@ -222,14 +222,14 @@ class FastModule {
|
|
|
222
222
|
|
|
223
223
|
} catch (error) {
|
|
224
224
|
if (error.response) {
|
|
225
|
-
// API
|
|
225
|
+
// API returned error
|
|
226
226
|
const errorMsg = error.response.data?.msg || error.response.data?.message || error.message;
|
|
227
227
|
throw new Error(`API请求失败 [${error.response.status}]: ${errorMsg}`);
|
|
228
228
|
} else if (error.request) {
|
|
229
|
-
//
|
|
229
|
+
// Network error
|
|
230
230
|
throw new Error(`网络请求失败: 无法连接到 ${this.baseUrl}`);
|
|
231
231
|
} else {
|
|
232
|
-
//
|
|
232
|
+
// Other errors
|
|
233
233
|
throw new Error(`请求处理失败: ${error.message}`);
|
|
234
234
|
}
|
|
235
235
|
}
|
|
@@ -238,71 +238,71 @@ class FastModule {
|
|
|
238
238
|
|
|
239
239
|
|
|
240
240
|
/**
|
|
241
|
-
*
|
|
242
|
-
* @param {string} mint -
|
|
243
|
-
* @param {Object} options -
|
|
244
|
-
* @param {string} options.type -
|
|
245
|
-
* @param {number} options.page -
|
|
246
|
-
* @param {number} options.limit -
|
|
247
|
-
* @returns {Promise<Object>}
|
|
241
|
+
* Get Orders Data (Active Orders)
|
|
242
|
+
* @param {string} mint - Token mint address
|
|
243
|
+
* @param {Object} options - Query parameters
|
|
244
|
+
* @param {string} options.type - Order type: "up_orders" (short) or "down_orders" (long)
|
|
245
|
+
* @param {number} options.page - Page number, default 1
|
|
246
|
+
* @param {number} options.limit - Items per page, default 500
|
|
247
|
+
* @returns {Promise<Object>} Order data with order list and pagination info
|
|
248
248
|
*
|
|
249
249
|
* @example
|
|
250
|
-
* //
|
|
250
|
+
* // Get long orders
|
|
251
251
|
* const ordersData = await sdk.fast.orders('T3NFPvYvpULCTgrhHb4b4Sj5J1qtSNvyKZfE8hCvuKM', { type: 'down_orders' });
|
|
252
252
|
*
|
|
253
|
-
* //
|
|
253
|
+
* // Return value example (compatible with chain.js format):
|
|
254
254
|
* // {
|
|
255
255
|
* // "success": true,
|
|
256
256
|
* // "data": {
|
|
257
257
|
* // "orders": [
|
|
258
258
|
* // {
|
|
259
|
-
* // "index": 0, //
|
|
260
|
-
* // "user": "7621yjkZJ1jxBHw3oCPoazFfMx82NWBSARk2AGV7EBig", //
|
|
261
|
-
* // "lock_lp_start_price": "2656104380242311276", // LP
|
|
262
|
-
* // "lock_lp_end_price": "2325402748045870207", // LP
|
|
263
|
-
* // "open_price": "2795899347623485554", //
|
|
264
|
-
* // "order_id": "0", //
|
|
265
|
-
* // "lock_lp_sol_amount": 1880793407, // LP
|
|
266
|
-
* // "lock_lp_token_amount": 75677963031921, // LP
|
|
267
|
-
* // "next_lp_sol_amount": 0, // Next LP SOL
|
|
268
|
-
* // "next_lp_token_amount": 0, // Next LP Token
|
|
269
|
-
* // "margin_init_sol_amount": 418211213, //
|
|
270
|
-
* // "margin_sol_amount": 418211213, //
|
|
271
|
-
* // "borrow_amount": 2276435100, //
|
|
272
|
-
* // "position_asset_amount": 75677963031921, //
|
|
273
|
-
* // "realized_sol_amount": 0, //
|
|
274
|
-
* // "version": 2, //
|
|
275
|
-
* // "start_time": 1764047379, //
|
|
276
|
-
* // "end_time": 1764652179, //
|
|
277
|
-
* // "next_order": 1, //
|
|
278
|
-
* // "prev_order": 65535, //
|
|
279
|
-
* // "borrow_fee": 1200, //
|
|
280
|
-
* // "order_type": "down_orders" //
|
|
259
|
+
* // "index": 0, // Order index position in the OrderBook
|
|
260
|
+
* // "user": "7621yjkZJ1jxBHw3oCPoazFfMx82NWBSARk2AGV7EBig", // User address
|
|
261
|
+
* // "lock_lp_start_price": "2656104380242311276", // LP start price (string)
|
|
262
|
+
* // "lock_lp_end_price": "2325402748045870207", // LP end price (string)
|
|
263
|
+
* // "open_price": "2795899347623485554", // Open price (string)
|
|
264
|
+
* // "order_id": "0", // Order ID (string)
|
|
265
|
+
* // "lock_lp_sol_amount": 1880793407, // LP locked SOL amount (lamports)
|
|
266
|
+
* // "lock_lp_token_amount": 75677963031921, // LP locked token amount (min unit)
|
|
267
|
+
* // "next_lp_sol_amount": 0, // Next LP SOL amount
|
|
268
|
+
* // "next_lp_token_amount": 0, // Next LP Token amount
|
|
269
|
+
* // "margin_init_sol_amount": 418211213, // Initial margin
|
|
270
|
+
* // "margin_sol_amount": 418211213, // Margin SOL amount (lamports)
|
|
271
|
+
* // "borrow_amount": 2276435100, // Borrow amount (lamports)
|
|
272
|
+
* // "position_asset_amount": 75677963031921, // Position asset amount (min unit)
|
|
273
|
+
* // "realized_sol_amount": 0, // Realized profit
|
|
274
|
+
* // "version": 2, // Version number
|
|
275
|
+
* // "start_time": 1764047379, // Start time (Unix timestamp)
|
|
276
|
+
* // "end_time": 1764652179, // End time (Unix timestamp)
|
|
277
|
+
* // "next_order": 1, // Next order index in linked list (65535=none)
|
|
278
|
+
* // "prev_order": 65535, // Previous order index in linked list (65535=none)
|
|
279
|
+
* // "borrow_fee": 1200, // Borrow fee (basis points, 1200 = 12%)
|
|
280
|
+
* // "order_type": "down_orders" // Order type (string)
|
|
281
281
|
* // }
|
|
282
282
|
* // ],
|
|
283
|
-
* // "total": 10, //
|
|
284
|
-
* // "order_type": "down_orders", //
|
|
285
|
-
* // "mint_account": "T3NFPvYvpULCTgrhHb4b4Sj5J1qtSNvyKZfE8hCvuKM", //
|
|
286
|
-
* // "page": 1, //
|
|
287
|
-
* // "limit": 3, //
|
|
288
|
-
* // "has_next": true, //
|
|
289
|
-
* // "has_prev": false //
|
|
283
|
+
* // "total": 10, // Total order count (total_count)
|
|
284
|
+
* // "order_type": "down_orders", // Order type (string)
|
|
285
|
+
* // "mint_account": "T3NFPvYvpULCTgrhHb4b4Sj5J1qtSNvyKZfE8hCvuKM", // Queried token address
|
|
286
|
+
* // "page": 1, // Current page number
|
|
287
|
+
* // "limit": 3, // Items per page
|
|
288
|
+
* // "has_next": true, // Whether there is a next page
|
|
289
|
+
* // "has_prev": false // Whether there is a previous page
|
|
290
290
|
* // },
|
|
291
|
-
* // "message": "Operation successful" //
|
|
291
|
+
* // "message": "Operation successful" // Operation result message
|
|
292
292
|
* // }
|
|
293
293
|
*
|
|
294
|
-
* //
|
|
295
|
-
* const orders = ordersData.data.orders; //
|
|
296
|
-
* const totalOrders = ordersData.data.total; //
|
|
297
|
-
* const firstOrder = orders[0]; //
|
|
298
|
-
* const userAddress = firstOrder.user; //
|
|
299
|
-
* const marginAmount = firstOrder.margin_sol_amount; //
|
|
300
|
-
* const borrowFee = firstOrder.borrow_fee; //
|
|
294
|
+
* // Access order data:
|
|
295
|
+
* const orders = ordersData.data.orders; // Order array
|
|
296
|
+
* const totalOrders = ordersData.data.total; // Total order count
|
|
297
|
+
* const firstOrder = orders[0]; // First order
|
|
298
|
+
* const userAddress = firstOrder.user; // User address
|
|
299
|
+
* const marginAmount = firstOrder.margin_sol_amount; // Margin amount (lamports)
|
|
300
|
+
* const borrowFee = firstOrder.borrow_fee; // Borrow fee (basis points)
|
|
301
301
|
*
|
|
302
|
-
* //
|
|
302
|
+
* // Get short orders:
|
|
303
303
|
* const shortOrders = await sdk.fast.orders(mint, { type: 'up_orders' });
|
|
304
304
|
*
|
|
305
|
-
* //
|
|
305
|
+
* // Get paginated orders:
|
|
306
306
|
* const pageTwo = await sdk.fast.orders(mint, { type: 'down_orders', page: 2, limit: 100 });
|
|
307
307
|
*/
|
|
308
308
|
async orders(mint, options = {}) {
|
|
@@ -416,17 +416,17 @@ class FastModule {
|
|
|
416
416
|
}));
|
|
417
417
|
|
|
418
418
|
// Sort orders by lock_lp_start_price
|
|
419
|
-
// down_orders (
|
|
420
|
-
// up_orders (
|
|
419
|
+
// down_orders (long): descending order
|
|
420
|
+
// up_orders (short): ascending order
|
|
421
421
|
processedOrders.sort((a, b) => {
|
|
422
422
|
const priceA = BigInt(a.lock_lp_start_price);
|
|
423
423
|
const priceB = BigInt(b.lock_lp_start_price);
|
|
424
424
|
|
|
425
425
|
if (type === 'down_orders') {
|
|
426
|
-
//
|
|
426
|
+
// Long orders: sort in descending order
|
|
427
427
|
return priceB > priceA ? 1 : (priceB < priceA ? -1 : 0);
|
|
428
428
|
} else {
|
|
429
|
-
//
|
|
429
|
+
// Short orders: sort in ascending order
|
|
430
430
|
return priceA > priceB ? 1 : (priceA < priceB ? -1 : 0);
|
|
431
431
|
}
|
|
432
432
|
});
|
|
@@ -463,9 +463,9 @@ class FastModule {
|
|
|
463
463
|
}
|
|
464
464
|
|
|
465
465
|
/**
|
|
466
|
-
* Get token price
|
|
467
|
-
* @param {string} mint - Token mint address
|
|
468
|
-
* @returns {Promise<string>} Latest price string
|
|
466
|
+
* Get token price
|
|
467
|
+
* @param {string} mint - Token mint address
|
|
468
|
+
* @returns {Promise<string>} Latest price string
|
|
469
469
|
*
|
|
470
470
|
* @example
|
|
471
471
|
* // Get token latest price
|
|
@@ -481,12 +481,12 @@ class FastModule {
|
|
|
481
481
|
// Call mint_info API
|
|
482
482
|
const result = await this.mint_info(mint);
|
|
483
483
|
|
|
484
|
-
// Check return data (
|
|
484
|
+
// Check return data (new format: { code: 200, msg: "success", data: {...} })
|
|
485
485
|
if (!result || !result.data) {
|
|
486
486
|
throw new Error('price: 无法获取代币信息 Unable to fetch token information');
|
|
487
487
|
}
|
|
488
488
|
|
|
489
|
-
// Extract latest price (
|
|
489
|
+
// Extract latest price (new format returns object directly, not array)
|
|
490
490
|
let latestPrice = result.data.latest_price;
|
|
491
491
|
|
|
492
492
|
if (!latestPrice) {
|
|
@@ -502,16 +502,16 @@ class FastModule {
|
|
|
502
502
|
}
|
|
503
503
|
|
|
504
504
|
/**
|
|
505
|
-
* Get User Active Orders
|
|
505
|
+
* Get User Active Orders
|
|
506
506
|
* Compatible with chain.js user_orders() method
|
|
507
507
|
*
|
|
508
|
-
* @param {string} user - User wallet address
|
|
509
|
-
* @param {string} mint - Token mint address (optional, null/undefined for all tokens)
|
|
510
|
-
* @param {Object} options - Query parameters
|
|
511
|
-
* @param {string} options.direction - Order direction (optional: 'up'=short, 'dn'=long, omit=all)
|
|
512
|
-
* @param {number} options.page - Page number, default 1
|
|
513
|
-
* @param {number} options.limit - Items per page, default 20
|
|
514
|
-
* @param {string} options.order_by - Sort order: 'start_time_desc' or 'start_time_asc', default 'start_time_desc'
|
|
508
|
+
* @param {string} user - User wallet address
|
|
509
|
+
* @param {string} mint - Token mint address (optional, null/undefined for all tokens)
|
|
510
|
+
* @param {Object} options - Query parameters
|
|
511
|
+
* @param {string} options.direction - Order direction (optional: 'up'=short, 'dn'=long, omit=all)
|
|
512
|
+
* @param {number} options.page - Page number, default 1
|
|
513
|
+
* @param {number} options.limit - Items per page, default 20
|
|
514
|
+
* @param {string} options.order_by - Sort order: 'start_time_desc' or 'start_time_asc', default 'start_time_desc'
|
|
515
515
|
* @returns {Promise<Object>} User orders data compatible with chain.js format
|
|
516
516
|
*
|
|
517
517
|
* @example
|