@perpbrain/sdk 0.1.2 → 0.1.4

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/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  /** Read-only and simulation-only SDK surface. */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.READ_AND_SIM_TOOLS = exports.simulate = exports.positions = exports.orderBook = exports.markets = exports.funding = exports.fees = exports.InfoClient = exports.executionCost = exports.writeReceiptDir = exports.verifyReceiptAt = exports.verifyReceipt = exports.snapshotRows = exports.simulateInputs = exports.sha256Json = exports.limitsPayload = exports.exampleInputs = exports.costBreakdownFromReceipt = exports.canonical = exports.buildReceiptFiles = exports.buildReceiptDir = exports.buildGuardedReceipt = exports.RECEIPT_VERSION = exports.RECEIPT_STATEMENT = exports.RECEIPT_KIND = exports.renderReceiptCard = exports.guardedSimulate = exports.wireCode = exports.survivalReport = exports.fromConfig = exports.defaultSnapshot = exports.defaultGuards = exports.GuardSession = exports.GuardLoosenError = exports.GuardConfigError = exports.GuardBlocked = exports.STALE_HALT_MS = exports.STALE_BLOCK_MS = exports.PRESETS = exports.HARD_MAX_SLIPPAGE_BPS = exports.HARD_MAX_ORDER_RATE = exports.HARD_MAX_OPEN_POSITIONS = exports.HARD_MAX_DAILY_LOSS_PCT = exports.CODES = void 0;
4
+ exports.InfoClient = exports.executionCost = exports.writeReceiptDir = exports.verifyReceiptAt = exports.verifyReceipt = exports.snapshotRows = exports.simulateInputs = exports.sha256Json = exports.limitsPayload = exports.exampleInputs = exports.costBreakdownFromReceipt = exports.canonical = exports.buildReceiptFiles = exports.buildReceiptDir = exports.buildGuardedReceipt = exports.RECEIPT_VERSION = exports.RECEIPT_STATEMENT = exports.RECEIPT_KIND = exports.verifyLink = exports.simFromReceipt = exports.selectFills = exports.normaliseSelector = exports.linkInputs = exports.computeLink = exports.buildLink = exports.LINK_VERSION = exports.LINK_KIND = exports.verifyReconReceipt = exports.reconcile = exports.RECON_VERSION = exports.RECON_KIND = exports.renderReceiptCard = exports.guardedSimulate = exports.wireCode = exports.survivalReport = exports.fromConfig = exports.defaultSnapshot = exports.defaultGuards = exports.GuardSession = exports.GuardLoosenError = exports.GuardConfigError = exports.GuardBlocked = exports.STALE_HALT_MS = exports.STALE_BLOCK_MS = exports.PRESETS = exports.HARD_MAX_SLIPPAGE_BPS = exports.HARD_MAX_ORDER_RATE = exports.HARD_MAX_OPEN_POSITIONS = exports.HARD_MAX_DAILY_LOSS_PCT = exports.CODES = void 0;
5
+ exports.READ_AND_SIM_TOOLS = exports.simulate = exports.positions = exports.orderBook = exports.markets = exports.funding = exports.fees = void 0;
5
6
  var guards_ts_1 = require("./guards.js");
6
7
  Object.defineProperty(exports, "CODES", { enumerable: true, get: function () { return guards_ts_1.CODES; } });
7
8
  Object.defineProperty(exports, "HARD_MAX_DAILY_LOSS_PCT", { enumerable: true, get: function () { return guards_ts_1.HARD_MAX_DAILY_LOSS_PCT; } });
@@ -24,6 +25,21 @@ var guarded_ts_1 = require("./guarded.js");
24
25
  Object.defineProperty(exports, "guardedSimulate", { enumerable: true, get: function () { return guarded_ts_1.guardedSimulate; } });
25
26
  var card_ts_1 = require("./card.js");
26
27
  Object.defineProperty(exports, "renderReceiptCard", { enumerable: true, get: function () { return card_ts_1.renderReceiptCard; } });
