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.
@@ -23,8 +23,8 @@ function transformOrdersData(ordersData) {
23
23
  lock_lp_end_price: BigInt(order.lock_lp_end_price),
24
24
  lock_lp_sol_amount: order.lock_lp_sol_amount,
25
25
  lock_lp_token_amount: order.lock_lp_token_amount,
26
- index: order.index, // 保留 OrderBook 中的索引
27
- order_id: order.order_id // 保留订单ID
26
+ index: order.index, // Preserve the index in the OrderBook
27
+ order_id: order.order_id // Preserve the order ID
28
28
  }));
29
29
  }
30
30
 
@@ -33,164 +33,164 @@ function transformOrdersData(ordersData) {
33
33
  * @property {number} order_type - Order type (e.g., 1 for down_orders, 2 for up_orders).
34
34
  * @property {bigint} lock_lp_start_price - Locked liquidity start price.
35
35
  * @property {bigint} lock_lp_end_price - Locked liquidity end price.
36
- * @property {number} lock_lp_sol_amount - 锁定的SOL数量。
37
- * @property {number} lock_lp_token_amount - 锁定的代币数量。
36
+ * @property {number} lock_lp_sol_amount - Locked SOL amount.
37
+ * @property {number} lock_lp_token_amount - Locked token amount.
38
38
  */
39
39
 
40
40
  /**
41
41
  * @typedef {Object} OverlapResult
42
- * @property {boolean} no_overlap - 是否没有重叠。`true` 表示没有重叠(可以安全插入),`false` 表示有重叠。
43
- * @property {number[]} close_insert_indices - 平仓时插入订单簿的位置索引数组。包含主位置索引及其前后3个节点的索引。
44
- * @property {string} overlap_reason - 重叠原因说明。当没有重叠时为空字符串,有重叠时说明具体原因。
42
+ * @property {boolean} no_overlap - Whether there is no overlap. `true` means no overlap (safe to insert), `false` means there is an overlap.
43
+ * @property {number[]} close_insert_indices - Array of position indices for inserting into the order book when closing. Contains the main position index and the indices of the 3 nodes before and after it.
44
+ * @property {string} overlap_reason - Description of the overlap reason. Empty string when there is no overlap, otherwise describes the specific reason.
45
45
  */
46
46
 
47
47
  /**
48
- * 检查给定价格区间是否与已排序的订单列表中的任何区间发生重叠,并返回合适的插入位置索引。
48
+ * Checks whether the given price range overlaps with any range in the sorted order list, and returns a suitable insertion position index.
49
49
  *
50
- * ## 功能说明
51
- * 此函数用于保证金交易(long/short)场景,在开仓时需要确定平仓订单应该插入到订单簿(OrderBook)的哪个位置。
52
- * 函数会检查新订单的价格区间是否与现有订单重叠,并返回多个候选插入位置索引,以提高合约执行成功率。
50
+ * ## Description
51
+ * This function is used in margin trading (long/short) scenarios, where the position to insert the closing order into the order book (OrderBook) needs to be determined when opening a position.
52
+ * The function checks whether the new order's price range overlaps with existing orders, and returns multiple candidate insertion position indices to improve the contract execution success rate.
53
53
  *
54
- * ## 核心逻辑
55
- * 1. **价格区间检查**:使用二分查找算法在已排序的订单列表中查找合适的插入位置
56
- * 2. **重叠检测**:
57
- * - 基础重叠:新区间与现有订单的价格区间直接重叠
58
- * - 流动性预留重叠:考虑到流动性预留区域(默认100%),防止价格区间过于接近
59
- * 3. **候选索引生成**:
60
- * - 主插入位置:逻辑上最合适的插入位置索引
61
- * - 备选位置:该位置前后各若干个节点的索引(数量由 MAX_CANDIDATE_INDICES 常量决定)
62
- * - 目的:即使主位置的订单被删除或移动,合约也能找到其他合适位置
54
+ * ## Core Logic
55
+ * 1. **Price range check**: Use a binary search algorithm to find a suitable insertion position in the sorted order list
56
+ * 2. **Overlap detection**:
57
+ * - Basic overlap: the new range directly overlaps with the price range of an existing order
58
+ * - Liquidity reservation overlap: considering the liquidity reservation area (default 100%), to prevent price ranges from being too close
59
+ * 3. **Candidate index generation**:
60
+ * - Main insertion position: the logically most suitable insertion position index
61
+ * - Alternative positions: the indices of several nodes before and after that position (the count is determined by the MAX_CANDIDATE_INDICES constant)
62
+ * - Purpose: even if the order at the main position is deleted or moved, the contract can still find another suitable position
63
63
  *
64
- * ## 返回值说明
65
- * - **无重叠时**:返回 `close_insert_indices` 数组,包含候选插入位置的 OrderBook 索引
66
- * - 优先级:主位置 → 前1个 → 后1个 → 前2个 → 后2个 → ... → 前N个 → 后N个
67
- * - 索引数量由 MAX_CANDIDATE_INDICES 常量决定(默认21个,即主位置+前10个+后10个)
68
- * - **有重叠时**:返回空数组 `[]`,表示无法插入
69
- * - **空订单簿**:返回 `[65535]` (u16::MAX),表示插入到头部
64
+ * ## Return Value Description
65
+ * - **When no overlap**: returns the `close_insert_indices` array, containing the OrderBook indices of candidate insertion positions
66
+ * - Priority: main position → 1 before → 1 after → 2 before → 2 after → ... → N before → N after
67
+ * - The number of indices is determined by the MAX_CANDIDATE_INDICES constant (default 21, i.e. main position + 10 before + 10 after)
68
+ * - **When there is overlap**: returns an empty array `[]`, indicating insertion is not possible
69
+ * - **Empty order book**: returns `[65535]` (u16::MAX), indicating insertion at the head
70
70
  *
71
- * ## 订单类型规则
72
- * - **down_orders(做多订单)**:价格从高到低排序
73
- * - lock_lp_start_price > lock_lp_end_price(价格下跌)
74
- * - 新订单的 end_price 必须 >= 下一个订单的 start_price
75
- * - **up_orders(做空订单)**:价格从低到高排序
76
- * - lock_lp_start_price < lock_lp_end_price(价格上涨)
77
- * - 新订单的 end_price 必须 <= 下一个订单的 start_price
71
+ * ## Order Type Rules
72
+ * - **down_orders (long orders)**: prices sorted from high to low
73
+ * - lock_lp_start_price > lock_lp_end_price (price falling)
74
+ * - the new order's end_price must be >= the next order's start_price
75
+ * - **up_orders (short orders)**: prices sorted from low to high
76
+ * - lock_lp_start_price < lock_lp_end_price (price rising)
77
+ * - the new order's end_price must be <= the next order's start_price
78
78
  *
79
- * @param {'down_orders' | 'up_orders'} order_type - 订单类型
80
- * - 'down_orders': 做多订单,价格从高到低排序
81
- * - 'up_orders': 做空订单,价格从低到高排序
79
+ * @param {'down_orders' | 'up_orders'} order_type - Order type
80
+ * - 'down_orders': long orders, prices sorted from high to low
81
+ * - 'up_orders': short orders, prices sorted from low to high
82
82
  *
83
- * @param {Order[]} order_list - 已排序的订单对象数组
84
- * - 每个订单必须包含以下字段:
85
- * - `index` {number}: 订单在 OrderBook 中的原始索引值(这是合约需要的关键字段)
86
- * - `lock_lp_start_price` {bigint|string}: 锁定流动池区间起始价格
87
- * - `lock_lp_end_price` {bigint|string}: 锁定流动池区间结束价格
88
- * - 数组必须已按价格排序(down_orders 从高到低,up_orders 从低到高)
89
- * - 通常来自 `sdk.chain.orders()` 或 `sdk.fast.orders()` 的返回数据
83
+ * @param {Order[]} order_list - Sorted array of order objects
84
+ * - Each order must contain the following fields:
85
+ * - `index` {number}: the original index value of the order in the OrderBook (this is the key field required by the contract)
86
+ * - `lock_lp_start_price` {bigint|string}: the start price of the locked liquidity pool range
87
+ * - `lock_lp_end_price` {bigint|string}: the end price of the locked liquidity pool range
88
+ * - The array must already be sorted by price (down_orders from high to low, up_orders from low to high)
89
+ * - Usually comes from the data returned by `sdk.chain.orders()` or `sdk.fast.orders()`
90
90
  *
91
- * @param {bigint | number | string} lp_start_price - 新订单的起始价格
92
- * - 对于 down_orders:这是较高的价格(开仓价附近)
93
- * - 对于 up_orders:这是较低的价格(止损价附近)
91
+ * @param {bigint | number | string} lp_start_price - The start price of the new order
92
+ * - For down_orders: this is the higher price (near the opening price)
93
+ * - For up_orders: this is the lower price (near the stop loss price)
94
94
  *
95
- * @param {bigint | number | string} lp_end_price - 新订单的结束价格
96
- * - 对于 down_orders:这是较低的价格(止损价附近)
97
- * - 对于 up_orders:这是较高的价格(开仓价附近)
95
+ * @param {bigint | number | string} lp_end_price - The end price of the new order
96
+ * - For down_orders: this is the lower price (near the stop loss price)
97
+ * - For up_orders: this is the higher price (near the opening price)
98
98
  *
99
- * @returns {OverlapResult} 返回包含重叠检查结果和候选插入索引的对象
100
- * @returns {boolean} returns.no_overlap - 是否没有重叠
101
- * - `true`: 可以安全插入,使用 `close_insert_indices` 中的索引
102
- * - `false`: 存在重叠,无法插入
103
- * @returns {number[]} returns.close_insert_indices - 候选插入位置的 OrderBook 索引数组
104
- * - 无重叠时:包含主位置及前后3个节点的索引(最多7个)
105
- * - 有重叠时:空数组 `[]`
106
- * - 空订单簿时:`[65535]` 表示插入到头部
107
- * @returns {string} returns.overlap_reason - 重叠原因说明
108
- * - 无重叠时:空字符串 `""`
109
- * - 有重叠时:描述具体原因(如 "Overlaps with existing order range")
99
+ * @returns {OverlapResult} Returns an object containing the overlap check result and candidate insertion indices
100
+ * @returns {boolean} returns.no_overlap - Whether there is no overlap
101
+ * - `true`: safe to insert, use the indices in `close_insert_indices`
102
+ * - `false`: there is an overlap, cannot insert
103
+ * @returns {number[]} returns.close_insert_indices - Array of OrderBook indices for candidate insertion positions
104
+ * - When no overlap: contains the main position and the indices of the 3 nodes before and after (up to 7)
105
+ * - When there is overlap: empty array `[]`
106
+ * - When order book is empty: `[65535]` indicates insertion at the head
107
+ * @returns {string} returns.overlap_reason - Description of the overlap reason
108
+ * - When no overlap: empty string `""`
109
+ * - When there is overlap: describes the specific reason (e.g. "Overlaps with existing order range")
110
110
  *
111
111
  * @example
112
- * // 示例 1: down_orders(做多订单)- 插入到中间位置
112
+ * // Example 1: down_orders (long orders) - insert into the middle position
113
113
  * const downOrders = [
114
- * { index: 10, lock_lp_start_price: 100n, lock_lp_end_price: 90n }, // 订单1
115
- * { index: 25, lock_lp_start_price: 80n, lock_lp_end_price: 70n }, // 订单2
116
- * { index: 33, lock_lp_start_price: 60n, lock_lp_end_price: 50n } // 订单3
114
+ * { index: 10, lock_lp_start_price: 100n, lock_lp_end_price: 90n }, // Order 1
115
+ * { index: 25, lock_lp_start_price: 80n, lock_lp_end_price: 70n }, // Order 2
116
+ * { index: 33, lock_lp_start_price: 60n, lock_lp_end_price: 50n } // Order 3
117
117
  * ];
118
118
  *
119
- * // 检查新订单 [75, 72] 是否可以插入
119
+ * // Check whether the new order [75, 72] can be inserted
120
120
  * const result = checkPriceRangeOverlap('down_orders', downOrders, 75n, 72n);
121
121
  * console.log(result);
122
- * // 返回: {
122
+ * // Returns: {
123
123
  * // no_overlap: true,
124
124
  * // close_insert_indices: [25, 10, 33],
125
- * // // 主位置是 25(订单2),因为新订单应该插入到订单2和订单3之间
126
- * // // 备选位置:10(订单1在前),33(订单3在后)
125
+ * // // The main position is 25 (Order 2), because the new order should be inserted between Order 2 and Order 3
126
+ * // // Alternative positions: 10 (Order 1 before), 33 (Order 3 after)
127
127
  * // overlap_reason: ""
128
128
  * // }
129
129
  *
130
130
  * @example
131
- * // 示例 2: down_orders - 价格重叠的情况
131
+ * // Example 2: down_orders - price overlap case
132
132
  * const downOrders = [
133
133
  * { index: 10, lock_lp_start_price: 100n, lock_lp_end_price: 90n },
134
134
  * { index: 25, lock_lp_start_price: 80n, lock_lp_end_price: 70n }
135
135
  * ];
136
136
  *
137
- * // 新订单 [95, 85] 与订单1 [100, 90] 重叠
137
+ * // New order [95, 85] overlaps with Order 1 [100, 90]
138
138
  * const result = checkPriceRangeOverlap('down_orders', downOrders, 95n, 85n);
139
139
  * console.log(result);
140
- * // 返回: {
140
+ * // Returns: {
141
141
  * // no_overlap: false,
142
142
  * // close_insert_indices: [],
143
143
  * // overlap_reason: "Overlaps with existing order range"
144
144
  * // }
145
145
  *
146
146
  * @example
147
- * // 示例 3: up_orders(做空订单)- 插入到末尾
147
+ * // Example 3: up_orders (short orders) - insert at the end
148
148
  * const upOrders = [
149
149
  * { index: 5, lock_lp_start_price: 70n, lock_lp_end_price: 80n },
150
150
  * { index: 12, lock_lp_start_price: 90n, lock_lp_end_price: 100n }
151
151
  * ];
152
152
  *
153
- * // 新订单 [110, 120] 应该插入到末尾
153
+ * // New order [110, 120] should be inserted at the end
154
154
  * const result = checkPriceRangeOverlap('up_orders', upOrders, 110n, 120n);
155
155
  * console.log(result);
156
- * // 返回: {
156
+ * // Returns: {
157
157
  * // no_overlap: true,
158
158
  * // close_insert_indices: [12, 5],
159
- * // // 主位置是 12(订单2),因为新订单应该插入到订单2之后
160
- * // // 备选位置:5(订单1在前)
159
+ * // // The main position is 12 (Order 2), because the new order should be inserted after Order 2
160
+ * // // Alternative positions: 5 (Order 1 before)
161
161
  * // overlap_reason: ""
162
162
  * // }
163
163
  *
164
164
  * @example
165
- * // 示例 4: 空订单簿 - 第一个订单
165
+ * // Example 4: empty order book - the first order
166
166
  * const emptyOrders = [];
167
167
  * const result = checkPriceRangeOverlap('down_orders', emptyOrders, 100n, 90n);
168
168
  * console.log(result);
169
- * // 返回: {
169
+ * // Returns: {
170
170
  * // no_overlap: true,
171
171
  * // close_insert_indices: [65535],
172
- * // // 65535 是 u16::MAX,表示插入到头部(空订单簿时的特殊值)
172
+ * // // 65535 is u16::MAX, indicating insertion at the head (special value when the order book is empty)
173
173
  * // overlap_reason: ""
174
174
  * // }
175
175
  *
176
176
  * @example
177
- * // 示例 5: 实际使用场景 - 做多交易
177
+ * // Example 5: actual usage scenario - long trade
178
178
  * async function openLongPosition(sdk, mint, buyTokenAmount, stopLossPrice) {
179
- * // 1. 获取 down_orders 数据
179
+ * // 1. Get down_orders data
180
180
  * const ordersData = await sdk.data.orders(mint, { type: 'down_orders' });
181
181
  * const orders = ordersData.data.orders;
182
182
  *
183
- * // 2. 获取当前价格
183
+ * // 2. Get the current price
184
184
  * const currentPrice = BigInt(await sdk.data.price(mint));
185
185
  *
186
- * // 3. 计算平仓价格区间(模拟)
186
+ * // 3. Calculate the closing price range (simulation)
187
187
  * const simulateResult = await sdk.simulator.simulateLongStopLoss(
188
188
  * mint,
189
189
  * buyTokenAmount,
190
190
  * stopLossPrice
191
191
  * );
192
192
  *
193
- * // 4. 检查价格区间是否可以插入
193
+ * // 4. Check whether the price range can be inserted
194
194
  * const overlapCheck = checkPriceRangeOverlap(
195
195
  * 'down_orders',
196
196
  * orders,
@@ -199,29 +199,29 @@ function transformOrdersData(ordersData) {
199
199
  * );
200
200
  *
201
201
  * if (!overlapCheck.no_overlap) {
202
- * throw new Error(`无法开仓: ${overlapCheck.overlap_reason}`);
202
+ * throw new Error(`Unable to open position: ${overlapCheck.overlap_reason}`);
203
203
  * }
204
204
  *
205
- * // 5. 使用 close_insert_indices 调用合约
205
+ * // 5. Use close_insert_indices to call the contract
206
206
  * const tx = await sdk.trading.long({
207
207
  * mint,
208
208
  * buyTokenAmount,
209
209
  * maxSolAmount,
210
210
  * marginSolMax,
211
211
  * closePrice: stopLossPrice,
212
- * closeInsertIndices: overlapCheck.close_insert_indices // 传递给合约
212
+ * closeInsertIndices: overlapCheck.close_insert_indices // Passed to the contract
213
213
  * });
214
214
  *
215
215
  * return tx;
216
216
  * }
217
217
  *
218
- * @throws {Error} 当输入的起始和结束价格与订单类型规则不匹配时抛出错误
218
+ * @throws {Error} Throws an error when the input start and end prices do not match the order type rules
219
219
  *
220
- * @see {@link https://github.com/your-repo/docs/orderbook.md|OrderBook 文档}
221
- * @see {@link transformOrdersData} 数据格式转换函数
220
+ * @see {@link https://github.com/your-repo/docs/orderbook.md|OrderBook documentation}
221
+ * @see {@link transformOrdersData} Data format transformation function
222
222
  *
223
223
  * @since 2.0.0
224
- * @version 2.0.0 - 从返回 prev_order_pda/next_order_pda 改为返回 close_insert_indices
224
+ * @version 2.0.0 - Changed from returning prev_order_pda/next_order_pda to returning close_insert_indices
225
225
  */
226
226
  function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_price) {
227
227
  // console.log("checkPriceRangeOverlap=",order_type,lp_start_price, lp_end_price)
@@ -236,7 +236,7 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
236
236
 
237
237
  const isDown = order_type === 'down_orders';
238
238
 
239
- // 验证并规范化输入价格区间,确保 minPrice <= maxPrice
239
+ // Validate and normalize the input price range, ensuring minPrice <= maxPrice
240
240
  if ((isDown && startPrice < endPrice) || (!isDown && startPrice > endPrice)) {
241
241
  throw new Error('输入的起始和结束价格与订单类型规则不匹配。');
242
242
  }
@@ -245,7 +245,7 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
245
245
 
246
246
  let low = 0;
247
247
  let high = order_list.length - 1;
248
- let insertionIndex = order_list.length; // 默认插入到最后
248
+ let insertionIndex = order_list.length; // Default to inserting at the end
249
249
 
250
250
  while (low <= high) {
251
251
  const mid = Math.floor((low + high) / 2);
@@ -256,9 +256,9 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
256
256
  const orderMin = isDown ? orderEnd : orderStart;
257
257
  const orderMax = isDown ? orderStart : orderEnd;
258
258
 
259
- // 核心重叠判断: (StartA < EndB) and (EndA > StartB)
259
+ // Core overlap check: (StartA < EndB) and (EndA > StartB)
260
260
  if (minPrice < orderMax && maxPrice > orderMin) {
261
- // 发生基础重叠
261
+ // Basic overlap occurred
262
262
  return {
263
263
  no_overlap: false,
264
264
  close_insert_indices: [],
@@ -267,16 +267,16 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
267
267
  }
268
268
 
269
269
  if (isDown) {
270
- // down_orders: 价格从大到小 (orderMax 递减)
271
- if (maxPrice > orderMax) { // 新区间在当前区间的“左边”(价格更高)
270
+ // down_orders: prices from high to low (orderMax decreasing)
271
+ if (maxPrice > orderMax) { // The new range is to the "left" of the current range (higher price)
272
272
  insertionIndex = mid;
273
273
  high = mid - 1;
274
274
  } else {
275
275
  low = mid + 1;
276
276
  }
277
277
  } else {
278
- // up_orders: 价格从小到大 (orderMin 递增)
279
- if (minPrice < orderMin) { // 新区间在当前区间的“左边”(价格更低)
278
+ // up_orders: prices from low to high (orderMin increasing)
279
+ if (minPrice < orderMin) { // The new range is to the "left" of the current range (lower price)
280
280
  insertionIndex = mid;
281
281
  high = mid - 1;
282
282
  } else {
@@ -285,12 +285,12 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
285
285
  }
286
286
  }
287
287
 
288
- // 根据找到的插入点,确定逻辑上的前后订单
289
- // insertionIndex 是新区间应该插入的位置,使得列表依然有序
288
+ // Based on the found insertion point, determine the logical previous and next orders
289
+ // insertionIndex is the position where the new range should be inserted so the list remains sorted
290
290
  const nextOrder = order_list[insertionIndex] || null;
291
291
  const prevOrder = order_list[insertionIndex - 1] || null;
292
292
 
293
- // 检查流动性预留重叠
293
+ // Check liquidity reservation overlap
294
294
  function checkLiquidityReservationOverlap(checkOrder) {
295
295
  if (!checkOrder) return false;
296
296
 
@@ -299,16 +299,16 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
299
299
  const orderMin = isDown ? orderEnd : orderStart;
300
300
  const orderMax = isDown ? orderStart : orderEnd;
301
301
 
302
- // 计算扩大区间值
302
+ // Calculate the expanded range value
303
303
  const expansionAmount = (orderMax - orderMin) * BigInt(Math.floor(LIQUIDITY_RESERVATION)) / 100n;
304
304
 
305
305
  let hasOverlap;
306
306
  if (isDown) {
307
- // down_orders: start不变,end向下扩大
307
+ // down_orders: start unchanged, end expands downward
308
308
  const expandedEnd = orderMin - expansionAmount;
309
309
  hasOverlap = startPrice >= expandedEnd;
310
310
  } else {
311
- // up_orders: start不变,end向上扩大
311
+ // up_orders: start unchanged, end expands upward
312
312
  const expandedEnd = orderMax + expansionAmount;
313
313
  hasOverlap = startPrice <= expandedEnd;
314
314
  }
@@ -316,7 +316,7 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
316
316
  return hasOverlap;
317
317
  }
318
318
 
319
- // 检查与前一个订单的流动性预留重叠
319
+ // Check liquidity reservation overlap with the previous order
320
320
  if (prevOrder && checkLiquidityReservationOverlap(prevOrder)) {
321
321
  return {
322
322
  no_overlap: false,
@@ -325,39 +325,39 @@ function checkPriceRangeOverlap(order_type, order_list, lp_start_price, lp_end_p
325
325
  };
326
326
  }
327
327
 
328
- // 无重叠,构建 close_insert_indices 数组
329
- // 优先级:主位置 → 前1个 → 后1个 → 前2个 → 后2个 → 前3个 → 后3个
328
+ // No overlap, build the close_insert_indices array
329
+ // Priority: main position → 1 before → 1 after → 2 before → 2 after → 3 before → 3 after
330
330
  const indices = [];
331
331
 
332
- // 主插入位置逻辑:
333
- // - down_orders (价格从高到低): 插入到 prevOrder 之后
334
- // - 如果没有 prevOrder (insertionIndex=0),说明价格最高,使用 u16::MAX 插入头部
335
- // - 如果有 prevOrder,使用 prevOrder.index,插入到它后面
336
- // - up_orders (价格从低到高): 插入到 prevOrder 之后
337
- // - 如果没有 prevOrder (insertionIndex=0),说明价格最低,使用 u16::MAX 插入头部
338
- // - 如果有 prevOrder,使用 prevOrder.index,插入到它后面
332
+ // Main insertion position logic:
333
+ // - down_orders (prices high to low): insert after prevOrder
334
+ // - If there is no prevOrder (insertionIndex=0), the price is the highest, use u16::MAX to insert at the head
335
+ // - If there is a prevOrder, use prevOrder.index, insert after it
336
+ // - up_orders (prices low to high): insert after prevOrder
337
+ // - If there is no prevOrder (insertionIndex=0), the price is the lowest, use u16::MAX to insert at the head
338
+ // - If there is a prevOrder, use prevOrder.index, insert after it
339
339
 
340
340
  if (prevOrder && prevOrder.index !== undefined) {
341
- // 有前置订单,插入到它后面
341
+ // There is a previous order, insert after it
342
342
  indices.push(prevOrder.index);
343
343
  } else {
344
- // 没有前置订单 (insertionIndex=0)
345
- // down_orders: 价格最高,插入头部 (65535)
346
- // up_orders: 价格最低,插入头部 (65535)
347
- indices.push(65535); // u16::MAX - 插入到头部
344
+ // No previous order (insertionIndex=0)
345
+ // down_orders: highest price, insert at the head (65535)
346
+ // up_orders: lowest price, insert at the head (65535)
347
+ indices.push(65535); // u16::MAX - insert at the head
348
348
  }
349
349
 
350
- // 添加前后节点的索引
351
- // 根据 MAX_CANDIDATE_INDICES 常量计算需要添加多少个前后节点
350
+ // Add the indices of the nodes before and after
351
+ // Calculate how many before/after nodes to add based on the MAX_CANDIDATE_INDICES constant
352
352
  for (let offset = 1; offset <= CANDIDATE_NODES_EACH_SIDE; offset++) {
353
- // 添加前面第 offset 个节点
353
+ // Add the offset-th node before
354
354
  const beforeIndex = insertionIndex - 1 - offset;
355
355
  if (beforeIndex >= 0 && order_list[beforeIndex] && order_list[beforeIndex].index !== undefined) {
356
356
  indices.push(order_list[beforeIndex].index);
357
357
  }
358
358
 
359
- // 添加后面第 offset 个节点
360
- // offset=1 应该是 nextOrder (insertionIndex),offset=2 是 insertionIndex+1,以此类推
359
+ // Add the offset-th node after
360
+ // offset=1 should be nextOrder (insertionIndex), offset=2 is insertionIndex+1, and so on
361
361
  const afterIndex = insertionIndex + offset - 1;
362
362
  if (afterIndex < order_list.length && order_list[afterIndex] && order_list[afterIndex].index !== undefined) {
363
363
  indices.push(order_list[afterIndex].index);
@@ -4,10 +4,9 @@
4
4
  const LIQUIDITY_RESERVATION = 100; // 100%
5
5
 
6
6
  // Price adjustment percentage
7
- const PRICE_ADJUSTMENT_PERCENTAGE = 15; // 5就是 0.5%
7
+ const PRICE_ADJUSTMENT_PERCENTAGE = 15; // 5 means 0.5%
8
8
 
9
9
  // Minimum stop loss percentage - stop loss price must be at least this far from current price
10
- // 最小止损百分比 - 止损价格必须与当前价格至少相差此百分比
11
10
  // Example: 40 means 4.0% (calculation: 40/1000 = 0.04 = 4%)
12
11
  const MIN_STOP_LOSS_PERCENT = 40; // 4.0%
13
12
 
@@ -21,10 +21,9 @@ class SimulatorModule {
21
21
 
22
22
  /**
23
23
  * Simulate token buy transaction - calculate if target token amount can be purchased
24
- * 模拟以 Token 数量为目标的买入交易 - 计算是否能买到指定数量的 Token
25
- * @param {string} mint - Token address 代币地址
26
- * @param {bigint|string|number} buyTokenAmount - Target token amount to buy 目标购买的 Token 数量
27
- * @param {string} passOrder - Optional order address to skip (won't be liquidated) 可选的跳过订单地址
24
+ * @param {string} mint - Token address
25
+ * @param {bigint|string|number} buyTokenAmount - Target token amount to buy
26
+ * @param {string} passOrder - Optional order address to skip (won't be liquidated)
28
27
  * @param {Object|null} lastPrice - Token price info, default null
29
28
  * @param {Object|null} ordersData - Orders response object, default null
30
29
  * @returns {Promise<Object>} Token buy simulation result with the following structure:
@@ -51,7 +50,7 @@ class SimulatorModule {
51
50
  * Simulate token sell transaction analysis
52
51
  * @param {string} mint - Token address
53
52
  * @param {bigint|string|number} sellTokenAmount - Token amount to sell (u64 format, precision 10^9)
54
- * @param {string} passOrder - Optional order address to skip (won't be liquidated) 可选的跳过订单地址
53
+ * @param {string} passOrder - Optional order address to skip (won't be liquidated)
55
54
  * @param {Object|null} lastPrice - Token price info, default null
56
55
  * @param {Object|null} ordersData - Orders response object, default null
57
56
  * @returns {Promise<Object>} Token sell simulation result with the following structure:
@@ -130,11 +129,10 @@ class SimulatorModule {
130
129
 
131
130
  /**
132
131
  * Generate candidate insertion indices for closing long position
133
- * 为做多平仓生成候选插入索引
134
- * @param {string} mint - Token address 代币地址
135
- * @param {number|string|anchor.BN} closeOrderId - Order ID to close (order_id, not index) 要平仓的订单ID
136
- * @param {Object|null} ordersData - Orders data (optional) 订单数据(可选)
137
- * @returns {Promise<Object>} Result containing closeOrderIndices array 包含候选索引数组的结果
132
+ * @param {string} mint - Token address
133
+ * @param {number|string|anchor.BN} closeOrderId - Order ID to close (order_id, not index)
134
+ * @param {Object|null} ordersData - Orders data (optional)
135
+ * @returns {Promise<Object>} Result containing closeOrderIndices array
138
136
  */
139
137
  async simulateLongClose(mint, closeOrderId, ordersData = null) {
140
138
  return simulateLongClose.call(this, mint, closeOrderId, ordersData);
@@ -142,11 +140,10 @@ class SimulatorModule {
142
140
 
143
141
  /**
144
142
  * Generate candidate insertion indices for closing short position
145
- * 为做空平仓生成候选插入索引
146
- * @param {string} mint - Token address 代币地址
147
- * @param {number|string|anchor.BN} closeOrderId - Order ID to close (order_id, not index) 要平仓的订单ID
148
- * @param {Object|null} ordersData - Orders data (optional) 订单数据(可选)
149
- * @returns {Promise<Object>} Result containing closeOrderIndices array 包含候选索引数组的结果
143
+ * @param {string} mint - Token address
144
+ * @param {number|string|anchor.BN} closeOrderId - Order ID to close (order_id, not index)
145
+ * @param {Object|null} ordersData - Orders data (optional)
146
+ * @returns {Promise<Object>} Result containing closeOrderIndices array
150
147
  */
151
148
  async simulateShortClose(mint, closeOrderId, ordersData = null) {
152
149
  return simulateShortClose.call(this, mint, closeOrderId, ordersData);
@@ -154,8 +151,7 @@ class SimulatorModule {
154
151
 
155
152
  /**
156
153
  * Simulate buy transaction with SOL amount input
157
- * 模拟以 SOL 金额为输入的买入交易
158
- * @param {string} mint - Token address 代币地址
154
+ * @param {string} mint - Token address
159
155
  * @param {bigint|string|number} buySolAmount - SOL amount to spend (u64 format, lamports)
160
156
  * @returns {Promise<Object>} Buy simulation result with the following structure:
161
157
  * - success: {boolean} Whether the simulation was successful
@@ -272,8 +268,7 @@ class SimulatorModule {
272
268
 
273
269
  /**
274
270
  * Simulate sell transaction with token amount input
275
- * 模拟以 Token 数量为输入的卖出交易
276
- * @param {string} mint - Token address 代币地址
271
+ * @param {string} mint - Token address
277
272
  * @param {bigint|string|number} sellTokenAmount - Token amount to sell (u64 format, lamports)
278
273
  * @returns {Promise<Object>} Sell simulation result with the following structure:
279
274
  * - success: {boolean} Whether the simulation was successful