@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/src/utils/helpers.ts
CHANGED
|
@@ -78,7 +78,7 @@ export function accountData(
|
|
|
78
78
|
export function accountIndex(
|
|
79
79
|
context: AccountLookupContext,
|
|
80
80
|
account: Uint8Array,
|
|
81
|
-
label
|
|
81
|
+
label = 'account'
|
|
82
82
|
): number {
|
|
83
83
|
const index = context.getAccountIndex(exactBytes(account, PUBKEY_LENGTH, label));
|
|
84
84
|
assertU16(index, `${label} index`);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { Pubkey } from '@thru/sdk';
|
|
3
|
+
import {
|
|
4
|
+
NULL_ACCOUNT_INDEX,
|
|
5
|
+
ORDER_FLAG_BUY,
|
|
6
|
+
ORDER_FLAG_HAS_CLIENT_ID,
|
|
7
|
+
assertStatusFlags,
|
|
8
|
+
bitField,
|
|
9
|
+
buildModifyOrderMetadata,
|
|
10
|
+
bytesFromView,
|
|
11
|
+
eventSideFromValue,
|
|
12
|
+
groupByPrice,
|
|
13
|
+
optionalAccountIndex,
|
|
14
|
+
orderFlags,
|
|
15
|
+
orderTypeFromValue,
|
|
16
|
+
orderTypeValue,
|
|
17
|
+
pubkeyViewToAddress,
|
|
18
|
+
removalReasonFromValue,
|
|
19
|
+
} from './orderbook';
|
|
20
|
+
|
|
21
|
+
describe('shared order-book helpers', () => {
|
|
22
|
+
it('encodes order flags the way the CLOB and perp programs read them', () => {
|
|
23
|
+
expect(orderFlags({ side: 'sell' })).toBe(0);
|
|
24
|
+
expect(orderFlags({ side: 'buy' })).toBe(ORDER_FLAG_BUY);
|
|
25
|
+
expect(orderFlags({ side: 'buy', orderType: 'ioc' })).toBe(ORDER_FLAG_BUY | (3 << 1));
|
|
26
|
+
expect(orderFlags({ side: 'sell', orderType: 'alo', discardAfterMatch: true, failIfOutsideBook: true })).toBe((2 << 1) | (1 << 4) | (1 << 5));
|
|
27
|
+
expect(orderFlags({ side: 'sell', clientId: new Uint8Array(16) })).toBe(ORDER_FLAG_HAS_CLIENT_ID);
|
|
28
|
+
for (const type of ['gtc', 'mtl', 'alo', 'ioc', 'fok'] as const) {
|
|
29
|
+
expect(orderTypeFromValue(orderTypeValue(type))).toBe(type);
|
|
30
|
+
}
|
|
31
|
+
expect(() => orderTypeFromValue(9)).toThrow(/unknown order type 9/);
|
|
32
|
+
expect(eventSideFromValue(0)).toBe('buy');
|
|
33
|
+
expect(eventSideFromValue(1)).toBe('sell');
|
|
34
|
+
expect(() => eventSideFromValue(2)).toThrow(/unknown order side 2/);
|
|
35
|
+
expect([0, 1, 2, 3].map(removalReasonFromValue)).toEqual(['filled', 'expired', 'evicted', 'user']);
|
|
36
|
+
expect(() => removalReasonFromValue(4)).toThrow(/removal reason 4/);
|
|
37
|
+
expect(assertStatusFlags(0b1111)).toBe(0b1111);
|
|
38
|
+
expect(() => assertStatusFlags(0b10000)).toThrow(/statusFlags/);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('builds the modify metadata as client id then order id', () => {
|
|
42
|
+
expect(buildModifyOrderMetadata({})).toEqual(new Uint8Array(0));
|
|
43
|
+
const clientId = new Uint8Array(16).fill(7);
|
|
44
|
+
expect(buildModifyOrderMetadata({ clientId })).toEqual(clientId);
|
|
45
|
+
const both = buildModifyOrderMetadata({ clientId, orderId: 0x0102n });
|
|
46
|
+
expect(both.length).toBe(24);
|
|
47
|
+
expect([...both.subarray(0, 16)]).toEqual([...clientId]);
|
|
48
|
+
expect([...both.subarray(16)]).toEqual([2, 1, 0, 0, 0, 0, 0, 0]);
|
|
49
|
+
expect(() => buildModifyOrderMetadata({ clientId: new Uint8Array(3) })).toThrow(/clientId/);
|
|
50
|
+
expect(() => buildModifyOrderMetadata({ orderId: -1n })).toThrow(/orderId/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('resolves optional accounts to 0xffff when absent', () => {
|
|
54
|
+
const key = new Uint8Array(32).fill(1);
|
|
55
|
+
const context = { getAccountIndex: (pubkey: Uint8Array) => (pubkey[0] === 1 ? 4 : 0) };
|
|
56
|
+
expect(optionalAccountIndex(context, key)).toBe(4);
|
|
57
|
+
expect(optionalAccountIndex(context, undefined)).toBe(NULL_ACCOUNT_INDEX);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('reads packed bit fields and view bytes', () => {
|
|
61
|
+
const packed = 5n | (6n << 20n) | (7n << 40n) | (1n << 60n);
|
|
62
|
+
expect(bitField(packed, 0n, 20n)).toBe(5);
|
|
63
|
+
expect(bitField(packed, 20n, 40n)).toBe(6);
|
|
64
|
+
expect(bitField(packed, 40n, 60n)).toBe(7);
|
|
65
|
+
expect(bitField(packed, 60n, 64n)).toBe(1);
|
|
66
|
+
const bytes = new Uint8Array(32).fill(9);
|
|
67
|
+
expect(bytesFromView({ buffer: bytes })).toEqual(bytes);
|
|
68
|
+
expect(bytesFromView(bytes)).not.toBe(bytes);
|
|
69
|
+
expect(bytesFromView(Pubkey.from(bytes))).toEqual(bytes);
|
|
70
|
+
expect(pubkeyViewToAddress({ buffer: bytes })).toBe(Pubkey.from(bytes).toThruFmt());
|
|
71
|
+
expect(() => bytesFromView({})).toThrow(/byte buffer/);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('groups orders by price, best first, dropping empty ones', () => {
|
|
75
|
+
const order = (priceInTicks: bigint, quantityInLots: bigint, id: number) => ({ priceInTicks, quantityInLots, id });
|
|
76
|
+
const orders = [order(10n, 1n, 1), order(12n, 2n, 2), order(10n, 3n, 3), order(11n, 0n, 4)];
|
|
77
|
+
const bids = groupByPrice(orders, 'buy');
|
|
78
|
+
expect(bids.map((level) => [level.priceInTicks, level.quantityInLots, level.orders.map((o) => o.id)])).toEqual([
|
|
79
|
+
[12n, 2n, [2]],
|
|
80
|
+
[10n, 4n, [1, 3]],
|
|
81
|
+
]);
|
|
82
|
+
expect(bids.every((level) => level.side === 'buy')).toBe(true);
|
|
83
|
+
const asks = groupByPrice(orders, 'sell');
|
|
84
|
+
expect(asks.map((level) => level.priceInTicks)).toEqual([10n, 12n]);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { Pubkey } from '@thru/sdk';
|
|
2
|
+
import { encodeAddress } from '@thru/sdk/helpers';
|
|
3
|
+
import { accountIndex, type AccountLookupContext } from './helpers';
|
|
4
|
+
|
|
5
|
+
/* Order-book primitives shared by the spot CLOB and the perp program, which
|
|
6
|
+
keeps the CLOB's book, instruction flags, event codes and arena entry
|
|
7
|
+
encodings unchanged. Each module re-exports the constants under its own
|
|
8
|
+
prefix (CLOB_*, PERP_*). */
|
|
9
|
+
|
|
10
|
+
export const ORDER_TYPE_GTC = 0;
|
|
11
|
+
export const ORDER_TYPE_MTL = 1;
|
|
12
|
+
export const ORDER_TYPE_ALO = 2;
|
|
13
|
+
export const ORDER_TYPE_IOC = 3;
|
|
14
|
+
export const ORDER_TYPE_FOK = 4;
|
|
15
|
+
|
|
16
|
+
export const ORDER_FLAG_BUY = 1 << 0;
|
|
17
|
+
export const ORDER_FLAG_HAS_CLIENT_ID = 1 << 6;
|
|
18
|
+
|
|
19
|
+
export const EVENT_SIDE_BUY = 0;
|
|
20
|
+
export const EVENT_SIDE_SELL = 1;
|
|
21
|
+
export const EVENT_REMOVE_REASON_FILLED = 0;
|
|
22
|
+
export const EVENT_REMOVE_REASON_EXPIRED = 1;
|
|
23
|
+
export const EVENT_REMOVE_REASON_EVICTED = 2;
|
|
24
|
+
export const EVENT_REMOVE_REASON_USER = 3;
|
|
25
|
+
|
|
26
|
+
export const STATUS_FLAG_PAUSED = 1 << 0;
|
|
27
|
+
export const STATUS_FLAG_POST_ONLY = 1 << 1;
|
|
28
|
+
export const STATUS_FLAG_WITHDRAWALS_FROZEN = 1 << 2;
|
|
29
|
+
export const STATUS_FLAG_DEPOSITS_FROZEN = 1 << 3;
|
|
30
|
+
export const STATUS_FLAG_MASK =
|
|
31
|
+
STATUS_FLAG_PAUSED | STATUS_FLAG_POST_ONLY | STATUS_FLAG_WITHDRAWALS_FROZEN | STATUS_FLAG_DEPOSITS_FROZEN;
|
|
32
|
+
|
|
33
|
+
export const CLIENT_ID_SIZE = 16;
|
|
34
|
+
/** Account index of an absent optional account in a market record. */
|
|
35
|
+
export const NULL_ACCOUNT_INDEX = 0xffff;
|
|
36
|
+
|
|
37
|
+
export type OrderSide = 'buy' | 'sell';
|
|
38
|
+
export type OrderType = 'gtc' | 'mtl' | 'alo' | 'ioc' | 'fok';
|
|
39
|
+
export type OrderRemovalReason = 'filled' | 'expired' | 'evicted' | 'user';
|
|
40
|
+
|
|
41
|
+
export interface OrderBookLevel<Order> {
|
|
42
|
+
side: OrderSide;
|
|
43
|
+
priceInTicks: bigint;
|
|
44
|
+
quantityInLots: bigint;
|
|
45
|
+
orders: Order[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Instruction encoding ******************************************************/
|
|
49
|
+
|
|
50
|
+
export function fixedSeed(value: string): Uint8Array {
|
|
51
|
+
const bytes = new TextEncoder().encode(value);
|
|
52
|
+
if (bytes.length > 32) throw new Error('seed cannot exceed 32 bytes');
|
|
53
|
+
const seed = new Uint8Array(32);
|
|
54
|
+
seed.set(bytes);
|
|
55
|
+
return seed;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function optionalAccountIndex(context: AccountLookupContext, pubkey?: Uint8Array): number {
|
|
59
|
+
return pubkey ? accountIndex(context, pubkey) : NULL_ACCOUNT_INDEX;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function assertU8(value: number, label: string): number {
|
|
63
|
+
if (!Number.isInteger(value) || value < 0 || value > 0xff) {
|
|
64
|
+
throw new Error(`${label} must be an integer between 0 and 255`);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function assertU64(value: bigint, label: string): void {
|
|
70
|
+
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
71
|
+
throw new Error(`${label} must be between 0 and 18446744073709551615`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function assertClientId(value: Uint8Array): Uint8Array {
|
|
76
|
+
if (value.length !== CLIENT_ID_SIZE) throw new Error(`clientId must be ${CLIENT_ID_SIZE} bytes`);
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function assertStatusFlags(value: number): number {
|
|
81
|
+
const flags = assertU8(value, 'statusFlags');
|
|
82
|
+
if ((flags & ~STATUS_FLAG_MASK) !== 0) {
|
|
83
|
+
throw new Error(`statusFlags may only contain bits in mask 0x${STATUS_FLAG_MASK.toString(16)}`);
|
|
84
|
+
}
|
|
85
|
+
return flags;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function orderFlags(args: {
|
|
89
|
+
side: OrderSide;
|
|
90
|
+
orderType?: OrderType;
|
|
91
|
+
clientId?: Uint8Array;
|
|
92
|
+
discardAfterMatch?: boolean;
|
|
93
|
+
failIfOutsideBook?: boolean;
|
|
94
|
+
}): number {
|
|
95
|
+
let flags = args.side === 'buy' ? ORDER_FLAG_BUY : 0;
|
|
96
|
+
flags |= orderTypeValue(args.orderType ?? 'gtc') << 1;
|
|
97
|
+
if (args.discardAfterMatch) flags |= 1 << 4;
|
|
98
|
+
if (args.failIfOutsideBook) flags |= 1 << 5;
|
|
99
|
+
if (args.clientId) flags |= ORDER_FLAG_HAS_CLIENT_ID;
|
|
100
|
+
return flags;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function orderTypeValue(orderType: OrderType): number {
|
|
104
|
+
switch (orderType) {
|
|
105
|
+
case 'gtc': return ORDER_TYPE_GTC;
|
|
106
|
+
case 'mtl': return ORDER_TYPE_MTL;
|
|
107
|
+
case 'alo': return ORDER_TYPE_ALO;
|
|
108
|
+
case 'ioc': return ORDER_TYPE_IOC;
|
|
109
|
+
case 'fok': return ORDER_TYPE_FOK;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Optional client id then optional order id, the modify_order_entry metadata. */
|
|
114
|
+
export function buildModifyOrderMetadata(args: { clientId?: Uint8Array; orderId?: bigint }): Uint8Array {
|
|
115
|
+
const metadata = new Uint8Array((args.clientId ? CLIENT_ID_SIZE : 0) + (args.orderId !== undefined ? 8 : 0));
|
|
116
|
+
let offset = 0;
|
|
117
|
+
if (args.clientId) {
|
|
118
|
+
metadata.set(assertClientId(args.clientId), offset);
|
|
119
|
+
offset += CLIENT_ID_SIZE;
|
|
120
|
+
}
|
|
121
|
+
if (args.orderId !== undefined) {
|
|
122
|
+
assertU64(args.orderId, 'orderId');
|
|
123
|
+
new DataView(metadata.buffer).setBigUint64(offset, args.orderId, true);
|
|
124
|
+
}
|
|
125
|
+
return metadata;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* Decoding *****************************************************************/
|
|
129
|
+
|
|
130
|
+
export function orderTypeFromValue(value: number): OrderType {
|
|
131
|
+
switch (value) {
|
|
132
|
+
case ORDER_TYPE_GTC: return 'gtc';
|
|
133
|
+
case ORDER_TYPE_MTL: return 'mtl';
|
|
134
|
+
case ORDER_TYPE_ALO: return 'alo';
|
|
135
|
+
case ORDER_TYPE_IOC: return 'ioc';
|
|
136
|
+
case ORDER_TYPE_FOK: return 'fok';
|
|
137
|
+
default: throw new Error(`unknown order type ${value}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function eventSideFromValue(value: number): OrderSide {
|
|
142
|
+
if (value === EVENT_SIDE_BUY) return 'buy';
|
|
143
|
+
if (value === EVENT_SIDE_SELL) return 'sell';
|
|
144
|
+
throw new Error(`unknown order side ${value}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function removalReasonFromValue(value: number): OrderRemovalReason {
|
|
148
|
+
switch (value) {
|
|
149
|
+
case EVENT_REMOVE_REASON_FILLED: return 'filled';
|
|
150
|
+
case EVENT_REMOVE_REASON_EXPIRED: return 'expired';
|
|
151
|
+
case EVENT_REMOVE_REASON_EVICTED: return 'evicted';
|
|
152
|
+
case EVENT_REMOVE_REASON_USER: return 'user';
|
|
153
|
+
default: throw new Error(`unknown order removal reason ${value}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Bits [begin, end) of a packed u64 as a number. */
|
|
158
|
+
export function bitField(value: bigint, begin: bigint, end: bigint): number {
|
|
159
|
+
const mask = (1n << (end - begin)) - 1n;
|
|
160
|
+
return Number((value >> begin) & mask);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A copy of the bytes behind a generated ABI view (or a Pubkey). */
|
|
164
|
+
export function bytesFromView(value: unknown): Uint8Array {
|
|
165
|
+
if (value instanceof Uint8Array) return new Uint8Array(value);
|
|
166
|
+
const buffer = (value as { buffer?: Uint8Array }).buffer;
|
|
167
|
+
if (buffer instanceof Uint8Array) return new Uint8Array(buffer);
|
|
168
|
+
if (value instanceof Pubkey) return value.toBytes();
|
|
169
|
+
throw new Error('generated view did not expose a byte buffer');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function pubkeyViewToAddress(value: unknown): string {
|
|
173
|
+
return encodeAddress(bytesFromView(value));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function cbookLevelFromView(
|
|
177
|
+
view: { get_head_entry_idx(): number; get_tail_entry_idx(): number },
|
|
178
|
+
levelIndex: number,
|
|
179
|
+
): { levelIndex: number; headOrderEntryIndex: number; tailOrderEntryIndex: number } {
|
|
180
|
+
return {
|
|
181
|
+
levelIndex,
|
|
182
|
+
headOrderEntryIndex: view.get_head_entry_idx(),
|
|
183
|
+
tailOrderEntryIndex: view.get_tail_entry_idx(),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Price levels of one side, best first (highest bid, lowest ask); orders
|
|
188
|
+
with zero quantity are dropped. */
|
|
189
|
+
export function groupByPrice<Order extends { priceInTicks: bigint; quantityInLots: bigint }>(
|
|
190
|
+
orders: Order[],
|
|
191
|
+
side: OrderSide,
|
|
192
|
+
): OrderBookLevel<Order>[] {
|
|
193
|
+
const levels = new Map<bigint, OrderBookLevel<Order>>();
|
|
194
|
+
for (const order of orders) {
|
|
195
|
+
if (order.quantityInLots === 0n) continue;
|
|
196
|
+
const existing = levels.get(order.priceInTicks);
|
|
197
|
+
if (existing) {
|
|
198
|
+
existing.quantityInLots += order.quantityInLots;
|
|
199
|
+
existing.orders.push(order);
|
|
200
|
+
} else {
|
|
201
|
+
levels.set(order.priceInTicks, {
|
|
202
|
+
side,
|
|
203
|
+
priceInTicks: order.priceInTicks,
|
|
204
|
+
quantityInLots: order.quantityInLots,
|
|
205
|
+
orders: [order],
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return [...levels.values()].sort((a, b) => {
|
|
210
|
+
if (a.priceInTicks === b.priceInTicks) return 0;
|
|
211
|
+
const asc = a.priceInTicks < b.priceInTicks ? -1 : 1;
|
|
212
|
+
return side === 'buy' ? -asc : asc;
|
|
213
|
+
});
|
|
214
|
+
}
|
package/tsup.config.ts
CHANGED