@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.cjs
CHANGED
|
@@ -1335,11 +1335,11 @@ _InstructionData.flexibleArrayWriters = Object.freeze([
|
|
|
1335
1335
|
{ field: "data", method: "data", sizeField: "data_size", paramKey: "data_size", elementSize: 1 }
|
|
1336
1336
|
]);
|
|
1337
1337
|
var InstructionData = _InstructionData;
|
|
1338
|
-
((
|
|
1339
|
-
|
|
1338
|
+
((InstructionData3) => {
|
|
1339
|
+
InstructionData3.ParamKeys = Object.freeze({
|
|
1340
1340
|
data_data_size: "data.data_size"
|
|
1341
1341
|
});
|
|
1342
|
-
|
|
1342
|
+
InstructionData3.Params = {
|
|
1343
1343
|
fromValues(input) {
|
|
1344
1344
|
return {
|
|
1345
1345
|
data_data_size: __tnToBigInt(input.data_data_size)
|
|
@@ -1356,9 +1356,9 @@ var InstructionData = _InstructionData;
|
|
|
1356
1356
|
}
|
|
1357
1357
|
};
|
|
1358
1358
|
function params(input) {
|
|
1359
|
-
return
|
|
1359
|
+
return InstructionData3.Params.fromValues(input);
|
|
1360
1360
|
}
|
|
1361
|
-
|
|
1361
|
+
InstructionData3.params = params;
|
|
1362
1362
|
})(InstructionData || (InstructionData = {}));
|
|
1363
1363
|
var InstructionDataBuilder = class {
|
|
1364
1364
|
constructor() {
|
|
@@ -11028,6 +11028,3307 @@ __tnRegisterDynamicValidate2("ClobProgramAccount", (buffer) => {
|
|
|
11028
11028
|
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt2(result.consumed), params };
|
|
11029
11029
|
});
|
|
11030
11030
|
|
|
11031
|
+
// src/multicall/abi/thru/common/primitives/types.ts
|
|
11032
|
+
var __tnWarnings3 = /* @__PURE__ */ new Set();
|
|
11033
|
+
var __tnHasNativeBigInt3 = typeof BigInt === "function";
|
|
11034
|
+
var __tnHasBigIntDataView3 = typeof DataView !== "undefined" && typeof DataView.prototype.getBigInt64 === "function" && typeof DataView.prototype.getBigUint64 === "function" && typeof DataView.prototype.setBigInt64 === "function" && typeof DataView.prototype.setBigUint64 === "function";
|
|
11035
|
+
var __tnConsole3 = typeof globalThis !== "undefined" ? globalThis.console : void 0;
|
|
11036
|
+
function __tnLogWarn3(message) {
|
|
11037
|
+
if (__tnConsole3 && typeof __tnConsole3.warn === "function") {
|
|
11038
|
+
__tnConsole3.warn(message);
|
|
11039
|
+
}
|
|
11040
|
+
}
|
|
11041
|
+
function __tnWarnOnce3(message) {
|
|
11042
|
+
if (!__tnWarnings3.has(message)) {
|
|
11043
|
+
__tnWarnings3.add(message);
|
|
11044
|
+
__tnLogWarn3(message);
|
|
11045
|
+
}
|
|
11046
|
+
}
|
|
11047
|
+
function __tnResolveBuilderInput3(input, context) {
|
|
11048
|
+
if (input instanceof Uint8Array) {
|
|
11049
|
+
return new Uint8Array(input);
|
|
11050
|
+
}
|
|
11051
|
+
if (input && typeof input.build === "function") {
|
|
11052
|
+
const built = input.build();
|
|
11053
|
+
if (!(built instanceof Uint8Array)) {
|
|
11054
|
+
throw new Error(`${context}: builder did not return Uint8Array`);
|
|
11055
|
+
}
|
|
11056
|
+
return new Uint8Array(built);
|
|
11057
|
+
}
|
|
11058
|
+
throw new Error(`${context}: expected Uint8Array or builder`);
|
|
11059
|
+
}
|
|
11060
|
+
function __tnCreateFamWriter3(parent, fieldName, assign) {
|
|
11061
|
+
let hasWritten = false;
|
|
11062
|
+
return {
|
|
11063
|
+
write(payload) {
|
|
11064
|
+
const bytes = __tnResolveBuilderInput3(
|
|
11065
|
+
payload,
|
|
11066
|
+
`flexible array '${fieldName}'`
|
|
11067
|
+
);
|
|
11068
|
+
const copy = new Uint8Array(bytes);
|
|
11069
|
+
assign(copy);
|
|
11070
|
+
hasWritten = true;
|
|
11071
|
+
return {
|
|
11072
|
+
finish() {
|
|
11073
|
+
return parent;
|
|
11074
|
+
}
|
|
11075
|
+
};
|
|
11076
|
+
},
|
|
11077
|
+
finish() {
|
|
11078
|
+
if (!hasWritten) {
|
|
11079
|
+
throw new Error(
|
|
11080
|
+
`flexible array '${fieldName}' requires write() before finish()`
|
|
11081
|
+
);
|
|
11082
|
+
}
|
|
11083
|
+
return parent;
|
|
11084
|
+
}
|
|
11085
|
+
};
|
|
11086
|
+
}
|
|
11087
|
+
var __tnMask323 = __tnHasNativeBigInt3 ? (BigInt(1) << BigInt(32)) - BigInt(1) : 4294967295;
|
|
11088
|
+
var __tnSignBit323 = __tnHasNativeBigInt3 ? BigInt(1) << BigInt(31) : 2147483648;
|
|
11089
|
+
function __tnToBigInt3(value) {
|
|
11090
|
+
if (__tnHasNativeBigInt3) {
|
|
11091
|
+
return typeof value === "bigint" ? value : BigInt(value);
|
|
11092
|
+
}
|
|
11093
|
+
if (typeof value === "bigint") return value;
|
|
11094
|
+
if (!Number.isFinite(value)) {
|
|
11095
|
+
throw new Error("IR runtime received non-finite numeric input");
|
|
11096
|
+
}
|
|
11097
|
+
if (!Number.isSafeInteger(value)) {
|
|
11098
|
+
__tnWarnOnce3(
|
|
11099
|
+
`[thru-net] Precision loss while polyfilling BigInt (value=${value})`
|
|
11100
|
+
);
|
|
11101
|
+
}
|
|
11102
|
+
return value;
|
|
11103
|
+
}
|
|
11104
|
+
function __tnBigIntToNumber3(value, context) {
|
|
11105
|
+
if (__tnHasNativeBigInt3) {
|
|
11106
|
+
const converted = Number(value);
|
|
11107
|
+
if (!Number.isFinite(converted)) {
|
|
11108
|
+
throw new Error(`${context} overflowed Number range`);
|
|
11109
|
+
}
|
|
11110
|
+
return converted;
|
|
11111
|
+
}
|
|
11112
|
+
return value;
|
|
11113
|
+
}
|
|
11114
|
+
function __tnBigIntEquals3(lhs, rhs) {
|
|
11115
|
+
if (__tnHasNativeBigInt3) return lhs === rhs;
|
|
11116
|
+
return lhs === rhs;
|
|
11117
|
+
}
|
|
11118
|
+
function __tnBigIntGreaterThan3(lhs, rhs) {
|
|
11119
|
+
if (__tnHasNativeBigInt3) return lhs > rhs;
|
|
11120
|
+
return lhs > rhs;
|
|
11121
|
+
}
|
|
11122
|
+
function __tnRaiseIrError3(code, message) {
|
|
11123
|
+
const err = new Error(message);
|
|
11124
|
+
err.code = code;
|
|
11125
|
+
throw err;
|
|
11126
|
+
}
|
|
11127
|
+
function __tnCheckedAdd3(lhs, rhs) {
|
|
11128
|
+
if (__tnHasNativeBigInt3) {
|
|
11129
|
+
const result = lhs + rhs;
|
|
11130
|
+
if (result < BigInt(0)) {
|
|
11131
|
+
__tnRaiseIrError3(
|
|
11132
|
+
"tn.ir.overflow",
|
|
11133
|
+
"IR runtime detected negative size via addition"
|
|
11134
|
+
);
|
|
11135
|
+
}
|
|
11136
|
+
return result;
|
|
11137
|
+
}
|
|
11138
|
+
const left = lhs;
|
|
11139
|
+
const right = rhs;
|
|
11140
|
+
const sum = left + right;
|
|
11141
|
+
if (sum < 0 || !Number.isFinite(sum)) {
|
|
11142
|
+
__tnRaiseIrError3(
|
|
11143
|
+
"tn.ir.overflow",
|
|
11144
|
+
"IR runtime detected invalid addition result"
|
|
11145
|
+
);
|
|
11146
|
+
}
|
|
11147
|
+
if (!Number.isSafeInteger(sum)) {
|
|
11148
|
+
__tnWarnOnce3("[thru-net] Precision loss while polyfilling BigInt addition");
|
|
11149
|
+
}
|
|
11150
|
+
return sum;
|
|
11151
|
+
}
|
|
11152
|
+
function __tnCheckedMul3(lhs, rhs) {
|
|
11153
|
+
if (__tnHasNativeBigInt3) {
|
|
11154
|
+
const result = lhs * rhs;
|
|
11155
|
+
if (result < BigInt(0)) {
|
|
11156
|
+
__tnRaiseIrError3(
|
|
11157
|
+
"tn.ir.overflow",
|
|
11158
|
+
"IR runtime detected negative size via multiplication"
|
|
11159
|
+
);
|
|
11160
|
+
}
|
|
11161
|
+
return result;
|
|
11162
|
+
}
|
|
11163
|
+
const left = lhs;
|
|
11164
|
+
const right = rhs;
|
|
11165
|
+
const product = left * right;
|
|
11166
|
+
if (product < 0 || !Number.isFinite(product)) {
|
|
11167
|
+
__tnRaiseIrError3(
|
|
11168
|
+
"tn.ir.overflow",
|
|
11169
|
+
"IR runtime detected invalid multiplication result"
|
|
11170
|
+
);
|
|
11171
|
+
}
|
|
11172
|
+
if (!Number.isSafeInteger(product)) {
|
|
11173
|
+
__tnWarnOnce3(
|
|
11174
|
+
"[thru-net] Precision loss while polyfilling BigInt multiplication"
|
|
11175
|
+
);
|
|
11176
|
+
}
|
|
11177
|
+
return product;
|
|
11178
|
+
}
|
|
11179
|
+
function __tnAlign3(value, alignment) {
|
|
11180
|
+
if (alignment <= 1) return value;
|
|
11181
|
+
const alignBig = __tnToBigInt3(alignment);
|
|
11182
|
+
if (__tnHasNativeBigInt3) {
|
|
11183
|
+
const remainder2 = value % alignBig;
|
|
11184
|
+
if (__tnBigIntEquals3(remainder2, __tnToBigInt3(0))) {
|
|
11185
|
+
return value;
|
|
11186
|
+
}
|
|
11187
|
+
const delta = alignBig - remainder2;
|
|
11188
|
+
return __tnCheckedAdd3(value, delta);
|
|
11189
|
+
}
|
|
11190
|
+
const current = __tnBigIntToNumber3(value, "IR align");
|
|
11191
|
+
const alignNum = alignment >>> 0;
|
|
11192
|
+
const remainder = current % alignNum;
|
|
11193
|
+
const next = remainder === 0 ? current : current + (alignNum - remainder);
|
|
11194
|
+
return __tnToBigInt3(next);
|
|
11195
|
+
}
|
|
11196
|
+
function __tnSplitUint643(value) {
|
|
11197
|
+
if (__tnHasNativeBigInt3) {
|
|
11198
|
+
const low2 = Number(value & __tnMask323);
|
|
11199
|
+
const high2 = Number(value >> BigInt(32) & __tnMask323);
|
|
11200
|
+
return { high: high2, low: low2 };
|
|
11201
|
+
}
|
|
11202
|
+
const num = __tnBigIntToNumber3(value, "DataView.setBigUint64");
|
|
11203
|
+
const low = num >>> 0;
|
|
11204
|
+
const high = Math.floor(num / 4294967296) >>> 0;
|
|
11205
|
+
return { high, low };
|
|
11206
|
+
}
|
|
11207
|
+
function __tnSplitInt643(value) {
|
|
11208
|
+
if (__tnHasNativeBigInt3) {
|
|
11209
|
+
const low2 = Number(value & __tnMask323);
|
|
11210
|
+
let high2 = Number(value >> BigInt(32) & __tnMask323);
|
|
11211
|
+
if ((BigInt(high2) & __tnSignBit323) !== BigInt(0)) {
|
|
11212
|
+
high2 -= 4294967296;
|
|
11213
|
+
}
|
|
11214
|
+
return { high: high2, low: low2 };
|
|
11215
|
+
}
|
|
11216
|
+
const num = __tnBigIntToNumber3(value, "DataView.setBigInt64");
|
|
11217
|
+
const low = num >>> 0;
|
|
11218
|
+
const high = Math.floor(num / 4294967296);
|
|
11219
|
+
return { high, low };
|
|
11220
|
+
}
|
|
11221
|
+
function __tnPolyfillReadUint643(view, offset, littleEndian) {
|
|
11222
|
+
const low = littleEndian ? view.getUint32(offset, true) : view.getUint32(offset + 4, false);
|
|
11223
|
+
const high = littleEndian ? view.getUint32(offset + 4, true) : view.getUint32(offset, false);
|
|
11224
|
+
if (__tnHasNativeBigInt3) {
|
|
11225
|
+
return BigInt(high) << BigInt(32) | BigInt(low);
|
|
11226
|
+
}
|
|
11227
|
+
const value = high * 4294967296 + low;
|
|
11228
|
+
if (!Number.isSafeInteger(value)) {
|
|
11229
|
+
__tnWarnOnce3(
|
|
11230
|
+
"[thru-net] Precision loss while polyfilling DataView.getBigUint64"
|
|
11231
|
+
);
|
|
11232
|
+
}
|
|
11233
|
+
return value;
|
|
11234
|
+
}
|
|
11235
|
+
function __tnPolyfillReadInt643(view, offset, littleEndian) {
|
|
11236
|
+
const low = littleEndian ? view.getUint32(offset, true) : view.getUint32(offset + 4, false);
|
|
11237
|
+
const high = littleEndian ? view.getInt32(offset + 4, true) : view.getInt32(offset, false);
|
|
11238
|
+
if (__tnHasNativeBigInt3) {
|
|
11239
|
+
return BigInt(high) << BigInt(32) | BigInt(low);
|
|
11240
|
+
}
|
|
11241
|
+
const value = high * 4294967296 + low;
|
|
11242
|
+
if (!Number.isSafeInteger(value)) {
|
|
11243
|
+
__tnWarnOnce3(
|
|
11244
|
+
"[thru-net] Precision loss while polyfilling DataView.getBigInt64"
|
|
11245
|
+
);
|
|
11246
|
+
}
|
|
11247
|
+
return value;
|
|
11248
|
+
}
|
|
11249
|
+
function __tnPolyfillWriteUint643(view, offset, value, littleEndian) {
|
|
11250
|
+
const parts = __tnSplitUint643(value);
|
|
11251
|
+
if (littleEndian) {
|
|
11252
|
+
view.setUint32(offset, parts.low, true);
|
|
11253
|
+
view.setUint32(offset + 4, parts.high, true);
|
|
11254
|
+
} else {
|
|
11255
|
+
view.setUint32(offset, parts.high, false);
|
|
11256
|
+
view.setUint32(offset + 4, parts.low, false);
|
|
11257
|
+
}
|
|
11258
|
+
}
|
|
11259
|
+
function __tnPolyfillWriteInt643(view, offset, value, littleEndian) {
|
|
11260
|
+
const parts = __tnSplitInt643(value);
|
|
11261
|
+
if (littleEndian) {
|
|
11262
|
+
view.setUint32(offset, parts.low >>> 0, true);
|
|
11263
|
+
view.setInt32(offset + 4, parts.high | 0, true);
|
|
11264
|
+
} else {
|
|
11265
|
+
view.setInt32(offset, parts.high | 0, false);
|
|
11266
|
+
view.setUint32(offset + 4, parts.low >>> 0, false);
|
|
11267
|
+
}
|
|
11268
|
+
}
|
|
11269
|
+
if (typeof DataView !== "undefined" && !__tnHasBigIntDataView3) {
|
|
11270
|
+
const proto = DataView.prototype;
|
|
11271
|
+
if (typeof proto.getBigUint64 !== "function") {
|
|
11272
|
+
proto.getBigUint64 = function(offset, littleEndian) {
|
|
11273
|
+
__tnWarnOnce3(
|
|
11274
|
+
"[thru-net] Polyfilling DataView.getBigUint64; precision may be lost"
|
|
11275
|
+
);
|
|
11276
|
+
return __tnPolyfillReadUint643(this, offset, !!littleEndian);
|
|
11277
|
+
};
|
|
11278
|
+
}
|
|
11279
|
+
if (typeof proto.getBigInt64 !== "function") {
|
|
11280
|
+
proto.getBigInt64 = function(offset, littleEndian) {
|
|
11281
|
+
__tnWarnOnce3(
|
|
11282
|
+
"[thru-net] Polyfilling DataView.getBigInt64; precision may be lost"
|
|
11283
|
+
);
|
|
11284
|
+
return __tnPolyfillReadInt643(this, offset, !!littleEndian);
|
|
11285
|
+
};
|
|
11286
|
+
}
|
|
11287
|
+
if (typeof proto.setBigUint64 !== "function") {
|
|
11288
|
+
proto.setBigUint64 = function(offset, value, littleEndian) {
|
|
11289
|
+
__tnWarnOnce3(
|
|
11290
|
+
"[thru-net] Polyfilling DataView.setBigUint64; precision may be lost"
|
|
11291
|
+
);
|
|
11292
|
+
__tnPolyfillWriteUint643(this, offset, value, !!littleEndian);
|
|
11293
|
+
};
|
|
11294
|
+
}
|
|
11295
|
+
if (typeof proto.setBigInt64 !== "function") {
|
|
11296
|
+
proto.setBigInt64 = function(offset, value, littleEndian) {
|
|
11297
|
+
__tnWarnOnce3(
|
|
11298
|
+
"[thru-net] Polyfilling DataView.setBigInt64; precision may be lost"
|
|
11299
|
+
);
|
|
11300
|
+
__tnPolyfillWriteInt643(this, offset, value, !!littleEndian);
|
|
11301
|
+
};
|
|
11302
|
+
}
|
|
11303
|
+
if (!__tnHasNativeBigInt3) {
|
|
11304
|
+
__tnWarnOnce3(
|
|
11305
|
+
"[thru-net] BigInt is unavailable; falling back to lossy 64-bit polyfill"
|
|
11306
|
+
);
|
|
11307
|
+
}
|
|
11308
|
+
}
|
|
11309
|
+
var __tnFootprintRegistry3 = {};
|
|
11310
|
+
var __tnValidateRegistry3 = {};
|
|
11311
|
+
var __tnDynamicValidateRegistry3 = {};
|
|
11312
|
+
function __tnRegisterFootprint3(typeName, fn) {
|
|
11313
|
+
__tnFootprintRegistry3[typeName] = fn;
|
|
11314
|
+
}
|
|
11315
|
+
function __tnRegisterValidate3(typeName, fn) {
|
|
11316
|
+
__tnValidateRegistry3[typeName] = fn;
|
|
11317
|
+
}
|
|
11318
|
+
function __tnRegisterDynamicValidate3(typeName, fn) {
|
|
11319
|
+
__tnDynamicValidateRegistry3[typeName] = fn;
|
|
11320
|
+
}
|
|
11321
|
+
function __tnInvokeFootprint3(typeName, params) {
|
|
11322
|
+
const fn = __tnFootprintRegistry3[typeName];
|
|
11323
|
+
if (!fn) throw new Error(`IR runtime missing footprint for ${typeName}`);
|
|
11324
|
+
return fn(params);
|
|
11325
|
+
}
|
|
11326
|
+
function __tnInvokeValidate3(typeName, buffer, params) {
|
|
11327
|
+
const fn = __tnValidateRegistry3[typeName];
|
|
11328
|
+
if (!fn) throw new Error(`IR runtime missing validate helper for ${typeName}`);
|
|
11329
|
+
return fn(buffer, params);
|
|
11330
|
+
}
|
|
11331
|
+
function __tnInvokeDynamicValidate3(typeName, buffer) {
|
|
11332
|
+
const fn = __tnDynamicValidateRegistry3[typeName];
|
|
11333
|
+
if (!fn) throw new Error(`IR runtime missing dynamic validate helper for ${typeName}`);
|
|
11334
|
+
return fn(buffer);
|
|
11335
|
+
}
|
|
11336
|
+
function __tnEvalFootprint3(node, ctx) {
|
|
11337
|
+
return __tnEvalIrNode3(node, ctx, __tnToBigInt3(0));
|
|
11338
|
+
}
|
|
11339
|
+
function __tnTryEvalIr3(node, ctx) {
|
|
11340
|
+
try {
|
|
11341
|
+
return { ok: true, value: __tnEvalIrNode3(node, ctx, __tnToBigInt3(0)) };
|
|
11342
|
+
} catch (err) {
|
|
11343
|
+
return { ok: false, code: __tnNormalizeIrError3(err) };
|
|
11344
|
+
}
|
|
11345
|
+
}
|
|
11346
|
+
function __tnIsEvalError3(result) {
|
|
11347
|
+
return result.ok === false;
|
|
11348
|
+
}
|
|
11349
|
+
function __tnValidateIrTree3(ir, buffer, params) {
|
|
11350
|
+
const evalResult = __tnTryEvalIr3(ir.root, {
|
|
11351
|
+
params,
|
|
11352
|
+
buffer,
|
|
11353
|
+
typeName: ir.typeName
|
|
11354
|
+
});
|
|
11355
|
+
if (__tnIsEvalError3(evalResult)) {
|
|
11356
|
+
return { ok: false, code: evalResult.code };
|
|
11357
|
+
}
|
|
11358
|
+
const required = evalResult.value;
|
|
11359
|
+
const available = __tnToBigInt3(buffer.length);
|
|
11360
|
+
if (__tnBigIntGreaterThan3(required, available)) {
|
|
11361
|
+
return { ok: false, code: "tn.buffer_too_small", consumed: required };
|
|
11362
|
+
}
|
|
11363
|
+
return { ok: true, consumed: required };
|
|
11364
|
+
}
|
|
11365
|
+
function __tnEvalIrNode3(node, ctx, baseOffset) {
|
|
11366
|
+
switch (node.op) {
|
|
11367
|
+
case "zero":
|
|
11368
|
+
return __tnToBigInt3(0);
|
|
11369
|
+
case "const":
|
|
11370
|
+
return node.value;
|
|
11371
|
+
case "field": {
|
|
11372
|
+
const val = ctx.params[node.param];
|
|
11373
|
+
if (val === void 0) {
|
|
11374
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
11375
|
+
__tnRaiseIrError3(
|
|
11376
|
+
"tn.ir.missing_param",
|
|
11377
|
+
`${prefix}Missing IR parameter '${node.param}'`
|
|
11378
|
+
);
|
|
11379
|
+
}
|
|
11380
|
+
return val;
|
|
11381
|
+
}
|
|
11382
|
+
case "add": {
|
|
11383
|
+
const left = __tnEvalIrNode3(node.left, ctx, baseOffset);
|
|
11384
|
+
const right = __tnEvalIrNode3(
|
|
11385
|
+
node.right,
|
|
11386
|
+
ctx,
|
|
11387
|
+
__tnCheckedAdd3(baseOffset, left)
|
|
11388
|
+
);
|
|
11389
|
+
return __tnCheckedAdd3(left, right);
|
|
11390
|
+
}
|
|
11391
|
+
case "mul":
|
|
11392
|
+
return __tnCheckedMul3(
|
|
11393
|
+
__tnEvalIrNode3(node.left, ctx, baseOffset),
|
|
11394
|
+
__tnEvalIrNode3(node.right, ctx, baseOffset)
|
|
11395
|
+
);
|
|
11396
|
+
case "align":
|
|
11397
|
+
return __tnAlign3(__tnEvalIrNode3(node.node, ctx, baseOffset), node.alignment);
|
|
11398
|
+
case "switch": {
|
|
11399
|
+
const tagVal = ctx.params[node.tag];
|
|
11400
|
+
if (tagVal === void 0) {
|
|
11401
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
11402
|
+
__tnRaiseIrError3(
|
|
11403
|
+
"tn.ir.missing_param",
|
|
11404
|
+
`${prefix}Missing IR switch tag '${node.tag}'`
|
|
11405
|
+
);
|
|
11406
|
+
}
|
|
11407
|
+
const tagNumber = Number(tagVal);
|
|
11408
|
+
for (const caseNode of node.cases) {
|
|
11409
|
+
if (caseNode.value === tagNumber) {
|
|
11410
|
+
return __tnEvalIrNode3(caseNode.node, ctx, baseOffset);
|
|
11411
|
+
}
|
|
11412
|
+
}
|
|
11413
|
+
if (node.default) return __tnEvalIrNode3(node.default, ctx, baseOffset);
|
|
11414
|
+
__tnRaiseIrError3(
|
|
11415
|
+
"tn.ir.invalid_tag",
|
|
11416
|
+
`Unhandled IR switch value ${tagNumber} for '${node.tag}'`
|
|
11417
|
+
);
|
|
11418
|
+
}
|
|
11419
|
+
case "call": {
|
|
11420
|
+
const nestedParams = /* @__PURE__ */ Object.create(null);
|
|
11421
|
+
for (const arg of node.args) {
|
|
11422
|
+
const val = ctx.params[arg.source];
|
|
11423
|
+
if (val === void 0) {
|
|
11424
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
11425
|
+
__tnRaiseIrError3(
|
|
11426
|
+
"tn.ir.missing_param",
|
|
11427
|
+
`${prefix}Missing IR parameter '${arg.source}' for nested call`
|
|
11428
|
+
);
|
|
11429
|
+
}
|
|
11430
|
+
nestedParams[arg.name] = val;
|
|
11431
|
+
}
|
|
11432
|
+
if (ctx.buffer) {
|
|
11433
|
+
const nestedOffset = __tnBigIntToNumber3(baseOffset, "IR nested offset");
|
|
11434
|
+
const nestedResult = __tnInvokeValidate3(
|
|
11435
|
+
node.typeName,
|
|
11436
|
+
ctx.buffer.subarray(nestedOffset),
|
|
11437
|
+
nestedParams
|
|
11438
|
+
);
|
|
11439
|
+
if (!nestedResult.ok) {
|
|
11440
|
+
const nestedCode = nestedResult.code ?? `tn.ir.runtime_error: ${node.typeName}`;
|
|
11441
|
+
const prefixed = nestedCode.startsWith("tn.") ? nestedCode : `tn.ir.runtime_error: ${node.typeName} -> ${nestedCode}`;
|
|
11442
|
+
__tnRaiseIrError3(
|
|
11443
|
+
prefixed,
|
|
11444
|
+
`Nested validator ${node.typeName} failed`
|
|
11445
|
+
);
|
|
11446
|
+
}
|
|
11447
|
+
if (nestedResult.consumed !== void 0) {
|
|
11448
|
+
return nestedResult.consumed;
|
|
11449
|
+
}
|
|
11450
|
+
}
|
|
11451
|
+
return __tnInvokeFootprint3(node.typeName, nestedParams);
|
|
11452
|
+
}
|
|
11453
|
+
case "sumOverArray": {
|
|
11454
|
+
if (!ctx.buffer) {
|
|
11455
|
+
__tnRaiseIrError3(
|
|
11456
|
+
"tn.ir.missing_buffer",
|
|
11457
|
+
`Jagged array '${node.fieldName}' requires buffer-backed validation`
|
|
11458
|
+
);
|
|
11459
|
+
}
|
|
11460
|
+
const count = __tnBigIntToNumber3(
|
|
11461
|
+
__tnEvalIrNode3(node.count, ctx, baseOffset),
|
|
11462
|
+
`Jagged array '${node.fieldName}' count`
|
|
11463
|
+
);
|
|
11464
|
+
let cursor = __tnBigIntToNumber3(baseOffset, "IR jagged array offset");
|
|
11465
|
+
let total = __tnToBigInt3(0);
|
|
11466
|
+
for (let i = 0; i < count; i++) {
|
|
11467
|
+
const result = __tnInvokeDynamicValidate3(
|
|
11468
|
+
node.elementTypeName,
|
|
11469
|
+
ctx.buffer.subarray(cursor)
|
|
11470
|
+
);
|
|
11471
|
+
if (!result.ok || result.consumed === void 0) {
|
|
11472
|
+
const code = result.code ?? "tn.ir.runtime_error";
|
|
11473
|
+
__tnRaiseIrError3(
|
|
11474
|
+
code,
|
|
11475
|
+
`Jagged array '${node.fieldName}' element ${i} failed validation`
|
|
11476
|
+
);
|
|
11477
|
+
}
|
|
11478
|
+
cursor += __tnBigIntToNumber3(result.consumed, "IR jagged element size");
|
|
11479
|
+
total = __tnCheckedAdd3(total, result.consumed);
|
|
11480
|
+
}
|
|
11481
|
+
return total;
|
|
11482
|
+
}
|
|
11483
|
+
default:
|
|
11484
|
+
__tnRaiseIrError3(
|
|
11485
|
+
"tn.ir.runtime_error",
|
|
11486
|
+
`Unsupported IR node ${node.op}`
|
|
11487
|
+
);
|
|
11488
|
+
}
|
|
11489
|
+
}
|
|
11490
|
+
function __tnNormalizeIrError3(err) {
|
|
11491
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
11492
|
+
const maybeCode = err.code;
|
|
11493
|
+
if (typeof maybeCode === "string" && maybeCode.length > 0) {
|
|
11494
|
+
return maybeCode;
|
|
11495
|
+
}
|
|
11496
|
+
}
|
|
11497
|
+
const message = err && typeof err === "object" && "message" in err ? String(err.message ?? "") : typeof err === "string" ? err : "";
|
|
11498
|
+
if (message.includes("Missing IR parameter")) return "tn.ir.missing_param";
|
|
11499
|
+
if (message.includes("Unhandled IR switch value")) return "tn.ir.invalid_tag";
|
|
11500
|
+
if (message.includes("invalid") || message.includes("overflow") || message.includes("negative size")) {
|
|
11501
|
+
return "tn.ir.overflow";
|
|
11502
|
+
}
|
|
11503
|
+
if (message.length > 0) return `tn.ir.runtime_error: ${message}`;
|
|
11504
|
+
return "tn.ir.runtime_error";
|
|
11505
|
+
}
|
|
11506
|
+
var __tn_ir_Date2 = {
|
|
11507
|
+
typeName: "Date",
|
|
11508
|
+
root: { op: "const", value: 6n }
|
|
11509
|
+
};
|
|
11510
|
+
var Date2 = class _Date {
|
|
11511
|
+
constructor(buffer) {
|
|
11512
|
+
this.buffer = buffer;
|
|
11513
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11514
|
+
}
|
|
11515
|
+
static __tnCreateView(buffer, opts) {
|
|
11516
|
+
if (!buffer || buffer.length === void 0) throw new Error("Date.__tnCreateView requires a Uint8Array");
|
|
11517
|
+
return new _Date(new Uint8Array(buffer));
|
|
11518
|
+
}
|
|
11519
|
+
static builder() {
|
|
11520
|
+
return new DateBuilder2();
|
|
11521
|
+
}
|
|
11522
|
+
static fromBuilder(builder) {
|
|
11523
|
+
const buffer = builder.build();
|
|
11524
|
+
return _Date.from_array(buffer);
|
|
11525
|
+
}
|
|
11526
|
+
get_year() {
|
|
11527
|
+
const offset = 0;
|
|
11528
|
+
return this.view.getInt32(offset, true);
|
|
11529
|
+
}
|
|
11530
|
+
set_year(value) {
|
|
11531
|
+
const offset = 0;
|
|
11532
|
+
this.view.setInt32(offset, value, true);
|
|
11533
|
+
}
|
|
11534
|
+
get year() {
|
|
11535
|
+
return this.get_year();
|
|
11536
|
+
}
|
|
11537
|
+
set year(value) {
|
|
11538
|
+
this.set_year(value);
|
|
11539
|
+
}
|
|
11540
|
+
get_month() {
|
|
11541
|
+
const offset = 4;
|
|
11542
|
+
return this.view.getUint8(offset);
|
|
11543
|
+
}
|
|
11544
|
+
set_month(value) {
|
|
11545
|
+
const offset = 4;
|
|
11546
|
+
this.view.setUint8(offset, value);
|
|
11547
|
+
}
|
|
11548
|
+
get month() {
|
|
11549
|
+
return this.get_month();
|
|
11550
|
+
}
|
|
11551
|
+
set month(value) {
|
|
11552
|
+
this.set_month(value);
|
|
11553
|
+
}
|
|
11554
|
+
get_day() {
|
|
11555
|
+
const offset = 5;
|
|
11556
|
+
return this.view.getUint8(offset);
|
|
11557
|
+
}
|
|
11558
|
+
set_day(value) {
|
|
11559
|
+
const offset = 5;
|
|
11560
|
+
this.view.setUint8(offset, value);
|
|
11561
|
+
}
|
|
11562
|
+
get day() {
|
|
11563
|
+
return this.get_day();
|
|
11564
|
+
}
|
|
11565
|
+
set day(value) {
|
|
11566
|
+
this.set_day(value);
|
|
11567
|
+
}
|
|
11568
|
+
static __tnFootprintInternal(__tnParams) {
|
|
11569
|
+
return __tnEvalFootprint3(__tn_ir_Date2.root, { params: __tnParams });
|
|
11570
|
+
}
|
|
11571
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
11572
|
+
return __tnValidateIrTree3(__tn_ir_Date2, buffer, __tnParams);
|
|
11573
|
+
}
|
|
11574
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
11575
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
11576
|
+
}
|
|
11577
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
11578
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
11579
|
+
}
|
|
11580
|
+
static footprintIr() {
|
|
11581
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
11582
|
+
}
|
|
11583
|
+
static footprint() {
|
|
11584
|
+
const irResult = this.footprintIr();
|
|
11585
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
11586
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
11587
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Date");
|
|
11588
|
+
}
|
|
11589
|
+
return __tnBigIntToNumber3(irResult, "Date::footprint");
|
|
11590
|
+
}
|
|
11591
|
+
static validate(buffer, _opts) {
|
|
11592
|
+
if (buffer.length < 6) return { ok: false, code: "tn.buffer_too_small", consumed: 6 };
|
|
11593
|
+
return { ok: true, consumed: 6 };
|
|
11594
|
+
}
|
|
11595
|
+
static new(year, month, day) {
|
|
11596
|
+
const buffer = new Uint8Array(6);
|
|
11597
|
+
const view = new DataView(buffer.buffer);
|
|
11598
|
+
view.setInt32(0, year, true);
|
|
11599
|
+
view.setUint8(4, month);
|
|
11600
|
+
view.setUint8(5, day);
|
|
11601
|
+
return new _Date(buffer);
|
|
11602
|
+
}
|
|
11603
|
+
static from_array(buffer) {
|
|
11604
|
+
if (!buffer || buffer.length === void 0) {
|
|
11605
|
+
return null;
|
|
11606
|
+
}
|
|
11607
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11608
|
+
const validation = this.validate(buffer);
|
|
11609
|
+
if (!validation.ok) {
|
|
11610
|
+
return null;
|
|
11611
|
+
}
|
|
11612
|
+
return new _Date(buffer);
|
|
11613
|
+
}
|
|
11614
|
+
};
|
|
11615
|
+
var DateBuilder2 = class {
|
|
11616
|
+
constructor() {
|
|
11617
|
+
this.buffer = new Uint8Array(6);
|
|
11618
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
11619
|
+
}
|
|
11620
|
+
set_year(value) {
|
|
11621
|
+
this.view.setInt32(0, value, true);
|
|
11622
|
+
return this;
|
|
11623
|
+
}
|
|
11624
|
+
set_month(value) {
|
|
11625
|
+
this.view.setUint8(4, value);
|
|
11626
|
+
return this;
|
|
11627
|
+
}
|
|
11628
|
+
set_day(value) {
|
|
11629
|
+
this.view.setUint8(5, value);
|
|
11630
|
+
return this;
|
|
11631
|
+
}
|
|
11632
|
+
build() {
|
|
11633
|
+
return this.buffer.slice();
|
|
11634
|
+
}
|
|
11635
|
+
buildInto(target, offset = 0) {
|
|
11636
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
11637
|
+
target.set(this.buffer, offset);
|
|
11638
|
+
return target;
|
|
11639
|
+
}
|
|
11640
|
+
finish() {
|
|
11641
|
+
const view = Date2.from_array(this.buffer.slice());
|
|
11642
|
+
if (!view) throw new Error("failed to build Date");
|
|
11643
|
+
return view;
|
|
11644
|
+
}
|
|
11645
|
+
};
|
|
11646
|
+
__tnRegisterFootprint3("Date", (params) => Date2.__tnInvokeFootprint(params));
|
|
11647
|
+
__tnRegisterValidate3("Date", (buffer, params) => Date2.__tnInvokeValidate(buffer, params));
|
|
11648
|
+
__tnRegisterDynamicValidate3("Date", (buffer) => {
|
|
11649
|
+
const result = Date2.validate(buffer);
|
|
11650
|
+
const params = result.params;
|
|
11651
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
11652
|
+
});
|
|
11653
|
+
var __tn_ir_Duration2 = {
|
|
11654
|
+
typeName: "Duration",
|
|
11655
|
+
root: { op: "const", value: 12n }
|
|
11656
|
+
};
|
|
11657
|
+
var Duration2 = class _Duration {
|
|
11658
|
+
constructor(buffer) {
|
|
11659
|
+
this.buffer = buffer;
|
|
11660
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11661
|
+
}
|
|
11662
|
+
static __tnCreateView(buffer, opts) {
|
|
11663
|
+
if (!buffer || buffer.length === void 0) throw new Error("Duration.__tnCreateView requires a Uint8Array");
|
|
11664
|
+
return new _Duration(new Uint8Array(buffer));
|
|
11665
|
+
}
|
|
11666
|
+
static builder() {
|
|
11667
|
+
return new DurationBuilder2();
|
|
11668
|
+
}
|
|
11669
|
+
static fromBuilder(builder) {
|
|
11670
|
+
const buffer = builder.build();
|
|
11671
|
+
return _Duration.from_array(buffer);
|
|
11672
|
+
}
|
|
11673
|
+
get_seconds() {
|
|
11674
|
+
const offset = 0;
|
|
11675
|
+
return this.view.getBigInt64(offset, true);
|
|
11676
|
+
}
|
|
11677
|
+
set_seconds(value) {
|
|
11678
|
+
const offset = 0;
|
|
11679
|
+
this.view.setBigInt64(offset, value, true);
|
|
11680
|
+
}
|
|
11681
|
+
get seconds() {
|
|
11682
|
+
return this.get_seconds();
|
|
11683
|
+
}
|
|
11684
|
+
set seconds(value) {
|
|
11685
|
+
this.set_seconds(value);
|
|
11686
|
+
}
|
|
11687
|
+
get_nanos() {
|
|
11688
|
+
const offset = 8;
|
|
11689
|
+
return this.view.getInt32(offset, true);
|
|
11690
|
+
}
|
|
11691
|
+
set_nanos(value) {
|
|
11692
|
+
const offset = 8;
|
|
11693
|
+
this.view.setInt32(offset, value, true);
|
|
11694
|
+
}
|
|
11695
|
+
get nanos() {
|
|
11696
|
+
return this.get_nanos();
|
|
11697
|
+
}
|
|
11698
|
+
set nanos(value) {
|
|
11699
|
+
this.set_nanos(value);
|
|
11700
|
+
}
|
|
11701
|
+
static __tnFootprintInternal(__tnParams) {
|
|
11702
|
+
return __tnEvalFootprint3(__tn_ir_Duration2.root, { params: __tnParams });
|
|
11703
|
+
}
|
|
11704
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
11705
|
+
return __tnValidateIrTree3(__tn_ir_Duration2, buffer, __tnParams);
|
|
11706
|
+
}
|
|
11707
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
11708
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
11709
|
+
}
|
|
11710
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
11711
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
11712
|
+
}
|
|
11713
|
+
static footprintIr() {
|
|
11714
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
11715
|
+
}
|
|
11716
|
+
static footprint() {
|
|
11717
|
+
const irResult = this.footprintIr();
|
|
11718
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
11719
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
11720
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Duration");
|
|
11721
|
+
}
|
|
11722
|
+
return __tnBigIntToNumber3(irResult, "Duration::footprint");
|
|
11723
|
+
}
|
|
11724
|
+
static validate(buffer, _opts) {
|
|
11725
|
+
if (buffer.length < 12) return { ok: false, code: "tn.buffer_too_small", consumed: 12 };
|
|
11726
|
+
return { ok: true, consumed: 12 };
|
|
11727
|
+
}
|
|
11728
|
+
static new(seconds, nanos) {
|
|
11729
|
+
const buffer = new Uint8Array(12);
|
|
11730
|
+
const view = new DataView(buffer.buffer);
|
|
11731
|
+
view.setBigInt64(0, seconds, true);
|
|
11732
|
+
view.setInt32(8, nanos, true);
|
|
11733
|
+
return new _Duration(buffer);
|
|
11734
|
+
}
|
|
11735
|
+
static from_array(buffer) {
|
|
11736
|
+
if (!buffer || buffer.length === void 0) {
|
|
11737
|
+
return null;
|
|
11738
|
+
}
|
|
11739
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11740
|
+
const validation = this.validate(buffer);
|
|
11741
|
+
if (!validation.ok) {
|
|
11742
|
+
return null;
|
|
11743
|
+
}
|
|
11744
|
+
return new _Duration(buffer);
|
|
11745
|
+
}
|
|
11746
|
+
};
|
|
11747
|
+
var DurationBuilder2 = class {
|
|
11748
|
+
constructor() {
|
|
11749
|
+
this.buffer = new Uint8Array(12);
|
|
11750
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
11751
|
+
}
|
|
11752
|
+
set_seconds(value) {
|
|
11753
|
+
const cast = __tnToBigInt3(value);
|
|
11754
|
+
this.view.setBigInt64(0, cast, true);
|
|
11755
|
+
return this;
|
|
11756
|
+
}
|
|
11757
|
+
set_nanos(value) {
|
|
11758
|
+
this.view.setInt32(8, value, true);
|
|
11759
|
+
return this;
|
|
11760
|
+
}
|
|
11761
|
+
build() {
|
|
11762
|
+
return this.buffer.slice();
|
|
11763
|
+
}
|
|
11764
|
+
buildInto(target, offset = 0) {
|
|
11765
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
11766
|
+
target.set(this.buffer, offset);
|
|
11767
|
+
return target;
|
|
11768
|
+
}
|
|
11769
|
+
finish() {
|
|
11770
|
+
const view = Duration2.from_array(this.buffer.slice());
|
|
11771
|
+
if (!view) throw new Error("failed to build Duration");
|
|
11772
|
+
return view;
|
|
11773
|
+
}
|
|
11774
|
+
};
|
|
11775
|
+
__tnRegisterFootprint3("Duration", (params) => Duration2.__tnInvokeFootprint(params));
|
|
11776
|
+
__tnRegisterValidate3("Duration", (buffer, params) => Duration2.__tnInvokeValidate(buffer, params));
|
|
11777
|
+
__tnRegisterDynamicValidate3("Duration", (buffer) => {
|
|
11778
|
+
const result = Duration2.validate(buffer);
|
|
11779
|
+
const params = result.params;
|
|
11780
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
11781
|
+
});
|
|
11782
|
+
var __tn_ir_FixedPoint2 = {
|
|
11783
|
+
typeName: "FixedPoint",
|
|
11784
|
+
root: { op: "const", value: 9n }
|
|
11785
|
+
};
|
|
11786
|
+
var FixedPoint2 = class _FixedPoint {
|
|
11787
|
+
constructor(buffer) {
|
|
11788
|
+
this.buffer = buffer;
|
|
11789
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11790
|
+
}
|
|
11791
|
+
static __tnCreateView(buffer, opts) {
|
|
11792
|
+
if (!buffer || buffer.length === void 0) throw new Error("FixedPoint.__tnCreateView requires a Uint8Array");
|
|
11793
|
+
return new _FixedPoint(new Uint8Array(buffer));
|
|
11794
|
+
}
|
|
11795
|
+
static builder() {
|
|
11796
|
+
return new FixedPointBuilder2();
|
|
11797
|
+
}
|
|
11798
|
+
static fromBuilder(builder) {
|
|
11799
|
+
const buffer = builder.build();
|
|
11800
|
+
return _FixedPoint.from_array(buffer);
|
|
11801
|
+
}
|
|
11802
|
+
get_mantissa() {
|
|
11803
|
+
const offset = 0;
|
|
11804
|
+
return this.view.getBigInt64(offset, true);
|
|
11805
|
+
}
|
|
11806
|
+
set_mantissa(value) {
|
|
11807
|
+
const offset = 0;
|
|
11808
|
+
this.view.setBigInt64(offset, value, true);
|
|
11809
|
+
}
|
|
11810
|
+
get mantissa() {
|
|
11811
|
+
return this.get_mantissa();
|
|
11812
|
+
}
|
|
11813
|
+
set mantissa(value) {
|
|
11814
|
+
this.set_mantissa(value);
|
|
11815
|
+
}
|
|
11816
|
+
get_scale() {
|
|
11817
|
+
const offset = 8;
|
|
11818
|
+
return this.view.getUint8(offset);
|
|
11819
|
+
}
|
|
11820
|
+
set_scale(value) {
|
|
11821
|
+
const offset = 8;
|
|
11822
|
+
this.view.setUint8(offset, value);
|
|
11823
|
+
}
|
|
11824
|
+
get scale() {
|
|
11825
|
+
return this.get_scale();
|
|
11826
|
+
}
|
|
11827
|
+
set scale(value) {
|
|
11828
|
+
this.set_scale(value);
|
|
11829
|
+
}
|
|
11830
|
+
static __tnFootprintInternal(__tnParams) {
|
|
11831
|
+
return __tnEvalFootprint3(__tn_ir_FixedPoint2.root, { params: __tnParams });
|
|
11832
|
+
}
|
|
11833
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
11834
|
+
return __tnValidateIrTree3(__tn_ir_FixedPoint2, buffer, __tnParams);
|
|
11835
|
+
}
|
|
11836
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
11837
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
11838
|
+
}
|
|
11839
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
11840
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
11841
|
+
}
|
|
11842
|
+
static footprintIr() {
|
|
11843
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
11844
|
+
}
|
|
11845
|
+
static footprint() {
|
|
11846
|
+
const irResult = this.footprintIr();
|
|
11847
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
11848
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
11849
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for FixedPoint");
|
|
11850
|
+
}
|
|
11851
|
+
return __tnBigIntToNumber3(irResult, "FixedPoint::footprint");
|
|
11852
|
+
}
|
|
11853
|
+
static validate(buffer, _opts) {
|
|
11854
|
+
if (buffer.length < 9) return { ok: false, code: "tn.buffer_too_small", consumed: 9 };
|
|
11855
|
+
return { ok: true, consumed: 9 };
|
|
11856
|
+
}
|
|
11857
|
+
static new(mantissa, scale) {
|
|
11858
|
+
const buffer = new Uint8Array(9);
|
|
11859
|
+
const view = new DataView(buffer.buffer);
|
|
11860
|
+
view.setBigInt64(0, mantissa, true);
|
|
11861
|
+
view.setUint8(8, scale);
|
|
11862
|
+
return new _FixedPoint(buffer);
|
|
11863
|
+
}
|
|
11864
|
+
static from_array(buffer) {
|
|
11865
|
+
if (!buffer || buffer.length === void 0) {
|
|
11866
|
+
return null;
|
|
11867
|
+
}
|
|
11868
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11869
|
+
const validation = this.validate(buffer);
|
|
11870
|
+
if (!validation.ok) {
|
|
11871
|
+
return null;
|
|
11872
|
+
}
|
|
11873
|
+
return new _FixedPoint(buffer);
|
|
11874
|
+
}
|
|
11875
|
+
};
|
|
11876
|
+
var FixedPointBuilder2 = class {
|
|
11877
|
+
constructor() {
|
|
11878
|
+
this.buffer = new Uint8Array(9);
|
|
11879
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
11880
|
+
}
|
|
11881
|
+
set_mantissa(value) {
|
|
11882
|
+
const cast = __tnToBigInt3(value);
|
|
11883
|
+
this.view.setBigInt64(0, cast, true);
|
|
11884
|
+
return this;
|
|
11885
|
+
}
|
|
11886
|
+
set_scale(value) {
|
|
11887
|
+
this.view.setUint8(8, value);
|
|
11888
|
+
return this;
|
|
11889
|
+
}
|
|
11890
|
+
build() {
|
|
11891
|
+
return this.buffer.slice();
|
|
11892
|
+
}
|
|
11893
|
+
buildInto(target, offset = 0) {
|
|
11894
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
11895
|
+
target.set(this.buffer, offset);
|
|
11896
|
+
return target;
|
|
11897
|
+
}
|
|
11898
|
+
finish() {
|
|
11899
|
+
const view = FixedPoint2.from_array(this.buffer.slice());
|
|
11900
|
+
if (!view) throw new Error("failed to build FixedPoint");
|
|
11901
|
+
return view;
|
|
11902
|
+
}
|
|
11903
|
+
};
|
|
11904
|
+
__tnRegisterFootprint3("FixedPoint", (params) => FixedPoint2.__tnInvokeFootprint(params));
|
|
11905
|
+
__tnRegisterValidate3("FixedPoint", (buffer, params) => FixedPoint2.__tnInvokeValidate(buffer, params));
|
|
11906
|
+
__tnRegisterDynamicValidate3("FixedPoint", (buffer) => {
|
|
11907
|
+
const result = FixedPoint2.validate(buffer);
|
|
11908
|
+
const params = result.params;
|
|
11909
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
11910
|
+
});
|
|
11911
|
+
var __tn_ir_Hash2 = {
|
|
11912
|
+
typeName: "Hash",
|
|
11913
|
+
root: { op: "const", value: 32n }
|
|
11914
|
+
};
|
|
11915
|
+
var Hash2 = class _Hash {
|
|
11916
|
+
constructor(buffer) {
|
|
11917
|
+
this.buffer = buffer;
|
|
11918
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11919
|
+
}
|
|
11920
|
+
static __tnCreateView(buffer, opts) {
|
|
11921
|
+
if (!buffer || buffer.length === void 0) throw new Error("Hash.__tnCreateView requires a Uint8Array");
|
|
11922
|
+
return new _Hash(new Uint8Array(buffer));
|
|
11923
|
+
}
|
|
11924
|
+
static builder() {
|
|
11925
|
+
return new HashBuilder2();
|
|
11926
|
+
}
|
|
11927
|
+
static fromBuilder(builder) {
|
|
11928
|
+
const buffer = builder.build();
|
|
11929
|
+
return _Hash.from_array(buffer);
|
|
11930
|
+
}
|
|
11931
|
+
get_bytes() {
|
|
11932
|
+
const offset = 0;
|
|
11933
|
+
const result = [];
|
|
11934
|
+
for (let i = 0; i < 32; i++) {
|
|
11935
|
+
result.push(this.view.getUint8(offset + i * 1));
|
|
11936
|
+
}
|
|
11937
|
+
return result;
|
|
11938
|
+
}
|
|
11939
|
+
set_bytes(value) {
|
|
11940
|
+
const offset = 0;
|
|
11941
|
+
if (value.length !== 32) {
|
|
11942
|
+
throw new Error("Array length must be 32");
|
|
11943
|
+
}
|
|
11944
|
+
for (let i = 0; i < 32; i++) {
|
|
11945
|
+
this.view.setUint8(offset + i * 1, value[i]);
|
|
11946
|
+
}
|
|
11947
|
+
}
|
|
11948
|
+
get bytes() {
|
|
11949
|
+
return this.get_bytes();
|
|
11950
|
+
}
|
|
11951
|
+
set bytes(value) {
|
|
11952
|
+
this.set_bytes(value);
|
|
11953
|
+
}
|
|
11954
|
+
static __tnFootprintInternal(__tnParams) {
|
|
11955
|
+
return __tnEvalFootprint3(__tn_ir_Hash2.root, { params: __tnParams });
|
|
11956
|
+
}
|
|
11957
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
11958
|
+
return __tnValidateIrTree3(__tn_ir_Hash2, buffer, __tnParams);
|
|
11959
|
+
}
|
|
11960
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
11961
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
11962
|
+
}
|
|
11963
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
11964
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
11965
|
+
}
|
|
11966
|
+
static footprintIr() {
|
|
11967
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
11968
|
+
}
|
|
11969
|
+
static footprint() {
|
|
11970
|
+
const irResult = this.footprintIr();
|
|
11971
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
11972
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
11973
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Hash");
|
|
11974
|
+
}
|
|
11975
|
+
return __tnBigIntToNumber3(irResult, "Hash::footprint");
|
|
11976
|
+
}
|
|
11977
|
+
static validate(buffer, _opts) {
|
|
11978
|
+
if (buffer.length < 32) return { ok: false, code: "tn.buffer_too_small", consumed: 32 };
|
|
11979
|
+
return { ok: true, consumed: 32 };
|
|
11980
|
+
}
|
|
11981
|
+
static from_array(buffer) {
|
|
11982
|
+
if (!buffer || buffer.length === void 0) {
|
|
11983
|
+
return null;
|
|
11984
|
+
}
|
|
11985
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
11986
|
+
const validation = this.validate(buffer);
|
|
11987
|
+
if (!validation.ok) {
|
|
11988
|
+
return null;
|
|
11989
|
+
}
|
|
11990
|
+
return new _Hash(buffer);
|
|
11991
|
+
}
|
|
11992
|
+
};
|
|
11993
|
+
var HashBuilder2 = class {
|
|
11994
|
+
constructor() {
|
|
11995
|
+
this.buffer = new Uint8Array(32);
|
|
11996
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
11997
|
+
}
|
|
11998
|
+
set_bytes(values) {
|
|
11999
|
+
if (values.length !== 32) throw new Error("bytes expects 32 elements");
|
|
12000
|
+
for (let i = 0; i < values.length; i++) {
|
|
12001
|
+
const byteOffset = 0 + i * 1;
|
|
12002
|
+
this.view.setUint8(byteOffset, values[i]);
|
|
12003
|
+
}
|
|
12004
|
+
return this;
|
|
12005
|
+
}
|
|
12006
|
+
build() {
|
|
12007
|
+
return this.buffer.slice();
|
|
12008
|
+
}
|
|
12009
|
+
buildInto(target, offset = 0) {
|
|
12010
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
12011
|
+
target.set(this.buffer, offset);
|
|
12012
|
+
return target;
|
|
12013
|
+
}
|
|
12014
|
+
finish() {
|
|
12015
|
+
const view = Hash2.from_array(this.buffer.slice());
|
|
12016
|
+
if (!view) throw new Error("failed to build Hash");
|
|
12017
|
+
return view;
|
|
12018
|
+
}
|
|
12019
|
+
};
|
|
12020
|
+
__tnRegisterFootprint3("Hash", (params) => Hash2.__tnInvokeFootprint(params));
|
|
12021
|
+
__tnRegisterValidate3("Hash", (buffer, params) => Hash2.__tnInvokeValidate(buffer, params));
|
|
12022
|
+
__tnRegisterDynamicValidate3("Hash", (buffer) => {
|
|
12023
|
+
const result = Hash2.validate(buffer);
|
|
12024
|
+
const params = result.params;
|
|
12025
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
12026
|
+
});
|
|
12027
|
+
var __tn_ir_InstructionData2 = {
|
|
12028
|
+
typeName: "InstructionData",
|
|
12029
|
+
root: { op: "align", alignment: 1, node: { op: "add", left: { op: "add", left: { op: "align", alignment: 2, node: { op: "const", value: 2n } }, right: { op: "align", alignment: 8, node: { op: "const", value: 8n } } }, right: { op: "align", alignment: 1, node: { op: "mul", left: { op: "field", param: "data.data_size" }, right: { op: "const", value: 1n } } } } }
|
|
12030
|
+
};
|
|
12031
|
+
var _InstructionData2 = class _InstructionData2 {
|
|
12032
|
+
constructor(buffer, params, fieldContext) {
|
|
12033
|
+
this.buffer = buffer;
|
|
12034
|
+
this.__tnFieldContext = null;
|
|
12035
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12036
|
+
this.__tnFieldContext = fieldContext ?? null;
|
|
12037
|
+
if (params) {
|
|
12038
|
+
this.__tnParams = params;
|
|
12039
|
+
} else {
|
|
12040
|
+
const derived = _InstructionData2.__tnExtractParams(this.view, buffer);
|
|
12041
|
+
if (!derived) {
|
|
12042
|
+
throw new Error("InstructionData: failed to derive dynamic parameters");
|
|
12043
|
+
}
|
|
12044
|
+
this.__tnParams = derived.params;
|
|
12045
|
+
}
|
|
12046
|
+
}
|
|
12047
|
+
static __tnCreateView(buffer, opts) {
|
|
12048
|
+
if (!buffer || buffer.length === void 0) throw new Error("InstructionData.__tnCreateView requires a Uint8Array");
|
|
12049
|
+
let params = opts?.params ?? null;
|
|
12050
|
+
if (!params) {
|
|
12051
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12052
|
+
const derived = _InstructionData2.__tnExtractParams(view, buffer);
|
|
12053
|
+
if (!derived) throw new Error("InstructionData.__tnCreateView: failed to derive params");
|
|
12054
|
+
params = derived.params;
|
|
12055
|
+
}
|
|
12056
|
+
const instance = new _InstructionData2(new Uint8Array(buffer), params, opts?.fieldContext);
|
|
12057
|
+
return instance;
|
|
12058
|
+
}
|
|
12059
|
+
dynamicParams() {
|
|
12060
|
+
return this.__tnParams;
|
|
12061
|
+
}
|
|
12062
|
+
withFieldContext(context) {
|
|
12063
|
+
this.__tnFieldContext = context;
|
|
12064
|
+
return this;
|
|
12065
|
+
}
|
|
12066
|
+
__tnResolveFieldRef(path) {
|
|
12067
|
+
const getterName = `get_${path.replace(/[.]/g, "_")}`;
|
|
12068
|
+
const getter = this[getterName];
|
|
12069
|
+
if (typeof getter === "function") {
|
|
12070
|
+
const value = getter.call(this);
|
|
12071
|
+
return typeof value === "bigint" ? __tnBigIntToNumber3(value, "InstructionData::__tnResolveFieldRef") : value;
|
|
12072
|
+
}
|
|
12073
|
+
if (this.__tnFieldContext && Object.prototype.hasOwnProperty.call(this.__tnFieldContext, path)) {
|
|
12074
|
+
const contextValue = this.__tnFieldContext[path];
|
|
12075
|
+
return typeof contextValue === "bigint" ? __tnBigIntToNumber3(contextValue, "InstructionData::__tnResolveFieldRef") : contextValue;
|
|
12076
|
+
}
|
|
12077
|
+
throw new Error("InstructionData: field reference '" + path + "' is not available; provide fieldContext when creating this view");
|
|
12078
|
+
}
|
|
12079
|
+
static builder() {
|
|
12080
|
+
return new InstructionDataBuilder2();
|
|
12081
|
+
}
|
|
12082
|
+
static fromBuilder(builder) {
|
|
12083
|
+
const buffer = builder.build();
|
|
12084
|
+
const params = builder.dynamicParams();
|
|
12085
|
+
return _InstructionData2.from_array(buffer, { params });
|
|
12086
|
+
}
|
|
12087
|
+
static __tnExtractParams(view, buffer) {
|
|
12088
|
+
if (buffer.length < 10) {
|
|
12089
|
+
return null;
|
|
12090
|
+
}
|
|
12091
|
+
const __tnParam_data_data_size = __tnToBigInt3(view.getBigUint64(2, true));
|
|
12092
|
+
const __tnExtractedParams = _InstructionData2.Params.fromValues({
|
|
12093
|
+
data_data_size: __tnParam_data_data_size
|
|
12094
|
+
});
|
|
12095
|
+
return { params: __tnExtractedParams, derived: null };
|
|
12096
|
+
}
|
|
12097
|
+
get_program_idx() {
|
|
12098
|
+
const offset = 0;
|
|
12099
|
+
return this.view.getUint16(offset, true);
|
|
12100
|
+
}
|
|
12101
|
+
set_program_idx(value) {
|
|
12102
|
+
const offset = 0;
|
|
12103
|
+
this.view.setUint16(offset, value, true);
|
|
12104
|
+
}
|
|
12105
|
+
get program_idx() {
|
|
12106
|
+
return this.get_program_idx();
|
|
12107
|
+
}
|
|
12108
|
+
set program_idx(value) {
|
|
12109
|
+
this.set_program_idx(value);
|
|
12110
|
+
}
|
|
12111
|
+
get_data_size() {
|
|
12112
|
+
const offset = 2;
|
|
12113
|
+
return this.view.getBigUint64(offset, true);
|
|
12114
|
+
}
|
|
12115
|
+
set_data_size(value) {
|
|
12116
|
+
const offset = 2;
|
|
12117
|
+
this.view.setBigUint64(offset, value, true);
|
|
12118
|
+
}
|
|
12119
|
+
get data_size() {
|
|
12120
|
+
return this.get_data_size();
|
|
12121
|
+
}
|
|
12122
|
+
set data_size(value) {
|
|
12123
|
+
this.set_data_size(value);
|
|
12124
|
+
}
|
|
12125
|
+
get_data_length() {
|
|
12126
|
+
return this.__tnResolveFieldRef("data_size");
|
|
12127
|
+
}
|
|
12128
|
+
get_data_at(index) {
|
|
12129
|
+
const offset = 10;
|
|
12130
|
+
return this.view.getUint8(offset + index * 1);
|
|
12131
|
+
}
|
|
12132
|
+
get_data() {
|
|
12133
|
+
const len = this.get_data_length();
|
|
12134
|
+
const result = [];
|
|
12135
|
+
for (let i = 0; i < len; i++) {
|
|
12136
|
+
result.push(this.get_data_at(i));
|
|
12137
|
+
}
|
|
12138
|
+
return result;
|
|
12139
|
+
}
|
|
12140
|
+
set_data_at(index, value) {
|
|
12141
|
+
const offset = 10;
|
|
12142
|
+
this.view.setUint8(offset + index * 1, value);
|
|
12143
|
+
}
|
|
12144
|
+
set_data(value) {
|
|
12145
|
+
const len = Math.min(this.get_data_length(), value.length);
|
|
12146
|
+
for (let i = 0; i < len; i++) {
|
|
12147
|
+
this.set_data_at(i, value[i]);
|
|
12148
|
+
}
|
|
12149
|
+
}
|
|
12150
|
+
get data() {
|
|
12151
|
+
return this.get_data();
|
|
12152
|
+
}
|
|
12153
|
+
set data(value) {
|
|
12154
|
+
this.set_data(value);
|
|
12155
|
+
}
|
|
12156
|
+
static __tnFootprintInternal(__tnParams) {
|
|
12157
|
+
return __tnEvalFootprint3(__tn_ir_InstructionData2.root, { params: __tnParams });
|
|
12158
|
+
}
|
|
12159
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
12160
|
+
return __tnValidateIrTree3(__tn_ir_InstructionData2, buffer, __tnParams);
|
|
12161
|
+
}
|
|
12162
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
12163
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
12164
|
+
}
|
|
12165
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
12166
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
12167
|
+
}
|
|
12168
|
+
static footprintIr(data_data_size) {
|
|
12169
|
+
const params = _InstructionData2.Params.fromValues({
|
|
12170
|
+
data_data_size
|
|
12171
|
+
});
|
|
12172
|
+
return this.footprintIrFromParams(params);
|
|
12173
|
+
}
|
|
12174
|
+
static __tnPackParams(params) {
|
|
12175
|
+
const record = /* @__PURE__ */ Object.create(null);
|
|
12176
|
+
record["data.data_size"] = params.data_data_size;
|
|
12177
|
+
return record;
|
|
12178
|
+
}
|
|
12179
|
+
static footprintIrFromParams(params) {
|
|
12180
|
+
const __tnParams = this.__tnPackParams(params);
|
|
12181
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
12182
|
+
}
|
|
12183
|
+
static footprintFromParams(params) {
|
|
12184
|
+
const irResult = this.footprintIrFromParams(params);
|
|
12185
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
12186
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for InstructionData");
|
|
12187
|
+
return __tnBigIntToNumber3(irResult, "InstructionData::footprintFromParams");
|
|
12188
|
+
}
|
|
12189
|
+
static footprintFromValues(input) {
|
|
12190
|
+
const params = _InstructionData2.params(input);
|
|
12191
|
+
return this.footprintFromParams(params);
|
|
12192
|
+
}
|
|
12193
|
+
static footprint(params) {
|
|
12194
|
+
return this.footprintFromParams(params);
|
|
12195
|
+
}
|
|
12196
|
+
static validate(buffer, opts) {
|
|
12197
|
+
if (!buffer || buffer.length === void 0) {
|
|
12198
|
+
return { ok: false, code: "tn.invalid_buffer" };
|
|
12199
|
+
}
|
|
12200
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12201
|
+
let params = opts?.params ?? null;
|
|
12202
|
+
if (!params) {
|
|
12203
|
+
const extracted = this.__tnExtractParams(view, buffer);
|
|
12204
|
+
if (!extracted) return { ok: false, code: "tn.param_extraction_failed" };
|
|
12205
|
+
params = extracted.params;
|
|
12206
|
+
}
|
|
12207
|
+
const __tnParamsRec = this.__tnPackParams(params);
|
|
12208
|
+
const irResult = this.__tnValidateInternal(buffer, __tnParamsRec);
|
|
12209
|
+
if (!irResult.ok) {
|
|
12210
|
+
return { ok: false, code: irResult.code, consumed: irResult.consumed ? __tnBigIntToNumber3(irResult.consumed, "InstructionData::validate") : void 0, params };
|
|
12211
|
+
}
|
|
12212
|
+
const consumed = irResult.consumed ? __tnBigIntToNumber3(irResult.consumed, "InstructionData::validate") : void 0;
|
|
12213
|
+
return { ok: true, consumed, params };
|
|
12214
|
+
}
|
|
12215
|
+
static from_array(buffer, opts) {
|
|
12216
|
+
if (!buffer || buffer.length === void 0) {
|
|
12217
|
+
return null;
|
|
12218
|
+
}
|
|
12219
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12220
|
+
let params = opts?.params ?? null;
|
|
12221
|
+
if (!params) {
|
|
12222
|
+
const derived = this.__tnExtractParams(view, buffer);
|
|
12223
|
+
if (!derived) return null;
|
|
12224
|
+
params = derived.params;
|
|
12225
|
+
}
|
|
12226
|
+
const validation = this.validate(buffer, { params });
|
|
12227
|
+
if (!validation.ok) {
|
|
12228
|
+
return null;
|
|
12229
|
+
}
|
|
12230
|
+
const cached = validation.params ?? params;
|
|
12231
|
+
const state = new _InstructionData2(buffer, cached);
|
|
12232
|
+
return state;
|
|
12233
|
+
}
|
|
12234
|
+
};
|
|
12235
|
+
_InstructionData2.flexibleArrayWriters = Object.freeze([
|
|
12236
|
+
{ field: "data", method: "data", sizeField: "data_size", paramKey: "data_size", elementSize: 1 }
|
|
12237
|
+
]);
|
|
12238
|
+
var InstructionData2 = _InstructionData2;
|
|
12239
|
+
((InstructionData3) => {
|
|
12240
|
+
InstructionData3.ParamKeys = Object.freeze({
|
|
12241
|
+
data_data_size: "data.data_size"
|
|
12242
|
+
});
|
|
12243
|
+
InstructionData3.Params = {
|
|
12244
|
+
fromValues(input) {
|
|
12245
|
+
return {
|
|
12246
|
+
data_data_size: __tnToBigInt3(input.data_data_size)
|
|
12247
|
+
};
|
|
12248
|
+
},
|
|
12249
|
+
fromBuilder(source) {
|
|
12250
|
+
if (source.dynamicParams) {
|
|
12251
|
+
return source.dynamicParams();
|
|
12252
|
+
}
|
|
12253
|
+
if (source.params) {
|
|
12254
|
+
return source.params;
|
|
12255
|
+
}
|
|
12256
|
+
return source;
|
|
12257
|
+
}
|
|
12258
|
+
};
|
|
12259
|
+
function params(input) {
|
|
12260
|
+
return InstructionData3.Params.fromValues(input);
|
|
12261
|
+
}
|
|
12262
|
+
InstructionData3.params = params;
|
|
12263
|
+
})(InstructionData2 || (InstructionData2 = {}));
|
|
12264
|
+
var InstructionDataBuilder2 = class {
|
|
12265
|
+
constructor() {
|
|
12266
|
+
this.__tnCachedParams = null;
|
|
12267
|
+
this.__tnLastBuffer = null;
|
|
12268
|
+
this.__tnLastParams = null;
|
|
12269
|
+
this.__tnFam_data = null;
|
|
12270
|
+
this.__tnFam_dataCount = null;
|
|
12271
|
+
this.buffer = new Uint8Array(10);
|
|
12272
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
12273
|
+
}
|
|
12274
|
+
__tnInvalidate() {
|
|
12275
|
+
this.__tnCachedParams = null;
|
|
12276
|
+
this.__tnLastBuffer = null;
|
|
12277
|
+
this.__tnLastParams = null;
|
|
12278
|
+
}
|
|
12279
|
+
set_program_idx(value) {
|
|
12280
|
+
this.view.setUint16(0, value, true);
|
|
12281
|
+
this.__tnInvalidate();
|
|
12282
|
+
return this;
|
|
12283
|
+
}
|
|
12284
|
+
set_data_size(value) {
|
|
12285
|
+
const cast = __tnToBigInt3(value);
|
|
12286
|
+
this.view.setBigUint64(2, cast, true);
|
|
12287
|
+
this.__tnInvalidate();
|
|
12288
|
+
return this;
|
|
12289
|
+
}
|
|
12290
|
+
data() {
|
|
12291
|
+
if (!this.__tnFamWriter_data) {
|
|
12292
|
+
this.__tnFamWriter_data = __tnCreateFamWriter3(this, "data", (payload) => {
|
|
12293
|
+
const bytes = new Uint8Array(payload);
|
|
12294
|
+
const elementCount = bytes.length;
|
|
12295
|
+
this.__tnFam_data = bytes;
|
|
12296
|
+
this.__tnFam_dataCount = elementCount;
|
|
12297
|
+
this.set_data_size(__tnToBigInt3(elementCount));
|
|
12298
|
+
this.__tnInvalidate();
|
|
12299
|
+
});
|
|
12300
|
+
}
|
|
12301
|
+
return this.__tnFamWriter_data;
|
|
12302
|
+
}
|
|
12303
|
+
build() {
|
|
12304
|
+
const params = this.__tnComputeParams();
|
|
12305
|
+
const size = InstructionData2.footprintFromParams(params);
|
|
12306
|
+
const buffer = new Uint8Array(size);
|
|
12307
|
+
this.__tnWriteInto(buffer);
|
|
12308
|
+
this.__tnValidateOrThrow(buffer, params);
|
|
12309
|
+
return buffer;
|
|
12310
|
+
}
|
|
12311
|
+
buildInto(target, offset = 0) {
|
|
12312
|
+
const params = this.__tnComputeParams();
|
|
12313
|
+
const size = InstructionData2.footprintFromParams(params);
|
|
12314
|
+
if (target.length - offset < size) throw new Error("InstructionDataBuilder: target buffer too small");
|
|
12315
|
+
const slice = target.subarray(offset, offset + size);
|
|
12316
|
+
this.__tnWriteInto(slice);
|
|
12317
|
+
this.__tnValidateOrThrow(slice, params);
|
|
12318
|
+
return target;
|
|
12319
|
+
}
|
|
12320
|
+
finish() {
|
|
12321
|
+
const buffer = this.build();
|
|
12322
|
+
const params = this.__tnLastParams ?? this.__tnComputeParams();
|
|
12323
|
+
const view = InstructionData2.from_array(buffer, { params });
|
|
12324
|
+
if (!view) throw new Error("InstructionDataBuilder: failed to finalize view");
|
|
12325
|
+
return view;
|
|
12326
|
+
}
|
|
12327
|
+
finishView() {
|
|
12328
|
+
return this.finish();
|
|
12329
|
+
}
|
|
12330
|
+
dynamicParams() {
|
|
12331
|
+
return this.__tnComputeParams();
|
|
12332
|
+
}
|
|
12333
|
+
__tnComputeParams() {
|
|
12334
|
+
if (this.__tnCachedParams) return this.__tnCachedParams;
|
|
12335
|
+
const params = InstructionData2.Params.fromValues({
|
|
12336
|
+
data_data_size: (() => {
|
|
12337
|
+
if (this.__tnFam_dataCount === null) throw new Error("InstructionDataBuilder: field 'data' must be written before computing params");
|
|
12338
|
+
return __tnToBigInt3(this.__tnFam_dataCount);
|
|
12339
|
+
})()
|
|
12340
|
+
});
|
|
12341
|
+
this.__tnCachedParams = params;
|
|
12342
|
+
return params;
|
|
12343
|
+
}
|
|
12344
|
+
__tnWriteInto(target) {
|
|
12345
|
+
target.set(this.buffer, 0);
|
|
12346
|
+
let cursor = this.buffer.length;
|
|
12347
|
+
const __tnLocal_data_bytes = this.__tnFam_data;
|
|
12348
|
+
if (!__tnLocal_data_bytes) throw new Error("InstructionDataBuilder: field 'data' must be written before build");
|
|
12349
|
+
target.set(__tnLocal_data_bytes, cursor);
|
|
12350
|
+
cursor += __tnLocal_data_bytes.length;
|
|
12351
|
+
}
|
|
12352
|
+
__tnValidateOrThrow(buffer, params) {
|
|
12353
|
+
const result = InstructionData2.validate(buffer, { params });
|
|
12354
|
+
if (!result.ok) {
|
|
12355
|
+
throw new Error(`${InstructionData2}Builder: builder produced invalid buffer (code=${result.code ?? "unknown"})`);
|
|
12356
|
+
}
|
|
12357
|
+
this.__tnLastParams = result.params ?? params;
|
|
12358
|
+
this.__tnLastBuffer = buffer;
|
|
12359
|
+
}
|
|
12360
|
+
};
|
|
12361
|
+
__tnRegisterFootprint3("InstructionData", (params) => InstructionData2.__tnInvokeFootprint(params));
|
|
12362
|
+
__tnRegisterValidate3("InstructionData", (buffer, params) => InstructionData2.__tnInvokeValidate(buffer, params));
|
|
12363
|
+
__tnRegisterDynamicValidate3("InstructionData", (buffer) => {
|
|
12364
|
+
const result = InstructionData2.validate(buffer);
|
|
12365
|
+
const params = result.params;
|
|
12366
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
12367
|
+
});
|
|
12368
|
+
var __tn_ir_Pubkey2 = {
|
|
12369
|
+
typeName: "Pubkey",
|
|
12370
|
+
root: { op: "const", value: 32n }
|
|
12371
|
+
};
|
|
12372
|
+
var Pubkey2 = class _Pubkey {
|
|
12373
|
+
constructor(buffer) {
|
|
12374
|
+
this.buffer = buffer;
|
|
12375
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12376
|
+
}
|
|
12377
|
+
static __tnCreateView(buffer, opts) {
|
|
12378
|
+
if (!buffer || buffer.length === void 0) throw new Error("Pubkey.__tnCreateView requires a Uint8Array");
|
|
12379
|
+
return new _Pubkey(new Uint8Array(buffer));
|
|
12380
|
+
}
|
|
12381
|
+
static builder() {
|
|
12382
|
+
return new PubkeyBuilder2();
|
|
12383
|
+
}
|
|
12384
|
+
static fromBuilder(builder) {
|
|
12385
|
+
const buffer = builder.build();
|
|
12386
|
+
return _Pubkey.from_array(buffer);
|
|
12387
|
+
}
|
|
12388
|
+
get_bytes() {
|
|
12389
|
+
const offset = 0;
|
|
12390
|
+
const result = [];
|
|
12391
|
+
for (let i = 0; i < 32; i++) {
|
|
12392
|
+
result.push(this.view.getUint8(offset + i * 1));
|
|
12393
|
+
}
|
|
12394
|
+
return result;
|
|
12395
|
+
}
|
|
12396
|
+
set_bytes(value) {
|
|
12397
|
+
const offset = 0;
|
|
12398
|
+
if (value.length !== 32) {
|
|
12399
|
+
throw new Error("Array length must be 32");
|
|
12400
|
+
}
|
|
12401
|
+
for (let i = 0; i < 32; i++) {
|
|
12402
|
+
this.view.setUint8(offset + i * 1, value[i]);
|
|
12403
|
+
}
|
|
12404
|
+
}
|
|
12405
|
+
get bytes() {
|
|
12406
|
+
return this.get_bytes();
|
|
12407
|
+
}
|
|
12408
|
+
set bytes(value) {
|
|
12409
|
+
this.set_bytes(value);
|
|
12410
|
+
}
|
|
12411
|
+
static __tnFootprintInternal(__tnParams) {
|
|
12412
|
+
return __tnEvalFootprint3(__tn_ir_Pubkey2.root, { params: __tnParams });
|
|
12413
|
+
}
|
|
12414
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
12415
|
+
return __tnValidateIrTree3(__tn_ir_Pubkey2, buffer, __tnParams);
|
|
12416
|
+
}
|
|
12417
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
12418
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
12419
|
+
}
|
|
12420
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
12421
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
12422
|
+
}
|
|
12423
|
+
static footprintIr() {
|
|
12424
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
12425
|
+
}
|
|
12426
|
+
static footprint() {
|
|
12427
|
+
const irResult = this.footprintIr();
|
|
12428
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
12429
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
12430
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Pubkey");
|
|
12431
|
+
}
|
|
12432
|
+
return __tnBigIntToNumber3(irResult, "Pubkey::footprint");
|
|
12433
|
+
}
|
|
12434
|
+
static validate(buffer, _opts) {
|
|
12435
|
+
if (buffer.length < 32) return { ok: false, code: "tn.buffer_too_small", consumed: 32 };
|
|
12436
|
+
return { ok: true, consumed: 32 };
|
|
12437
|
+
}
|
|
12438
|
+
static from_array(buffer) {
|
|
12439
|
+
if (!buffer || buffer.length === void 0) {
|
|
12440
|
+
return null;
|
|
12441
|
+
}
|
|
12442
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12443
|
+
const validation = this.validate(buffer);
|
|
12444
|
+
if (!validation.ok) {
|
|
12445
|
+
return null;
|
|
12446
|
+
}
|
|
12447
|
+
return new _Pubkey(buffer);
|
|
12448
|
+
}
|
|
12449
|
+
};
|
|
12450
|
+
var PubkeyBuilder2 = class {
|
|
12451
|
+
constructor() {
|
|
12452
|
+
this.buffer = new Uint8Array(32);
|
|
12453
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
12454
|
+
}
|
|
12455
|
+
set_bytes(values) {
|
|
12456
|
+
if (values.length !== 32) throw new Error("bytes expects 32 elements");
|
|
12457
|
+
for (let i = 0; i < values.length; i++) {
|
|
12458
|
+
const byteOffset = 0 + i * 1;
|
|
12459
|
+
this.view.setUint8(byteOffset, values[i]);
|
|
12460
|
+
}
|
|
12461
|
+
return this;
|
|
12462
|
+
}
|
|
12463
|
+
build() {
|
|
12464
|
+
return this.buffer.slice();
|
|
12465
|
+
}
|
|
12466
|
+
buildInto(target, offset = 0) {
|
|
12467
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
12468
|
+
target.set(this.buffer, offset);
|
|
12469
|
+
return target;
|
|
12470
|
+
}
|
|
12471
|
+
finish() {
|
|
12472
|
+
const view = Pubkey2.from_array(this.buffer.slice());
|
|
12473
|
+
if (!view) throw new Error("failed to build Pubkey");
|
|
12474
|
+
return view;
|
|
12475
|
+
}
|
|
12476
|
+
};
|
|
12477
|
+
__tnRegisterFootprint3("Pubkey", (params) => Pubkey2.__tnInvokeFootprint(params));
|
|
12478
|
+
__tnRegisterValidate3("Pubkey", (buffer, params) => Pubkey2.__tnInvokeValidate(buffer, params));
|
|
12479
|
+
__tnRegisterDynamicValidate3("Pubkey", (buffer) => {
|
|
12480
|
+
const result = Pubkey2.validate(buffer);
|
|
12481
|
+
const params = result.params;
|
|
12482
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
12483
|
+
});
|
|
12484
|
+
var __tn_ir_Signature2 = {
|
|
12485
|
+
typeName: "Signature",
|
|
12486
|
+
root: { op: "const", value: 64n }
|
|
12487
|
+
};
|
|
12488
|
+
var Signature2 = class _Signature {
|
|
12489
|
+
constructor(buffer) {
|
|
12490
|
+
this.buffer = buffer;
|
|
12491
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12492
|
+
}
|
|
12493
|
+
static __tnCreateView(buffer, opts) {
|
|
12494
|
+
if (!buffer || buffer.length === void 0) throw new Error("Signature.__tnCreateView requires a Uint8Array");
|
|
12495
|
+
return new _Signature(new Uint8Array(buffer));
|
|
12496
|
+
}
|
|
12497
|
+
static builder() {
|
|
12498
|
+
return new SignatureBuilder2();
|
|
12499
|
+
}
|
|
12500
|
+
static fromBuilder(builder) {
|
|
12501
|
+
const buffer = builder.build();
|
|
12502
|
+
return _Signature.from_array(buffer);
|
|
12503
|
+
}
|
|
12504
|
+
get_bytes() {
|
|
12505
|
+
const offset = 0;
|
|
12506
|
+
const result = [];
|
|
12507
|
+
for (let i = 0; i < 64; i++) {
|
|
12508
|
+
result.push(this.view.getUint8(offset + i * 1));
|
|
12509
|
+
}
|
|
12510
|
+
return result;
|
|
12511
|
+
}
|
|
12512
|
+
set_bytes(value) {
|
|
12513
|
+
const offset = 0;
|
|
12514
|
+
if (value.length !== 64) {
|
|
12515
|
+
throw new Error("Array length must be 64");
|
|
12516
|
+
}
|
|
12517
|
+
for (let i = 0; i < 64; i++) {
|
|
12518
|
+
this.view.setUint8(offset + i * 1, value[i]);
|
|
12519
|
+
}
|
|
12520
|
+
}
|
|
12521
|
+
get bytes() {
|
|
12522
|
+
return this.get_bytes();
|
|
12523
|
+
}
|
|
12524
|
+
set bytes(value) {
|
|
12525
|
+
this.set_bytes(value);
|
|
12526
|
+
}
|
|
12527
|
+
static __tnFootprintInternal(__tnParams) {
|
|
12528
|
+
return __tnEvalFootprint3(__tn_ir_Signature2.root, { params: __tnParams });
|
|
12529
|
+
}
|
|
12530
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
12531
|
+
return __tnValidateIrTree3(__tn_ir_Signature2, buffer, __tnParams);
|
|
12532
|
+
}
|
|
12533
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
12534
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
12535
|
+
}
|
|
12536
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
12537
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
12538
|
+
}
|
|
12539
|
+
static footprintIr() {
|
|
12540
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
12541
|
+
}
|
|
12542
|
+
static footprint() {
|
|
12543
|
+
const irResult = this.footprintIr();
|
|
12544
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
12545
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
12546
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Signature");
|
|
12547
|
+
}
|
|
12548
|
+
return __tnBigIntToNumber3(irResult, "Signature::footprint");
|
|
12549
|
+
}
|
|
12550
|
+
static validate(buffer, _opts) {
|
|
12551
|
+
if (buffer.length < 64) return { ok: false, code: "tn.buffer_too_small", consumed: 64 };
|
|
12552
|
+
return { ok: true, consumed: 64 };
|
|
12553
|
+
}
|
|
12554
|
+
static from_array(buffer) {
|
|
12555
|
+
if (!buffer || buffer.length === void 0) {
|
|
12556
|
+
return null;
|
|
12557
|
+
}
|
|
12558
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12559
|
+
const validation = this.validate(buffer);
|
|
12560
|
+
if (!validation.ok) {
|
|
12561
|
+
return null;
|
|
12562
|
+
}
|
|
12563
|
+
return new _Signature(buffer);
|
|
12564
|
+
}
|
|
12565
|
+
};
|
|
12566
|
+
var SignatureBuilder2 = class {
|
|
12567
|
+
constructor() {
|
|
12568
|
+
this.buffer = new Uint8Array(64);
|
|
12569
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
12570
|
+
}
|
|
12571
|
+
set_bytes(values) {
|
|
12572
|
+
if (values.length !== 64) throw new Error("bytes expects 64 elements");
|
|
12573
|
+
for (let i = 0; i < values.length; i++) {
|
|
12574
|
+
const byteOffset = 0 + i * 1;
|
|
12575
|
+
this.view.setUint8(byteOffset, values[i]);
|
|
12576
|
+
}
|
|
12577
|
+
return this;
|
|
12578
|
+
}
|
|
12579
|
+
build() {
|
|
12580
|
+
return this.buffer.slice();
|
|
12581
|
+
}
|
|
12582
|
+
buildInto(target, offset = 0) {
|
|
12583
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
12584
|
+
target.set(this.buffer, offset);
|
|
12585
|
+
return target;
|
|
12586
|
+
}
|
|
12587
|
+
finish() {
|
|
12588
|
+
const view = Signature2.from_array(this.buffer.slice());
|
|
12589
|
+
if (!view) throw new Error("failed to build Signature");
|
|
12590
|
+
return view;
|
|
12591
|
+
}
|
|
12592
|
+
};
|
|
12593
|
+
__tnRegisterFootprint3("Signature", (params) => Signature2.__tnInvokeFootprint(params));
|
|
12594
|
+
__tnRegisterValidate3("Signature", (buffer, params) => Signature2.__tnInvokeValidate(buffer, params));
|
|
12595
|
+
__tnRegisterDynamicValidate3("Signature", (buffer) => {
|
|
12596
|
+
const result = Signature2.validate(buffer);
|
|
12597
|
+
const params = result.params;
|
|
12598
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
12599
|
+
});
|
|
12600
|
+
var __tn_ir_Timestamp2 = {
|
|
12601
|
+
typeName: "Timestamp",
|
|
12602
|
+
root: { op: "const", value: 8n }
|
|
12603
|
+
};
|
|
12604
|
+
var Timestamp2 = class _Timestamp {
|
|
12605
|
+
constructor(buffer) {
|
|
12606
|
+
this.buffer = buffer;
|
|
12607
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12608
|
+
}
|
|
12609
|
+
static __tnCreateView(buffer, opts) {
|
|
12610
|
+
if (!buffer || buffer.length === void 0) throw new Error("Timestamp.__tnCreateView requires a Uint8Array");
|
|
12611
|
+
return new _Timestamp(new Uint8Array(buffer));
|
|
12612
|
+
}
|
|
12613
|
+
static builder() {
|
|
12614
|
+
return new TimestampBuilder2();
|
|
12615
|
+
}
|
|
12616
|
+
static fromBuilder(builder) {
|
|
12617
|
+
const buffer = builder.build();
|
|
12618
|
+
return _Timestamp.from_array(buffer);
|
|
12619
|
+
}
|
|
12620
|
+
get_seconds() {
|
|
12621
|
+
const offset = 0;
|
|
12622
|
+
return this.view.getBigInt64(offset, true);
|
|
12623
|
+
}
|
|
12624
|
+
set_seconds(value) {
|
|
12625
|
+
const offset = 0;
|
|
12626
|
+
this.view.setBigInt64(offset, value, true);
|
|
12627
|
+
}
|
|
12628
|
+
get seconds() {
|
|
12629
|
+
return this.get_seconds();
|
|
12630
|
+
}
|
|
12631
|
+
set seconds(value) {
|
|
12632
|
+
this.set_seconds(value);
|
|
12633
|
+
}
|
|
12634
|
+
static __tnFootprintInternal(__tnParams) {
|
|
12635
|
+
return __tnEvalFootprint3(__tn_ir_Timestamp2.root, { params: __tnParams });
|
|
12636
|
+
}
|
|
12637
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
12638
|
+
return __tnValidateIrTree3(__tn_ir_Timestamp2, buffer, __tnParams);
|
|
12639
|
+
}
|
|
12640
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
12641
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
12642
|
+
}
|
|
12643
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
12644
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
12645
|
+
}
|
|
12646
|
+
static footprintIr() {
|
|
12647
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
12648
|
+
}
|
|
12649
|
+
static footprint() {
|
|
12650
|
+
const irResult = this.footprintIr();
|
|
12651
|
+
const maxSafe = __tnToBigInt3(Number.MAX_SAFE_INTEGER);
|
|
12652
|
+
if (__tnBigIntGreaterThan3(irResult, maxSafe)) {
|
|
12653
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for Timestamp");
|
|
12654
|
+
}
|
|
12655
|
+
return __tnBigIntToNumber3(irResult, "Timestamp::footprint");
|
|
12656
|
+
}
|
|
12657
|
+
static validate(buffer, _opts) {
|
|
12658
|
+
if (buffer.length < 8) return { ok: false, code: "tn.buffer_too_small", consumed: 8 };
|
|
12659
|
+
return { ok: true, consumed: 8 };
|
|
12660
|
+
}
|
|
12661
|
+
static new(seconds) {
|
|
12662
|
+
const buffer = new Uint8Array(8);
|
|
12663
|
+
const view = new DataView(buffer.buffer);
|
|
12664
|
+
view.setBigInt64(0, seconds, true);
|
|
12665
|
+
return new _Timestamp(buffer);
|
|
12666
|
+
}
|
|
12667
|
+
static from_array(buffer) {
|
|
12668
|
+
if (!buffer || buffer.length === void 0) {
|
|
12669
|
+
return null;
|
|
12670
|
+
}
|
|
12671
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
12672
|
+
const validation = this.validate(buffer);
|
|
12673
|
+
if (!validation.ok) {
|
|
12674
|
+
return null;
|
|
12675
|
+
}
|
|
12676
|
+
return new _Timestamp(buffer);
|
|
12677
|
+
}
|
|
12678
|
+
};
|
|
12679
|
+
var TimestampBuilder2 = class {
|
|
12680
|
+
constructor() {
|
|
12681
|
+
this.buffer = new Uint8Array(8);
|
|
12682
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
12683
|
+
}
|
|
12684
|
+
set_seconds(value) {
|
|
12685
|
+
const cast = __tnToBigInt3(value);
|
|
12686
|
+
this.view.setBigInt64(0, cast, true);
|
|
12687
|
+
return this;
|
|
12688
|
+
}
|
|
12689
|
+
build() {
|
|
12690
|
+
return this.buffer.slice();
|
|
12691
|
+
}
|
|
12692
|
+
buildInto(target, offset = 0) {
|
|
12693
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
12694
|
+
target.set(this.buffer, offset);
|
|
12695
|
+
return target;
|
|
12696
|
+
}
|
|
12697
|
+
finish() {
|
|
12698
|
+
const view = Timestamp2.from_array(this.buffer.slice());
|
|
12699
|
+
if (!view) throw new Error("failed to build Timestamp");
|
|
12700
|
+
return view;
|
|
12701
|
+
}
|
|
12702
|
+
};
|
|
12703
|
+
__tnRegisterFootprint3("Timestamp", (params) => Timestamp2.__tnInvokeFootprint(params));
|
|
12704
|
+
__tnRegisterValidate3("Timestamp", (buffer, params) => Timestamp2.__tnInvokeValidate(buffer, params));
|
|
12705
|
+
__tnRegisterDynamicValidate3("Timestamp", (buffer) => {
|
|
12706
|
+
const result = Timestamp2.validate(buffer);
|
|
12707
|
+
const params = result.params;
|
|
12708
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt3(result.consumed), params };
|
|
12709
|
+
});
|
|
12710
|
+
|
|
12711
|
+
// src/multicall/abi/thru/program/multicall/types.ts
|
|
12712
|
+
var __tnWarnings4 = /* @__PURE__ */ new Set();
|
|
12713
|
+
var __tnHasNativeBigInt4 = typeof BigInt === "function";
|
|
12714
|
+
var __tnHasBigIntDataView4 = typeof DataView !== "undefined" && typeof DataView.prototype.getBigInt64 === "function" && typeof DataView.prototype.getBigUint64 === "function" && typeof DataView.prototype.setBigInt64 === "function" && typeof DataView.prototype.setBigUint64 === "function";
|
|
12715
|
+
var __tnConsole4 = typeof globalThis !== "undefined" ? globalThis.console : void 0;
|
|
12716
|
+
function __tnLogWarn4(message) {
|
|
12717
|
+
if (__tnConsole4 && typeof __tnConsole4.warn === "function") {
|
|
12718
|
+
__tnConsole4.warn(message);
|
|
12719
|
+
}
|
|
12720
|
+
}
|
|
12721
|
+
function __tnWarnOnce4(message) {
|
|
12722
|
+
if (!__tnWarnings4.has(message)) {
|
|
12723
|
+
__tnWarnings4.add(message);
|
|
12724
|
+
__tnLogWarn4(message);
|
|
12725
|
+
}
|
|
12726
|
+
}
|
|
12727
|
+
function __tnResolveBuilderInput4(input, context) {
|
|
12728
|
+
if (input instanceof Uint8Array) {
|
|
12729
|
+
return new Uint8Array(input);
|
|
12730
|
+
}
|
|
12731
|
+
if (input && typeof input.build === "function") {
|
|
12732
|
+
const built = input.build();
|
|
12733
|
+
if (!(built instanceof Uint8Array)) {
|
|
12734
|
+
throw new Error(`${context}: builder did not return Uint8Array`);
|
|
12735
|
+
}
|
|
12736
|
+
return new Uint8Array(built);
|
|
12737
|
+
}
|
|
12738
|
+
throw new Error(`${context}: expected Uint8Array or builder`);
|
|
12739
|
+
}
|
|
12740
|
+
function __tnResolveStructFieldInput(input, context) {
|
|
12741
|
+
if (input instanceof Uint8Array || input && typeof input.build === "function") {
|
|
12742
|
+
return __tnResolveBuilderInput4(input, context);
|
|
12743
|
+
}
|
|
12744
|
+
if (input && typeof input.asUint8Array === "function") {
|
|
12745
|
+
const bytes = input.asUint8Array();
|
|
12746
|
+
return new Uint8Array(bytes);
|
|
12747
|
+
}
|
|
12748
|
+
if (input && typeof input.bytes === "function") {
|
|
12749
|
+
const bytes = input.bytes();
|
|
12750
|
+
return new Uint8Array(bytes);
|
|
12751
|
+
}
|
|
12752
|
+
if (input && input.buffer instanceof Uint8Array) {
|
|
12753
|
+
return new Uint8Array(input.buffer);
|
|
12754
|
+
}
|
|
12755
|
+
throw new Error(`${context}: expected Uint8Array, builder, or view-like value`);
|
|
12756
|
+
}
|
|
12757
|
+
var __tnMask324 = __tnHasNativeBigInt4 ? (BigInt(1) << BigInt(32)) - BigInt(1) : 4294967295;
|
|
12758
|
+
var __tnSignBit324 = __tnHasNativeBigInt4 ? BigInt(1) << BigInt(31) : 2147483648;
|
|
12759
|
+
function __tnToBigInt4(value) {
|
|
12760
|
+
if (__tnHasNativeBigInt4) {
|
|
12761
|
+
return typeof value === "bigint" ? value : BigInt(value);
|
|
12762
|
+
}
|
|
12763
|
+
if (typeof value === "bigint") return value;
|
|
12764
|
+
if (!Number.isFinite(value)) {
|
|
12765
|
+
throw new Error("IR runtime received non-finite numeric input");
|
|
12766
|
+
}
|
|
12767
|
+
if (!Number.isSafeInteger(value)) {
|
|
12768
|
+
__tnWarnOnce4(
|
|
12769
|
+
`[thru-net] Precision loss while polyfilling BigInt (value=${value})`
|
|
12770
|
+
);
|
|
12771
|
+
}
|
|
12772
|
+
return value;
|
|
12773
|
+
}
|
|
12774
|
+
function __tnBigIntToNumber4(value, context) {
|
|
12775
|
+
if (__tnHasNativeBigInt4) {
|
|
12776
|
+
const converted = Number(value);
|
|
12777
|
+
if (!Number.isFinite(converted)) {
|
|
12778
|
+
throw new Error(`${context} overflowed Number range`);
|
|
12779
|
+
}
|
|
12780
|
+
return converted;
|
|
12781
|
+
}
|
|
12782
|
+
return value;
|
|
12783
|
+
}
|
|
12784
|
+
function __tnBigIntEquals4(lhs, rhs) {
|
|
12785
|
+
if (__tnHasNativeBigInt4) return lhs === rhs;
|
|
12786
|
+
return lhs === rhs;
|
|
12787
|
+
}
|
|
12788
|
+
function __tnBigIntGreaterThan4(lhs, rhs) {
|
|
12789
|
+
if (__tnHasNativeBigInt4) return lhs > rhs;
|
|
12790
|
+
return lhs > rhs;
|
|
12791
|
+
}
|
|
12792
|
+
function __tnRaiseIrError4(code, message) {
|
|
12793
|
+
const err = new Error(message);
|
|
12794
|
+
err.code = code;
|
|
12795
|
+
throw err;
|
|
12796
|
+
}
|
|
12797
|
+
function __tnCheckedAdd4(lhs, rhs) {
|
|
12798
|
+
if (__tnHasNativeBigInt4) {
|
|
12799
|
+
const result = lhs + rhs;
|
|
12800
|
+
if (result < BigInt(0)) {
|
|
12801
|
+
__tnRaiseIrError4(
|
|
12802
|
+
"tn.ir.overflow",
|
|
12803
|
+
"IR runtime detected negative size via addition"
|
|
12804
|
+
);
|
|
12805
|
+
}
|
|
12806
|
+
return result;
|
|
12807
|
+
}
|
|
12808
|
+
const left = lhs;
|
|
12809
|
+
const right = rhs;
|
|
12810
|
+
const sum = left + right;
|
|
12811
|
+
if (sum < 0 || !Number.isFinite(sum)) {
|
|
12812
|
+
__tnRaiseIrError4(
|
|
12813
|
+
"tn.ir.overflow",
|
|
12814
|
+
"IR runtime detected invalid addition result"
|
|
12815
|
+
);
|
|
12816
|
+
}
|
|
12817
|
+
if (!Number.isSafeInteger(sum)) {
|
|
12818
|
+
__tnWarnOnce4("[thru-net] Precision loss while polyfilling BigInt addition");
|
|
12819
|
+
}
|
|
12820
|
+
return sum;
|
|
12821
|
+
}
|
|
12822
|
+
function __tnCheckedMul4(lhs, rhs) {
|
|
12823
|
+
if (__tnHasNativeBigInt4) {
|
|
12824
|
+
const result = lhs * rhs;
|
|
12825
|
+
if (result < BigInt(0)) {
|
|
12826
|
+
__tnRaiseIrError4(
|
|
12827
|
+
"tn.ir.overflow",
|
|
12828
|
+
"IR runtime detected negative size via multiplication"
|
|
12829
|
+
);
|
|
12830
|
+
}
|
|
12831
|
+
return result;
|
|
12832
|
+
}
|
|
12833
|
+
const left = lhs;
|
|
12834
|
+
const right = rhs;
|
|
12835
|
+
const product = left * right;
|
|
12836
|
+
if (product < 0 || !Number.isFinite(product)) {
|
|
12837
|
+
__tnRaiseIrError4(
|
|
12838
|
+
"tn.ir.overflow",
|
|
12839
|
+
"IR runtime detected invalid multiplication result"
|
|
12840
|
+
);
|
|
12841
|
+
}
|
|
12842
|
+
if (!Number.isSafeInteger(product)) {
|
|
12843
|
+
__tnWarnOnce4(
|
|
12844
|
+
"[thru-net] Precision loss while polyfilling BigInt multiplication"
|
|
12845
|
+
);
|
|
12846
|
+
}
|
|
12847
|
+
return product;
|
|
12848
|
+
}
|
|
12849
|
+
function __tnAlign4(value, alignment) {
|
|
12850
|
+
if (alignment <= 1) return value;
|
|
12851
|
+
const alignBig = __tnToBigInt4(alignment);
|
|
12852
|
+
if (__tnHasNativeBigInt4) {
|
|
12853
|
+
const remainder2 = value % alignBig;
|
|
12854
|
+
if (__tnBigIntEquals4(remainder2, __tnToBigInt4(0))) {
|
|
12855
|
+
return value;
|
|
12856
|
+
}
|
|
12857
|
+
const delta = alignBig - remainder2;
|
|
12858
|
+
return __tnCheckedAdd4(value, delta);
|
|
12859
|
+
}
|
|
12860
|
+
const current = __tnBigIntToNumber4(value, "IR align");
|
|
12861
|
+
const alignNum = alignment >>> 0;
|
|
12862
|
+
const remainder = current % alignNum;
|
|
12863
|
+
const next = remainder === 0 ? current : current + (alignNum - remainder);
|
|
12864
|
+
return __tnToBigInt4(next);
|
|
12865
|
+
}
|
|
12866
|
+
function __tnSplitUint644(value) {
|
|
12867
|
+
if (__tnHasNativeBigInt4) {
|
|
12868
|
+
const low2 = Number(value & __tnMask324);
|
|
12869
|
+
const high2 = Number(value >> BigInt(32) & __tnMask324);
|
|
12870
|
+
return { high: high2, low: low2 };
|
|
12871
|
+
}
|
|
12872
|
+
const num = __tnBigIntToNumber4(value, "DataView.setBigUint64");
|
|
12873
|
+
const low = num >>> 0;
|
|
12874
|
+
const high = Math.floor(num / 4294967296) >>> 0;
|
|
12875
|
+
return { high, low };
|
|
12876
|
+
}
|
|
12877
|
+
function __tnSplitInt644(value) {
|
|
12878
|
+
if (__tnHasNativeBigInt4) {
|
|
12879
|
+
const low2 = Number(value & __tnMask324);
|
|
12880
|
+
let high2 = Number(value >> BigInt(32) & __tnMask324);
|
|
12881
|
+
if ((BigInt(high2) & __tnSignBit324) !== BigInt(0)) {
|
|
12882
|
+
high2 -= 4294967296;
|
|
12883
|
+
}
|
|
12884
|
+
return { high: high2, low: low2 };
|
|
12885
|
+
}
|
|
12886
|
+
const num = __tnBigIntToNumber4(value, "DataView.setBigInt64");
|
|
12887
|
+
const low = num >>> 0;
|
|
12888
|
+
const high = Math.floor(num / 4294967296);
|
|
12889
|
+
return { high, low };
|
|
12890
|
+
}
|
|
12891
|
+
function __tnPolyfillReadUint644(view, offset, littleEndian) {
|
|
12892
|
+
const low = littleEndian ? view.getUint32(offset, true) : view.getUint32(offset + 4, false);
|
|
12893
|
+
const high = littleEndian ? view.getUint32(offset + 4, true) : view.getUint32(offset, false);
|
|
12894
|
+
if (__tnHasNativeBigInt4) {
|
|
12895
|
+
return BigInt(high) << BigInt(32) | BigInt(low);
|
|
12896
|
+
}
|
|
12897
|
+
const value = high * 4294967296 + low;
|
|
12898
|
+
if (!Number.isSafeInteger(value)) {
|
|
12899
|
+
__tnWarnOnce4(
|
|
12900
|
+
"[thru-net] Precision loss while polyfilling DataView.getBigUint64"
|
|
12901
|
+
);
|
|
12902
|
+
}
|
|
12903
|
+
return value;
|
|
12904
|
+
}
|
|
12905
|
+
function __tnPolyfillReadInt644(view, offset, littleEndian) {
|
|
12906
|
+
const low = littleEndian ? view.getUint32(offset, true) : view.getUint32(offset + 4, false);
|
|
12907
|
+
const high = littleEndian ? view.getInt32(offset + 4, true) : view.getInt32(offset, false);
|
|
12908
|
+
if (__tnHasNativeBigInt4) {
|
|
12909
|
+
return BigInt(high) << BigInt(32) | BigInt(low);
|
|
12910
|
+
}
|
|
12911
|
+
const value = high * 4294967296 + low;
|
|
12912
|
+
if (!Number.isSafeInteger(value)) {
|
|
12913
|
+
__tnWarnOnce4(
|
|
12914
|
+
"[thru-net] Precision loss while polyfilling DataView.getBigInt64"
|
|
12915
|
+
);
|
|
12916
|
+
}
|
|
12917
|
+
return value;
|
|
12918
|
+
}
|
|
12919
|
+
function __tnPolyfillWriteUint644(view, offset, value, littleEndian) {
|
|
12920
|
+
const parts = __tnSplitUint644(value);
|
|
12921
|
+
if (littleEndian) {
|
|
12922
|
+
view.setUint32(offset, parts.low, true);
|
|
12923
|
+
view.setUint32(offset + 4, parts.high, true);
|
|
12924
|
+
} else {
|
|
12925
|
+
view.setUint32(offset, parts.high, false);
|
|
12926
|
+
view.setUint32(offset + 4, parts.low, false);
|
|
12927
|
+
}
|
|
12928
|
+
}
|
|
12929
|
+
function __tnPolyfillWriteInt644(view, offset, value, littleEndian) {
|
|
12930
|
+
const parts = __tnSplitInt644(value);
|
|
12931
|
+
if (littleEndian) {
|
|
12932
|
+
view.setUint32(offset, parts.low >>> 0, true);
|
|
12933
|
+
view.setInt32(offset + 4, parts.high | 0, true);
|
|
12934
|
+
} else {
|
|
12935
|
+
view.setInt32(offset, parts.high | 0, false);
|
|
12936
|
+
view.setUint32(offset + 4, parts.low >>> 0, false);
|
|
12937
|
+
}
|
|
12938
|
+
}
|
|
12939
|
+
if (typeof DataView !== "undefined" && !__tnHasBigIntDataView4) {
|
|
12940
|
+
const proto = DataView.prototype;
|
|
12941
|
+
if (typeof proto.getBigUint64 !== "function") {
|
|
12942
|
+
proto.getBigUint64 = function(offset, littleEndian) {
|
|
12943
|
+
__tnWarnOnce4(
|
|
12944
|
+
"[thru-net] Polyfilling DataView.getBigUint64; precision may be lost"
|
|
12945
|
+
);
|
|
12946
|
+
return __tnPolyfillReadUint644(this, offset, !!littleEndian);
|
|
12947
|
+
};
|
|
12948
|
+
}
|
|
12949
|
+
if (typeof proto.getBigInt64 !== "function") {
|
|
12950
|
+
proto.getBigInt64 = function(offset, littleEndian) {
|
|
12951
|
+
__tnWarnOnce4(
|
|
12952
|
+
"[thru-net] Polyfilling DataView.getBigInt64; precision may be lost"
|
|
12953
|
+
);
|
|
12954
|
+
return __tnPolyfillReadInt644(this, offset, !!littleEndian);
|
|
12955
|
+
};
|
|
12956
|
+
}
|
|
12957
|
+
if (typeof proto.setBigUint64 !== "function") {
|
|
12958
|
+
proto.setBigUint64 = function(offset, value, littleEndian) {
|
|
12959
|
+
__tnWarnOnce4(
|
|
12960
|
+
"[thru-net] Polyfilling DataView.setBigUint64; precision may be lost"
|
|
12961
|
+
);
|
|
12962
|
+
__tnPolyfillWriteUint644(this, offset, value, !!littleEndian);
|
|
12963
|
+
};
|
|
12964
|
+
}
|
|
12965
|
+
if (typeof proto.setBigInt64 !== "function") {
|
|
12966
|
+
proto.setBigInt64 = function(offset, value, littleEndian) {
|
|
12967
|
+
__tnWarnOnce4(
|
|
12968
|
+
"[thru-net] Polyfilling DataView.setBigInt64; precision may be lost"
|
|
12969
|
+
);
|
|
12970
|
+
__tnPolyfillWriteInt644(this, offset, value, !!littleEndian);
|
|
12971
|
+
};
|
|
12972
|
+
}
|
|
12973
|
+
if (!__tnHasNativeBigInt4) {
|
|
12974
|
+
__tnWarnOnce4(
|
|
12975
|
+
"[thru-net] BigInt is unavailable; falling back to lossy 64-bit polyfill"
|
|
12976
|
+
);
|
|
12977
|
+
}
|
|
12978
|
+
}
|
|
12979
|
+
var __tnFootprintRegistry4 = {};
|
|
12980
|
+
var __tnValidateRegistry4 = {};
|
|
12981
|
+
var __tnDynamicValidateRegistry4 = {};
|
|
12982
|
+
function __tnRegisterFootprint4(typeName, fn) {
|
|
12983
|
+
__tnFootprintRegistry4[typeName] = fn;
|
|
12984
|
+
}
|
|
12985
|
+
function __tnRegisterValidate4(typeName, fn) {
|
|
12986
|
+
__tnValidateRegistry4[typeName] = fn;
|
|
12987
|
+
}
|
|
12988
|
+
function __tnRegisterDynamicValidate4(typeName, fn) {
|
|
12989
|
+
__tnDynamicValidateRegistry4[typeName] = fn;
|
|
12990
|
+
}
|
|
12991
|
+
function __tnInvokeFootprint4(typeName, params) {
|
|
12992
|
+
const fn = __tnFootprintRegistry4[typeName];
|
|
12993
|
+
if (!fn) throw new Error(`IR runtime missing footprint for ${typeName}`);
|
|
12994
|
+
return fn(params);
|
|
12995
|
+
}
|
|
12996
|
+
function __tnInvokeValidate4(typeName, buffer, params) {
|
|
12997
|
+
const fn = __tnValidateRegistry4[typeName];
|
|
12998
|
+
if (!fn) throw new Error(`IR runtime missing validate helper for ${typeName}`);
|
|
12999
|
+
return fn(buffer, params);
|
|
13000
|
+
}
|
|
13001
|
+
function __tnInvokeDynamicValidate4(typeName, buffer) {
|
|
13002
|
+
const fn = __tnDynamicValidateRegistry4[typeName];
|
|
13003
|
+
if (!fn) throw new Error(`IR runtime missing dynamic validate helper for ${typeName}`);
|
|
13004
|
+
return fn(buffer);
|
|
13005
|
+
}
|
|
13006
|
+
function __tnEvalFootprint4(node, ctx) {
|
|
13007
|
+
return __tnEvalIrNode4(node, ctx, __tnToBigInt4(0));
|
|
13008
|
+
}
|
|
13009
|
+
function __tnTryEvalIr4(node, ctx) {
|
|
13010
|
+
try {
|
|
13011
|
+
return { ok: true, value: __tnEvalIrNode4(node, ctx, __tnToBigInt4(0)) };
|
|
13012
|
+
} catch (err) {
|
|
13013
|
+
return { ok: false, code: __tnNormalizeIrError4(err) };
|
|
13014
|
+
}
|
|
13015
|
+
}
|
|
13016
|
+
function __tnIsEvalError4(result) {
|
|
13017
|
+
return result.ok === false;
|
|
13018
|
+
}
|
|
13019
|
+
function __tnValidateIrTree4(ir, buffer, params) {
|
|
13020
|
+
const evalResult = __tnTryEvalIr4(ir.root, {
|
|
13021
|
+
params,
|
|
13022
|
+
buffer,
|
|
13023
|
+
typeName: ir.typeName
|
|
13024
|
+
});
|
|
13025
|
+
if (__tnIsEvalError4(evalResult)) {
|
|
13026
|
+
return { ok: false, code: evalResult.code };
|
|
13027
|
+
}
|
|
13028
|
+
const required = evalResult.value;
|
|
13029
|
+
const available = __tnToBigInt4(buffer.length);
|
|
13030
|
+
if (__tnBigIntGreaterThan4(required, available)) {
|
|
13031
|
+
return { ok: false, code: "tn.buffer_too_small", consumed: required };
|
|
13032
|
+
}
|
|
13033
|
+
return { ok: true, consumed: required };
|
|
13034
|
+
}
|
|
13035
|
+
function __tnEvalIrNode4(node, ctx, baseOffset) {
|
|
13036
|
+
switch (node.op) {
|
|
13037
|
+
case "zero":
|
|
13038
|
+
return __tnToBigInt4(0);
|
|
13039
|
+
case "const":
|
|
13040
|
+
return node.value;
|
|
13041
|
+
case "field": {
|
|
13042
|
+
const val = ctx.params[node.param];
|
|
13043
|
+
if (val === void 0) {
|
|
13044
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
13045
|
+
__tnRaiseIrError4(
|
|
13046
|
+
"tn.ir.missing_param",
|
|
13047
|
+
`${prefix}Missing IR parameter '${node.param}'`
|
|
13048
|
+
);
|
|
13049
|
+
}
|
|
13050
|
+
return val;
|
|
13051
|
+
}
|
|
13052
|
+
case "add": {
|
|
13053
|
+
const left = __tnEvalIrNode4(node.left, ctx, baseOffset);
|
|
13054
|
+
const right = __tnEvalIrNode4(
|
|
13055
|
+
node.right,
|
|
13056
|
+
ctx,
|
|
13057
|
+
__tnCheckedAdd4(baseOffset, left)
|
|
13058
|
+
);
|
|
13059
|
+
return __tnCheckedAdd4(left, right);
|
|
13060
|
+
}
|
|
13061
|
+
case "mul":
|
|
13062
|
+
return __tnCheckedMul4(
|
|
13063
|
+
__tnEvalIrNode4(node.left, ctx, baseOffset),
|
|
13064
|
+
__tnEvalIrNode4(node.right, ctx, baseOffset)
|
|
13065
|
+
);
|
|
13066
|
+
case "align":
|
|
13067
|
+
return __tnAlign4(__tnEvalIrNode4(node.node, ctx, baseOffset), node.alignment);
|
|
13068
|
+
case "switch": {
|
|
13069
|
+
const tagVal = ctx.params[node.tag];
|
|
13070
|
+
if (tagVal === void 0) {
|
|
13071
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
13072
|
+
__tnRaiseIrError4(
|
|
13073
|
+
"tn.ir.missing_param",
|
|
13074
|
+
`${prefix}Missing IR switch tag '${node.tag}'`
|
|
13075
|
+
);
|
|
13076
|
+
}
|
|
13077
|
+
const tagNumber = Number(tagVal);
|
|
13078
|
+
for (const caseNode of node.cases) {
|
|
13079
|
+
if (caseNode.value === tagNumber) {
|
|
13080
|
+
return __tnEvalIrNode4(caseNode.node, ctx, baseOffset);
|
|
13081
|
+
}
|
|
13082
|
+
}
|
|
13083
|
+
if (node.default) return __tnEvalIrNode4(node.default, ctx, baseOffset);
|
|
13084
|
+
__tnRaiseIrError4(
|
|
13085
|
+
"tn.ir.invalid_tag",
|
|
13086
|
+
`Unhandled IR switch value ${tagNumber} for '${node.tag}'`
|
|
13087
|
+
);
|
|
13088
|
+
}
|
|
13089
|
+
case "call": {
|
|
13090
|
+
const nestedParams = /* @__PURE__ */ Object.create(null);
|
|
13091
|
+
for (const arg of node.args) {
|
|
13092
|
+
const val = ctx.params[arg.source];
|
|
13093
|
+
if (val === void 0) {
|
|
13094
|
+
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
13095
|
+
__tnRaiseIrError4(
|
|
13096
|
+
"tn.ir.missing_param",
|
|
13097
|
+
`${prefix}Missing IR parameter '${arg.source}' for nested call`
|
|
13098
|
+
);
|
|
13099
|
+
}
|
|
13100
|
+
nestedParams[arg.name] = val;
|
|
13101
|
+
}
|
|
13102
|
+
if (ctx.buffer) {
|
|
13103
|
+
const nestedOffset = __tnBigIntToNumber4(baseOffset, "IR nested offset");
|
|
13104
|
+
const nestedResult = __tnInvokeValidate4(
|
|
13105
|
+
node.typeName,
|
|
13106
|
+
ctx.buffer.subarray(nestedOffset),
|
|
13107
|
+
nestedParams
|
|
13108
|
+
);
|
|
13109
|
+
if (!nestedResult.ok) {
|
|
13110
|
+
const nestedCode = nestedResult.code ?? `tn.ir.runtime_error: ${node.typeName}`;
|
|
13111
|
+
const prefixed = nestedCode.startsWith("tn.") ? nestedCode : `tn.ir.runtime_error: ${node.typeName} -> ${nestedCode}`;
|
|
13112
|
+
__tnRaiseIrError4(
|
|
13113
|
+
prefixed,
|
|
13114
|
+
`Nested validator ${node.typeName} failed`
|
|
13115
|
+
);
|
|
13116
|
+
}
|
|
13117
|
+
if (nestedResult.consumed !== void 0) {
|
|
13118
|
+
return nestedResult.consumed;
|
|
13119
|
+
}
|
|
13120
|
+
}
|
|
13121
|
+
return __tnInvokeFootprint4(node.typeName, nestedParams);
|
|
13122
|
+
}
|
|
13123
|
+
case "sumOverArray": {
|
|
13124
|
+
if (!ctx.buffer) {
|
|
13125
|
+
__tnRaiseIrError4(
|
|
13126
|
+
"tn.ir.missing_buffer",
|
|
13127
|
+
`Jagged array '${node.fieldName}' requires buffer-backed validation`
|
|
13128
|
+
);
|
|
13129
|
+
}
|
|
13130
|
+
const count = __tnBigIntToNumber4(
|
|
13131
|
+
__tnEvalIrNode4(node.count, ctx, baseOffset),
|
|
13132
|
+
`Jagged array '${node.fieldName}' count`
|
|
13133
|
+
);
|
|
13134
|
+
let cursor = __tnBigIntToNumber4(baseOffset, "IR jagged array offset");
|
|
13135
|
+
let total = __tnToBigInt4(0);
|
|
13136
|
+
for (let i = 0; i < count; i++) {
|
|
13137
|
+
const result = __tnInvokeDynamicValidate4(
|
|
13138
|
+
node.elementTypeName,
|
|
13139
|
+
ctx.buffer.subarray(cursor)
|
|
13140
|
+
);
|
|
13141
|
+
if (!result.ok || result.consumed === void 0) {
|
|
13142
|
+
const code = result.code ?? "tn.ir.runtime_error";
|
|
13143
|
+
__tnRaiseIrError4(
|
|
13144
|
+
code,
|
|
13145
|
+
`Jagged array '${node.fieldName}' element ${i} failed validation`
|
|
13146
|
+
);
|
|
13147
|
+
}
|
|
13148
|
+
cursor += __tnBigIntToNumber4(result.consumed, "IR jagged element size");
|
|
13149
|
+
total = __tnCheckedAdd4(total, result.consumed);
|
|
13150
|
+
}
|
|
13151
|
+
return total;
|
|
13152
|
+
}
|
|
13153
|
+
default:
|
|
13154
|
+
__tnRaiseIrError4(
|
|
13155
|
+
"tn.ir.runtime_error",
|
|
13156
|
+
`Unsupported IR node ${node.op}`
|
|
13157
|
+
);
|
|
13158
|
+
}
|
|
13159
|
+
}
|
|
13160
|
+
function __tnNormalizeIrError4(err) {
|
|
13161
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
13162
|
+
const maybeCode = err.code;
|
|
13163
|
+
if (typeof maybeCode === "string" && maybeCode.length > 0) {
|
|
13164
|
+
return maybeCode;
|
|
13165
|
+
}
|
|
13166
|
+
}
|
|
13167
|
+
const message = err && typeof err === "object" && "message" in err ? String(err.message ?? "") : typeof err === "string" ? err : "";
|
|
13168
|
+
if (message.includes("Missing IR parameter")) return "tn.ir.missing_param";
|
|
13169
|
+
if (message.includes("Unhandled IR switch value")) return "tn.ir.invalid_tag";
|
|
13170
|
+
if (message.includes("invalid") || message.includes("overflow") || message.includes("negative size")) {
|
|
13171
|
+
return "tn.ir.overflow";
|
|
13172
|
+
}
|
|
13173
|
+
if (message.length > 0) return `tn.ir.runtime_error: ${message}`;
|
|
13174
|
+
return "tn.ir.runtime_error";
|
|
13175
|
+
}
|
|
13176
|
+
__tnRegisterFootprint4("InstructionData", (params) => InstructionData2.__tnInvokeFootprint(params));
|
|
13177
|
+
__tnRegisterValidate4("InstructionData", (buffer, params) => InstructionData2.__tnInvokeValidate(buffer, params));
|
|
13178
|
+
__tnRegisterDynamicValidate4("InstructionData", (buffer) => {
|
|
13179
|
+
const result = InstructionData2.validate(buffer);
|
|
13180
|
+
const params = result.params;
|
|
13181
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt4(result.consumed), params };
|
|
13182
|
+
});
|
|
13183
|
+
var __tn_ir_MulticallArgs = {
|
|
13184
|
+
typeName: "MulticallArgs",
|
|
13185
|
+
root: { op: "align", alignment: 1, node: { op: "add", left: { op: "align", alignment: 2, node: { op: "const", value: 2n } }, right: { op: "align", alignment: 1, node: { op: "sumOverArray", count: { op: "field", param: "calls.calls_count" }, elementTypeName: "InstructionData", fieldName: "calls" } } } }
|
|
13186
|
+
};
|
|
13187
|
+
var MulticallArgs = class _MulticallArgs {
|
|
13188
|
+
constructor(buffer, params, fieldContext) {
|
|
13189
|
+
this.buffer = buffer;
|
|
13190
|
+
this.__tnFieldContext = null;
|
|
13191
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13192
|
+
this.__tnFieldContext = fieldContext ?? null;
|
|
13193
|
+
if (params) {
|
|
13194
|
+
this.__tnParams = params;
|
|
13195
|
+
} else {
|
|
13196
|
+
const derived = _MulticallArgs.__tnExtractParams(this.view, buffer);
|
|
13197
|
+
if (!derived) {
|
|
13198
|
+
throw new Error("MulticallArgs: failed to derive dynamic parameters");
|
|
13199
|
+
}
|
|
13200
|
+
this.__tnParams = derived.params;
|
|
13201
|
+
}
|
|
13202
|
+
}
|
|
13203
|
+
static __tnCreateView(buffer, opts) {
|
|
13204
|
+
if (!buffer || buffer.length === void 0) throw new Error("MulticallArgs.__tnCreateView requires a Uint8Array");
|
|
13205
|
+
let params = opts?.params ?? null;
|
|
13206
|
+
if (!params) {
|
|
13207
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13208
|
+
const derived = _MulticallArgs.__tnExtractParams(view, buffer);
|
|
13209
|
+
if (!derived) throw new Error("MulticallArgs.__tnCreateView: failed to derive params");
|
|
13210
|
+
params = derived.params;
|
|
13211
|
+
}
|
|
13212
|
+
const instance = new _MulticallArgs(new Uint8Array(buffer), params, opts?.fieldContext);
|
|
13213
|
+
return instance;
|
|
13214
|
+
}
|
|
13215
|
+
dynamicParams() {
|
|
13216
|
+
return this.__tnParams;
|
|
13217
|
+
}
|
|
13218
|
+
withFieldContext(context) {
|
|
13219
|
+
this.__tnFieldContext = context;
|
|
13220
|
+
return this;
|
|
13221
|
+
}
|
|
13222
|
+
__tnResolveFieldRef(path) {
|
|
13223
|
+
const getterName = `get_${path.replace(/[.]/g, "_")}`;
|
|
13224
|
+
const getter = this[getterName];
|
|
13225
|
+
if (typeof getter === "function") {
|
|
13226
|
+
const value = getter.call(this);
|
|
13227
|
+
return typeof value === "bigint" ? __tnBigIntToNumber4(value, "MulticallArgs::__tnResolveFieldRef") : value;
|
|
13228
|
+
}
|
|
13229
|
+
if (this.__tnFieldContext && Object.prototype.hasOwnProperty.call(this.__tnFieldContext, path)) {
|
|
13230
|
+
const contextValue = this.__tnFieldContext[path];
|
|
13231
|
+
return typeof contextValue === "bigint" ? __tnBigIntToNumber4(contextValue, "MulticallArgs::__tnResolveFieldRef") : contextValue;
|
|
13232
|
+
}
|
|
13233
|
+
throw new Error("MulticallArgs: field reference '" + path + "' is not available; provide fieldContext when creating this view");
|
|
13234
|
+
}
|
|
13235
|
+
static builder() {
|
|
13236
|
+
return new MulticallArgsBuilder();
|
|
13237
|
+
}
|
|
13238
|
+
static fromBuilder(builder) {
|
|
13239
|
+
const buffer = builder.build();
|
|
13240
|
+
const params = builder.dynamicParams();
|
|
13241
|
+
return _MulticallArgs.from_array(buffer, { params });
|
|
13242
|
+
}
|
|
13243
|
+
static __tnExtractParams(view, buffer) {
|
|
13244
|
+
if (buffer.length < 2) {
|
|
13245
|
+
return null;
|
|
13246
|
+
}
|
|
13247
|
+
const __tnParam_calls_calls_count = __tnToBigInt4(view.getUint16(0, true));
|
|
13248
|
+
const __tnExtractedParams = _MulticallArgs.Params.fromValues({
|
|
13249
|
+
calls_calls_count: __tnParam_calls_calls_count
|
|
13250
|
+
});
|
|
13251
|
+
return { params: __tnExtractedParams, derived: null };
|
|
13252
|
+
}
|
|
13253
|
+
get_calls_count() {
|
|
13254
|
+
const offset = 0;
|
|
13255
|
+
return this.view.getUint16(offset, true);
|
|
13256
|
+
}
|
|
13257
|
+
set_calls_count(value) {
|
|
13258
|
+
const offset = 0;
|
|
13259
|
+
this.view.setUint16(offset, value, true);
|
|
13260
|
+
}
|
|
13261
|
+
get calls_count() {
|
|
13262
|
+
return this.get_calls_count();
|
|
13263
|
+
}
|
|
13264
|
+
set calls_count(value) {
|
|
13265
|
+
this.set_calls_count(value);
|
|
13266
|
+
}
|
|
13267
|
+
/** Returns the number of elements in the jagged array. */
|
|
13268
|
+
get_calls_length() {
|
|
13269
|
+
return this.__tnResolveFieldRef("calls_count");
|
|
13270
|
+
}
|
|
13271
|
+
/** Returns the element at the given index, or null if out of bounds.
|
|
13272
|
+
* Note: This is O(n) as jagged arrays require sequential traversal. */
|
|
13273
|
+
get_calls_at(index) {
|
|
13274
|
+
const count = this.get_calls_length();
|
|
13275
|
+
if (index < 0 || index >= count) {
|
|
13276
|
+
return null;
|
|
13277
|
+
}
|
|
13278
|
+
const offset = 2;
|
|
13279
|
+
let cursor = offset;
|
|
13280
|
+
for (let i = 0; i < index; i++) {
|
|
13281
|
+
const elem = InstructionData2.from_array(this.buffer.subarray(cursor));
|
|
13282
|
+
if (!elem) {
|
|
13283
|
+
throw new Error("MulticallArgs: invalid element at index " + i + " in jagged array 'calls'");
|
|
13284
|
+
}
|
|
13285
|
+
const validation = InstructionData2.validate(this.buffer.subarray(cursor));
|
|
13286
|
+
if (!validation.ok || validation.consumed === void 0) {
|
|
13287
|
+
throw new Error("MulticallArgs: failed to get footprint for element at index " + i);
|
|
13288
|
+
}
|
|
13289
|
+
cursor += validation.consumed;
|
|
13290
|
+
}
|
|
13291
|
+
return InstructionData2.from_array(this.buffer.subarray(cursor));
|
|
13292
|
+
}
|
|
13293
|
+
/** Returns a generator over the jagged array elements.
|
|
13294
|
+
* This is more efficient than repeated calls to `get_calls_at()` for sequential access. */
|
|
13295
|
+
*callsIter() {
|
|
13296
|
+
const count = this.get_calls_length();
|
|
13297
|
+
const offset = 2;
|
|
13298
|
+
let cursor = offset;
|
|
13299
|
+
for (let i = 0; i < count; i++) {
|
|
13300
|
+
const elem = InstructionData2.from_array(this.buffer.subarray(cursor));
|
|
13301
|
+
if (!elem) {
|
|
13302
|
+
throw new Error("MulticallArgs: invalid element at index " + i + " in jagged array 'calls'");
|
|
13303
|
+
}
|
|
13304
|
+
yield elem;
|
|
13305
|
+
const validation = InstructionData2.validate(this.buffer.subarray(cursor));
|
|
13306
|
+
if (!validation.ok || validation.consumed === void 0) {
|
|
13307
|
+
throw new Error("MulticallArgs: failed to get footprint for element at index " + i);
|
|
13308
|
+
}
|
|
13309
|
+
cursor += validation.consumed;
|
|
13310
|
+
}
|
|
13311
|
+
}
|
|
13312
|
+
get_calls() {
|
|
13313
|
+
return Array.from(this.callsIter());
|
|
13314
|
+
}
|
|
13315
|
+
set_calls(_value) {
|
|
13316
|
+
throw new Error("MulticallArgs: jagged array 'calls' cannot be set in-place");
|
|
13317
|
+
}
|
|
13318
|
+
/** Returns the total byte size of all elements in the jagged array. */
|
|
13319
|
+
get_calls_size() {
|
|
13320
|
+
const count = this.get_calls_length();
|
|
13321
|
+
const offset = 2;
|
|
13322
|
+
let cursor = offset;
|
|
13323
|
+
for (let i = 0; i < count; i++) {
|
|
13324
|
+
const elem = InstructionData2.from_array(this.buffer.subarray(cursor));
|
|
13325
|
+
if (!elem) {
|
|
13326
|
+
throw new Error("MulticallArgs: invalid element at index " + i + " in jagged array 'calls'");
|
|
13327
|
+
}
|
|
13328
|
+
const validation = InstructionData2.validate(this.buffer.subarray(cursor));
|
|
13329
|
+
if (!validation.ok || validation.consumed === void 0) {
|
|
13330
|
+
throw new Error("MulticallArgs: failed to get footprint for element at index " + i);
|
|
13331
|
+
}
|
|
13332
|
+
cursor += validation.consumed;
|
|
13333
|
+
}
|
|
13334
|
+
return cursor - offset;
|
|
13335
|
+
}
|
|
13336
|
+
get calls() {
|
|
13337
|
+
return this.get_calls();
|
|
13338
|
+
}
|
|
13339
|
+
set calls(value) {
|
|
13340
|
+
this.set_calls(value);
|
|
13341
|
+
}
|
|
13342
|
+
static __tnFootprintInternal(__tnParams) {
|
|
13343
|
+
return __tnEvalFootprint4(__tn_ir_MulticallArgs.root, { params: __tnParams });
|
|
13344
|
+
}
|
|
13345
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
13346
|
+
return __tnValidateIrTree4(__tn_ir_MulticallArgs, buffer, __tnParams);
|
|
13347
|
+
}
|
|
13348
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
13349
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
13350
|
+
}
|
|
13351
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
13352
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
13353
|
+
}
|
|
13354
|
+
static footprintIr(calls_calls_count) {
|
|
13355
|
+
const params = _MulticallArgs.Params.fromValues({
|
|
13356
|
+
calls_calls_count
|
|
13357
|
+
});
|
|
13358
|
+
return this.footprintIrFromParams(params);
|
|
13359
|
+
}
|
|
13360
|
+
static __tnPackParams(params) {
|
|
13361
|
+
const record = /* @__PURE__ */ Object.create(null);
|
|
13362
|
+
record["calls.calls_count"] = params.calls_calls_count;
|
|
13363
|
+
return record;
|
|
13364
|
+
}
|
|
13365
|
+
static footprintIrFromParams(params) {
|
|
13366
|
+
const __tnParams = this.__tnPackParams(params);
|
|
13367
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
13368
|
+
}
|
|
13369
|
+
static footprintFromParams(params) {
|
|
13370
|
+
const irResult = this.footprintIrFromParams(params);
|
|
13371
|
+
const maxSafe = __tnToBigInt4(Number.MAX_SAFE_INTEGER);
|
|
13372
|
+
if (__tnBigIntGreaterThan4(irResult, maxSafe)) throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for MulticallArgs");
|
|
13373
|
+
return __tnBigIntToNumber4(irResult, "MulticallArgs::footprintFromParams");
|
|
13374
|
+
}
|
|
13375
|
+
static footprintFromValues(input) {
|
|
13376
|
+
const params = _MulticallArgs.params(input);
|
|
13377
|
+
return this.footprintFromParams(params);
|
|
13378
|
+
}
|
|
13379
|
+
static footprint(params) {
|
|
13380
|
+
return this.footprintFromParams(params);
|
|
13381
|
+
}
|
|
13382
|
+
static validate(buffer, opts) {
|
|
13383
|
+
if (!buffer || buffer.length === void 0) {
|
|
13384
|
+
return { ok: false, code: "tn.invalid_buffer" };
|
|
13385
|
+
}
|
|
13386
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13387
|
+
let params = opts?.params ?? null;
|
|
13388
|
+
if (!params) {
|
|
13389
|
+
const extracted = this.__tnExtractParams(view, buffer);
|
|
13390
|
+
if (!extracted) return { ok: false, code: "tn.param_extraction_failed" };
|
|
13391
|
+
params = extracted.params;
|
|
13392
|
+
}
|
|
13393
|
+
const __tnParamsRec = this.__tnPackParams(params);
|
|
13394
|
+
const irResult = this.__tnValidateInternal(buffer, __tnParamsRec);
|
|
13395
|
+
if (!irResult.ok) {
|
|
13396
|
+
return { ok: false, code: irResult.code, consumed: irResult.consumed ? __tnBigIntToNumber4(irResult.consumed, "MulticallArgs::validate") : void 0, params };
|
|
13397
|
+
}
|
|
13398
|
+
const consumed = irResult.consumed ? __tnBigIntToNumber4(irResult.consumed, "MulticallArgs::validate") : void 0;
|
|
13399
|
+
return { ok: true, consumed, params };
|
|
13400
|
+
}
|
|
13401
|
+
static from_array(buffer, opts) {
|
|
13402
|
+
if (!buffer || buffer.length === void 0) {
|
|
13403
|
+
return null;
|
|
13404
|
+
}
|
|
13405
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13406
|
+
let params = opts?.params ?? null;
|
|
13407
|
+
if (!params) {
|
|
13408
|
+
const derived = this.__tnExtractParams(view, buffer);
|
|
13409
|
+
if (!derived) return null;
|
|
13410
|
+
params = derived.params;
|
|
13411
|
+
}
|
|
13412
|
+
const validation = this.validate(buffer, { params });
|
|
13413
|
+
if (!validation.ok) {
|
|
13414
|
+
return null;
|
|
13415
|
+
}
|
|
13416
|
+
const cached = validation.params ?? params;
|
|
13417
|
+
const state = new _MulticallArgs(buffer, cached);
|
|
13418
|
+
return state;
|
|
13419
|
+
}
|
|
13420
|
+
};
|
|
13421
|
+
((MulticallArgs2) => {
|
|
13422
|
+
MulticallArgs2.ParamKeys = Object.freeze({
|
|
13423
|
+
calls_calls_count: "calls.calls_count"
|
|
13424
|
+
});
|
|
13425
|
+
MulticallArgs2.Params = {
|
|
13426
|
+
fromValues(input) {
|
|
13427
|
+
return {
|
|
13428
|
+
calls_calls_count: __tnToBigInt4(input.calls_calls_count)
|
|
13429
|
+
};
|
|
13430
|
+
},
|
|
13431
|
+
fromBuilder(source) {
|
|
13432
|
+
if (source.dynamicParams) {
|
|
13433
|
+
return source.dynamicParams();
|
|
13434
|
+
}
|
|
13435
|
+
if (source.params) {
|
|
13436
|
+
return source.params;
|
|
13437
|
+
}
|
|
13438
|
+
return source;
|
|
13439
|
+
}
|
|
13440
|
+
};
|
|
13441
|
+
function params(input) {
|
|
13442
|
+
return MulticallArgs2.Params.fromValues(input);
|
|
13443
|
+
}
|
|
13444
|
+
MulticallArgs2.params = params;
|
|
13445
|
+
})(MulticallArgs || (MulticallArgs = {}));
|
|
13446
|
+
var MulticallArgsBuilder = class {
|
|
13447
|
+
constructor() {
|
|
13448
|
+
this.__tnJagged_calls = null;
|
|
13449
|
+
this.__tnCachedParams = null;
|
|
13450
|
+
this.__tnLastBuffer = null;
|
|
13451
|
+
this.__tnLastParams = null;
|
|
13452
|
+
this.buffer = new Uint8Array(2);
|
|
13453
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
13454
|
+
}
|
|
13455
|
+
__tnInvalidate() {
|
|
13456
|
+
this.__tnCachedParams = null;
|
|
13457
|
+
this.__tnLastBuffer = null;
|
|
13458
|
+
this.__tnLastParams = null;
|
|
13459
|
+
}
|
|
13460
|
+
set_calls_count(value) {
|
|
13461
|
+
this.view.setUint16(0, value, true);
|
|
13462
|
+
this.__tnInvalidate();
|
|
13463
|
+
return this;
|
|
13464
|
+
}
|
|
13465
|
+
set_calls(values) {
|
|
13466
|
+
const fragments = [];
|
|
13467
|
+
for (let i = 0; i < values.length; i++) {
|
|
13468
|
+
const bytes = __tnResolveStructFieldInput(values[i], "MulticallArgsBuilder::calls[" + i + "]");
|
|
13469
|
+
const validation = __tnInvokeDynamicValidate4("InstructionData", bytes);
|
|
13470
|
+
if (!validation.ok || validation.consumed === void 0) throw new Error("MulticallArgsBuilder: field 'calls' element failed validation");
|
|
13471
|
+
if (__tnBigIntToNumber4(validation.consumed, "MulticallArgsBuilder::calls") !== bytes.length) throw new Error("MulticallArgsBuilder: field 'calls' element validation did not consume the full buffer");
|
|
13472
|
+
fragments.push(bytes);
|
|
13473
|
+
}
|
|
13474
|
+
this.__tnJagged_calls = fragments;
|
|
13475
|
+
this.set_calls_count(fragments.length);
|
|
13476
|
+
this.__tnInvalidate();
|
|
13477
|
+
return this;
|
|
13478
|
+
}
|
|
13479
|
+
build() {
|
|
13480
|
+
const params = this.__tnComputeParams();
|
|
13481
|
+
const fragments = this.__tnCollectFragments();
|
|
13482
|
+
const size = this.__tnComputeSize(fragments);
|
|
13483
|
+
const buffer = new Uint8Array(size);
|
|
13484
|
+
this.__tnWriteInto(buffer, fragments);
|
|
13485
|
+
this.__tnValidateOrThrow(buffer, params);
|
|
13486
|
+
return buffer;
|
|
13487
|
+
}
|
|
13488
|
+
buildInto(target, offset = 0) {
|
|
13489
|
+
const params = this.__tnComputeParams();
|
|
13490
|
+
const fragments = this.__tnCollectFragments();
|
|
13491
|
+
const size = this.__tnComputeSize(fragments);
|
|
13492
|
+
if (target.length - offset < size) throw new Error("MulticallArgsBuilder: target buffer too small");
|
|
13493
|
+
const slice = target.subarray(offset, offset + size);
|
|
13494
|
+
this.__tnWriteInto(slice, fragments);
|
|
13495
|
+
this.__tnValidateOrThrow(slice, params);
|
|
13496
|
+
return target;
|
|
13497
|
+
}
|
|
13498
|
+
finish() {
|
|
13499
|
+
const buffer = this.build();
|
|
13500
|
+
const params = this.__tnLastParams ?? this.__tnComputeParams();
|
|
13501
|
+
const view = MulticallArgs.from_array(buffer, { params });
|
|
13502
|
+
if (!view) throw new Error("MulticallArgsBuilder: failed to finalize view");
|
|
13503
|
+
return view;
|
|
13504
|
+
}
|
|
13505
|
+
finishView() {
|
|
13506
|
+
return this.finish();
|
|
13507
|
+
}
|
|
13508
|
+
dynamicParams() {
|
|
13509
|
+
return this.__tnComputeParams();
|
|
13510
|
+
}
|
|
13511
|
+
__tnComputeParams() {
|
|
13512
|
+
if (this.__tnCachedParams) return this.__tnCachedParams;
|
|
13513
|
+
const params = MulticallArgs.Params.fromValues({
|
|
13514
|
+
calls_calls_count: (() => {
|
|
13515
|
+
const fragments = this.__tnJagged_calls;
|
|
13516
|
+
if (!fragments) throw new Error("MulticallArgsBuilder: field 'calls' must be written before computing params");
|
|
13517
|
+
return __tnToBigInt4(fragments.length);
|
|
13518
|
+
})()
|
|
13519
|
+
});
|
|
13520
|
+
this.__tnCachedParams = params;
|
|
13521
|
+
return params;
|
|
13522
|
+
}
|
|
13523
|
+
__tnCollectFragments() {
|
|
13524
|
+
const fragments = this.__tnJagged_calls;
|
|
13525
|
+
if (!fragments) throw new Error("MulticallArgsBuilder: field 'calls' must be written before build");
|
|
13526
|
+
return fragments;
|
|
13527
|
+
}
|
|
13528
|
+
__tnComputeSize(fragments) {
|
|
13529
|
+
let total = this.buffer.length;
|
|
13530
|
+
for (const fragment of fragments) total += fragment.length;
|
|
13531
|
+
return total;
|
|
13532
|
+
}
|
|
13533
|
+
__tnWriteInto(target, fragments) {
|
|
13534
|
+
target.set(this.buffer, 0);
|
|
13535
|
+
let cursor = this.buffer.length;
|
|
13536
|
+
for (const fragment of fragments) {
|
|
13537
|
+
target.set(fragment, cursor);
|
|
13538
|
+
cursor += fragment.length;
|
|
13539
|
+
}
|
|
13540
|
+
}
|
|
13541
|
+
__tnValidateOrThrow(buffer, params) {
|
|
13542
|
+
const result = MulticallArgs.validate(buffer, { params });
|
|
13543
|
+
if (!result.ok) {
|
|
13544
|
+
throw new Error(`${MulticallArgs}Builder: builder produced invalid buffer (code=${result.code ?? "unknown"})`);
|
|
13545
|
+
}
|
|
13546
|
+
this.__tnLastParams = result.params ?? params;
|
|
13547
|
+
this.__tnLastBuffer = buffer;
|
|
13548
|
+
}
|
|
13549
|
+
};
|
|
13550
|
+
__tnRegisterFootprint4("MulticallArgs", (params) => MulticallArgs.__tnInvokeFootprint(params));
|
|
13551
|
+
__tnRegisterValidate4("MulticallArgs", (buffer, params) => MulticallArgs.__tnInvokeValidate(buffer, params));
|
|
13552
|
+
__tnRegisterDynamicValidate4("MulticallArgs", (buffer) => {
|
|
13553
|
+
const result = MulticallArgs.validate(buffer);
|
|
13554
|
+
const params = result.params;
|
|
13555
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt4(result.consumed), params };
|
|
13556
|
+
});
|
|
13557
|
+
var __tn_ir_MulticallError = {
|
|
13558
|
+
typeName: "MulticallError",
|
|
13559
|
+
root: { op: "const", value: 8n }
|
|
13560
|
+
};
|
|
13561
|
+
var MulticallError = class _MulticallError {
|
|
13562
|
+
constructor(buffer) {
|
|
13563
|
+
this.buffer = buffer;
|
|
13564
|
+
this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13565
|
+
}
|
|
13566
|
+
static __tnCreateView(buffer, opts) {
|
|
13567
|
+
if (!buffer || buffer.length === void 0) throw new Error("MulticallError.__tnCreateView requires a Uint8Array");
|
|
13568
|
+
return new _MulticallError(new Uint8Array(buffer));
|
|
13569
|
+
}
|
|
13570
|
+
static builder() {
|
|
13571
|
+
return new MulticallErrorBuilder();
|
|
13572
|
+
}
|
|
13573
|
+
static fromBuilder(builder) {
|
|
13574
|
+
const buffer = builder.build();
|
|
13575
|
+
return _MulticallError.from_array(buffer);
|
|
13576
|
+
}
|
|
13577
|
+
get_code() {
|
|
13578
|
+
const offset = 0;
|
|
13579
|
+
return this.view.getBigUint64(offset, true);
|
|
13580
|
+
}
|
|
13581
|
+
set_code(value) {
|
|
13582
|
+
const offset = 0;
|
|
13583
|
+
this.view.setBigUint64(offset, value, true);
|
|
13584
|
+
}
|
|
13585
|
+
get code() {
|
|
13586
|
+
return this.get_code();
|
|
13587
|
+
}
|
|
13588
|
+
set code(value) {
|
|
13589
|
+
this.set_code(value);
|
|
13590
|
+
}
|
|
13591
|
+
static __tnFootprintInternal(__tnParams) {
|
|
13592
|
+
return __tnEvalFootprint4(__tn_ir_MulticallError.root, { params: __tnParams });
|
|
13593
|
+
}
|
|
13594
|
+
static __tnValidateInternal(buffer, __tnParams) {
|
|
13595
|
+
return __tnValidateIrTree4(__tn_ir_MulticallError, buffer, __tnParams);
|
|
13596
|
+
}
|
|
13597
|
+
static __tnInvokeFootprint(__tnParams) {
|
|
13598
|
+
return this.__tnFootprintInternal(__tnParams);
|
|
13599
|
+
}
|
|
13600
|
+
static __tnInvokeValidate(buffer, __tnParams) {
|
|
13601
|
+
return this.__tnValidateInternal(buffer, __tnParams);
|
|
13602
|
+
}
|
|
13603
|
+
static footprintIr() {
|
|
13604
|
+
return this.__tnFootprintInternal(/* @__PURE__ */ Object.create(null));
|
|
13605
|
+
}
|
|
13606
|
+
static footprint() {
|
|
13607
|
+
const irResult = this.footprintIr();
|
|
13608
|
+
const maxSafe = __tnToBigInt4(Number.MAX_SAFE_INTEGER);
|
|
13609
|
+
if (__tnBigIntGreaterThan4(irResult, maxSafe)) {
|
|
13610
|
+
throw new Error("footprint exceeds Number.MAX_SAFE_INTEGER for MulticallError");
|
|
13611
|
+
}
|
|
13612
|
+
return __tnBigIntToNumber4(irResult, "MulticallError::footprint");
|
|
13613
|
+
}
|
|
13614
|
+
static validate(buffer, _opts) {
|
|
13615
|
+
if (buffer.length < 8) return { ok: false, code: "tn.buffer_too_small", consumed: 8 };
|
|
13616
|
+
return { ok: true, consumed: 8 };
|
|
13617
|
+
}
|
|
13618
|
+
static new(code) {
|
|
13619
|
+
const buffer = new Uint8Array(8);
|
|
13620
|
+
const view = new DataView(buffer.buffer);
|
|
13621
|
+
view.setBigUint64(0, code, true);
|
|
13622
|
+
return new _MulticallError(buffer);
|
|
13623
|
+
}
|
|
13624
|
+
static from_array(buffer) {
|
|
13625
|
+
if (!buffer || buffer.length === void 0) {
|
|
13626
|
+
return null;
|
|
13627
|
+
}
|
|
13628
|
+
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
13629
|
+
const validation = this.validate(buffer);
|
|
13630
|
+
if (!validation.ok) {
|
|
13631
|
+
return null;
|
|
13632
|
+
}
|
|
13633
|
+
return new _MulticallError(buffer);
|
|
13634
|
+
}
|
|
13635
|
+
};
|
|
13636
|
+
var MulticallErrorBuilder = class {
|
|
13637
|
+
constructor() {
|
|
13638
|
+
this.buffer = new Uint8Array(8);
|
|
13639
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
13640
|
+
}
|
|
13641
|
+
set_code(value) {
|
|
13642
|
+
const cast = __tnToBigInt4(value);
|
|
13643
|
+
this.view.setBigUint64(0, cast, true);
|
|
13644
|
+
return this;
|
|
13645
|
+
}
|
|
13646
|
+
build() {
|
|
13647
|
+
return this.buffer.slice();
|
|
13648
|
+
}
|
|
13649
|
+
buildInto(target, offset = 0) {
|
|
13650
|
+
if (target.length - offset < this.buffer.length) throw new Error("target buffer too small");
|
|
13651
|
+
target.set(this.buffer, offset);
|
|
13652
|
+
return target;
|
|
13653
|
+
}
|
|
13654
|
+
finish() {
|
|
13655
|
+
const view = MulticallError.from_array(this.buffer.slice());
|
|
13656
|
+
if (!view) throw new Error("failed to build MulticallError");
|
|
13657
|
+
return view;
|
|
13658
|
+
}
|
|
13659
|
+
};
|
|
13660
|
+
__tnRegisterFootprint4("MulticallError", (params) => MulticallError.__tnInvokeFootprint(params));
|
|
13661
|
+
__tnRegisterValidate4("MulticallError", (buffer, params) => MulticallError.__tnInvokeValidate(buffer, params));
|
|
13662
|
+
__tnRegisterDynamicValidate4("MulticallError", (buffer) => {
|
|
13663
|
+
const result = MulticallError.validate(buffer);
|
|
13664
|
+
const params = result.params;
|
|
13665
|
+
return { ok: result.ok, code: result.code, consumed: result.consumed === void 0 ? void 0 : __tnToBigInt4(result.consumed), params };
|
|
13666
|
+
});
|
|
13667
|
+
|
|
13668
|
+
// src/multicall/index.ts
|
|
13669
|
+
var MULTICALL_PROGRAM_PUBKEY = new Uint8Array([
|
|
13670
|
+
0,
|
|
13671
|
+
0,
|
|
13672
|
+
0,
|
|
13673
|
+
0,
|
|
13674
|
+
0,
|
|
13675
|
+
0,
|
|
13676
|
+
0,
|
|
13677
|
+
0,
|
|
13678
|
+
0,
|
|
13679
|
+
0,
|
|
13680
|
+
0,
|
|
13681
|
+
0,
|
|
13682
|
+
0,
|
|
13683
|
+
0,
|
|
13684
|
+
0,
|
|
13685
|
+
0,
|
|
13686
|
+
0,
|
|
13687
|
+
0,
|
|
13688
|
+
0,
|
|
13689
|
+
0,
|
|
13690
|
+
0,
|
|
13691
|
+
0,
|
|
13692
|
+
0,
|
|
13693
|
+
0,
|
|
13694
|
+
0,
|
|
13695
|
+
0,
|
|
13696
|
+
0,
|
|
13697
|
+
0,
|
|
13698
|
+
0,
|
|
13699
|
+
0,
|
|
13700
|
+
0,
|
|
13701
|
+
9
|
|
13702
|
+
]);
|
|
13703
|
+
var MULTICALL_PROGRAM_ADDRESS = helpers.encodeAddress(MULTICALL_PROGRAM_PUBKEY);
|
|
13704
|
+
function assertProgramIdx(programIdx) {
|
|
13705
|
+
if (!Number.isInteger(programIdx) || programIdx < 0 || programIdx > 65535) {
|
|
13706
|
+
throw new Error("programIdx must be 0-65535");
|
|
13707
|
+
}
|
|
13708
|
+
}
|
|
13709
|
+
function buildInstructionData(call) {
|
|
13710
|
+
assertProgramIdx(call.programIdx);
|
|
13711
|
+
if (!(call.instructionData instanceof Uint8Array)) {
|
|
13712
|
+
throw new Error("instructionData must be a Uint8Array");
|
|
13713
|
+
}
|
|
13714
|
+
const builder = new InstructionDataBuilder2();
|
|
13715
|
+
builder.set_program_idx(call.programIdx);
|
|
13716
|
+
builder.data().write(call.instructionData).finish();
|
|
13717
|
+
const buffer = builder.build();
|
|
13718
|
+
const view = InstructionData2.from_array(buffer);
|
|
13719
|
+
if (!view) {
|
|
13720
|
+
throw new Error("generated InstructionData failed validation");
|
|
13721
|
+
}
|
|
13722
|
+
return buffer;
|
|
13723
|
+
}
|
|
13724
|
+
function buildMulticallInstruction(calls) {
|
|
13725
|
+
if (!Array.isArray(calls)) throw new Error("calls must be an array");
|
|
13726
|
+
if (calls.length > 65535) throw new Error("calls length must be 0-65535");
|
|
13727
|
+
const encodedCalls = calls.map(buildInstructionData);
|
|
13728
|
+
const output = new MulticallArgsBuilder().set_calls(encodedCalls).build();
|
|
13729
|
+
const validation = MulticallArgs.validate(output);
|
|
13730
|
+
if (!validation.ok || validation.consumed !== output.length) {
|
|
13731
|
+
throw new Error(
|
|
13732
|
+
`generated MulticallArgs failed validation (code=${validation.code ?? "unknown"})`
|
|
13733
|
+
);
|
|
13734
|
+
}
|
|
13735
|
+
return output;
|
|
13736
|
+
}
|
|
13737
|
+
|
|
13738
|
+
// src/clob/exchange.ts
|
|
13739
|
+
var CLOB_MAX_ATOMIC_FUNDING_SOURCES = 64;
|
|
13740
|
+
var ClobExchangeError = class extends Error {
|
|
13741
|
+
constructor(code, message) {
|
|
13742
|
+
super(message);
|
|
13743
|
+
this.name = "ClobExchangeError";
|
|
13744
|
+
this.code = code;
|
|
13745
|
+
}
|
|
13746
|
+
};
|
|
13747
|
+
function aggregateClobExchangeBalances(allocations) {
|
|
13748
|
+
const groups = /* @__PURE__ */ new Map();
|
|
13749
|
+
for (const allocation of allocations) {
|
|
13750
|
+
validateAllocation(allocation);
|
|
13751
|
+
if (!allocation.metadataReady || !allocation.mint || allocation.freeAmount === 0n) continue;
|
|
13752
|
+
const key = allocation.authority + "\0" + allocation.mint + "\0" + allocation.tokenProgram;
|
|
13753
|
+
let balance = groups.get(key);
|
|
13754
|
+
if (!balance) {
|
|
13755
|
+
balance = {
|
|
13756
|
+
authority: allocation.authority,
|
|
13757
|
+
mint: allocation.mint,
|
|
13758
|
+
tokenProgram: allocation.tokenProgram,
|
|
13759
|
+
freeAmount: 0n,
|
|
13760
|
+
transferableAmount: 0n,
|
|
13761
|
+
allocations: []
|
|
13762
|
+
};
|
|
13763
|
+
groups.set(key, balance);
|
|
13764
|
+
}
|
|
13765
|
+
balance.freeAmount += allocation.freeAmount;
|
|
13766
|
+
if (!(allocation.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN)) {
|
|
13767
|
+
balance.transferableAmount += allocation.freeAmount;
|
|
13768
|
+
}
|
|
13769
|
+
balance.allocations.push(allocation);
|
|
13770
|
+
}
|
|
13771
|
+
return [...groups.values()].map((balance) => ({
|
|
13772
|
+
...balance,
|
|
13773
|
+
allocations: balance.allocations.slice().sort(compareAllocationIdentity)
|
|
13774
|
+
})).sort((a, b) => compareText(a.mint, b.mint) || compareText(a.tokenProgram, b.tokenProgram));
|
|
13775
|
+
}
|
|
13776
|
+
function calculateClobOrderFundingAmount(args) {
|
|
13777
|
+
assertPositiveU64(args.price, "price");
|
|
13778
|
+
assertPositiveU64(args.quantity, "quantity");
|
|
13779
|
+
const amount = args.side === "buy" ? args.price * args.quantity : args.quantity;
|
|
13780
|
+
if (amount > 0xffffffffffffffffn) {
|
|
13781
|
+
throw new ClobExchangeError(
|
|
13782
|
+
"invalid_funding_amount",
|
|
13783
|
+
"order funding amount exceeds the CLOB u64 balance range"
|
|
13784
|
+
);
|
|
13785
|
+
}
|
|
13786
|
+
return amount;
|
|
13787
|
+
}
|
|
13788
|
+
function planClobOrderFunding(args) {
|
|
13789
|
+
assertMarketCanTrade(args.targetMarket, args.orderType ?? "gtc");
|
|
13790
|
+
const targetTokenSide = args.side === "buy" ? "quote" : "base";
|
|
13791
|
+
const mint = targetTokenSide === "base" ? args.targetMarket.baseMint : args.targetMarket.quoteMint;
|
|
13792
|
+
if (!mint) {
|
|
13793
|
+
throw new ClobExchangeError(
|
|
13794
|
+
"funding_plan_mismatch",
|
|
13795
|
+
"target market is missing " + targetTokenSide + " mint metadata"
|
|
13796
|
+
);
|
|
13797
|
+
}
|
|
13798
|
+
const requiredAmount = calculateClobOrderFundingAmount(args);
|
|
13799
|
+
const compatible = args.allocations.filter((allocation) => {
|
|
13800
|
+
validateAllocation(allocation);
|
|
13801
|
+
return allocation.authority === args.authority && allocation.metadataReady && allocation.mint === mint && allocation.tokenProgram === args.targetMarket.tokenProgram;
|
|
13802
|
+
});
|
|
13803
|
+
assertUniqueAllocationIdentities(compatible);
|
|
13804
|
+
const targetAllocations = compatible.filter((allocation) => allocation.market === args.targetMarket.address && allocation.tokenSide === targetTokenSide && allocation.vault === vaultForSide(args.targetMarket, targetTokenSide)).sort(compareTargetAllocation);
|
|
13805
|
+
const target = args.targetSeatIndex === void 0 ? targetAllocations[0] ?? null : targetAllocations.find((allocation) => allocation.seatIndex === args.targetSeatIndex) ?? null;
|
|
13806
|
+
if (args.targetSeatIndex !== void 0 && !target) {
|
|
13807
|
+
throw new ClobExchangeError(
|
|
13808
|
+
"funding_plan_mismatch",
|
|
13809
|
+
"requested target seat is not present in the indexed allocations"
|
|
13810
|
+
);
|
|
13811
|
+
}
|
|
13812
|
+
const targetAmount = minBigInt(requiredAmount, target?.freeAmount ?? 0n);
|
|
13813
|
+
let remaining = requiredAmount - targetAmount;
|
|
13814
|
+
const targetAllocationIdentity = target ? sourceAllocationIdentity(target) : null;
|
|
13815
|
+
const sourceCandidates = compatible.filter(
|
|
13816
|
+
(allocation) => sourceAllocationIdentity(allocation) !== targetAllocationIdentity && allocation.freeAmount > 0n && !(allocation.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN)
|
|
13817
|
+
);
|
|
13818
|
+
const sources = [];
|
|
13819
|
+
for (const allocation of sourceCandidates) {
|
|
13820
|
+
if (remaining === 0n) break;
|
|
13821
|
+
if (sources.length >= CLOB_MAX_ATOMIC_FUNDING_SOURCES) {
|
|
13822
|
+
throw new ClobExchangeError(
|
|
13823
|
+
"too_many_funding_sources",
|
|
13824
|
+
"exchange balance is fragmented across more than " + CLOB_MAX_ATOMIC_FUNDING_SOURCES.toString() + " source allocations for one atomic order"
|
|
13825
|
+
);
|
|
13826
|
+
}
|
|
13827
|
+
const amount = minBigInt(remaining, allocation.freeAmount);
|
|
13828
|
+
sources.push({ allocation, amount });
|
|
13829
|
+
remaining -= amount;
|
|
13830
|
+
}
|
|
13831
|
+
if (requiredAmount > targetAmount && args.targetMarket.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
13832
|
+
throw new ClobExchangeError(
|
|
13833
|
+
"deposits_frozen",
|
|
13834
|
+
"target market deposits are frozen and its existing allocation cannot fund the order"
|
|
13835
|
+
);
|
|
13836
|
+
}
|
|
13837
|
+
return {
|
|
13838
|
+
authority: args.authority,
|
|
13839
|
+
targetMarket: args.targetMarket.address,
|
|
13840
|
+
targetTokenSide,
|
|
13841
|
+
targetSeatIndex: target?.seatIndex ?? null,
|
|
13842
|
+
mint,
|
|
13843
|
+
tokenProgram: args.targetMarket.tokenProgram,
|
|
13844
|
+
requiredAmount,
|
|
13845
|
+
targetAmount,
|
|
13846
|
+
exchangeAvailableAmount: (target?.freeAmount ?? 0n) + sourceCandidates.reduce(
|
|
13847
|
+
(sum, allocation) => sum + allocation.freeAmount,
|
|
13848
|
+
0n
|
|
13849
|
+
),
|
|
13850
|
+
sources,
|
|
13851
|
+
shortfall: remaining
|
|
13852
|
+
};
|
|
13853
|
+
}
|
|
13854
|
+
function validateClobOrderFundingPlanSeats(args) {
|
|
13855
|
+
if (args.plan.targetSeatIndex === null) {
|
|
13856
|
+
if (args.targetSeats.some((seat) => seat.seatAuthority === args.plan.authority)) {
|
|
13857
|
+
throw new ClobExchangeError("stale_balance", "a target seat appeared after funding was planned");
|
|
13858
|
+
}
|
|
13859
|
+
} else {
|
|
13860
|
+
const seat = args.targetSeats.find(
|
|
13861
|
+
(entry) => entry.seatIndex === args.plan.targetSeatIndex && entry.seatAuthority === args.plan.authority
|
|
13862
|
+
);
|
|
13863
|
+
const currentAmount = seat ? seatAmount(seat, args.plan.targetTokenSide) : null;
|
|
13864
|
+
if (currentAmount === null || currentAmount < args.plan.targetAmount || args.plan.sources.length > 0 && currentAmount !== args.plan.targetAmount) {
|
|
13865
|
+
throw new ClobExchangeError("stale_balance", "target seat balance changed after funding was planned");
|
|
13866
|
+
}
|
|
13867
|
+
}
|
|
13868
|
+
const hasExternalSources = args.plan.sources.some(
|
|
13869
|
+
(source) => source.allocation.market !== args.plan.targetMarket
|
|
13870
|
+
);
|
|
13871
|
+
if (hasExternalSources && !args.sourceSeatsByMarket) {
|
|
13872
|
+
throw new ClobExchangeError(
|
|
13873
|
+
"funding_plan_mismatch",
|
|
13874
|
+
"fresh source seat data is required for every planned funding market"
|
|
13875
|
+
);
|
|
13876
|
+
}
|
|
13877
|
+
for (const source of args.plan.sources) {
|
|
13878
|
+
const isTargetMarketSource = source.allocation.market === args.plan.targetMarket;
|
|
13879
|
+
if (!isTargetMarketSource && !Object.prototype.hasOwnProperty.call(args.sourceSeatsByMarket, source.allocation.market)) {
|
|
13880
|
+
throw new ClobExchangeError(
|
|
13881
|
+
"funding_plan_mismatch",
|
|
13882
|
+
"fresh source seat data is missing for market " + source.allocation.market
|
|
13883
|
+
);
|
|
13884
|
+
}
|
|
13885
|
+
const sourceSeats = isTargetMarketSource ? args.targetSeats : args.sourceSeatsByMarket[source.allocation.market];
|
|
13886
|
+
const seat = sourceSeats.find(
|
|
13887
|
+
(entry) => entry.seatIndex === source.allocation.seatIndex && entry.seatAuthority === args.plan.authority
|
|
13888
|
+
);
|
|
13889
|
+
if (!seat || seatAmount(seat, source.allocation.tokenSide) < source.amount) {
|
|
13890
|
+
throw new ClobExchangeError(
|
|
13891
|
+
"stale_balance",
|
|
13892
|
+
"source seat balance changed after funding was planned for market " + source.allocation.market
|
|
13893
|
+
);
|
|
13894
|
+
}
|
|
13895
|
+
}
|
|
13896
|
+
}
|
|
13897
|
+
function buildClobExchangeDepositInstructionPlan(args) {
|
|
13898
|
+
assertPositiveU64(args.amount, "amount");
|
|
13899
|
+
if (args.market.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
13900
|
+
throw new ClobExchangeError("deposits_frozen", "market deposits are frozen");
|
|
13901
|
+
}
|
|
13902
|
+
const marketRecordIndex = 1;
|
|
13903
|
+
const opensSeat = args.seatIndex === null;
|
|
13904
|
+
const instructions = [
|
|
13905
|
+
marketRecordInstruction(args.market, marketRecordIndex, opensSeat ? null : args.authority, args.seatIndex),
|
|
13906
|
+
...opensSeat ? [createSeatCreateInstruction({
|
|
13907
|
+
marketRecordIndex,
|
|
13908
|
+
seatAuthorityAccountBytes: addressBytes(args.authority)
|
|
13909
|
+
})] : [],
|
|
13910
|
+
createTokenDepositInstruction({
|
|
13911
|
+
marketRecordIndex,
|
|
13912
|
+
tokenProgramAccountBytes: addressBytes(args.market.tokenProgram),
|
|
13913
|
+
fromAccountBytes: addressBytes(args.walletTokenAccount),
|
|
13914
|
+
toAccountBytes: addressBytes(vaultForSide(args.market, args.tokenSide)),
|
|
13915
|
+
amount: args.amount
|
|
13916
|
+
})
|
|
13917
|
+
];
|
|
13918
|
+
const accounts = new AccountPlan();
|
|
13919
|
+
addTargetMarketAccounts(accounts, args.market);
|
|
13920
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
13921
|
+
accounts.addWrite(vaultForSide(args.market, args.tokenSide));
|
|
13922
|
+
accounts.addRead(args.authority);
|
|
13923
|
+
accounts.addRead(args.market.tokenProgram);
|
|
13924
|
+
return accounts.finish(concatClobInstructions(...instructions), {
|
|
13925
|
+
instruction: "exchange_deposit",
|
|
13926
|
+
label: "Deposit to CLOB exchange",
|
|
13927
|
+
value: {
|
|
13928
|
+
market: args.market.address,
|
|
13929
|
+
mint: mintForSide(args.market, args.tokenSide),
|
|
13930
|
+
amount: args.amount.toString()
|
|
13931
|
+
}
|
|
13932
|
+
});
|
|
13933
|
+
}
|
|
13934
|
+
function buildClobExchangeWithdrawalInstructionPlan(args) {
|
|
13935
|
+
assertPositiveU64(args.amount, "amount");
|
|
13936
|
+
if (args.market.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN) {
|
|
13937
|
+
throw new ClobExchangeError("withdrawals_frozen", "market withdrawals are frozen");
|
|
13938
|
+
}
|
|
13939
|
+
const marketRecordIndex = 1;
|
|
13940
|
+
const vault = vaultForSide(args.market, args.tokenSide);
|
|
13941
|
+
const instructionData = concatClobInstructions(
|
|
13942
|
+
marketRecordInstruction(args.market, marketRecordIndex, args.authority, args.seatIndex),
|
|
13943
|
+
createTokenWithdrawInstruction({
|
|
13944
|
+
marketRecordIndex,
|
|
13945
|
+
tokenProgramAccountBytes: addressBytes(args.market.tokenProgram),
|
|
13946
|
+
fromAccountBytes: addressBytes(vault),
|
|
13947
|
+
toAccountBytes: addressBytes(args.walletTokenAccount),
|
|
13948
|
+
amount: args.amount
|
|
13949
|
+
})
|
|
13950
|
+
);
|
|
13951
|
+
const accounts = new AccountPlan();
|
|
13952
|
+
addSourceMarketAccounts(accounts, args.market, vault);
|
|
13953
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
13954
|
+
accounts.addRead(args.authority);
|
|
13955
|
+
accounts.addRead(args.market.tokenProgram);
|
|
13956
|
+
return accounts.finish(instructionData, {
|
|
13957
|
+
instruction: "exchange_withdrawal",
|
|
13958
|
+
label: "Withdraw from CLOB exchange",
|
|
13959
|
+
value: {
|
|
13960
|
+
market: args.market.address,
|
|
13961
|
+
mint: mintForSide(args.market, args.tokenSide),
|
|
13962
|
+
amount: args.amount.toString()
|
|
13963
|
+
}
|
|
13964
|
+
});
|
|
13965
|
+
}
|
|
13966
|
+
function buildClobFundedOrderInstructionPlan(args) {
|
|
13967
|
+
validateFundedOrderPlan(args);
|
|
13968
|
+
const instructions = [];
|
|
13969
|
+
const accounts = new AccountPlan();
|
|
13970
|
+
const sourceMarkets = indexSourceMarkets(args.sourceMarkets ?? []);
|
|
13971
|
+
args.plan.sources.forEach((source, index) => {
|
|
13972
|
+
const sourceMarket = source.allocation.market === args.targetMarket.address ? args.targetMarket : sourceMarkets.get(source.allocation.market);
|
|
13973
|
+
const sourceRecordIndex = index + 1;
|
|
13974
|
+
instructions.push(
|
|
13975
|
+
marketRecordInstruction(
|
|
13976
|
+
sourceMarket,
|
|
13977
|
+
sourceRecordIndex,
|
|
13978
|
+
args.plan.authority,
|
|
13979
|
+
source.allocation.seatIndex
|
|
13980
|
+
),
|
|
13981
|
+
createTokenWithdrawInstruction({
|
|
13982
|
+
marketRecordIndex: sourceRecordIndex,
|
|
13983
|
+
tokenProgramAccountBytes: addressBytes(args.plan.tokenProgram),
|
|
13984
|
+
fromAccountBytes: addressBytes(source.allocation.vault),
|
|
13985
|
+
toAccountBytes: addressBytes(args.walletTokenAccount),
|
|
13986
|
+
amount: source.amount
|
|
13987
|
+
})
|
|
13988
|
+
);
|
|
13989
|
+
addSourceMarketAccounts(accounts, sourceMarket, source.allocation.vault);
|
|
13990
|
+
});
|
|
13991
|
+
const targetRecordIndex = args.plan.sources.length + 1;
|
|
13992
|
+
const opensSeat = args.plan.targetSeatIndex === null;
|
|
13993
|
+
instructions.push(
|
|
13994
|
+
marketRecordInstruction(
|
|
13995
|
+
args.targetMarket,
|
|
13996
|
+
targetRecordIndex,
|
|
13997
|
+
opensSeat ? null : args.plan.authority,
|
|
13998
|
+
args.plan.targetSeatIndex
|
|
13999
|
+
)
|
|
14000
|
+
);
|
|
14001
|
+
if (opensSeat) {
|
|
14002
|
+
instructions.push(createSeatCreateInstruction({
|
|
14003
|
+
marketRecordIndex: targetRecordIndex,
|
|
14004
|
+
seatAuthorityAccountBytes: addressBytes(args.plan.authority)
|
|
14005
|
+
}));
|
|
14006
|
+
}
|
|
14007
|
+
const sourceAmount = fundingSourcesAmount(args.plan.sources);
|
|
14008
|
+
if (sourceAmount > 0n) {
|
|
14009
|
+
instructions.push(createTokenDepositInstruction({
|
|
14010
|
+
marketRecordIndex: targetRecordIndex,
|
|
14011
|
+
tokenProgramAccountBytes: addressBytes(args.plan.tokenProgram),
|
|
14012
|
+
fromAccountBytes: addressBytes(args.walletTokenAccount),
|
|
14013
|
+
toAccountBytes: addressBytes(vaultForSide(args.targetMarket, args.plan.targetTokenSide)),
|
|
14014
|
+
amount: sourceAmount
|
|
14015
|
+
}));
|
|
14016
|
+
accounts.addWrite(args.walletTokenAccount);
|
|
14017
|
+
accounts.addWrite(vaultForSide(args.targetMarket, args.plan.targetTokenSide));
|
|
14018
|
+
}
|
|
14019
|
+
instructions.push(createOrderEntryInstruction({
|
|
14020
|
+
marketRecordIndex: targetRecordIndex,
|
|
14021
|
+
side: args.plan.targetTokenSide === "quote" ? "buy" : "sell",
|
|
14022
|
+
orderType: args.orderType,
|
|
14023
|
+
price: args.price,
|
|
14024
|
+
quantity: args.quantity,
|
|
14025
|
+
expirationTime: args.expirationTime,
|
|
14026
|
+
clientId: args.clientId,
|
|
14027
|
+
discardAfterMatch: args.discardAfterMatch,
|
|
14028
|
+
failIfOutsideBook: args.failIfOutsideBook
|
|
14029
|
+
}));
|
|
14030
|
+
addTargetMarketAccounts(accounts, args.targetMarket);
|
|
14031
|
+
accounts.addRead(args.plan.authority);
|
|
14032
|
+
accounts.addRead(args.plan.tokenProgram);
|
|
14033
|
+
return accounts.finish(concatClobInstructions(...instructions), {
|
|
14034
|
+
instruction: "funded_order",
|
|
14035
|
+
label: "Place order using CLOB exchange balance",
|
|
14036
|
+
value: {
|
|
14037
|
+
targetMarket: args.targetMarket.address,
|
|
14038
|
+
mint: args.plan.mint,
|
|
14039
|
+
requiredAmount: args.plan.requiredAmount.toString(),
|
|
14040
|
+
exchangeAmount: (args.plan.targetAmount + sourceAmount).toString(),
|
|
14041
|
+
sourceMarketCount: args.plan.sources.length.toString(),
|
|
14042
|
+
...args.plan.sources.length > 0 ? {
|
|
14043
|
+
sourceMarkets: args.plan.sources.map((source) => source.allocation.market + ":" + source.amount.toString()).join(",")
|
|
14044
|
+
} : {}
|
|
14045
|
+
}
|
|
14046
|
+
});
|
|
14047
|
+
}
|
|
14048
|
+
function concatClobInstructions(...instructions) {
|
|
14049
|
+
return async (context) => {
|
|
14050
|
+
const chunks = await Promise.all(instructions.map((instruction) => instruction(context)));
|
|
14051
|
+
const output = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.length, 0));
|
|
14052
|
+
let offset = 0;
|
|
14053
|
+
for (const chunk of chunks) {
|
|
14054
|
+
output.set(chunk, offset);
|
|
14055
|
+
offset += chunk.length;
|
|
14056
|
+
}
|
|
14057
|
+
return output;
|
|
14058
|
+
};
|
|
14059
|
+
}
|
|
14060
|
+
function wrapClobPlanWithTokenAccountInitialization(args) {
|
|
14061
|
+
const instructionData = async (context) => {
|
|
14062
|
+
const [initializeAccountData, clobData] = await Promise.all([
|
|
14063
|
+
args.initialization.instructionData(context),
|
|
14064
|
+
args.plan.instructionData(context)
|
|
14065
|
+
]);
|
|
14066
|
+
return buildMulticallInstruction([
|
|
14067
|
+
{
|
|
14068
|
+
programIdx: context.getAccountIndex(addressBytes(args.initialization.tokenProgram)),
|
|
14069
|
+
instructionData: initializeAccountData
|
|
14070
|
+
},
|
|
14071
|
+
{
|
|
14072
|
+
programIdx: context.getAccountIndex(addressBytes(args.clobProgram)),
|
|
14073
|
+
instructionData: clobData
|
|
14074
|
+
}
|
|
14075
|
+
]);
|
|
14076
|
+
};
|
|
14077
|
+
const accounts = new AccountPlan();
|
|
14078
|
+
for (const account of args.plan.readWriteAccounts) accounts.addWrite(account);
|
|
14079
|
+
for (const account of args.plan.readOnlyAccounts) accounts.addRead(account);
|
|
14080
|
+
accounts.addWrite(args.initialization.tokenAccount);
|
|
14081
|
+
accounts.addRead(args.initialization.mint);
|
|
14082
|
+
accounts.addRead(args.initialization.owner);
|
|
14083
|
+
accounts.addRead(args.initialization.tokenProgram);
|
|
14084
|
+
accounts.addRead(args.clobProgram);
|
|
14085
|
+
const plan = accounts.finish(instructionData, {
|
|
14086
|
+
...args.plan.review,
|
|
14087
|
+
value: {
|
|
14088
|
+
...args.plan.review.value,
|
|
14089
|
+
initializesWalletTokenAccount: "true"
|
|
14090
|
+
}
|
|
14091
|
+
});
|
|
14092
|
+
return {
|
|
14093
|
+
...plan,
|
|
14094
|
+
programAddress: MULTICALL_PROGRAM_ADDRESS
|
|
14095
|
+
};
|
|
14096
|
+
}
|
|
14097
|
+
function validateAllocation(allocation) {
|
|
14098
|
+
assertU64(allocation.freeAmount, "allocation.freeAmount");
|
|
14099
|
+
assertClobActiveSeatIndex(allocation.seatIndex, "allocation.seatIndex");
|
|
14100
|
+
}
|
|
14101
|
+
function assertMarketCanTrade(market, orderType) {
|
|
14102
|
+
if (market.statusFlags & CLOB_STATUS_FLAG_PAUSED) {
|
|
14103
|
+
throw new ClobExchangeError("market_paused", "target market is paused");
|
|
14104
|
+
}
|
|
14105
|
+
if (market.statusFlags & CLOB_STATUS_FLAG_POST_ONLY && orderType !== "alo") {
|
|
14106
|
+
throw new ClobExchangeError(
|
|
14107
|
+
"market_post_only",
|
|
14108
|
+
"target market is post-only and requires an ALO order"
|
|
14109
|
+
);
|
|
14110
|
+
}
|
|
14111
|
+
}
|
|
14112
|
+
function validateFundedOrderPlan(args) {
|
|
14113
|
+
const plan = args.plan;
|
|
14114
|
+
if (!Array.isArray(plan.sources)) {
|
|
14115
|
+
throw new ClobExchangeError("funding_plan_mismatch", "funding sources must be an array");
|
|
14116
|
+
}
|
|
14117
|
+
if (plan.sources.length > CLOB_MAX_ATOMIC_FUNDING_SOURCES) {
|
|
14118
|
+
throw new ClobExchangeError(
|
|
14119
|
+
"too_many_funding_sources",
|
|
14120
|
+
"funding plan exceeds the atomic source allocation limit"
|
|
14121
|
+
);
|
|
14122
|
+
}
|
|
14123
|
+
assertFundingPlanU64(plan.requiredAmount, "requiredAmount");
|
|
14124
|
+
assertFundingPlanU64(plan.targetAmount, "targetAmount");
|
|
14125
|
+
assertFundingPlanU64(plan.shortfall, "shortfall");
|
|
14126
|
+
assertUniqueAllocationIdentities(plan.sources.map((source) => source.allocation));
|
|
14127
|
+
plan.sources.forEach((source, index) => {
|
|
14128
|
+
validateAllocation(source.allocation);
|
|
14129
|
+
assertFundingPlanU64(source.amount, "sources[" + index.toString() + "].amount");
|
|
14130
|
+
if (source.amount === 0n || source.amount > source.allocation.freeAmount) {
|
|
14131
|
+
throw new ClobExchangeError(
|
|
14132
|
+
"funding_plan_mismatch",
|
|
14133
|
+
"funding source amount must be positive and no greater than its free allocation"
|
|
14134
|
+
);
|
|
14135
|
+
}
|
|
14136
|
+
});
|
|
14137
|
+
if (plan.shortfall > 0n) {
|
|
14138
|
+
throw new ClobExchangeError(
|
|
14139
|
+
"insufficient_funds",
|
|
14140
|
+
"order funding plan is short " + plan.shortfall.toString() + " units"
|
|
14141
|
+
);
|
|
14142
|
+
}
|
|
14143
|
+
assertMarketCanTrade(args.targetMarket, args.orderType ?? "gtc");
|
|
14144
|
+
const expectedAmount = calculateClobOrderFundingAmount({
|
|
14145
|
+
side: plan.targetTokenSide === "quote" ? "buy" : "sell",
|
|
14146
|
+
price: args.price,
|
|
14147
|
+
quantity: args.quantity
|
|
14148
|
+
});
|
|
14149
|
+
const plannedAmount = plan.targetAmount + fundingSourcesAmount(plan.sources);
|
|
14150
|
+
if (plannedAmount !== plan.requiredAmount) {
|
|
14151
|
+
throw new ClobExchangeError(
|
|
14152
|
+
"funding_plan_mismatch",
|
|
14153
|
+
"funding plan amounts do not add up to the required order funding"
|
|
14154
|
+
);
|
|
14155
|
+
}
|
|
14156
|
+
if (plan.targetMarket !== args.targetMarket.address || plan.tokenProgram !== args.targetMarket.tokenProgram || plan.mint !== mintForSide(args.targetMarket, plan.targetTokenSide) || plan.requiredAmount !== expectedAmount) {
|
|
14157
|
+
throw new ClobExchangeError(
|
|
14158
|
+
"funding_plan_mismatch",
|
|
14159
|
+
"funding plan does not match the target market or order"
|
|
14160
|
+
);
|
|
14161
|
+
}
|
|
14162
|
+
const sourceMarkets = indexSourceMarkets(args.sourceMarkets ?? []);
|
|
14163
|
+
const plannedExternalSourceMarkets = new Set(
|
|
14164
|
+
plan.sources.map((source) => source.allocation.market).filter((market) => market !== args.targetMarket.address)
|
|
14165
|
+
);
|
|
14166
|
+
if (sourceMarkets.size !== plannedExternalSourceMarkets.size) {
|
|
14167
|
+
throw new ClobExchangeError(
|
|
14168
|
+
"funding_plan_mismatch",
|
|
14169
|
+
"prepared source markets do not exactly match the funding plan"
|
|
14170
|
+
);
|
|
14171
|
+
}
|
|
14172
|
+
for (const source of plan.sources) {
|
|
14173
|
+
const isTargetMarketSource = source.allocation.market === args.targetMarket.address;
|
|
14174
|
+
const sourceMarket = isTargetMarketSource ? args.targetMarket : sourceMarkets.get(source.allocation.market);
|
|
14175
|
+
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) {
|
|
14176
|
+
throw new ClobExchangeError(
|
|
14177
|
+
"funding_plan_mismatch",
|
|
14178
|
+
"funding source does not match its prepared source market allocation"
|
|
14179
|
+
);
|
|
14180
|
+
}
|
|
14181
|
+
if (sourceMarket.statusFlags & CLOB_STATUS_FLAG_WITHDRAWALS_FROZEN) {
|
|
14182
|
+
throw new ClobExchangeError("withdrawals_frozen", "source market withdrawals are frozen");
|
|
14183
|
+
}
|
|
14184
|
+
}
|
|
14185
|
+
if (plan.sources.length > 0 && args.targetMarket.statusFlags & CLOB_STATUS_FLAG_DEPOSITS_FROZEN) {
|
|
14186
|
+
throw new ClobExchangeError("deposits_frozen", "target market deposits are frozen");
|
|
14187
|
+
}
|
|
14188
|
+
}
|
|
14189
|
+
function assertFundingPlanU64(value, label) {
|
|
14190
|
+
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
14191
|
+
throw new ClobExchangeError(
|
|
14192
|
+
"funding_plan_mismatch",
|
|
14193
|
+
label + " must be between 0 and 18446744073709551615"
|
|
14194
|
+
);
|
|
14195
|
+
}
|
|
14196
|
+
}
|
|
14197
|
+
function marketRecordInstruction(market, marketRecordIndex, authority, seatIndex) {
|
|
14198
|
+
return createMarketRecordInstruction({
|
|
14199
|
+
marketRecordIndex,
|
|
14200
|
+
seatArenaAccountBytes: addressBytes(market.address),
|
|
14201
|
+
orderArenaAccountBytes: addressBytes(market.orderArena),
|
|
14202
|
+
bidsCbookAccountBytes: addressBytes(market.bidsCbook),
|
|
14203
|
+
asksCbookAccountBytes: addressBytes(market.asksCbook),
|
|
14204
|
+
seatAuthorityAccountBytes: authority ? addressBytes(authority) : void 0,
|
|
14205
|
+
seatIndex: seatIndex ?? void 0,
|
|
14206
|
+
tokenProgramAccountBytes: addressBytes(market.tokenProgram),
|
|
14207
|
+
baseVaultAccountBytes: addressBytes(market.baseVault),
|
|
14208
|
+
quoteVaultAccountBytes: addressBytes(market.quoteVault)
|
|
14209
|
+
});
|
|
14210
|
+
}
|
|
14211
|
+
function addTargetMarketAccounts(accounts, market) {
|
|
14212
|
+
accounts.addWrite(market.address);
|
|
14213
|
+
accounts.addWrite(market.orderArena);
|
|
14214
|
+
accounts.addWrite(market.bidsCbook);
|
|
14215
|
+
accounts.addWrite(market.asksCbook);
|
|
14216
|
+
accounts.addRead(market.baseVault);
|
|
14217
|
+
accounts.addRead(market.quoteVault);
|
|
14218
|
+
}
|
|
14219
|
+
function addSourceMarketAccounts(accounts, market, sourceVault) {
|
|
14220
|
+
accounts.addWrite(market.address);
|
|
14221
|
+
accounts.addWrite(sourceVault);
|
|
14222
|
+
accounts.addWrite(market.orderArena);
|
|
14223
|
+
accounts.addWrite(market.bidsCbook);
|
|
14224
|
+
accounts.addWrite(market.asksCbook);
|
|
14225
|
+
accounts.addRead(market.baseVault);
|
|
14226
|
+
accounts.addRead(market.quoteVault);
|
|
14227
|
+
}
|
|
14228
|
+
var AccountPlan = class {
|
|
14229
|
+
constructor() {
|
|
14230
|
+
this.writes = /* @__PURE__ */ new Set();
|
|
14231
|
+
this.reads = /* @__PURE__ */ new Set();
|
|
14232
|
+
}
|
|
14233
|
+
addWrite(address) {
|
|
14234
|
+
this.writes.add(address);
|
|
14235
|
+
this.reads.delete(address);
|
|
14236
|
+
}
|
|
14237
|
+
addRead(address) {
|
|
14238
|
+
if (!this.writes.has(address)) this.reads.add(address);
|
|
14239
|
+
}
|
|
14240
|
+
finish(instructionData, review) {
|
|
14241
|
+
return {
|
|
14242
|
+
instructionData,
|
|
14243
|
+
readWriteAccounts: [...this.writes],
|
|
14244
|
+
readOnlyAccounts: [...this.reads],
|
|
14245
|
+
review
|
|
14246
|
+
};
|
|
14247
|
+
}
|
|
14248
|
+
};
|
|
14249
|
+
function addressBytes(address) {
|
|
14250
|
+
return sdk.Pubkey.from(address).toBytes();
|
|
14251
|
+
}
|
|
14252
|
+
function mintForSide(market, side) {
|
|
14253
|
+
const mint = side === "base" ? market.baseMint : market.quoteMint;
|
|
14254
|
+
if (!mint) {
|
|
14255
|
+
throw new ClobExchangeError(
|
|
14256
|
+
"funding_plan_mismatch",
|
|
14257
|
+
"market is missing " + side + " mint metadata"
|
|
14258
|
+
);
|
|
14259
|
+
}
|
|
14260
|
+
return mint;
|
|
14261
|
+
}
|
|
14262
|
+
function vaultForSide(market, side) {
|
|
14263
|
+
return side === "base" ? market.baseVault : market.quoteVault;
|
|
14264
|
+
}
|
|
14265
|
+
function sideForVault(market, vault) {
|
|
14266
|
+
if (vault === market.baseVault) return "base";
|
|
14267
|
+
if (vault === market.quoteVault) return "quote";
|
|
14268
|
+
return null;
|
|
14269
|
+
}
|
|
14270
|
+
function seatAmount(seat, side) {
|
|
14271
|
+
return side === "base" ? seat.quantityBase : seat.quantityQuote;
|
|
14272
|
+
}
|
|
14273
|
+
function compareAllocationIdentity(a, b) {
|
|
14274
|
+
return compareText(a.market, b.market) || a.seatIndex - b.seatIndex || compareText(a.tokenSide, b.tokenSide);
|
|
14275
|
+
}
|
|
14276
|
+
function compareTargetAllocation(a, b) {
|
|
14277
|
+
return compareBigIntDescending(a.freeAmount, b.freeAmount) || a.seatIndex - b.seatIndex;
|
|
14278
|
+
}
|
|
14279
|
+
function compareBigIntDescending(a, b) {
|
|
14280
|
+
return a > b ? -1 : a < b ? 1 : 0;
|
|
14281
|
+
}
|
|
14282
|
+
function compareText(a, b) {
|
|
14283
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
14284
|
+
}
|
|
14285
|
+
function sourceAllocationIdentity(allocation) {
|
|
14286
|
+
return allocation.market + "\0" + allocation.seatIndex.toString() + "\0" + allocation.tokenSide;
|
|
14287
|
+
}
|
|
14288
|
+
function assertUniqueAllocationIdentities(allocations) {
|
|
14289
|
+
const identities = /* @__PURE__ */ new Set();
|
|
14290
|
+
for (const allocation of allocations) {
|
|
14291
|
+
const identity = sourceAllocationIdentity(allocation);
|
|
14292
|
+
if (identities.has(identity)) {
|
|
14293
|
+
throw new ClobExchangeError(
|
|
14294
|
+
"funding_plan_mismatch",
|
|
14295
|
+
"duplicate indexed allocation for market " + allocation.market + " seat " + allocation.seatIndex.toString()
|
|
14296
|
+
);
|
|
14297
|
+
}
|
|
14298
|
+
identities.add(identity);
|
|
14299
|
+
}
|
|
14300
|
+
}
|
|
14301
|
+
function fundingSourcesAmount(sources) {
|
|
14302
|
+
return sources.reduce((amount, source) => amount + source.amount, 0n);
|
|
14303
|
+
}
|
|
14304
|
+
function indexSourceMarkets(markets) {
|
|
14305
|
+
const byAddress = /* @__PURE__ */ new Map();
|
|
14306
|
+
for (const market of markets) {
|
|
14307
|
+
if (byAddress.has(market.address)) {
|
|
14308
|
+
throw new ClobExchangeError(
|
|
14309
|
+
"funding_plan_mismatch",
|
|
14310
|
+
"duplicate prepared source market " + market.address
|
|
14311
|
+
);
|
|
14312
|
+
}
|
|
14313
|
+
byAddress.set(market.address, market);
|
|
14314
|
+
}
|
|
14315
|
+
return byAddress;
|
|
14316
|
+
}
|
|
14317
|
+
function minBigInt(a, b) {
|
|
14318
|
+
return a < b ? a : b;
|
|
14319
|
+
}
|
|
14320
|
+
function assertPositiveU64(value, label) {
|
|
14321
|
+
assertU64(value, label);
|
|
14322
|
+
if (value === 0n) {
|
|
14323
|
+
throw new ClobExchangeError("invalid_funding_amount", label + " must be positive");
|
|
14324
|
+
}
|
|
14325
|
+
}
|
|
14326
|
+
function assertU64(value, label) {
|
|
14327
|
+
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
14328
|
+
throw new Error(label + " must be between 0 and 18446744073709551615");
|
|
14329
|
+
}
|
|
14330
|
+
}
|
|
14331
|
+
|
|
11031
14332
|
// src/clob/index.ts
|
|
11032
14333
|
var CLOB_INSTRUCTION_MARKET_RECORD = 0;
|
|
11033
14334
|
var CLOB_INSTRUCTION_SEAT_CREATE = 1;
|
|
@@ -11083,15 +14384,16 @@ var CLOB_CLIENT_ID_SIZE = 16;
|
|
|
11083
14384
|
var CLOB_SEATLESS_SEAT_IDX = 1;
|
|
11084
14385
|
var CLOB_NULL_INDEX = 1048575;
|
|
11085
14386
|
function createMarketRecordInstruction(args) {
|
|
14387
|
+
const seatIndex = args.seatIndex === void 0 ? CLOB_NULL_INDEX : assertClobActiveSeatIndex(args.seatIndex);
|
|
11086
14388
|
return async (context) => {
|
|
11087
|
-
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(
|
|
14389
|
+
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();
|
|
11088
14390
|
return buildClobInstruction("market_record", payload);
|
|
11089
14391
|
};
|
|
11090
14392
|
}
|
|
11091
14393
|
function createMarketCreateInstruction(args) {
|
|
11092
14394
|
return async (context) => {
|
|
11093
|
-
|
|
11094
|
-
|
|
14395
|
+
assertU642(args.lotSize, "lotSize");
|
|
14396
|
+
assertU642(args.tickSize, "tickSize");
|
|
11095
14397
|
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));
|
|
11096
14398
|
builder.proof_seat_arena().write(args.seatArenaStateProof).finish();
|
|
11097
14399
|
builder.proof_order_arena().write(args.orderArenaStateProof).finish();
|
|
@@ -11117,8 +14419,8 @@ function createTokenWithdrawInstruction(args) {
|
|
|
11117
14419
|
function createOrderEntryInstruction(args) {
|
|
11118
14420
|
return async () => {
|
|
11119
14421
|
const flags = orderFlags(args);
|
|
11120
|
-
|
|
11121
|
-
|
|
14422
|
+
assertU642(args.price, "price");
|
|
14423
|
+
assertU642(args.quantity, "quantity");
|
|
11122
14424
|
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);
|
|
11123
14425
|
if (args.clientId) builder.client_id().write(assertClientId(args.clientId)).finish();
|
|
11124
14426
|
return buildClobInstruction("create_order_entry", builder.build());
|
|
@@ -11127,8 +14429,8 @@ function createOrderEntryInstruction(args) {
|
|
|
11127
14429
|
function createSeatlessOrderEntryInstruction(args) {
|
|
11128
14430
|
return async (context) => {
|
|
11129
14431
|
const flags = orderFlags(args);
|
|
11130
|
-
|
|
11131
|
-
|
|
14432
|
+
assertU642(args.price, "price");
|
|
14433
|
+
assertU642(args.quantity, "quantity");
|
|
11132
14434
|
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]);
|
|
11133
14435
|
if (args.clientId) builder.client_id().write(assertClientId(args.clientId)).finish();
|
|
11134
14436
|
return buildClobInstruction("create_seatless_order_entry", builder.build());
|
|
@@ -11140,7 +14442,7 @@ function createModifyOrderEntryInstruction(args) {
|
|
|
11140
14442
|
if (args.clientId) flags |= CLOB_MODIFY_FLAG_HAS_CLIENT_ID;
|
|
11141
14443
|
if (args.orderId !== void 0) flags |= CLOB_MODIFY_FLAG_HAS_ORDER_ID;
|
|
11142
14444
|
const metadata = buildModifyOrderMetadata(args);
|
|
11143
|
-
|
|
14445
|
+
assertU642(args.quantity, "quantity");
|
|
11144
14446
|
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);
|
|
11145
14447
|
if (metadata.length) builder.metadata().write(metadata).finish();
|
|
11146
14448
|
return buildClobInstruction("modify_order_entry", builder.build());
|
|
@@ -11396,14 +14698,14 @@ function buildModifyOrderMetadata(args) {
|
|
|
11396
14698
|
offset += CLOB_CLIENT_ID_SIZE;
|
|
11397
14699
|
}
|
|
11398
14700
|
if (args.orderId !== void 0) {
|
|
11399
|
-
|
|
14701
|
+
assertU642(args.orderId, "orderId");
|
|
11400
14702
|
new DataView(metadata.buffer).setBigUint64(offset, args.orderId, true);
|
|
11401
14703
|
}
|
|
11402
14704
|
return metadata;
|
|
11403
14705
|
}
|
|
11404
14706
|
function createTokenTransferInstruction(variant, args) {
|
|
11405
14707
|
return async (context) => {
|
|
11406
|
-
|
|
14708
|
+
assertU642(args.amount, "amount");
|
|
11407
14709
|
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();
|
|
11408
14710
|
return buildClobInstruction(variant, payload);
|
|
11409
14711
|
};
|
|
@@ -11580,7 +14882,13 @@ function assertU16(value, label) {
|
|
|
11580
14882
|
}
|
|
11581
14883
|
return value;
|
|
11582
14884
|
}
|
|
11583
|
-
function
|
|
14885
|
+
function assertClobActiveSeatIndex(value, label = "seatIndex") {
|
|
14886
|
+
if (!Number.isSafeInteger(value) || value < 2 || value >= CLOB_NULL_INDEX) {
|
|
14887
|
+
throw new Error(`${label} must be an integer between 2 and ${CLOB_NULL_INDEX - 1}`);
|
|
14888
|
+
}
|
|
14889
|
+
return value;
|
|
14890
|
+
}
|
|
14891
|
+
function assertU642(value, label) {
|
|
11584
14892
|
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
11585
14893
|
throw new Error(`${label} must be between 0 and 18446744073709551615`);
|
|
11586
14894
|
}
|
|
@@ -11682,6 +14990,7 @@ exports.CLOB_INSTRUCTION_SEAT_CREATE = CLOB_INSTRUCTION_SEAT_CREATE;
|
|
|
11682
14990
|
exports.CLOB_INSTRUCTION_TOKEN_DEPOSIT = CLOB_INSTRUCTION_TOKEN_DEPOSIT;
|
|
11683
14991
|
exports.CLOB_INSTRUCTION_TOKEN_WITHDRAW = CLOB_INSTRUCTION_TOKEN_WITHDRAW;
|
|
11684
14992
|
exports.CLOB_MARKET_ACCOUNT_SIZE = CLOB_MARKET_ACCOUNT_SIZE;
|
|
14993
|
+
exports.CLOB_MAX_ATOMIC_FUNDING_SOURCES = CLOB_MAX_ATOMIC_FUNDING_SOURCES;
|
|
11685
14994
|
exports.CLOB_MODIFY_FLAG_FAIL_IF_OUT_OF_RANGE = CLOB_MODIFY_FLAG_FAIL_IF_OUT_OF_RANGE;
|
|
11686
14995
|
exports.CLOB_MODIFY_FLAG_HAS_CLIENT_ID = CLOB_MODIFY_FLAG_HAS_CLIENT_ID;
|
|
11687
14996
|
exports.CLOB_MODIFY_FLAG_HAS_ORDER_ID = CLOB_MODIFY_FLAG_HAS_ORDER_ID;
|
|
@@ -11709,6 +15018,7 @@ exports.ClientId = ClientId;
|
|
|
11709
15018
|
exports.ClobError = ClobError;
|
|
11710
15019
|
exports.ClobErrorBuilder = ClobErrorBuilder;
|
|
11711
15020
|
exports.ClobEventBuilder = ClobEventBuilder;
|
|
15021
|
+
exports.ClobExchangeError = ClobExchangeError;
|
|
11712
15022
|
exports.ClobInstructionBuilder = ClobInstructionBuilder;
|
|
11713
15023
|
exports.ClobProgramAccount = ClobProgramAccount;
|
|
11714
15024
|
exports.ClobProgramAccountBuilder = ClobProgramAccountBuilder;
|
|
@@ -11748,7 +15058,14 @@ exports.TokenBalanceEvent = TokenBalanceEvent;
|
|
|
11748
15058
|
exports.TokenBalanceEventBuilder = TokenBalanceEventBuilder;
|
|
11749
15059
|
exports.TokenTransferInstruction = TokenTransferInstruction;
|
|
11750
15060
|
exports.TokenTransferInstructionBuilder = TokenTransferInstructionBuilder;
|
|
15061
|
+
exports.aggregateClobExchangeBalances = aggregateClobExchangeBalances;
|
|
15062
|
+
exports.assertClobActiveSeatIndex = assertClobActiveSeatIndex;
|
|
15063
|
+
exports.buildClobExchangeDepositInstructionPlan = buildClobExchangeDepositInstructionPlan;
|
|
15064
|
+
exports.buildClobExchangeWithdrawalInstructionPlan = buildClobExchangeWithdrawalInstructionPlan;
|
|
15065
|
+
exports.buildClobFundedOrderInstructionPlan = buildClobFundedOrderInstructionPlan;
|
|
11751
15066
|
exports.buildOrderBookSnapshot = buildOrderBookSnapshot;
|
|
15067
|
+
exports.calculateClobOrderFundingAmount = calculateClobOrderFundingAmount;
|
|
15068
|
+
exports.concatClobInstructions = concatClobInstructions;
|
|
11752
15069
|
exports.createMarketCreateInstruction = createMarketCreateInstruction;
|
|
11753
15070
|
exports.createMarketRecordInstruction = createMarketRecordInstruction;
|
|
11754
15071
|
exports.createMarketSetStatusInstruction = createMarketSetStatusInstruction;
|
|
@@ -11765,5 +15082,8 @@ exports.parseClobProgramAccount = parseClobProgramAccount;
|
|
|
11765
15082
|
exports.parseMarketAccount = parseMarketAccount;
|
|
11766
15083
|
exports.parseOrderArenaAccount = parseOrderArenaAccount;
|
|
11767
15084
|
exports.parseSeatArenaAccount = parseSeatArenaAccount;
|
|
15085
|
+
exports.planClobOrderFunding = planClobOrderFunding;
|
|
15086
|
+
exports.validateClobOrderFundingPlanSeats = validateClobOrderFundingPlanSeats;
|
|
15087
|
+
exports.wrapClobPlanWithTokenAccountInitialization = wrapClobPlanWithTokenAccountInitialization;
|
|
11768
15088
|
//# sourceMappingURL=index.cjs.map
|
|
11769
15089
|
//# sourceMappingURL=index.cjs.map
|