@thru/programs 0.2.38 → 0.3.0
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/chunk-J7B7XYOI.js +2714 -0
- package/dist/chunk-J7B7XYOI.js.map +1 -0
- package/dist/clob/index.cjs +15089 -0
- package/dist/clob/index.cjs.map +1 -0
- package/dist/clob/index.d.cts +3103 -0
- package/dist/clob/index.d.ts +3103 -0
- package/dist/clob/index.js +12246 -0
- package/dist/clob/index.js.map +1 -0
- package/dist/multicall/index.js +1 -2712
- package/dist/multicall/index.js.map +1 -1
- 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/exchange.test.ts +901 -0
- package/src/clob/exchange.ts +939 -0
- package/src/clob/index.test.ts +309 -0
- package/src/clob/index.ts +1159 -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,901 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { encodeAddress } from '@thru/sdk/helpers';
|
|
3
|
+
import { MULTICALL_PROGRAM_ADDRESS, MulticallArgs } from '../multicall';
|
|
4
|
+
import {
|
|
5
|
+
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
6
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
7
|
+
CLOB_INSTRUCTION_SEAT_CREATE,
|
|
8
|
+
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
9
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
10
|
+
CLOB_MAX_ATOMIC_FUNDING_SOURCES,
|
|
11
|
+
CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
12
|
+
ClobExchangeError,
|
|
13
|
+
aggregateClobExchangeBalances,
|
|
14
|
+
buildClobExchangeDepositInstructionPlan,
|
|
15
|
+
buildClobExchangeWithdrawalInstructionPlan,
|
|
16
|
+
buildClobFundedOrderInstructionPlan,
|
|
17
|
+
calculateClobOrderFundingAmount,
|
|
18
|
+
planClobOrderFunding,
|
|
19
|
+
validateClobOrderFundingPlanSeats,
|
|
20
|
+
wrapClobPlanWithTokenAccountInitialization,
|
|
21
|
+
} from './index';
|
|
22
|
+
import type {
|
|
23
|
+
ClobAssetBalanceAllocation,
|
|
24
|
+
ClobInstructionPlan,
|
|
25
|
+
ClobTradingMarket,
|
|
26
|
+
} from './index';
|
|
27
|
+
|
|
28
|
+
const AUTHORITY = address(1);
|
|
29
|
+
const TOKEN_PROGRAM = address(2);
|
|
30
|
+
const BASE_MINT = address(3);
|
|
31
|
+
const QUOTE_MINT = address(4);
|
|
32
|
+
const WALLET_QUOTE = address(5);
|
|
33
|
+
|
|
34
|
+
describe('CLOB exchange balance SDK', () => {
|
|
35
|
+
it('aggregates compatible physical allocations without losing transferability', () => {
|
|
36
|
+
const allocations = [
|
|
37
|
+
allocation({ market: address(10), seatIndex: 2, tokenSide: 'base', mint: QUOTE_MINT, freeAmount: 5n }),
|
|
38
|
+
allocation({
|
|
39
|
+
market: address(11),
|
|
40
|
+
seatIndex: 4,
|
|
41
|
+
tokenSide: 'quote',
|
|
42
|
+
mint: QUOTE_MINT,
|
|
43
|
+
freeAmount: 7n,
|
|
44
|
+
statusFlags: CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
45
|
+
}),
|
|
46
|
+
allocation({
|
|
47
|
+
market: address(12),
|
|
48
|
+
seatIndex: 6,
|
|
49
|
+
tokenSide: 'quote',
|
|
50
|
+
mint: QUOTE_MINT,
|
|
51
|
+
freeAmount: 9n,
|
|
52
|
+
metadataReady: false,
|
|
53
|
+
}),
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
expect(aggregateClobExchangeBalances(allocations)).toEqual([{
|
|
57
|
+
authority: AUTHORITY,
|
|
58
|
+
mint: QUOTE_MINT,
|
|
59
|
+
tokenProgram: TOKEN_PROGRAM,
|
|
60
|
+
freeAmount: 12n,
|
|
61
|
+
transferableAmount: 5n,
|
|
62
|
+
allocations: [allocations[0], allocations[1]],
|
|
63
|
+
}]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('uses another exchange allocation after target funds', () => {
|
|
67
|
+
const target = market(20);
|
|
68
|
+
const source = market(40);
|
|
69
|
+
const plan = planClobOrderFunding({
|
|
70
|
+
authority: AUTHORITY,
|
|
71
|
+
targetMarket: target,
|
|
72
|
+
side: 'buy',
|
|
73
|
+
price: 2n,
|
|
74
|
+
quantity: 200n,
|
|
75
|
+
allocations: [
|
|
76
|
+
allocation({
|
|
77
|
+
market: target.address,
|
|
78
|
+
seatIndex: 2,
|
|
79
|
+
tokenSide: 'quote',
|
|
80
|
+
mint: QUOTE_MINT,
|
|
81
|
+
vault: target.quoteVault,
|
|
82
|
+
freeAmount: 100n,
|
|
83
|
+
}),
|
|
84
|
+
allocation({
|
|
85
|
+
market: source.address,
|
|
86
|
+
seatIndex: 8,
|
|
87
|
+
tokenSide: 'base',
|
|
88
|
+
mint: QUOTE_MINT,
|
|
89
|
+
vault: source.baseVault,
|
|
90
|
+
freeAmount: 350n,
|
|
91
|
+
}),
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(plan.requiredAmount).toBe(400n);
|
|
96
|
+
expect(plan.targetAmount).toBe(100n);
|
|
97
|
+
expect(plan.sources[0]?.amount).toBe(300n);
|
|
98
|
+
expect(plan.sources[0]?.allocation.market).toBe(source.address);
|
|
99
|
+
expect(plan.shortfall).toBe(0n);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('uses secondary target-market seats and validates them from the target read', () => {
|
|
103
|
+
const target = market(20);
|
|
104
|
+
const primary = allocation({
|
|
105
|
+
market: target.address,
|
|
106
|
+
seatIndex: 2,
|
|
107
|
+
tokenSide: 'quote',
|
|
108
|
+
mint: QUOTE_MINT,
|
|
109
|
+
vault: target.quoteVault,
|
|
110
|
+
freeAmount: 60n,
|
|
111
|
+
});
|
|
112
|
+
const secondary = allocation({
|
|
113
|
+
market: target.address,
|
|
114
|
+
seatIndex: 3,
|
|
115
|
+
tokenSide: 'quote',
|
|
116
|
+
mint: QUOTE_MINT,
|
|
117
|
+
vault: target.quoteVault,
|
|
118
|
+
freeAmount: 60n,
|
|
119
|
+
});
|
|
120
|
+
const plan = planClobOrderFunding({
|
|
121
|
+
authority: AUTHORITY,
|
|
122
|
+
targetMarket: target,
|
|
123
|
+
side: 'buy',
|
|
124
|
+
price: 1n,
|
|
125
|
+
quantity: 100n,
|
|
126
|
+
allocations: [primary, secondary],
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(plan.targetSeatIndex).toBe(2);
|
|
130
|
+
expect(plan.targetAmount).toBe(60n);
|
|
131
|
+
expect(plan.sources).toEqual([{ allocation: secondary, amount: 40n }]);
|
|
132
|
+
expect(plan.exchangeAvailableAmount).toBe(120n);
|
|
133
|
+
expect(plan.shortfall).toBe(0n);
|
|
134
|
+
expect(() => validateClobOrderFundingPlanSeats({
|
|
135
|
+
plan,
|
|
136
|
+
targetSeats: [seat(2, 0n, 60n), seat(3, 0n, 60n)],
|
|
137
|
+
})).not.toThrow();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('reports full eligible exchange funds independently of the current order size', () => {
|
|
141
|
+
const target = market(20);
|
|
142
|
+
const source = market(40);
|
|
143
|
+
const plan = planClobOrderFunding({
|
|
144
|
+
authority: AUTHORITY,
|
|
145
|
+
targetMarket: target,
|
|
146
|
+
side: 'buy',
|
|
147
|
+
price: 1n,
|
|
148
|
+
quantity: 100n,
|
|
149
|
+
allocations: [
|
|
150
|
+
allocation({
|
|
151
|
+
market: target.address,
|
|
152
|
+
seatIndex: 2,
|
|
153
|
+
tokenSide: 'quote',
|
|
154
|
+
mint: QUOTE_MINT,
|
|
155
|
+
vault: target.quoteVault,
|
|
156
|
+
freeAmount: 1_000n,
|
|
157
|
+
}),
|
|
158
|
+
allocation({
|
|
159
|
+
market: source.address,
|
|
160
|
+
seatIndex: 8,
|
|
161
|
+
tokenSide: 'base',
|
|
162
|
+
mint: QUOTE_MINT,
|
|
163
|
+
vault: source.baseVault,
|
|
164
|
+
freeAmount: 50n,
|
|
165
|
+
}),
|
|
166
|
+
allocation({
|
|
167
|
+
market: address(60),
|
|
168
|
+
seatIndex: 9,
|
|
169
|
+
mint: QUOTE_MINT,
|
|
170
|
+
freeAmount: 70n,
|
|
171
|
+
statusFlags: CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
172
|
+
}),
|
|
173
|
+
],
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
expect(plan.targetAmount).toBe(100n);
|
|
177
|
+
expect(plan.sources).toEqual([]);
|
|
178
|
+
expect(plan.exchangeAvailableAmount).toBe(1_050n);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('ignores a withdrawal-frozen secondary target-market seat', () => {
|
|
182
|
+
const target = market(20);
|
|
183
|
+
const plan = planClobOrderFunding({
|
|
184
|
+
authority: AUTHORITY,
|
|
185
|
+
targetMarket: target,
|
|
186
|
+
side: 'buy',
|
|
187
|
+
price: 1n,
|
|
188
|
+
quantity: 100n,
|
|
189
|
+
allocations: [
|
|
190
|
+
allocation({
|
|
191
|
+
market: target.address,
|
|
192
|
+
seatIndex: 2,
|
|
193
|
+
tokenSide: 'quote',
|
|
194
|
+
mint: QUOTE_MINT,
|
|
195
|
+
vault: target.quoteVault,
|
|
196
|
+
freeAmount: 60n,
|
|
197
|
+
}),
|
|
198
|
+
allocation({
|
|
199
|
+
market: target.address,
|
|
200
|
+
seatIndex: 3,
|
|
201
|
+
tokenSide: 'quote',
|
|
202
|
+
mint: QUOTE_MINT,
|
|
203
|
+
vault: target.quoteVault,
|
|
204
|
+
freeAmount: 60n,
|
|
205
|
+
statusFlags: CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
206
|
+
}),
|
|
207
|
+
],
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
expect(plan.sources).toEqual([]);
|
|
211
|
+
expect(plan.exchangeAvailableAmount).toBe(60n);
|
|
212
|
+
expect(plan.shortfall).toBe(40n);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('reports the remaining shortfall after internal exchange funds', () => {
|
|
216
|
+
const target = market(20);
|
|
217
|
+
const source = market(40);
|
|
218
|
+
const plan = planClobOrderFunding({
|
|
219
|
+
authority: AUTHORITY,
|
|
220
|
+
targetMarket: target,
|
|
221
|
+
side: 'sell',
|
|
222
|
+
price: 5n,
|
|
223
|
+
quantity: 300n,
|
|
224
|
+
allocations: [
|
|
225
|
+
allocation({
|
|
226
|
+
market: source.address,
|
|
227
|
+
seatIndex: 8,
|
|
228
|
+
tokenSide: 'quote',
|
|
229
|
+
mint: BASE_MINT,
|
|
230
|
+
vault: source.quoteVault,
|
|
231
|
+
freeAmount: 200n,
|
|
232
|
+
}),
|
|
233
|
+
],
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
expect(plan.targetTokenSide).toBe('base');
|
|
237
|
+
expect(plan.sources[0]?.amount).toBe(200n);
|
|
238
|
+
expect(plan.shortfall).toBe(100n);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('stacks compatible allocations in their existing input order', () => {
|
|
242
|
+
const target = market(20);
|
|
243
|
+
const first = allocation({
|
|
244
|
+
market: address(40),
|
|
245
|
+
seatIndex: 8,
|
|
246
|
+
mint: QUOTE_MINT,
|
|
247
|
+
freeAmount: 100n,
|
|
248
|
+
});
|
|
249
|
+
const second = allocation({
|
|
250
|
+
market: address(50),
|
|
251
|
+
seatIndex: 9,
|
|
252
|
+
mint: QUOTE_MINT,
|
|
253
|
+
freeAmount: 500n,
|
|
254
|
+
});
|
|
255
|
+
const plan = planClobOrderFunding({
|
|
256
|
+
authority: AUTHORITY,
|
|
257
|
+
targetMarket: target,
|
|
258
|
+
side: 'buy',
|
|
259
|
+
price: 2n,
|
|
260
|
+
quantity: 200n,
|
|
261
|
+
allocations: [first, second],
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(plan.sources).toEqual([
|
|
265
|
+
{ allocation: first, amount: 100n },
|
|
266
|
+
{ allocation: second, amount: 300n },
|
|
267
|
+
]);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('reports a shortfall after every compatible exchange allocation', () => {
|
|
271
|
+
const target = market(20);
|
|
272
|
+
const first = allocation({ market: address(40), seatIndex: 8, mint: QUOTE_MINT, freeAmount: 100n });
|
|
273
|
+
const second = allocation({ market: address(50), seatIndex: 9, mint: QUOTE_MINT, freeAmount: 150n });
|
|
274
|
+
const plan = planClobOrderFunding({
|
|
275
|
+
authority: AUTHORITY,
|
|
276
|
+
targetMarket: target,
|
|
277
|
+
side: 'buy',
|
|
278
|
+
price: 1n,
|
|
279
|
+
quantity: 300n,
|
|
280
|
+
allocations: [first, second],
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
expect(plan.sources.map((source) => source.amount)).toEqual([100n, 150n]);
|
|
284
|
+
expect(plan.shortfall).toBe(50n);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('rejects funding fragmented beyond the atomic source limit', () => {
|
|
288
|
+
const target = market(20);
|
|
289
|
+
const allocations = Array.from(
|
|
290
|
+
{ length: CLOB_MAX_ATOMIC_FUNDING_SOURCES + 1 },
|
|
291
|
+
(_, index) => allocation({
|
|
292
|
+
market: address(40 + index),
|
|
293
|
+
seatIndex: index + 2,
|
|
294
|
+
mint: QUOTE_MINT,
|
|
295
|
+
freeAmount: 1n,
|
|
296
|
+
})
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
expect(() => planClobOrderFunding({
|
|
300
|
+
authority: AUTHORITY,
|
|
301
|
+
targetMarket: target,
|
|
302
|
+
side: 'buy',
|
|
303
|
+
price: 1n,
|
|
304
|
+
quantity: BigInt(allocations.length),
|
|
305
|
+
allocations,
|
|
306
|
+
})).toThrowError(expect.objectContaining({ code: 'too_many_funding_sources' }));
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('rejects duplicate indexed source allocations', () => {
|
|
310
|
+
const target = market(20);
|
|
311
|
+
const duplicate = allocation({
|
|
312
|
+
market: address(40),
|
|
313
|
+
seatIndex: 8,
|
|
314
|
+
mint: QUOTE_MINT,
|
|
315
|
+
freeAmount: 50n,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
expect(() => planClobOrderFunding({
|
|
319
|
+
authority: AUTHORITY,
|
|
320
|
+
targetMarket: target,
|
|
321
|
+
side: 'buy',
|
|
322
|
+
price: 1n,
|
|
323
|
+
quantity: 100n,
|
|
324
|
+
allocations: [duplicate, { ...duplicate }],
|
|
325
|
+
})).toThrowError(expect.objectContaining({ code: 'funding_plan_mismatch' }));
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('matches currencies by mint and token program rather than token side', () => {
|
|
329
|
+
const target = market(20);
|
|
330
|
+
const compatible = allocation({
|
|
331
|
+
market: address(40),
|
|
332
|
+
seatIndex: 8,
|
|
333
|
+
tokenSide: 'base',
|
|
334
|
+
mint: QUOTE_MINT,
|
|
335
|
+
freeAmount: 50n,
|
|
336
|
+
});
|
|
337
|
+
const wrongProgram = allocation({
|
|
338
|
+
market: address(50),
|
|
339
|
+
seatIndex: 9,
|
|
340
|
+
tokenSide: 'quote',
|
|
341
|
+
mint: QUOTE_MINT,
|
|
342
|
+
tokenProgram: address(99),
|
|
343
|
+
freeAmount: 100n,
|
|
344
|
+
});
|
|
345
|
+
const plan = planClobOrderFunding({
|
|
346
|
+
authority: AUTHORITY,
|
|
347
|
+
targetMarket: target,
|
|
348
|
+
side: 'buy',
|
|
349
|
+
price: 1n,
|
|
350
|
+
quantity: 50n,
|
|
351
|
+
allocations: [wrongProgram, compatible],
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
expect(plan.sources[0]?.allocation).toBe(compatible);
|
|
355
|
+
expect(plan.shortfall).toBe(0n);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('calculates conservative order funding and rejects u64 overflow', () => {
|
|
359
|
+
expect(calculateClobOrderFundingAmount({ side: 'sell', price: 25n, quantity: 10n })).toBe(10n);
|
|
360
|
+
expect(calculateClobOrderFundingAmount({ side: 'buy', price: 25n, quantity: 10n })).toBe(250n);
|
|
361
|
+
expect(() => calculateClobOrderFundingAmount({
|
|
362
|
+
side: 'buy',
|
|
363
|
+
price: 0xffffffffffffffffn,
|
|
364
|
+
quantity: 2n,
|
|
365
|
+
})).toThrowError(ClobExchangeError);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('rejects an indexed funding plan when an exact seat balance becomes stale', () => {
|
|
369
|
+
const target = market(20);
|
|
370
|
+
const source = market(40);
|
|
371
|
+
const plan = planClobOrderFunding({
|
|
372
|
+
authority: AUTHORITY,
|
|
373
|
+
targetMarket: target,
|
|
374
|
+
side: 'buy',
|
|
375
|
+
price: 2n,
|
|
376
|
+
quantity: 100n,
|
|
377
|
+
allocations: [
|
|
378
|
+
allocation({
|
|
379
|
+
market: target.address,
|
|
380
|
+
seatIndex: 2,
|
|
381
|
+
tokenSide: 'quote',
|
|
382
|
+
mint: QUOTE_MINT,
|
|
383
|
+
vault: target.quoteVault,
|
|
384
|
+
freeAmount: 50n,
|
|
385
|
+
}),
|
|
386
|
+
allocation({
|
|
387
|
+
market: source.address,
|
|
388
|
+
seatIndex: 8,
|
|
389
|
+
tokenSide: 'base',
|
|
390
|
+
mint: QUOTE_MINT,
|
|
391
|
+
vault: source.baseVault,
|
|
392
|
+
freeAmount: 150n,
|
|
393
|
+
}),
|
|
394
|
+
],
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
expect(() => validateClobOrderFundingPlanSeats({
|
|
398
|
+
plan,
|
|
399
|
+
targetSeats: [seat(2, 0n, 50n)],
|
|
400
|
+
sourceSeatsByMarket: { [source.address]: [seat(8, 149n, 0n)] },
|
|
401
|
+
})).toThrowError(expect.objectContaining({ code: 'stale_balance' }));
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('rejects a cross-market plan when the target balance increases', () => {
|
|
405
|
+
const target = market(20);
|
|
406
|
+
const source = market(40);
|
|
407
|
+
const plan = planClobOrderFunding({
|
|
408
|
+
authority: AUTHORITY,
|
|
409
|
+
targetMarket: target,
|
|
410
|
+
side: 'buy',
|
|
411
|
+
price: 1n,
|
|
412
|
+
quantity: 100n,
|
|
413
|
+
allocations: [
|
|
414
|
+
allocation({
|
|
415
|
+
market: target.address,
|
|
416
|
+
seatIndex: 2,
|
|
417
|
+
tokenSide: 'quote',
|
|
418
|
+
mint: QUOTE_MINT,
|
|
419
|
+
vault: target.quoteVault,
|
|
420
|
+
freeAmount: 25n,
|
|
421
|
+
}),
|
|
422
|
+
allocation({
|
|
423
|
+
market: source.address,
|
|
424
|
+
seatIndex: 8,
|
|
425
|
+
tokenSide: 'base',
|
|
426
|
+
mint: QUOTE_MINT,
|
|
427
|
+
vault: source.baseVault,
|
|
428
|
+
freeAmount: 75n,
|
|
429
|
+
}),
|
|
430
|
+
],
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(() => validateClobOrderFundingPlanSeats({
|
|
434
|
+
plan,
|
|
435
|
+
targetSeats: [seat(2, 0n, 30n)],
|
|
436
|
+
sourceSeatsByMarket: { [source.address]: [seat(8, 75n, 0n)] },
|
|
437
|
+
})).toThrowError(expect.objectContaining({ code: 'stale_balance' }));
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('distinguishes omitted fresh source data from supplied stale state', () => {
|
|
441
|
+
const target = market(20);
|
|
442
|
+
const source = market(40);
|
|
443
|
+
const plan = planClobOrderFunding({
|
|
444
|
+
authority: AUTHORITY,
|
|
445
|
+
targetMarket: target,
|
|
446
|
+
side: 'buy',
|
|
447
|
+
price: 1n,
|
|
448
|
+
quantity: 100n,
|
|
449
|
+
allocations: [allocation({
|
|
450
|
+
market: source.address,
|
|
451
|
+
seatIndex: 8,
|
|
452
|
+
tokenSide: 'base',
|
|
453
|
+
mint: QUOTE_MINT,
|
|
454
|
+
vault: source.baseVault,
|
|
455
|
+
freeAmount: 100n,
|
|
456
|
+
})],
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
expect(() => validateClobOrderFundingPlanSeats({
|
|
460
|
+
plan,
|
|
461
|
+
targetSeats: [],
|
|
462
|
+
})).toThrowError(expect.objectContaining({ code: 'funding_plan_mismatch' }));
|
|
463
|
+
expect(() => validateClobOrderFundingPlanSeats({
|
|
464
|
+
plan,
|
|
465
|
+
targetSeats: [],
|
|
466
|
+
sourceSeatsByMarket: { [source.address]: [] },
|
|
467
|
+
})).toThrowError(expect.objectContaining({ code: 'stale_balance' }));
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('rejects offsetting negative amounts in a funded order plan', () => {
|
|
471
|
+
const target = market(20);
|
|
472
|
+
const source = market(40, { baseMint: QUOTE_MINT, quoteMint: address(6) });
|
|
473
|
+
const plan = planClobOrderFunding({
|
|
474
|
+
authority: AUTHORITY,
|
|
475
|
+
targetMarket: target,
|
|
476
|
+
side: 'buy',
|
|
477
|
+
price: 1n,
|
|
478
|
+
quantity: 100n,
|
|
479
|
+
allocations: [
|
|
480
|
+
allocation({
|
|
481
|
+
market: source.address,
|
|
482
|
+
seatIndex: 8,
|
|
483
|
+
tokenSide: 'base',
|
|
484
|
+
mint: QUOTE_MINT,
|
|
485
|
+
vault: source.baseVault,
|
|
486
|
+
freeAmount: 200n,
|
|
487
|
+
}),
|
|
488
|
+
],
|
|
489
|
+
});
|
|
490
|
+
const invalidPlan = {
|
|
491
|
+
...plan,
|
|
492
|
+
sources: [{
|
|
493
|
+
allocation: plan.sources[0]!.allocation,
|
|
494
|
+
amount: 101n,
|
|
495
|
+
}],
|
|
496
|
+
shortfall: -1n,
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
expect(() => buildClobFundedOrderInstructionPlan({
|
|
500
|
+
plan: invalidPlan,
|
|
501
|
+
targetMarket: target,
|
|
502
|
+
sourceMarkets: [source],
|
|
503
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
504
|
+
price: 1n,
|
|
505
|
+
quantity: 100n,
|
|
506
|
+
expirationTime: 999n,
|
|
507
|
+
})).toThrowError(expect.objectContaining({ code: 'funding_plan_mismatch' }));
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('preserves insufficient-funds errors for valid positive shortfalls', () => {
|
|
511
|
+
const target = market(20);
|
|
512
|
+
const plan = planClobOrderFunding({
|
|
513
|
+
authority: AUTHORITY,
|
|
514
|
+
targetMarket: target,
|
|
515
|
+
side: 'buy',
|
|
516
|
+
price: 1n,
|
|
517
|
+
quantity: 100n,
|
|
518
|
+
allocations: [],
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
expect(() => buildClobFundedOrderInstructionPlan({
|
|
522
|
+
plan,
|
|
523
|
+
targetMarket: target,
|
|
524
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
525
|
+
price: 1n,
|
|
526
|
+
quantity: 100n,
|
|
527
|
+
expirationTime: 999n,
|
|
528
|
+
})).toThrowError(expect.objectContaining({ code: 'insufficient_funds' }));
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it('builds source withdrawal, target deposit, and order with distinct market records', async () => {
|
|
532
|
+
const target = market(20);
|
|
533
|
+
const source = market(40, { baseMint: QUOTE_MINT, quoteMint: address(6) });
|
|
534
|
+
const plan = planClobOrderFunding({
|
|
535
|
+
authority: AUTHORITY,
|
|
536
|
+
targetMarket: target,
|
|
537
|
+
side: 'buy',
|
|
538
|
+
price: 2n,
|
|
539
|
+
quantity: 200n,
|
|
540
|
+
allocations: [
|
|
541
|
+
allocation({
|
|
542
|
+
market: target.address,
|
|
543
|
+
seatIndex: 2,
|
|
544
|
+
tokenSide: 'quote',
|
|
545
|
+
mint: QUOTE_MINT,
|
|
546
|
+
vault: target.quoteVault,
|
|
547
|
+
freeAmount: 100n,
|
|
548
|
+
}),
|
|
549
|
+
allocation({
|
|
550
|
+
market: source.address,
|
|
551
|
+
seatIndex: 8,
|
|
552
|
+
tokenSide: 'base',
|
|
553
|
+
mint: QUOTE_MINT,
|
|
554
|
+
vault: source.baseVault,
|
|
555
|
+
freeAmount: 300n,
|
|
556
|
+
}),
|
|
557
|
+
],
|
|
558
|
+
});
|
|
559
|
+
const instructionPlan = buildClobFundedOrderInstructionPlan({
|
|
560
|
+
plan,
|
|
561
|
+
targetMarket: target,
|
|
562
|
+
sourceMarkets: [source],
|
|
563
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
564
|
+
price: 2n,
|
|
565
|
+
quantity: 200n,
|
|
566
|
+
expirationTime: 999n,
|
|
567
|
+
});
|
|
568
|
+
const bytes = await instructionPlan.instructionData(contextForPlan(instructionPlan));
|
|
569
|
+
|
|
570
|
+
expect([
|
|
571
|
+
bytes[0],
|
|
572
|
+
bytes[24],
|
|
573
|
+
bytes[48],
|
|
574
|
+
bytes[72],
|
|
575
|
+
bytes[96],
|
|
576
|
+
]).toEqual([
|
|
577
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
578
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
579
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
580
|
+
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
581
|
+
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
582
|
+
]);
|
|
583
|
+
expect(bytes[1]).toBe(1);
|
|
584
|
+
expect(bytes[25]).toBe(1);
|
|
585
|
+
expect(bytes[49]).toBe(2);
|
|
586
|
+
expect(bytes[73]).toBe(2);
|
|
587
|
+
expect(bytes[98]).toBe(2);
|
|
588
|
+
expect(readU16(bytes, 22)).toBe(0xffff);
|
|
589
|
+
expect(readU16(bytes, 70)).toBe(0xffff);
|
|
590
|
+
expect(instructionPlan.readWriteAccounts).toContain(source.address);
|
|
591
|
+
expect(instructionPlan.readWriteAccounts).toContain(source.orderArena);
|
|
592
|
+
expect(instructionPlan.readWriteAccounts).toContain(source.bidsCbook);
|
|
593
|
+
expect(instructionPlan.readWriteAccounts).toContain(source.asksCbook);
|
|
594
|
+
expect(instructionPlan.readWriteAccounts).toContain(target.orderArena);
|
|
595
|
+
expect(instructionPlan.readWriteAccounts).toContain(WALLET_QUOTE);
|
|
596
|
+
expect(instructionPlan.readOnlyAccounts).not.toContain(source.orderArena);
|
|
597
|
+
expect(instructionPlan.readOnlyAccounts).not.toContain(source.bidsCbook);
|
|
598
|
+
expect(instructionPlan.readOnlyAccounts).not.toContain(source.asksCbook);
|
|
599
|
+
expect(instructionPlan.readOnlyAccounts).not.toContain(source.marketAuthority);
|
|
600
|
+
expect(instructionPlan.readOnlyAccounts).not.toContain(target.marketAuthority);
|
|
601
|
+
expect(instructionPlan.review.value).not.toHaveProperty('walletDeposit');
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it('builds a secondary target-market withdrawal before the target deposit and order', async () => {
|
|
605
|
+
const target = market(20);
|
|
606
|
+
const plan = planClobOrderFunding({
|
|
607
|
+
authority: AUTHORITY,
|
|
608
|
+
targetMarket: target,
|
|
609
|
+
side: 'buy',
|
|
610
|
+
price: 1n,
|
|
611
|
+
quantity: 100n,
|
|
612
|
+
allocations: [
|
|
613
|
+
allocation({
|
|
614
|
+
market: target.address,
|
|
615
|
+
seatIndex: 2,
|
|
616
|
+
tokenSide: 'quote',
|
|
617
|
+
mint: QUOTE_MINT,
|
|
618
|
+
vault: target.quoteVault,
|
|
619
|
+
freeAmount: 60n,
|
|
620
|
+
}),
|
|
621
|
+
allocation({
|
|
622
|
+
market: target.address,
|
|
623
|
+
seatIndex: 3,
|
|
624
|
+
tokenSide: 'quote',
|
|
625
|
+
mint: QUOTE_MINT,
|
|
626
|
+
vault: target.quoteVault,
|
|
627
|
+
freeAmount: 60n,
|
|
628
|
+
}),
|
|
629
|
+
],
|
|
630
|
+
});
|
|
631
|
+
const instructionPlan = buildClobFundedOrderInstructionPlan({
|
|
632
|
+
plan,
|
|
633
|
+
targetMarket: target,
|
|
634
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
635
|
+
price: 1n,
|
|
636
|
+
quantity: 100n,
|
|
637
|
+
expirationTime: 999n,
|
|
638
|
+
});
|
|
639
|
+
const bytes = await instructionPlan.instructionData(contextForPlan(instructionPlan));
|
|
640
|
+
|
|
641
|
+
expect([bytes[0], bytes[24], bytes[48], bytes[72], bytes[96]]).toEqual([
|
|
642
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
643
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
644
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
645
|
+
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
646
|
+
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
647
|
+
]);
|
|
648
|
+
expect(readU32(bytes, 12)).toBe(3);
|
|
649
|
+
expect(readU32(bytes, 60)).toBe(2);
|
|
650
|
+
expect(instructionPlan.readWriteAccounts.filter((account) => account === target.address))
|
|
651
|
+
.toHaveLength(1);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it('builds withdrawals from multiple markets before one target deposit and order', async () => {
|
|
655
|
+
const target = market(20);
|
|
656
|
+
const first = market(40, { baseMint: QUOTE_MINT, quoteMint: address(46) });
|
|
657
|
+
const second = market(60, { baseMint: QUOTE_MINT, quoteMint: address(66) });
|
|
658
|
+
const plan = planClobOrderFunding({
|
|
659
|
+
authority: AUTHORITY,
|
|
660
|
+
targetMarket: target,
|
|
661
|
+
side: 'buy',
|
|
662
|
+
price: 1n,
|
|
663
|
+
quantity: 300n,
|
|
664
|
+
allocations: [
|
|
665
|
+
allocation({
|
|
666
|
+
market: first.address,
|
|
667
|
+
seatIndex: 8,
|
|
668
|
+
tokenSide: 'base',
|
|
669
|
+
mint: QUOTE_MINT,
|
|
670
|
+
vault: first.baseVault,
|
|
671
|
+
freeAmount: 100n,
|
|
672
|
+
}),
|
|
673
|
+
allocation({
|
|
674
|
+
market: second.address,
|
|
675
|
+
seatIndex: 9,
|
|
676
|
+
tokenSide: 'base',
|
|
677
|
+
mint: QUOTE_MINT,
|
|
678
|
+
vault: second.baseVault,
|
|
679
|
+
freeAmount: 200n,
|
|
680
|
+
}),
|
|
681
|
+
],
|
|
682
|
+
});
|
|
683
|
+
const instructionPlan = buildClobFundedOrderInstructionPlan({
|
|
684
|
+
plan,
|
|
685
|
+
targetMarket: target,
|
|
686
|
+
sourceMarkets: [first, second],
|
|
687
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
688
|
+
price: 1n,
|
|
689
|
+
quantity: 300n,
|
|
690
|
+
expirationTime: 999n,
|
|
691
|
+
});
|
|
692
|
+
const bytes = await instructionPlan.instructionData(contextForPlan(instructionPlan));
|
|
693
|
+
|
|
694
|
+
expect([bytes[0], bytes[24], bytes[48], bytes[72], bytes[96], bytes[120], bytes[128], bytes[152]]).toEqual([
|
|
695
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
696
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
697
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
698
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
699
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
700
|
+
CLOB_INSTRUCTION_SEAT_CREATE,
|
|
701
|
+
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
702
|
+
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
703
|
+
]);
|
|
704
|
+
expect([bytes[1], bytes[25], bytes[49], bytes[73], bytes[97], bytes[129], bytes[154]])
|
|
705
|
+
.toEqual([1, 1, 2, 2, 3, 3, 3]);
|
|
706
|
+
expect(instructionPlan.review.value.sourceMarketCount).toBe('2');
|
|
707
|
+
expect(instructionPlan.review.value.exchangeAmount).toBe('300');
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it('rejects an order plan that tries to add wallet funds implicitly', () => {
|
|
711
|
+
const target = market(20);
|
|
712
|
+
const plan = planClobOrderFunding({
|
|
713
|
+
authority: AUTHORITY,
|
|
714
|
+
targetMarket: target,
|
|
715
|
+
side: 'buy',
|
|
716
|
+
price: 2n,
|
|
717
|
+
quantity: 50n,
|
|
718
|
+
allocations: [],
|
|
719
|
+
});
|
|
720
|
+
const walletFundedPlan = {
|
|
721
|
+
...plan,
|
|
722
|
+
walletAmount: 100n,
|
|
723
|
+
shortfall: 0n,
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
expect(() => buildClobFundedOrderInstructionPlan({
|
|
727
|
+
plan: walletFundedPlan,
|
|
728
|
+
targetMarket: target,
|
|
729
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
730
|
+
price: 2n,
|
|
731
|
+
quantity: 50n,
|
|
732
|
+
expirationTime: 999n,
|
|
733
|
+
})).toThrowError(expect.objectContaining({ code: 'funding_plan_mismatch' }));
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
it('uses exchange-level labels for deposit and withdrawal plans', async () => {
|
|
737
|
+
const target = market(20);
|
|
738
|
+
const deposit = buildClobExchangeDepositInstructionPlan({
|
|
739
|
+
market: target,
|
|
740
|
+
authority: AUTHORITY,
|
|
741
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
742
|
+
tokenSide: 'quote',
|
|
743
|
+
amount: 25n,
|
|
744
|
+
seatIndex: null,
|
|
745
|
+
});
|
|
746
|
+
const depositBytes = await deposit.instructionData(contextForPlan(deposit));
|
|
747
|
+
expect([depositBytes[0], depositBytes[24], depositBytes[32]]).toEqual([
|
|
748
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
749
|
+
CLOB_INSTRUCTION_SEAT_CREATE,
|
|
750
|
+
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
751
|
+
]);
|
|
752
|
+
expect(deposit.review.label).toBe('Deposit to CLOB exchange');
|
|
753
|
+
|
|
754
|
+
const withdrawal = buildClobExchangeWithdrawalInstructionPlan({
|
|
755
|
+
market: target,
|
|
756
|
+
authority: AUTHORITY,
|
|
757
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
758
|
+
tokenSide: 'quote',
|
|
759
|
+
amount: 10n,
|
|
760
|
+
seatIndex: 2,
|
|
761
|
+
});
|
|
762
|
+
const withdrawalBytes = await withdrawal.instructionData(contextForPlan(withdrawal));
|
|
763
|
+
expect([withdrawalBytes[0], withdrawalBytes[24]]).toEqual([
|
|
764
|
+
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
765
|
+
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
766
|
+
]);
|
|
767
|
+
expect(withdrawal.review.label).toBe('Withdraw from CLOB exchange');
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
it('wraps missing wallet token-account setup ahead of the CLOB action', async () => {
|
|
771
|
+
const target = market(20);
|
|
772
|
+
const source = market(40, { baseMint: QUOTE_MINT, quoteMint: address(6) });
|
|
773
|
+
const clobProgram = address(90);
|
|
774
|
+
const plan = planClobOrderFunding({
|
|
775
|
+
authority: AUTHORITY,
|
|
776
|
+
targetMarket: target,
|
|
777
|
+
side: 'buy',
|
|
778
|
+
price: 2n,
|
|
779
|
+
quantity: 50n,
|
|
780
|
+
allocations: [allocation({
|
|
781
|
+
market: source.address,
|
|
782
|
+
seatIndex: 8,
|
|
783
|
+
tokenSide: 'base',
|
|
784
|
+
mint: QUOTE_MINT,
|
|
785
|
+
vault: source.baseVault,
|
|
786
|
+
freeAmount: 100n,
|
|
787
|
+
})],
|
|
788
|
+
});
|
|
789
|
+
const orderPlan = buildClobFundedOrderInstructionPlan({
|
|
790
|
+
plan,
|
|
791
|
+
targetMarket: target,
|
|
792
|
+
sourceMarkets: [source],
|
|
793
|
+
walletTokenAccount: WALLET_QUOTE,
|
|
794
|
+
price: 2n,
|
|
795
|
+
quantity: 50n,
|
|
796
|
+
expirationTime: 999n,
|
|
797
|
+
});
|
|
798
|
+
const transactionPlan = wrapClobPlanWithTokenAccountInitialization({
|
|
799
|
+
plan: orderPlan,
|
|
800
|
+
clobProgram,
|
|
801
|
+
initialization: {
|
|
802
|
+
instructionData: async () => new Uint8Array([0xaa]),
|
|
803
|
+
tokenProgram: TOKEN_PROGRAM,
|
|
804
|
+
tokenAccount: WALLET_QUOTE,
|
|
805
|
+
mint: QUOTE_MINT,
|
|
806
|
+
owner: AUTHORITY,
|
|
807
|
+
},
|
|
808
|
+
});
|
|
809
|
+
const bytes = await transactionPlan.instructionData(contextForPlan(transactionPlan));
|
|
810
|
+
const multicall = MulticallArgs.from_array(bytes);
|
|
811
|
+
|
|
812
|
+
expect(transactionPlan.programAddress).toBe(MULTICALL_PROGRAM_ADDRESS);
|
|
813
|
+
expect(multicall?.get_calls_count()).toBe(2);
|
|
814
|
+
expect(multicall?.get_calls()[0].get_data()).toEqual([0xaa]);
|
|
815
|
+
expect(multicall?.get_calls()[1].get_data()[0]).toBe(CLOB_INSTRUCTION_MARKET_RECORD);
|
|
816
|
+
expect(transactionPlan.readWriteAccounts).toContain(WALLET_QUOTE);
|
|
817
|
+
expect(transactionPlan.readOnlyAccounts).toContain(clobProgram);
|
|
818
|
+
expect(transactionPlan.review.value.initializesWalletTokenAccount).toBe('true');
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
function market(
|
|
823
|
+
id: number,
|
|
824
|
+
overrides: Partial<ClobTradingMarket> = {}
|
|
825
|
+
): ClobTradingMarket {
|
|
826
|
+
return {
|
|
827
|
+
address: address(id),
|
|
828
|
+
orderArena: address(id + 1),
|
|
829
|
+
bidsCbook: address(id + 2),
|
|
830
|
+
asksCbook: address(id + 3),
|
|
831
|
+
tokenProgram: TOKEN_PROGRAM,
|
|
832
|
+
baseMint: BASE_MINT,
|
|
833
|
+
quoteMint: QUOTE_MINT,
|
|
834
|
+
baseVault: address(id + 4),
|
|
835
|
+
quoteVault: address(id + 5),
|
|
836
|
+
marketAuthority: address(id + 6),
|
|
837
|
+
statusFlags: 0,
|
|
838
|
+
...overrides,
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function allocation(
|
|
843
|
+
overrides: Partial<ClobAssetBalanceAllocation> = {}
|
|
844
|
+
): ClobAssetBalanceAllocation {
|
|
845
|
+
return {
|
|
846
|
+
market: address(10),
|
|
847
|
+
authority: AUTHORITY,
|
|
848
|
+
seatIndex: 2,
|
|
849
|
+
tokenSide: 'quote',
|
|
850
|
+
mint: QUOTE_MINT,
|
|
851
|
+
vault: address(15),
|
|
852
|
+
freeAmount: 1n,
|
|
853
|
+
statusFlags: 0,
|
|
854
|
+
tokenProgram: TOKEN_PROGRAM,
|
|
855
|
+
metadataReady: true,
|
|
856
|
+
...overrides,
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function seat(seatIndex: number, quantityBase: bigint, quantityQuote: bigint) {
|
|
861
|
+
return {
|
|
862
|
+
seatIndex,
|
|
863
|
+
seatAuthority: AUTHORITY,
|
|
864
|
+
quantityBase,
|
|
865
|
+
quantityQuote,
|
|
866
|
+
headOrderEntryIndex: 0,
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function contextForPlan(plan: ClobInstructionPlan) {
|
|
871
|
+
const indexes = new Map<string, number>();
|
|
872
|
+
[...plan.readWriteAccounts, ...plan.readOnlyAccounts].forEach((account, index) => {
|
|
873
|
+
indexes.set(account, index + 2);
|
|
874
|
+
});
|
|
875
|
+
return {
|
|
876
|
+
getAccountIndex(pubkey: Uint8Array): number {
|
|
877
|
+
const account = encodeAddress(pubkey);
|
|
878
|
+
const index = indexes.get(account);
|
|
879
|
+
if (index === undefined) throw new Error('missing account ' + account);
|
|
880
|
+
return index;
|
|
881
|
+
},
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
function key(id: number): Uint8Array {
|
|
886
|
+
const bytes = new Uint8Array(32);
|
|
887
|
+
bytes[0] = id;
|
|
888
|
+
return bytes;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function address(id: number): string {
|
|
892
|
+
return encodeAddress(key(id));
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function readU16(bytes: Uint8Array, offset: number): number {
|
|
896
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint16(offset, true);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
function readU32(bytes: Uint8Array, offset: number): number {
|
|
900
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(offset, true);
|
|
901
|
+
}
|