@thru/programs 0.3.4 → 0.3.5
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/README.md +123 -37
- package/dist/abi-manager/index.cjs +9568 -0
- package/dist/abi-manager/index.cjs.map +1 -0
- package/dist/abi-manager/index.d.cts +2169 -0
- package/dist/abi-manager/index.d.ts +2169 -0
- package/dist/abi-manager/index.js +4 -0
- package/dist/abi-manager/index.js.map +1 -0
- package/dist/amm/index.cjs +1 -3
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +1 -1
- package/dist/amm/index.d.ts +1 -1
- package/dist/amm/index.js +2 -2
- package/dist/amm/index.js.map +1 -1
- package/dist/chunk-6IC6CO5S.js +9443 -0
- package/dist/chunk-6IC6CO5S.js.map +1 -0
- package/dist/chunk-7FBCBZIY.js +41 -0
- package/dist/chunk-7FBCBZIY.js.map +1 -0
- package/dist/chunk-BNYDH4ZP.js +4351 -0
- package/dist/chunk-BNYDH4ZP.js.map +1 -0
- package/dist/chunk-OODN6LJA.js +6567 -0
- package/dist/chunk-OODN6LJA.js.map +1 -0
- package/dist/chunk-YX7BLFQZ.js +81 -0
- package/dist/chunk-YX7BLFQZ.js.map +1 -0
- package/dist/clob/index.cjs +6674 -3209
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +1114 -52
- package/dist/clob/index.d.ts +1114 -52
- package/dist/clob/index.js +6812 -3393
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +24734 -0
- package/dist/deploy/index.cjs.map +1 -0
- package/dist/deploy/index.d.cts +180 -0
- package/dist/deploy/index.d.ts +180 -0
- package/dist/deploy/index.js +1846 -0
- package/dist/deploy/index.js.map +1 -0
- package/dist/helpers-DD7q61nr.d.cts +7 -0
- package/dist/helpers-DD7q61nr.d.ts +7 -0
- package/dist/manager/index.cjs +6680 -0
- package/dist/manager/index.cjs.map +1 -0
- package/dist/manager/index.d.cts +1279 -0
- package/dist/manager/index.d.ts +1279 -0
- package/dist/manager/index.js +4 -0
- package/dist/manager/index.js.map +1 -0
- package/dist/passkey-manager/index.cjs +4 -5
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.js +2 -2
- package/dist/uploader/index.cjs +4442 -0
- package/dist/uploader/index.cjs.map +1 -0
- package/dist/uploader/index.d.cts +761 -0
- package/dist/uploader/index.d.ts +761 -0
- package/dist/uploader/index.js +4 -0
- package/dist/uploader/index.js.map +1 -0
- package/package.json +22 -2
- package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
- package/src/abi-manager/accounts.ts +91 -0
- package/src/abi-manager/constants.ts +19 -0
- package/src/abi-manager/derivation.ts +86 -0
- package/src/abi-manager/errors.ts +48 -0
- package/src/abi-manager/index.test.ts +136 -0
- package/src/abi-manager/index.ts +56 -0
- package/src/abi-manager/instructions.ts +573 -0
- package/src/abi-manager/types.ts +150 -0
- package/src/amm/index.test.ts +8 -2
- package/src/amm/index.ts +1 -1
- package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
- package/src/clob/exchange-client.test.ts +115 -2
- package/src/clob/exchange-client.ts +52 -13
- package/src/clob/exchange.test.ts +18 -16
- package/src/clob/exchange.ts +4 -0
- package/src/clob/index.test.ts +134 -7
- package/src/clob/index.ts +302 -10
- package/src/clob/price.test.ts +32 -0
- package/src/clob/price.ts +77 -0
- package/src/deploy/accounts.ts +208 -0
- package/src/deploy/chain.ts +163 -0
- package/src/deploy/constants.ts +25 -0
- package/src/deploy/deploy.test.ts +349 -0
- package/src/deploy/errors.ts +72 -0
- package/src/deploy/index.ts +44 -0
- package/src/deploy/inspection.ts +146 -0
- package/src/deploy/seeds.ts +29 -0
- package/src/deploy/types.ts +178 -0
- package/src/deploy/uploader.test.ts +326 -0
- package/src/deploy/uploader.ts +558 -0
- package/src/deploy/validation-imports.test.ts +55 -0
- package/src/deploy/validation.ts +183 -0
- package/src/deploy/workflows.test.ts +422 -0
- package/src/deploy/workflows.ts +1088 -0
- package/src/helpers/bytes.test.ts +58 -0
- package/src/helpers/bytes.ts +28 -3
- package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/manager/abi/thru/program/manager/types.ts +3814 -0
- package/src/manager/accounts.ts +44 -0
- package/src/manager/constants.ts +16 -0
- package/src/manager/derivation.ts +32 -0
- package/src/manager/errors.ts +47 -0
- package/src/manager/index.test.ts +108 -0
- package/src/manager/index.ts +68 -0
- package/src/manager/instructions.ts +281 -0
- package/src/manager/types.ts +95 -0
- package/src/manager/validation.ts +35 -0
- package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
- package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
- package/src/uploader/accounts.ts +38 -0
- package/src/uploader/constants.ts +14 -0
- package/src/uploader/derivation.ts +34 -0
- package/src/uploader/errors.ts +25 -0
- package/src/uploader/index.test.ts +107 -0
- package/src/uploader/index.ts +34 -0
- package/src/uploader/instructions.ts +163 -0
- package/src/uploader/types.ts +67 -0
- package/src/utils/helpers.ts +121 -0
- package/tsup.config.ts +4 -0
- package/dist/chunk-P5OABVJI.js +0 -28
- package/dist/chunk-P5OABVJI.js.map +0 -1
|
@@ -3,8 +3,17 @@ import { Pubkey } from '@thru/sdk';
|
|
|
3
3
|
import { encodeAddress } from '@thru/sdk/helpers';
|
|
4
4
|
import type { Thru } from '@thru/sdk/client';
|
|
5
5
|
import { deriveTokenAccountAddress } from '../token';
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
TickerFieldBuilder,
|
|
8
|
+
TokenAccountBuilder,
|
|
9
|
+
TokenMintAccountBuilder,
|
|
10
|
+
} from '../token/abi/thru/program/token/types';
|
|
11
|
+
import {
|
|
12
|
+
CLOB_PROGRAM_ADDRESS,
|
|
13
|
+
CLOB_STATUS_FLAG_DEPOSITS_FROZEN,
|
|
14
|
+
CLOB_STATUS_FLAG_PAUSED,
|
|
15
|
+
SeatEntryBuilder,
|
|
16
|
+
} from './index';
|
|
8
17
|
import {
|
|
9
18
|
ClobExchangeClientError,
|
|
10
19
|
createClobExchangeClient,
|
|
@@ -14,6 +23,7 @@ import type { ClobAssetBalanceAllocation, ClobTradingMarket } from './exchange';
|
|
|
14
23
|
|
|
15
24
|
const AUTHORITY = address(10);
|
|
16
25
|
const TOKEN_PROGRAM = address(11);
|
|
26
|
+
const EXCHANGE_META = address(12);
|
|
17
27
|
const BTC = address(20);
|
|
18
28
|
const ETH = address(21);
|
|
19
29
|
const USD = address(22);
|
|
@@ -29,11 +39,16 @@ describe('ClobExchangeClient', () => {
|
|
|
29
39
|
balance(markets[1]!, USD, 'base', 60n),
|
|
30
40
|
],
|
|
31
41
|
});
|
|
42
|
+
harness.setMint(USD, 6, 'USD');
|
|
32
43
|
|
|
33
44
|
const snapshot = await harness.client.getAccountSnapshot({ authority: AUTHORITY });
|
|
34
45
|
|
|
35
46
|
expect(snapshot.assets.map((asset) => asset.mint)).toEqual([BTC, ETH, USD].sort());
|
|
36
47
|
expect(snapshot.assets.find((asset) => asset.mint === USD)?.exchange.freeAmount).toBe(100n);
|
|
48
|
+
expect(snapshot.assets.find((asset) => asset.mint === USD)).toMatchObject({
|
|
49
|
+
symbol: 'USD',
|
|
50
|
+
decimals: 6,
|
|
51
|
+
});
|
|
37
52
|
expect(snapshot.unavailableMarkets).toEqual([]);
|
|
38
53
|
for (const asset of snapshot.assets) {
|
|
39
54
|
expect(asset.discoveryComplete).toBe(true);
|
|
@@ -164,6 +179,73 @@ describe('ClobExchangeClient', () => {
|
|
|
164
179
|
expect(prepared.market.address).toBe(available.address);
|
|
165
180
|
});
|
|
166
181
|
|
|
182
|
+
it('skips a paused existing market when an active market accepts deposits', async () => {
|
|
183
|
+
const paused = { ...market(44, BTC, USD), statusFlags: CLOB_STATUS_FLAG_PAUSED };
|
|
184
|
+
const active = market(45, BTC, USD);
|
|
185
|
+
const harness = createHarness({
|
|
186
|
+
markets: [paused, active],
|
|
187
|
+
allocations: [balance(paused, BTC, 'base', 25n)],
|
|
188
|
+
});
|
|
189
|
+
harness.setWalletBalance(BTC, 100n);
|
|
190
|
+
harness.setSeats(active, []);
|
|
191
|
+
|
|
192
|
+
const prepared = await harness.client.prepareDeposit({
|
|
193
|
+
authority: AUTHORITY,
|
|
194
|
+
mint: BTC,
|
|
195
|
+
amount: 10n,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(prepared.market.address).toBe(active.address);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('rejects deposits when every compatible market is paused', async () => {
|
|
202
|
+
const paused = { ...market(46, BTC, USD), statusFlags: CLOB_STATUS_FLAG_PAUSED };
|
|
203
|
+
const harness = createHarness({ markets: [paused] });
|
|
204
|
+
|
|
205
|
+
await expect(harness.client.prepareDeposit({
|
|
206
|
+
authority: AUTHORITY,
|
|
207
|
+
mint: BTC,
|
|
208
|
+
amount: 10n,
|
|
209
|
+
})).rejects.toMatchObject({ code: 'market_paused' });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('keeps paused markets available for withdrawals', async () => {
|
|
213
|
+
const paused = { ...market(48, BTC, USD), statusFlags: CLOB_STATUS_FLAG_PAUSED };
|
|
214
|
+
const harness = createHarness({
|
|
215
|
+
markets: [paused],
|
|
216
|
+
allocations: [balance(paused, BTC, 'base', 25n)],
|
|
217
|
+
});
|
|
218
|
+
harness.setSeats(paused, [{ authority: AUTHORITY, base: 25n, quote: 0n }]);
|
|
219
|
+
|
|
220
|
+
await expect(harness.client.prepareWithdrawal({
|
|
221
|
+
authority: AUTHORITY,
|
|
222
|
+
mint: BTC,
|
|
223
|
+
amount: 10n,
|
|
224
|
+
})).resolves.toMatchObject({ market: { address: paused.address } });
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('rejects a selected vault whose on-chain mint does not match', async () => {
|
|
228
|
+
const target = market(47, BTC, USD);
|
|
229
|
+
const harness = createHarness({ markets: [target] });
|
|
230
|
+
harness.setWalletBalance(BTC, 100n);
|
|
231
|
+
harness.setSeats(target, []);
|
|
232
|
+
harness.setVaultMint(target.baseVault, OTHER);
|
|
233
|
+
|
|
234
|
+
await expect(harness.client.prepareDeposit({
|
|
235
|
+
authority: AUTHORITY,
|
|
236
|
+
mint: BTC,
|
|
237
|
+
amount: 10n,
|
|
238
|
+
})).rejects.toMatchObject({
|
|
239
|
+
code: 'invalid_token_account',
|
|
240
|
+
details: {
|
|
241
|
+
market: target.address,
|
|
242
|
+
vault: target.baseVault,
|
|
243
|
+
expectedMint: BTC,
|
|
244
|
+
actualMint: OTHER,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
167
249
|
it('uses fresh seat balances for withdrawals and reports fragmented funds', async () => {
|
|
168
250
|
const first = market(50, BTC, USD);
|
|
169
251
|
const second = market(51, BTC, USD);
|
|
@@ -383,9 +465,27 @@ function createHarness({
|
|
|
383
465
|
tokenProgramAddress: TOKEN_PROGRAM,
|
|
384
466
|
});
|
|
385
467
|
|
|
468
|
+
for (const target of markets) {
|
|
469
|
+
accounts.set(target.baseVault, tokenAccount(target.baseMint, CLOB_PROGRAM_ADDRESS));
|
|
470
|
+
accounts.set(target.quoteVault, tokenAccount(target.quoteMint, CLOB_PROGRAM_ADDRESS));
|
|
471
|
+
}
|
|
472
|
+
|
|
386
473
|
return {
|
|
387
474
|
client,
|
|
388
475
|
reads,
|
|
476
|
+
setMint(mint: string, decimals: number, symbol: string) {
|
|
477
|
+
const symbolBytes = new TextEncoder().encode(symbol);
|
|
478
|
+
const paddedSymbol = new Uint8Array(8);
|
|
479
|
+
paddedSymbol.set(symbolBytes);
|
|
480
|
+
const ticker = new TickerFieldBuilder()
|
|
481
|
+
.set_length(symbolBytes.length)
|
|
482
|
+
.set_bytes(Array.from(paddedSymbol))
|
|
483
|
+
.build();
|
|
484
|
+
accounts.set(mint, new TokenMintAccountBuilder()
|
|
485
|
+
.set_decimals(decimals)
|
|
486
|
+
.set_ticker(ticker)
|
|
487
|
+
.build());
|
|
488
|
+
},
|
|
389
489
|
setWalletBalance(mint: string, amount: bigint, owner = AUTHORITY) {
|
|
390
490
|
const tokenAccount = deriveTokenAccountAddress(
|
|
391
491
|
thruClient,
|
|
@@ -400,6 +500,9 @@ function createHarness({
|
|
|
400
500
|
.set_is_frozen(0)
|
|
401
501
|
.build());
|
|
402
502
|
},
|
|
503
|
+
setVaultMint(vault: string, mint: string) {
|
|
504
|
+
accounts.set(vault, tokenAccount(mint, CLOB_PROGRAM_ADDRESS));
|
|
505
|
+
},
|
|
403
506
|
setSeats(target: ClobTradingMarket, seats: Array<{
|
|
404
507
|
authority: string;
|
|
405
508
|
base: bigint;
|
|
@@ -421,12 +524,22 @@ function createHarness({
|
|
|
421
524
|
};
|
|
422
525
|
}
|
|
423
526
|
|
|
527
|
+
function tokenAccount(mint: string, owner: string): Uint8Array {
|
|
528
|
+
return new TokenAccountBuilder()
|
|
529
|
+
.set_mint(bytesFor(mint))
|
|
530
|
+
.set_owner(bytesFor(owner))
|
|
531
|
+
.set_amount(0n)
|
|
532
|
+
.set_is_frozen(0)
|
|
533
|
+
.build();
|
|
534
|
+
}
|
|
535
|
+
|
|
424
536
|
function market(id: number, baseMint: string, quoteMint: string): ClobTradingMarket {
|
|
425
537
|
return {
|
|
426
538
|
address: address(id),
|
|
427
539
|
orderArena: address(id + 40),
|
|
428
540
|
bidsCbook: address(id + 80),
|
|
429
541
|
asksCbook: address(id + 120),
|
|
542
|
+
exchangeMeta: EXCHANGE_META,
|
|
430
543
|
tokenProgram: TOKEN_PROGRAM,
|
|
431
544
|
baseMint,
|
|
432
545
|
quoteMint,
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
import {
|
|
25
25
|
CLOB_PROGRAM_ADDRESS,
|
|
26
26
|
CLOB_STATUS_FLAG_DEPOSITS_FROZEN,
|
|
27
|
+
CLOB_STATUS_FLAG_PAUSED,
|
|
27
28
|
CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
28
29
|
parseSeatArenaAccount,
|
|
29
30
|
type ClobOrderSide,
|
|
@@ -63,6 +64,7 @@ export interface ClobExchangeAssetSnapshot {
|
|
|
63
64
|
mint: string;
|
|
64
65
|
tokenProgram: string;
|
|
65
66
|
symbol: string | null;
|
|
67
|
+
decimals: number | null;
|
|
66
68
|
discoveryComplete: boolean;
|
|
67
69
|
exchange: {
|
|
68
70
|
freeAmount: bigint;
|
|
@@ -121,6 +123,7 @@ export type ClobExchangeClientErrorCode =
|
|
|
121
123
|
| 'insufficient_wallet_balance'
|
|
122
124
|
| 'insufficient_exchange_balance'
|
|
123
125
|
| 'fragmented_balance'
|
|
126
|
+
| 'market_paused'
|
|
124
127
|
| 'invalid_token_account';
|
|
125
128
|
|
|
126
129
|
export class ClobExchangeClientError extends Error {
|
|
@@ -227,14 +230,15 @@ export function createClobExchangeClient(
|
|
|
227
230
|
const assets = uniqueSnapshotAssets(markets, allocations, authority);
|
|
228
231
|
const snapshots = await Promise.all(assets.map(async ({ mint, tokenProgram }) => {
|
|
229
232
|
const balance = balances.get(assetKey(mint, tokenProgram));
|
|
230
|
-
const [wallet,
|
|
233
|
+
const [wallet, mintMetadata] = await Promise.all([
|
|
231
234
|
readCanonicalWalletAccount(authority, mint, tokenProgram),
|
|
232
|
-
|
|
235
|
+
readMintMetadata(mint),
|
|
233
236
|
]);
|
|
234
237
|
return {
|
|
235
238
|
mint,
|
|
236
239
|
tokenProgram,
|
|
237
|
-
symbol,
|
|
240
|
+
symbol: mintMetadata?.symbol ?? null,
|
|
241
|
+
decimals: mintMetadata?.decimals ?? null,
|
|
238
242
|
discoveryComplete: isAssetDiscoveryComplete(
|
|
239
243
|
unavailableMarkets,
|
|
240
244
|
balance?.allocations ?? [],
|
|
@@ -298,28 +302,41 @@ export function createClobExchangeClient(
|
|
|
298
302
|
);
|
|
299
303
|
const eligible = marketsForAsset(markets, depositArgs.mint, args.tokenProgramAddress);
|
|
300
304
|
if (eligible.length === 0) throw assetNotFound(depositArgs.mint);
|
|
301
|
-
const
|
|
305
|
+
const unpaused = eligible.filter(
|
|
306
|
+
(market) => !(market.statusFlags & CLOB_STATUS_FLAG_PAUSED)
|
|
307
|
+
);
|
|
308
|
+
if (unpaused.length === 0) {
|
|
309
|
+
throw new ClobExchangeClientError(
|
|
310
|
+
'market_paused',
|
|
311
|
+
'no active CLOB market accepts deposits for this asset',
|
|
312
|
+
{ mint: depositArgs.mint }
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
const acceptingDeposits = unpaused.filter(
|
|
302
316
|
(market) => !(market.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN)
|
|
303
317
|
);
|
|
304
318
|
/* Preserve the builder's deposits_frozen error when every compatible
|
|
305
319
|
market is frozen, but never prefer a frozen existing allocation over
|
|
306
320
|
another market that can actually accept the deposit. */
|
|
307
321
|
const market = chooseDepositMarket(
|
|
308
|
-
acceptingDeposits.length > 0 ? acceptingDeposits :
|
|
322
|
+
acceptingDeposits.length > 0 ? acceptingDeposits : unpaused,
|
|
309
323
|
allocations,
|
|
310
324
|
depositArgs.authority,
|
|
311
325
|
depositArgs.mint
|
|
312
326
|
);
|
|
313
327
|
const tokenSide = tokenSideForMint(market, depositArgs.mint);
|
|
314
|
-
const seats = await
|
|
328
|
+
const [seats, wallet] = await Promise.all([
|
|
329
|
+
readMarketSeats(market),
|
|
330
|
+
readCanonicalWalletAccount(
|
|
331
|
+
depositArgs.authority,
|
|
332
|
+
depositArgs.mint,
|
|
333
|
+
args.tokenProgramAddress
|
|
334
|
+
),
|
|
335
|
+
validateMarketVault(market, tokenSide, depositArgs.mint),
|
|
336
|
+
]);
|
|
315
337
|
const existingSeat = seats
|
|
316
338
|
.filter((seat) => seat.seatAuthority === depositArgs.authority)
|
|
317
339
|
.sort((a, b) => a.seatIndex - b.seatIndex)[0] ?? null;
|
|
318
|
-
const wallet = await readCanonicalWalletAccount(
|
|
319
|
-
depositArgs.authority,
|
|
320
|
-
depositArgs.mint,
|
|
321
|
-
args.tokenProgramAddress
|
|
322
|
-
);
|
|
323
340
|
if (wallet.amount < depositArgs.amount) {
|
|
324
341
|
throw new ClobExchangeClientError(
|
|
325
342
|
'insufficient_wallet_balance',
|
|
@@ -566,9 +583,15 @@ export function createClobExchangeClient(
|
|
|
566
583
|
}
|
|
567
584
|
}
|
|
568
585
|
|
|
569
|
-
async function
|
|
586
|
+
async function readMintMetadata(
|
|
587
|
+
mint: string
|
|
588
|
+
): Promise<{ symbol: string | null; decimals: number } | null> {
|
|
570
589
|
try {
|
|
571
|
-
|
|
590
|
+
const parsed = parseMintAccountData(await args.thruClient.accounts.get(mint));
|
|
591
|
+
return {
|
|
592
|
+
symbol: parsed.ticker || null,
|
|
593
|
+
decimals: parsed.decimals,
|
|
594
|
+
};
|
|
572
595
|
} catch (error) {
|
|
573
596
|
if (isAccountNotFoundError(error)) return null;
|
|
574
597
|
return null;
|
|
@@ -579,6 +602,22 @@ export function createClobExchangeClient(
|
|
|
579
602
|
return parseSeatArenaAccount(await args.thruClient.accounts.get(market.address)).seats;
|
|
580
603
|
}
|
|
581
604
|
|
|
605
|
+
async function validateMarketVault(
|
|
606
|
+
market: ClobTradingMarket,
|
|
607
|
+
tokenSide: ClobTokenSide,
|
|
608
|
+
mint: string
|
|
609
|
+
): Promise<void> {
|
|
610
|
+
const vault = tokenSide === 'base' ? market.baseVault : market.quoteVault;
|
|
611
|
+
const parsed = parseTokenAccountData(await args.thruClient.accounts.get(vault));
|
|
612
|
+
if (parsed.mint !== mint || parsed.owner !== clobProgramAddress) {
|
|
613
|
+
throw new ClobExchangeClientError(
|
|
614
|
+
'invalid_token_account',
|
|
615
|
+
'selected market vault does not match the requested mint and CLOB program',
|
|
616
|
+
{ market: market.address, vault, expectedMint: mint, actualMint: parsed.mint }
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
582
621
|
async function wrapWithWalletTokenAccountInitialization(initArgs: {
|
|
583
622
|
authority: string;
|
|
584
623
|
mint: string;
|
|
@@ -30,6 +30,7 @@ const TOKEN_PROGRAM = address(2);
|
|
|
30
30
|
const BASE_MINT = address(3);
|
|
31
31
|
const QUOTE_MINT = address(4);
|
|
32
32
|
const WALLET_QUOTE = address(5);
|
|
33
|
+
const EXCHANGE_META = address(250);
|
|
33
34
|
|
|
34
35
|
describe('CLOB exchange balance SDK', () => {
|
|
35
36
|
it('aggregates compatible physical allocations without losing transferability', () => {
|
|
@@ -569,10 +570,10 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
569
570
|
|
|
570
571
|
expect([
|
|
571
572
|
bytes[0],
|
|
572
|
-
bytes[
|
|
573
|
-
bytes[
|
|
574
|
-
bytes[
|
|
575
|
-
bytes[
|
|
573
|
+
bytes[32],
|
|
574
|
+
bytes[56],
|
|
575
|
+
bytes[88],
|
|
576
|
+
bytes[112],
|
|
576
577
|
]).toEqual([
|
|
577
578
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
578
579
|
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
@@ -581,12 +582,12 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
581
582
|
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
582
583
|
]);
|
|
583
584
|
expect(bytes[1]).toBe(1);
|
|
584
|
-
expect(bytes[
|
|
585
|
-
expect(bytes[
|
|
586
|
-
expect(bytes[
|
|
587
|
-
expect(bytes[
|
|
588
|
-
expect(readU16(bytes,
|
|
589
|
-
expect(readU16(bytes,
|
|
585
|
+
expect(bytes[33]).toBe(1);
|
|
586
|
+
expect(bytes[57]).toBe(2);
|
|
587
|
+
expect(bytes[89]).toBe(2);
|
|
588
|
+
expect(bytes[114]).toBe(2);
|
|
589
|
+
expect(readU16(bytes, 24)).toBe(0xffff);
|
|
590
|
+
expect(readU16(bytes, 80)).toBe(0xffff);
|
|
590
591
|
expect(instructionPlan.readWriteAccounts).toContain(source.address);
|
|
591
592
|
expect(instructionPlan.readWriteAccounts).toContain(source.orderArena);
|
|
592
593
|
expect(instructionPlan.readWriteAccounts).toContain(source.bidsCbook);
|
|
@@ -638,7 +639,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
638
639
|
});
|
|
639
640
|
const bytes = await instructionPlan.instructionData(contextForPlan(instructionPlan));
|
|
640
641
|
|
|
641
|
-
expect([bytes[0], bytes[
|
|
642
|
+
expect([bytes[0], bytes[32], bytes[56], bytes[88], bytes[112]]).toEqual([
|
|
642
643
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
643
644
|
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
644
645
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
@@ -646,7 +647,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
646
647
|
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
647
648
|
]);
|
|
648
649
|
expect(readU32(bytes, 12)).toBe(3);
|
|
649
|
-
expect(readU32(bytes,
|
|
650
|
+
expect(readU32(bytes, 68)).toBe(2);
|
|
650
651
|
expect(instructionPlan.readWriteAccounts.filter((account) => account === target.address))
|
|
651
652
|
.toHaveLength(1);
|
|
652
653
|
});
|
|
@@ -691,7 +692,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
691
692
|
});
|
|
692
693
|
const bytes = await instructionPlan.instructionData(contextForPlan(instructionPlan));
|
|
693
694
|
|
|
694
|
-
expect([bytes[0], bytes[
|
|
695
|
+
expect([bytes[0], bytes[32], bytes[56], bytes[88], bytes[112], bytes[144], bytes[152], bytes[176]]).toEqual([
|
|
695
696
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
696
697
|
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
697
698
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
@@ -701,7 +702,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
701
702
|
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
702
703
|
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
703
704
|
]);
|
|
704
|
-
expect([bytes[1], bytes[
|
|
705
|
+
expect([bytes[1], bytes[33], bytes[57], bytes[89], bytes[113], bytes[153], bytes[178]])
|
|
705
706
|
.toEqual([1, 1, 2, 2, 3, 3, 3]);
|
|
706
707
|
expect(instructionPlan.review.value.sourceMarketCount).toBe('2');
|
|
707
708
|
expect(instructionPlan.review.value.exchangeAmount).toBe('300');
|
|
@@ -744,7 +745,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
744
745
|
seatIndex: null,
|
|
745
746
|
});
|
|
746
747
|
const depositBytes = await deposit.instructionData(contextForPlan(deposit));
|
|
747
|
-
expect([depositBytes[0], depositBytes[
|
|
748
|
+
expect([depositBytes[0], depositBytes[32], depositBytes[40]]).toEqual([
|
|
748
749
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
749
750
|
CLOB_INSTRUCTION_SEAT_CREATE,
|
|
750
751
|
CLOB_INSTRUCTION_TOKEN_DEPOSIT,
|
|
@@ -760,7 +761,7 @@ describe('CLOB exchange balance SDK', () => {
|
|
|
760
761
|
seatIndex: 2,
|
|
761
762
|
});
|
|
762
763
|
const withdrawalBytes = await withdrawal.instructionData(contextForPlan(withdrawal));
|
|
763
|
-
expect([withdrawalBytes[0], withdrawalBytes[
|
|
764
|
+
expect([withdrawalBytes[0], withdrawalBytes[32]]).toEqual([
|
|
764
765
|
CLOB_INSTRUCTION_MARKET_RECORD,
|
|
765
766
|
CLOB_INSTRUCTION_TOKEN_WITHDRAW,
|
|
766
767
|
]);
|
|
@@ -825,6 +826,7 @@ function market(
|
|
|
825
826
|
): ClobTradingMarket {
|
|
826
827
|
return {
|
|
827
828
|
address: address(id),
|
|
829
|
+
exchangeMeta: EXCHANGE_META,
|
|
828
830
|
orderArena: address(id + 1),
|
|
829
831
|
bidsCbook: address(id + 2),
|
|
830
832
|
asksCbook: address(id + 3),
|
package/src/clob/exchange.ts
CHANGED
|
@@ -48,6 +48,7 @@ export interface ClobExchangeAssetBalance {
|
|
|
48
48
|
|
|
49
49
|
export interface ClobTradingMarket {
|
|
50
50
|
address: string;
|
|
51
|
+
exchangeMeta: string;
|
|
51
52
|
orderArena: string;
|
|
52
53
|
bidsCbook: string;
|
|
53
54
|
asksCbook: string;
|
|
@@ -775,6 +776,7 @@ function marketRecordInstruction(
|
|
|
775
776
|
asksCbookAccountBytes: addressBytes(market.asksCbook),
|
|
776
777
|
seatAuthorityAccountBytes: authority ? addressBytes(authority) : undefined,
|
|
777
778
|
seatIndex: seatIndex ?? undefined,
|
|
779
|
+
exchangeMetaAccountBytes: addressBytes(market.exchangeMeta),
|
|
778
780
|
tokenProgramAccountBytes: addressBytes(market.tokenProgram),
|
|
779
781
|
baseVaultAccountBytes: addressBytes(market.baseVault),
|
|
780
782
|
quoteVaultAccountBytes: addressBytes(market.quoteVault),
|
|
@@ -788,6 +790,7 @@ function addTargetMarketAccounts(accounts: AccountPlan, market: ClobTradingMarke
|
|
|
788
790
|
accounts.addWrite(market.asksCbook);
|
|
789
791
|
accounts.addRead(market.baseVault);
|
|
790
792
|
accounts.addRead(market.quoteVault);
|
|
793
|
+
accounts.addRead(market.exchangeMeta);
|
|
791
794
|
}
|
|
792
795
|
|
|
793
796
|
function addSourceMarketAccounts(
|
|
@@ -806,6 +809,7 @@ function addSourceMarketAccounts(
|
|
|
806
809
|
accounts.addWrite(market.asksCbook);
|
|
807
810
|
accounts.addRead(market.baseVault);
|
|
808
811
|
accounts.addRead(market.quoteVault);
|
|
812
|
+
accounts.addRead(market.exchangeMeta);
|
|
809
813
|
}
|
|
810
814
|
|
|
811
815
|
class AccountPlan {
|
package/src/clob/index.test.ts
CHANGED
|
@@ -5,6 +5,13 @@ import {
|
|
|
5
5
|
CLOB_NULL_INDEX,
|
|
6
6
|
CLOB_INSTRUCTION_CREATE_ORDER_ENTRY,
|
|
7
7
|
CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY,
|
|
8
|
+
CLOB_INSTRUCTION_EXCHANGE_INITIALIZE,
|
|
9
|
+
CLOB_INSTRUCTION_EXCHANGE_SET_STATUS,
|
|
10
|
+
CLOB_INSTRUCTION_EXCHANGE_SET_ADMIN,
|
|
11
|
+
CLOB_INSTRUCTION_EXCHANGE_RECOVER_ADMIN,
|
|
12
|
+
CLOB_INSTRUCTION_MARKET_SET_EXCHANGE_STATUS,
|
|
13
|
+
CLOB_EXCHANGE_META_MAGIC,
|
|
14
|
+
CLOB_EXCHANGE_META_VERSION,
|
|
8
15
|
CLOB_CBOOK_MAX_PRICE_IN_TICKS,
|
|
9
16
|
CLOB_EVENT_SIDE_BUY,
|
|
10
17
|
CLOB_ORDER_FLAG_BUY,
|
|
@@ -13,18 +20,30 @@ import {
|
|
|
13
20
|
CLOB_ORDER_TYPE_GTC,
|
|
14
21
|
CLOB_ORDER_TYPE_IOC,
|
|
15
22
|
CLOB_ORDER_TYPE_MTL,
|
|
23
|
+
CLOB_STATUS_FLAG_DEPOSITS_FROZEN,
|
|
24
|
+
CLOB_STATUS_FLAG_PAUSED,
|
|
25
|
+
CLOB_STATUS_FLAG_POST_ONLY,
|
|
26
|
+
CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN,
|
|
16
27
|
ClobEventBuilder,
|
|
28
|
+
ExchangeMetaAccountBuilder,
|
|
17
29
|
MarketAccountBuilder,
|
|
18
30
|
MarketCreatedEventBuilder,
|
|
19
31
|
OrderEntryBuilder,
|
|
20
32
|
OrderFilledEventBuilder,
|
|
21
33
|
SeatEntryBuilder,
|
|
22
34
|
buildOrderBookSnapshot,
|
|
35
|
+
createExchangeInitializeInstruction,
|
|
36
|
+
createExchangeSetStatusInstruction,
|
|
37
|
+
createExchangeSetAdminInstruction,
|
|
38
|
+
createExchangeRecoverAdminInstruction,
|
|
39
|
+
createMarketSetExchangeStatusInstruction,
|
|
23
40
|
createMarketRecordInstruction,
|
|
24
41
|
createOrderEntryInstruction,
|
|
25
42
|
createSeatlessOrderEntryInstruction,
|
|
43
|
+
effectiveClobStatusFlags,
|
|
26
44
|
encodeOrderFlags,
|
|
27
45
|
parseClobEvent,
|
|
46
|
+
parseExchangeMetaAccount,
|
|
28
47
|
parseMarketAccount,
|
|
29
48
|
parseOrderArenaAccount,
|
|
30
49
|
parseSeatArenaAccount,
|
|
@@ -48,16 +67,17 @@ function context(indexes: Record<string, number>) {
|
|
|
48
67
|
|
|
49
68
|
describe('clob helpers', () => {
|
|
50
69
|
it('validates market-record seat indices before ABI serialization', async () => {
|
|
51
|
-
const accounts = Array.from({ length:
|
|
70
|
+
const accounts = Array.from({ length: 8 }, (_, index) => key(index + 1));
|
|
52
71
|
const args = {
|
|
53
72
|
marketRecordIndex: 1,
|
|
54
73
|
seatArenaAccountBytes: accounts[0],
|
|
55
74
|
orderArenaAccountBytes: accounts[1],
|
|
56
75
|
bidsCbookAccountBytes: accounts[2],
|
|
57
76
|
asksCbookAccountBytes: accounts[3],
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
exchangeMetaAccountBytes: accounts[4],
|
|
78
|
+
tokenProgramAccountBytes: accounts[5],
|
|
79
|
+
baseVaultAccountBytes: accounts[6],
|
|
80
|
+
quoteVaultAccountBytes: accounts[7],
|
|
61
81
|
};
|
|
62
82
|
const lookup = context(Object.fromEntries(
|
|
63
83
|
accounts.map((account, index) => [bytesToHex(account), index + 2])
|
|
@@ -76,6 +96,113 @@ describe('clob helpers', () => {
|
|
|
76
96
|
}
|
|
77
97
|
});
|
|
78
98
|
|
|
99
|
+
it('builds and parses exchange metadata instructions and accounts', async () => {
|
|
100
|
+
const exchangeMeta = key(1);
|
|
101
|
+
const programMeta = key(2);
|
|
102
|
+
const authority = key(3);
|
|
103
|
+
const tokenProgram = key(4);
|
|
104
|
+
const exchangeAdmin = key(5);
|
|
105
|
+
const proof = new Uint8Array([6, 7, 8]);
|
|
106
|
+
const instruction = await createExchangeInitializeInstruction({
|
|
107
|
+
exchangeMetaAccountBytes: exchangeMeta,
|
|
108
|
+
programMetaAccountBytes: programMeta,
|
|
109
|
+
authorityAccountBytes: authority,
|
|
110
|
+
tokenProgramAccountBytes: tokenProgram,
|
|
111
|
+
exchangeAdminAccountBytes: exchangeAdmin,
|
|
112
|
+
stateProof: proof,
|
|
113
|
+
})(context({
|
|
114
|
+
[bytesToHex(exchangeMeta)]: 10,
|
|
115
|
+
[bytesToHex(programMeta)]: 11,
|
|
116
|
+
[bytesToHex(authority)]: 12,
|
|
117
|
+
[bytesToHex(tokenProgram)]: 13,
|
|
118
|
+
[bytesToHex(exchangeAdmin)]: 14,
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
expect(instruction[0]).toBe(CLOB_INSTRUCTION_EXCHANGE_INITIALIZE);
|
|
122
|
+
const view = new DataView(instruction.buffer, instruction.byteOffset, instruction.byteLength);
|
|
123
|
+
expect(view.getUint16(2, true)).toBe(10);
|
|
124
|
+
expect(view.getUint16(4, true)).toBe(11);
|
|
125
|
+
expect(view.getUint16(6, true)).toBe(12);
|
|
126
|
+
expect(view.getUint16(8, true)).toBe(13);
|
|
127
|
+
expect(view.getUint16(10, true)).toBe(14);
|
|
128
|
+
expect(view.getUint32(12, true)).toBe(proof.length);
|
|
129
|
+
expect(instruction.slice(16)).toEqual(proof);
|
|
130
|
+
|
|
131
|
+
const data = new ExchangeMetaAccountBuilder()
|
|
132
|
+
.set_magic(CLOB_EXCHANGE_META_MAGIC)
|
|
133
|
+
.set_version(CLOB_EXCHANGE_META_VERSION)
|
|
134
|
+
.set_status_flags(3)
|
|
135
|
+
.set_reserved0([0, 0, 0, 0, 0, 0])
|
|
136
|
+
.set_token_program_pubkey(tokenProgram)
|
|
137
|
+
.set_exchange_admin_pubkey(exchangeAdmin)
|
|
138
|
+
.set_reserved1(new Uint8Array(16))
|
|
139
|
+
.build();
|
|
140
|
+
expect(parseExchangeMetaAccount(data)).toEqual({
|
|
141
|
+
magic: CLOB_EXCHANGE_META_MAGIC,
|
|
142
|
+
version: CLOB_EXCHANGE_META_VERSION,
|
|
143
|
+
statusFlags: 3,
|
|
144
|
+
tokenProgram: bytesToAddress(tokenProgram),
|
|
145
|
+
exchangeAdmin: bytesToAddress(exchangeAdmin),
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('builds exchange administration instructions', async () => {
|
|
150
|
+
const exchangeMeta = key(1);
|
|
151
|
+
const admin = key(2);
|
|
152
|
+
const nextAdmin = key(3);
|
|
153
|
+
const programMeta = key(4);
|
|
154
|
+
const lookup = context({
|
|
155
|
+
[bytesToHex(exchangeMeta)]: 10,
|
|
156
|
+
[bytesToHex(admin)]: 11,
|
|
157
|
+
[bytesToHex(nextAdmin)]: 12,
|
|
158
|
+
[bytesToHex(programMeta)]: 13,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const setStatus = await createExchangeSetStatusInstruction({
|
|
162
|
+
exchangeMetaAccountBytes: exchangeMeta,
|
|
163
|
+
exchangeAdminAccountBytes: admin,
|
|
164
|
+
statusFlags: 5,
|
|
165
|
+
})(lookup);
|
|
166
|
+
const setAdmin = await createExchangeSetAdminInstruction({
|
|
167
|
+
exchangeMetaAccountBytes: exchangeMeta,
|
|
168
|
+
authorityAccountBytes: admin,
|
|
169
|
+
newAdminAccountBytes: nextAdmin,
|
|
170
|
+
})(lookup);
|
|
171
|
+
const recoverAdmin = await createExchangeRecoverAdminInstruction({
|
|
172
|
+
exchangeMetaAccountBytes: exchangeMeta,
|
|
173
|
+
programMetaAccountBytes: programMeta,
|
|
174
|
+
authorityAccountBytes: admin,
|
|
175
|
+
newAdminAccountBytes: nextAdmin,
|
|
176
|
+
})(lookup);
|
|
177
|
+
const marketOverride = await createMarketSetExchangeStatusInstruction({
|
|
178
|
+
marketRecordIndex: 7,
|
|
179
|
+
exchangeAdminAccountBytes: admin,
|
|
180
|
+
statusFlags: 2,
|
|
181
|
+
})(lookup);
|
|
182
|
+
|
|
183
|
+
expect(setStatus[0]).toBe(CLOB_INSTRUCTION_EXCHANGE_SET_STATUS);
|
|
184
|
+
expect(setAdmin[0]).toBe(CLOB_INSTRUCTION_EXCHANGE_SET_ADMIN);
|
|
185
|
+
expect(recoverAdmin[0]).toBe(CLOB_INSTRUCTION_EXCHANGE_RECOVER_ADMIN);
|
|
186
|
+
expect(marketOverride[0]).toBe(CLOB_INSTRUCTION_MARKET_SET_EXCHANGE_STATUS);
|
|
187
|
+
await expect(createExchangeSetStatusInstruction({
|
|
188
|
+
exchangeMetaAccountBytes: exchangeMeta,
|
|
189
|
+
exchangeAdminAccountBytes: admin,
|
|
190
|
+
statusFlags: 16,
|
|
191
|
+
})(lookup)).rejects.toThrow(/statusFlags/);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('combines global, exchange-admin, and market-authority status flags', () => {
|
|
195
|
+
expect(effectiveClobStatusFlags(
|
|
196
|
+
CLOB_STATUS_FLAG_PAUSED,
|
|
197
|
+
{
|
|
198
|
+
statusFlags: CLOB_STATUS_FLAG_POST_ONLY,
|
|
199
|
+
exchangeStatusFlags: CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN | CLOB_STATUS_FLAG_DEPOSITS_FROZEN,
|
|
200
|
+
},
|
|
201
|
+
)).toBe(15);
|
|
202
|
+
expect(() => effectiveClobStatusFlags(16, { statusFlags: 0, exchangeStatusFlags: 0 }))
|
|
203
|
+
.toThrow(/statusFlags/);
|
|
204
|
+
});
|
|
205
|
+
|
|
79
206
|
it('encodes order flags with C program bit semantics', () => {
|
|
80
207
|
expect(encodeOrderFlags({ side: 'buy', orderType: 'gtc' })).toBe(
|
|
81
208
|
CLOB_ORDER_FLAG_BUY | (CLOB_ORDER_TYPE_GTC << 1)
|
|
@@ -134,14 +261,14 @@ describe('clob helpers', () => {
|
|
|
134
261
|
const data = new MarketAccountBuilder()
|
|
135
262
|
.set_magic(0xc1)
|
|
136
263
|
.set_status_flags(3)
|
|
137
|
-
.
|
|
264
|
+
.set_exchange_status_flags(4)
|
|
265
|
+
.set_reserved0([0, 0, 0, 0, 0])
|
|
138
266
|
.set_lot_size(100n)
|
|
139
267
|
.set_tick_size(5n)
|
|
140
268
|
.set_next_order_id(42n)
|
|
141
269
|
.set_order_entry_pubkey(key(10))
|
|
142
270
|
.set_bids_cbook_pubkey(key(11))
|
|
143
271
|
.set_asks_cbook_pubkey(key(12))
|
|
144
|
-
.set_token_program_pubkey(key(16))
|
|
145
272
|
.set_base_vault_pubkey(key(13))
|
|
146
273
|
.set_quote_vault_pubkey(key(14))
|
|
147
274
|
.set_market_authority_pubkey(key(15))
|
|
@@ -150,11 +277,11 @@ describe('clob helpers', () => {
|
|
|
150
277
|
const market = parseMarketAccount(data);
|
|
151
278
|
expect(market.magic).toBe(0xc1);
|
|
152
279
|
expect(market.statusFlags).toBe(3);
|
|
280
|
+
expect(market.exchangeStatusFlags).toBe(4);
|
|
153
281
|
expect(market.lotSize).toBe(100n);
|
|
154
282
|
expect(market.tickSize).toBe(5n);
|
|
155
283
|
expect(market.nextOrderId).toBe(42n);
|
|
156
284
|
expect(market.bidsCbook).not.toBe(market.asksCbook);
|
|
157
|
-
expect(market.tokenProgram).toBe(bytesToAddress(key(16)));
|
|
158
285
|
});
|
|
159
286
|
|
|
160
287
|
it('parses order arenas and groups active orderbook depth', () => {
|