28
+ var fundingRecon_ts_1 = require("./fundingRecon.js");
29
+ Object.defineProperty(exports, "RECON_KIND", { enumerable: true, get: function () { return fundingRecon_ts_1.RECON_KIND; } });
30
+ Object.defineProperty(exports, "RECON_VERSION", { enumerable: true, get: function () { return fundingRecon_ts_1.RECON_VERSION; } });
31
+ Object.defineProperty(exports, "reconcile", { enumerable: true, get: function () { return fundingRecon_ts_1.reconcile; } });
32
+ Object.defineProperty(exports, "verifyReconReceipt", { enumerable: true, get: function () { return fundingRecon_ts_1.verifyReconReceipt; } });
33
+ var fillLink_ts_1 = require("./fillLink.js");
34
+ Object.defineProperty(exports, "LINK_KIND", { enumerable: true, get: function () { return fillLink_ts_1.LINK_KIND; } });
35
+ Object.defineProperty(exports, "LINK_VERSION", { enumerable: true, get: function () { return fillLink_ts_1.LINK_VERSION; } });
36
+ Object.defineProperty(exports, "buildLink", { enumerable: true, get: function () { return fillLink_ts_1.buildLink; } });
37
+ Object.defineProperty(exports, "computeLink", { enumerable: true, get: function () { return fillLink_ts_1.computeLink; } });
38
+ Object.defineProperty(exports, "linkInputs", { enumerable: true, get: function () { return fillLink_ts_1.linkInputs; } });
39
+ Object.defineProperty(exports, "normaliseSelector", { enumerable: true, get: function () { return fillLink_ts_1.normaliseSelector; } });
40
+ Object.defineProperty(exports, "selectFills", { enumerable: true, get: function () { return fillLink_ts_1.selectFills; } });
41
+ Object.defineProperty(exports, "simFromReceipt", { enumerable: true, get: function () { return fillLink_ts_1.simFromReceipt; } });
42
+ Object.defineProperty(exports, "verifyLink", { enumerable: true, get: function () { return fillLink_ts_1.verifyLink; } });
27
43
  var receipt_ts_1 = require("./receipt.js");
28
44
  Object.defineProperty(exports, "RECEIPT_KIND", { enumerable: true, get: function () { return receipt_ts_1.RECEIPT_KIND; } });
29
45
  Object.defineProperty(exports, "RECEIPT_STATEMENT", { enumerable: true, get: function () { return receipt_ts_1.RECEIPT_STATEMENT; } });
@@ -19,6 +19,7 @@ const node_crypto_1 = require("node:crypto");
19
19
  const node_fs_1 = require("node:fs");
20
20
  const node_path_1 = require("node:path");
21
21
  const guards_ts_1 = require("./guards.js");
22
+ const fundingRecon_ts_1 = require("./fundingRecon.js");
22
23
  const guarded_ts_1 = require("./guarded.js");
23
24
  const simDefaults_ts_1 = require("./vendor/simDefaults.js");
24
25
  const study_ts_1 = require("./study.js");
@@ -244,6 +245,14 @@ function verifyReceiptAt(path) {
244
245
  return report(["receipt mismatch"]);
245
246
  }
246
247
  const root = (0, node_path_1.dirname)(receiptFile);
248
+ if (receipt.kind === fundingRecon_ts_1.RECON_KIND) {
249
+ // A funding-reconciliation receipt is checked against inputs.json beside it.
250
+ const inputsPath = (0, node_path_1.join)(root, "inputs.json");
251
+ if (!(0, node_fs_1.existsSync)(inputsPath) || !(0, node_fs_1.statSync)(inputsPath).isFile()) {
252
+ return report(["inputs.json missing (download it next to receipt.json)"]);
253
+ }
254
+ return (0, fundingRecon_ts_1.verifyReconReceipt)(receipt, (0, node_fs_1.readFileSync)(inputsPath));
255
+ }
247
256
  const missing = [];
248
257
  const manifestPath = (0, node_path_1.join)(root, "manifest.json");
249
258
  const manifest = (0, node_fs_1.existsSync)(manifestPath) && (0, node_fs_1.statSync)(manifestPath).isFile() ? (0, node_fs_1.readFileSync)(manifestPath) : null;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Fill linking (issue 98): attach real Hyperliquid fills to a simulation receipt.
