@thru/programs 0.2.39 → 0.3.1
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 +3336 -16
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +143 -2
- package/dist/clob/index.d.ts +143 -2
- package/dist/clob/index.js +612 -14
- package/dist/clob/index.js.map +1 -1
- package/dist/multicall/index.js +1 -2712
- package/dist/multicall/index.js.map +1 -1
- package/package.json +2 -2
- package/src/clob/exchange.test.ts +901 -0
- package/src/clob/exchange.ts +939 -0
- package/src/clob/index.test.ts +32 -0
- package/src/clob/index.ts +16 -3
package/dist/clob/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { MULTICALL_PROGRAM_ADDRESS, buildMulticallInstruction } from '../chunk-J7B7XYOI.js';
|
|
1
2
|
import { Pubkey as Pubkey$1 } from '@thru/sdk';
|
|
2
3
|
import { encodeAddress } from '@thru/sdk/helpers';
|
|
3
4
|
|
|
4
|
-
// src/clob/index.ts
|
|
5
|
-
|
|
6
5
|
// src/clob/abi/thru/common/primitives/types.ts
|
|
7
6
|
var __tnWarnings = /* @__PURE__ */ new Set();
|
|
8
7
|
var __tnHasNativeBigInt = typeof BigInt === "function";
|
|
@@ -11025,6 +11024,598 @@ __tnRegisterDynamicValidate2("ClobProgramAccount", (buffer) => {
|
|
|
11025
11024
|
const params = result.params;
|
|
11026
11025
|
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt2(result.consumed), params };
|
|
11027
11026
|
});
|
|
11027
|
+
var CLOB_MAX_ATOMIC_FUNDING_SOURCES = 64;
|
|
11028
|
+
var ClobExchangeError = class extends Error {
|
|
11029
|
+
constructor(code, message) {
|
|
11030
|
+
super(message);
|
|
11031
|
+
this.name = "ClobExchangeError";
|
|
11032
|
+
this.code = code;
|
|
11033
|
+
}
|
|
11034
|
+
};
|
|
11035
|
+
function aggregateClobExchangeBalances(allocations) {
|
|
11036
|
+
const groups = /* @__PURE__ */ new Map();
|
|
11037
|
+
for (const allocation of allocations) {
|
|
11038
|
+
validateAllocation(allocation);
|
|
11039
|
+
if (!allocation.metadataReady || !allocation.mint || allocation.freeAmount === 0n) continue;
|
|
11040
|
+
const key = allocation.authority + "\0" + allocation.mint + "\0" + allocation.tokenProgram;
|
|
11041
|
+
let balance = groups.get(key);
|
|
11042
|
+
if (!balance) {
|
|
11043
|
+
balance = {
|
|
11044
|
+
authority: allocation.authority,
|
|
11045
|
+
mint: allocation.mint,
|
|
11046
|
+
tokenProgram: allocation.tokenProgram,
|
|
11047
|
+
freeAmount: 0n,
|
|
11048
|
+
transferableAmount: 0n,
|
|
11049
|
+
allocations: []
|
|
11050
|
+
};
|
|
11051
|
+
groups.set(key, balance);
|
|
11052
|
+
}
|
|
11053
|
+
balance.freeAmount += allocation.freeAmount;
|
|
11054
|
+
if (!(allocation.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN)) {
|
|
11055
|
+
balance.transferableAmount += allocation.freeAmount;
|
|
11056
|
+
}
|
|
11057
|
+
balance.allocations.push(allocation);
|
|
11058
|
+
}
|
|
11059
|
+
return [...groups.values()].map((balance) => ({
|
|
11060
|
+
...balance,
|
|
11061
|
+
allocations: balance.allocations.slice().sort(compareAllocationIdentity)
|
|
11062
|
+
})).sort((a, b) => compareText(a.mint, b.mint) || compareText(a.tokenProgram, b.tokenProgram));
|
|
11063
|
+
}
|
|
11064
|
+
function calculateClobOrderFundingAmount(args) {
|
|
11065
|
+
assertPositiveU64(args.price, "price");
|
|
11066
|
+
assertPositiveU64(args.quantity, "quantity");
|
|
11067
|
+
const amount = args.side === "buy" ? args.price * args.quantity : args.quantity;
|
|
11068
|
+
if (amount > 0xffffffffffffffffn) {
|
|
11069
|
+
throw new ClobExchangeError(
|
|
11070
|
+
"invalid_funding_amount",
|
|
11071
|
+
"order funding amount exceeds the CLOB u64 balance range"
|
|
11072
|
+
);
|
|
11073
|
+
}
|
|
11074
|
+
return amount;
|
|
11075
|
+
}
|
|
11076
|
+
function planClobOrderFunding(args) {
|
|
11077
|
+
assertMarketCanTrade(args.targetMarket, args.orderType ?? "gtc");
|
|
11078
|
+
const targetTokenSide = args.side === "buy" ? "quote" : "base";
|
|
11079
|
+
const mint = targetTokenSide === "base" ? args.targetMarket.baseMint : args.targetMarket.quoteMint;
|
|
11080
|
+
if (!mint) {
|
|
11081
|
+
throw new ClobExchangeError(
|
|
11082
|
+
"funding_plan_mismatch",
|
|
11083
|
+
"target market is missing " + targetTokenSide + " mint metadata"
|
|
11084
|
+
);
|
|
11085
|
+
}
|
|
11086
|
+
const requiredAmount = calculateClobOrderFundingAmount(args);
|
|
11087
|
+
const compatible = args.allocations.filter((allocation) => {
|
|
11088
|
+
validateAllocation(allocation);
|
|
11089
|
+
return allocation.authority === args.authority && allocation.metadataReady && allocation.mint === mint && allocation.tokenProgram === args.targetMarket.tokenProgram;
|
|
11090
|
+
});
|
|
11091
|
+
assertUniqueAllocationIdentities(compatible);
|
|
11092
|
+
const targetAllocations = compatible.filter((allocation) => allocation.market === args.targetMarket.address && allocation.tokenSide === targetTokenSide && allocation.vault === vaultForSide(args.targetMarket, targetTokenSide)).sort(compareTargetAllocation);
|
|
11093
|
+
const target = args.targetSeatIndex === void 0 ? targetAllocations[0] ?? null : targetAllocations.find((allocation) => allocation.seatIndex === args.targetSeatIndex) ?? null;
|
|
11094
|
+
if (args.targetSeatIndex !== void 0 && !target) {
|
|
11095
|
+
throw new ClobExchangeError(
|
|
11096
|
+
"funding_plan_mismatch",
|
|
11097
|
+
"requested target seat is not present in the indexed allocations"
|
|
11098
|
+
);
|
|
11099
|
+
}
|
|
11100
|
+
const targetAmount = minBigInt(requiredAmount, target?.freeAmount ?? 0n);
|
|
11101
|
+
let remaining = requiredAmount - targetAmount;
|
|
11102
|
+
const targetAllocationIdentity = target ? sourceAllocationIdentity(target) : null;
|
|
11103
|
+
const sourceCandidates = compatible.filter(
|
|
11104
|
+
(allocation) => sourceAllocationIdentity(allocation) !== targetAllocationIdentity && allocation.freeAmount > 0n && !(allocation.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN)
|
|
11105
|
+
);
|
|
11106
|
+
const sources = [];
|
|
11107
|
+
for (const allocation of sourceCandidates) {
|
|
11108
|
+
if (remaining === 0n) break;
|
|
11109
|
+
if (sources.length >= CLOB_MAX_ATOMIC_FUNDING_SOURCES) {
|
|
11110
|
+
throw new ClobExchangeError(
|
|
11111
|
+
"too_many_funding_sources",
|
|
11112
|
+
"exchange balance is fragmented across more than " + CLOB_MAX_ATOMIC_FUNDING_SOURCES.toString() + " source allocations for one atomic order"
|
|
11113
|
+
);
|
|
11114
|
+
}
|
|
11115
|
+
const amount = minBigInt(remaining, allocation.freeAmount);
|
|
11116
|
+
sources.push({ allocation, amount });
|
|
11117
|
+
remaining -= amount;
|
|
11118
|
+
}
|
|
11119
|
+
if (requiredAmount > targetAmount && args.targetMarket.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
11120
|
+
throw new ClobExchangeError(
|
|
11121
|
+
"deposits_frozen",
|
|
11122
|
+
"target market deposits are frozen and its existing allocation cannot fund the order"
|
|
11123
|
+
);
|
|
11124
|
+
}
|
|
11125
|
+
return {
|
|
11126
|
+
authority: args.authority,
|
|
11127
|
+
targetMarket: args.targetMarket.address,
|
|
11128
|
+
targetTokenSide,
|
|
11129
|
+
targetSeatIndex: target?.seatIndex ?? null,
|
|
11130
|
+
mint,
|
|
11131
|
+
tokenProgram: args.targetMarket.tokenProgram,
|
|
11132
|
+
requiredAmount,
|
|
11133
|
+
targetAmount,
|
|
11134
|
+
exchangeAvailableAmount: (target?.freeAmount ?? 0n) + sourceCandidates.reduce(
|
|
11135
|
+
(sum, allocation) => sum + allocation.freeAmount,
|
|
11136
|
+
0n
|
|
11137
|
+
),
|
|
11138
|
+
sources,
|
|
11139
|
+
shortfall: remaining
|
|
11140
|
+
};
|
|
11141
|
+
}
|
|
11142
|
+
function validateClobOrderFundingPlanSeats(args) {
|
|
11143
|
+
if (args.plan.targetSeatIndex === null) {
|
|
11144
|
+
if (args.targetSeats.some((seat) => seat.seatAuthority === args.plan.authority)) {
|
|
11145
|
+
throw new ClobExchangeError("stale_balance", "a target seat appeared after funding was planned");
|
|
11146
|
+
}
|
|
11147
|
+
} else {
|
|
11148
|
+
const seat = args.targetSeats.find(
|
|
11149
|
+
(entry) => entry.seatIndex === args.plan.targetSeatIndex && entry.seatAuthority === args.plan.authority
|
|
11150
|
+
);
|
|
11151
|
+
const currentAmount = seat ? seatAmount(seat, args.plan.targetTokenSide) : null;
|
|
11152
|
+
if (currentAmount === null || currentAmount < args.plan.targetAmount || args.plan.sources.length > 0 && currentAmount !== args.plan.targetAmount) {
|
|
11153
|
+
throw new ClobExchangeError("stale_balance", "target seat balance changed after funding was planned");
|
|
11154
|
+
}
|
|
11155
|
+
}
|
|
11156
|
+
const hasExternalSources = args.plan.sources.some(
|
|
11157
|
+
(source) => source.allocation.market !== args.plan.targetMarket
|
|
11158
|
+
);
|
|
11159
|
+
if (hasExternalSources && !args.sourceSeatsByMarket) {
|
|
11160
|
+
throw new ClobExchangeError(
|
|
11161
|
+
"funding_plan_mismatch",
|
|
11162
|
+
"fresh source seat data is required for every planned funding market"
|
|
11163
|
+
);
|
|
11164
|
+
}
|
|
11165
|
+
for (const source of args.plan.sources) {
|
|
11166
|
+
const isTargetMarketSource = source.allocation.market === args.plan.targetMarket;
|
|
11167
|
+
if (!isTargetMarketSource && !Object.prototype.hasOwnProperty.call(args.sourceSeatsByMarket, source.allocation.market)) {
|
|
11168
|
+
throw new ClobExchangeError(
|
|
11169
|
+
"funding_plan_mismatch",
|
|
11170
|
+
"fresh source seat data is missing for market " + source.allocation.market
|
|
11171
|
+
);
|
|
11172
|
+
}
|
|
11173
|
+
const sourceSeats = isTargetMarketSource ? args.targetSeats : args.sourceSeatsByMarket[source.allocation.market];
|
|
11174
|
+
const seat = sourceSeats.find(
|
|
11175
|
+
(entry) => entry.seatIndex === source.allocation.seatIndex && entry.seatAuthority === args.plan.authority
|
|
11176
|
+
);
|
|
11177
|
+
if (!seat || seatAmount(seat, source.allocation.tokenSide) < source.amount) {
|
|
11178
|
+
throw new ClobExchangeError(
|
|
11179
|
+
"stale_balance",
|
|
11180
|
+
"source seat balance changed after funding was planned for market " + source.allocation.market
|
|
11181
|
+
);
|
|
11182
|
+
}
|
|
11183
|
+
}
|
|
11184
|
+
}
|
|
11185
|
+
function buildClobExchangeDepositInstructionPlan(args) {
|
|
11186
|
+
assertPositiveU64(args.amount, "amount");
|
|
11187
|
+
if (args.market.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
11188
|
+
throw new ClobExchangeError("deposits_frozen", "market deposits are frozen");
|
|
11189
|
+
}
|
|
11190
|
+
const marketRecordIndex = 1;
|
|
11191
|
+
const opensSeat = args.seatIndex === null;
|
|
11192
|
+
const instructions = [
|
|
11193
|
+
marketRecordInstruction(args.market, marketRecordIndex, opensSeat ? null : args.authority, args.seatIndex),
|
|
11194
|
+
...opensSeat ? [createSeatCreateInstruction({
|
|
11195
|
+
marketRecordIndex,
|
|
11196
|
+
seatAuthorityAccountBytes: addressBytes(args.authority)
|
|
11197
|
+
})] : [],
|
|
11198
|
+
createTokenDepositInstruction({
|
|
11199
|
+
marketRecordIndex,
|
|
11200
|
+
tokenProgramAccountBytes: addressBytes(args.market.tokenProgram),
|
|
11201
|
+
fromAccountBytes: addressBytes(args.walletTokenAccount),
|
|
11202
|
+
toAccountBytes: addressBytes(vaultForSide(args.market, args.tokenSide)),
|
|
11203
|
+
amount: args.amount
|
|
11204
|
+
})
|
|
11205
|
+
];
|
|
11206
|
+
const accounts = new AccountPlan();
|
|
11207
|
+
addTargetMarketAccounts(accounts, args.market);
|
|
11208
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
11209
|
+
accounts.addWrite(vaultForSide(args.market, args.tokenSide));
|
|
11210
|
+
accounts.addRead(args.authority);
|
|
11211
|
+
accounts.addRead(args.market.tokenProgram);
|
|
11212
|
+
return accounts.finish(concatClobInstructions(...instructions), {
|
|
11213
|
+
instruction: "exchange_deposit",
|
|
11214
|
+
label: "Deposit to CLOB exchange",
|
|
11215
|
+
value: {
|
|
11216
|
+
market: args.market.address,
|
|
11217
|
+
mint: mintForSide(args.market, args.tokenSide),
|
|
11218
|
+
amount: args.amount.toString()
|
|
11219
|
+
}
|
|
11220
|
+
});
|
|
11221
|
+
}
|
|
11222
|
+
function buildClobExchangeWithdrawalInstructionPlan(args) {
|
|
11223
|
+
assertPositiveU64(args.amount, "amount");
|
|
11224
|
+
if (args.market.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN) {
|
|
11225
|
+
throw new ClobExchangeError("withdrawals_frozen", "market withdrawals are frozen");
|
|
11226
|
+
}
|
|
11227
|
+
const marketRecordIndex = 1;
|
|
11228
|
+
const vault = vaultForSide(args.market, args.tokenSide);
|
|
11229
|
+
const instructionData = concatClobInstructions(
|
|
11230
|
+
marketRecordInstruction(args.market, marketRecordIndex, args.authority, args.seatIndex),
|
|
11231
|
+
createTokenWithdrawInstruction({
|
|
11232
|
+
marketRecordIndex,
|
|
11233
|
+
tokenProgramAccountBytes: addressBytes(args.market.tokenProgram),
|
|
11234
|
+
fromAccountBytes: addressBytes(vault),
|
|
11235
|
+
toAccountBytes: addressBytes(args.walletTokenAccount),
|
|
11236
|
+
amount: args.amount
|
|
11237
|
+
})
|
|
11238
|
+
);
|
|
11239
|
+
const accounts = new AccountPlan();
|
|
11240
|
+
addSourceMarketAccounts(accounts, args.market, vault);
|
|
11241
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
11242
|
+
accounts.addRead(args.authority);
|
|
11243
|
+
accounts.addRead(args.market.tokenProgram);
|
|
11244
|
+
return accounts.finish(instructionData, {
|
|
11245
|
+
instruction: "exchange_withdrawal",
|
|
11246
|
+
label: "Withdraw from CLOB exchange",
|
|
11247
|
+
value: {
|
|
11248
|
+
market: args.market.address,
|
|
11249
|
+
mint: mintForSide(args.market, args.tokenSide),
|
|
11250
|
+
amount: args.amount.toString()
|
|
11251
|
+
}
|
|
11252
|
+
});
|
|
11253
|
+
}
|
|
11254
|
+
function buildClobFundedOrderInstructionPlan(args) {
|
|
11255
|
+
validateFundedOrderPlan(args);
|
|
11256
|
+
const instructions = [];
|
|
11257
|
+
const accounts = new AccountPlan();
|
|
11258
|
+
const sourceMarkets = indexSourceMarkets(args.sourceMarkets ?? []);
|
|
11259
|
+
args.plan.sources.forEach((source, index) => {
|
|
11260
|
+
const sourceMarket = source.allocation.market === args.targetMarket.address ? args.targetMarket : sourceMarkets.get(source.allocation.market);
|
|
11261
|
+
const sourceRecordIndex = index + 1;
|
|
11262
|
+
instructions.push(
|
|
11263
|
+
marketRecordInstruction(
|
|
11264
|
+
sourceMarket,
|
|
11265
|
+
sourceRecordIndex,
|
|
11266
|
+
args.plan.authority,
|
|
11267
|
+
source.allocation.seatIndex
|
|
11268
|
+
),
|
|
11269
|
+
createTokenWithdrawInstruction({
|
|
11270
|
+
marketRecordIndex: sourceRecordIndex,
|
|
11271
|
+
tokenProgramAccountBytes: addressBytes(args.plan.tokenProgram),
|
|
11272
|
+
fromAccountBytes: addressBytes(source.allocation.vault),
|
|
11273
|
+
toAccountBytes: addressBytes(args.walletTokenAccount),
|
|
11274
|
+
amount: source.amount
|
|
11275
|
+
})
|
|
11276
|
+
);
|
|
11277
|
+
addSourceMarketAccounts(accounts, sourceMarket, source.allocation.vault);
|
|
11278
|
+
});
|
|
11279
|
+
const targetRecordIndex = args.plan.sources.length + 1;
|
|
11280
|
+
const opensSeat = args.plan.targetSeatIndex === null;
|
|
11281
|
+
instructions.push(
|
|
11282
|
+
marketRecordInstruction(
|
|
11283
|
+
args.targetMarket,
|
|
11284
|
+
targetRecordIndex,
|
|
11285
|
+
opensSeat ? null : args.plan.authority,
|
|
11286
|
+
args.plan.targetSeatIndex
|
|
11287
|
+
)
|
|
11288
|
+
);
|
|
11289
|
+
if (opensSeat) {
|
|
11290
|
+
instructions.push(createSeatCreateInstruction({
|
|
11291
|
+
marketRecordIndex: targetRecordIndex,
|
|
11292
|
+
seatAuthorityAccountBytes: addressBytes(args.plan.authority)
|
|
11293
|
+
}));
|
|
11294
|
+
}
|
|
11295
|
+
const sourceAmount = fundingSourcesAmount(args.plan.sources);
|
|
11296
|
+
if (sourceAmount > 0n) {
|
|
11297
|
+
instructions.push(createTokenDepositInstruction({
|
|
11298
|
+
marketRecordIndex: targetRecordIndex,
|
|
11299
|
+
tokenProgramAccountBytes: addressBytes(args.plan.tokenProgram),
|
|
11300
|
+
fromAccountBytes: addressBytes(args.walletTokenAccount),
|
|
11301
|
+
toAccountBytes: addressBytes(vaultForSide(args.targetMarket, args.plan.targetTokenSide)),
|
|
11302
|
+
amount: sourceAmount
|
|
11303
|
+
}));
|
|
11304
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
11305
|
+
accounts.addWrite(vaultForSide(args.targetMarket, args.plan.targetTokenSide));
|
|
11306
|
+
}
|
|
11307
|
+
instructions.push(createOrderEntryInstruction({
|
|
11308
|
+
marketRecordIndex: targetRecordIndex,
|
|
11309
|
+
side: args.plan.targetTokenSide === "quote" ? "buy" : "sell",
|
|
11310
|
+
orderType: args.orderType,
|
|
11311
|
+
price: args.price,
|
|
11312
|
+
quantity: args.quantity,
|
|
11313
|
+
expirationTime: args.expirationTime,
|
|
11314
|
+
clientId: args.clientId,
|
|
11315
|
+
discardAfterMatch: args.discardAfterMatch,
|
|
11316
|
+
failIfOutsideBook: args.failIfOutsideBook
|
|
11317
|
+
}));
|
|
11318
|
+
addTargetMarketAccounts(accounts, args.targetMarket);
|
|
11319
|
+
accounts.addRead(args.plan.authority);
|
|
11320
|
+
accounts.addRead(args.plan.tokenProgram);
|
|
11321
|
+
return accounts.finish(concatClobInstructions(...instructions), {
|
|
11322
|
+
instruction: "funded_order",
|
|
11323
|
+
label: "Place order using CLOB exchange balance",
|
|
11324
|
+
value: {
|
|
11325
|
+
targetMarket: args.targetMarket.address,
|
|
11326
|
+
mint: args.plan.mint,
|
|
11327
|
+
requiredAmount: args.plan.requiredAmount.toString(),
|
|
11328
|
+
exchangeAmount: (args.plan.targetAmount + sourceAmount).toString(),
|
|
11329
|
+
sourceMarketCount: args.plan.sources.length.toString(),
|
|
11330
|
+
...args.plan.sources.length > 0 ? {
|
|
11331
|
+
sourceMarkets: args.plan.sources.map((source) => source.allocation.market + ":" + source.amount.toString()).join(",")
|
|
11332
|
+
} : {}
|
|
11333
|
+
}
|
|
11334
|
+
});
|
|
11335
|
+
}
|
|
11336
|
+
function concatClobInstructions(...instructions) {
|
|
11337
|
+
return async (context) => {
|
|
11338
|
+
const chunks = await Promise.all(instructions.map((instruction) => instruction(context)));
|
|
11339
|
+
const output = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.length, 0));
|
|
11340
|
+
let offset = 0;
|
|
11341
|
+
for (const chunk of chunks) {
|
|
11342
|
+
output.set(chunk, offset);
|
|
11343
|
+
offset += chunk.length;
|
|
11344
|
+
}
|
|
11345
|
+
return output;
|
|
11346
|
+
};
|
|
11347
|
+
}
|
|
11348
|
+
function wrapClobPlanWithTokenAccountInitialization(args) {
|
|
11349
|
+
const instructionData = async (context) => {
|
|
11350
|
+
const [initializeAccountData, clobData] = await Promise.all([
|
|
11351
|
+
args.initialization.instructionData(context),
|
|
11352
|
+
args.plan.instructionData(context)
|
|
11353
|
+
]);
|
|
11354
|
+
return buildMulticallInstruction([
|
|
11355
|
+
{
|
|
11356
|
+
programIdx: context.getAccountIndex(addressBytes(args.initialization.tokenProgram)),
|
|
11357
|
+
instructionData: initializeAccountData
|
|
11358
|
+
},
|
|
11359
|
+
{
|
|
11360
|
+
programIdx: context.getAccountIndex(addressBytes(args.clobProgram)),
|
|
11361
|
+
instructionData: clobData
|
|
11362
|
+
}
|
|
11363
|
+
]);
|
|
11364
|
+
};
|
|
11365
|
+
const accounts = new AccountPlan();
|
|
11366
|
+
for (const account of args.plan.readWriteAccounts) accounts.addWrite(account);
|
|
11367
|
+
for (const account of args.plan.readOnlyAccounts) accounts.addRead(account);
|
|
11368
|
+
accounts.addWrite(args.initialization.tokenAccount);
|
|
11369
|
+
accounts.addRead(args.initialization.mint);
|
|
11370
|
+
accounts.addRead(args.initialization.owner);
|
|
11371
|
+
accounts.addRead(args.initialization.tokenProgram);
|
|
11372
|
+
accounts.addRead(args.clobProgram);
|
|
11373
|
+
const plan = accounts.finish(instructionData, {
|
|
11374
|
+
...args.plan.review,
|
|
11375
|
+
value: {
|
|
11376
|
+
...args.plan.review.value,
|
|
11377
|
+
initializesWalletTokenAccount: "true"
|
|
11378
|
+
}
|
|
11379
|
+
});
|
|
11380
|
+
return {
|
|
11381
|
+
...plan,
|
|
11382
|
+
programAddress: MULTICALL_PROGRAM_ADDRESS
|
|
11383
|
+
};
|
|
11384
|
+
}
|
|
11385
|
+
function validateAllocation(allocation) {
|
|
11386
|
+
assertU64(allocation.freeAmount, "allocation.freeAmount");
|
|
11387
|
+
assertClobActiveSeatIndex(allocation.seatIndex, "allocation.seatIndex");
|
|
11388
|
+
}
|
|
11389
|
+
function assertMarketCanTrade(market, orderType) {
|
|
11390
|
+
if (market.statusFlags & CLOB_STATUS_FLAG_PAUSED) {
|
|
11391
|
+
throw new ClobExchangeError("market_paused", "target market is paused");
|
|
11392
|
+
}
|
|
11393
|
+
if (market.statusFlags & CLOB_STATUS_FLAG_POST_ONLY && orderType !== "alo") {
|
|
11394
|
+
throw new ClobExchangeError(
|
|
11395
|
+
"market_post_only",
|
|
11396
|
+
"target market is post-only and requires an ALO order"
|
|
11397
|
+
);
|
|
11398
|
+
}
|
|
11399
|
+
}
|
|
11400
|
+
function validateFundedOrderPlan(args) {
|
|
11401
|
+
const plan = args.plan;
|
|
11402
|
+
if (!Array.isArray(plan.sources)) {
|
|
11403
|
+
throw new ClobExchangeError("funding_plan_mismatch", "funding sources must be an array");
|
|
11404
|
+
}
|
|
11405
|
+
if (plan.sources.length > CLOB_MAX_ATOMIC_FUNDING_SOURCES) {
|
|
11406
|
+
throw new ClobExchangeError(
|
|
11407
|
+
"too_many_funding_sources",
|
|
11408
|
+
"funding plan exceeds the atomic source allocation limit"
|
|
11409
|
+
);
|
|
11410
|
+
}
|
|
11411
|
+
assertFundingPlanU64(plan.requiredAmount, "requiredAmount");
|
|
11412
|
+
assertFundingPlanU64(plan.targetAmount, "targetAmount");
|
|
11413
|
+
assertFundingPlanU64(plan.shortfall, "shortfall");
|
|
11414
|
+
assertUniqueAllocationIdentities(plan.sources.map((source) => source.allocation));
|
|
11415
|
+
plan.sources.forEach((source, index) => {
|
|
11416
|
+
validateAllocation(source.allocation);
|
|
11417
|
+
assertFundingPlanU64(source.amount, "sources[" + index.toString() + "].amount");
|
|
11418
|
+
if (source.amount === 0n || source.amount > source.allocation.freeAmount) {
|
|
11419
|
+
throw new ClobExchangeError(
|
|
11420
|
+
"funding_plan_mismatch",
|
|
11421
|
+
"funding source amount must be positive and no greater than its free allocation"
|
|
11422
|
+
);
|
|
11423
|
+
}
|
|
11424
|
+
});
|
|
11425
|
+
if (plan.shortfall > 0n) {
|
|
11426
|
+
throw new ClobExchangeError(
|
|
11427
|
+
"insufficient_funds",
|
|
11428
|
+
"order funding plan is short " + plan.shortfall.toString() + " units"
|
|
11429
|
+
);
|
|
11430
|
+
}
|
|
11431
|
+
assertMarketCanTrade(args.targetMarket, args.orderType ?? "gtc");
|
|
11432
|
+
const expectedAmount = calculateClobOrderFundingAmount({
|
|
11433
|
+
side: plan.targetTokenSide === "quote" ? "buy" : "sell",
|
|
11434
|
+
price: args.price,
|
|
11435
|
+
quantity: args.quantity
|
|
11436
|
+
});
|
|
11437
|
+
const plannedAmount = plan.targetAmount + fundingSourcesAmount(plan.sources);
|
|
11438
|
+
if (plannedAmount !== plan.requiredAmount) {
|
|
11439
|
+
throw new ClobExchangeError(
|
|
11440
|
+
"funding_plan_mismatch",
|
|
11441
|
+
"funding plan amounts do not add up to the required order funding"
|
|
11442
|
+
);
|
|
11443
|
+
}
|
|
11444
|
+
if (plan.targetMarket !== args.targetMarket.address || plan.tokenProgram !== args.targetMarket.tokenProgram || plan.mint !== mintForSide(args.targetMarket, plan.targetTokenSide) || plan.requiredAmount !== expectedAmount) {
|
|
11445
|
+
throw new ClobExchangeError(
|
|
11446
|
+
"funding_plan_mismatch",
|
|
11447
|
+
"funding plan does not match the target market or order"
|
|
11448
|
+
);
|
|
11449
|
+
}
|
|
11450
|
+
const sourceMarkets = indexSourceMarkets(args.sourceMarkets ?? []);
|
|
11451
|
+
const plannedExternalSourceMarkets = new Set(
|
|
11452
|
+
plan.sources.map((source) => source.allocation.market).filter((market) => market !== args.targetMarket.address)
|
|
11453
|
+
);
|
|
11454
|
+
if (sourceMarkets.size !== plannedExternalSourceMarkets.size) {
|
|
11455
|
+
throw new ClobExchangeError(
|
|
11456
|
+
"funding_plan_mismatch",
|
|
11457
|
+
"prepared source markets do not exactly match the funding plan"
|
|
11458
|
+
);
|
|
11459
|
+
}
|
|
11460
|
+
for (const source of plan.sources) {
|
|
11461
|
+
const isTargetMarketSource = source.allocation.market === args.targetMarket.address;
|
|
11462
|
+
const sourceMarket = isTargetMarketSource ? args.targetMarket : sourceMarkets.get(source.allocation.market);
|
|
11463
|
+
if (!sourceMarket || sourceMarket.tokenProgram !== plan.tokenProgram || source.allocation.authority !== plan.authority || source.allocation.mint !== plan.mint || source.allocation.tokenSide !== sideForVault(sourceMarket, source.allocation.vault) || isTargetMarketSource && plan.targetSeatIndex !== null && source.allocation.seatIndex === plan.targetSeatIndex && source.allocation.tokenSide === plan.targetTokenSide) {
|
|
11464
|
+
throw new ClobExchangeError(
|
|
11465
|
+
"funding_plan_mismatch",
|
|
11466
|
+
"funding source does not match its prepared source market allocation"
|
|
11467
|
+
);
|
|
11468
|
+
}
|
|
11469
|
+
if (sourceMarket.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN) {
|
|
11470
|
+
throw new ClobExchangeError("withdrawals_frozen", "source market withdrawals are frozen");
|
|
11471
|
+
}
|
|
11472
|
+
}
|
|
11473
|
+
if (plan.sources.length > 0 && args.targetMarket.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
11474
|
+
throw new ClobExchangeError("deposits_frozen", "target market deposits are frozen");
|
|
11475
|
+
}
|
|
11476
|
+
}
|
|
11477
|
+
function assertFundingPlanU64(value, label) {
|
|
11478
|
+
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
11479
|
+
throw new ClobExchangeError(
|
|
11480
|
+
"funding_plan_mismatch",
|
|
11481
|
+
label + " must be between 0 and 18446744073709551615"
|
|
11482
|
+
);
|
|
11483
|
+
}
|
|
11484
|
+
}
|
|
11485
|
+
function marketRecordInstruction(market, marketRecordIndex, authority, seatIndex) {
|
|
11486
|
+
return createMarketRecordInstruction({
|
|
11487
|
+
marketRecordIndex,
|
|
11488
|
+
seatArenaAccountBytes: addressBytes(market.address),
|
|
11489
|
+
orderArenaAccountBytes: addressBytes(market.orderArena),
|
|
11490
|
+
bidsCbookAccountBytes: addressBytes(market.bidsCbook),
|
|
11491
|
+
asksCbookAccountBytes: addressBytes(market.asksCbook),
|
|
11492
|
+
seatAuthorityAccountBytes: authority ? addressBytes(authority) : void 0,
|
|
11493
|
+
seatIndex: seatIndex ?? void 0,
|
|
11494
|
+
tokenProgramAccountBytes: addressBytes(market.tokenProgram),
|
|
11495
|
+
baseVaultAccountBytes: addressBytes(market.baseVault),
|
|
11496
|
+
quoteVaultAccountBytes: addressBytes(market.quoteVault)
|
|
11497
|
+
});
|
|
11498
|
+
}
|
|
11499
|
+
function addTargetMarketAccounts(accounts, market) {
|
|
11500
|
+
accounts.addWrite(market.address);
|
|
11501
|
+
accounts.addWrite(market.orderArena);
|
|
11502
|
+
accounts.addWrite(market.bidsCbook);
|
|
11503
|
+
accounts.addWrite(market.asksCbook);
|
|
11504
|
+
accounts.addRead(market.baseVault);
|
|
11505
|
+
accounts.addRead(market.quoteVault);
|
|
11506
|
+
}
|
|
11507
|
+
function addSourceMarketAccounts(accounts, market, sourceVault) {
|
|
11508
|
+
accounts.addWrite(market.address);
|
|
11509
|
+
accounts.addWrite(sourceVault);
|
|
11510
|
+
accounts.addWrite(market.orderArena);
|
|
11511
|
+
accounts.addWrite(market.bidsCbook);
|
|
11512
|
+
accounts.addWrite(market.asksCbook);
|
|
11513
|
+
accounts.addRead(market.baseVault);
|
|
11514
|
+
accounts.addRead(market.quoteVault);
|
|
11515
|
+
}
|
|
11516
|
+
var AccountPlan = class {
|
|
11517
|
+
constructor() {
|
|
11518
|
+
this.writes = /* @__PURE__ */ new Set();
|
|
11519
|
+
this.reads = /* @__PURE__ */ new Set();
|
|
11520
|
+
}
|
|
11521
|
+
addWrite(address) {
|
|
11522
|
+
this.writes.add(address);
|
|
11523
|
+
this.reads.delete(address);
|
|
11524
|
+
}
|
|
11525
|
+
addRead(address) {
|
|
11526
|
+
if (!this.writes.has(address)) this.reads.add(address);
|
|
11527
|
+
}
|
|
11528
|
+
finish(instructionData, review) {
|
|
11529
|
+
return {
|
|
11530
|
+
instructionData,
|
|
11531
|
+
readWriteAccounts: [...this.writes],
|
|
11532
|
+
readOnlyAccounts: [...this.reads],
|
|
11533
|
+
review
|
|
11534
|
+
};
|
|
11535
|
+
}
|
|
11536
|
+
};
|
|
11537
|
+
function addressBytes(address) {
|
|
11538
|
+
return Pubkey$1.from(address).toBytes();
|
|
11539
|
+
}
|
|
11540
|
+
function mintForSide(market, side) {
|
|
11541
|
+
const mint = side === "base" ? market.baseMint : market.quoteMint;
|
|
11542
|
+
if (!mint) {
|
|
11543
|
+
throw new ClobExchangeError(
|
|
11544
|
+
"funding_plan_mismatch",
|
|
11545
|
+
"market is missing " + side + " mint metadata"
|
|
11546
|
+
);
|
|
11547
|
+
}
|
|
11548
|
+
return mint;
|
|
11549
|
+
}
|
|
11550
|
+
function vaultForSide(market, side) {
|
|
11551
|
+
return side === "base" ? market.baseVault : market.quoteVault;
|
|
11552
|
+
}
|
|
11553
|
+
function sideForVault(market, vault) {
|
|
11554
|
+
if (vault === market.baseVault) return "base";
|
|
11555
|
+
if (vault === market.quoteVault) return "quote";
|
|
11556
|
+
return null;
|
|
11557
|
+
}
|
|
11558
|
+
function seatAmount(seat, side) {
|
|
11559
|
+
return side === "base" ? seat.quantityBase : seat.quantityQuote;
|
|
11560
|
+
}
|
|
11561
|
+
function compareAllocationIdentity(a, b) {
|
|
11562
|
+
return compareText(a.market, b.market) || a.seatIndex - b.seatIndex || compareText(a.tokenSide, b.tokenSide);
|
|
11563
|
+
}
|
|
11564
|
+
function compareTargetAllocation(a, b) {
|
|
11565
|
+
return compareBigIntDescending(a.freeAmount, b.freeAmount) || a.seatIndex - b.seatIndex;
|
|
11566
|
+
}
|
|
11567
|
+
function compareBigIntDescending(a, b) {
|
|
11568
|
+
return a > b ? -1 : a < b ? 1 : 0;
|
|
11569
|
+
}
|
|
11570
|
+
function compareText(a, b) {
|
|
11571
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
11572
|
+
}
|
|
11573
|
+
function sourceAllocationIdentity(allocation) {
|
|
11574
|
+
return allocation.market + "\0" + allocation.seatIndex.toString() + "\0" + allocation.tokenSide;
|
|
11575
|
+
}
|
|
11576
|
+
function assertUniqueAllocationIdentities(allocations) {
|
|
11577
|
+
const identities = /* @__PURE__ */ new Set();
|
|
11578
|
+
for (const allocation of allocations) {
|
|
11579
|
+
const identity = sourceAllocationIdentity(allocation);
|
|
11580
|
+
if (identities.has(identity)) {
|
|
11581
|
+
throw new ClobExchangeError(
|
|
11582
|
+
"funding_plan_mismatch",
|
|
11583
|
+
"duplicate indexed allocation for market " + allocation.market + " seat " + allocation.seatIndex.toString()
|
|
11584
|
+
);
|
|
11585
|
+
}
|
|
11586
|
+
identities.add(identity);
|
|
11587
|
+
}
|
|
11588
|
+
}
|
|
11589
|
+
function fundingSourcesAmount(sources) {
|
|
11590
|
+
return sources.reduce((amount, source) => amount + source.amount, 0n);
|
|
11591
|
+
}
|
|
11592
|
+
function indexSourceMarkets(markets) {
|
|
11593
|
+
const byAddress = /* @__PURE__ */ new Map();
|
|
11594
|
+
for (const market of markets) {
|
|
11595
|
+
if (byAddress.has(market.address)) {
|
|
11596
|
+
throw new ClobExchangeError(
|
|
11597
|
+
"funding_plan_mismatch",
|
|
11598
|
+
"duplicate prepared source market " + market.address
|
|
11599
|
+
);
|
|
11600
|
+
}
|
|
11601
|
+
byAddress.set(market.address, market);
|
|
11602
|
+
}
|
|
11603
|
+
return byAddress;
|
|
11604
|
+
}
|
|
11605
|
+
function minBigInt(a, b) {
|
|
11606
|
+
return a < b ? a : b;
|
|
11607
|
+
}
|
|
11608
|
+
function assertPositiveU64(value, label) {
|
|
11609
|
+
assertU64(value, label);
|
|
11610
|
+
if (value === 0n) {
|
|
11611
|
+
throw new ClobExchangeError("invalid_funding_amount", label + " must be positive");
|
|
11612
|
+
}
|
|
11613
|
+
}
|
|
11614
|
+
function assertU64(value, label) {
|
|
11615
|
+
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
11616
|
+
throw new Error(label + " must be between 0 and 18446744073709551615");
|
|
11617
|
+
}
|
|
11618
|
+
}
|
|
11028
11619
|
|
|
11029
11620
|
// src/clob/index.ts
|
|
11030
11621
|
var CLOB_INSTRUCTION_MARKET_RECORD = 0;
|
|
@@ -11081,15 +11672,16 @@ var CLOB_CLIENT_ID_SIZE = 16;
|
|
|
11081
11672
|
var CLOB_SEATLESS_SEAT_IDX = 1;
|
|
11082
11673
|
var CLOB_NULL_INDEX = 1048575;
|
|
11083
11674
|
function createMarketRecordInstruction(args) {
|
|
11675
|
+
const seatIndex = args.seatIndex === void 0 ? CLOB_NULL_INDEX : assertClobActiveSeatIndex(args.seatIndex);
|
|
11084
11676
|
return async (context) => {
|
|
11085
|
-
const payload = new MarketRecordInstructionBuilder().set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_seat_arena_account_idx(accountIndex(context, args.seatArenaAccountBytes)).set_order_arena_account_idx(accountIndex(context, args.orderArenaAccountBytes)).set_bids_cbook_account_idx(accountIndex(context, args.bidsCbookAccountBytes)).set_asks_cbook_account_idx(accountIndex(context, args.asksCbookAccountBytes)).set_seat_authority_account_idx(optionalAccountIndex(context, args.seatAuthorityAccountBytes)).set_seat_idx(
|
|
11677
|
+
const payload = new MarketRecordInstructionBuilder().set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_seat_arena_account_idx(accountIndex(context, args.seatArenaAccountBytes)).set_order_arena_account_idx(accountIndex(context, args.orderArenaAccountBytes)).set_bids_cbook_account_idx(accountIndex(context, args.bidsCbookAccountBytes)).set_asks_cbook_account_idx(accountIndex(context, args.asksCbookAccountBytes)).set_seat_authority_account_idx(optionalAccountIndex(context, args.seatAuthorityAccountBytes)).set_seat_idx(seatIndex).set_token_program_idx(accountIndex(context, args.tokenProgramAccountBytes)).set_base_vault_account_idx(accountIndex(context, args.baseVaultAccountBytes)).set_quote_vault_account_idx(accountIndex(context, args.quoteVaultAccountBytes)).set_market_authority_account_idx(optionalAccountIndex(context, args.marketAuthorityAccountBytes)).build();
|
|
11086
11678
|
return buildClobInstruction("market_record", payload);
|
|
11087
11679
|
};
|
|
11088
11680
|
}
|
|
11089
11681
|
function createMarketCreateInstruction(args) {
|
|
11090
11682
|
return async (context) => {
|
|
11091
|
-
|
|
11092
|
-
|
|
11683
|
+
assertU642(args.lotSize, "lotSize");
|
|
11684
|
+
assertU642(args.tickSize, "tickSize");
|
|
11093
11685
|
const builder = new MarketCreateInstructionBuilder().set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_reserved0([0, 0, 0, 0, 0, 0]).set_lot_size(args.lotSize).set_tick_size(args.tickSize).set_token_program_idx(accountIndex(context, args.tokenProgramAccountBytes)).set_base_mint_idx(accountIndex(context, args.baseMintAccountBytes)).set_quote_mint_idx(accountIndex(context, args.quoteMintAccountBytes)).set_seat_arena_account_idx(accountIndex(context, args.seatArenaAccountBytes)).set_order_arena_account_idx(accountIndex(context, args.orderArenaAccountBytes)).set_bids_cbook_account_idx(accountIndex(context, args.bidsCbookAccountBytes)).set_asks_cbook_account_idx(accountIndex(context, args.asksCbookAccountBytes)).set_base_vault_account_idx(accountIndex(context, args.baseVaultAccountBytes)).set_quote_vault_account_idx(accountIndex(context, args.quoteVaultAccountBytes)).set_market_authority_account_idx(accountIndex(context, args.marketAuthorityAccountBytes));
|
|
11094
11686
|
builder.proof_seat_arena().write(args.seatArenaStateProof).finish();
|
|
11095
11687
|
builder.proof_order_arena().write(args.orderArenaStateProof).finish();
|
|
@@ -11115,8 +11707,8 @@ function createTokenWithdrawInstruction(args) {
|
|
|
11115
11707
|
function createOrderEntryInstruction(args) {
|
|
11116
11708
|
return async () => {
|
|
11117
11709
|
const flags = orderFlags(args);
|
|
11118
|
-
|
|
11119
|
-
|
|
11710
|
+
assertU642(args.price, "price");
|
|
11711
|
+
assertU642(args.quantity, "quantity");
|
|
11120
11712
|
const builder = new CreateOrderEntryInstructionBuilder().set_instruction_flags(flags).set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_reserved0([0, 0, 0, 0, 0]).set_price(args.price).set_quantity(args.quantity).set_exp_time(args.expirationTime);
|
|
11121
11713
|
if (args.clientId) builder.client_id().write(assertClientId(args.clientId)).finish();
|
|
11122
11714
|
return buildClobInstruction("create_order_entry", builder.build());
|
|
@@ -11125,8 +11717,8 @@ function createOrderEntryInstruction(args) {
|
|
|
11125
11717
|
function createSeatlessOrderEntryInstruction(args) {
|
|
11126
11718
|
return async (context) => {
|
|
11127
11719
|
const flags = orderFlags(args);
|
|
11128
|
-
|
|
11129
|
-
|
|
11720
|
+
assertU642(args.price, "price");
|
|
11721
|
+
assertU642(args.quantity, "quantity");
|
|
11130
11722
|
const builder = new CreateSeatlessOrderEntryInstructionBuilder().set_instruction_flags(flags).set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_reserved0([0, 0, 0, 0, 0]).set_price(args.price).set_quantity(args.quantity).set_quote_wallet_idx(accountIndex(context, args.quoteWalletBytes)).set_base_wallet_idx(accountIndex(context, args.baseWalletBytes)).set_reserved1([0, 0, 0, 0]);
|
|
11131
11723
|
if (args.clientId) builder.client_id().write(assertClientId(args.clientId)).finish();
|
|
11132
11724
|
return buildClobInstruction("create_seatless_order_entry", builder.build());
|
|
@@ -11138,7 +11730,7 @@ function createModifyOrderEntryInstruction(args) {
|
|
|
11138
11730
|
if (args.clientId) flags |= CLOB_MODIFY_FLAG_HAS_CLIENT_ID;
|
|
11139
11731
|
if (args.orderId !== void 0) flags |= CLOB_MODIFY_FLAG_HAS_ORDER_ID;
|
|
11140
11732
|
const metadata = buildModifyOrderMetadata(args);
|
|
11141
|
-
|
|
11733
|
+
assertU642(args.quantity, "quantity");
|
|
11142
11734
|
const builder = new ModifyOrderEntryInstructionBuilder().set_instruction_flags(flags).set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_reserved0(0).set_order_entry_idx(args.orderEntryIndex).set_quantity(args.quantity).set_exp_time(args.expirationTime);
|
|
11143
11735
|
if (metadata.length) builder.metadata().write(metadata).finish();
|
|
11144
11736
|
return buildClobInstruction("modify_order_entry", builder.build());
|
|
@@ -11394,14 +11986,14 @@ function buildModifyOrderMetadata(args) {
|
|
|
11394
11986
|
offset += CLOB_CLIENT_ID_SIZE;
|
|
11395
11987
|
}
|
|
11396
11988
|
if (args.orderId !== void 0) {
|
|
11397
|
-
|
|
11989
|
+
assertU642(args.orderId, "orderId");
|
|
11398
11990
|
new DataView(metadata.buffer).setBigUint64(offset, args.orderId, true);
|
|
11399
11991
|
}
|
|
11400
11992
|
return metadata;
|
|
11401
11993
|
}
|
|
11402
11994
|
function createTokenTransferInstruction(variant, args) {
|
|
11403
11995
|
return async (context) => {
|
|
11404
|
-
|
|
11996
|
+
assertU642(args.amount, "amount");
|
|
11405
11997
|
const payload = new TokenTransferInstructionBuilder().set_market_record_idx(assertU8(args.marketRecordIndex, "marketRecordIndex")).set_token_program_idx(accountIndex(context, args.tokenProgramAccountBytes)).set_reserved0([0, 0, 0, 0]).set_amount(args.amount).set_from_account_idx(accountIndex(context, args.fromAccountBytes)).set_to_account_idx(accountIndex(context, args.toAccountBytes)).set_reserved1([0, 0, 0, 0]).build();
|
|
11406
11998
|
return buildClobInstruction(variant, payload);
|
|
11407
11999
|
};
|
|
@@ -11578,7 +12170,13 @@ function assertU16(value, label) {
|
|
|
11578
12170
|
}
|
|
11579
12171
|
return value;
|
|
11580
12172
|
}
|
|
11581
|
-
function
|
|
12173
|
+
function assertClobActiveSeatIndex(value, label = "seatIndex") {
|
|
12174
|
+
if (!Number.isSafeInteger(value) || value < 2 || value >= CLOB_NULL_INDEX) {
|
|
12175
|
+
throw new Error(`${label} must be an integer between 2 and ${CLOB_NULL_INDEX - 1}`);
|
|
12176
|
+
}
|
|
12177
|
+
return value;
|
|
12178
|
+
}
|
|
12179
|
+
function assertU642(value, label) {
|
|
11582
12180
|
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
11583
12181
|
throw new Error(`${label} must be between 0 and 18446744073709551615`);
|
|
11584
12182
|
}
|
|
@@ -11643,6 +12241,6 @@ function bytesFromView(value) {
|
|
|
11643
12241
|
throw new Error("generated view did not expose a byte buffer");
|
|
11644
12242
|
}
|
|
11645
12243
|
|
|
11646
|
-
export { ArenaHeader, ArenaHeaderBuilder, CLOB_ARENA_HEADER_SIZE, CLOB_CBOOK_EMPTY_PRICE_IN_TICKS, CLOB_CBOOK_HEADER_SIZE, CLOB_CBOOK_LEVEL_SIZE, CLOB_CBOOK_MAX_PRICE_IN_TICKS, CLOB_CBOOK_MAX_SIZE, CLOB_CLIENT_ID_SIZE, CLOB_EVENT_MARKET_CREATED, CLOB_EVENT_MARKET_STATUS, CLOB_EVENT_ORDER_CANCELLED, CLOB_EVENT_ORDER_ENTRY_REMOVED, CLOB_EVENT_ORDER_FILLED, CLOB_EVENT_ORDER_MODIFIED, CLOB_EVENT_ORDER_POSTED, CLOB_EVENT_REMOVE_REASON_EVICTED, CLOB_EVENT_REMOVE_REASON_EXPIRED, CLOB_EVENT_REMOVE_REASON_FILLED, CLOB_EVENT_REMOVE_REASON_USER, CLOB_EVENT_SEAT_ASSIGNED, CLOB_EVENT_SIDE_BUY, CLOB_EVENT_SIDE_SELL, CLOB_EVENT_TOKEN_DEPOSIT, CLOB_EVENT_TOKEN_SIDE_BASE, CLOB_EVENT_TOKEN_SIDE_QUOTE, CLOB_EVENT_TOKEN_WITHDRAW, CLOB_INSTRUCTION_CREATE_ORDER_ENTRY, CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY, CLOB_INSTRUCTION_MARKET_CREATE, CLOB_INSTRUCTION_MARKET_RECORD, CLOB_INSTRUCTION_MARKET_SET_STATUS, CLOB_INSTRUCTION_MODIFY_ORDER_ENTRY, CLOB_INSTRUCTION_SEAT_CREATE, CLOB_INSTRUCTION_TOKEN_DEPOSIT, CLOB_INSTRUCTION_TOKEN_WITHDRAW, CLOB_MARKET_ACCOUNT_SIZE, CLOB_MODIFY_FLAG_FAIL_IF_OUT_OF_RANGE, CLOB_MODIFY_FLAG_HAS_CLIENT_ID, CLOB_MODIFY_FLAG_HAS_ORDER_ID, CLOB_NULL_INDEX, CLOB_ORDER_ENTRY_SIZE, CLOB_ORDER_FLAG_BUY, CLOB_ORDER_FLAG_HAS_CLIENT_ID, CLOB_ORDER_TYPE_ALO, CLOB_ORDER_TYPE_FOK, CLOB_ORDER_TYPE_GTC, CLOB_ORDER_TYPE_IOC, CLOB_ORDER_TYPE_MTL, CLOB_SEATLESS_SEAT_IDX, CLOB_SEAT_ENTRY_SIZE, CLOB_STATUS_FLAG_DEPOSITS_FROZEN, CLOB_STATUS_FLAG_PAUSED, CLOB_STATUS_FLAG_POST_ONLY, CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN, CbookAccount, CbookHeader, CbookHeaderBuilder, CbookLevel, CbookLevelBuilder, ClientId, ClobError, ClobErrorBuilder, ClobEvent, ClobEventBuilder, ClobInstruction, ClobInstructionBuilder, ClobProgramAccount, ClobProgramAccountBuilder, CreateOrderEntryInstruction, CreateOrderEntryInstructionBuilder, CreateSeatlessOrderEntryInstruction, CreateSeatlessOrderEntryInstructionBuilder, MarketAccountBuilder, MarketAccount as MarketAccountView, MarketCreateInstruction, MarketCreateInstructionBuilder, MarketCreatedEvent, MarketCreatedEventBuilder, MarketRecordInstruction, MarketRecordInstructionBuilder, MarketSetStatusInstruction, MarketSetStatusInstructionBuilder, ModifyOrderEntryInstruction, ModifyOrderEntryInstructionBuilder, OrderArenaAccount, OrderCancelledEvent, OrderCancelledEventBuilder, OrderEntryBuilder, OrderEntryRemovedEvent, OrderEntryRemovedEventBuilder, OrderEntry as OrderEntryView, OrderFilledEvent, OrderFilledEventBuilder, OrderModifiedEvent, OrderModifiedEventBuilder, OrderPostedEvent, OrderPostedEventBuilder, SeatArenaAccount, SeatAssignedEvent, SeatAssignedEventBuilder, SeatCreateInstruction, SeatCreateInstructionBuilder, SeatEntryBuilder, SeatEntry as SeatEntryView, TokenBalanceEvent, TokenBalanceEventBuilder, TokenTransferInstruction, TokenTransferInstructionBuilder, buildOrderBookSnapshot, createMarketCreateInstruction, createMarketRecordInstruction, createMarketSetStatusInstruction, createModifyOrderEntryInstruction, createOrderEntryInstruction, createSeatCreateInstruction, createSeatlessOrderEntryInstruction, createTokenDepositInstruction, createTokenWithdrawInstruction, encodeOrderFlags, parseCbookAccount, parseClobEvent, parseClobProgramAccount, parseMarketAccount, parseOrderArenaAccount, parseSeatArenaAccount };
|
|
12244
|
+
export { ArenaHeader, ArenaHeaderBuilder, CLOB_ARENA_HEADER_SIZE, CLOB_CBOOK_EMPTY_PRICE_IN_TICKS, CLOB_CBOOK_HEADER_SIZE, CLOB_CBOOK_LEVEL_SIZE, CLOB_CBOOK_MAX_PRICE_IN_TICKS, CLOB_CBOOK_MAX_SIZE, CLOB_CLIENT_ID_SIZE, CLOB_EVENT_MARKET_CREATED, CLOB_EVENT_MARKET_STATUS, CLOB_EVENT_ORDER_CANCELLED, CLOB_EVENT_ORDER_ENTRY_REMOVED, CLOB_EVENT_ORDER_FILLED, CLOB_EVENT_ORDER_MODIFIED, CLOB_EVENT_ORDER_POSTED, CLOB_EVENT_REMOVE_REASON_EVICTED, CLOB_EVENT_REMOVE_REASON_EXPIRED, CLOB_EVENT_REMOVE_REASON_FILLED, CLOB_EVENT_REMOVE_REASON_USER, CLOB_EVENT_SEAT_ASSIGNED, CLOB_EVENT_SIDE_BUY, CLOB_EVENT_SIDE_SELL, CLOB_EVENT_TOKEN_DEPOSIT, CLOB_EVENT_TOKEN_SIDE_BASE, CLOB_EVENT_TOKEN_SIDE_QUOTE, CLOB_EVENT_TOKEN_WITHDRAW, CLOB_INSTRUCTION_CREATE_ORDER_ENTRY, CLOB_INSTRUCTION_CREATE_SEATLESS_ORDER_ENTRY, CLOB_INSTRUCTION_MARKET_CREATE, CLOB_INSTRUCTION_MARKET_RECORD, CLOB_INSTRUCTION_MARKET_SET_STATUS, CLOB_INSTRUCTION_MODIFY_ORDER_ENTRY, CLOB_INSTRUCTION_SEAT_CREATE, CLOB_INSTRUCTION_TOKEN_DEPOSIT, CLOB_INSTRUCTION_TOKEN_WITHDRAW, CLOB_MARKET_ACCOUNT_SIZE, CLOB_MAX_ATOMIC_FUNDING_SOURCES, CLOB_MODIFY_FLAG_FAIL_IF_OUT_OF_RANGE, CLOB_MODIFY_FLAG_HAS_CLIENT_ID, CLOB_MODIFY_FLAG_HAS_ORDER_ID, CLOB_NULL_INDEX, CLOB_ORDER_ENTRY_SIZE, CLOB_ORDER_FLAG_BUY, CLOB_ORDER_FLAG_HAS_CLIENT_ID, CLOB_ORDER_TYPE_ALO, CLOB_ORDER_TYPE_FOK, CLOB_ORDER_TYPE_GTC, CLOB_ORDER_TYPE_IOC, CLOB_ORDER_TYPE_MTL, CLOB_SEATLESS_SEAT_IDX, CLOB_SEAT_ENTRY_SIZE, CLOB_STATUS_FLAG_DEPOSITS_FROZEN, CLOB_STATUS_FLAG_PAUSED, CLOB_STATUS_FLAG_POST_ONLY, CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN, CbookAccount, CbookHeader, CbookHeaderBuilder, CbookLevel, CbookLevelBuilder, ClientId, ClobError, ClobErrorBuilder, ClobEvent, ClobEventBuilder, ClobExchangeError, ClobInstruction, ClobInstructionBuilder, ClobProgramAccount, ClobProgramAccountBuilder, CreateOrderEntryInstruction, CreateOrderEntryInstructionBuilder, CreateSeatlessOrderEntryInstruction, CreateSeatlessOrderEntryInstructionBuilder, MarketAccountBuilder, MarketAccount as MarketAccountView, MarketCreateInstruction, MarketCreateInstructionBuilder, MarketCreatedEvent, MarketCreatedEventBuilder, MarketRecordInstruction, MarketRecordInstructionBuilder, MarketSetStatusInstruction, MarketSetStatusInstructionBuilder, ModifyOrderEntryInstruction, ModifyOrderEntryInstructionBuilder, OrderArenaAccount, OrderCancelledEvent, OrderCancelledEventBuilder, OrderEntryBuilder, OrderEntryRemovedEvent, OrderEntryRemovedEventBuilder, OrderEntry as OrderEntryView, OrderFilledEvent, OrderFilledEventBuilder, OrderModifiedEvent, OrderModifiedEventBuilder, OrderPostedEvent, OrderPostedEventBuilder, SeatArenaAccount, SeatAssignedEvent, SeatAssignedEventBuilder, SeatCreateInstruction, SeatCreateInstructionBuilder, SeatEntryBuilder, SeatEntry as SeatEntryView, TokenBalanceEvent, TokenBalanceEventBuilder, TokenTransferInstruction, TokenTransferInstructionBuilder, aggregateClobExchangeBalances, assertClobActiveSeatIndex, buildClobExchangeDepositInstructionPlan, buildClobExchangeWithdrawalInstructionPlan, buildClobFundedOrderInstructionPlan, buildOrderBookSnapshot, calculateClobOrderFundingAmount, concatClobInstructions, createMarketCreateInstruction, createMarketRecordInstruction, createMarketSetStatusInstruction, createModifyOrderEntryInstruction, createOrderEntryInstruction, createSeatCreateInstruction, createSeatlessOrderEntryInstruction, createTokenDepositInstruction, createTokenWithdrawInstruction, encodeOrderFlags, parseCbookAccount, parseClobEvent, parseClobProgramAccount, parseMarketAccount, parseOrderArenaAccount, parseSeatArenaAccount, planClobOrderFunding, validateClobOrderFundingPlanSeats, wrapClobPlanWithTokenAccountInitialization };
|
|
11647
12245
|
//# sourceMappingURL=index.js.map
|
|
11648
12246
|
//# sourceMappingURL=index.js.map
|