@inversealtruism/cairnx-core 0.2.4 → 0.2.7

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/index.cjs CHANGED
@@ -24,6 +24,8 @@ __export(index_exports, {
24
24
  ADDR_RE: () => ADDR_RE,
25
25
  AMOUNT_RE: () => AMOUNT_RE,
26
26
  BID_KEYS: () => BID_KEYS,
27
+ CAIRNX_EVENT_RULE: () => CAIRNX_EVENT_RULE,
28
+ CAP_CODES: () => CAP_CODES,
27
29
  CLAIM_ATTACKER_Q: () => CLAIM_ATTACKER_Q,
28
30
  CLAIM_COOLDOWN_BLOCKS: () => CLAIM_COOLDOWN_BLOCKS,
29
31
  CLAIM_FILL_GRACE_BLOCKS: () => CLAIM_FILL_GRACE_BLOCKS,
@@ -36,7 +38,8 @@ __export(index_exports, {
36
38
  DEPLOY_FEE: () => DEPLOY_FEE,
37
39
  DEPLOY_KEYS: () => DEPLOY_KEYS,
38
40
  DOMAIN: () => DOMAIN,
39
- EPOCH_LEN: () => import_csd_codec5.EPOCH_LEN,
41
+ EPOCH_LEN: () => import_csd_codec7.EPOCH_LEN,
42
+ EVENTS_HASH_START: () => EVENTS_HASH_START,
40
43
  FCLAIM_KEYS: () => FCLAIM_KEYS,
41
44
  FCLAIM_MAX_EPOCH_AHEAD: () => FCLAIM_MAX_EPOCH_AHEAD,
42
45
  FCLAIM_MAX_HOLD_SPAN: () => FCLAIM_MAX_HOLD_SPAN,
@@ -46,10 +49,11 @@ __export(index_exports, {
46
49
  FEE_GATE_MARGIN_BLOCKS: () => FEE_GATE_MARGIN_BLOCKS,
47
50
  FILL_TIP_MARGIN: () => FILL_TIP_MARGIN,
48
51
  FINALIZE_TIP_MARGIN: () => FINALIZE_TIP_MARGIN,
52
+ FINAL_DEPTH: () => FINAL_DEPTH,
49
53
  GAP_NEEDED: () => GAP_NEEDED,
50
- HALVING_INTERVAL: () => import_csd_codec5.HALVING_INTERVAL,
54
+ HALVING_INTERVAL: () => import_csd_codec7.HALVING_INTERVAL,
51
55
  HASH_RE: () => HASH_RE,
52
- INITIAL_REWARD: () => import_csd_codec5.INITIAL_REWARD,
56
+ INITIAL_REWARD: () => import_csd_codec7.INITIAL_REWARD,
53
57
  LEGACY_HOLD_SUNSET_HEIGHT: () => LEGACY_HOLD_SUNSET_HEIGHT,
54
58
  MAX_ACTIVE_CLAIMS: () => MAX_ACTIVE_CLAIMS,
55
59
  MAX_AMOUNT: () => MAX_AMOUNT,
@@ -59,8 +63,8 @@ __export(index_exports, {
59
63
  MAX_SCAN: () => MAX_SCAN,
60
64
  MAX_TOKEN_DECIMALS: () => MAX_TOKEN_DECIMALS,
61
65
  MINT_KEYS: () => MINT_KEYS,
62
- MIN_FEE_ATTEST: () => import_csd_codec5.MIN_FEE_ATTEST,
63
- MIN_FEE_PROPOSE: () => import_csd_codec5.MIN_FEE_PROPOSE,
66
+ MIN_FEE_ATTEST: () => import_csd_codec7.MIN_FEE_ATTEST,
67
+ MIN_FEE_PROPOSE: () => import_csd_codec7.MIN_FEE_PROPOSE,
64
68
  NAME_FEE_LEN3_V24: () => NAME_FEE_LEN3_V24,
65
69
  NAME_FEE_LEN4_V24: () => NAME_FEE_LEN4_V24,
66
70
  NAME_FEE_LONG_V24: () => NAME_FEE_LONG_V24,
@@ -79,11 +83,14 @@ __export(index_exports, {
79
83
  PKEY: () => PKEY,
80
84
  PROFILE_MAX_KEYS: () => PROFILE_MAX_KEYS,
81
85
  PROFILE_MAX_VALUE_BYTES: () => PROFILE_MAX_VALUE_BYTES,
86
+ REASON_CODES: () => REASON_CODES,
87
+ REASON_CODE_OUTCOME: () => REASON_CODE_OUTCOME,
82
88
  REBATE_BPS: () => REBATE_BPS,
83
89
  REBATE_FLAT: () => REBATE_FLAT,
84
90
  REG_COMMIT_MAX_BLOCKS: () => REG_COMMIT_MAX_BLOCKS,
85
91
  REG_FINALIZE_GRACE_BLOCKS: () => REG_FINALIZE_GRACE_BLOCKS,
86
92
  RESERVED_NAMES: () => RESERVED_NAMES,
93
+ RecordError: () => RecordError,
87
94
  SALT_RE: () => SALT_RE,
88
95
  SCAN_MARGIN: () => SCAN_MARGIN,
89
96
  SCORE_CANCEL: () => SCORE_CANCEL,
@@ -112,15 +119,18 @@ __export(index_exports, {
112
119
  V28_HEIGHT: () => V28_HEIGHT,
113
120
  V29_HEIGHT: () => V29_HEIGHT,
114
121
  ZERO_ADDR: () => ZERO_ADDR,
122
+ applyOrder: () => applyOrder,
115
123
  beforeActivation: () => beforeActivation,
116
124
  bid: () => bid,
117
125
  bindOfferTerms: () => bindOfferTerms,
118
126
  bindProvenOffer: () => bindProvenOffer,
119
- blockReward: () => import_csd_codec5.blockReward,
127
+ blockEvents: () => blockEvents,
128
+ blockMembers: () => blockMembers,
129
+ blockReward: () => import_csd_codec7.blockReward,
120
130
  buildFeeHeight: () => buildFeeHeight,
121
131
  buildRecord: () => buildRecord,
122
132
  burnedPayment: () => burnedPayment,
123
- canonicalJson: () => import_csd_codec5.canonicalJson,
133
+ canonicalJson: () => import_csd_codec7.canonicalJson,
124
134
  canonicalState: () => canonicalState,
125
135
  claimGraceOf: () => claimGraceOf,
126
136
  claimWindowAt: () => claimWindowAt,
@@ -128,7 +138,12 @@ __export(index_exports, {
128
138
  countOtherHoldsAtGrant: () => countOtherHoldsAtGrant,
129
139
  deploy: () => deploy,
130
140
  epochOf: () => import_csd_codec.epochOf,
141
+ eventId: () => eventId,
142
+ eventOfTx: () => eventOfTx,
143
+ eventsHash: () => eventsHash,
144
+ eventsHashStep: () => eventsHashStep,
131
145
  expiredClaimFee: () => expiredClaimFee,
146
+ explainRecord: () => explainRecord,
132
147
  fclaim: () => fclaim,
133
148
  fclaimEpochFor: () => fclaimEpochFor,
134
149
  fclaimHoldEnd: () => fclaimHoldEnd,
@@ -162,7 +177,7 @@ __export(index_exports, {
162
177
  paidToFromOutputs: () => paidToFromOutputs,
163
178
  parseAmount: () => parseAmount,
164
179
  parseRecord: () => parseRecord,
165
- payloadHash: () => import_csd_codec5.payloadHash,
180
+ payloadHash: () => import_csd_codec7.payloadHash,
166
181
  pickPrimaryName: () => pickPrimaryName,
167
182
  previewFill: () => previewFill,
168
183
  primaryRankBefore: () => primaryRankBefore,
@@ -173,6 +188,7 @@ __export(index_exports, {
173
188
  requiredClaimDepth: () => requiredClaimDepth,
174
189
  requiredFillOutputs: () => requiredFillOutputs,
175
190
  resolve: () => resolve,
191
+ stateHash: () => stateHash,
176
192
  tokenMeta: () => tokenMeta,
177
193
  tradeFee: () => tradeFee,
178
194
  transfer: () => transfer,
@@ -298,17 +314,26 @@ var isTokenWant = (w) => typeof w.ticker === "string";
298
314
 
299
315
  // src/records.ts
300
316
  var import_csd_codec2 = require("@inversealtruism/csd-codec");
301
- function parseAmount(s, opts = {}) {
302
- if (typeof s !== "string" || !AMOUNT_RE.test(s)) return null;
317
+ function amountOrCode(s, allowZero) {
318
+ if (typeof s !== "string" || !AMOUNT_RE.test(s)) return "RECORD_BAD_AMOUNT";
303
319
  const v = BigInt(s);
304
- if (v > MAX_AMOUNT) return null;
305
- if (v === 0n && !opts.allowZero) return null;
320
+ if (v > MAX_AMOUNT) return "RECORD_AMOUNT_TOO_LARGE";
321
+ if (v === 0n && !allowZero) return "RECORD_AMOUNT_ZERO";
306
322
  return v;
307
323
  }
324
+ function parseAmount(s, opts = {}) {
325
+ const v = amountOrCode(s, Boolean(opts.allowZero));
326
+ return typeof v === "bigint" ? v : null;
327
+ }
328
+ var amountFault = (s, allowZero = false) => {
329
+ const v = amountOrCode(s, allowZero);
330
+ return typeof v === "bigint" ? null : v;
331
+ };
308
332
  var isAddr = (a) => typeof a === "string" && ADDR_RE.test(a);
309
333
  var isTicker = (t) => typeof t === "string" && TICKER_RE.test(t);
310
334
  var isHash = (h) => typeof h === "string" && HASH_RE.test(h);
311
- var isName = (n) => typeof n === "string" && NAME_RE.test(n) && !RESERVED_NAMES.has(n);
335
+ var nameFault = (n) => typeof n !== "string" || !NAME_RE.test(n) ? "RECORD_BAD_NAME" : RESERVED_NAMES.has(n) ? "RECORD_NAME_RESERVED" : null;
336
+ var isName = (n) => nameFault(n) === null;
312
337
  function nameCommit(name, salt, owner) {
313
338
  return (0, import_csd_codec2.payloadHash)({ t: "cairnx:name:commit:v1", name, salt, owner: owner.toLowerCase() });
314
339
  }
@@ -334,172 +359,201 @@ var NFINALIZE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
334
359
  var NPROFILE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "p"]);
335
360
  var FCLAIM_KEYS = /* @__PURE__ */ new Set(["v", "t", "offer"]);
336
361
  function parseRecord(uri, payloadHashHex) {
337
- if (new TextEncoder().encode(uri).length > MAX_RECORD_BYTES) return null;
362
+ const r = recordOrCode(uri, payloadHashHex);
363
+ return typeof r === "string" ? null : r;
364
+ }
365
+ function explainRecord(uri, payloadHashHex) {
366
+ const r = recordOrCode(uri, payloadHashHex);
367
+ return typeof r === "string" ? { ok: false, code: r } : { ok: true, record: r };
368
+ }
369
+ function recordOrCode(uri, payloadHashHex) {
370
+ if (new TextEncoder().encode(uri).length > MAX_RECORD_BYTES) return "RECORD_TOO_LARGE";
338
371
  let obj;
339
372
  try {
340
373
  obj = JSON.parse(uri);
341
374
  } catch {
342
- return null;
375
+ return "RECORD_NOT_OBJECT";
343
376
  }
344
- if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return null;
377
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return "RECORD_NOT_OBJECT";
345
378
  try {
346
- if ((0, import_csd_codec2.canonicalJson)(obj) !== uri) return null;
347
- if ((0, import_csd_codec2.payloadHash)(obj).toLowerCase() !== payloadHashHex.toLowerCase()) return null;
379
+ if ((0, import_csd_codec2.canonicalJson)(obj) !== uri) return "RECORD_NOT_CANONICAL";
348
380
  } catch {
349
- return null;
381
+ return "RECORD_NOT_CANONICAL";
350
382
  }
351
- if (!isWellFormedDeep(obj)) return null;
383
+ try {
384
+ if ((0, import_csd_codec2.payloadHash)(obj).toLowerCase() !== payloadHashHex.toLowerCase()) return "RECORD_HASH_MISMATCH";
385
+ } catch {
386
+ return "RECORD_HASH_MISMATCH";
387
+ }
388
+ if (!isWellFormedDeep(obj)) return "RECORD_LONE_SURROGATE";
352
389
  const r = obj;
353
- if (r.v !== 1 || typeof r.t !== "string") return null;
390
+ if (r.v !== 1 || typeof r.t !== "string") return "RECORD_NOT_V1";
391
+ let fault;
354
392
  switch (r.t) {
355
393
  case "deploy": {
356
- if (!onlyKeys(r, DEPLOY_KEYS)) return null;
357
- if (!isTicker(r.ticker)) return null;
358
- if (r.name !== void 0 && (typeof r.name !== "string" || r.name.length > 32)) return null;
359
- if (typeof r.decimals !== "number" || !Number.isInteger(r.decimals) || r.decimals < 0 || r.decimals > MAX_TOKEN_DECIMALS) return null;
360
- if (parseAmount(r.supply) === null) return null;
361
- if (r.mint !== "open" && r.mint !== "issuer") return null;
362
- if (r.mint === "open" && parseAmount(r.mintLimit) === null) return null;
363
- if (r.mint === "issuer" && r.mintLimit !== void 0) return null;
394
+ if (!onlyKeys(r, DEPLOY_KEYS)) return "RECORD_EXTRA_KEY";
395
+ if (!isTicker(r.ticker)) return "RECORD_BAD_TICKER";
396
+ if (r.name !== void 0 && (typeof r.name !== "string" || r.name.length > 32)) return "RECORD_BAD_TEXT";
397
+ if (typeof r.decimals !== "number" || !Number.isInteger(r.decimals) || r.decimals < 0 || r.decimals > MAX_TOKEN_DECIMALS) return "RECORD_BAD_DECIMALS";
398
+ if (fault = amountFault(r.supply)) return fault;
399
+ if (r.mint !== "open" && r.mint !== "issuer") return "RECORD_BAD_MINT_MODE";
400
+ if (r.mint === "open" && (fault = amountFault(r.mintLimit))) return fault;
401
+ if (r.mint === "issuer" && r.mintLimit !== void 0) return "RECORD_BAD_SHAPE";
364
402
  return r;
365
403
  }
366
404
  case "mint": {
367
- if (!onlyKeys(r, MINT_KEYS)) return null;
368
- if (!isTicker(r.ticker)) return null;
369
- if (r.amount !== void 0 && parseAmount(r.amount) === null) return null;
405
+ if (!onlyKeys(r, MINT_KEYS)) return "RECORD_EXTRA_KEY";
406
+ if (!isTicker(r.ticker)) return "RECORD_BAD_TICKER";
407
+ if (r.amount !== void 0 && (fault = amountFault(r.amount))) return fault;
370
408
  return r;
371
409
  }
372
410
  case "transfer": {
373
- if (!onlyKeys(r, TRANSFER_KEYS)) return null;
374
- if (!isTicker(r.ticker) || !isAddr(r.to) || parseAmount(r.amount) === null) return null;
375
- if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
376
- if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
411
+ if (!onlyKeys(r, TRANSFER_KEYS)) return "RECORD_EXTRA_KEY";
412
+ if (!isTicker(r.ticker)) return "RECORD_BAD_TICKER";
413
+ if (!isAddr(r.to)) return "RECORD_BAD_ADDRESS";
414
+ if (fault = amountFault(r.amount)) return fault;
415
+ if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return "RECORD_BAD_TEXT";
416
+ if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return "RECORD_BAD_TS";
377
417
  return r;
378
418
  }
379
419
  case "offer": {
380
- if (!onlyKeys(r, OFFER_KEYS)) return null;
420
+ if (!onlyKeys(r, OFFER_KEYS)) return "RECORD_EXTRA_KEY";
381
421
  const g = r.give;
382
422
  const w = r.want;
383
- if (!g || !w || typeof g !== "object" || Array.isArray(g) || typeof w !== "object" || Array.isArray(w)) return null;
423
+ if (!g || !w || typeof g !== "object" || Array.isArray(g) || typeof w !== "object" || Array.isArray(w)) return "RECORD_BAD_SHAPE";
384
424
  const gKeys = Object.keys(g).sort().join(",");
385
425
  if (gKeys === "amount,ticker") {
386
- if (!isTicker(g.ticker) || parseAmount(g.amount) === null) return null;
426
+ if (!isTicker(g.ticker)) return "RECORD_BAD_TICKER";
427
+ if (fault = amountFault(g.amount)) return fault;
387
428
  } else if (gKeys === "name") {
388
- if (!isName(g.name)) return null;
389
- } else return null;
429
+ if (fault = nameFault(g.name)) return fault;
430
+ } else return "RECORD_BAD_SHAPE";
390
431
  const wKeys = Object.keys(w).filter((k) => k !== "payto").sort().join(",");
391
432
  if (wKeys === "value") {
392
- if (parseAmount(w.value, { allowZero: true }) === null) return null;
433
+ if (fault = amountFault(w.value, true)) return fault;
393
434
  } else if (wKeys === "amount,ticker") {
394
- if (!isTicker(w.ticker) || parseAmount(w.amount, { allowZero: true }) === null) return null;
395
- if (gKeys === "amount,ticker" && w.ticker === g.ticker) return null;
396
- if (r.min !== void 0) return null;
397
- } else return null;
398
- if (w.payto !== void 0 && !isAddr(w.payto)) return null;
435
+ if (!isTicker(w.ticker)) return "RECORD_BAD_TICKER";
436
+ if (fault = amountFault(w.amount, true)) return fault;
437
+ if (gKeys === "amount,ticker" && w.ticker === g.ticker) return "RECORD_SAME_TOKEN";
438
+ if (r.min !== void 0) return "RECORD_BAD_SHAPE";
439
+ } else return "RECORD_BAD_SHAPE";
440
+ if (w.payto !== void 0 && !isAddr(w.payto)) return "RECORD_BAD_ADDRESS";
399
441
  if (r.min !== void 0) {
400
- if (gKeys !== "amount,ticker" || wKeys !== "value") return null;
401
- const mn = parseAmount(r.min);
402
- if (mn === null || mn > parseAmount(w.value, { allowZero: true })) return null;
442
+ if (gKeys !== "amount,ticker" || wKeys !== "value") return "RECORD_BAD_SHAPE";
443
+ if (fault = amountFault(r.min)) return fault;
444
+ if (parseAmount(r.min) > parseAmount(w.value, { allowZero: true })) return "RECORD_MIN_ABOVE_WANT";
403
445
  }
404
- if (r.bid !== void 0 && !isHash(r.bid)) return null;
405
- if (r.taker !== void 0 && !isAddr(r.taker)) return null;
406
- if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
407
- if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
446
+ if (r.bid !== void 0 && !isHash(r.bid)) return "RECORD_BAD_HASH";
447
+ if (r.taker !== void 0 && !isAddr(r.taker)) return "RECORD_BAD_ADDRESS";
448
+ if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return "RECORD_BAD_TEXT";
449
+ if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return "RECORD_BAD_TS";
408
450
  return r;
409
451
  }
410
452
  case "ocancel": {
411
- if (r.ticker !== void 0 && r.name !== void 0) return null;
412
- if (r.ticker !== void 0 && !isTicker(r.ticker)) return null;
413
- if (r.name !== void 0 && !isName(r.name)) return null;
453
+ if (r.ticker !== void 0 && r.name !== void 0) return "RECORD_BAD_SHAPE";
454
+ if (r.ticker !== void 0 && !isTicker(r.ticker)) return "RECORD_BAD_TICKER";
455
+ if (r.name !== void 0 && (fault = nameFault(r.name))) return fault;
414
456
  const n = Object.keys(r).length;
415
- if (n !== 2 + (r.ticker !== void 0 ? 1 : 0) + (r.name !== void 0 ? 1 : 0)) return null;
457
+ if (n !== 2 + (r.ticker !== void 0 ? 1 : 0) + (r.name !== void 0 ? 1 : 0)) return "RECORD_EXTRA_KEY";
416
458
  return r;
417
459
  }
418
460
  case "bid": {
419
- if (!onlyKeys(r, BID_KEYS)) return null;
461
+ if (!onlyKeys(r, BID_KEYS)) return "RECORD_EXTRA_KEY";
420
462
  const w = r.want;
421
463
  const g = r.give;
422
- if (!w || !g || typeof w !== "object" || Array.isArray(w) || typeof g !== "object" || Array.isArray(g)) return null;
464
+ if (!w || !g || typeof w !== "object" || Array.isArray(w) || typeof g !== "object" || Array.isArray(g)) return "RECORD_BAD_SHAPE";
423
465
  const wKeys = Object.keys(w).sort().join(",");
424
466
  if (wKeys === "amount,ticker") {
425
- if (!isTicker(w.ticker) || parseAmount(w.amount) === null) return null;
467
+ if (!isTicker(w.ticker)) return "RECORD_BAD_TICKER";
468
+ if (fault = amountFault(w.amount)) return fault;
426
469
  } else if (wKeys === "name") {
427
- if (!isName(w.name)) return null;
428
- } else return null;
429
- if (Object.keys(g).sort().join(",") !== "value" || parseAmount(g.value) === null) return null;
430
- if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
431
- if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
470
+ if (fault = nameFault(w.name)) return fault;
471
+ } else return "RECORD_BAD_SHAPE";
472
+ if (Object.keys(g).sort().join(",") !== "value") return "RECORD_BAD_SHAPE";
473
+ if (fault = amountFault(g.value)) return fault;
474
+ if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return "RECORD_BAD_TEXT";
475
+ if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return "RECORD_BAD_TS";
432
476
  return r;
433
477
  }
434
478
  // ── names (v1.1) ──
435
479
  case "ncommit": {
436
- if (!isHash(r.commit)) return null;
437
- if (Object.keys(r).length !== 3) return null;
480
+ if (!isHash(r.commit)) return "RECORD_BAD_HASH";
481
+ if (Object.keys(r).length !== 3) return "RECORD_EXTRA_KEY";
438
482
  return r;
439
483
  }
440
484
  case "name": {
441
- if (!onlyKeys(r, NAME_KEYS)) return null;
442
- if (!isName(r.name)) return null;
443
- if (r.salt !== void 0 && (typeof r.salt !== "string" || !SALT_RE.test(r.salt))) return null;
485
+ if (!onlyKeys(r, NAME_KEYS)) return "RECORD_EXTRA_KEY";
486
+ if (fault = nameFault(r.name)) return fault;
487
+ if (r.salt !== void 0 && (typeof r.salt !== "string" || !SALT_RE.test(r.salt))) return "RECORD_BAD_SALT";
444
488
  return r;
445
489
  }
446
490
  case "nfinalize": {
447
- if (!onlyKeys(r, NFINALIZE_KEYS)) return null;
448
- if (!isName(r.name)) return null;
449
- if (typeof r.salt !== "string" || !SALT_RE.test(r.salt)) return null;
491
+ if (!onlyKeys(r, NFINALIZE_KEYS)) return "RECORD_EXTRA_KEY";
492
+ if (fault = nameFault(r.name)) return fault;
493
+ if (typeof r.salt !== "string" || !SALT_RE.test(r.salt)) return "RECORD_BAD_SALT";
450
494
  return r;
451
495
  }
452
496
  case "nxfer": {
453
- if (!isName(r.name) || !isAddr(r.to)) return null;
454
- if (Object.keys(r).length !== 4) return null;
497
+ if (fault = nameFault(r.name)) return fault;
498
+ if (!isAddr(r.to)) return "RECORD_BAD_ADDRESS";
499
+ if (Object.keys(r).length !== 4) return "RECORD_EXTRA_KEY";
455
500
  return r;
456
501
  }
457
502
  case "nset": {
458
- if (!isName(r.name) || !isAddr(r.addr)) return null;
459
- if (Object.keys(r).length !== 4) return null;
503
+ if (fault = nameFault(r.name)) return fault;
504
+ if (!isAddr(r.addr)) return "RECORD_BAD_ADDRESS";
505
+ if (Object.keys(r).length !== 4) return "RECORD_EXTRA_KEY";
460
506
  return r;
461
507
  }
462
508
  // ── v1.5 ──
463
509
  case "nrenew": {
464
- if (!isName(r.name)) return null;
465
- if (Object.keys(r).length !== 3) return null;
510
+ if (fault = nameFault(r.name)) return fault;
511
+ if (Object.keys(r).length !== 3) return "RECORD_EXTRA_KEY";
466
512
  return r;
467
513
  }
468
514
  case "nprofile": {
469
- if (!onlyKeys(r, NPROFILE_KEYS)) return null;
470
- if (!isName(r.name)) return null;
515
+ if (!onlyKeys(r, NPROFILE_KEYS)) return "RECORD_EXTRA_KEY";
516
+ if (fault = nameFault(r.name)) return fault;
471
517
  const p = r.p;
472
- if (!p || typeof p !== "object" || Array.isArray(p)) return null;
518
+ if (!p || typeof p !== "object" || Array.isArray(p)) return "RECORD_BAD_SHAPE";
473
519
  const keys = Object.keys(p);
474
- if (keys.length > PROFILE_MAX_KEYS) return null;
520
+ if (keys.length > PROFILE_MAX_KEYS) return "RECORD_PROFILE_TOO_MANY_KEYS";
475
521
  for (const k of keys) {
476
- if (!PKEY.test(k)) return null;
522
+ if (!PKEY.test(k)) return "RECORD_PROFILE_BAD_KEY";
477
523
  const val = p[k];
478
- if (typeof val !== "string") return null;
479
- if (new TextEncoder().encode(val).length > PROFILE_MAX_VALUE_BYTES) return null;
524
+ if (typeof val !== "string") return "RECORD_PROFILE_BAD_VALUE";
525
+ if (new TextEncoder().encode(val).length > PROFILE_MAX_VALUE_BYTES) return "RECORD_PROFILE_VALUE_TOO_LONG";
480
526
  }
481
527
  return r;
482
528
  }
483
529
  case "tmeta": {
484
- if (!isTicker(r.ticker)) return null;
485
- if (typeof r.hash !== "string" || !HASH_RE.test(r.hash)) return null;
486
- if (Object.keys(r).length !== 4) return null;
530
+ if (!isTicker(r.ticker)) return "RECORD_BAD_TICKER";
531
+ if (typeof r.hash !== "string" || !HASH_RE.test(r.hash)) return "RECORD_BAD_HASH";
532
+ if (Object.keys(r).length !== 4) return "RECORD_EXTRA_KEY";
487
533
  return r;
488
534
  }
489
535
  case "fclaim": {
490
- if (!onlyKeys(r, FCLAIM_KEYS)) return null;
491
- if (!isHash(r.offer)) return null;
536
+ if (!onlyKeys(r, FCLAIM_KEYS)) return "RECORD_EXTRA_KEY";
537
+ if (!isHash(r.offer)) return "RECORD_BAD_HASH";
492
538
  return r;
493
539
  }
494
540
  default:
495
- return null;
541
+ return "RECORD_UNKNOWN_TYPE";
496
542
  }
497
543
  }
544
+ var RecordError = class extends Error {
545
+ code;
546
+ constructor(code) {
547
+ super(`record does not validate against CONVENTION.md (${code})`);
548
+ this.name = "RecordError";
549
+ this.code = code;
550
+ }
551
+ };
498
552
  function buildRecord(record) {
499
553
  const uri = (0, import_csd_codec2.canonicalJson)(record);
500
554
  const ph = (0, import_csd_codec2.payloadHash)(record);
501
- const back = parseRecord(uri, ph);
502
- if (back === null) throw new Error("record does not validate against CONVENTION.md");
555
+ const back = explainRecord(uri, ph);
556
+ if (!back.ok) throw new RecordError(back.code);
503
557
  return { record, uri, payloadHash: ph };
504
558
  }
505
559
  var deploy = (r) => buildRecord({ v: 1, t: "deploy", ...r });
@@ -525,33 +579,12 @@ function ptAmt(v) {
525
579
  var effExpiry = (e, height) => e.height >= V22_HEIGHT ? e.expiresEpoch : height >= V21_HEIGHT ? Math.min(e.expiresEpoch, (0, import_csd_codec.epochOf)(e.height) + MAX_OFFER_EPOCHS) : e.expiresEpoch;
526
580
  var claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
527
581
  var claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
528
- function createContext() {
582
+ function createContext(store) {
529
583
  const tokens = /* @__PURE__ */ new Map();
530
584
  const balances = /* @__PURE__ */ new Map();
531
- const names = /* @__PURE__ */ new Map();
532
585
  const commits = /* @__PURE__ */ new Map();
533
- const recaptures = /* @__PURE__ */ new Map();
534
- const offers = /* @__PURE__ */ new Map();
535
586
  const offerLock = /* @__PURE__ */ new Map();
536
- const bids = /* @__PURE__ */ new Map();
537
- const openOffers = /* @__PURE__ */ new Set();
538
- const openBids = /* @__PURE__ */ new Set();
539
- const pendingNames = /* @__PURE__ */ new Set();
540
- const setOfferStatus = (o, status) => {
541
- o.status = status;
542
- if (status === "open") openOffers.add(o.id);
543
- else openOffers.delete(o.id);
544
- };
545
- const setBidStatus = (b, status) => {
546
- b.status = status;
547
- if (status === "open") openBids.add(b.id);
548
- else openBids.delete(b.id);
549
- };
550
- const putName = (nm, n) => {
551
- names.set(nm, n);
552
- if (n.pending) pendingNames.add(nm);
553
- else pendingNames.delete(nm);
554
- };
587
+ const { names, recaptures, offers, bids, setOfferStatus, setBidStatus, putName } = store;
555
588
  const fclaims = /* @__PURE__ */ new Map();
556
589
  const log = [];
557
590
  const bal = (ticker, addr) => {
@@ -567,7 +600,6 @@ function createContext() {
567
600
  }
568
601
  return b;
569
602
  };
570
- const note = (e, id, kind, ok, why) => log.push({ height: e.height, pos: e.pos, id, kind, ok, ...why ? { note: why } : {} });
571
603
  const releaseGive = (o) => {
572
604
  if (isNameGive(o.give)) {
573
605
  const n = names.get(o.give.name);
@@ -579,66 +611,27 @@ function createContext() {
579
611
  b.available += amt;
580
612
  }
581
613
  };
582
- const sweepExpired = (height) => {
583
- const ep = (0, import_csd_codec.epochOf)(height);
584
- for (const id of [...openOffers]) {
585
- const o = offers.get(id);
586
- if (!o || o.status !== "open") {
587
- openOffers.delete(id);
588
- continue;
589
- }
590
- if (ep > effExpiry(o, height)) {
591
- releaseGive(o);
592
- setOfferStatus(o, "expired");
593
- }
594
- }
595
- for (const id of [...openBids]) {
596
- const b = bids.get(id);
597
- if (!b || b.status !== "open") {
598
- openBids.delete(id);
599
- continue;
600
- }
601
- if (ep > effExpiry(b, height)) setBidStatus(b, "expired");
602
- }
603
- for (const nm of [...pendingNames]) {
604
- const n = names.get(nm);
605
- if (!n || !n.pending) {
606
- pendingNames.delete(nm);
607
- continue;
608
- }
609
- if (n.finalizeBy !== void 0 && height > n.finalizeBy) {
610
- names.delete(nm);
611
- pendingNames.delete(nm);
612
- }
613
- }
614
- for (const nm of [...recaptures.keys()]) {
615
- const r = recaptures.get(nm);
616
- if (height > r.finalizeBy) recaptures.delete(nm);
617
- }
618
- };
614
+ const sweepExpired = (height) => store.sweep(height, releaseGive);
619
615
  const markBidDone = (o, buyer) => {
620
616
  if (!o.bid) return;
621
617
  const b = bids.get(o.bid);
622
618
  if (b && b.status === "open" && b.bidder === buyer) setBidStatus(b, "done");
623
619
  };
624
620
  const voidOpenNameOffers = (name, height) => {
625
- for (const o of offers.values()) if (o.status === "open" && isNameGive(o.give) && o.give.name === name) {
626
- if (height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, height)) continue;
621
+ for (const o of store.voidTargets(name, height)) {
627
622
  releaseGive(o);
628
623
  setOfferStatus(o, "cancelled");
629
624
  }
630
625
  };
631
- const liveHoldCount = (who, height) => {
632
- let liveN = 0;
633
- for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, height) && (height < V29_HEIGHT || x.status === "open")) liveN++;
634
- return liveN;
635
- };
636
626
  const releaseGiveLock = (o, who, amt) => {
637
627
  const t = o.give.ticker;
638
628
  bal(t, o.seller).locked -= amt;
639
629
  bal(t, who).available += amt;
640
630
  offerLock.delete(o.id);
641
631
  };
632
+ const entry = (e, id, kind, ok, code, why) => log.push({ height: e.height, pos: e.pos, id, kind, ok, code, ...why ? { note: why } : {} });
633
+ const applied = (e, id, kind, code, why) => entry(e, id, kind, true, code, why);
634
+ const refused = (e, id, kind, code, why) => entry(e, id, kind, false, code, why);
642
635
  return {
643
636
  tokens,
644
637
  balances,
@@ -648,21 +641,19 @@ function createContext() {
648
641
  offers,
649
642
  offerLock,
650
643
  bids,
651
- openOffers,
652
- openBids,
653
- pendingNames,
654
644
  fclaims,
655
645
  log,
646
+ store,
656
647
  setOfferStatus,
657
648
  setBidStatus,
658
649
  putName,
659
650
  bal,
660
- note,
651
+ applied,
652
+ refused,
661
653
  releaseGive,
662
654
  sweepExpired,
663
655
  markBidDone,
664
656
  voidOpenNameOffers,
665
- liveHoldCount,
666
657
  releaseGiveLock,
667
658
  feesPaid: 0n,
668
659
  // v1.4 fill-before-cancel: at V14_HEIGHT+ a cancel/ocancel's EFFECT is deferred to the block
@@ -678,13 +669,13 @@ function createContext() {
678
669
  // src/resolve/attest.ts
679
670
  var openFillReject = (o, height, who, targetId) => {
680
671
  if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
681
- if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
682
- if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
683
- if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
672
+ if (height < V17_HEIGHT) return { code: "FILL_OPEN_LANE_CLOSED", note: "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)" };
673
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return { code: "FILL_WRONG_TARGET", note: "v2.8: fill the fclaim txid, not the offer" };
674
+ if (!(claimHeld(o, height) && who === o.claimedBy)) return { code: "FILL_NO_LIVE_CLAIM", note: "v1.7: open offer \u2014 claim it first (no live claim by you)" };
684
675
  return void 0;
685
676
  };
686
- var deliverNameToBuyer = (n, who, ev) => {
687
- n.owner = who;
677
+ var deliverNameToBuyer = (c, name, n, who, ev) => {
678
+ c.store.setNameOwner(name, n, who);
688
679
  n.locked = false;
689
680
  n.addr = void 0;
690
681
  n.profile = void 0;
@@ -697,7 +688,7 @@ var deliverNameToBuyer = (n, who, ev) => {
697
688
  }
698
689
  };
699
690
  function applyAttest(c, ev) {
700
- const { offers, fclaims, bids, note, setBidStatus } = c;
691
+ const { offers, fclaims, bids, applied, refused, setBidStatus } = c;
701
692
  const who = ev.attester.toLowerCase();
702
693
  let o = offers.get(ev.proposalId);
703
694
  if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
@@ -709,21 +700,21 @@ function applyAttest(c, ev) {
709
700
  }
710
701
  if (!o) {
711
702
  if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
712
- note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
703
+ refused(ev, ev.txid, "fill", "FILL_FCLAIM_NOT_CURRENT", "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
713
704
  return;
714
705
  }
715
706
  const b = bids.get(ev.proposalId);
716
707
  if (b && ev.score === SCORE_CANCEL) {
717
708
  if (who !== b.bidder) {
718
- note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
709
+ refused(ev, ev.txid, "bidcancel", "BID_NOT_BIDDER", "only bidder may cancel");
719
710
  return;
720
711
  }
721
712
  if (b.status !== "open") {
722
- note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
713
+ refused(ev, ev.txid, "bidcancel", "BID_NOT_OPEN", `bid ${b.status}`);
723
714
  return;
724
715
  }
725
716
  setBidStatus(b, "cancelled");
726
- note(ev, ev.txid, "bidcancel", true);
717
+ applied(ev, ev.txid, "bidcancel", "BID_CANCELLED");
727
718
  }
728
719
  return;
729
720
  }
@@ -734,95 +725,95 @@ function applyAttest(c, ev) {
734
725
  else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) applyLegacyClaim(c, ev, o, who);
735
726
  }
736
727
  function applyCancel(c, ev, o, who) {
737
- const { note, releaseGive, setOfferStatus } = c;
728
+ const { applied, refused, releaseGive, setOfferStatus } = c;
738
729
  if (who !== o.seller) {
739
- note(ev, ev.txid, "cancel", false, "only seller may cancel");
730
+ refused(ev, ev.txid, "cancel", "OFFER_NOT_SELLER", "only seller may cancel");
740
731
  return;
741
732
  }
742
733
  if (o.status !== "open") {
743
- note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
734
+ refused(ev, ev.txid, "cancel", "OFFER_NOT_OPEN", `offer ${o.status}`);
744
735
  return;
745
736
  }
746
737
  if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
747
- note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
738
+ refused(ev, ev.txid, "cancel", "OFFER_FIRM_QUOTE", "v2.8: taker-bound offers are firm quotes (uncancellable)");
748
739
  return;
749
740
  }
750
741
  if (ev.height >= V14_HEIGHT) {
751
742
  c.pendingCancels.push(() => {
752
743
  if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
753
- note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
744
+ refused(ev, ev.txid, "cancel", "OFFER_CANCEL_HOLD_LIVE", "v2.8: frozen (fclaim hold live)");
754
745
  return;
755
746
  }
756
747
  if (o.status === "open") {
757
748
  releaseGive(o);
758
749
  setOfferStatus(o, "cancelled");
759
- note(ev, ev.txid, "cancel", true);
760
- } else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
750
+ applied(ev, ev.txid, "cancel", "OFFER_CANCELLED");
751
+ } else refused(ev, ev.txid, "cancel", "OFFER_CLOSED_BEFORE_CANCEL", "superseded by same-block fill (v1.4)");
761
752
  });
762
753
  } else {
763
754
  releaseGive(o);
764
755
  setOfferStatus(o, "cancelled");
765
- note(ev, ev.txid, "cancel", true);
756
+ applied(ev, ev.txid, "cancel", "OFFER_CANCELLED");
766
757
  }
767
758
  }
768
759
  function applyTokenFill(c, ev, o, who) {
769
- const { tokens, names, offerLock, bal, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
760
+ const { tokens, names, offerLock, bal, applied, refused, setOfferStatus, markBidDone, releaseGiveLock } = c;
770
761
  if (o.status !== "open") {
771
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
762
+ refused(ev, ev.txid, "fill", "OFFER_NOT_OPEN", `offer ${o.status}`);
772
763
  return;
773
764
  }
774
765
  if (o.taker && who !== o.taker) {
775
- note(ev, ev.txid, "fill", false, "taker-bound offer");
766
+ refused(ev, ev.txid, "fill", "FILL_NOT_TAKER", "taker-bound offer");
776
767
  return;
777
768
  }
778
769
  if (ev.confidence !== CONF_TOKEN_FILL) {
779
- note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
770
+ refused(ev, ev.txid, "fill", "FILL_NO_TOKEN_MARKER", "token fill requires confidence marker");
780
771
  return;
781
772
  }
782
773
  if (!tokens.has(o.want.ticker)) {
783
- note(ev, ev.txid, "fill", false, "want ticker does not exist");
774
+ refused(ev, ev.txid, "fill", "TOKEN_UNKNOWN", "want ticker does not exist");
784
775
  return;
785
776
  }
786
777
  const amt = BigInt(o.want.amount);
787
778
  const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
788
779
  const buyer = bal(o.want.ticker, who);
789
780
  if (buyer.available < amt + fee) {
790
- note(ev, ev.txid, "fill", false, "insufficient want-token balance");
781
+ refused(ev, ev.txid, "fill", "TOKEN_BALANCE_LOW", "insufficient want-token balance");
791
782
  return;
792
783
  }
793
784
  const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
794
785
  if (isNameGive(o.give) && !giveName) {
795
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
786
+ refused(ev, ev.txid, "fill", "INVARIANT_BROKEN", "name vanished (consensus violation)");
796
787
  return;
797
788
  }
798
789
  const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
799
790
  if (!isNameGive(o.give) && giveLock === void 0) {
800
- note(ev, ev.txid, "fill", false, "offer lock missing");
791
+ refused(ev, ev.txid, "fill", "INVARIANT_BROKEN", "offer lock missing");
801
792
  return;
802
793
  }
803
794
  buyer.available -= amt + fee;
804
795
  bal(o.want.ticker, o.want.payto).available += amt;
805
796
  if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
806
- if (isNameGive(o.give)) deliverNameToBuyer(giveName, who, ev);
797
+ if (isNameGive(o.give)) deliverNameToBuyer(c, o.give.name, giveName, who, ev);
807
798
  else releaseGiveLock(o, who, giveLock);
808
799
  setOfferStatus(o, "filled");
809
800
  o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
810
801
  markBidDone(o, who);
811
- note(ev, ev.txid, "fill", true);
802
+ applied(ev, ev.txid, "fill", "FILL_APPLIED");
812
803
  }
813
804
  function applyPartialFill(c, ev, o, who) {
814
- const { offerLock, bal, note, setOfferStatus, markBidDone } = c;
805
+ const { offerLock, bal, applied, refused, setOfferStatus, markBidDone } = c;
815
806
  if (o.status !== "open") {
816
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
807
+ refused(ev, ev.txid, "fill", "OFFER_NOT_OPEN", `offer ${o.status}`);
817
808
  return;
818
809
  }
819
810
  if (o.taker && who !== o.taker) {
820
- note(ev, ev.txid, "fill", false, "taker-bound offer");
811
+ refused(ev, ev.txid, "fill", "FILL_NOT_TAKER", "taker-bound offer");
821
812
  return;
822
813
  }
823
814
  const blk = openFillReject(o, ev.height, who, ev.proposalId);
824
815
  if (blk) {
825
- note(ev, ev.txid, "fill", false, blk);
816
+ refused(ev, ev.txid, "fill", blk.code, blk.note);
826
817
  return;
827
818
  }
828
819
  const pt = ev.paidTo ?? {};
@@ -833,13 +824,13 @@ function applyPartialFill(c, ev, o, who) {
833
824
  const minV = BigInt(o.min);
834
825
  const effMin = remaining < minV ? remaining : minV;
835
826
  if (X < effMin) {
836
- note(ev, ev.txid, "fill", false, "payment below offer min");
827
+ refused(ev, ev.txid, "fill", "FILL_BELOW_MIN", "payment below offer min");
837
828
  return;
838
829
  }
839
830
  const x = X < remaining ? X : remaining;
840
831
  const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
841
832
  if (ptAmt(pt[TREASURY_ADDR]) < fee) {
842
- note(ev, ev.txid, "fill", false, "protocol fee unpaid");
833
+ refused(ev, ev.txid, "fill", "TREASURY_FEE_UNPAID", "protocol fee unpaid");
843
834
  return;
844
835
  }
845
836
  const giveTotal = BigInt(o.give.amount);
@@ -848,12 +839,12 @@ function applyPartialFill(c, ev, o, who) {
848
839
  const newDelivered = giveTotal * newPaid / want;
849
840
  const out = newDelivered - deliveredSoFar;
850
841
  if (out === 0n) {
851
- note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
842
+ refused(ev, ev.txid, "fill", "FILL_ZERO_DELIVERY", "fill too small to deliver any tokens");
852
843
  return;
853
844
  }
854
845
  const lock = offerLock.get(o.id);
855
846
  if (lock === void 0) {
856
- note(ev, ev.txid, "fill", false, "offer lock missing");
847
+ refused(ev, ev.txid, "fill", "INVARIANT_BROKEN", "offer lock missing");
857
848
  return;
858
849
  }
859
850
  bal(o.give.ticker, o.seller).locked -= out;
@@ -870,21 +861,21 @@ function applyPartialFill(c, ev, o, who) {
870
861
  offerLock.delete(o.id);
871
862
  markBidDone(o, who);
872
863
  }
873
- note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
864
+ applied(ev, ev.txid, "fill", "FILL_PARTIAL", `partial ${x}/${want}`);
874
865
  }
875
866
  function applyFill(c, ev, o, who) {
876
- const { names, offerLock, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
867
+ const { names, offerLock, applied, refused, setOfferStatus, markBidDone, releaseGiveLock } = c;
877
868
  if (o.status !== "open") {
878
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
869
+ refused(ev, ev.txid, "fill", "OFFER_NOT_OPEN", `offer ${o.status}`);
879
870
  return;
880
871
  }
881
872
  if (o.taker && who !== o.taker) {
882
- note(ev, ev.txid, "fill", false, "taker-bound offer");
873
+ refused(ev, ev.txid, "fill", "FILL_NOT_TAKER", "taker-bound offer");
883
874
  return;
884
875
  }
885
876
  const blk = openFillReject(o, ev.height, who, ev.proposalId);
886
877
  if (blk) {
887
- note(ev, ev.txid, "fill", false, blk);
878
+ refused(ev, ev.txid, "fill", blk.code, blk.note);
888
879
  return;
889
880
  }
890
881
  const pt = ev.paidTo ?? {};
@@ -899,25 +890,25 @@ function applyFill(c, ev, o, who) {
899
890
  if (rebate > 0n) addNeed(o.seller, rebate);
900
891
  let unpaid;
901
892
  for (const [addr, amt] of need) if (ptAmt(pt[addr]) < amt) {
902
- unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
893
+ unpaid = addr === TREASURY_ADDR ? { code: "TREASURY_FEE_UNPAID", note: "protocol fee unpaid" } : rebate > 0n && addr === o.seller ? { code: "FILL_REBATE_UNPAID", note: "maker rebate unpaid (v1.6)" } : { code: "FILL_UNDERPAID", note: "payment below want.value" };
903
894
  break;
904
895
  }
905
896
  if (unpaid) {
906
- note(ev, ev.txid, "fill", false, unpaid);
897
+ refused(ev, ev.txid, "fill", unpaid.code, unpaid.note);
907
898
  return;
908
899
  }
909
900
  const paid = ptAmt(pt[o.want.payto]);
910
901
  if (isNameGive(o.give)) {
911
902
  const n = names.get(o.give.name);
912
903
  if (!n) {
913
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
904
+ refused(ev, ev.txid, "fill", "INVARIANT_BROKEN", "name vanished (consensus violation)");
914
905
  return;
915
906
  }
916
- deliverNameToBuyer(n, who, ev);
907
+ deliverNameToBuyer(c, o.give.name, n, who, ev);
917
908
  } else {
918
909
  const amt = offerLock.get(o.id);
919
910
  if (amt === void 0) {
920
- note(ev, ev.txid, "fill", false, "offer lock missing");
911
+ refused(ev, ev.txid, "fill", "INVARIANT_BROKEN", "offer lock missing");
921
912
  return;
922
913
  }
923
914
  releaseGiveLock(o, who, amt);
@@ -926,41 +917,464 @@ function applyFill(c, ev, o, who) {
926
917
  setOfferStatus(o, "filled");
927
918
  o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
928
919
  markBidDone(o, who);
929
- note(ev, ev.txid, "fill", true);
920
+ applied(ev, ev.txid, "fill", "FILL_APPLIED");
930
921
  }
931
922
  function applyLegacyClaim(c, ev, o, who) {
932
- const { note, liveHoldCount } = c;
923
+ const { applied, refused, store } = c;
933
924
  if (ev.height >= V28_HEIGHT) {
934
- note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
925
+ refused(ev, ev.txid, "claim", "CLAIM_LEGACY_SUNSET", "v2.8: claims are fclaim proposals now");
935
926
  return;
936
927
  }
937
928
  if (o.status !== "open") {
938
- note(ev, ev.txid, "claim", false, `offer ${o.status}`);
929
+ refused(ev, ev.txid, "claim", "OFFER_NOT_OPEN", `offer ${o.status}`);
939
930
  return;
940
931
  }
941
932
  if (o.taker) {
942
- note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
933
+ refused(ev, ev.txid, "claim", "CLAIM_TAKER_BOUND", "taker-bound offer needs no claim");
943
934
  return;
944
935
  }
945
936
  if (isTokenWant(o.want)) {
946
- note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
937
+ refused(ev, ev.txid, "claim", "CLAIM_TOKEN_PRICED", "claims are for CSD-priced offers (token offers are no-op-safe)");
947
938
  return;
948
939
  }
949
940
  if (claimHeld(o, ev.height)) {
950
- note(ev, ev.txid, "claim", false, "offer already claimed (hold live)");
941
+ refused(ev, ev.txid, "claim", "CLAIM_HOLD_LIVE", "offer already claimed (hold live)");
951
942
  return;
952
943
  }
953
944
  if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + claimGrace(o) + CLAIM_COOLDOWN_BLOCKS) {
954
- note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
945
+ refused(ev, ev.txid, "claim", "CLAIM_COOLDOWN", "claim cooldown (you just held this offer)");
955
946
  return;
956
947
  }
957
- if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
958
- note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
948
+ if (store.liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
949
+ refused(ev, ev.txid, "claim", "CAP_LIVE_HOLDS", `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
959
950
  return;
960
951
  }
961
- o.claimedBy = who;
962
- o.claimUntilHeight = ev.height + claimWindowAt(ev.height);
963
- note(ev, ev.txid, "claim", true);
952
+ store.setClaim(o, who, ev.height + claimWindowAt(ev.height));
953
+ applied(ev, ev.txid, "claim", "CLAIM_GRANTED");
954
+ }
955
+
956
+ // src/resolve/minqueue.ts
957
+ var MinQueue = class {
958
+ keys = [];
959
+ seqs = [];
960
+ ids = [];
961
+ pushed = 0;
962
+ get size() {
963
+ return this.ids.length;
964
+ }
965
+ /** Adds an entry. A NaN key is not stored: no comparison with NaN is true, so such an item is never due. */
966
+ push(key, id) {
967
+ if (key !== key) return;
968
+ let i = this.ids.length;
969
+ this.keys.push(key);
970
+ this.seqs.push(++this.pushed);
971
+ this.ids.push(id);
972
+ while (i > 0) {
973
+ const p = i - 1 >> 1;
974
+ if (!this.less(i, p)) break;
975
+ this.swap(i, p);
976
+ i = p;
977
+ }
978
+ }
979
+ /** Removes every entry whose key is below `limit` and appends its id to `out`, smallest key first. */
980
+ popBelow(limit, out) {
981
+ while (this.ids.length > 0 && this.keys[0] < limit) {
982
+ out.push(this.ids[0]);
983
+ this.swap(0, this.ids.length - 1);
984
+ this.keys.pop();
985
+ this.seqs.pop();
986
+ this.ids.pop();
987
+ let i = 0;
988
+ for (; ; ) {
989
+ const l = 2 * i + 1, r = l + 1;
990
+ let m = i;
991
+ if (l < this.ids.length && this.less(l, m)) m = l;
992
+ if (r < this.ids.length && this.less(r, m)) m = r;
993
+ if (m === i) break;
994
+ this.swap(i, m);
995
+ i = m;
996
+ }
997
+ }
998
+ }
999
+ /** Every stored (key, id), in no particular order: for the replay's check mode. */
1000
+ *entries() {
1001
+ for (let i = 0; i < this.ids.length; i++) yield [this.keys[i], this.ids[i]];
1002
+ }
1003
+ less(i, j) {
1004
+ const a = this.keys[i], b = this.keys[j];
1005
+ return a < b || a === b && this.seqs[i] < this.seqs[j];
1006
+ }
1007
+ swap(i, j) {
1008
+ const k = this.keys[i], s = this.seqs[i], d = this.ids[i];
1009
+ this.keys[i] = this.keys[j];
1010
+ this.seqs[i] = this.seqs[j];
1011
+ this.ids[i] = this.ids[j];
1012
+ this.keys[j] = k;
1013
+ this.seqs[j] = s;
1014
+ this.ids[j] = d;
1015
+ }
1016
+ };
1017
+
1018
+ // src/resolve/store.ts
1019
+ function createStore(opts = {}) {
1020
+ const check = opts.check === true;
1021
+ const checkEvery = Math.max(1, Math.floor(opts.checkEvery ?? 1));
1022
+ const stats = opts.stats ?? { visits: 0 };
1023
+ const offers = /* @__PURE__ */ new Map();
1024
+ const bids = /* @__PURE__ */ new Map();
1025
+ const names = /* @__PURE__ */ new Map();
1026
+ const recaptures = /* @__PURE__ */ new Map();
1027
+ const openOffers = /* @__PURE__ */ new Set();
1028
+ const openBids = /* @__PURE__ */ new Set();
1029
+ const pendingNames = /* @__PURE__ */ new Set();
1030
+ let stamp = 0;
1031
+ const openOfferAt = /* @__PURE__ */ new Map(), openBidAt = /* @__PURE__ */ new Map();
1032
+ const pendingAt = /* @__PURE__ */ new Map(), recaptureAt = /* @__PURE__ */ new Map();
1033
+ const offerAt = /* @__PURE__ */ new Map();
1034
+ const offerQ = new MinQueue(), bidQ = new MinQueue(), nameQ = new MinQueue(), recaptureQ = new MinQueue();
1035
+ let maxSweep = -Infinity;
1036
+ const pendingBy = /* @__PURE__ */ new Map(), recaptureBy = /* @__PURE__ */ new Map();
1037
+ const holdsBy = /* @__PURE__ */ new Map();
1038
+ let maxHoldRead = -Infinity;
1039
+ const bySeller = /* @__PURE__ */ new Map(), bySellerAsset = /* @__PURE__ */ new Map(), byName = /* @__PURE__ */ new Map();
1040
+ const assetKey = (seller, g) => `${seller}\0${isNameGive(g) ? `n\0${g.name}` : `t\0${g.ticker}`}`;
1041
+ const addTo = (m, k, v) => {
1042
+ let s = m.get(k);
1043
+ if (!s) {
1044
+ s = /* @__PURE__ */ new Set();
1045
+ m.set(k, s);
1046
+ }
1047
+ s.add(v);
1048
+ };
1049
+ const regimeHeight = () => maxSweep >= V21_HEIGHT ? V21_HEIGHT : V21_HEIGHT - 1;
1050
+ const addOffer = (o) => {
1051
+ if (!offers.has(o.id)) offerAt.set(o.id, ++stamp);
1052
+ offers.set(o.id, o);
1053
+ if (!openOffers.has(o.id)) {
1054
+ openOffers.add(o.id);
1055
+ openOfferAt.set(o.id, ++stamp);
1056
+ }
1057
+ offerQ.push(effExpiry(o, regimeHeight()), o.id);
1058
+ if (!(o.taker !== void 0 && o.height >= V28_HEIGHT)) {
1059
+ addTo(bySeller, o.seller, o.id);
1060
+ addTo(bySellerAsset, assetKey(o.seller, o.give), o.id);
1061
+ }
1062
+ if (isNameGive(o.give)) addTo(byName, o.give.name, o.id);
1063
+ };
1064
+ const setClaim = (o, who, untilHeight, txid) => {
1065
+ const m = o;
1066
+ m.claimedBy = who;
1067
+ m.claimUntilHeight = untilHeight;
1068
+ if (txid !== void 0) m.claimTxid = txid;
1069
+ addTo(holdsBy, who, o.id);
1070
+ };
1071
+ const setOfferStatus = (o, status) => {
1072
+ o.status = status;
1073
+ openOffers.delete(o.id);
1074
+ };
1075
+ const addBid = (b) => {
1076
+ bids.set(b.id, b);
1077
+ if (!openBids.has(b.id)) {
1078
+ openBids.add(b.id);
1079
+ openBidAt.set(b.id, ++stamp);
1080
+ }
1081
+ bidQ.push(effExpiry(b, regimeHeight()), b.id);
1082
+ };
1083
+ const setBidStatus = (b, status) => {
1084
+ b.status = status;
1085
+ openBids.delete(b.id);
1086
+ };
1087
+ const putName = (nm, n) => {
1088
+ names.set(nm, n);
1089
+ if (n.pending) {
1090
+ if (!pendingNames.has(nm)) {
1091
+ pendingNames.add(nm);
1092
+ pendingAt.set(nm, ++stamp);
1093
+ }
1094
+ if (n.finalizeBy !== void 0) nameQ.push(n.finalizeBy, nm);
1095
+ addTo(pendingBy, n.owner, nm);
1096
+ } else pendingNames.delete(nm);
1097
+ };
1098
+ const setNameOwner = (nm, n, owner) => {
1099
+ n.owner = owner;
1100
+ if (n.pending) addTo(pendingBy, owner, nm);
1101
+ };
1102
+ const finalizeName = (nm, n) => {
1103
+ delete n.pending;
1104
+ delete n.finalizeBy;
1105
+ pendingNames.delete(nm);
1106
+ };
1107
+ const putRecapture = (nm, r) => {
1108
+ if (!recaptures.has(nm)) recaptureAt.set(nm, ++stamp);
1109
+ recaptures.set(nm, r);
1110
+ recaptureQ.push(r.finalizeBy, nm);
1111
+ addTo(recaptureBy, r.owner, nm);
1112
+ };
1113
+ const deleteRecapture = (nm) => {
1114
+ recaptures.delete(nm);
1115
+ };
1116
+ const walkSweep = (height, expireOffer) => {
1117
+ const ep = (0, import_csd_codec.epochOf)(height);
1118
+ stats.visits += openOffers.size + openBids.size + pendingNames.size + recaptures.size;
1119
+ for (const id of [...openOffers]) {
1120
+ const o = offers.get(id);
1121
+ if (!o || o.status !== "open") {
1122
+ openOffers.delete(id);
1123
+ continue;
1124
+ }
1125
+ if (ep > effExpiry(o, height)) {
1126
+ expireOffer(o);
1127
+ setOfferStatus(o, "expired");
1128
+ }
1129
+ }
1130
+ for (const id of [...openBids]) {
1131
+ const b = bids.get(id);
1132
+ if (!b || b.status !== "open") {
1133
+ openBids.delete(id);
1134
+ continue;
1135
+ }
1136
+ if (ep > effExpiry(b, height)) setBidStatus(b, "expired");
1137
+ }
1138
+ for (const nm of [...pendingNames]) {
1139
+ const n = names.get(nm);
1140
+ if (!n || !n.pending) {
1141
+ pendingNames.delete(nm);
1142
+ continue;
1143
+ }
1144
+ if (n.finalizeBy !== void 0 && height > n.finalizeBy) {
1145
+ names.delete(nm);
1146
+ pendingNames.delete(nm);
1147
+ }
1148
+ }
1149
+ for (const nm of [...recaptures.keys()]) {
1150
+ const r = recaptures.get(nm);
1151
+ if (height > r.finalizeBy) recaptures.delete(nm);
1152
+ }
1153
+ };
1154
+ const offerDue = (o, height) => !!o && o.status === "open" && (0, import_csd_codec.epochOf)(height) > effExpiry(o, height);
1155
+ const bidDue = (b, height) => !!b && b.status === "open" && (0, import_csd_codec.epochOf)(height) > effExpiry(b, height);
1156
+ const nameDue = (n, height) => !!n && !!n.pending && n.finalizeBy !== void 0 && height > n.finalizeBy;
1157
+ const walkDue = (height) => ({
1158
+ offers: [...openOffers].filter((id) => offerDue(offers.get(id), height)),
1159
+ bids: [...openBids].filter((id) => bidDue(bids.get(id), height)),
1160
+ names: [...pendingNames].filter((nm) => nameDue(names.get(nm), height)),
1161
+ recaptures: [...recaptures].filter(([, r]) => height > r.finalizeBy).map(([nm]) => nm)
1162
+ });
1163
+ const walkPending = (who, except, height) => {
1164
+ let k = 0;
1165
+ for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== except && n.finalizeBy !== void 0 && height <= n.finalizeBy) k++;
1166
+ return k;
1167
+ };
1168
+ const walkRecaptures = (who, except, height) => {
1169
+ let k = 0;
1170
+ for (const [nm, r] of recaptures) if (r.owner === who && nm !== except && height <= r.finalizeBy) k++;
1171
+ return k;
1172
+ };
1173
+ const holdLive = (x, who, height) => x.claimedBy === who && claimHeld(x, height) && (height < V29_HEIGHT || x.status === "open");
1174
+ const walkHolds = (who, height) => {
1175
+ let k = 0;
1176
+ for (const x of offers.values()) if (holdLive(x, who, height)) k++;
1177
+ return k;
1178
+ };
1179
+ const ocancelMatch = (o, who, ticker, name) => o.status === "open" && o.seller === who && !(o.taker !== void 0 && o.height >= V28_HEIGHT) && !(ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === ticker)) && !(name !== void 0 && !(isNameGive(o.give) && o.give.name === name));
1180
+ const voidMatch = (o, name, height) => o.status === "open" && isNameGive(o.give) && o.give.name === name && !(height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, height));
1181
+ const same = (what, index, walk) => {
1182
+ if (JSON.stringify(index) !== JSON.stringify(walk)) throw new Error(`replay index check: ${what}: index ${JSON.stringify(index)}, walk ${JSON.stringify(walk)}`);
1183
+ };
1184
+ const popDue = (q, limit, at, isDue) => {
1185
+ const popped = [];
1186
+ q.popBelow(limit, popped);
1187
+ stats.visits += popped.length;
1188
+ return [...new Set(popped)].filter(isDue).sort((a, b) => at.get(a) - at.get(b));
1189
+ };
1190
+ const sweep = (height, expireOffer) => {
1191
+ if (!(height >= maxSweep)) {
1192
+ walkSweep(height, expireOffer);
1193
+ return;
1194
+ }
1195
+ if (!(maxSweep >= V21_HEIGHT) && height >= V21_HEIGHT) {
1196
+ stats.visits += openOffers.size + openBids.size;
1197
+ for (const id of openOffers) {
1198
+ const o = offers.get(id);
1199
+ if (o && o.status === "open" && !(o.height >= V22_HEIGHT)) offerQ.push(effExpiry(o, V21_HEIGHT), id);
1200
+ }
1201
+ for (const id of openBids) {
1202
+ const b = bids.get(id);
1203
+ if (b && b.status === "open" && !(b.height >= V22_HEIGHT)) bidQ.push(effExpiry(b, V21_HEIGHT), id);
1204
+ }
1205
+ }
1206
+ maxSweep = height;
1207
+ const ep = (0, import_csd_codec.epochOf)(height);
1208
+ const want = check ? walkDue(height) : void 0;
1209
+ const dueOffers = popDue(offerQ, ep, openOfferAt, (id) => openOffers.has(id) && offerDue(offers.get(id), height));
1210
+ const dueBids = popDue(bidQ, ep, openBidAt, (id) => openBids.has(id) && bidDue(bids.get(id), height));
1211
+ const dueNames = popDue(nameQ, height, pendingAt, (nm) => pendingNames.has(nm) && nameDue(names.get(nm), height));
1212
+ const dueRecaptures = popDue(recaptureQ, height, recaptureAt, (nm) => {
1213
+ const r = recaptures.get(nm);
1214
+ return !!r && height > r.finalizeBy;
1215
+ });
1216
+ if (want) {
1217
+ same(`expired offers at ${height}`, dueOffers, want.offers);
1218
+ same(`expired bids at ${height}`, dueBids, want.bids);
1219
+ same(`expired reservations at ${height}`, dueNames, want.names);
1220
+ same(`expired recaptures at ${height}`, dueRecaptures, want.recaptures);
1221
+ }
1222
+ for (const id of dueOffers) {
1223
+ const o = offers.get(id);
1224
+ expireOffer(o);
1225
+ setOfferStatus(o, "expired");
1226
+ }
1227
+ for (const id of dueBids) setBidStatus(bids.get(id), "expired");
1228
+ for (const nm of dueNames) {
1229
+ names.delete(nm);
1230
+ pendingNames.delete(nm);
1231
+ }
1232
+ for (const nm of dueRecaptures) recaptures.delete(nm);
1233
+ };
1234
+ const pendingCount = (who, except, height) => {
1235
+ let k = 0;
1236
+ const set = pendingBy.get(who);
1237
+ if (set) for (const nm of set) {
1238
+ stats.visits++;
1239
+ const n = names.get(nm);
1240
+ if (!n || !n.pending || n.owner !== who) {
1241
+ set.delete(nm);
1242
+ continue;
1243
+ }
1244
+ if (nm !== except && n.finalizeBy !== void 0 && height <= n.finalizeBy) k++;
1245
+ }
1246
+ if (check) same(`reservations of ${who} at ${height}`, k, walkPending(who, except, height));
1247
+ return k;
1248
+ };
1249
+ const recaptureCount = (who, except, height) => {
1250
+ let k = 0;
1251
+ const set = recaptureBy.get(who);
1252
+ if (set) for (const nm of set) {
1253
+ stats.visits++;
1254
+ const r = recaptures.get(nm);
1255
+ if (!r || r.owner !== who) {
1256
+ set.delete(nm);
1257
+ continue;
1258
+ }
1259
+ if (nm !== except && height <= r.finalizeBy) k++;
1260
+ }
1261
+ if (check) same(`recaptures of ${who} at ${height}`, k, walkRecaptures(who, except, height));
1262
+ return k;
1263
+ };
1264
+ const liveHoldCount = (who, height) => {
1265
+ if (!(height >= maxHoldRead)) {
1266
+ stats.visits += offers.size;
1267
+ return walkHolds(who, height);
1268
+ }
1269
+ maxHoldRead = height;
1270
+ let k = 0;
1271
+ const set = holdsBy.get(who);
1272
+ if (set) for (const id of set) {
1273
+ stats.visits++;
1274
+ const x = offers.get(id);
1275
+ if (x && holdLive(x, who, height)) k++;
1276
+ else set.delete(id);
1277
+ }
1278
+ if (check) same(`live holds of ${who} at ${height}`, k, walkHolds(who, height));
1279
+ return k;
1280
+ };
1281
+ const byInsertion = (a, b) => offerAt.get(a.id) - offerAt.get(b.id);
1282
+ const ocancelTargets = (who, ticker, name) => {
1283
+ const set = ticker !== void 0 ? bySellerAsset.get(`${who}\0t\0${ticker}`) : name !== void 0 ? bySellerAsset.get(`${who}\0n\0${name}`) : bySeller.get(who);
1284
+ const out = [];
1285
+ if (set) for (const id of set) {
1286
+ stats.visits++;
1287
+ const o = offers.get(id);
1288
+ if (!o || o.status !== "open" || o.seller !== who) {
1289
+ set.delete(id);
1290
+ continue;
1291
+ }
1292
+ if (ocancelMatch(o, who, ticker, name)) out.push(o);
1293
+ }
1294
+ out.sort(byInsertion);
1295
+ if (check) same(`bulk cancel by ${who}`, out.map((o) => o.id), [...offers.values()].filter((o) => ocancelMatch(o, who, ticker, name)).map((o) => o.id));
1296
+ return out;
1297
+ };
1298
+ const voidTargets = (name, height) => {
1299
+ const set = byName.get(name);
1300
+ const out = [];
1301
+ if (set) for (const id of set) {
1302
+ stats.visits++;
1303
+ const o = offers.get(id);
1304
+ if (!o || o.status !== "open" || !isNameGive(o.give) || o.give.name !== name) {
1305
+ set.delete(id);
1306
+ continue;
1307
+ }
1308
+ if (voidMatch(o, name, height)) out.push(o);
1309
+ }
1310
+ out.sort(byInsertion);
1311
+ if (check) same(`voids of ${name} at ${height}`, out.map((o) => o.id), [...offers.values()].filter((o) => voidMatch(o, name, height)).map((o) => o.id));
1312
+ return out;
1313
+ };
1314
+ let checked = 0;
1315
+ const verify = (height, force = false) => {
1316
+ if (!check || !force && ++checked % checkEvery !== 0) return;
1317
+ const fail3 = (what) => {
1318
+ throw new Error(`replay index check: ${what}`);
1319
+ };
1320
+ const queued = (q) => new Set([...q.entries()].map(([k, id]) => `${k}\0${id}`));
1321
+ const oq = queued(offerQ), bq = queued(bidQ), nq = queued(nameQ), rq = queued(recaptureQ);
1322
+ for (const id of openOffers) {
1323
+ const o = offers.get(id);
1324
+ if (!o || o.status !== "open") fail3(`openOffers names ${id}, which is not open`);
1325
+ const k = effExpiry(o, regimeHeight());
1326
+ if (k === k && !oq.has(`${k}\0${id}`)) fail3(`open offer ${id} has no queue entry at ${k}`);
1327
+ }
1328
+ for (const id of openBids) {
1329
+ const b = bids.get(id);
1330
+ if (!b || b.status !== "open") fail3(`openBids names ${id}, which is not open`);
1331
+ const k = effExpiry(b, regimeHeight());
1332
+ if (k === k && !bq.has(`${k}\0${id}`)) fail3(`open bid ${id} has no queue entry at ${k}`);
1333
+ }
1334
+ for (const nm of pendingNames) {
1335
+ const n = names.get(nm);
1336
+ if (!n || !n.pending) fail3(`pendingNames names ${nm}, which is not pending`);
1337
+ }
1338
+ for (const [nm, n] of names) {
1339
+ if (!n.pending) continue;
1340
+ if (!pendingNames.has(nm)) fail3(`pending ${nm} is not in pendingNames`);
1341
+ if (!pendingBy.get(n.owner)?.has(nm)) fail3(`pending ${nm} is not a candidate of its owner ${n.owner}`);
1342
+ if (n.finalizeBy !== void 0 && !nq.has(`${n.finalizeBy}\0${nm}`)) fail3(`pending ${nm} has no queue entry at ${n.finalizeBy}`);
1343
+ }
1344
+ for (const [nm, r] of recaptures) {
1345
+ if (!recaptureBy.get(r.owner)?.has(nm)) fail3(`recapture ${nm} is not a candidate of its owner ${r.owner}`);
1346
+ if (!rq.has(`${r.finalizeBy}\0${nm}`)) fail3(`recapture ${nm} has no queue entry at ${r.finalizeBy}`);
1347
+ }
1348
+ for (const o of offers.values()) {
1349
+ if (o.status === "open" && !(o.taker !== void 0 && o.height >= V28_HEIGHT) && !(bySeller.get(o.seller)?.has(o.id) && bySellerAsset.get(assetKey(o.seller, o.give))?.has(o.id))) fail3(`open offer ${o.id} is not a candidate of its seller`);
1350
+ if (o.status === "open" && isNameGive(o.give) && !byName.get(o.give.name)?.has(o.id)) fail3(`open name offer ${o.id} is not a candidate of ${o.give.name}`);
1351
+ if (height >= maxHoldRead && o.claimedBy !== void 0 && holdLive(o, o.claimedBy, height) && !holdsBy.get(o.claimedBy)?.has(o.id)) fail3(`live hold on ${o.id} is not a candidate of ${o.claimedBy}`);
1352
+ }
1353
+ };
1354
+ return {
1355
+ offers,
1356
+ bids,
1357
+ names,
1358
+ recaptures,
1359
+ addOffer,
1360
+ setOfferStatus,
1361
+ setClaim,
1362
+ addBid,
1363
+ setBidStatus,
1364
+ putName,
1365
+ setNameOwner,
1366
+ finalizeName,
1367
+ putRecapture,
1368
+ deleteRecapture,
1369
+ sweep,
1370
+ pendingCount,
1371
+ recaptureCount,
1372
+ liveHoldCount,
1373
+ ocancelTargets,
1374
+ voidTargets,
1375
+ verify,
1376
+ stats
1377
+ };
964
1378
  }
965
1379
 
966
1380
  // src/resolve/names.ts
@@ -970,24 +1384,24 @@ var lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
970
1384
  var inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
971
1385
  var earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
972
1386
  function applyNcommit(c, ev, rec) {
973
- const { commits, note } = c;
1387
+ const { commits, applied, refused } = c;
974
1388
  const v11 = ev.height >= V11_HEIGHT;
975
1389
  if (!v11) {
976
- note(ev, ev.id, "ncommit", false, "before v1.1 activation");
1390
+ refused(ev, ev.id, "ncommit", "GATE_NOT_ACTIVE", "before v1.1 activation");
977
1391
  return;
978
1392
  }
979
1393
  const prev = commits.get(rec.commit);
980
1394
  if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
981
- note(ev, ev.id, "ncommit", true);
1395
+ applied(ev, ev.id, "ncommit", "NAME_COMMITTED");
982
1396
  }
983
1397
  function applyName(c, ev, rec, who, feeToTreasury) {
984
- const { commits, names, recaptures, note, putName, voidOpenNameOffers } = c;
1398
+ const { commits, names, recaptures, applied, refused, putName, voidOpenNameOffers, store } = c;
985
1399
  const v11 = ev.height >= V11_HEIGHT;
986
1400
  const v15 = ev.height >= V15_HEIGHT;
987
1401
  const v25 = ev.height >= V25_HEIGHT;
988
1402
  const v26 = ev.height >= V26_HEIGHT;
989
1403
  if (!v11) {
990
- note(ev, ev.id, "name", false, "before v1.1 activation");
1404
+ refused(ev, ev.id, "name", "GATE_NOT_ACTIVE", "before v1.1 activation");
991
1405
  return;
992
1406
  }
993
1407
  const regWindow = v25 || v26 ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
@@ -996,7 +1410,7 @@ function applyName(c, ev, rec, who, feeToTreasury) {
996
1410
  const ch = nameCommit(rec.name, rec.salt, who);
997
1411
  const cH = commits.get(ch);
998
1412
  if (cH === void 0 || cH >= ev.height || ev.height - cH > regWindow) {
999
- note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
1413
+ refused(ev, ev.id, "name", "NAME_NO_COMMIT", "no valid in-window commit for this reveal");
1000
1414
  return;
1001
1415
  }
1002
1416
  effHeight = cH;
@@ -1007,62 +1421,61 @@ function applyName(c, ev, rec, who, feeToTreasury) {
1007
1421
  if (curActive && !curActive.pending && v15 && lapsed(curActive, epClaim)) {
1008
1422
  if (v26) {
1009
1423
  if (rec.salt === void 0) {
1010
- note(ev, ev.id, "name", false, "v2.6: recapture requires a commit-reveal (salt)");
1424
+ refused(ev, ev.id, "name", "NAME_SALT_REQUIRED", "v2.6: recapture requires a commit-reveal (salt)");
1011
1425
  return;
1012
1426
  }
1013
1427
  const cr = recaptures.get(rec.name);
1014
1428
  const crActive = cr && ev.height > cr.finalizeBy ? void 0 : cr;
1015
1429
  const better2 = !crActive || earlierAnchor(effHeight, ev.pos, ev.id, crActive);
1016
1430
  if (!better2) {
1017
- note(ev, ev.id, "name", false, "recapture already reserved (earlier anchor wins)");
1431
+ refused(ev, ev.id, "name", "NAME_TAKEN_EARLIER", "recapture already reserved (earlier anchor wins)");
1018
1432
  return;
1019
1433
  }
1020
- let myR = 0;
1021
- for (const [nm, r] of recaptures) if (r.owner === who && nm !== rec.name && ev.height <= r.finalizeBy) myR++;
1022
- if (myR >= MAX_PENDING_REG) {
1023
- note(ev, ev.id, "name", false, "too many pending recaptures (max reached)");
1434
+ if (store.recaptureCount(who, rec.name, ev.height) >= MAX_PENDING_REG) {
1435
+ refused(ev, ev.id, "name", "CAP_PENDING_RECAPTURES", "too many pending recaptures (max reached)");
1024
1436
  return;
1025
1437
  }
1026
- recaptures.set(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, finalizeBy: effHeight + REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS });
1027
- note(ev, ev.id, "name", true, crActive ? "recapture reserved (displaced prior reservation)" : "recapture reserved (pending finalize)");
1438
+ store.putRecapture(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, finalizeBy: effHeight + REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS });
1439
+ if (crActive) applied(ev, ev.id, "name", "NAME_RECAPTURE_RESERVED_DISPLACED", "recapture reserved (displaced prior reservation)");
1440
+ else applied(ev, ev.id, "name", "NAME_RECAPTURE_RESERVED", "recapture reserved (pending finalize)");
1028
1441
  return;
1029
1442
  }
1030
1443
  const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(curActive) + NAME_GRACE_EPOCHS), ev.height);
1031
1444
  if (feeToTreasury < fee) {
1032
- note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
1445
+ refused(ev, ev.id, "name", "TREASURY_FEE_UNPAID", "lapsed-name claim fee unpaid (decaying premium)");
1033
1446
  return;
1034
1447
  }
1035
1448
  voidOpenNameOffers(rec.name, ev.height);
1036
1449
  putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
1037
1450
  c.feesPaid += fee;
1038
- note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
1451
+ applied(ev, ev.id, "name", "NAME_RECLAIMED", "lapsed lease re-claimed (premium)");
1039
1452
  return;
1040
1453
  }
1041
1454
  if (v25) {
1042
1455
  if (rec.salt === void 0) {
1043
- note(ev, ev.id, "name", false, "v2.5: registration requires a commit-reveal (salt)");
1456
+ refused(ev, ev.id, "name", "NAME_SALT_REQUIRED", "v2.5: registration requires a commit-reveal (salt)");
1044
1457
  return;
1045
1458
  }
1046
1459
  const better2 = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
1047
1460
  if (!better2) {
1048
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
1461
+ if (curActive?.viaFill) refused(ev, ev.id, "name", "NAME_TAKEN_PURCHASED", "name taken (purchased \u2014 not displaceable)");
1462
+ else refused(ev, ev.id, "name", "NAME_TAKEN_EARLIER", "name taken (earlier anchor wins)");
1049
1463
  return;
1050
1464
  }
1051
- let myPending = 0;
1052
- for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== rec.name && n.finalizeBy !== void 0 && ev.height <= n.finalizeBy) myPending++;
1053
- if (myPending >= MAX_PENDING_REG) {
1054
- note(ev, ev.id, "name", false, "too many pending reservations (max reached)");
1465
+ if (store.pendingCount(who, rec.name, ev.height) >= MAX_PENDING_REG) {
1466
+ refused(ev, ev.id, "name", "CAP_PENDING_RESERVATIONS", "too many pending reservations (max reached)");
1055
1467
  return;
1056
1468
  }
1057
1469
  if (curActive) {
1058
1470
  voidOpenNameOffers(rec.name, ev.height);
1059
1471
  }
1060
1472
  putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, pending: true, finalizeBy: effHeight + REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS });
1061
- note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
1473
+ if (curActive) applied(ev, ev.id, "name", "NAME_RESERVED_DISPLACED", "reserved (displaced prior reservation)");
1474
+ else applied(ev, ev.id, "name", "NAME_RESERVED", "reserved (pending finalize)");
1062
1475
  return;
1063
1476
  }
1064
1477
  if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1065
- note(ev, ev.id, "name", false, "name registration fee unpaid");
1478
+ refused(ev, ev.id, "name", "TREASURY_FEE_UNPAID", "name registration fee unpaid");
1066
1479
  return;
1067
1480
  }
1068
1481
  const cand = {
@@ -1076,7 +1489,8 @@ function applyName(c, ev, rec, who, feeToTreasury) {
1076
1489
  };
1077
1490
  const better = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
1078
1491
  if (!better) {
1079
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
1492
+ if (curActive?.viaFill) refused(ev, ev.id, "name", "NAME_TAKEN_PURCHASED", "name taken (purchased \u2014 not displaceable)");
1493
+ else refused(ev, ev.id, "name", "NAME_TAKEN_EARLIER", "name taken (earlier anchor wins)");
1080
1494
  return;
1081
1495
  }
1082
1496
  if (curActive) {
@@ -1084,189 +1498,188 @@ function applyName(c, ev, rec, who, feeToTreasury) {
1084
1498
  }
1085
1499
  putName(rec.name, cand);
1086
1500
  c.feesPaid += nameRegFee(rec.name, ev.height);
1087
- note(ev, ev.id, "name", true, curActive ? "displaced prior holder" : void 0);
1501
+ if (curActive) applied(ev, ev.id, "name", "NAME_REGISTERED_DISPLACED", "displaced prior holder");
1502
+ else applied(ev, ev.id, "name", "NAME_REGISTERED");
1088
1503
  }
1089
1504
  function applyNfinalize(c, ev, rec, who, feeToTreasury) {
1090
- const { commits, names, recaptures, pendingNames, note, putName, voidOpenNameOffers } = c;
1505
+ const { commits, names, recaptures, applied, refused, putName, voidOpenNameOffers, store } = c;
1091
1506
  const v25 = ev.height >= V25_HEIGHT;
1092
1507
  const v26 = ev.height >= V26_HEIGHT;
1093
1508
  if (!v25) {
1094
- note(ev, ev.id, "nfinalize", false, "before v2.5 activation");
1509
+ refused(ev, ev.id, "nfinalize", "GATE_NOT_ACTIVE", "before v2.5 activation");
1095
1510
  return;
1096
1511
  }
1097
1512
  const n = names.get(rec.name);
1098
1513
  if (n && n.pending && n.owner === who) {
1099
1514
  const cH = commits.get(nameCommit(rec.name, rec.salt, who));
1100
1515
  if (cH === void 0 || cH !== n.effHeight) {
1101
- note(ev, ev.id, "nfinalize", false, "salt does not match the reservation commit");
1516
+ refused(ev, ev.id, "nfinalize", "NAME_SALT_MISMATCH", "salt does not match the reservation commit");
1102
1517
  return;
1103
1518
  }
1104
1519
  if (!(ev.height > n.effHeight + REG_COMMIT_MAX_BLOCKS)) {
1105
- note(ev, ev.id, "nfinalize", false, "too early \u2014 displacement contest not yet frozen");
1520
+ refused(ev, ev.id, "nfinalize", "NAME_FINALIZE_TOO_EARLY", "too early \u2014 displacement contest not yet frozen");
1106
1521
  return;
1107
1522
  }
1108
1523
  if (n.finalizeBy !== void 0 && ev.height > n.finalizeBy) {
1109
- note(ev, ev.id, "nfinalize", false, "reservation expired");
1524
+ refused(ev, ev.id, "nfinalize", "NAME_RESERVATION_EXPIRED", "reservation expired");
1110
1525
  return;
1111
1526
  }
1112
1527
  if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1113
- note(ev, ev.id, "nfinalize", false, "name registration fee unpaid");
1528
+ refused(ev, ev.id, "nfinalize", "TREASURY_FEE_UNPAID", "name registration fee unpaid");
1114
1529
  return;
1115
1530
  }
1116
- delete n.pending;
1117
- delete n.finalizeBy;
1118
- pendingNames.delete(rec.name);
1531
+ store.finalizeName(rec.name, n);
1119
1532
  n.paidThroughEpoch = (0, import_csd_codec.epochOf)(ev.height) + NAME_TERM_EPOCHS;
1120
1533
  c.feesPaid += nameRegFee(rec.name, ev.height);
1121
- note(ev, ev.id, "nfinalize", true);
1534
+ applied(ev, ev.id, "nfinalize", "NAME_FINALIZED");
1122
1535
  return;
1123
1536
  }
1124
1537
  const r = v26 ? recaptures.get(rec.name) : void 0;
1125
1538
  if (r && r.owner === who) {
1126
1539
  const cH = commits.get(nameCommit(rec.name, rec.salt, who));
1127
1540
  if (cH === void 0 || cH !== r.effHeight) {
1128
- note(ev, ev.id, "nfinalize", false, "salt does not match the recapture commit");
1541
+ refused(ev, ev.id, "nfinalize", "NAME_SALT_MISMATCH", "salt does not match the recapture commit");
1129
1542
  return;
1130
1543
  }
1131
1544
  if (!(ev.height > r.effHeight + REG_COMMIT_MAX_BLOCKS)) {
1132
- note(ev, ev.id, "nfinalize", false, "too early \u2014 recapture contest not yet frozen");
1545
+ refused(ev, ev.id, "nfinalize", "NAME_FINALIZE_TOO_EARLY", "too early \u2014 recapture contest not yet frozen");
1133
1546
  return;
1134
1547
  }
1135
1548
  if (ev.height > r.finalizeBy) {
1136
- note(ev, ev.id, "nfinalize", false, "recapture reservation expired");
1549
+ refused(ev, ev.id, "nfinalize", "NAME_RESERVATION_EXPIRED", "recapture reservation expired");
1137
1550
  return;
1138
1551
  }
1139
1552
  const cur = names.get(rec.name);
1140
1553
  const ep = (0, import_csd_codec.epochOf)(ev.height);
1141
1554
  if (!cur || cur.pending || !lapsed(cur, ep)) {
1142
- recaptures.delete(rec.name);
1143
- note(ev, ev.id, "nfinalize", false, "name is no longer lapsed");
1555
+ store.deleteRecapture(rec.name);
1556
+ refused(ev, ev.id, "nfinalize", "NAME_NOT_LAPSED", "name is no longer lapsed");
1144
1557
  return;
1145
1558
  }
1146
1559
  const fee = expiredClaimFee(rec.name, ep - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
1147
1560
  if (feeToTreasury < fee) {
1148
- note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
1561
+ refused(ev, ev.id, "nfinalize", "TREASURY_FEE_UNPAID", "recapture premium unpaid (decaying)");
1149
1562
  return;
1150
1563
  }
1151
1564
  voidOpenNameOffers(rec.name, ev.height);
1152
1565
  putName(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
1153
- recaptures.delete(rec.name);
1566
+ store.deleteRecapture(rec.name);
1154
1567
  c.feesPaid += fee;
1155
- note(ev, ev.id, "nfinalize", true, "recapture finalized (premium)");
1568
+ applied(ev, ev.id, "nfinalize", "NAME_RECAPTURE_FINALIZED", "recapture finalized (premium)");
1156
1569
  return;
1157
1570
  }
1158
- note(ev, ev.id, "nfinalize", false, "no matching pending reservation you own");
1571
+ refused(ev, ev.id, "nfinalize", "NAME_NO_RESERVATION", "no matching pending reservation you own");
1159
1572
  }
1160
1573
  function applyNxfer(c, ev, rec, who) {
1161
- const { names, note } = c;
1574
+ const { names, applied, refused, store } = c;
1162
1575
  const v15 = ev.height >= V15_HEIGHT;
1163
1576
  const n = names.get(rec.name);
1164
1577
  if (!n || n.owner !== who) {
1165
- note(ev, ev.id, "nxfer", false, "not the name owner");
1578
+ refused(ev, ev.id, "nxfer", "NAME_NOT_OWNER", "not the name owner");
1166
1579
  return;
1167
1580
  }
1168
1581
  if (n.pending) {
1169
- note(ev, ev.id, "nxfer", false, "name reservation not yet finalized");
1582
+ refused(ev, ev.id, "nxfer", "NAME_PENDING", "name reservation not yet finalized");
1170
1583
  return;
1171
1584
  }
1172
1585
  if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1173
- note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
1586
+ refused(ev, ev.id, "nxfer", "NAME_LAPSED", "lease lapsed \u2014 claim it instead");
1174
1587
  return;
1175
1588
  }
1176
1589
  if (n.locked) {
1177
- note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
1590
+ refused(ev, ev.id, "nxfer", "NAME_LOCKED", "name is locked by an open offer");
1178
1591
  return;
1179
1592
  }
1180
- n.owner = rec.to.toLowerCase();
1593
+ store.setNameOwner(rec.name, n, rec.to.toLowerCase());
1181
1594
  n.addr = void 0;
1182
1595
  n.profile = void 0;
1183
- note(ev, ev.id, "nxfer", true);
1596
+ applied(ev, ev.id, "nxfer", "NAME_TRANSFERRED");
1184
1597
  }
1185
1598
  function applyNset(c, ev, rec, who) {
1186
- const { names, note } = c;
1599
+ const { names, applied, refused } = c;
1187
1600
  const v15 = ev.height >= V15_HEIGHT;
1188
1601
  const v23 = ev.height >= V23_HEIGHT;
1189
1602
  const n = names.get(rec.name);
1190
1603
  if (!n || n.owner !== who) {
1191
- note(ev, ev.id, "nset", false, "not the name owner");
1604
+ refused(ev, ev.id, "nset", "NAME_NOT_OWNER", "not the name owner");
1192
1605
  return;
1193
1606
  }
1194
1607
  if (n.pending) {
1195
- note(ev, ev.id, "nset", false, "name reservation not yet finalized");
1608
+ refused(ev, ev.id, "nset", "NAME_PENDING", "name reservation not yet finalized");
1196
1609
  return;
1197
1610
  }
1198
1611
  if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1199
- note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
1612
+ refused(ev, ev.id, "nset", "NAME_LAPSED", "lease lapsed \u2014 claim it instead");
1200
1613
  return;
1201
1614
  }
1202
1615
  if (v23 && rec.addr.toLowerCase() === ZERO_ADDR) n.addr = void 0;
1203
1616
  else n.addr = rec.addr.toLowerCase();
1204
- note(ev, ev.id, "nset", true);
1617
+ applied(ev, ev.id, "nset", "NAME_ADDR_SET");
1205
1618
  }
1206
1619
  function applyNprofile(c, ev, rec, who) {
1207
- const { names, note } = c;
1620
+ const { names, applied, refused } = c;
1208
1621
  const v15 = ev.height >= V15_HEIGHT;
1209
1622
  const v19 = ev.height >= V19_HEIGHT;
1210
1623
  if (!v19) {
1211
- note(ev, ev.id, "nprofile", false, "before v1.9 activation");
1624
+ refused(ev, ev.id, "nprofile", "GATE_NOT_ACTIVE", "before v1.9 activation");
1212
1625
  return;
1213
1626
  }
1214
1627
  const n = names.get(rec.name);
1215
1628
  if (!n || n.owner !== who) {
1216
- note(ev, ev.id, "nprofile", false, "not the name owner");
1629
+ refused(ev, ev.id, "nprofile", "NAME_NOT_OWNER", "not the name owner");
1217
1630
  return;
1218
1631
  }
1219
1632
  if (n.pending) {
1220
- note(ev, ev.id, "nprofile", false, "name reservation not yet finalized");
1633
+ refused(ev, ev.id, "nprofile", "NAME_PENDING", "name reservation not yet finalized");
1221
1634
  return;
1222
1635
  }
1223
1636
  if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1224
- note(ev, ev.id, "nprofile", false, "lease lapsed \u2014 claim it instead");
1637
+ refused(ev, ev.id, "nprofile", "NAME_LAPSED", "lease lapsed \u2014 claim it instead");
1225
1638
  return;
1226
1639
  }
1227
1640
  n.profile = Object.keys(rec.p).length ? { ...rec.p } : void 0;
1228
- note(ev, ev.id, "nprofile", true);
1641
+ applied(ev, ev.id, "nprofile", "NAME_PROFILE_SET");
1229
1642
  }
1230
1643
  function applyNrenew(c, ev, rec, who, feeToTreasury) {
1231
- const { names, note } = c;
1644
+ const { names, applied, refused } = c;
1232
1645
  const v15 = ev.height >= V15_HEIGHT;
1233
1646
  if (!v15) {
1234
- note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
1647
+ refused(ev, ev.id, "nrenew", "GATE_NOT_ACTIVE", "nrenew needs v1.5");
1235
1648
  return;
1236
1649
  }
1237
1650
  const n = names.get(rec.name);
1238
1651
  const ep = (0, import_csd_codec.epochOf)(ev.height);
1239
1652
  if (n && n.pending) {
1240
- note(ev, ev.id, "nrenew", false, "name reservation not yet finalized");
1653
+ refused(ev, ev.id, "nrenew", "NAME_PENDING", "name reservation not yet finalized");
1241
1654
  return;
1242
1655
  }
1243
1656
  if (!n || lapsed(n, ep)) {
1244
- note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
1657
+ refused(ev, ev.id, "nrenew", "NAME_NO_LIVE_LEASE", "no live lease (lapsed names are claimed, not renewed)");
1245
1658
  return;
1246
1659
  }
1247
1660
  if (inGrace(n, ep) && who !== n.owner) {
1248
- note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
1661
+ refused(ev, ev.id, "nrenew", "NAME_GRACE_OWNER_ONLY", "grace period: only the owner may renew");
1249
1662
  return;
1250
1663
  }
1251
1664
  if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1252
- note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
1665
+ refused(ev, ev.id, "nrenew", "TREASURY_FEE_UNPAID", "renewal fee unpaid");
1253
1666
  return;
1254
1667
  }
1255
1668
  n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
1256
1669
  c.feesPaid += nameRegFee(rec.name, ev.height);
1257
- note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
1670
+ applied(ev, ev.id, "nrenew", "NAME_RENEWED", `paid through epoch ${n.paidThroughEpoch}`);
1258
1671
  }
1259
1672
 
1260
1673
  // src/resolve/market.ts
1261
1674
  function applyOffer(c, ev, rec, who) {
1262
- const { tokens, names, offers, offerLock, openOffers, bids, bal, note } = c;
1675
+ const { tokens, names, offerLock, bids, bal, applied, refused, store } = c;
1263
1676
  const v11 = ev.height >= V11_HEIGHT;
1264
1677
  const v15 = ev.height >= V15_HEIGHT;
1265
1678
  const v16 = ev.height >= V16_HEIGHT;
1266
1679
  const wantIsToken = isTokenWant(rec.want);
1267
1680
  const refusal = offerEnvelopeRefusal(rec, who, ev.height, ev.expiresEpoch);
1268
1681
  if (refusal !== null) {
1269
- note(ev, ev.id, "offer", false, refusal);
1682
+ refused(ev, ev.id, "offer", refusal.code, refusal.note);
1270
1683
  return;
1271
1684
  }
1272
1685
  const payto = (rec.want.payto ?? who).toLowerCase();
@@ -1274,36 +1687,36 @@ function applyOffer(c, ev, rec, who) {
1274
1687
  if (isNameGive(give)) {
1275
1688
  const n = names.get(give.name);
1276
1689
  if (!n || n.owner !== who) {
1277
- note(ev, ev.id, "offer", false, "you don't own this name");
1690
+ refused(ev, ev.id, "offer", "NAME_NOT_OWNER", "you don't own this name");
1278
1691
  return;
1279
1692
  }
1280
1693
  if (n.pending) {
1281
- note(ev, ev.id, "offer", false, "name reservation not yet finalized");
1694
+ refused(ev, ev.id, "offer", "NAME_PENDING", "name reservation not yet finalized");
1282
1695
  return;
1283
1696
  }
1284
1697
  if (n.locked) {
1285
- note(ev, ev.id, "offer", false, "name already locked by another offer");
1698
+ refused(ev, ev.id, "offer", "NAME_LOCKED", "name already locked by another offer");
1286
1699
  return;
1287
1700
  }
1288
1701
  const saleEmbargo = ev.height >= V27_HEIGHT ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
1289
1702
  if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= saleEmbargo) {
1290
- note(ev, ev.id, "offer", false, "name too young to sell (must out-age the reveal window)");
1703
+ refused(ev, ev.id, "offer", "NAME_TOO_YOUNG", "name too young to sell (must out-age the reveal window)");
1291
1704
  return;
1292
1705
  }
1293
1706
  if (v15 && paidThrough(n) < ev.expiresEpoch) {
1294
- note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
1707
+ refused(ev, ev.id, "offer", "OFFER_LEASE_TOO_SHORT", "v1.5: lease ends inside the offer window (renew first)");
1295
1708
  return;
1296
1709
  }
1297
1710
  n.locked = true;
1298
1711
  } else {
1299
1712
  if (!tokens.has(give.ticker)) {
1300
- note(ev, ev.id, "offer", false, "unknown ticker");
1713
+ refused(ev, ev.id, "offer", "TOKEN_UNKNOWN", "unknown ticker");
1301
1714
  return;
1302
1715
  }
1303
1716
  const amt = parseAmount(give.amount);
1304
1717
  const s = bal(give.ticker, who);
1305
1718
  if (s.available < amt) {
1306
- note(ev, ev.id, "offer", false, "insufficient available balance");
1719
+ refused(ev, ev.id, "offer", "TOKEN_BALANCE_LOW", "insufficient available balance");
1307
1720
  return;
1308
1721
  }
1309
1722
  s.available -= amt;
@@ -1323,32 +1736,31 @@ function applyOffer(c, ev, rec, who) {
1323
1736
  height: ev.height,
1324
1737
  feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
1325
1738
  };
1326
- offers.set(ev.id, o);
1327
- openOffers.add(ev.id);
1739
+ store.addOffer(o);
1328
1740
  const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
1329
1741
  if (linked) linked.offers.push(ev.id);
1330
- note(ev, ev.id, "offer", true);
1742
+ applied(ev, ev.id, "offer", "OFFER_CREATED");
1331
1743
  }
1332
1744
  function applyBid(c, ev, rec, who) {
1333
- const { bids, openBids, note } = c;
1745
+ const { applied, refused, store } = c;
1334
1746
  const v12 = ev.height >= V12_HEIGHT;
1335
1747
  if (!v12) {
1336
- note(ev, ev.id, "bid", false, "bids need v1.2");
1748
+ refused(ev, ev.id, "bid", "GATE_NOT_ACTIVE", "bids need v1.2");
1337
1749
  return;
1338
1750
  }
1339
1751
  if (!Number.isSafeInteger(ev.expiresEpoch)) {
1340
- note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
1752
+ refused(ev, ev.id, "bid", "EXPIRY_UNSAFE", "expiresEpoch out of safe-integer range");
1341
1753
  return;
1342
1754
  }
1343
1755
  if ((0, import_csd_codec.epochOf)(ev.height) > ev.expiresEpoch) {
1344
- note(ev, ev.id, "bid", false, "already expired at anchor");
1756
+ refused(ev, ev.id, "bid", "EXPIRY_PAST", "already expired at anchor");
1345
1757
  return;
1346
1758
  }
1347
1759
  if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - (0, import_csd_codec.epochOf)(ev.height) > MAX_OFFER_EPOCHS) {
1348
- note(ev, ev.id, "bid", false, "v2.1: bid duration exceeds the max");
1760
+ refused(ev, ev.id, "bid", "EXPIRY_TOO_LONG", "v2.1: bid duration exceeds the max");
1349
1761
  return;
1350
1762
  }
1351
- bids.set(ev.id, {
1763
+ store.addBid({
1352
1764
  id: ev.id,
1353
1765
  bidder: who,
1354
1766
  want: rec.want,
@@ -1358,24 +1770,16 @@ function applyBid(c, ev, rec, who) {
1358
1770
  height: ev.height,
1359
1771
  offers: []
1360
1772
  });
1361
- openBids.add(ev.id);
1362
- note(ev, ev.id, "bid", true);
1773
+ applied(ev, ev.id, "bid", "BID_CREATED");
1363
1774
  }
1364
1775
  function applyOcancel(c, ev, rec, who) {
1365
- const { offers, note, releaseGive, setOfferStatus } = c;
1776
+ const { applied, refused, releaseGive, setOfferStatus, store } = c;
1366
1777
  const v12 = ev.height >= V12_HEIGHT;
1367
1778
  if (!v12) {
1368
- note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
1779
+ refused(ev, ev.id, "ocancel", "GATE_NOT_ACTIVE", "ocancel needs v1.2");
1369
1780
  return;
1370
1781
  }
1371
- const targets = [];
1372
- for (const o of offers.values()) {
1373
- if (o.status !== "open" || o.seller !== who) continue;
1374
- if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
1375
- if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
1376
- if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
1377
- targets.push(o);
1378
- }
1782
+ const targets = store.ocancelTargets(who, rec.ticker, rec.name);
1379
1783
  if (ev.height >= V14_HEIGHT) {
1380
1784
  c.pendingCancels.push(() => {
1381
1785
  let n = 0;
@@ -1387,100 +1791,98 @@ function applyOcancel(c, ev, rec, who) {
1387
1791
  n++;
1388
1792
  }
1389
1793
  }
1390
- note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
1794
+ applied(ev, ev.id, "ocancel", "OFFER_BULK_CANCELLED", `${n} cancelled (deferred past same-block fills)`);
1391
1795
  });
1392
1796
  } else {
1393
1797
  for (const o of targets) {
1394
1798
  releaseGive(o);
1395
1799
  setOfferStatus(o, "cancelled");
1396
1800
  }
1397
- note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
1801
+ applied(ev, ev.id, "ocancel", "OFFER_BULK_CANCELLED", `${targets.length} cancelled`);
1398
1802
  }
1399
1803
  }
1400
1804
  function applyFclaim(c, ev, rec, who) {
1401
- const { offers, fclaims, note, liveHoldCount } = c;
1805
+ const { offers, fclaims, applied, refused, store } = c;
1402
1806
  if (ev.height < V28_HEIGHT) {
1403
- note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1807
+ refused(ev, ev.id, "fclaim", "GATE_NOT_ACTIVE", "fclaim needs v2.8");
1404
1808
  return;
1405
1809
  }
1406
1810
  const target = offers.get(rec.offer);
1407
1811
  const E = ev.expiresEpoch;
1408
- const deny = (why) => {
1812
+ const deny = (code, why) => {
1409
1813
  fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1410
- note(ev, ev.id, "fclaim", false, why);
1814
+ refused(ev, ev.id, "fclaim", code, why);
1411
1815
  };
1412
1816
  if (!Number.isSafeInteger(E)) {
1413
- deny("expiresEpoch out of safe-integer range");
1817
+ deny("EXPIRY_UNSAFE", "expiresEpoch out of safe-integer range");
1414
1818
  return;
1415
1819
  }
1416
1820
  if (!target) {
1417
- deny("unknown offer");
1821
+ deny("OFFER_UNKNOWN", "unknown offer");
1418
1822
  return;
1419
1823
  }
1420
1824
  if (target.status !== "open") {
1421
- deny(`offer ${target.status}`);
1825
+ deny("OFFER_NOT_OPEN", `offer ${target.status}`);
1422
1826
  return;
1423
1827
  }
1424
1828
  if (target.taker) {
1425
- deny("taker-bound offer needs no claim");
1829
+ deny("CLAIM_TAKER_BOUND", "taker-bound offer needs no claim");
1426
1830
  return;
1427
1831
  }
1428
1832
  if (isTokenWant(target.want)) {
1429
- deny("claims are for CSD-priced offers");
1833
+ deny("CLAIM_TOKEN_PRICED", "claims are for CSD-priced offers");
1430
1834
  return;
1431
1835
  }
1432
1836
  if (E > effExpiry(target, ev.height)) {
1433
- deny("hold would outlive the offer expiry");
1837
+ deny("CLAIM_OUTLIVES_OFFER", "hold would outlive the offer expiry");
1434
1838
  return;
1435
1839
  }
1436
1840
  if (E > (0, import_csd_codec.epochOf)(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1437
- deny("hold too far ahead (anti-squat)");
1841
+ deny("CLAIM_TOO_FAR_AHEAD", "hold too far ahead (anti-squat)");
1438
1842
  return;
1439
1843
  }
1440
1844
  if ((0, import_csd_codec.epochOf)(ev.height) > E) {
1441
- deny("expiry already in the past");
1845
+ deny("EXPIRY_PAST", "expiry already in the past");
1442
1846
  return;
1443
1847
  }
1444
1848
  if (claimHeld(target, ev.height)) {
1445
- deny("offer already claimed (hold live)");
1849
+ deny("CLAIM_HOLD_LIVE", "offer already claimed (hold live)");
1446
1850
  return;
1447
1851
  }
1448
1852
  if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1449
- deny("claim cooldown (you just held this offer)");
1853
+ deny("CLAIM_COOLDOWN", "claim cooldown (you just held this offer)");
1450
1854
  return;
1451
1855
  }
1452
- if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
1453
- deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1856
+ if (store.liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
1857
+ deny("CAP_LIVE_HOLDS", `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1454
1858
  return;
1455
1859
  }
1456
- target.claimedBy = who;
1457
- target.claimUntilHeight = (E + 1) * import_csd_codec.EPOCH_LEN;
1458
- target.claimTxid = ev.id;
1860
+ store.setClaim(target, who, (E + 1) * import_csd_codec.EPOCH_LEN, ev.id);
1459
1861
  fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1460
- note(ev, ev.id, "fclaim", true);
1862
+ applied(ev, ev.id, "fclaim", "CLAIM_GRANTED");
1461
1863
  }
1462
1864
  function offerEnvelopeRefusal(rec, who, height, expiresEpoch) {
1463
1865
  const wantIsToken = isTokenWant(rec.want);
1464
- if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && height < V12_HEIGHT) return "v1.2 offer shape before activation";
1465
- if (height >= V13_HEIGHT && height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) return "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)";
1466
- if ((rec.want.payto ?? who).toLowerCase() === TREASURY_ADDR) return "payto cannot be the protocol treasury";
1467
- if (!Number.isSafeInteger(expiresEpoch)) return "expiresEpoch out of safe-integer range";
1468
- if ((0, import_csd_codec.epochOf)(height) > expiresEpoch) return "already expired at anchor";
1469
- if (height >= V21_HEIGHT && height < V22_HEIGHT && expiresEpoch - (0, import_csd_codec.epochOf)(height) > MAX_OFFER_EPOCHS) return "v2.1: offer duration exceeds the max";
1470
- if (isNameGive(rec.give) && height < V11_HEIGHT) return "name offers need v1.1";
1866
+ if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && height < V12_HEIGHT) return { code: "GATE_NOT_ACTIVE", note: "v1.2 offer shape before activation" };
1867
+ if (height >= V13_HEIGHT && height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) return { code: "OFFER_TAKER_REQUIRED", note: "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)" };
1868
+ if ((rec.want.payto ?? who).toLowerCase() === TREASURY_ADDR) return { code: "OFFER_PAYTO_TREASURY", note: "payto cannot be the protocol treasury" };
1869
+ if (!Number.isSafeInteger(expiresEpoch)) return { code: "EXPIRY_UNSAFE", note: "expiresEpoch out of safe-integer range" };
1870
+ if ((0, import_csd_codec.epochOf)(height) > expiresEpoch) return { code: "EXPIRY_PAST", note: "already expired at anchor" };
1871
+ if (height >= V21_HEIGHT && height < V22_HEIGHT && expiresEpoch - (0, import_csd_codec.epochOf)(height) > MAX_OFFER_EPOCHS) return { code: "EXPIRY_TOO_LONG", note: "v2.1: offer duration exceeds the max" };
1872
+ if (isNameGive(rec.give) && height < V11_HEIGHT) return { code: "GATE_NOT_ACTIVE", note: "name offers need v1.1" };
1471
1873
  return null;
1472
1874
  }
1473
1875
 
1474
1876
  // src/resolve/tokens.ts
1475
1877
  function applyDeploy(c, ev, rec, who, feeToTreasury) {
1476
- const { tokens, note } = c;
1878
+ const { tokens, applied, refused } = c;
1477
1879
  const v11 = ev.height >= V11_HEIGHT;
1478
1880
  if (tokens.has(rec.ticker)) {
1479
- note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
1881
+ refused(ev, ev.id, "deploy", "TOKEN_TICKER_TAKEN", "ticker taken (first anchor wins)");
1480
1882
  return;
1481
1883
  }
1482
1884
  if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
1483
- note(ev, ev.id, "deploy", false, "deploy fee unpaid");
1885
+ refused(ev, ev.id, "deploy", "TREASURY_FEE_UNPAID", "deploy fee unpaid");
1484
1886
  return;
1485
1887
  }
1486
1888
  const supply = parseAmount(rec.supply);
@@ -1492,29 +1894,29 @@ function applyDeploy(c, ev, rec, who, feeToTreasury) {
1492
1894
  meta: { ticker: rec.ticker, deployId: ev.id, deployer: who, name: rec.name, decimals: rec.decimals, supply: rec.supply, minted: "0", mint: rec.mint, mintLimit: rec.mintLimit, height: ev.height }
1493
1895
  });
1494
1896
  if (v11) c.feesPaid += BigInt(DEPLOY_FEE);
1495
- note(ev, ev.id, "deploy", true);
1897
+ applied(ev, ev.id, "deploy", "TOKEN_DEPLOYED");
1496
1898
  }
1497
1899
  function applyMint(c, ev, rec, who) {
1498
- const { tokens, bal, note } = c;
1900
+ const { tokens, bal, applied, refused } = c;
1499
1901
  const tok = tokens.get(rec.ticker);
1500
1902
  if (!tok) {
1501
- note(ev, ev.id, "mint", false, "unknown ticker");
1903
+ refused(ev, ev.id, "mint", "TOKEN_UNKNOWN", "unknown ticker");
1502
1904
  return;
1503
1905
  }
1504
1906
  const remaining = tok.supply - tok.minted;
1505
1907
  if (remaining <= 0n) {
1506
- note(ev, ev.id, "mint", false, "supply exhausted");
1908
+ refused(ev, ev.id, "mint", "TOKEN_SUPPLY_EXHAUSTED", "supply exhausted");
1507
1909
  return;
1508
1910
  }
1509
1911
  let credit;
1510
1912
  if (tok.meta.mint === "issuer") {
1511
1913
  if (who !== tok.meta.deployer) {
1512
- note(ev, ev.id, "mint", false, "issuer-only mint");
1914
+ refused(ev, ev.id, "mint", "TOKEN_NOT_ISSUER", "issuer-only mint");
1513
1915
  return;
1514
1916
  }
1515
1917
  const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
1516
1918
  if (req === null) {
1517
- note(ev, ev.id, "mint", false, "issuer mint requires amount");
1919
+ refused(ev, ev.id, "mint", "TOKEN_MINT_AMOUNT_REQUIRED", "issuer mint requires amount");
1518
1920
  return;
1519
1921
  }
1520
1922
  credit = req < remaining ? req : remaining;
@@ -1525,66 +1927,70 @@ function applyMint(c, ev, rec, who) {
1525
1927
  }
1526
1928
  tok.minted += credit;
1527
1929
  bal(rec.ticker, who).available += credit;
1528
- note(ev, ev.id, "mint", true, credit.toString());
1930
+ applied(ev, ev.id, "mint", "TOKEN_MINTED", credit.toString());
1529
1931
  }
1530
1932
  function applyTransfer(c, ev, rec, who) {
1531
- const { tokens, bal, note } = c;
1933
+ const { tokens, bal, applied, refused } = c;
1532
1934
  if (!tokens.has(rec.ticker)) {
1533
- note(ev, ev.id, "transfer", false, "unknown ticker");
1935
+ refused(ev, ev.id, "transfer", "TOKEN_UNKNOWN", "unknown ticker");
1534
1936
  return;
1535
1937
  }
1536
1938
  const amt = parseAmount(rec.amount);
1537
1939
  const from = bal(rec.ticker, who);
1538
1940
  if (from.available < amt) {
1539
- note(ev, ev.id, "transfer", false, "insufficient available balance");
1941
+ refused(ev, ev.id, "transfer", "TOKEN_BALANCE_LOW", "insufficient available balance");
1540
1942
  return;
1541
1943
  }
1542
1944
  from.available -= amt;
1543
1945
  bal(rec.ticker, rec.to.toLowerCase()).available += amt;
1544
- note(ev, ev.id, "transfer", true);
1946
+ applied(ev, ev.id, "transfer", "TOKEN_TRANSFERRED");
1545
1947
  }
1546
1948
  function applyTmeta(c, ev, rec, who) {
1547
- const { tokens, note } = c;
1949
+ const { tokens, applied, refused } = c;
1548
1950
  const v15 = ev.height >= V15_HEIGHT;
1549
1951
  if (!v15) {
1550
- note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
1952
+ refused(ev, ev.id, "tmeta", "GATE_NOT_ACTIVE", "tmeta needs v1.5");
1551
1953
  return;
1552
1954
  }
1553
1955
  const tok = tokens.get(rec.ticker);
1554
1956
  if (!tok) {
1555
- note(ev, ev.id, "tmeta", false, "unknown ticker");
1957
+ refused(ev, ev.id, "tmeta", "TOKEN_UNKNOWN", "unknown ticker");
1556
1958
  return;
1557
1959
  }
1558
1960
  if (who !== tok.meta.deployer) {
1559
- note(ev, ev.id, "tmeta", false, "issuer-only metadata");
1961
+ refused(ev, ev.id, "tmeta", "TOKEN_NOT_ISSUER", "issuer-only metadata");
1560
1962
  return;
1561
1963
  }
1562
1964
  tok.meta.tmeta = rec.hash;
1563
- note(ev, ev.id, "tmeta", true);
1965
+ applied(ev, ev.id, "tmeta", "TOKEN_META_SET");
1564
1966
  }
1565
1967
 
1566
1968
  // src/resolve.ts
1567
1969
  var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
1970
+ function eventId(e) {
1971
+ return e.kind === "propose" ? e.id : e.txid;
1972
+ }
1973
+ function applyOrder(a, b) {
1974
+ return a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(eventId(a), eventId(b));
1975
+ }
1568
1976
  function resolve(events, tipHeight) {
1569
- const ordered = [...events].sort(
1570
- (a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
1571
- a.kind === "propose" ? a.id : a.txid,
1572
- b.kind === "propose" ? b.id : b.txid
1573
- )
1574
- );
1575
- const eid = (e) => e.kind === "propose" ? e.id : e.txid;
1977
+ return replay(events, tipHeight);
1978
+ }
1979
+ function replay(events, tipHeight, opts = {}) {
1980
+ const ordered = [...events].sort(applyOrder);
1576
1981
  const seenIds = /* @__PURE__ */ new Set();
1577
1982
  const applied = [];
1578
1983
  for (const e of ordered) {
1579
1984
  if (e.height >= V29_HEIGHT) {
1580
- const k = eid(e);
1985
+ const k = eventId(e);
1581
1986
  if (seenIds.has(k)) continue;
1582
1987
  seenIds.add(k);
1583
1988
  }
1584
1989
  applied.push(e);
1585
1990
  }
1586
- const c = createContext();
1587
- const { tokens, balances, names, recaptures, offers, bids, fclaims, log, note, sweepExpired } = c;
1991
+ const store = createStore(opts);
1992
+ const c = createContext(store);
1993
+ const { tokens, balances, names, recaptures, offers, bids, fclaims, log, refused, sweepExpired } = c;
1588
1994
  let pendingBlock = -1;
1589
1995
  const applyPendingCancels = () => {
1590
1996
  for (const f of c.pendingCancels) f();
@@ -1597,13 +2003,15 @@ function resolve(events, tipHeight) {
1597
2003
  pendingBlock = ev.height;
1598
2004
  }
1599
2005
  sweepExpired(ev.height);
2006
+ store.verify(ev.height);
1600
2007
  const feeToTreasury = ev.kind === "propose" ? ptAmt((ev.paidTo ?? {})[TREASURY_ADDR]) : 0n;
1601
2008
  if (ev.kind === "propose") {
1602
- const rec = parseRecord(ev.uri, ev.payloadHash);
1603
- if (!rec) {
1604
- note(ev, ev.id, "invalid", false, "unparseable/non-canonical record");
2009
+ const parsed = explainRecord(ev.uri, ev.payloadHash);
2010
+ if (!parsed.ok) {
2011
+ refused(ev, ev.id, "invalid", parsed.code, "unparseable/non-canonical record");
1605
2012
  continue;
1606
2013
  }
2014
+ const rec = parsed.record;
1607
2015
  const who = ev.proposer.toLowerCase();
1608
2016
  switch (rec.t) {
1609
2017
  case "deploy":
@@ -1658,6 +2066,7 @@ function resolve(events, tipHeight) {
1658
2066
  }
1659
2067
  applyPendingCancels();
1660
2068
  sweepExpired(tipHeight + 1);
2069
+ store.verify(tipHeight + 1, true);
1661
2070
  const tokensOut = {};
1662
2071
  for (const [t, tok] of [...tokens.entries()].sort(([a], [b]) => ord(a, b))) tokensOut[t] = { ...tok.meta, minted: tok.minted.toString() };
1663
2072
  const balancesOut = {};
@@ -2065,6 +2474,176 @@ function burnedPayment(paidTo, payer, offer2) {
2065
2474
  return { treasury: read(TREASURY_ADDR), counterparty };
2066
2475
  }
2067
2476
 
2477
+ // src/events.ts
2478
+ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2479
+ var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
2480
+ var fail = (what) => {
2481
+ throw new Error(`events rule: ${what}`);
2482
+ };
2483
+ var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
2484
+ function hexOf(v, bytes, what) {
2485
+ const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
2486
+ if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
2487
+ return "0x" + h.toLowerCase();
2488
+ }
2489
+ function u64Of(v, what) {
2490
+ if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
2491
+ if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
2492
+ if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
2493
+ return fail(`${what} is not a u64`);
2494
+ }
2495
+ function u32Of(v, what) {
2496
+ return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
2497
+ }
2498
+ function checkRule(rule) {
2499
+ const d = rule?.domain;
2500
+ if (typeof d !== "string" || d === "" || !(0, import_csd_codec5.isWellFormedUtf16)(d) || d.includes("\0") || d.includes(""))
2501
+ fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
2502
+ if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
2503
+ }
2504
+ var kindOf = (app) => {
2505
+ const t = app?.type;
2506
+ return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
2507
+ };
2508
+ var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
2509
+ function paidToOf(outputs) {
2510
+ if (!Array.isArray(outputs)) fail("outputs is not a list");
2511
+ const counted = [];
2512
+ for (const o of outputs) {
2513
+ const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
2514
+ if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
2515
+ }
2516
+ return paidToFromOutputs(counted);
2517
+ }
2518
+ function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
2519
+ checkRule(rule);
2520
+ if (!isIndex(height)) fail("height is not a safe integer >= 0");
2521
+ if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
2522
+ const txid = hexOf(t.txid, 32, "txid");
2523
+ const app = t.tx?.app;
2524
+ const kind = kindOf(app);
2525
+ if (kind === "none" || height < rule.activationHeight) return null;
2526
+ if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
2527
+ if (kind === "propose" && domainOf(app) !== rule.domain) return null;
2528
+ const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
2529
+ if (kind === "propose") {
2530
+ const p = app;
2531
+ const uri = p.uri;
2532
+ if (typeof uri !== "string" || !(0, import_csd_codec5.isWellFormedUtf16)(uri)) return fail("uri is not well-formed text");
2533
+ const e = u64Of(p.expiresEpoch, "expiresEpoch");
2534
+ return {
2535
+ kind: "propose",
2536
+ id: txid,
2537
+ proposer: who,
2538
+ uri: uri.replaceAll("\0", ""),
2539
+ payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
2540
+ expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
2541
+ height,
2542
+ pos: t.pos,
2543
+ paidTo
2544
+ };
2545
+ }
2546
+ const a = app;
2547
+ return {
2548
+ kind: "attest",
2549
+ txid,
2550
+ proposalId: hexOf(a.proposalId, 32, "proposalId"),
2551
+ attester: who,
2552
+ score: u32Of(a.score, "score"),
2553
+ confidence: u32Of(a.confidence, "confidence"),
2554
+ height,
2555
+ pos: t.pos,
2556
+ paidTo
2557
+ };
2558
+ }
2559
+ function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
2560
+ checkRule(rule);
2561
+ if (!isIndex(height)) fail("height is not a safe integer >= 0");
2562
+ if (height < rule.activationHeight) return [];
2563
+ if (!Array.isArray(txs)) fail("txs is not a list");
2564
+ const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
2565
+ const read = [];
2566
+ let last = -1;
2567
+ for (const item of txs) {
2568
+ if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
2569
+ last = item.pos;
2570
+ const id = hexOf(item.txid, 32, "txid");
2571
+ if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
2572
+ ids.add(id);
2573
+ const kind = kindOf(item.tx?.app);
2574
+ if (kind === "none") continue;
2575
+ if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
2576
+ if (kind === "propose") {
2577
+ if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
2578
+ proposals.add(id);
2579
+ read.push({ item, kind, id });
2580
+ } else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
2581
+ }
2582
+ return read.filter((r) => r.kind === "propose" || !sofar.eventIds.has(r.id) && (sofar.proposeIds.has(r.proposal) || proposals.has(r.proposal))).map((r) => ({ kind: r.kind, item: r.item }));
2583
+ }
2584
+ function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
2585
+ const members = blockMembers(height, txs, sofar, rule);
2586
+ if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
2587
+ const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
2588
+ return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
2589
+ }
2590
+
2591
+ // src/hashes.ts
2592
+ var import_csd_codec6 = require("@inversealtruism/csd-codec");
2593
+ var import_sha256 = require("@noble/hashes/sha256");
2594
+ var EVENTS_HASH_START = "0x" + "00".repeat(32);
2595
+ var FINAL_DEPTH = 12;
2596
+ var fail2 = (what) => {
2597
+ throw new Error(`events hash: ${what}`);
2598
+ };
2599
+ var hex = (b) => "0x" + (0, import_csd_codec6.bytesToHex)(b);
2600
+ function bytesOf(v, re, what) {
2601
+ return typeof v === "string" && re.test(v) ? (0, import_csd_codec6.hexToBytes)(v.slice(2)) : fail2(`${what} is not 0x and lowercase hex of its length`);
2602
+ }
2603
+ function stateHash(state) {
2604
+ return hex((0, import_sha256.sha256)((0, import_csd_codec6.utf8ToBytes)(canonicalState(state))));
2605
+ }
2606
+ function rowOf(e, height) {
2607
+ const kind = e?.kind;
2608
+ if (kind !== "propose" && kind !== "attest") return fail2("an event's kind is neither propose nor attest");
2609
+ if (e.height !== height) fail2(`an event at height ${String(e.height)} is in the block at ${height}`);
2610
+ if (!Number.isInteger(e.pos) || e.pos < 0 || e.pos > 4294967295) fail2("an event's position is not an integer in [0, 2^32 - 1]");
2611
+ const p = e, a = e;
2612
+ const id = bytesOf(kind === "propose" ? p.id : a.txid, HASH_RE, "an event's transaction id");
2613
+ const author = bytesOf(kind === "propose" ? p.proposer : a.attester, ADDR_RE, "an event's author");
2614
+ const row = new Uint8Array(56);
2615
+ new DataView(row.buffer).setUint32(0, e.pos);
2616
+ row.set(id, 4);
2617
+ row.set(author, 36);
2618
+ return { pos: e.pos, row };
2619
+ }
2620
+ function eventsHashStep(prev, height, events) {
2621
+ const e = bytesOf(prev, HASH_RE, "the previous value");
2622
+ if (!Number.isSafeInteger(height) || height < 0) fail2("height is not an integer in [0, 2^53 - 1]");
2623
+ if (!Array.isArray(events)) fail2("events is not a list");
2624
+ if (events.length === 0) return prev;
2625
+ const rows = events.map((x) => rowOf(x, height)).sort((x, y) => x.pos - y.pos);
2626
+ for (let i = 1; i < rows.length; i++) if (rows[i].pos === rows[i - 1].pos) fail2(`position ${rows[i].pos} is repeated in the block`);
2627
+ const head = new Uint8Array(8);
2628
+ new DataView(head.buffer).setBigUint64(0, BigInt(height));
2629
+ const b = (0, import_sha256.sha256)((0, import_csd_codec6.concatBytes)(head, ...rows.map((r) => r.row)));
2630
+ return hex((0, import_sha256.sha256)((0, import_csd_codec6.concatBytes)(e, b)));
2631
+ }
2632
+ function eventsHash(events) {
2633
+ if (!Array.isArray(events)) fail2("events is not a list");
2634
+ let value = EVENTS_HASH_START, last = -1;
2635
+ for (let i = 0; i < events.length; ) {
2636
+ const height = events[i]?.height;
2637
+ let j = i + 1;
2638
+ while (j < events.length && events[j]?.height === height) j++;
2639
+ value = eventsHashStep(value, height, events.slice(i, j));
2640
+ if (height < last) fail2(`height ${height} follows height ${last}`);
2641
+ last = height;
2642
+ i = j;
2643
+ }
2644
+ return value;
2645
+ }
2646
+
2068
2647
  // src/fillclosed.ts
2069
2648
  var lc = (v) => String(v).toLowerCase();
2070
2649
  function myProvenFillClosedOffer(fill, fclaim2, offerEv, me, grantHeight) {
@@ -2128,17 +2707,139 @@ function provenOfferRefusal(rec, tx) {
2128
2707
  if (tx.domain !== DOMAIN) return `not a ${DOMAIN} Propose (domain ${JSON.stringify(String(tx.domain))})`;
2129
2708
  if (beforeActivation(tx.height)) return `anchored before CairnX activation (block ${ACTIVATION_HEIGHT})`;
2130
2709
  const ee = typeof tx.expiresEpoch === "bigint" ? Number(tx.expiresEpoch) : tx.expiresEpoch;
2131
- return offerEnvelopeRefusal(rec, String(tx.seller).toLowerCase(), tx.height, ee);
2710
+ return offerEnvelopeRefusal(rec, String(tx.seller).toLowerCase(), tx.height, ee)?.note ?? null;
2132
2711
  }
2133
2712
 
2713
+ // src/codes.ts
2714
+ var TABLE = {
2715
+ TOKEN_DEPLOYED: "applied",
2716
+ TOKEN_MINTED: "applied",
2717
+ TOKEN_TRANSFERRED: "applied",
2718
+ TOKEN_META_SET: "applied",
2719
+ NAME_COMMITTED: "applied",
2720
+ NAME_REGISTERED: "applied",
2721
+ NAME_REGISTERED_DISPLACED: "applied",
2722
+ NAME_RESERVED: "applied",
2723
+ NAME_RESERVED_DISPLACED: "applied",
2724
+ NAME_RECAPTURE_RESERVED: "applied",
2725
+ NAME_RECAPTURE_RESERVED_DISPLACED: "applied",
2726
+ NAME_RECLAIMED: "applied",
2727
+ NAME_FINALIZED: "applied",
2728
+ NAME_RECAPTURE_FINALIZED: "applied",
2729
+ NAME_TRANSFERRED: "applied",
2730
+ NAME_ADDR_SET: "applied",
2731
+ NAME_PROFILE_SET: "applied",
2732
+ NAME_RENEWED: "applied",
2733
+ OFFER_CREATED: "applied",
2734
+ OFFER_BULK_CANCELLED: "applied",
2735
+ OFFER_CANCELLED: "applied",
2736
+ BID_CREATED: "applied",
2737
+ BID_CANCELLED: "applied",
2738
+ FILL_APPLIED: "applied",
2739
+ FILL_PARTIAL: "applied",
2740
+ CLAIM_GRANTED: "applied",
2741
+ GATE_NOT_ACTIVE: "refused",
2742
+ CLAIM_LEGACY_SUNSET: "refused",
2743
+ TOKEN_TICKER_TAKEN: "refused",
2744
+ TOKEN_UNKNOWN: "refused",
2745
+ TOKEN_NOT_ISSUER: "refused",
2746
+ TOKEN_SUPPLY_EXHAUSTED: "refused",
2747
+ TOKEN_MINT_AMOUNT_REQUIRED: "refused",
2748
+ TOKEN_BALANCE_LOW: "refused",
2749
+ TREASURY_FEE_UNPAID: "refused",
2750
+ NAME_NO_COMMIT: "refused",
2751
+ NAME_SALT_REQUIRED: "refused",
2752
+ NAME_TAKEN_EARLIER: "refused",
2753
+ NAME_TAKEN_PURCHASED: "refused",
2754
+ NAME_SALT_MISMATCH: "refused",
2755
+ NAME_FINALIZE_TOO_EARLY: "refused",
2756
+ NAME_RESERVATION_EXPIRED: "refused",
2757
+ NAME_NOT_LAPSED: "refused",
2758
+ NAME_NO_RESERVATION: "refused",
2759
+ NAME_NOT_OWNER: "refused",
2760
+ NAME_PENDING: "refused",
2761
+ NAME_LAPSED: "refused",
2762
+ NAME_NO_LIVE_LEASE: "refused",
2763
+ NAME_GRACE_OWNER_ONLY: "refused",
2764
+ NAME_LOCKED: "refused",
2765
+ NAME_TOO_YOUNG: "refused",
2766
+ OFFER_LEASE_TOO_SHORT: "refused",
2767
+ OFFER_TAKER_REQUIRED: "refused",
2768
+ OFFER_PAYTO_TREASURY: "refused",
2769
+ OFFER_UNKNOWN: "refused",
2770
+ OFFER_NOT_OPEN: "refused",
2771
+ OFFER_NOT_SELLER: "refused",
2772
+ OFFER_FIRM_QUOTE: "refused",
2773
+ OFFER_CANCEL_HOLD_LIVE: "refused",
2774
+ OFFER_CLOSED_BEFORE_CANCEL: "refused",
2775
+ BID_NOT_BIDDER: "refused",
2776
+ BID_NOT_OPEN: "refused",
2777
+ EXPIRY_UNSAFE: "refused",
2778
+ EXPIRY_PAST: "refused",
2779
+ EXPIRY_TOO_LONG: "refused",
2780
+ FILL_FCLAIM_NOT_CURRENT: "refused",
2781
+ FILL_NOT_TAKER: "refused",
2782
+ FILL_NO_TOKEN_MARKER: "refused",
2783
+ FILL_OPEN_LANE_CLOSED: "refused",
2784
+ FILL_WRONG_TARGET: "refused",
2785
+ FILL_NO_LIVE_CLAIM: "refused",
2786
+ FILL_BELOW_MIN: "refused",
2787
+ FILL_ZERO_DELIVERY: "refused",
2788
+ FILL_UNDERPAID: "refused",
2789
+ FILL_REBATE_UNPAID: "refused",
2790
+ CLAIM_TAKER_BOUND: "refused",
2791
+ CLAIM_TOKEN_PRICED: "refused",
2792
+ CLAIM_HOLD_LIVE: "refused",
2793
+ CLAIM_COOLDOWN: "refused",
2794
+ CLAIM_OUTLIVES_OFFER: "refused",
2795
+ CLAIM_TOO_FAR_AHEAD: "refused",
2796
+ CAP_PENDING_RESERVATIONS: "refused",
2797
+ CAP_PENDING_RECAPTURES: "refused",
2798
+ CAP_LIVE_HOLDS: "refused",
2799
+ INVARIANT_BROKEN: "refused",
2800
+ RECORD_TOO_LARGE: "refused",
2801
+ RECORD_NOT_OBJECT: "refused",
2802
+ RECORD_NOT_CANONICAL: "refused",
2803
+ RECORD_HASH_MISMATCH: "refused",
2804
+ RECORD_LONE_SURROGATE: "refused",
2805
+ RECORD_NOT_V1: "refused",
2806
+ RECORD_UNKNOWN_TYPE: "refused",
2807
+ RECORD_EXTRA_KEY: "refused",
2808
+ RECORD_BAD_SHAPE: "refused",
2809
+ RECORD_SAME_TOKEN: "refused",
2810
+ RECORD_BAD_TICKER: "refused",
2811
+ RECORD_BAD_NAME: "refused",
2812
+ RECORD_NAME_RESERVED: "refused",
2813
+ RECORD_BAD_ADDRESS: "refused",
2814
+ RECORD_BAD_AMOUNT: "refused",
2815
+ RECORD_AMOUNT_TOO_LARGE: "refused",
2816
+ RECORD_AMOUNT_ZERO: "refused",
2817
+ RECORD_MIN_ABOVE_WANT: "refused",
2818
+ RECORD_BAD_HASH: "refused",
2819
+ RECORD_BAD_SALT: "refused",
2820
+ RECORD_BAD_DECIMALS: "refused",
2821
+ RECORD_BAD_MINT_MODE: "refused",
2822
+ RECORD_BAD_TEXT: "refused",
2823
+ RECORD_BAD_TS: "refused",
2824
+ RECORD_PROFILE_TOO_MANY_KEYS: "refused",
2825
+ RECORD_PROFILE_BAD_KEY: "refused",
2826
+ RECORD_PROFILE_BAD_VALUE: "refused",
2827
+ RECORD_PROFILE_VALUE_TOO_LONG: "refused"
2828
+ };
2829
+ var REASON_CODES = Object.freeze(Object.keys(TABLE));
2830
+ var REASON_CODE_OUTCOME = Object.freeze({ ...TABLE });
2831
+ var CAP_CODES = Object.freeze(["CAP_PENDING_RESERVATIONS", "CAP_PENDING_RECAPTURES", "CAP_LIVE_HOLDS"]);
2832
+
2134
2833
  // src/index.ts
2135
- var import_csd_codec5 = require("@inversealtruism/csd-codec");
2834
+ var import_csd_codec7 = require("@inversealtruism/csd-codec");
2136
2835
  // Annotate the CommonJS export names for ESM import in node:
2137
2836
  0 && (module.exports = {
2138
2837
  ACTIVATION_HEIGHT,
2139
2838
  ADDR_RE,
2140
2839
  AMOUNT_RE,
2141
2840
  BID_KEYS,
2841
+ CAIRNX_EVENT_RULE,
2842
+ CAP_CODES,
2142
2843
  CLAIM_ATTACKER_Q,
2143
2844
  CLAIM_COOLDOWN_BLOCKS,
2144
2845
  CLAIM_FILL_GRACE_BLOCKS,
@@ -2152,6 +2853,7 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2152
2853
  DEPLOY_KEYS,
2153
2854
  DOMAIN,
2154
2855
  EPOCH_LEN,
2856
+ EVENTS_HASH_START,
2155
2857
  FCLAIM_KEYS,
2156
2858
  FCLAIM_MAX_EPOCH_AHEAD,
2157
2859
  FCLAIM_MAX_HOLD_SPAN,
@@ -2161,6 +2863,7 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2161
2863
  FEE_GATE_MARGIN_BLOCKS,
2162
2864
  FILL_TIP_MARGIN,
2163
2865
  FINALIZE_TIP_MARGIN,
2866
+ FINAL_DEPTH,
2164
2867
  GAP_NEEDED,
2165
2868
  HALVING_INTERVAL,
2166
2869
  HASH_RE,
@@ -2194,11 +2897,14 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2194
2897
  PKEY,
2195
2898
  PROFILE_MAX_KEYS,
2196
2899
  PROFILE_MAX_VALUE_BYTES,
2900
+ REASON_CODES,
2901
+ REASON_CODE_OUTCOME,
2197
2902
  REBATE_BPS,
2198
2903
  REBATE_FLAT,
2199
2904
  REG_COMMIT_MAX_BLOCKS,
2200
2905
  REG_FINALIZE_GRACE_BLOCKS,
2201
2906
  RESERVED_NAMES,
2907
+ RecordError,
2202
2908
  SALT_RE,
2203
2909
  SCAN_MARGIN,
2204
2910
  SCORE_CANCEL,
@@ -2227,10 +2933,13 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2227
2933
  V28_HEIGHT,
2228
2934
  V29_HEIGHT,
2229
2935
  ZERO_ADDR,
2936
+ applyOrder,
2230
2937
  beforeActivation,
2231
2938
  bid,
2232
2939
  bindOfferTerms,
2233
2940
  bindProvenOffer,
2941
+ blockEvents,
2942
+ blockMembers,
2234
2943
  blockReward,
2235
2944
  buildFeeHeight,
2236
2945
  buildRecord,
@@ -2243,7 +2952,12 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2243
2952
  countOtherHoldsAtGrant,
2244
2953
  deploy,
2245
2954
  epochOf,
2955
+ eventId,
2956
+ eventOfTx,
2957
+ eventsHash,
2958
+ eventsHashStep,
2246
2959
  expiredClaimFee,
2960
+ explainRecord,
2247
2961
  fclaim,
2248
2962
  fclaimEpochFor,
2249
2963
  fclaimHoldEnd,
@@ -2288,6 +3002,7 @@ var import_csd_codec5 = require("@inversealtruism/csd-codec");
2288
3002
  requiredClaimDepth,
2289
3003
  requiredFillOutputs,
2290
3004
  resolve,
3005
+ stateHash,
2291
3006
  tokenMeta,
2292
3007
  tradeFee,
2293
3008
  transfer,