@thru/programs 0.3.17 → 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.
- package/dist/abi-manager/index.cjs +1 -1
- package/dist/abi-manager/index.cjs.map +1 -1
- package/dist/abi-manager/index.js +2 -2
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.js +3 -3
- package/dist/bootstrap-addresses/index.cjs.map +1 -1
- package/dist/bootstrap-addresses/index.js +3 -3
- package/dist/chunk-4F54VFIH.js +58 -0
- package/dist/chunk-4F54VFIH.js.map +1 -0
- package/dist/{chunk-7DRJVAYI.js → chunk-A44ZPXY6.js} +5 -58
- package/dist/chunk-A44ZPXY6.js.map +1 -0
- package/dist/{chunk-VNTPD4AR.js → chunk-FHYZQ5R3.js} +3 -3
- package/dist/{chunk-VNTPD4AR.js.map → chunk-FHYZQ5R3.js.map} +1 -1
- package/dist/{chunk-BNYDH4ZP.js → chunk-FQDO6J6E.js} +3 -3
- package/dist/{chunk-BNYDH4ZP.js.map → chunk-FQDO6J6E.js.map} +1 -1
- package/dist/{chunk-ZHF2PH5H.js → chunk-IGZZOZ73.js} +4 -4
- package/dist/{chunk-ZHF2PH5H.js.map → chunk-IGZZOZ73.js.map} +1 -1
- package/dist/{chunk-CYG4PB65.js → chunk-NMPO53TW.js} +4 -4
- package/dist/{chunk-CYG4PB65.js.map → chunk-NMPO53TW.js.map} +1 -1
- package/dist/{chunk-7DHFYBRW.js → chunk-O2ONVSHW.js} +3 -3
- package/dist/{chunk-7DHFYBRW.js.map → chunk-O2ONVSHW.js.map} +1 -1
- package/dist/{chunk-YX7BLFQZ.js → chunk-PRU4KGQO.js} +3 -3
- package/dist/chunk-PRU4KGQO.js.map +1 -0
- package/dist/chunk-Q6762F4G.js +174 -0
- package/dist/chunk-Q6762F4G.js.map +1 -0
- package/dist/{chunk-6IC6CO5S.js → chunk-UPB725FV.js} +3 -3
- package/dist/{chunk-6IC6CO5S.js.map → chunk-UPB725FV.js.map} +1 -1
- package/dist/clob/index.cjs +328 -304
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +4 -3
- package/dist/clob/index.d.ts +4 -3
- package/dist/clob/index.js +37 -198
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +1 -1
- package/dist/deploy/index.cjs.map +1 -1
- package/dist/deploy/index.js +7 -7
- package/dist/manager/index.cjs +1 -1
- package/dist/manager/index.cjs.map +1 -1
- package/dist/manager/index.d.cts +2 -79
- package/dist/manager/index.d.ts +2 -79
- package/dist/manager/index.js +3 -3
- package/dist/multicall/index.cjs.map +1 -1
- package/dist/multicall/index.js +4 -4
- package/dist/oracle/index.cjs.map +1 -1
- package/dist/oracle/index.d.cts +3 -39
- package/dist/oracle/index.d.ts +3 -39
- package/dist/oracle/index.js +3 -3
- package/dist/orderbook-CEYM4N_S.d.cts +5 -0
- package/dist/orderbook-CEYM4N_S.d.ts +5 -0
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.js +3 -3
- package/dist/perp/index.cjs +16740 -0
- package/dist/perp/index.cjs.map +1 -0
- package/dist/perp/index.d.cts +4577 -0
- package/dist/perp/index.d.ts +4577 -0
- package/dist/perp/index.js +16245 -0
- package/dist/perp/index.js.map +1 -0
- package/dist/token/index.js +2 -1
- package/dist/types-5XmDprcB.d.ts +81 -0
- package/dist/types-Cpi7Ibs2.d.cts +81 -0
- package/dist/types-T4cL02K7.d.cts +39 -0
- package/dist/types-T4cL02K7.d.ts +39 -0
- package/dist/uploader/index.cjs +1 -1
- package/dist/uploader/index.cjs.map +1 -1
- package/dist/uploader/index.js +2 -2
- package/package.json +7 -2
- package/src/clob/index.ts +67 -201
- package/src/perp/abi/thru/common/primitives/types.ts +2428 -0
- package/src/perp/abi/thru/program/perp/types.ts +16813 -0
- package/src/perp/index.test.ts +822 -0
- package/src/perp/index.ts +1793 -0
- package/src/utils/helpers.ts +1 -1
- package/src/utils/orderbook.test.ts +86 -0
- package/src/utils/orderbook.ts +214 -0
- package/tsup.config.ts +1 -0
- package/dist/chunk-7DRJVAYI.js.map +0 -1
- package/dist/chunk-YX7BLFQZ.js.map +0 -1
package/dist/uploader/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { CreateArgs, CreateArgsBuilder, DestroyArgs, DestroyArgsBuilder, FinalizeArgs, FinalizeArgsBuilder, UPLOADER_DEFAULT_CHUNK_SIZE, UPLOADER_ERRORS, UPLOADER_MAX_CHUNK_SIZE, UPLOADER_META_SIZE, UPLOADER_MIN_CHUNK_SIZE, UPLOADER_PROGRAM_ADDRESS, UPLOADER_PROGRAM_PUBKEY, UPLOADER_STATE_FINALIZED, UPLOADER_STATE_OPEN, UploaderError, UploaderErrorBuilder, UploaderInstruction, UploaderInstructionBuilder, UploaderInstruction_payload_Inner, UploaderProgramMeta, UploaderProgramMetaBuilder, WriteArgs, WriteArgsBuilder, buildUploaderInstructionBytes, createDestroyUploadInstruction, createFinalizeUploadInstruction, createUploadBufferInstruction, createWriteUploadInstruction, deriveUploadAddresses, parseUploaderProgramMeta, uploaderErrorName } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
1
|
+
export { CreateArgs, CreateArgsBuilder, DestroyArgs, DestroyArgsBuilder, FinalizeArgs, FinalizeArgsBuilder, UPLOADER_DEFAULT_CHUNK_SIZE, UPLOADER_ERRORS, UPLOADER_MAX_CHUNK_SIZE, UPLOADER_META_SIZE, UPLOADER_MIN_CHUNK_SIZE, UPLOADER_PROGRAM_ADDRESS, UPLOADER_PROGRAM_PUBKEY, UPLOADER_STATE_FINALIZED, UPLOADER_STATE_OPEN, UploaderError, UploaderErrorBuilder, UploaderInstruction, UploaderInstructionBuilder, UploaderInstruction_payload_Inner, UploaderProgramMeta, UploaderProgramMetaBuilder, WriteArgs, WriteArgsBuilder, buildUploaderInstructionBytes, createDestroyUploadInstruction, createFinalizeUploadInstruction, createUploadBufferInstruction, createWriteUploadInstruction, deriveUploadAddresses, parseUploaderProgramMeta, uploaderErrorName } from '../chunk-FQDO6J6E.js';
|
|
2
|
+
import '../chunk-PRU4KGQO.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thru/programs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.18",
|
|
4
4
|
"description": "Typed TypeScript clients for Thru's core on-chain programs: token, AMM, CLOB, oracle, multicall, passkey manager, and others.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://thru.org/docs/sdks/web-packages/programs/",
|
|
@@ -81,10 +81,15 @@
|
|
|
81
81
|
"types": "./dist/deploy/index.d.ts",
|
|
82
82
|
"import": "./dist/deploy/index.js",
|
|
83
83
|
"require": "./dist/deploy/index.cjs"
|
|
84
|
+
},
|
|
85
|
+
"./perp": {
|
|
86
|
+
"types": "./dist/perp/index.d.ts",
|
|
87
|
+
"import": "./dist/perp/index.js",
|
|
88
|
+
"require": "./dist/perp/index.cjs"
|
|
84
89
|
}
|
|
85
90
|
},
|
|
86
91
|
"dependencies": {
|
|
87
|
-
"@thru/sdk": "0.3.
|
|
92
|
+
"@thru/sdk": "0.3.18"
|
|
88
93
|
},
|
|
89
94
|
"devDependencies": {
|
|
90
95
|
"@types/node": "^25.9.1",
|
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
|
-
|
|
143
|
-
export const
|
|
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 =
|
|
147
|
-
export const CLOB_EVENT_REMOVE_REASON_EXPIRED =
|
|
148
|
-
export const CLOB_EVENT_REMOVE_REASON_EVICTED =
|
|
149
|
-
export const CLOB_EVENT_REMOVE_REASON_USER =
|
|
150
|
-
|
|
151
|
-
export const CLOB_STATUS_FLAG_PAUSED =
|
|
152
|
-
export const CLOB_STATUS_FLAG_POST_ONLY =
|
|
153
|
-
export const CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN =
|
|
154
|
-
export const CLOB_STATUS_FLAG_DEPOSITS_FROZEN =
|
|
155
|
-
export const CLOB_STATUS_FLAG_MASK =
|
|
156
|
-
|
|
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 =
|
|
164
|
-
export const CLOB_ORDER_TYPE_MTL =
|
|
165
|
-
export const CLOB_ORDER_TYPE_ALO =
|
|
166
|
-
export const CLOB_ORDER_TYPE_IOC =
|
|
167
|
-
export const CLOB_ORDER_TYPE_FOK =
|
|
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 =
|
|
170
|
-
export const CLOB_ORDER_FLAG_HAS_CLIENT_ID =
|
|
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 =
|
|
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 =
|
|
212
|
-
export type ClobOrderType =
|
|
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 =
|
|
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';
|