100x-sdk 1.0.1

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.
@@ -0,0 +1,386 @@
1
+ import { Connection, PublicKey, Transaction, Keypair } from '@solana/web3.js';
2
+ import { BN, Wallet, Program } from '@coral-xyz/anchor';
3
+
4
+ // ========================= 基础类型定义 =========================
5
+
6
+ export type DataSourceType = 'fast' | 'chain';
7
+
8
+ export type NetworkType = 'mainnet' | 'localnet';
9
+
10
+ export interface NetworkConfig {
11
+ name: string;
12
+ network: NetworkType;
13
+ programId: string;
14
+ defaultDataSource: DataSourceType;
15
+ solanaEndpoint: string;
16
+ fastApiUrl: string;
17
+ feeRecipient: string;
18
+ baseFeeRecipient: string;
19
+ paramsAccount: string;
20
+ }
21
+
22
+ export interface Fun100xSdkOptions {
23
+ network?: NetworkType;
24
+ defaultDataSource?: DataSourceType;
25
+ solanaEndpoint?: string;
26
+ fastApiUrl?: string;
27
+ feeRecipient?: string;
28
+ baseFeeRecipient?: string;
29
+ paramsAccount?: string;
30
+ }
31
+
32
+ // ========================= 订单和交易相关类型 =========================
33
+
34
+ export interface OrderData {
35
+ order_pda: string;
36
+ user: string;
37
+ mint: string;
38
+ order_type: string;
39
+ lock_lp_sol_amount: string;
40
+ lock_lp_token_amount: string;
41
+ lock_lp_start_price: string;
42
+ lock_lp_end_price: string;
43
+ margin_sol_amount: string;
44
+ borrow_amount: string;
45
+ position_asset_amount: string;
46
+ created_at?: string;
47
+ updated_at?: string;
48
+ }
49
+
50
+ export interface LpPair {
51
+ solAmount: BN;
52
+ tokenAmount: BN;
53
+ }
54
+
55
+ export interface TransactionResult {
56
+ transaction: Transaction;
57
+ signers: Keypair[];
58
+ accounts: Record<string, PublicKey>;
59
+ orderData?: {
60
+ ordersUsed: number;
61
+ lpPairsCount: number;
62
+ lpPairs: LpPair[];
63
+ orderAccounts: (string | null)[];
64
+ [key: string]: any;
65
+ };
66
+ }
67
+
68
+ export interface OrdersResponse {
69
+ data: {
70
+ orders: OrderData[];
71
+ total?: number;
72
+ page?: number;
73
+ limit?: number;
74
+ };
75
+ }
76
+
77
+ export interface PriceResponse {
78
+ price: string;
79
+ price_u128: string;
80
+ [key: string]: any;
81
+ }
82
+
83
+ export interface MintInfo {
84
+ mint: string;
85
+ name?: string;
86
+ symbol?: string;
87
+ decimals: number;
88
+ total_supply?: string;
89
+ [key: string]: any;
90
+ }
91
+
92
+ // ========================= 交易参数类型 =========================
93
+
94
+ export interface BuyParams {
95
+ mintAccount: string | PublicKey;
96
+ buyTokenAmount: BN;
97
+ maxSolAmount: BN;
98
+ payer: PublicKey;
99
+ }
100
+
101
+ export interface SellParams {
102
+ mintAccount: string | PublicKey;
103
+ sellTokenAmount: BN;
104
+ minSolOutput: BN;
105
+ payer: PublicKey;
106
+ }
107
+
108
+ export interface LongParams {
109
+ mintAccount: string | PublicKey;
110
+ buyTokenAmount: BN;
111
+ maxSolAmount: BN;
112
+ marginSol: BN;
113
+ closePrice: BN;
114
+ prevOrder?: PublicKey | null;
115
+ nextOrder?: PublicKey | null;
116
+ payer: PublicKey;
117
+ }
118
+
119
+ export interface ShortParams {
120
+ mintAccount: string | PublicKey;
121
+ borrowSellTokenAmount: BN;
122
+ minSolOutput: BN;
123
+ marginSol: BN;
124
+ closePrice: BN;
125
+ prevOrder?: PublicKey | null;
126
+ nextOrder?: PublicKey | null;
127
+ payer: PublicKey;
128
+ }
129
+
130
+ export interface CloseLongParams {
131
+ mintAccount: string | PublicKey;
132
+ closeOrder: string | PublicKey;
133
+ sellTokenAmount: BN;
134
+ minSolOutput: BN;
135
+ payer: PublicKey;
136
+ }
137
+
138
+ export interface CloseShortParams {
139
+ mintAccount: string | PublicKey;
140
+ closeOrder: string | PublicKey;
141
+ buyTokenAmount: BN;
142
+ maxSolAmount: BN;
143
+ payer: PublicKey;
144
+ }
145
+
146
+ export interface TransactionOptions {
147
+ computeUnits?: number;
148
+ }
149
+
150
+ // ========================= 查询参数类型 =========================
151
+
152
+ export interface OrdersQueryOptions {
153
+ type?: 'up_orders' | 'down_orders';
154
+ limit?: number;
155
+ page?: number;
156
+ dataSource?: DataSourceType;
157
+ }
158
+
159
+ export interface PriceQueryOptions {
160
+ dataSource?: DataSourceType;
161
+ }
162
+
163
+ export interface UserOrdersQueryOptions {
164
+ type?: 'up_orders' | 'down_orders';
165
+ limit?: number;
166
+ page?: number;
167
+ dataSource?: DataSourceType;
168
+ }
169
+
170
+ // ========================= 模拟器相关类型 =========================
171
+
172
+ export interface SimulationResult {
173
+ liqResult: {
174
+ free_lp_sol_amount_sum: bigint;
175
+ free_lp_token_amount_sum: bigint;
176
+ lock_lp_sol_amount_sum: bigint;
177
+ lock_lp_token_amount_sum: bigint;
178
+ has_infinite_lp: boolean;
179
+ pass_order_id: number;
180
+ force_close_num: number;
181
+ ideal_lp_sol_amount: bigint;
182
+ real_lp_sol_amount: bigint;
183
+ };
184
+ completion: string;
185
+ slippage: string;
186
+ suggestedTokenAmount: string;
187
+ suggestedSolAmount: string;
188
+ }
189
+
190
+ // ========================= 工具类相关类型 =========================
191
+
192
+ export interface FindPrevNextResult {
193
+ prevOrder: OrderData | null;
194
+ nextOrder: OrderData | null;
195
+ }
196
+
197
+ export interface ValidationResult {
198
+ valid: boolean;
199
+ errors: string[];
200
+ warnings: string[];
201
+ }
202
+
203
+ // ========================= 模块接口定义 =========================
204
+
205
+ export interface TradingModule {
206
+ buy(params: BuyParams, options?: TransactionOptions): Promise<TransactionResult>;
207
+ sell(params: SellParams, options?: TransactionOptions): Promise<TransactionResult>;
208
+ long(params: LongParams, options?: TransactionOptions): Promise<TransactionResult>;
209
+ short(params: ShortParams, options?: TransactionOptions): Promise<TransactionResult>;
210
+ closeLong(params: CloseLongParams, options?: TransactionOptions): Promise<TransactionResult>;
211
+ closeShort(params: CloseShortParams, options?: TransactionOptions): Promise<TransactionResult>;
212
+ }
213
+
214
+ export interface FastModule {
215
+ mints(options?: any): Promise<any>;
216
+ mint_info(mint: string): Promise<MintInfo>;
217
+ orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
218
+ price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
219
+ user_orders(user: string, mint: string, options?: UserOrdersQueryOptions): Promise<OrdersResponse>;
220
+ }
221
+
222
+ export interface ChainModule {
223
+ getCurveAccount(mint: string): Promise<any>;
224
+ orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
225
+ price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
226
+ }
227
+
228
+ export interface TokenModule {
229
+ create(params: any): Promise<TransactionResult>;
230
+ }
231
+
232
+ export interface ParamModule {
233
+ createParams(params: any): Promise<TransactionResult>;
234
+ getParams(partner: string): Promise<any>;
235
+ getAdmin(): Promise<any>;
236
+ }
237
+
238
+ export interface SimulatorModule {
239
+ simulateTokenBuy(mint: string, buyTokenAmount: bigint | string | number, passOrder?: string | null): Promise<SimulationResult>;
240
+ simulateTokenSell(mint: string, sellTokenAmount: bigint | string | number, passOrder?: string | null): Promise<SimulationResult>;
241
+ simulateLongStopLoss(mint: string, buyTokenAmount: bigint | string | number, stopLossPrice: bigint | string | number, lastPrice?: any, ordersData?: any): Promise<any>;
242
+ simulateSellStopLoss(mint: string, sellTokenAmount: bigint | string | number, stopLossPrice: bigint | string | number, lastPrice?: any, ordersData?: any): Promise<any>;
243
+ }
244
+
245
+ // ========================= 数据接口类型 =========================
246
+
247
+ export interface DataInterface {
248
+ orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
249
+ price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
250
+ }
251
+
252
+ // ========================= 主 SDK 类型定义 =========================
253
+
254
+ export declare class Fun100xSdk {
255
+ connection: Connection;
256
+ programId: PublicKey;
257
+ program: Program;
258
+ options: Fun100xSdkOptions;
259
+ defaultDataSource: DataSourceType;
260
+ feeRecipient: PublicKey;
261
+ baseFeeRecipient: PublicKey;
262
+ paramsAccount: PublicKey;
263
+ fastApiUrl: string;
264
+
265
+ // 常量
266
+ readonly MAX_ORDERS_COUNT: number;
267
+ readonly FIND_MAX_ORDERS_COUNT: number;
268
+ readonly SUGGEST_LIQ_RATIO: number;
269
+
270
+ // 模块
271
+ trading: TradingModule;
272
+ fast: FastModule;
273
+ chain: ChainModule;
274
+ token: TokenModule;
275
+ param: ParamModule;
276
+ simulator: SimulatorModule;
277
+ data: DataInterface;
278
+
279
+ // 静态工具类引用
280
+ static CurveAMM: typeof CurveAMM;
281
+ static OrderUtils: typeof OrderUtils;
282
+
283
+ constructor(
284
+ connection: Connection,
285
+ programId: string | PublicKey,
286
+ options?: Fun100xSdkOptions
287
+ );
288
+
289
+ // OrderUtils 快捷方法
290
+ buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
291
+ buildOrderAccounts(orders: OrderData[], maxCount?: number): (string | null)[];
292
+ findPrevNext(orders: OrderData[], findOrderPda: string): FindPrevNextResult;
293
+ findOrderIndex(orders: OrderData[], targetOrderPda: string | PublicKey | null): number;
294
+ }
295
+
296
+ // ========================= 工具类导出 =========================
297
+
298
+ export declare class OrderUtils {
299
+ static buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
300
+ static buildOrderAccounts(orders: OrderData[], maxCount?: number): (string | null)[];
301
+ static findPrevNext(orders: OrderData[], findOrderPda: string): FindPrevNextResult;
302
+ static findOrderIndex(orders: OrderData[], targetOrderPda: string | PublicKey | null): number;
303
+ static validateOrdersFormat(orders: OrderData[], throwOnError?: boolean): boolean | ValidationResult;
304
+ }
305
+
306
+ export declare class CurveAMM {
307
+ static readonly INITIAL_SOL_RESERVE_DECIMAL: any;
308
+ static readonly INITIAL_TOKEN_RESERVE_DECIMAL: any;
309
+ static readonly INITIAL_K_DECIMAL: any;
310
+ static readonly INITIAL_MIN_PRICE_DECIMAL: any;
311
+ static readonly PRICE_PRECISION_FACTOR_DECIMAL: any;
312
+ static readonly TOKEN_PRECISION_FACTOR_DECIMAL: any;
313
+ static readonly SOL_PRECISION_FACTOR_DECIMAL: any;
314
+ static readonly MAX_U128_PRICE: bigint;
315
+ static readonly MIN_U128_PRICE: bigint;
316
+
317
+ // Price conversion methods
318
+ static u128ToDecimal(price: bigint | string | number): any;
319
+ static decimalToU128(price: any): bigint | null;
320
+ static decimalToU128Ceil(price: any): bigint | null;
321
+ static u64ToDecimal(price: bigint | string | number): any;
322
+ static decimalToU64(price: any): bigint | null;
323
+ static decimalToU64Ceil(price: any): bigint | null;
324
+
325
+ // Token amount conversion methods
326
+ static tokenDecimalToU64(amount: any): bigint | null;
327
+ static tokenDecimalToU64Ceil(amount: any): bigint | null;
328
+ static u64ToTokenDecimal(amount: bigint | string | number): any;
329
+
330
+ // SOL amount conversion methods
331
+ static solDecimalToU64(amount: any): bigint | null;
332
+ static solDecimalToU64Ceil(amount: any): bigint | null;
333
+ static u64ToSolDecimal(amount: bigint | string | number): any;
334
+
335
+ // Initial price and K calculation
336
+ static calculateInitialK(): any;
337
+ static getInitialPrice(): bigint | null;
338
+
339
+ // Custom pool parameters calculation (动态流动池)
340
+ static calculateK(initialVirtualSol: any, initialVirtualToken: any): any;
341
+ static getInitialPriceWithParams(initialVirtualSol: any, initialVirtualToken: any): bigint | null;
342
+
343
+ // AMM calculation methods (default pool parameters)
344
+ static calculateReservesByPrice(price: any, k: any): [any, any] | null;
345
+ static buyFromPriceToPrice(startLowPrice: bigint | string | number, endHighPrice: bigint | string | number): [bigint, bigint] | null;
346
+ static sellFromPriceToPrice(startHighPrice: bigint | string | number, endLowPrice: bigint | string | number): [bigint, bigint] | null;
347
+ static buyFromPriceWithSolInput(startLowPrice: bigint | string | number, solInputAmount: bigint | string | number): [bigint, bigint] | null;
348
+ static sellFromPriceWithTokenInput(startHighPrice: bigint | string | number, tokenInputAmount: bigint | string | number): [bigint, bigint] | null;
349
+ static buyFromPriceWithTokenOutput(startLowPrice: bigint | string | number, tokenOutputAmount: bigint | string | number): [bigint, bigint] | null;
350
+ static sellFromPriceWithSolOutput(startHighPrice: bigint | string | number, solOutputAmount: bigint | string | number): [bigint, bigint] | null;
351
+
352
+ // AMM calculation methods with custom pool parameters (动态流动池)
353
+ static priceToReservesWithParams(price: any, initialVirtualSol: any, initialVirtualToken: any): [any, any] | null;
354
+ static buyFromPriceToPriceWithParams(startLowPrice: bigint | string | number, endHighPrice: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
355
+ static sellFromPriceToPriceWithParams(startHighPrice: bigint | string | number, endLowPrice: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
356
+ static buyFromPriceWithSolInputWithParams(startLowPrice: bigint | string | number, solInputAmount: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
357
+ static sellFromPriceWithTokenInputWithParams(startHighPrice: bigint | string | number, tokenInputAmount: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
358
+ static buyFromPriceWithTokenOutputWithParams(startLowPrice: bigint | string | number, tokenOutputAmount: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
359
+ static sellFromPriceWithSolOutputWithParams(startHighPrice: bigint | string | number, solOutputAmount: bigint | string | number, initialVirtualSol: any, initialVirtualToken: any): [bigint, bigint] | null;
360
+
361
+ // Fee and price display methods
362
+ static calculateAmountAfterFee(amount: bigint | string | number, fee: number): bigint | null;
363
+ static formatPriceForDisplay(price: bigint | string | number, decimalPlaces?: number): string;
364
+ static createPriceDisplayString(price: bigint | string | number, decimalPlaces?: number): string;
365
+ static calculatePoolPrice(lpTokenReserve: bigint | string | number | BN, lpSolReserve: bigint | string | number | BN): string | null;
366
+ }
367
+
368
+ // ========================= 常量和函数导出 =========================
369
+
370
+ export declare const FUN100X_PROGRAM_ID: PublicKey;
371
+
372
+ export declare function getProgramId(network?: NetworkType): PublicKey;
373
+
374
+ export declare function getDefaultOptions(networkName?: 'MAINNET' | 'DEVNET' | 'LOCALNET'): NetworkConfig;
375
+
376
+ // ========================= 模块类导出 =========================
377
+
378
+ export declare class TradingModule implements TradingModule {}
379
+ export declare class FastModule implements FastModule {}
380
+ export declare class ChainModule implements ChainModule {}
381
+ export declare class TokenModule implements TokenModule {}
382
+ export declare class ParamModule implements ParamModule {}
383
+ export declare class SimulatorModule implements SimulatorModule {}
384
+
385
+ // 默认导出
386
+ export default Fun100xSdk;
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "100x-sdk",
3
+ "version": "1.0.1",
4
+ "description": "Solana 100x.fun SDK",
5
+ "main": "dist/100x-sdk.cjs.js",
6
+ "module": "dist/100x-sdk.esm.js",
7
+ "browser": "dist/100x-sdk.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "browser": "./dist/100x-sdk.js",
13
+ "import": "./dist/100x-sdk.esm.js",
14
+ "require": "./dist/100x-sdk.cjs.js",
15
+ "default": "./dist/100x-sdk.esm.js"
16
+ }
17
+ },
18
+ "sideEffects": false,
19
+ "scripts": {
20
+ "prebuild": "mkdir -p dist",
21
+ "build": "rollup -c",
22
+ "build:dev": "rollup -c -w",
23
+ "build:types": "cp src/types/index.d.ts dist/index.d.ts",
24
+ "test": "mocha tests/unit/**/*.test.js",
25
+ "test:integration": "mocha tests/integration/**/*.test.js",
26
+ "lint": "eslint src"
27
+ },
28
+ "keywords": [
29
+ "solana",
30
+ "anchor",
31
+ "blockchain",
32
+ "sdk"
33
+ ],
34
+ "author": "",
35
+ "license": "MIT",
36
+ "dependencies": {
37
+ "@coral-xyz/anchor": "^0.31.1",
38
+ "@pythnetwork/client": "^2.22.1",
39
+ "@solana/spl-token": "^0.4.13",
40
+ "@solana/web3.js": "^1.78.0",
41
+ "axios": "^1.11.0",
42
+ "bs58": "^4.0.1",
43
+ "buffer": "^6.0.3",
44
+ "decimal.js": "^10.6.0",
45
+ "isomorphic-fetch": "^3.0.0",
46
+ "json-bigint": "^1.0.0",
47
+ "node-fetch": "^3.3.2"
48
+ },
49
+ "devDependencies": {
50
+ "@rollup/plugin-commonjs": "^24.1.0",
51
+ "@rollup/plugin-json": "^6.1.0",
52
+ "@rollup/plugin-node-resolve": "^15.2.3",
53
+ "@rollup/plugin-replace": "^5.0.5",
54
+ "@rollup/plugin-terser": "^0.4.4",
55
+ "eslint": "^9.0.0",
56
+ "glob": "^10.3.10",
57
+ "mocha": "^10.2.0",
58
+ "rimraf": "^5.0.5",
59
+ "rollup": "^2.79.1",
60
+ "rollup-plugin-polyfill-node": "^0.13.0"
61
+ },
62
+ "engines": {
63
+ "node": ">=14.0.0"
64
+ },
65
+ "files": [
66
+ "dist",
67
+ "src"
68
+ ]
69
+ }