@thru/programs 0.3.16 → 0.3.18

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.
Files changed (84) hide show
  1. package/dist/abi-manager/index.cjs +1 -1
  2. package/dist/abi-manager/index.cjs.map +1 -1
  3. package/dist/abi-manager/index.js +2 -2
  4. package/dist/amm/index.cjs +235 -28
  5. package/dist/amm/index.cjs.map +1 -1
  6. package/dist/amm/index.d.cts +34 -6
  7. package/dist/amm/index.d.ts +34 -6
  8. package/dist/amm/index.js +238 -31
  9. package/dist/amm/index.js.map +1 -1
  10. package/dist/bootstrap-addresses/index.cjs.map +1 -1
  11. package/dist/bootstrap-addresses/index.js +3 -3
  12. package/dist/chunk-4F54VFIH.js +58 -0
  13. package/dist/chunk-4F54VFIH.js.map +1 -0
  14. package/dist/{chunk-7DRJVAYI.js → chunk-A44ZPXY6.js} +5 -58
  15. package/dist/chunk-A44ZPXY6.js.map +1 -0
  16. package/dist/{chunk-VNTPD4AR.js → chunk-FHYZQ5R3.js} +3 -3
  17. package/dist/{chunk-VNTPD4AR.js.map → chunk-FHYZQ5R3.js.map} +1 -1
  18. package/dist/{chunk-BNYDH4ZP.js → chunk-FQDO6J6E.js} +3 -3
  19. package/dist/{chunk-BNYDH4ZP.js.map → chunk-FQDO6J6E.js.map} +1 -1
  20. package/dist/{chunk-ZHF2PH5H.js → chunk-IGZZOZ73.js} +4 -4
  21. package/dist/{chunk-ZHF2PH5H.js.map → chunk-IGZZOZ73.js.map} +1 -1
  22. package/dist/{chunk-CYG4PB65.js → chunk-NMPO53TW.js} +4 -4
  23. package/dist/{chunk-CYG4PB65.js.map → chunk-NMPO53TW.js.map} +1 -1
  24. package/dist/{chunk-7DHFYBRW.js → chunk-O2ONVSHW.js} +3 -3
  25. package/dist/{chunk-7DHFYBRW.js.map → chunk-O2ONVSHW.js.map} +1 -1
  26. package/dist/{chunk-YX7BLFQZ.js → chunk-PRU4KGQO.js} +3 -3
  27. package/dist/chunk-PRU4KGQO.js.map +1 -0
  28. package/dist/chunk-Q6762F4G.js +174 -0
  29. package/dist/chunk-Q6762F4G.js.map +1 -0
  30. package/dist/{chunk-6IC6CO5S.js → chunk-UPB725FV.js} +3 -3
  31. package/dist/{chunk-6IC6CO5S.js.map → chunk-UPB725FV.js.map} +1 -1
  32. package/dist/clob/index.cjs +328 -304
  33. package/dist/clob/index.cjs.map +1 -1
  34. package/dist/clob/index.d.cts +4 -3
  35. package/dist/clob/index.d.ts +4 -3
  36. package/dist/clob/index.js +37 -198
  37. package/dist/clob/index.js.map +1 -1
  38. package/dist/deploy/index.cjs +1 -1
  39. package/dist/deploy/index.cjs.map +1 -1
  40. package/dist/deploy/index.js +7 -7
  41. package/dist/manager/index.cjs +1 -1
  42. package/dist/manager/index.cjs.map +1 -1
  43. package/dist/manager/index.d.cts +2 -79
  44. package/dist/manager/index.d.ts +2 -79
  45. package/dist/manager/index.js +3 -3
  46. package/dist/multicall/index.cjs.map +1 -1
  47. package/dist/multicall/index.js +4 -4
  48. package/dist/oracle/index.cjs.map +1 -1
  49. package/dist/oracle/index.d.cts +3 -39
  50. package/dist/oracle/index.d.ts +3 -39
  51. package/dist/oracle/index.js +3 -3
  52. package/dist/orderbook-CEYM4N_S.d.cts +5 -0
  53. package/dist/orderbook-CEYM4N_S.d.ts +5 -0
  54. package/dist/passkey-manager/index.cjs.map +1 -1
  55. package/dist/passkey-manager/index.js +3 -3
  56. package/dist/perp/index.cjs +16740 -0
  57. package/dist/perp/index.cjs.map +1 -0
  58. package/dist/perp/index.d.cts +4577 -0
  59. package/dist/perp/index.d.ts +4577 -0
  60. package/dist/perp/index.js +16245 -0
  61. package/dist/perp/index.js.map +1 -0
  62. package/dist/token/index.js +2 -1
  63. package/dist/types-5XmDprcB.d.ts +81 -0
  64. package/dist/types-Cpi7Ibs2.d.cts +81 -0
  65. package/dist/types-T4cL02K7.d.cts +39 -0
  66. package/dist/types-T4cL02K7.d.ts +39 -0
  67. package/dist/uploader/index.cjs +1 -1
  68. package/dist/uploader/index.cjs.map +1 -1
  69. package/dist/uploader/index.js +2 -2
  70. package/package.json +7 -2
  71. package/src/amm/abi/thru/program/amm/types.ts +282 -23
  72. package/src/amm/index.test.ts +93 -3
  73. package/src/amm/index.ts +18 -1
  74. package/src/clob/index.ts +67 -201
  75. package/src/perp/abi/thru/common/primitives/types.ts +2428 -0
  76. package/src/perp/abi/thru/program/perp/types.ts +16813 -0
  77. package/src/perp/index.test.ts +822 -0
  78. package/src/perp/index.ts +1793 -0
  79. package/src/utils/helpers.ts +1 -1
  80. package/src/utils/orderbook.test.ts +86 -0
  81. package/src/utils/orderbook.ts +214 -0
  82. package/tsup.config.ts +1 -0
  83. package/dist/chunk-7DRJVAYI.js.map +0 -1
  84. package/dist/chunk-YX7BLFQZ.js.map +0 -1
