@perpbrain/sdk 0.1.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/calibration.d.ts +31 -0
- package/dist/cjs/calibration.js +180 -0
- package/dist/cjs/clientMeta.d.ts +12 -0
- package/dist/cjs/clientMeta.js +83 -0
- package/dist/cjs/fillLink.d.ts +23 -3
- package/dist/cjs/fillLink.js +568 -12
- package/dist/cjs/fixedDecimal.d.ts +11 -0
- package/dist/cjs/fixedDecimal.js +39 -0
- package/dist/cjs/fundingRecon.js +33 -0
- package/dist/cjs/holding.d.ts +48 -0
- package/dist/cjs/holding.js +263 -0
- package/dist/cjs/index.d.ts +8 -2
- package/dist/cjs/index.js +35 -2
- package/dist/cjs/receipt.d.ts +49 -2
- package/dist/cjs/receipt.js +152 -11
- package/dist/esm/calibration.d.ts +31 -0
- package/dist/esm/calibration.js +174 -0
- package/dist/esm/clientMeta.d.ts +12 -0
- package/dist/esm/clientMeta.js +77 -0
- package/dist/esm/fillLink.d.ts +23 -3
- package/dist/esm/fillLink.js +564 -11
- package/dist/esm/fixedDecimal.d.ts +11 -0
- package/dist/esm/fixedDecimal.js +32 -0
- package/dist/esm/fundingRecon.js +33 -0
- package/dist/esm/holding.d.ts +48 -0
- package/dist/esm/holding.js +257 -0
- package/dist/esm/index.d.ts +8 -2
- package/dist/esm/index.js +5 -2
- package/dist/esm/receipt.d.ts +49 -2
- package/dist/esm/receipt.js +146 -11
- package/package.json +1 -1
package/dist/cjs/fillLink.js
CHANGED
|
@@ -8,11 +8,14 @@
|
|
|
8
8
|
* Read-only: no keys, no order path, no network in this module.
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.LINK_VERSION = exports.LINK_KIND = void 0;
|
|
11
|
+
exports.LINK_VERSION_V2 = exports.LINK_VERSION = exports.LINK_KIND = void 0;
|
|
12
12
|
exports.simFromReceipt = simFromReceipt;
|
|
13
13
|
exports.normaliseSelector = normaliseSelector;
|
|
14
14
|
exports.selectFills = selectFills;
|
|
15
15
|
exports.linkInputs = linkInputs;
|
|
16
|
+
exports.checkV2Value = checkV2Value;
|
|
17
|
+
exports.checkV2Text = checkV2Text;
|
|
18
|
+
exports.receiptBounds = receiptBounds;
|
|
16
19
|
exports.computeLink = computeLink;
|
|
17
20
|
exports.buildLink = buildLink;
|
|
18
21
|
exports.verifyLink = verifyLink;
|
|
@@ -21,7 +24,10 @@ const receipt_ts_1 = require("./receipt.js");
|
|
|
21
24
|
const fees_ts_1 = require("./vendor/fees.js");
|
|
22
25
|
exports.LINK_KIND = "perpbrain.execution-link";
|
|
23
26
|
exports.LINK_VERSION = 1;
|
|
27
|
+
exports.LINK_VERSION_V2 = 2;
|
|
24
28
|
const STATEMENT = "verified and reproducible";
|
|
29
|
+
const STATEMENT_V2 = "integrity verified and reproducible; the fill is associated with the receipt by selector, " +
|
|
30
|
+
"coin and side only; see correspondence for the declared-policy checks";
|
|
25
31
|
const SCALE = 10n ** 18n;
|
|
26
32
|
const HASH = /^0x[0-9a-f]{64}$/;
|
|
27
33
|
const ZERO_HASH = `0x${"0".repeat(64)}`;
|
|
@@ -163,7 +169,7 @@ function linkInputs(receipt, rows, options) {
|
|
|
163
169
|
if (!ADDRESS.test(account)) {
|
|
164
170
|
throw new Error("user must be a 0x-prefixed 20-byte address");
|
|
165
171
|
}
|
|
166
|
-
|
|
172
|
+
const out = {
|
|
167
173
|
network: options.network,
|
|
168
174
|
user: account,
|
|
169
175
|
by: options.by,
|
|
@@ -172,8 +178,15 @@ function linkInputs(receipt, rows, options) {
|
|
|
172
178
|
sim: simFromReceipt(receipt, options.tradeIndex ?? 0),
|
|
173
179
|
fills: selectFills(rows, options.by, options.value),
|
|
174
180
|
};
|
|
181
|
+
if (options.policy !== undefined) {
|
|
182
|
+
v2Contract(receipt);
|
|
183
|
+
v2Contract(options.policy);
|
|
184
|
+
out.policy = JSON.parse((0, receipt_ts_1.canonical)(options.policy));
|
|
185
|
+
out.bounds = receiptBounds(receipt);
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
175
188
|
}
|
|
176
|
-
function
|
|
189
|
+
function computeBase(inputs) {
|
|
177
190
|
const sim = inputs.sim;
|
|
178
191
|
const by = inputs.by;
|
|
179
192
|
const value = normaliseSelector(by, inputs.value);
|
|
@@ -247,14 +260,539 @@ function computeLink(inputs) {
|
|
|
247
260
|
time_to_first_fill_ms: first === null ? null : first - sim.decision_time_ms,
|
|
248
261
|
};
|
|
249
262
|
}
|
|
250
|
-
|
|
263
|
+
// --- correspondence (v2): declared-policy checks, never a proof of causation -------------------
|
|
264
|
+
// Port of perpbrain_read.sim.fill_link v2. Shared vectors: fixtures/fill_link/v2-vectors.json.
|
|
265
|
+
const POLICY_KEYS = new Set([
|
|
266
|
+
"sim_provenance",
|
|
267
|
+
"precommit_ref",
|
|
268
|
+
"clock_source",
|
|
269
|
+
"time_unit",
|
|
270
|
+
"clock_uncertainty_ms",
|
|
271
|
+
"window_after_ms",
|
|
272
|
+
"size_mode",
|
|
273
|
+
"size_rel_tol",
|
|
274
|
+
"price_tol_bps",
|
|
275
|
+
"bar_time_convention",
|
|
276
|
+
]);
|
|
277
|
+
const PROVENANCE = ["derived_from_fill", "independent_precommit", "unknown"];
|
|
278
|
+
const CAVEATS = [
|
|
279
|
+
"association is by selector, coin and side; it is not proof the fill executed this decision",
|
|
280
|
+
"decision time is bound to the receipt only when the record is built and verified with the " +
|
|
281
|
+
"receipt; otherwise it is an unverified input and no positive verdict is given",
|
|
282
|
+
"sim_provenance, precommit_ref and the whole policy are declared by the caller, possibly " +
|
|
283
|
+
"after the fact; this module never verifies a precommit reference, so provenance is always " +
|
|
284
|
+
"declared and unverified",
|
|
285
|
+
"CONSISTENT_WITH_DECLARED_POLICY means only that the declared numeric checks passed; " +
|
|
286
|
+
"tolerances are the caller's, not justified here, and it does not make metrics comparable",
|
|
287
|
+
];
|
|
288
|
+
const MS_LOW = 10 ** 12;
|
|
289
|
+
const MS_HIGH = 10 ** 13;
|
|
290
|
+
function stripWs(value) {
|
|
291
|
+
return value.replace(/^[ \t\n\r]+|[ \t\n\r]+$/g, "");
|
|
292
|
+
}
|
|
293
|
+
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
|
|
294
|
+
/** v2 inputs are canonical in both SDKs only when every number is a JSON integer within the
|
|
295
|
+
* double-safe range and every object key is ASCII. Anything else is refused, never coerced. */
|
|
296
|
+
function checkV2Value(value) {
|
|
297
|
+
v2Contract(value);
|
|
298
|
+
}
|
|
299
|
+
const LONE_SURROGATE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;
|
|
300
|
+
function v2Contract(value) {
|
|
301
|
+
if (value === null || typeof value === "boolean") {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (typeof value === "string") {
|
|
305
|
+
if (LONE_SURROGATE.test(value)) {
|
|
306
|
+
throw new Error("v2 inputs must not contain unpaired surrogates");
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (typeof value === "number") {
|
|
311
|
+
if (!Number.isInteger(value)) {
|
|
312
|
+
throw new Error("v2 inputs must not contain non-integer or float-formatted numbers");
|
|
313
|
+
}
|
|
314
|
+
if (Math.abs(value) > MAX_SAFE) {
|
|
315
|
+
throw new Error("v2 inputs must keep integers within the double-safe range");
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (Array.isArray(value)) {
|
|
320
|
+
value.forEach(v2Contract);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (typeof value === "object") {
|
|
324
|
+
for (const [k, v] of Object.entries(value)) {
|
|
325
|
+
if (!/^[\x00-\x7f]*$/.test(k)) {
|
|
326
|
+
throw new Error("v2 inputs must use ASCII object keys");
|
|
327
|
+
}
|
|
328
|
+
v2Contract(v);
|
|
329
|
+
}
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
throw new Error("v2 inputs must be plain JSON");
|
|
333
|
+
}
|
|
334
|
+
/** Float spellings (1.0, 1e3) parse differently across languages: refused in the raw bytes. */
|
|
335
|
+
function checkV2Text(raw) {
|
|
336
|
+
const stripped = raw.replace(/"(?:\\.|[^"\\])*"/g, '""');
|
|
337
|
+
for (const token of stripped.match(/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g) ?? []) {
|
|
338
|
+
if (/[.eE]/.test(token)) {
|
|
339
|
+
throw new Error("v2 inputs must not contain float-formatted numbers");
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
function isInt(value) {
|
|
344
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
345
|
+
}
|
|
346
|
+
function tryFixed(value) {
|
|
347
|
+
try {
|
|
348
|
+
return fixed(value);
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function policyInt(policy, key) {
|
|
355
|
+
const v = policy[key];
|
|
356
|
+
return isInt(v) && v >= 0 ? v : null;
|
|
357
|
+
}
|
|
358
|
+
function policyDec(policy, key) {
|
|
359
|
+
const out = tryFixed(policy[key]);
|
|
360
|
+
return out !== null && out >= 0n ? out : null;
|
|
361
|
+
}
|
|
362
|
+
function uniq(items) {
|
|
363
|
+
return [...new Set(items)];
|
|
364
|
+
}
|
|
365
|
+
function feeUnassessed(r) {
|
|
366
|
+
const token = r.feeToken;
|
|
367
|
+
return (token !== undefined && token !== null && token !== "USDC") || r.fee === undefined || r.fee === null;
|
|
368
|
+
}
|
|
369
|
+
/** Structural facts taken from the receipt: the smallest positive bar gap and where the decision
|
|
370
|
+
* time came from ("bar", "book", or "unknown" when timed bars exist but the decision bar is not). */
|
|
371
|
+
function receiptBounds(receipt) {
|
|
372
|
+
const inputs = obj(obj(receipt)?.inputs);
|
|
373
|
+
const rawBars = inputs?.bars;
|
|
374
|
+
const bars = Array.isArray(rawBars) ? rawBars : [];
|
|
375
|
+
const times = [];
|
|
376
|
+
for (const b of bars) {
|
|
377
|
+
const t = obj(b)?.time_ms;
|
|
378
|
+
if (isInt(t)) {
|
|
379
|
+
times.push(t);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const gaps = [];
|
|
383
|
+
for (let n = 1; n < times.length; n++) {
|
|
384
|
+
if (times[n] > times[n - 1]) {
|
|
385
|
+
gaps.push(times[n] - times[n - 1]);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const index = obj(inputs?.decision)?.bar_index;
|
|
389
|
+
const bar = isInt(index) && index >= 0 && index < bars.length ? bars[index] : null;
|
|
390
|
+
const barTimed = isInt(obj(bar)?.time_ms);
|
|
391
|
+
return {
|
|
392
|
+
bar_interval_ms: gaps.length > 0 ? Math.min(...gaps) : null,
|
|
393
|
+
time_field: barTimed ? "bar" : times.length > 0 ? "unknown" : "book",
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function availability(inputs, policy, na) {
|
|
397
|
+
const bounds = obj(inputs.bounds);
|
|
398
|
+
const field = bounds?.time_field;
|
|
399
|
+
const interval = bounds?.bar_interval_ms;
|
|
400
|
+
const decision = inputs.sim.decision_time_ms;
|
|
401
|
+
if (!isInt(decision)) {
|
|
402
|
+
return null;
|
|
403
|
+
}
|
|
404
|
+
if (field === "book") {
|
|
405
|
+
return decision;
|
|
406
|
+
}
|
|
407
|
+
if (field !== "bar") {
|
|
408
|
+
na.push("DECISION_TIME_FIELD_UNKNOWN");
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const conv = policy.bar_time_convention;
|
|
412
|
+
if (conv === "close") {
|
|
413
|
+
return decision;
|
|
414
|
+
}
|
|
415
|
+
if (conv === "open") {
|
|
416
|
+
if (isInt(interval) && interval > 0) {
|
|
417
|
+
return decision + interval;
|
|
418
|
+
}
|
|
419
|
+
na.push("BAR_INTERVAL_UNKNOWN_FOR_OPEN_CONVENTION");
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
na.push("BAR_TIME_CONVENTION_MISSING");
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
function partial(inputs, bound) {
|
|
426
|
+
const rawPolicy = obj(inputs.policy);
|
|
427
|
+
const policy = rawPolicy ?? {};
|
|
428
|
+
const na = ["INPUT_MALFORMED"];
|
|
429
|
+
const bad = [];
|
|
430
|
+
if (!bound) {
|
|
431
|
+
na.push("DECISION_TIME_NOT_BOUND_TO_RECEIPT");
|
|
432
|
+
}
|
|
433
|
+
const avail = availability(inputs, policy, na);
|
|
434
|
+
const unc = policyInt(policy, "clock_uncertainty_ms");
|
|
435
|
+
const after = policyInt(policy, "window_after_ms");
|
|
436
|
+
const tol = policyDec(policy, "size_rel_tol");
|
|
437
|
+
const rows = Array.isArray(inputs.fills) ? inputs.fills : [];
|
|
438
|
+
const sim = obj(inputs.sim) ?? {};
|
|
439
|
+
const sideHl = sim.side === "buy" ? "B" : "A";
|
|
440
|
+
let sizes = 0n;
|
|
441
|
+
const tids = [];
|
|
442
|
+
rows.forEach((raw, n) => {
|
|
443
|
+
const r = obj(raw);
|
|
444
|
+
if (r === null) {
|
|
445
|
+
na.push(`ROW_${n}_NOT_AN_OBJECT`);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const t = r.time;
|
|
449
|
+
const tOk = isInt(t) && t >= MS_LOW && t < MS_HIGH;
|
|
450
|
+
if (!tOk) {
|
|
451
|
+
na.push(`ROW_${n}_MALFORMED:time`);
|
|
452
|
+
}
|
|
453
|
+
else if (unc !== null) {
|
|
454
|
+
const floor = avail !== null ? avail : sim.decision_time_ms;
|
|
455
|
+
if (isInt(floor) && t < floor - unc) {
|
|
456
|
+
bad.push("FILL_BEFORE_DECISION");
|
|
457
|
+
}
|
|
458
|
+
else if (avail !== null && after !== null && t > avail + after) {
|
|
459
|
+
bad.push("FILL_AFTER_WINDOW");
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
for (const key of ["sz", "px"]) {
|
|
463
|
+
const v = tryFixed(r[key]);
|
|
464
|
+
if (v === null || v <= 0n) {
|
|
465
|
+
na.push(`ROW_${n}_MALFORMED:${key}`);
|
|
466
|
+
}
|
|
467
|
+
else if (key === "sz") {
|
|
468
|
+
sizes += v;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (r.feeToken === undefined || r.feeToken === null || r.feeToken === "USDC") {
|
|
472
|
+
if (tryFixed(r.fee) === null) {
|
|
473
|
+
na.push(`ROW_${n}_MALFORMED:fee`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
const tid = r.tid;
|
|
477
|
+
if (!isInt(tid)) {
|
|
478
|
+
na.push(`ROW_${n}_MALFORMED:tid`);
|
|
479
|
+
}
|
|
480
|
+
else if (tids.includes(tid)) {
|
|
481
|
+
bad.push("DUPLICATE_OR_MISSING_FILL_ID");
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
tids.push(tid);
|
|
485
|
+
}
|
|
486
|
+
if (typeof r.coin === "string" && r.coin !== sim.market) {
|
|
487
|
+
bad.push("BASE_MISMATCHED_TRADE");
|
|
488
|
+
}
|
|
489
|
+
if (typeof r.side === "string" && r.side !== sideHl) {
|
|
490
|
+
bad.push("BASE_MISMATCHED_TRADE");
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
const simSz = tryFixed(sim.size) ?? 0n;
|
|
494
|
+
if (simSz > 0n && tol !== null && tol < SCALE && sizes > (simSz * (SCALE + tol)) / SCALE) {
|
|
495
|
+
bad.push("SIZE_OUTSIDE_TOLERANCE");
|
|
496
|
+
}
|
|
497
|
+
return {
|
|
498
|
+
verdict: bad.length > 0 ? "INCONSISTENT" : "NOT_ASSESSED",
|
|
499
|
+
inconsistent_reasons: uniq(bad),
|
|
500
|
+
not_assessed_reasons: uniq(na),
|
|
501
|
+
checks_run: ["partial_row_checks"],
|
|
502
|
+
checks_not_run: ["full_correspondence", "underfill_size", "price", "fees"],
|
|
503
|
+
decision_time_source: bound ? "receipt_verified" : "inputs_unbound",
|
|
504
|
+
provenance_status: "DECLARED_UNVERIFIED",
|
|
505
|
+
malformed_reason: "MALFORMED_INPUT",
|
|
506
|
+
caveats: CAVEATS,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
function correspondence(inputs, base, bound) {
|
|
510
|
+
const rawPolicy = obj(inputs.policy);
|
|
511
|
+
const policy = rawPolicy ?? {};
|
|
512
|
+
const na = [];
|
|
513
|
+
const bad = [];
|
|
514
|
+
const ran = [];
|
|
515
|
+
const skipped = [];
|
|
516
|
+
if (rawPolicy === null) {
|
|
517
|
+
na.push("POLICY_MISSING");
|
|
518
|
+
}
|
|
519
|
+
if (!bound) {
|
|
520
|
+
na.push("DECISION_TIME_NOT_BOUND_TO_RECEIPT");
|
|
521
|
+
}
|
|
522
|
+
const bounds = obj(inputs.bounds);
|
|
523
|
+
const interval = bounds?.bar_interval_ms;
|
|
524
|
+
if (bounds === null) {
|
|
525
|
+
na.push("BOUNDS_MISSING");
|
|
526
|
+
}
|
|
527
|
+
for (const k of Object.keys(policy).sort()) {
|
|
528
|
+
if (!POLICY_KEYS.has(k)) {
|
|
529
|
+
na.push(`POLICY_UNKNOWN_KEY:${k}`);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const prov = policy.sim_provenance;
|
|
533
|
+
if (typeof prov !== "string" || !PROVENANCE.includes(prov)) {
|
|
534
|
+
na.push("SIM_PROVENANCE_MISSING");
|
|
535
|
+
}
|
|
536
|
+
else if (prov === "derived_from_fill") {
|
|
537
|
+
na.push("SIM_DERIVED_FROM_FILL");
|
|
538
|
+
}
|
|
539
|
+
else if (prov === "unknown") {
|
|
540
|
+
na.push("SIM_PROVENANCE_UNKNOWN");
|
|
541
|
+
}
|
|
542
|
+
else if (!(typeof policy.precommit_ref === "string" && stripWs(policy.precommit_ref) !== "")) {
|
|
543
|
+
na.push("PRECOMMIT_REF_MISSING");
|
|
544
|
+
}
|
|
545
|
+
const clock = policy.clock_source;
|
|
546
|
+
if (!(typeof clock === "string" && stripWs(clock) !== "")) {
|
|
547
|
+
na.push("CLOCK_SOURCE_MISSING");
|
|
548
|
+
}
|
|
549
|
+
if (policy.time_unit !== "ms_utc") {
|
|
550
|
+
na.push("TIME_UNIT_NOT_MS_UTC");
|
|
551
|
+
}
|
|
552
|
+
const unc = policyInt(policy, "clock_uncertainty_ms");
|
|
553
|
+
const after = policyInt(policy, "window_after_ms");
|
|
554
|
+
if (unc === null) {
|
|
555
|
+
na.push("CLOCK_UNCERTAINTY_MISSING");
|
|
556
|
+
}
|
|
557
|
+
if (after === null) {
|
|
558
|
+
na.push("WINDOW_AFTER_MISSING");
|
|
559
|
+
}
|
|
560
|
+
else if (unc !== null && unc >= after) {
|
|
561
|
+
na.push("POLICY_NOT_DISCRIMINATING:uncertainty_not_below_window");
|
|
562
|
+
}
|
|
563
|
+
if (after !== null && !(isInt(interval) && interval > 0)) {
|
|
564
|
+
na.push("WINDOW_NOT_BOUNDED_BY_RECEIPT");
|
|
565
|
+
}
|
|
566
|
+
else if (after !== null && isInt(interval) && after > interval) {
|
|
567
|
+
na.push("POLICY_NOT_DISCRIMINATING:window_longer_than_receipt_bar_interval");
|
|
568
|
+
}
|
|
569
|
+
const mode = policy.size_mode;
|
|
570
|
+
if (mode === "scale_in") {
|
|
571
|
+
na.push("SIZE_MODE_UNSUPPORTED:scale_in");
|
|
572
|
+
}
|
|
573
|
+
else if (mode !== "single" && mode !== "aggregate") {
|
|
574
|
+
na.push("SIZE_MODE_MISSING");
|
|
575
|
+
}
|
|
576
|
+
const tol = policyDec(policy, "size_rel_tol");
|
|
577
|
+
if (tol === null) {
|
|
578
|
+
na.push("SIZE_TOLERANCE_MISSING");
|
|
579
|
+
}
|
|
580
|
+
else if (tol >= SCALE) {
|
|
581
|
+
na.push("POLICY_NOT_DISCRIMINATING:size_tolerance_at_least_100pct");
|
|
582
|
+
}
|
|
583
|
+
let priceTol = null;
|
|
584
|
+
if ("price_tol_bps" in policy) {
|
|
585
|
+
priceTol = policyDec(policy, "price_tol_bps");
|
|
586
|
+
if (priceTol === null) {
|
|
587
|
+
na.push("PRICE_TOLERANCE_INVALID");
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const rows = inputs.fills;
|
|
591
|
+
const sim = inputs.sim;
|
|
592
|
+
if (base.status === "NO_FILLS") {
|
|
593
|
+
na.push("NO_FILLS");
|
|
594
|
+
}
|
|
595
|
+
else if (base.status !== "LINKED") {
|
|
596
|
+
bad.push(`BASE_${base.status}`);
|
|
597
|
+
}
|
|
598
|
+
const tids = rows.map((r) => r.tid);
|
|
599
|
+
if (tids.some((t) => !isInt(t))) {
|
|
600
|
+
bad.push("FILL_ID_INVALID");
|
|
601
|
+
}
|
|
602
|
+
else if (new Set(tids).size !== tids.length) {
|
|
603
|
+
bad.push("DUPLICATE_OR_MISSING_FILL_ID");
|
|
604
|
+
}
|
|
605
|
+
if (rows.some(feeUnassessed)) {
|
|
606
|
+
na.push("FEE_UNASSESSED");
|
|
607
|
+
}
|
|
608
|
+
for (const r of rows) {
|
|
609
|
+
const px = tryFixed(r.px);
|
|
610
|
+
if (px === null || px <= 0n) {
|
|
611
|
+
na.push("FILL_PRICE_NOT_POSITIVE");
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const avail = availability(inputs, policy, na);
|
|
616
|
+
const decision = avail !== null ? avail : sim.decision_time_ms;
|
|
617
|
+
const times = rows.map((r) => r.time);
|
|
618
|
+
const stamps = [decision, ...times];
|
|
619
|
+
let ordering = "NOT_ASSESSED";
|
|
620
|
+
let timeOk = true;
|
|
621
|
+
if (stamps.some((t) => !isInt(t))) {
|
|
622
|
+
na.push("TIMESTAMP_MISSING");
|
|
623
|
+
timeOk = false;
|
|
624
|
+
}
|
|
625
|
+
else if (stamps.some((t) => t < MS_LOW || t >= MS_HIGH)) {
|
|
626
|
+
na.push("TIMESTAMP_UNIT_IMPLAUSIBLE");
|
|
627
|
+
timeOk = false;
|
|
628
|
+
}
|
|
629
|
+
if (timeOk && unc !== null) {
|
|
630
|
+
const ts = times;
|
|
631
|
+
const d = decision;
|
|
632
|
+
if (ts.some((t) => t < d - unc)) {
|
|
633
|
+
bad.push("FILL_BEFORE_DECISION");
|
|
634
|
+
ordering = "BEFORE";
|
|
635
|
+
}
|
|
636
|
+
else if (ts.some((t) => t < d + unc)) {
|
|
637
|
+
na.push("ORDERING_AMBIGUOUS_WITHIN_CLOCK_UNCERTAINTY");
|
|
638
|
+
ordering = "AMBIGUOUS_WITHIN_UNCERTAINTY";
|
|
639
|
+
}
|
|
640
|
+
else if (avail === null) {
|
|
641
|
+
ordering = "NOT_ASSESSED";
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
ordering = "AFTER";
|
|
645
|
+
}
|
|
646
|
+
ran.push("ordering");
|
|
647
|
+
if (avail !== null && after !== null && ts.some((t) => t > d + after)) {
|
|
648
|
+
bad.push("FILL_AFTER_WINDOW");
|
|
649
|
+
}
|
|
650
|
+
if (after !== null && avail !== null) {
|
|
651
|
+
ran.push("window_after");
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
skipped.push("ordering");
|
|
656
|
+
skipped.push("window_after");
|
|
657
|
+
}
|
|
658
|
+
const simSz = fixed(sim.size);
|
|
659
|
+
if (fixed(sim.price) <= 0n) {
|
|
660
|
+
na.push("SIM_PRICE_NOT_POSITIVE");
|
|
661
|
+
}
|
|
662
|
+
let sizesOk = simSz > 0n && rows.length > 0;
|
|
663
|
+
if (simSz <= 0n) {
|
|
664
|
+
na.push("SIM_SIZE_NOT_POSITIVE");
|
|
665
|
+
}
|
|
666
|
+
for (const r of rows) {
|
|
667
|
+
const sz = tryFixed(r.sz);
|
|
668
|
+
if (sz === null || sz <= 0n) {
|
|
669
|
+
na.push("FILL_SIZE_MISSING_OR_NOT_POSITIVE");
|
|
670
|
+
sizesOk = false;
|
|
671
|
+
break;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (sizesOk && tol !== null && (mode === "single" || mode === "aggregate")) {
|
|
675
|
+
const ratio = tdiv(fixed(base.size) * SCALE, simSz);
|
|
676
|
+
if (mode === "single" && rows.length !== 1) {
|
|
677
|
+
bad.push("MULTIPLE_FILLS_UNDER_SINGLE_MODE");
|
|
678
|
+
}
|
|
679
|
+
const dev = ratio - SCALE;
|
|
680
|
+
if ((dev < 0n ? -dev : dev) > tol) {
|
|
681
|
+
bad.push("SIZE_OUTSIDE_TOLERANCE");
|
|
682
|
+
}
|
|
683
|
+
ran.push("size");
|
|
684
|
+
}
|
|
685
|
+
else {
|
|
686
|
+
skipped.push("size");
|
|
687
|
+
}
|
|
688
|
+
if (priceTol !== null && rows.length > 0 && base.status === "LINKED" && fixed(sim.price) > 0n) {
|
|
689
|
+
const simPx = fixed(sim.price);
|
|
690
|
+
const diff = fixed(base.avg_px) - simPx;
|
|
691
|
+
const dev = tdiv((diff < 0n ? -diff : diff) * 10000n * SCALE, simPx);
|
|
692
|
+
if (dev > priceTol) {
|
|
693
|
+
bad.push("PRICE_OUTSIDE_TOLERANCE");
|
|
694
|
+
}
|
|
695
|
+
ran.push("price");
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
skipped.push("price");
|
|
699
|
+
}
|
|
700
|
+
const verdict = bad.length > 0 ? "INCONSISTENT" : na.length > 0 ? "NOT_ASSESSED" : "CONSISTENT_WITH_DECLARED_POLICY";
|
|
701
|
+
return {
|
|
702
|
+
verdict,
|
|
703
|
+
inconsistent_reasons: uniq(bad),
|
|
704
|
+
not_assessed_reasons: uniq(na),
|
|
705
|
+
ordering,
|
|
706
|
+
checks_run: ran,
|
|
707
|
+
checks_not_run: skipped,
|
|
708
|
+
decision_time_ms: sim.decision_time_ms,
|
|
709
|
+
decision_available_ms: avail,
|
|
710
|
+
decision_time_source: bound ? "receipt_verified" : "inputs_unbound",
|
|
711
|
+
provenance_status: "DECLARED_UNVERIFIED",
|
|
712
|
+
receipt_bar_interval_ms: isInt(interval) ? interval : null,
|
|
713
|
+
policy: Object.keys(policy).length > 0 ? JSON.parse((0, receipt_ts_1.canonical)(policy)) : null,
|
|
714
|
+
caveats: CAVEATS,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
/** v1 inputs (no `policy` key) give the v1 result unchanged and malformed rows throw, as in 0.2.1.
|
|
718
|
+
* v2 inputs add `correspondence`; malformed rows give a structured INVALID_INPUT result.
|
|
719
|
+
* `bound` is true only when the caller verified the inputs against the receipt. */
|
|
720
|
+
function computeLink(inputs, bound = false) {
|
|
721
|
+
if (!("policy" in inputs)) {
|
|
722
|
+
return computeBase(inputs);
|
|
723
|
+
}
|
|
724
|
+
let out;
|
|
725
|
+
let corr;
|
|
726
|
+
try {
|
|
727
|
+
const base = computeBase(inputs);
|
|
728
|
+
out = { ...base };
|
|
729
|
+
if (inputs.fills.some(feeUnassessed)) {
|
|
730
|
+
out.fee_drag_usd = null;
|
|
731
|
+
out.fees_usdc = null;
|
|
732
|
+
out.fee_note = "FEE_TOKEN_UNSUPPORTED_OR_MISSING";
|
|
733
|
+
}
|
|
734
|
+
corr = correspondence(inputs, base, bound);
|
|
735
|
+
}
|
|
736
|
+
catch {
|
|
737
|
+
return {
|
|
738
|
+
status: "INVALID_INPUT",
|
|
739
|
+
problems: ["malformed input"],
|
|
740
|
+
correspondence: partial(inputs, bound),
|
|
741
|
+
metrics_comparable: false,
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
out.correspondence = corr;
|
|
745
|
+
out.metrics_comparable = false;
|
|
746
|
+
out.comparability_note = "NEVER_TRUE_WITHOUT_INDEPENDENTLY_VALIDATED_PROVENANCE";
|
|
747
|
+
return out;
|
|
748
|
+
}
|
|
749
|
+
function receiptBinding(inputs, receipt) {
|
|
750
|
+
try {
|
|
751
|
+
v2Contract(receipt);
|
|
752
|
+
}
|
|
753
|
+
catch (error) {
|
|
754
|
+
return [`receipt: ${error.message}`];
|
|
755
|
+
}
|
|
756
|
+
if (sha256((0, receipt_ts_1.canonical)(receipt)) !== inputs.receipt_sha256) {
|
|
757
|
+
return ["receipt does not match receipt_sha256"];
|
|
758
|
+
}
|
|
759
|
+
let sim;
|
|
760
|
+
try {
|
|
761
|
+
sim = simFromReceipt(receipt, inputs.sim.trade_index);
|
|
762
|
+
}
|
|
763
|
+
catch (error) {
|
|
764
|
+
return [`receipt: ${error.message}`];
|
|
765
|
+
}
|
|
766
|
+
const problems = [];
|
|
767
|
+
if ((0, receipt_ts_1.canonical)(sim) !== (0, receipt_ts_1.canonical)(inputs.sim)) {
|
|
768
|
+
problems.push("sim trade differs from the receipt");
|
|
769
|
+
}
|
|
770
|
+
if ("policy" in inputs && (0, receipt_ts_1.canonical)(inputs.bounds ?? null) !== (0, receipt_ts_1.canonical)(receiptBounds(receipt))) {
|
|
771
|
+
problems.push("bounds differ from the receipt");
|
|
772
|
+
}
|
|
773
|
+
return problems;
|
|
774
|
+
}
|
|
775
|
+
/** Without `receipt` a v2 link is built unbound: it can never be positive. */
|
|
776
|
+
function buildLink(inputs, receipt) {
|
|
251
777
|
const raw = (0, receipt_ts_1.canonical)(inputs);
|
|
252
|
-
const
|
|
778
|
+
const v2 = "policy" in inputs;
|
|
779
|
+
if (v2) {
|
|
780
|
+
v2Contract(inputs);
|
|
781
|
+
}
|
|
782
|
+
let bound = false;
|
|
783
|
+
if (v2 && receipt !== undefined) {
|
|
784
|
+
const problems = receiptBinding(inputs, receipt);
|
|
785
|
+
if (problems.length > 0) {
|
|
786
|
+
throw new Error(problems.join("; "));
|
|
787
|
+
}
|
|
788
|
+
bound = true;
|
|
789
|
+
}
|
|
790
|
+
const result = computeLink(inputs, bound);
|
|
253
791
|
const record = {
|
|
254
792
|
kind: exports.LINK_KIND,
|
|
255
|
-
version: exports.LINK_VERSION,
|
|
793
|
+
version: v2 ? exports.LINK_VERSION_V2 : exports.LINK_VERSION,
|
|
256
794
|
label: inputs.network === "mainnet" ? "MAINNET" : "TESTNET",
|
|
257
|
-
statement: STATEMENT,
|
|
795
|
+
statement: v2 ? STATEMENT_V2 : STATEMENT,
|
|
258
796
|
receipt_sha256: inputs.receipt_sha256,
|
|
259
797
|
user: inputs.user,
|
|
260
798
|
selector: { by: inputs.by, value: inputs.value },
|
|
@@ -268,23 +806,41 @@ function buildLink(inputs) {
|
|
|
268
806
|
inputsBytes: Buffer.from(`${raw}\n`, "utf8"),
|
|
269
807
|
};
|
|
270
808
|
}
|
|
271
|
-
/** Re-hash the inputs, rerun the comparison and, given the receipt, check the binding.
|
|
809
|
+
/** Re-hash the inputs, rerun the comparison and, given the receipt, check the binding. A v2
|
|
810
|
+
* record that claims a receipt-bound decision time needs the receipt to verify. */
|
|
272
811
|
function verifyLink(record, inputsBytes, receipt) {
|
|
273
812
|
const problems = [];
|
|
274
813
|
if (record.kind !== exports.LINK_KIND) {
|
|
275
814
|
problems.push("kind");
|
|
276
815
|
}
|
|
277
|
-
if (record.version !== exports.LINK_VERSION) {
|
|
278
|
-
problems.push("version");
|
|
279
|
-
}
|
|
280
816
|
let inputs;
|
|
281
817
|
let result;
|
|
282
818
|
try {
|
|
283
819
|
inputs = JSON.parse(Buffer.from(inputsBytes).toString("utf8"));
|
|
820
|
+
const v2 = "policy" in inputs;
|
|
821
|
+
if (v2) {
|
|
822
|
+
v2Contract(inputs);
|
|
823
|
+
checkV2Text(Buffer.from(inputsBytes).toString("utf8"));
|
|
824
|
+
}
|
|
825
|
+
if (record.version !== (v2 ? exports.LINK_VERSION_V2 : exports.LINK_VERSION)) {
|
|
826
|
+
problems.push("version");
|
|
827
|
+
}
|
|
284
828
|
if (sha256((0, receipt_ts_1.canonical)(inputs)) !== record.inputs_sha256) {
|
|
285
829
|
problems.push("inputs_sha256");
|
|
286
830
|
}
|
|
287
|
-
|
|
831
|
+
const claimed = obj(record.result);
|
|
832
|
+
const corr = obj(claimed?.correspondence);
|
|
833
|
+
const claimsBound = corr?.decision_time_source === "receipt_verified";
|
|
834
|
+
if (claimsBound && v2) {
|
|
835
|
+
if (receipt === undefined) {
|
|
836
|
+
problems.push("receipt required: record claims receipt-bound decision time");
|
|
837
|
+
}
|
|
838
|
+
else {
|
|
839
|
+
problems.push(...receiptBinding(inputs, receipt));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const bound = claimsBound && v2 && receipt !== undefined && problems.length === 0;
|
|
843
|
+
result = computeLink(inputs, bound);
|
|
288
844
|
}
|
|
289
845
|
catch (error) {
|
|
290
846
|
return {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strict 18-decimal fixed-point helpers shared by the holding and calibration ports.
|
|
3
|
+
* Same grammar as the Python side: ASCII digits, optional leading minus, at most 18 fraction
|
|
4
|
+
* digits. No exponent, no spaces, no underscores, no NaN or Infinity. Division truncates
|
|
5
|
+
* toward zero, like Python `fill_link.tdiv`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SCALE: bigint;
|
|
8
|
+
export declare function isStrictDecimal(text: unknown): text is string;
|
|
9
|
+
export declare function parseStrictFixed(text: unknown, what: string): bigint;
|
|
10
|
+
export declare function formatFixed(value: bigint): string;
|
|
11
|
+
export declare function tdiv(numerator: bigint, denominator: bigint): bigint;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SCALE = void 0;
|
|
4
|
+
exports.isStrictDecimal = isStrictDecimal;
|
|
5
|
+
exports.parseStrictFixed = parseStrictFixed;
|
|
6
|
+
exports.formatFixed = formatFixed;
|
|
7
|
+
exports.tdiv = tdiv;
|
|
8
|
+
/**
|
|
9
|
+
* Strict 18-decimal fixed-point helpers shared by the holding and calibration ports.
|
|
10
|
+
* Same grammar as the Python side: ASCII digits, optional leading minus, at most 18 fraction
|
|
11
|
+
* digits. No exponent, no spaces, no underscores, no NaN or Infinity. Division truncates
|
|
12
|
+
* toward zero, like Python `fill_link.tdiv`.
|
|
13
|
+
*/
|
|
14
|
+
exports.SCALE = 10n ** 18n;
|
|
15
|
+
const STRICT = /^-?[0-9]+(\.[0-9]{1,18})?$/;
|
|
16
|
+
function isStrictDecimal(text) {
|
|
17
|
+
return typeof text === "string" && STRICT.test(text);
|
|
18
|
+
}
|
|
19
|
+
function parseStrictFixed(text, what) {
|
|
20
|
+
if (!isStrictDecimal(text))
|
|
21
|
+
throw new Error(`${what} must be a plain decimal string`);
|
|
22
|
+
const negative = text.startsWith("-");
|
|
23
|
+
const [whole, frac = ""] = (negative ? text.slice(1) : text).split(".");
|
|
24
|
+
const value = BigInt(whole) * exports.SCALE + BigInt((frac + "0".repeat(18)).slice(0, 18));
|
|
25
|
+
return negative ? -value : value;
|
|
26
|
+
}
|
|
27
|
+
function formatFixed(value) {
|
|
28
|
+
const negative = value < 0n;
|
|
29
|
+
const abs = negative ? -value : value;
|
|
30
|
+
const whole = abs / exports.SCALE;
|
|
31
|
+
const frac = (abs % exports.SCALE).toString().padStart(18, "0").replace(/0+$/, "");
|
|
32
|
+
const text = frac ? `${whole}.${frac}` : `${whole}`;
|
|
33
|
+
return negative && text !== "0" ? `-${text}` : text;
|
|
34
|
+
}
|
|
35
|
+
function tdiv(numerator, denominator) {
|
|
36
|
+
if (denominator === 0n)
|
|
37
|
+
throw new Error("division by zero");
|
|
38
|
+
return numerator / denominator; // BigInt division truncates toward zero
|
|
39
|
+
}
|