@thru/programs 0.2.37 → 0.2.39
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/clob/index.cjs +11769 -0
- package/dist/clob/index.cjs.map +1 -0
- package/dist/clob/index.d.cts +2962 -0
- package/dist/clob/index.d.ts +2962 -0
- package/dist/clob/index.js +11648 -0
- package/dist/clob/index.js.map +1 -0
- package/dist/passkey-manager/index.cjs +48 -0
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.d.cts +27 -1
- package/dist/passkey-manager/index.d.ts +27 -1
- package/dist/passkey-manager/index.js +48 -2
- package/dist/passkey-manager/index.js.map +1 -1
- package/package.json +8 -2
- package/src/clob/abi/thru/common/primitives/types.ts +2428 -0
- package/src/clob/abi/thru/program/clob/types.ts +11402 -0
- package/src/clob/index.test.ts +277 -0
- package/src/clob/index.ts +1146 -0
- package/src/passkey-manager/context.test.ts +63 -0
- package/src/passkey-manager/context.ts +84 -1
- package/src/passkey-manager/index.ts +7 -1
- package/src/passkey-manager/types.ts +10 -0
- package/tsconfig.json +2 -1
- package/tsup.config.ts +1 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { encodeAddress } from '@thru/sdk/helpers';
|
|
3
|
+
import {
|
|
4
|
+
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
5
|
+
CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY,
|
|
6
|
+
CLOB_CBOOK_MAX_PRICE_IN_TICKS,
|
|
7
|
+
CLOB_EVENT_SIDE_BUY,
|
|
8
|
+
CLOB_ORDER_FLAG_BUY,
|
|
9
|
+
CLOB_ORDER_FLAG_HAS_CLIENT_ID,
|
|
10
|
+
CLOB_ORDER_TYPE_FOK,
|
|
11
|
+
CLOB_ORDER_TYPE_GTC,
|
|
12
|
+
CLOB_ORDER_TYPE_IOC,
|
|
13
|
+
CLOB_ORDER_TYPE_MTL,
|
|
14
|
+
ClobEventBuilder,
|
|
15
|
+
MarketAccountBuilder,
|
|
16
|
+
MarketCreatedEventBuilder,
|
|
17
|
+
OrderEntryBuilder,
|
|
18
|
+
OrderFilledEventBuilder,
|
|
19
|
+
SeatEntryBuilder,
|
|
20
|
+
buildOrderBookSnapshot,
|
|
21
|
+
createOrderEntryInstruction,
|
|
22
|
+
createSeatlessOrderEntryInstruction,
|
|
23
|
+
encodeOrderFlags,
|
|
24
|
+
parseClobEvent,
|
|
25
|
+
parseMarketAccount,
|
|
26
|
+
parseOrderArenaAccount,
|
|
27
|
+
parseSeatArenaAccount,
|
|
28
|
+
} from './index';
|
|
29
|
+
|
|
30
|
+
function key(id: number): Uint8Array {
|
|
31
|
+
const bytes = new Uint8Array(32);
|
|
32
|
+
bytes[0] = id;
|
|
33
|
+
return bytes;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function context(indexes: Record<string, number>) {
|
|
37
|
+
return {
|
|
38
|
+
getAccountIndex(pubkey: Uint8Array): number {
|
|
39
|
+
const index = indexes[bytesToHex(pubkey)];
|
|
40
|
+
if (index === undefined) throw new Error(`missing account ${bytesToHex(pubkey)}`);
|
|
41
|
+
return index;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('clob helpers', () => {
|
|
47
|
+
it('encodes order flags with C program bit semantics', () => {
|
|
48
|
+
expect(encodeOrderFlags({ side: 'buy', orderType: 'gtc' })).toBe(
|
|
49
|
+
CLOB_ORDER_FLAG_BUY | (CLOB_ORDER_TYPE_GTC << 1)
|
|
50
|
+
);
|
|
51
|
+
expect(encodeOrderFlags({ side: 'sell', orderType: 'mtl' })).toBe(CLOB_ORDER_TYPE_MTL << 1);
|
|
52
|
+
expect(encodeOrderFlags({ side: 'buy', orderType: 'fok', clientId: new Uint8Array(16) })).toBe(
|
|
53
|
+
CLOB_ORDER_FLAG_BUY | (CLOB_ORDER_TYPE_FOK << 1) | CLOB_ORDER_FLAG_HAS_CLIENT_ID
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('packs create order entry instructions for the CLOB envelope', async () => {
|
|
58
|
+
const clientId = new Uint8Array(16).fill(0xab);
|
|
59
|
+
const instruction = await createOrderEntryInstruction({
|
|
60
|
+
marketRecordIndex: 2,
|
|
61
|
+
side: 'buy',
|
|
62
|
+
orderType: 'gtc',
|
|
63
|
+
price: 123n,
|
|
64
|
+
quantity: 456n,
|
|
65
|
+
expirationTime: 789n,
|
|
66
|
+
clientId,
|
|
67
|
+
})(context({}));
|
|
68
|
+
|
|
69
|
+
expect(instruction[0]).toBe(CLOB_INSTRUCTION_CREATE_ORDER_ENTRY);
|
|
70
|
+
expect(instruction[1]).toBe(CLOB_ORDER_FLAG_BUY | CLOB_ORDER_FLAG_HAS_CLIENT_ID);
|
|
71
|
+
expect(instruction[2]).toBe(2);
|
|
72
|
+
expect(new DataView(instruction.buffer).getBigUint64(8, true)).toBe(123n);
|
|
73
|
+
expect(new DataView(instruction.buffer).getBigUint64(16, true)).toBe(456n);
|
|
74
|
+
expect(new DataView(instruction.buffer).getBigInt64(24, true)).toBe(789n);
|
|
75
|
+
expect(instruction.slice(32)).toEqual(clientId);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('packs seatless order instructions with wallet account indexes', async () => {
|
|
79
|
+
const quote = key(1);
|
|
80
|
+
const base = key(2);
|
|
81
|
+
const instruction = await createSeatlessOrderEntryInstruction({
|
|
82
|
+
marketRecordIndex: 3,
|
|
83
|
+
side: 'sell',
|
|
84
|
+
orderType: 'ioc',
|
|
85
|
+
price: 10n,
|
|
86
|
+
quantity: 20n,
|
|
87
|
+
quoteWalletBytes: quote,
|
|
88
|
+
baseWalletBytes: base,
|
|
89
|
+
})(context({
|
|
90
|
+
[bytesToHex(quote)]: 5,
|
|
91
|
+
[bytesToHex(base)]: 6,
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
expect(instruction[0]).toBe(CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY);
|
|
95
|
+
expect(instruction[1]).toBe(CLOB_ORDER_TYPE_IOC << 1);
|
|
96
|
+
expect(instruction[2]).toBe(3);
|
|
97
|
+
expect(new DataView(instruction.buffer).getUint16(24, true)).toBe(5);
|
|
98
|
+
expect(new DataView(instruction.buffer).getUint16(26, true)).toBe(6);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('parses market accounts into frontend-oriented addresses', () => {
|
|
102
|
+
const data = new MarketAccountBuilder()
|
|
103
|
+
.set_magic(0xc1)
|
|
104
|
+
.set_status_flags(3)
|
|
105
|
+
.set_reserved0([0, 0, 0, 0, 0, 0])
|
|
106
|
+
.set_lot_size(100n)
|
|
107
|
+
.set_tick_size(5n)
|
|
108
|
+
.set_next_order_id(42n)
|
|
109
|
+
.set_order_entry_pubkey(key(10))
|
|
110
|
+
.set_bids_cbook_pubkey(key(11))
|
|
111
|
+
.set_asks_cbook_pubkey(key(12))
|
|
112
|
+
.set_token_program_pubkey(key(16))
|
|
113
|
+
.set_base_vault_pubkey(key(13))
|
|
114
|
+
.set_quote_vault_pubkey(key(14))
|
|
115
|
+
.set_market_authority_pubkey(key(15))
|
|
116
|
+
.build();
|
|
117
|
+
|
|
118
|
+
const market = parseMarketAccount(data);
|
|
119
|
+
expect(market.magic).toBe(0xc1);
|
|
120
|
+
expect(market.statusFlags).toBe(3);
|
|
121
|
+
expect(market.lotSize).toBe(100n);
|
|
122
|
+
expect(market.tickSize).toBe(5n);
|
|
123
|
+
expect(market.nextOrderId).toBe(42n);
|
|
124
|
+
expect(market.bidsCbook).not.toBe(market.asksCbook);
|
|
125
|
+
expect(market.tokenProgram).toBe(bytesToAddress(key(16)));
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('parses order arenas and groups active orderbook depth', () => {
|
|
129
|
+
const data = new Uint8Array(72 + 3 * 64);
|
|
130
|
+
new DataView(data.buffer).setUint32(60, 2, true);
|
|
131
|
+
new OrderEntryBuilder()
|
|
132
|
+
.set_seat_ptrs(2n << 40n)
|
|
133
|
+
.set_level_ptrs(7n << 40n)
|
|
134
|
+
.set_qty_in_lots(10n)
|
|
135
|
+
.set_order_id(100n)
|
|
136
|
+
.set_reserved0([0, 0, 0, 0, 0, 0, 0, 0])
|
|
137
|
+
.set_client_id(new Uint8Array(16).fill(1))
|
|
138
|
+
.set_expiry(99n)
|
|
139
|
+
.buildInto(data, 72);
|
|
140
|
+
new OrderEntryBuilder()
|
|
141
|
+
.set_seat_ptrs(9n << 40n)
|
|
142
|
+
.set_level_ptrs(7n << 40n)
|
|
143
|
+
.set_qty_in_lots(999n)
|
|
144
|
+
.set_order_id(999n)
|
|
145
|
+
.set_reserved0([0, 0, 0, 0, 0, 0, 0, 0])
|
|
146
|
+
.set_client_id(new Uint8Array(16).fill(9))
|
|
147
|
+
.set_expiry(0n)
|
|
148
|
+
.buildInto(data, 136);
|
|
149
|
+
new OrderEntryBuilder()
|
|
150
|
+
.set_seat_ptrs(3n << 40n)
|
|
151
|
+
.set_level_ptrs(7n << 40n)
|
|
152
|
+
.set_qty_in_lots(15n)
|
|
153
|
+
.set_order_id(101n)
|
|
154
|
+
.set_reserved0([0, 0, 0, 0, 0, 0, 0, 0])
|
|
155
|
+
.set_client_id(new Uint8Array(16).fill(2))
|
|
156
|
+
.set_expiry(100n)
|
|
157
|
+
.buildInto(data, 200);
|
|
158
|
+
|
|
159
|
+
const arena = parseOrderArenaAccount(data);
|
|
160
|
+
expect(arena.slots).toHaveLength(3);
|
|
161
|
+
expect(arena.slots.map((slot) => slot.active)).toEqual([true, false, true]);
|
|
162
|
+
expect(arena.orders).toHaveLength(2);
|
|
163
|
+
expect(arena.orders[0].seatIndex).toBe(2);
|
|
164
|
+
|
|
165
|
+
const snapshot = buildOrderBookSnapshot({
|
|
166
|
+
bids: arena.orders,
|
|
167
|
+
asks: [],
|
|
168
|
+
bidsCbook: {
|
|
169
|
+
bestLevelIndex: 5,
|
|
170
|
+
bestPriceInTicks: CLOB_CBOOK_MAX_PRICE_IN_TICKS - 52n,
|
|
171
|
+
levels: Array.from({ length: 10 }, (_, levelIndex) => ({
|
|
172
|
+
levelIndex,
|
|
173
|
+
headOrderEntryIndex: 0,
|
|
174
|
+
tailOrderEntryIndex: 0,
|
|
175
|
+
})),
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
expect(snapshot.bestBid?.priceInTicks).toBe(50n);
|
|
179
|
+
expect(snapshot.bestBid?.quantityInLots).toBe(25n);
|
|
180
|
+
expect(snapshot.bestAsk).toBeNull();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('parses seat arenas without treating the reserved seatless slot as a user seat', () => {
|
|
184
|
+
const data = new Uint8Array(320 + 3 * 64);
|
|
185
|
+
new SeatEntryBuilder()
|
|
186
|
+
.set_seat_authority_pubkey(key(1))
|
|
187
|
+
.set_quantity_base(111n)
|
|
188
|
+
.set_quantity_quote(222n)
|
|
189
|
+
.set_head_order_entry_idx(0)
|
|
190
|
+
.set_reserved0(0)
|
|
191
|
+
.set_non_nullable_reserved(1n)
|
|
192
|
+
.buildInto(data, 320);
|
|
193
|
+
new SeatEntryBuilder()
|
|
194
|
+
.set_seat_authority_pubkey(key(2))
|
|
195
|
+
.set_quantity_base(333n)
|
|
196
|
+
.set_quantity_quote(444n)
|
|
197
|
+
.set_head_order_entry_idx(5)
|
|
198
|
+
.set_reserved0(0)
|
|
199
|
+
.set_non_nullable_reserved(1n)
|
|
200
|
+
.buildInto(data, 384);
|
|
201
|
+
|
|
202
|
+
const arena = parseSeatArenaAccount(data);
|
|
203
|
+
expect(arena.slots).toHaveLength(3);
|
|
204
|
+
expect(arena.slots.map((slot) => slot.active)).toEqual([true, true, false]);
|
|
205
|
+
expect(arena.seatlessSeat?.index).toBe(1);
|
|
206
|
+
expect(arena.seats).toHaveLength(1);
|
|
207
|
+
expect(arena.seats[0].seatIndex).toBe(2);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
it('parses market created events into indexer-friendly fields', () => {
|
|
212
|
+
const payload = new MarketCreatedEventBuilder()
|
|
213
|
+
.set_lot_size(100n)
|
|
214
|
+
.set_tick_size(5n)
|
|
215
|
+
.set_base_mint(key(1))
|
|
216
|
+
.set_quote_mint(key(2))
|
|
217
|
+
.set_market_authority(key(3))
|
|
218
|
+
.set_market(key(4))
|
|
219
|
+
.set_order_arena(key(5))
|
|
220
|
+
.set_bids_cbook(key(6))
|
|
221
|
+
.set_asks_cbook(key(7))
|
|
222
|
+
.set_base_vault(key(8))
|
|
223
|
+
.set_quote_vault(key(9))
|
|
224
|
+
.build();
|
|
225
|
+
const builder = new ClobEventBuilder();
|
|
226
|
+
builder.payload().select('market_created').writePayload(payload).finish();
|
|
227
|
+
|
|
228
|
+
const event = parseClobEvent(builder.build());
|
|
229
|
+
expect(event.variant).toBe('market_created');
|
|
230
|
+
if (event.variant !== 'market_created') throw new Error('expected market_created');
|
|
231
|
+
expect(event.lotSize).toBe(100n);
|
|
232
|
+
expect(event.tickSize).toBe(5n);
|
|
233
|
+
expect(event.baseMint).toBe(bytesToAddress(key(1)));
|
|
234
|
+
expect(event.quoteMint).toBe(bytesToAddress(key(2)));
|
|
235
|
+
expect(event.market).toBe(bytesToAddress(key(4)));
|
|
236
|
+
expect(event.orderArena).toBe(bytesToAddress(key(5)));
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('parses order filled events into trade fields', () => {
|
|
240
|
+
const clientId = new Uint8Array(16).fill(0xcd);
|
|
241
|
+
const payload = new OrderFilledEventBuilder()
|
|
242
|
+
.set_taker_seat_idx(2)
|
|
243
|
+
.set_maker_seat_idx(3)
|
|
244
|
+
.set_taker_side(CLOB_EVENT_SIDE_BUY)
|
|
245
|
+
.set_reserved0([0, 0, 0])
|
|
246
|
+
.set_price(123n)
|
|
247
|
+
.set_qty(456n)
|
|
248
|
+
.set_maker_order_id(789n)
|
|
249
|
+
.set_maker_client_id(clientId)
|
|
250
|
+
.set_market(key(10))
|
|
251
|
+
.set_taker_seat_authority(key(11))
|
|
252
|
+
.set_maker_seat_authority(key(12))
|
|
253
|
+
.build();
|
|
254
|
+
const builder = new ClobEventBuilder();
|
|
255
|
+
builder.payload().select('order_filled').writePayload(payload).finish();
|
|
256
|
+
|
|
257
|
+
const event = parseClobEvent(builder.build());
|
|
258
|
+
expect(event.variant).toBe('order_filled');
|
|
259
|
+
if (event.variant !== 'order_filled') throw new Error('expected order_filled');
|
|
260
|
+
expect(event.takerSeatIndex).toBe(2);
|
|
261
|
+
expect(event.makerSeatIndex).toBe(3);
|
|
262
|
+
expect(event.takerSide).toBe('buy');
|
|
263
|
+
expect(event.price).toBe(123n);
|
|
264
|
+
expect(event.quantity).toBe(456n);
|
|
265
|
+
expect(event.makerOrderId).toBe(789n);
|
|
266
|
+
expect(event.makerClientId).toEqual(clientId);
|
|
267
|
+
expect(event.market).toBe(bytesToAddress(key(10)));
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
272
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function bytesToAddress(bytes: Uint8Array): string {
|
|
276
|
+
return encodeAddress(bytes);
|
|
277
|
+
}
|