3
+ *
4
+ * TypeScript port of `perpbrain_read.sim.fill_link`. Same record, same bigint fixed-point
5
+ * arithmetic (18 decimals, truncating division), same canonical JSON, so a link record built
6
+ * by either SDK verifies in the other. Shared vectors: fixtures/fill_link/expected.json.
7
+ * Read-only: no keys, no order path, no network in this module.
8
+ */
9
+ export declare const LINK_KIND = "perpbrain.execution-link";
10
+ export declare const LINK_VERSION = 1;
11
+ export type LinkSelector = "oid" | "hash" | "cloid";
12
+ export type LinkNetwork = "mainnet" | "testnet";
13
+ type Row = Record<string, unknown>;
14
+ export interface SimTrade {
15
+ market: string;
16
+ side: string;
17
+ price: string;
18
+ size: string;
19
+ fee_usd: string;
20
+ decision_time_ms: number;
21
+ trade_index: number;
22
+ }
23
+ export interface LinkInputs {
24
+ network: string;
25
+ user: string;
26
+ by: LinkSelector;
27
+ value: string;
28
+ receipt_sha256: string;
29
+ sim: SimTrade;
30
+ fills: Row[];
31
+ }
32
+ export interface LinkResult {
33
+ status: "LINKED" | "NO_FILLS" | "MISMATCHED_TRADE";
34
+ problems: string[];
35
+ fills: number;
36
+ oids: number[];
37
+ hashes: string[];
38
+ size: string;
39
+ avg_px: string;
40
+ fees_usdc: string;
41
+ first_time_ms: number | null;
42
+ last_time_ms: number | null;
43
+ sim_price: string;
44
+ sim_size: string;
45
+ sim_fee_usd: string;
46
+ fill_ratio: string;
47
+ slippage_vs_sim_bps: string;
48
+ fee_drag_usd: string;
49
+ time_to_first_fill_ms: number | null;
50
+ }
51
+ export interface BuiltLink {
52
+ record: Record<string, unknown>;
53
+ recordBytes: Buffer;
54
+ inputsBytes: Buffer;
55
+ }
56
+ export interface LinkVerify {
57
+ status: "VERIFIED" | "MISMATCH";
58
+ mismatches: string[];
59
+ }
60
+ /** The simulated trade `tradeIndex` (0 = entry) of a perpbrain.sim-receipt. */
61
+ export declare function simFromReceipt(receipt: unknown, tradeIndex?: number): SimTrade;
62
+ /** The selector value as stored: decimal oid, lowercase 0x hash or cloid. */
63
+ export declare function normaliseSelector(by: string, value: unknown): string;
64
+ /** Raw fill rows matching the selector, verbatim, oldest first. */
65
+ export declare function selectFills(rows: readonly Row[], by: string, value: unknown): Row[];
66
+ export declare function linkInputs(receipt: unknown, rows: readonly Row[], options: {
67
+ network: string;
68
+ user: string;
69
+ by: LinkSelector;
70
+ value: unknown;
71
+ tradeIndex?: number;
72
+ }): LinkInputs;
73
+ export declare function computeLink(inputs: LinkInputs): LinkResult;
74
+ export declare function buildLink(inputs: LinkInputs): BuiltLink;
75
+ /** Re-hash the inputs, rerun the comparison and, given the receipt, check the binding. */
76
+ export declare function verifyLink(record: Record<string, unknown>, inputsBytes: Uint8Array, receipt?: unknown): LinkVerify;
77
+ export {};
@@ -0,0 +1,318 @@
1
+ /**
2
+ * Fill linking (issue 98): attach real Hyperliquid fills to a simulation receipt.
3
+ *
4
+ * TypeScript port of `perpbrain_read.sim.fill_link`. Same record, same bigint fixed-point
5
+ * arithmetic (18 decimals, truncating division), same canonical JSON, so a link record built
6
+ * by either SDK verifies in the other. Shared vectors: fixtures/fill_link/expected.json.
7
+ * Read-only: no keys, no order path, no network in this module.
8
+ */
9
+ import { createHash } from "node:crypto";
10
+ import { canonical } from "./receipt.js";
11
+ import { formatDecimal, parseDecimal } from "./vendor/fees.js";
12
+ export const LINK_KIND = "perpbrain.execution-link";
13
+ export const LINK_VERSION = 1;
14
+ const STATEMENT = "verified and reproducible";
15
+ const SCALE = 10n ** 18n;
16
+ const HASH = /^0x[0-9a-f]{64}$/;
17
+ const ZERO_HASH = `0x${"0".repeat(64)}`;
18
+ const CLOID = /^0x[0-9a-f]{32}$/;
19
+ const ADDRESS = /^0x[0-9a-f]{40}$/;
20
+ function sha256(text) {
21
+ return createHash("sha256").update(text).digest("hex");
22
+ }
23
+ function obj(value) {
24
+ return typeof value === "object" && value !== null && !Array.isArray(value)
25
+ ? value
26
+ : null;
27
+ }
28
+ function int(value) {
29
+ if (typeof value !== "number" || !Number.isInteger(value)) {
30
+ throw new Error("fill time and tid must be integers");
31
+ }
32
+ return value;
33
+ }
34
+ function text(value) {
35
+ return typeof value === "string" ? value : "";
36
+ }
37
+ function fixed(value) {
38
+ if (typeof value !== "string") {
39
+ throw new Error("decimal required");
40
+ }
41
+ return parseDecimal(value);
42
+ }
43
+ function tdiv(n, d) {
44
+ if (d === 0n) {
45
+ throw new Error("division by zero");
46
+ }
47
+ return n / d; // bigint division truncates toward zero, like the Python tdiv
48
+ }
49
+ /** The simulated trade `tradeIndex` (0 = entry) of a perpbrain.sim-receipt. */
50
+ export function simFromReceipt(receipt, tradeIndex = 0) {
51
+ const body = obj(receipt);
52
+ if (body === null || body.kind !== "perpbrain.sim-receipt") {
53
+ throw new Error("not a perpbrain.sim-receipt");
54
+ }
55
+ const result = obj(body.result);
56
+ const inputs = obj(body.inputs);
57
+ if (result === null || inputs === null) {
58
+ throw new Error("receipt has no result or inputs");
59
+ }
60
+ const fills = result.fills;
61
+ if (!Array.isArray(fills) || tradeIndex < 0 || tradeIndex >= fills.length) {
62
+ throw new Error(`receipt has no simulated trade ${tradeIndex}`);
63
+ }
64
+ const trade = obj(fills[tradeIndex]);
65
+ if (trade === null) {
66
+ throw new Error("simulated trade must be an object");
67
+ }
68
+ const decision = obj(inputs.decision);
69
+ const bars = inputs.bars;
70
+ if (decision === null || !Array.isArray(bars)) {
71
+ throw new Error("receipt inputs have no decision or bars");
72
+ }
73
+ const index = decision.bar_index;
74
+ if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index >= bars.length) {
75
+ throw new Error("decision bar_index is out of range");
76
+ }
77
+ let when = obj(bars[index])?.time_ms;
78
+ if (typeof when !== "number" || !Number.isInteger(when)) {
79
+ // Paper receipts use a flat bar without a time; the live book time is the decision time.
80
+ when = obj(inputs.book)?.time_ms;
81
+ }
82
+ if (typeof when !== "number" || !Number.isInteger(when)) {
83
+ throw new Error("decision bar has no time_ms");
84
+ }
85
+ const side = trade.side;
86
+ if (side !== "buy" && side !== "sell") {
87
+ throw new Error("simulated side must be buy or sell");
88
+ }
89
+ const market = "market" in result ? result.market : inputs.market;
90
+ if (typeof market !== "string") {
91
+ throw new Error("receipt has no market");
92
+ }
93
+ for (const key of ["price", "size", "fee"]) {
94
+ fixed(trade[key]);
95
+ }
96
+ return {
97
+ market,
98
+ side,
99
+ price: trade.price,
100
+ size: trade.size,
101
+ fee_usd: trade.fee,
102
+ decision_time_ms: when,
103
+ trade_index: tradeIndex,
104
+ };
105
+ }
106
+ /** The selector value as stored: decimal oid, lowercase 0x hash or cloid. */
107
+ export function normaliseSelector(by, value) {
108
+ const raw = String(value).trim();
109
+ if (by === "oid") {
110
+ if (!/^\d{1,20}$/.test(raw)) {
111
+ throw new Error("oid must be a non-negative integer");
112
+ }
113
+ return BigInt(raw).toString();
114
+ }
115
+ const lowered = raw.toLowerCase();
116
+ if (by === "hash") {
117
+ if (!HASH.test(lowered)) {
118
+ throw new Error("fill hash must be 0x followed by 64 hex digits");
119
+ }
120
+ if (lowered === ZERO_HASH) {
121
+ throw new Error("the all-zero fill hash is not unique; link by oid instead");
122
+ }
123
+ return lowered;
124
+ }
125
+ if (by === "cloid") {
126
+ if (!CLOID.test(lowered)) {
127
+ throw new Error("cloid must be 0x followed by 32 hex digits");
128
+ }
129
+ return lowered;
130
+ }
131
+ throw new Error("link by oid, hash or cloid");
132
+ }
133
+ function matches(row, by, value) {
134
+ if (by === "oid") {
135
+ return typeof row.oid === "number" && Number.isInteger(row.oid) && String(row.oid) === value;
136
+ }
137
+ if (by === "hash") {
138
+ return text(row.hash).toLowerCase() === value;
139
+ }
140
+ return text(row.cloid).toLowerCase() === value;
141
+ }
142
+ /** Raw fill rows matching the selector, verbatim, oldest first. */
143
+ export function selectFills(rows, by, value) {
144
+ const key = normaliseSelector(by, value);
145
+ const picked = rows.filter((r) => matches(r, by, key)).map((r) => ({ ...r }));
146
+ return picked.sort((a, b) => int(a.time) - int(b.time) || int(a.tid) - int(b.tid));
147
+ }
148
+ export function linkInputs(receipt, rows, options) {
149
+ if (options.network !== "mainnet" && options.network !== "testnet") {
150
+ throw new Error("network must be mainnet or testnet");
151
+ }
152
+ const account = options.user.toLowerCase();
153
+ if (!ADDRESS.test(account)) {
154
+ throw new Error("user must be a 0x-prefixed 20-byte address");
155
+ }
156
+ return {
157
+ network: options.network,
158
+ user: account,
159
+ by: options.by,
160
+ value: normaliseSelector(options.by, options.value),
161
+ receipt_sha256: sha256(canonical(receipt)),
162
+ sim: simFromReceipt(receipt, options.tradeIndex ?? 0),
163
+ fills: selectFills(rows, options.by, options.value),
164
+ };
165
+ }
166
+ export function computeLink(inputs) {
167
+ const sim = inputs.sim;
168
+ const by = inputs.by;
169
+ const value = normaliseSelector(by, inputs.value);
170
+ const rows = inputs.fills;
171
+ let problems = [];
172
+ if (rows.some((r) => !matches(r, by, value))) {
173
+ problems.push("a fill does not match the selector");
174
+ }
175
+ let size = 0n;
176
+ let notional = 0n;
177
+ let fees = 0n;
178
+ let first = null;
179
+ let last = null;
180
+ const oids = [];
181
+ const hashes = [];
182
+ const sideHl = sim.side === "buy" ? "B" : "A";
183
+ for (const row of rows) {
184
+ const sz = fixed(row.sz);
185
+ const px = fixed(row.px);
186
+ size += sz;
187
+ notional += tdiv(sz * px, SCALE);
188
+ if (row.feeToken === undefined || row.feeToken === null || row.feeToken === "USDC") {
189
+ fees += fixed(row.fee);
190
+ }
191
+ const t = int(row.time);
192
+ first = first === null ? t : Math.min(first, t);
193
+ last = last === null ? t : Math.max(last, t);
194
+ const oid = int(row.oid);
195
+ if (!oids.includes(oid)) {
196
+ oids.push(oid);
197
+ }
198
+ const h = text(row.hash).toLowerCase();
199
+ if (!hashes.includes(h)) {
200
+ hashes.push(h);
201
+ }
202
+ const coin = text(row.coin);
203
+ if (coin !== sim.market) {
204
+ problems.push(`fill coin ${coin || "?"} is not the simulated ${sim.market}`);
205
+ }
206
+ const side = text(row.side);
207
+ if (side !== sideHl) {
208
+ problems.push(`fill side ${side || "?"} is not the simulated ${sim.side}`);
209
+ }
210
+ }
211
+ const simPx = parseDecimal(sim.price);
212
+ const simSz = parseDecimal(sim.size);
213
+ const simFee = parseDecimal(sim.fee_usd);
214
+ const avg = size !== 0n ? tdiv(notional * SCALE, size) : 0n;
215
+ const diff = sim.side === "buy" ? avg - simPx : simPx - avg;
216
+ const bps = size !== 0n && simPx !== 0n ? tdiv(diff * 10000n * SCALE, simPx) : 0n;
217
+ const ratio = simSz !== 0n ? tdiv(size * SCALE, simSz) : 0n;
218
+ problems = [...new Set(problems)];
219
+ const status = rows.length === 0 ? "NO_FILLS" : problems.length > 0 ? "MISMATCHED_TRADE" : "LINKED";
220
+ return {
221
+ status,
222
+ problems,
223
+ fills: rows.length,
224
+ oids,
225
+ hashes,
226
+ size: formatDecimal(size),
227
+ avg_px: formatDecimal(avg),
228
+ fees_usdc: formatDecimal(fees),
229
+ first_time_ms: first,
230
+ last_time_ms: last,
231
+ sim_price: sim.price,
232
+ sim_size: sim.size,
233
+ sim_fee_usd: sim.fee_usd,
234
+ fill_ratio: formatDecimal(ratio),
235
+ slippage_vs_sim_bps: formatDecimal(bps),
236
+ fee_drag_usd: rows.length > 0 ? formatDecimal(fees - simFee) : "0",
237
+ time_to_first_fill_ms: first === null ? null : first - sim.decision_time_ms,
238
+ };
239
+ }
240
+ export function buildLink(inputs) {
241
+ const raw = canonical(inputs);
242
+ const result = computeLink(inputs);
243
+ const record = {
244
+ kind: LINK_KIND,
245
+ version: LINK_VERSION,
246
+ label: inputs.network === "mainnet" ? "MAINNET" : "TESTNET",
247
+ statement: STATEMENT,
248
+ receipt_sha256: inputs.receipt_sha256,
249
+ user: inputs.user,
250
+ selector: { by: inputs.by, value: inputs.value },
251
+ inputs_sha256: sha256(raw),
252
+ result,
253
+ result_sha256: sha256(canonical(result)),
254
+ };
255
+ return {
256
+ record,
257
+ recordBytes: Buffer.from(`${canonical(record)}\n`, "utf8"),
258
+ inputsBytes: Buffer.from(`${raw}\n`, "utf8"),
259
+ };
260
+ }
261
+ /** Re-hash the inputs, rerun the comparison and, given the receipt, check the binding. */
262
+ export function verifyLink(record, inputsBytes, receipt) {
263
+ const problems = [];
264
+ if (record.kind !== LINK_KIND) {
265
+ problems.push("kind");
266
+ }
267
+ if (record.version !== LINK_VERSION) {
268
+ problems.push("version");
269
+ }
270
+ let inputs;
271
+ let result;
272
+ try {
273
+ inputs = JSON.parse(Buffer.from(inputsBytes).toString("utf8"));
274
+ if (sha256(canonical(inputs)) !== record.inputs_sha256) {
275
+ problems.push("inputs_sha256");
276
+ }
277
+ result = computeLink(inputs);
278
+ }
279
+ catch (error) {
280
+ return {
281
+ status: "MISMATCH",
282
+ mismatches: [...problems, `inputs: ${error.message}`],
283
+ };
284
+ }
285
+ const label = inputs.network === "mainnet" ? "MAINNET" : "TESTNET";
286
+ if (record.label !== label) {
287
+ problems.push("label");
288
+ }
289
+ if (record.receipt_sha256 !== inputs.receipt_sha256) {
290
+ problems.push("receipt_sha256");
291
+ }
292
+ if (canonical(record.selector) !== canonical({ by: inputs.by, value: inputs.value })) {
293
+ problems.push("selector");
294
+ }
295
+ if (canonical(record.result) !== canonical(result)) {
296
+ problems.push("result");
297
+ }
298
+ if (record.result_sha256 !== sha256(canonical(result))) {
299
+ problems.push("result_sha256");
300
+ }
301
+ if (receipt !== undefined) {
302
+ if (sha256(canonical(receipt)) !== inputs.receipt_sha256) {
303
+ problems.push("receipt does not match receipt_sha256");
304
+ }
305
+ else {
306
+ try {
307
+ const sim = simFromReceipt(receipt, inputs.sim.trade_index);
308
+ if (canonical(sim) !== canonical(inputs.sim)) {
309
+ problems.push("sim trade differs from the receipt");
310
+ }
311
+ }
312
+ catch (error) {
313
+ problems.push(`receipt: ${error.message}`);
314
+ }
315
+ }
316
+ }
317
+ return { status: problems.length > 0 ? "MISMATCH" : "VERIFIED", mismatches: problems };
318
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Funding-reconciliation receipts (`perpbrain.funding-recon-receipt`), TypeScript port of
3
+ * `perpbrain_read.sim.funding_recon`: re-run the match of on-chain funding measurements against
4
+ * the venue's `userFunding` rows and check the receipt's hashes. No network, no keys.
5
+ *
6
+ * Decimal strings follow Python's `str(Decimal)` for the values these receipts carry
7
+ * (plain notation), as in the in-browser checker at perpbrain.com/verify.
8
+ */
9
+ export declare const RECON_KIND = "perpbrain.funding-recon-receipt";
10
+ export declare const RECON_VERSION = 1;
11
+ export interface ReconVerify {
12
+ status: "VERIFIED" | "MISMATCH";
13
+ mismatches: string[];
14
+ }
15
+ export declare function reconcile(inputs: Record<string, unknown>): Record<string, unknown>;
16
+ export declare function verifyReconReceipt(receipt: unknown, inputsBytes: Uint8Array): ReconVerify;