package/src/amm/index.ts CHANGED
@@ -85,6 +85,7 @@ export const AMM_ERROR_LABELS: Record<number, string> = {
85
85
  17: 'Liquidity bounds',
86
86
  18: 'Vault mismatch',
87
87
  19: 'LP mint mismatch',
88
+ 20: 'Slippage exceeded',
88
89
  };
89
90
 
90
91
  const PUBKEY_LENGTH = 32;
@@ -169,6 +170,8 @@ export interface AddLiquidityArgs {
169
170
  tokenProgramAccountBytes: Uint8Array;
170
171
  maxAmountMintOne: bigint;
171
172
  maxAmountMintTwo: bigint;
173
+ /** Inclusive minimum LP output; zero explicitly disables this bound. */
174
+ minLpOut: bigint;
172
175
  }
173
176
 
174
177
  export interface WithdrawLiquidityArgs {
@@ -182,6 +185,10 @@ export interface WithdrawLiquidityArgs {
182
185
  lpMintAccountBytes: Uint8Array;
183
186
  tokenProgramAccountBytes: Uint8Array;
184
187
  lpAmount: bigint;
188
+ /** Inclusive minimum token-one output; zero explicitly disables this bound. */
189
+ minAmountOneOut: bigint;
190
+ /** Inclusive minimum token-two output; zero explicitly disables this bound. */
191
+ minAmountTwoOut: bigint;
185
192
  }
186
193
 
187
194
  export interface SwapArgs {
@@ -194,6 +201,8 @@ export interface SwapArgs {
194
201
  lpMintAccountBytes: Uint8Array;
195
202
  tokenProgramAccountBytes: Uint8Array;
196
203
  amountIn: bigint;
204
+ /** Inclusive minimum swap output; zero explicitly disables this bound. */
205
+ minAmountOut: bigint;
197
206
  }
198
207
 
199
208
  export function sortAmmMints(
@@ -292,6 +301,7 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
292
301
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
293
302
  assertU64(args.maxAmountMintOne, 'maxAmountMintOne');
294
303
  assertU64(args.maxAmountMintTwo, 'maxAmountMintTwo');
304
+ assertU64(args.minLpOut, 'minLpOut');
295
305
  const payload = new AmmAddLiquidityInstructionBuilder()
296
306
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
297
307
  .set_depositor_account_idx(accountIndex(context, args.depositorAccountBytes))
@@ -304,6 +314,7 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
304
314
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
305
315
  .set_max_amount_mint_one(args.maxAmountMintOne)
306
316
  .set_max_amount_mint_two(args.maxAmountMintTwo)
317
+ .set_min_lp_out(args.minLpOut)
307
318
  .build();
308
319
  return buildAmmInstruction('add_liquidity', payload);
309
320
  };
@@ -312,6 +323,8 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
312
323
  export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs): InstructionData {
313
324
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
314
325
  assertU64(args.lpAmount, 'lpAmount');
326
+ assertU64(args.minAmountOneOut, 'minAmountOneOut');
327
+ assertU64(args.minAmountTwoOut, 'minAmountTwoOut');
315
328
  const payload = new AmmWithdrawLiquidityInstructionBuilder()
316
329
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
317
330
  .set_withdrawer_account_idx(accountIndex(context, args.withdrawerAccountBytes))
@@ -323,6 +336,8 @@ export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs):
323
336
  .set_lp_mint_account_idx(accountIndex(context, args.lpMintAccountBytes))
324
337
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
325
338
  .set_lp_amount(args.lpAmount)
339
+ .set_min_amount_one_out(args.minAmountOneOut)
340
+ .set_min_amount_two_out(args.minAmountTwoOut)
326
341
  .build();
327
342
  return buildAmmInstruction('withdraw_liquidity', payload);
328
343
  };
