@thru/programs 0.2.39 → 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.
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
  import { encodeAddress } from '@thru/sdk/helpers';
3
3
  import {
4
+ CLOB_INSTRUCTION_MARKET_RECORD,
5
+ CLOB_NULL_INDEX,
4
6
  CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
5
7
  CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY,
6
8
  CLOB_CBOOK_MAX_PRICE_IN_TICKS,
@@ -18,6 +20,7 @@ import {
18
20
  OrderFilledEventBuilder,
19
21
  SeatEntryBuilder,
20
22
  buildOrderBookSnapshot,
23
+ createMarketRecordInstruction,
21
24
  createOrderEntryInstruction,
22
25
  createSeatlessOrderEntryInstruction,
23
26
  encodeOrderFlags,
@@ -44,6 +47,35 @@ function context(indexes: Record<string, number>) {
44
47
  }
45
48
 
46
49
  describe('clob helpers', () => {
50
+ it('validates market-record seat indices before ABI serialization', async () => {
51
+ const accounts = Array.from({ length: 7 }, (_, index) => key(index + 1));
52
+ const args = {
53
+ marketRecordIndex: 1,
54
+ seatArenaAccountBytes: accounts[0],
55
+ orderArenaAccountBytes: accounts[1],
56
+ bidsCbookAccountBytes: accounts[2],
57
+ asksCbookAccountBytes: accounts[3],
58
+ tokenProgramAccountBytes: accounts[4],
59
+ baseVaultAccountBytes: accounts[5],
60
+ quoteVaultAccountBytes: accounts[6],
61
+ };
62
+ const lookup = context(Object.fromEntries(
63
+ accounts.map((account, index) => [bytesToHex(account), index + 2])
64
+ ));
65
+
66
+ const anonymous = await createMarketRecordInstruction(args)(lookup);
67
+ const active = await createMarketRecordInstruction({ ...args, seatIndex: 2 })(lookup);
68
+ expect(anonymous[0]).toBe(CLOB_INSTRUCTION_MARKET_RECORD);
69
+ expect(new DataView(anonymous.buffer).getUint32(12, true)).toBe(CLOB_NULL_INDEX);
70
+ expect(new DataView(active.buffer).getUint32(12, true)).toBe(2);
71
+
72
+ for (const seatIndex of [0, 1, 1.5, CLOB_NULL_INDEX, 0x1_0000_0002]) {
73
+ expect(() => createMarketRecordInstruction({ ...args, seatIndex })).toThrowError(
74
+ /seatIndex must be an integer/
75
+ );
76
+ }
77
+ });
78
+
47
79
  it('encodes order flags with C program bit semantics', () => {
48
80
  expect(encodeOrderFlags({ side: 'buy', orderType: 'gtc' })).toBe(
49
81
  CLOB_ORDER_FLAG_BUY | (CLOB_ORDER_TYPE_GTC << 1)
package/src/clob/index.ts CHANGED
@@ -174,10 +174,11 @@ export interface MarketRecordArgs {
174
174
  tokenProgramAccountBytes: Uint8Array;
175
175
  baseVaultAccountBytes: Uint8Array;
176
176
  quoteVaultAccountBytes: Uint8Array;
177
- marketAuthorityAccountBytes: Uint8Array;
177
+ marketAuthorityAccountBytes?: Uint8Array;
178
178
  }
179
179
 
180
180
  export interface MarketCreateArgs extends MarketRecordArgs {
181
+ marketAuthorityAccountBytes: Uint8Array;
181
182
  lotSize: bigint;
182
183
  tickSize: bigint;
183
184
  baseMintAccountBytes: Uint8Array;
@@ -438,6 +439,9 @@ export type ParsedClobEvent =
438
439
  });
439
440
 
440
441
  export function createMarketRecordInstruction(args: MarketRecordArgs): InstructionData {
442
+ const seatIndex = args.seatIndex === undefined
443
+ ? CLOB_NULL_INDEX
444
+ : assertClobActiveSeatIndex(args.seatIndex);
441
445
  return async (context) => {
442
446
  const payload = new MarketRecordInstructionBuilder()
443
447
  .set_market_record_idx(assertU8(args.marketRecordIndex, 'marketRecordIndex'))
@@ -446,11 +450,11 @@ export function createMarketRecordInstruction(args: MarketRecordArgs): Instructi
446
450
  .set_bids_cbook_account_idx(accountIndex(context, args.bidsCbookAccountBytes))
447
451
  .set_asks_cbook_account_idx(accountIndex(context, args.asksCbookAccountBytes))
448
452
  .set_seat_authority_account_idx(optionalAccountIndex(context, args.seatAuthorityAccountBytes))
449
- .set_seat_idx(args.seatIndex ?? CLOB_NULL_INDEX)
453
+ .set_seat_idx(seatIndex)
450
454
  .set_token_program_idx(accountIndex(context, args.tokenProgramAccountBytes))
451
455
  .set_base_vault_account_idx(accountIndex(context, args.baseVaultAccountBytes))
452
456
  .set_quote_vault_account_idx(accountIndex(context, args.quoteVaultAccountBytes))
453
- .set_market_authority_account_idx(accountIndex(context, args.marketAuthorityAccountBytes))
457
+ .set_market_authority_account_idx(optionalAccountIndex(context, args.marketAuthorityAccountBytes))
454
458
  .build();
455
459
  return buildClobInstruction('market_record', payload);
456
460
  };
@@ -1083,6 +1087,13 @@ function assertU16(value: number, label: string): number {
1083
1087
  return value;
1084
1088
  }
1085
1089
 
1090
+ export function assertClobActiveSeatIndex(value: number, label = 'seatIndex'): number {
1091
+ if (!Number.isSafeInteger(value) || value < 2 || value >= CLOB_NULL_INDEX) {
1092
+ throw new Error(`${label} must be an integer between 2 and ${CLOB_NULL_INDEX - 1}`);
1093
+ }
1094
+ return value;
1095
+ }
1096
+
1086
1097
  function assertU64(value: bigint, label: string): void {
1087
1098
  if (value < 0n || value > 0xffffffffffffffffn) {
1088
1099
  throw new Error(`${label} must be between 0 and 18446744073709551615`);
@@ -1144,3 +1155,5 @@ function bytesFromView(value: unknown): Uint8Array {
1144
1155
  if (value instanceof Pubkey) return value.toBytes();
1145
1156
  throw new Error('generated view did not expose a byte buffer');
1146
1157
  }
1158
+
1159
+ export * from './exchange';