@@ -331,6 +346,7 @@ export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs):
331
346
  export function createSwapInstruction(args: SwapArgs): InstructionData {
332
347
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
333
348
  assertU64(args.amountIn, 'amountIn');
349
+ assertU64(args.minAmountOut, 'minAmountOut');
334
350
  const payload = new AmmSwapInstructionBuilder()
335
351
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
336
352
  .set_user_transfer_authority_idx(accountIndex(context, args.userTransferAuthorityBytes))
@@ -341,6 +357,7 @@ export function createSwapInstruction(args: SwapArgs): InstructionData {
341
357
  .set_lp_mint_account_idx(accountIndex(context, args.lpMintAccountBytes))
342
358
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
343
359
  .set_amount_in(args.amountIn)
360
+ .set_min_amount_out(args.minAmountOut)
344
361
  .build();
345
362
  return buildAmmInstruction('swap', payload);
346
363
  };
@@ -400,7 +417,7 @@ function assertU16(value: number, label: string): void {
400
417
  }
401
418
 
402
419
  function assertU64(value: bigint, label: string): void {
403
- if (value < 0n || value > 0xffffffffffffffffn) {
420
+ if (typeof value !== 'bigint' || value < 0n || value > 0xffffffffffffffffn) {
404
421
  throw new Error(`${label} must be between 0 and 18446744073709551615`);
405
422
  }
406
423
  }
package/src/clob/index.ts CHANGED
@@ -1,12 +1,51 @@
1
1
  import { Pubkey, deriveProgramAddress } from '@thru/sdk';
2
- import { encodeAddress } from '@thru/sdk/helpers';
3
2
  import type { Account } from '@thru/sdk';
4
3
  import { BOOTSTRAP_PROGRAM_ADDRESSES } from '../bootstrap-addresses';
4
+ import { accountData, accountIndex } from '../utils/helpers';
5
+ import {
6
+ CLIENT_ID_SIZE,
7
+ EVENT_REMOVE_REASON_EVICTED,
8
+ EVENT_REMOVE_REASON_EXPIRED,
9
+ EVENT_REMOVE_REASON_FILLED,
10
+ EVENT_REMOVE_REASON_USER,
11
+ EVENT_SIDE_BUY,
12
+ EVENT_SIDE_SELL,
13
+ ORDER_FLAG_BUY,
14
+ ORDER_FLAG_HAS_CLIENT_ID,
15
+ ORDER_TYPE_ALO,
16
+ ORDER_TYPE_FOK,
17
+ ORDER_TYPE_GTC,
18
+ ORDER_TYPE_IOC,
19
+ ORDER_TYPE_MTL,
20
+ STATUS_FLAG_DEPOSITS_FROZEN,
21
+ STATUS_FLAG_MASK,
22
+ STATUS_FLAG_PAUSED,
23
+ STATUS_FLAG_POST_ONLY,
24
+ STATUS_FLAG_WITHDRAWALS_FROZEN,
25
+ assertClientId,
26
+ assertStatusFlags,
27
+ assertU64,
28
+ assertU8,
29
+ buildModifyOrderMetadata,
30
+ bitField,
31
+ bytesFromView,
32
+ cbookLevelFromView,
33
+ eventSideFromValue,
34
+ fixedSeed,
35
+ groupByPrice,
36
+ optionalAccountIndex,
37
+ orderFlags,
38
+ orderTypeFromValue,
39
+ pubkeyViewToAddress,
40
+ removalReasonFromValue,
41
+ type OrderRemovalReason,
42
+ type OrderSide,
43
+ type OrderType,
44
+ } from '../utils/orderbook';
5
45
  import {
6
46
  ArenaHeader,
7
47
  CbookAccount,
8
48
  CbookHeader,
9
- CbookLevel,
10
49
  ClobEvent,
11
50
  ClobInstructionBuilder,
12
51
  CreateOrderEntryInstructionBuilder,
@@ -139,35 +178,33 @@ export const CLOB_EVENT_EXCHANGE_STATUS = 12;
139
178
  export const CLOB_EVENT_EXCHANGE_ADMIN = 13;
140
179
  export const CLOB_EVENT_MARKET_EXCHANGE_STATUS = 14;
141
180
 
142
- export const CLOB_EVENT_SIDE_BUY = 0;
143
- export const CLOB_EVENT_SIDE_SELL = 1;
181
+ /* Book, flag and event encodings shared with the perp program (utils/orderbook). */
182
+ export const CLOB_EVENT_SIDE_BUY = EVENT_SIDE_BUY;
183
+ export const CLOB_EVENT_SIDE_SELL = EVENT_SIDE_SELL;
144
184
  export const CLOB_EVENT_TOKEN_SIDE_BASE = 0;
145
185
  export const CLOB_EVENT_TOKEN_SIDE_QUOTE = 1;
146
- export const CLOB_EVENT_REMOVE_REASON_FILLED = 0;
147
- export const CLOB_EVENT_REMOVE_REASON_EXPIRED = 1;
148
- export const CLOB_EVENT_REMOVE_REASON_EVICTED = 2;
149
- export const CLOB_EVENT_REMOVE_REASON_USER = 3;
150
-
151
- export const CLOB_STATUS_FLAG_PAUSED = 1 << 0;
152
- export const CLOB_STATUS_FLAG_POST_ONLY = 1 << 1;
153
- export const CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN = 1 << 2;
154
- export const CLOB_STATUS_FLAG_DEPOSITS_FROZEN = 1 << 3;
155
- export const CLOB_STATUS_FLAG_MASK =
156
- CLOB_STATUS_FLAG_PAUSED |
157
- CLOB_STATUS_FLAG_POST_ONLY |
158
- CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN |
159
- CLOB_STATUS_FLAG_DEPOSITS_FROZEN;
186
+ export const CLOB_EVENT_REMOVE_REASON_FILLED = EVENT_REMOVE_REASON_FILLED;
187
+ export const CLOB_EVENT_REMOVE_REASON_EXPIRED = EVENT_REMOVE_REASON_EXPIRED;
188
+ export const CLOB_EVENT_REMOVE_REASON_EVICTED = EVENT_REMOVE_REASON_EVICTED;
189
+ export const CLOB_EVENT_REMOVE_REASON_USER = EVENT_REMOVE_REASON_USER;
190
+
191
+ export const CLOB_STATUS_FLAG_PAUSED = STATUS_FLAG_PAUSED;
192
+ export const CLOB_STATUS_FLAG_POST_ONLY = STATUS_FLAG_POST_ONLY;
193
+ export const CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN = STATUS_FLAG_WITHDRAWALS_FROZEN;
194
+ export const CLOB_STATUS_FLAG_DEPOSITS_FROZEN = STATUS_FLAG_DEPOSITS_FROZEN;
195
+ export const CLOB_STATUS_FLAG_MASK = STATUS_FLAG_MASK;
196
+
160
197
 
161
198
  export const CLOB_PROGRAM_ADDRESS = BOOTSTRAP_PROGRAM_ADDRESSES.clob;
162
199
 
163
- export const CLOB_ORDER_TYPE_GTC = 0;
164
- export const CLOB_ORDER_TYPE_MTL = 1;
165
- export const CLOB_ORDER_TYPE_ALO = 2;
166
- export const CLOB_ORDER_TYPE_IOC = 3;
167
- export const CLOB_ORDER_TYPE_FOK = 4;
200
+ export const CLOB_ORDER_TYPE_GTC = ORDER_TYPE_GTC;
201
+ export const CLOB_ORDER_TYPE_MTL = ORDER_TYPE_MTL;
202
+ export const CLOB_ORDER_TYPE_ALO = ORDER_TYPE_ALO;
203
+ export const CLOB_ORDER_TYPE_IOC = ORDER_TYPE_IOC;
204
+ export const CLOB_ORDER_TYPE_FOK = ORDER_TYPE_FOK;
168
205
 
169
- export const CLOB_ORDER_FLAG_BUY = 1 << 0;
170
- export const CLOB_ORDER_FLAG_HAS_CLIENT_ID = 1 << 6;
206
+ export const CLOB_ORDER_FLAG_BUY = ORDER_FLAG_BUY;
207
+ export const CLOB_ORDER_FLAG_HAS_CLIENT_ID = ORDER_FLAG_HAS_CLIENT_ID;
171
208
  export const CLOB_MODIFY_FLAG_FAIL_IF_OUT_OF_RANGE = 1 << 0;
172
209
  export const CLOB_MODIFY_FLAG_HAS_CLIENT_ID = 1 << 1;
173
210
  export const CLOB_MODIFY_FLAG_HAS_ORDER_ID = 1 << 2;
@@ -188,7 +225,7 @@ export const CLOB_CBOOK_LEVEL_SIZE = 8;
188
225
  export const CLOB_CBOOK_MAX_SIZE = ((1 << 24) - CLOB_CBOOK_HEADER_SIZE) / CLOB_CBOOK_LEVEL_SIZE;
189
226
  export const CLOB_CBOOK_EMPTY_PRICE_IN_TICKS = (1n << 64n) - 1n;
190
227
  export const CLOB_CBOOK_MAX_PRICE_IN_TICKS = CLOB_CBOOK_EMPTY_PRICE_IN_TICKS - 2n - BigInt(CLOB_CBOOK_MAX_SIZE);
191
- export const CLOB_CLIENT_ID_SIZE = 16;
228
+ export const CLOB_CLIENT_ID_SIZE = CLIENT_ID_SIZE;
192
229
  export const CLOB_SEATLESS_SEAT_IDX = 1;
193
230
  export const CLOB_NULL_INDEX = 0xfffff;
194
231
 
@@ -208,8 +245,8 @@ type ClobInstructionVariant =
208
245
  | 'exchange_recover_admin'
209
246
  | 'market_set_exchange_status';
210
247
 
211
- export type ClobOrderSide = 'buy' | 'sell';
212
- export type ClobOrderType = 'gtc' | 'mtl' | 'alo' | 'ioc' | 'fok';
248
+ export type ClobOrderSide = OrderSide;
249
+ export type ClobOrderType = OrderType;
213
250
 
214
251
  export type AccountLookupContext = {
215
252
  getAccountIndex: (pubkey: Uint8Array) => number;
@@ -422,7 +459,8 @@ export interface ClobOrderBookSnapshot {
422
459
 
423
460
 
424
461
  export type ClobTokenSide = 'base' | 'quote';
425
- export type ClobOrderRemovalReason = 'filled' | 'expired' | 'evicted' | 'user';
462
+ export type ClobOrderRemovalReason = OrderRemovalReason;
463
+
426
464
 
427
465
  export interface ParsedClobEventBase {
428
466
  eventType: bigint;
@@ -1114,20 +1152,6 @@ export function encodeOrderFlags(args: {
1114
1152
  return orderFlags(args);
1115
1153
  }
1116
1154
 
1117
- function buildModifyOrderMetadata(args: ModifyOrderArgs): Uint8Array {
1118
- const metadata = new Uint8Array((args.clientId ? CLOB_CLIENT_ID_SIZE : 0) + (args.orderId !== undefined ? 8 : 0));
1119
- let offset = 0;
1120
- if (args.clientId) {
1121
- metadata.set(assertClientId(args.clientId), offset);
1122
- offset += CLOB_CLIENT_ID_SIZE;
1123
- }
1124
- if (args.orderId !== undefined) {
1125
- assertU64(args.orderId, 'orderId');
1126
- new DataView(metadata.buffer).setBigUint64(offset, args.orderId, true);
1127
- }
1128
- return metadata;
1129
- }
1130
-
1131
1155
  function createTokenTransferInstruction(
1132
1156
  variant: 'token_deposit' | 'token_withdraw',
1133
1157
  args: TokenTransferArgs
@@ -1153,31 +1177,6 @@ function buildClobInstruction(variant: ClobInstructionVariant, payload: Uint8Arr
1153
1177
  return builder.build();
1154
1178
  }
1155
1179
 
1156
- function orderFlags(args: {
1157
- side: ClobOrderSide;
1158
- orderType?: ClobOrderType;
1159
- clientId?: Uint8Array;
1160
- discardAfterMatch?: boolean;
1161
- failIfOutsideBook?: boolean;
1162
- }): number {
1163
- let flags = args.side === 'buy' ? CLOB_ORDER_FLAG_BUY : 0;
1164
- flags |= orderTypeValue(args.orderType ?? 'gtc') << 1;
1165
- if (args.discardAfterMatch) flags |= 1 << 4;
1166
- if (args.failIfOutsideBook) flags |= 1 << 5;
1167
- if (args.clientId) flags |= CLOB_ORDER_FLAG_HAS_CLIENT_ID;
1168
- return flags;
1169
- }
1170
-
1171
- function orderTypeValue(orderType: ClobOrderType): number {
1172
- switch (orderType) {
1173
- case 'gtc': return CLOB_ORDER_TYPE_GTC;
1174
- case 'mtl': return CLOB_ORDER_TYPE_MTL;
1175
- case 'alo': return CLOB_ORDER_TYPE_ALO;
1176
- case 'ioc': return CLOB_ORDER_TYPE_IOC;
1177
- case 'fok': return CLOB_ORDER_TYPE_FOK;
1178
- }
1179
- }
1180
-
1181
1180
  function marketFromView(view: MarketAccountView): ClobMarket {
1182
1181
  return {
1183
1182
  magic: view.get_magic(),
@@ -1239,14 +1238,6 @@ function orderEntryFromView(view: OrderEntryView, orderEntryIndex: number): Clob
1239
1238
  };
1240
1239
  }
1241
1240
 
1242
- function cbookLevelFromView(view: CbookLevel, levelIndex: number): ClobCbookLevel {
1243
- return {
1244
- levelIndex,
1245
- headOrderEntryIndex: view.get_head_entry_idx(),
1246
- tailOrderEntryIndex: view.get_tail_entry_idx(),
1247
- };
1248
- }
1249
-
1250
1241
  function normalizeBookOrder(
1251
1242
  order: ClobOrderBookOrder | ClobOrderEntry,
1252
1243
  side: ClobOrderSide,
@@ -1266,30 +1257,6 @@ function normalizeBookOrder(
1266
1257
  };
1267
1258
  }
1268
1259
 
1269
- function groupByPrice(orders: ClobOrderBookOrder[], side: ClobOrderSide): ClobOrderBookLevel[] {
1270
- const levels = new Map<bigint, ClobOrderBookLevel>();
1271
- for (const order of orders) {
1272
- if (order.quantityInLots === 0n) continue;
1273
- const existing = levels.get(order.priceInTicks);
1274
- if (existing) {
1275
- existing.quantityInLots += order.quantityInLots;
1276
- existing.orders.push(order);
1277
- } else {
1278
- levels.set(order.priceInTicks, {
1279
- side,
1280
- priceInTicks: order.priceInTicks,
1281
- quantityInLots: order.quantityInLots,
1282
- orders: [order],
1283
- });
1284
- }
1285
- }
1286
- return [...levels.values()].sort((a, b) => {
1287
- if (a.priceInTicks === b.priceInTicks) return 0;
1288
- const asc = a.priceInTicks < b.priceInTicks ? -1 : 1;
1289
- return side === 'buy' ? -asc : asc;
1290
- });
1291
- }
1292
-
1293
1260
  function priceForLevel(
1294
1261
  levelIndex: number,
1295
1262
  side: ClobOrderSide,
@@ -1327,44 +1294,6 @@ function priceForCbookLevel(levelIndex: number, side: ClobOrderSide, cbook: Clob
1327
1294
  return side === 'buy' ? CLOB_CBOOK_MAX_PRICE_IN_TICKS - cbookPriceInTicks : cbookPriceInTicks;
1328
1295
  }
1329
1296
 
1330
- function accountData(accountOrData: Account | Uint8Array, label: string): Uint8Array {
1331
- const data = accountOrData instanceof Uint8Array ? accountOrData : accountOrData.data?.data;
1332
- if (!data) throw new Error(`${label} data is missing`);
1333
- return data;
1334
- }
1335
-
1336
- function fixedSeed(value: string): Uint8Array {
1337
- const bytes = new TextEncoder().encode(value);
1338
- if (bytes.length > 32) throw new Error('seed cannot exceed 32 bytes');
1339
- const seed = new Uint8Array(32);
1340
- seed.set(bytes);
1341
- return seed;
1342
- }
1343
-
1344
- function optionalAccountIndex(context: AccountLookupContext, pubkey?: Uint8Array): number {
1345
- return pubkey ? accountIndex(context, pubkey) : 0xffff;
1346
- }
1347
-
1348
- function accountIndex(context: AccountLookupContext, pubkey: Uint8Array): number {
1349
- const index = context.getAccountIndex(pubkey);
1350
- return assertU16(index, 'account index');
1351
- }
1352
-
1353
- function assertU8(value: number, label: string): number {
1354
- if (!Number.isInteger(value) || value < 0 || value > 0xff) {
1355
- throw new Error(`${label} must be an integer between 0 and 255`);
1356
- }
1357
- return value;
1358
- }
1359
-
1360
- function assertStatusFlags(value: number): number {
1361
- const flags = assertU8(value, 'statusFlags');
1362
- if ((flags & ~CLOB_STATUS_FLAG_MASK) !== 0) {
1363
- throw new Error("statusFlags may only contain bits in mask 0x" + CLOB_STATUS_FLAG_MASK.toString(16));
1364
- }
1365
- return flags;
1366
- }
1367
-
1368
1297
  export function effectiveClobStatusFlags(
1369
1298
  exchangeStatusFlags: number,
1370
1299
  market: Pick<ClobMarket, 'statusFlags' | 'exchangeStatusFlags'>
@@ -1374,13 +1303,6 @@ export function effectiveClobStatusFlags(
1374
1303
  assertStatusFlags(market.exchangeStatusFlags);
1375
1304
  }
1376
1305
 
1377
- function assertU16(value: number, label: string): number {
1378
- if (!Number.isInteger(value) || value < 0 || value > 0xffff) {
1379
- throw new Error(`${label} must be an integer between 0 and 65535`);
1380
- }
1381
- return value;
1382
- }
1383
-
1384
1306
  export function assertClobActiveSeatIndex(value: number, label = 'seatIndex'): number {
1385
1307
  if (!Number.isSafeInteger(value) || value < 2 || value >= CLOB_NULL_INDEX) {
1386
1308
  throw new Error(`${label} must be an integer between 2 and ${CLOB_NULL_INDEX - 1}`);
@@ -1388,68 +1310,12 @@ export function assertClobActiveSeatIndex(value: number, label = 'seatIndex'): n
1388
1310
  return value;
1389
1311
  }
1390
1312
 
1391
- function assertU64(value: bigint, label: string): void {
1392
- if (value < 0n || value > 0xffffffffffffffffn) {
1393
- throw new Error(`${label} must be between 0 and 18446744073709551615`);
1394
- }
1395
- }
1396
-
1397
- function assertClientId(value: Uint8Array): Uint8Array {
1398
- if (value.length !== CLOB_CLIENT_ID_SIZE) {
1399
- throw new Error(`clientId must be ${CLOB_CLIENT_ID_SIZE} bytes`);
1400
- }
1401
- return value;
1402
- }
1403
-
1404
- function bitField(value: bigint, begin: bigint, end: bigint): number {
1405
- const mask = (1n << (end - begin)) - 1n;
1406
- return Number((value >> begin) & mask);
1407
- }
1408
-
1409
- function eventSideFromValue(value: number): ClobOrderSide {
1410
- if (value === CLOB_EVENT_SIDE_BUY) return 'buy';
1411
- if (value === CLOB_EVENT_SIDE_SELL) return 'sell';
1412
- throw new Error(`unknown CLOB event side: ${value}`);
1413
- }
1414
-
1415
1313
  function tokenSideFromValue(value: number): ClobTokenSide {
1416
1314
  if (value === CLOB_EVENT_TOKEN_SIDE_BASE) return 'base';
1417
1315
  if (value === CLOB_EVENT_TOKEN_SIDE_QUOTE) return 'quote';
1418
1316
  throw new Error(`unknown CLOB token side: ${value}`);
1419
1317
  }
1420
1318
 
1421
- function removalReasonFromValue(value: number): ClobOrderRemovalReason {
1422
- switch (value) {
1423
- case CLOB_EVENT_REMOVE_REASON_FILLED: return 'filled';
1424
- case CLOB_EVENT_REMOVE_REASON_EXPIRED: return 'expired';
1425
- case CLOB_EVENT_REMOVE_REASON_EVICTED: return 'evicted';
1426
- case CLOB_EVENT_REMOVE_REASON_USER: return 'user';
1427
- default: throw new Error(`unknown CLOB removal reason: ${value}`);
1428
- }
1429
- }
1430
-
1431
- function orderTypeFromValue(value: number): ClobOrderType {
1432
- switch (value) {
1433
- case CLOB_ORDER_TYPE_GTC: return 'gtc';
1434
- case CLOB_ORDER_TYPE_MTL: return 'mtl';
1435
- case CLOB_ORDER_TYPE_ALO: return 'alo';
1436
- case CLOB_ORDER_TYPE_IOC: return 'ioc';
1437
- case CLOB_ORDER_TYPE_FOK: return 'fok';
1438
- default: throw new Error(`unknown CLOB order type: ${value}`);
1439
- }
1440
- }
1441
-
1442
- function pubkeyViewToAddress(pubkey: unknown): string {
1443
- return encodeAddress(bytesFromView(pubkey));
1444
- }
1445
-
1446
- function bytesFromView(value: unknown): Uint8Array {
1447
- const buffer = (value as { buffer?: Uint8Array }).buffer;
1448
- if (buffer instanceof Uint8Array) return new Uint8Array(buffer);
1449
- if (value instanceof Pubkey) return value.toBytes();
1450
- throw new Error('generated view did not expose a byte buffer');
1451
- }
1452
-
1453
1319
  export * from './exchange';
1454
1320
  export * from './exchange-client';
1455
1321
  export * from './price';