@inversealtruism/cairnx-core 0.1.44 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -111,7 +111,7 @@ var isNameGive = (g) => typeof g.name === "string";
111
111
  var isTokenWant = (w) => typeof w.ticker === "string";
112
112
 
113
113
  // src/records.ts
114
- import { canonicalJson, payloadHash } from "@inversealtruism/csd-codec";
114
+ import { canonicalJson, payloadHash, isWellFormedUtf16 } from "@inversealtruism/csd-codec";
115
115
  function parseAmount(s, opts = {}) {
116
116
  if (typeof s !== "string" || !AMOUNT_RE.test(s)) return null;
117
117
  const v = BigInt(s);
@@ -126,27 +126,12 @@ var isName = (n) => typeof n === "string" && NAME_RE.test(n) && !RESERVED_NAMES.
126
126
  function nameCommit(name, salt, owner) {
127
127
  return payloadHash({ t: "cairnx:name:commit:v1", name, salt, owner: owner.toLowerCase() });
128
128
  }
129
- function strWellFormed(s) {
130
- const wf = String.prototype.isWellFormed;
131
- if (typeof wf === "function") return wf.call(s);
132
- for (let i = 0; i < s.length; i++) {
133
- const c = s.charCodeAt(i);
134
- if (c >= 55296 && c <= 56319) {
135
- const n = s.charCodeAt(i + 1);
136
- if (!(n >= 56320 && n <= 57343)) return false;
137
- i++;
138
- } else if (c >= 56320 && c <= 57343) {
139
- return false;
140
- }
141
- }
142
- return true;
143
- }
144
129
  function isWellFormedDeep(v) {
145
- if (typeof v === "string") return strWellFormed(v);
130
+ if (typeof v === "string") return isWellFormedUtf16(v);
146
131
  if (Array.isArray(v)) return v.every(isWellFormedDeep);
147
132
  if (v && typeof v === "object") {
148
133
  for (const [k, val] of Object.entries(v)) {
149
- if (!strWellFormed(k)) return false;
134
+ if (!isWellFormedUtf16(k)) return false;
150
135
  if (!isWellFormedDeep(val)) return false;
151
136
  }
152
137
  }
@@ -347,29 +332,14 @@ var tokenMeta = (r) => buildRecord({ v: 1, t: "tmeta", ...r });
347
332
  var nameProfile = (r) => buildRecord({ v: 1, t: "nprofile", ...r });
348
333
  var fclaim = (r) => buildRecord({ v: 1, t: "fclaim", ...r });
349
334
 
350
- // src/resolve.ts
335
+ // src/resolve/context.ts
351
336
  function ptAmt(v) {
352
337
  return typeof v === "string" && AMOUNT_RE.test(v) ? BigInt(v) : 0n;
353
338
  }
354
- var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
355
- function resolve(events, tipHeight) {
356
- const ordered = [...events].sort(
357
- (a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
358
- a.kind === "propose" ? a.id : a.txid,
359
- b.kind === "propose" ? b.id : b.txid
360
- )
361
- );
362
- const eid = (e) => e.kind === "propose" ? e.id : e.txid;
363
- const seenIds = /* @__PURE__ */ new Set();
364
- const applied = [];
365
- for (const e of ordered) {
366
- if (e.height >= V29_HEIGHT) {
367
- const k = eid(e);
368
- if (seenIds.has(k)) continue;
369
- seenIds.add(k);
370
- }
371
- applied.push(e);
372
- }
339
+ var effExpiry = (e, height) => e.height >= V22_HEIGHT ? e.expiresEpoch : height >= V21_HEIGHT ? Math.min(e.expiresEpoch, epochOf(e.height) + MAX_OFFER_EPOCHS) : e.expiresEpoch;
340
+ var claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
341
+ var claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
342
+ function createContext() {
373
343
  const tokens = /* @__PURE__ */ new Map();
374
344
  const balances = /* @__PURE__ */ new Map();
375
345
  const names = /* @__PURE__ */ new Map();
@@ -398,13 +368,6 @@ function resolve(events, tipHeight) {
398
368
  };
399
369
  const fclaims = /* @__PURE__ */ new Map();
400
370
  const log = [];
401
- let feesPaid = 0n;
402
- let pendingCancels = [];
403
- let pendingBlock = -1;
404
- const applyPendingCancels = () => {
405
- for (const f of pendingCancels) f();
406
- pendingCancels = [];
407
- };
408
371
  const bal = (ticker, addr) => {
409
372
  let m = balances.get(ticker);
410
373
  if (!m) {
@@ -430,7 +393,6 @@ function resolve(events, tipHeight) {
430
393
  b.available += amt;
431
394
  }
432
395
  };
433
- const effExpiry = (e, height) => e.height >= V22_HEIGHT ? e.expiresEpoch : height >= V21_HEIGHT ? Math.min(e.expiresEpoch, epochOf(e.height) + MAX_OFFER_EPOCHS) : e.expiresEpoch;
434
396
  const sweepExpired = (height) => {
435
397
  const ep = epochOf(height);
436
398
  for (const id of [...openOffers]) {
@@ -468,10 +430,6 @@ function resolve(events, tipHeight) {
468
430
  if (height > r.finalizeBy) recaptures.delete(nm);
469
431
  }
470
432
  };
471
- const V15_EPOCH = epochOf(V15_HEIGHT);
472
- const paidThrough = (n) => n.paidThroughEpoch ?? V15_EPOCH + NAME_TERM_EPOCHS;
473
- const lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
474
- const inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
475
433
  const markBidDone = (o, buyer) => {
476
434
  if (!o.bid) return;
477
435
  const b = bids.get(o.bid);
@@ -484,28 +442,10 @@ function resolve(events, tipHeight) {
484
442
  setOfferStatus(o, "cancelled");
485
443
  }
486
444
  };
487
- const earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
488
- const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
489
- const claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
490
- const openFillReject = (o, height, who, targetId) => {
491
- if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
492
- if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
493
- if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
494
- if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
495
- return void 0;
496
- };
497
- const deliverNameToBuyer = (n, who, ev) => {
498
- n.owner = who;
499
- n.locked = false;
500
- n.addr = void 0;
501
- n.profile = void 0;
502
- if (ev.height >= V13_HEIGHT) {
503
- n.effHeight = ev.height;
504
- n.pos = ev.pos;
505
- n.id = ev.txid;
506
- n.height = ev.height;
507
- n.viaFill = true;
508
- }
445
+ const liveHoldCount = (who, height) => {
446
+ let liveN = 0;
447
+ for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, height) && (height < V29_HEIGHT || x.status === "open")) liveN++;
448
+ return liveN;
509
449
  };
510
450
  const releaseGiveLock = (o, who, amt) => {
511
451
  const t = o.give.ticker;
@@ -513,6 +453,970 @@ function resolve(events, tipHeight) {
513
453
  bal(t, who).available += amt;
514
454
  offerLock.delete(o.id);
515
455
  };
456
+ return {
457
+ tokens,
458
+ balances,
459
+ names,
460
+ commits,
461
+ recaptures,
462
+ offers,
463
+ offerLock,
464
+ bids,
465
+ openOffers,
466
+ openBids,
467
+ pendingNames,
468
+ fclaims,
469
+ log,
470
+ setOfferStatus,
471
+ setBidStatus,
472
+ putName,
473
+ bal,
474
+ note,
475
+ releaseGive,
476
+ sweepExpired,
477
+ markBidDone,
478
+ voidOpenNameOffers,
479
+ liveHoldCount,
480
+ releaseGiveLock,
481
+ feesPaid: 0n,
482
+ // v1.4 fill-before-cancel: at V14_HEIGHT+ a cancel/ocancel's EFFECT is deferred to the block
483
+ // boundary, so any same-block fill wins — a seller can no longer snipe a buyer's in-flight
484
+ // payment by front-running the fill with a cheap cancel (cancel applies first in consensus
485
+ // order: proposals before attests, and a paid-up cancel attest can out-pos the fill). A fill
486
+ // that lands = seller got paid = a completed sale; the late cancel then no-ops. Pre-v14 blocks
487
+ // never populate pendingCancels, so all historical replay stays byte-identical.
488
+ pendingCancels: []
489
+ };
490
+ }
491
+
492
+ // src/resolve/attest.ts
493
+ var openFillReject = (o, height, who, targetId) => {
494
+ if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
495
+ if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
496
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
497
+ if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
498
+ return void 0;
499
+ };
500
+ var deliverNameToBuyer = (n, who, ev) => {
501
+ n.owner = who;
502
+ n.locked = false;
503
+ n.addr = void 0;
504
+ n.profile = void 0;
505
+ if (ev.height >= V13_HEIGHT) {
506
+ n.effHeight = ev.height;
507
+ n.pos = ev.pos;
508
+ n.id = ev.txid;
509
+ n.height = ev.height;
510
+ n.viaFill = true;
511
+ }
512
+ };
513
+ function applyAttest(c, ev) {
514
+ const { offers, fclaims, bids, note, setBidStatus } = c;
515
+ const who = ev.attester.toLowerCase();
516
+ let o = offers.get(ev.proposalId);
517
+ if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
518
+ const fc = fclaims.get(ev.proposalId);
519
+ if (fc && fc.granted) {
520
+ const linked = offers.get(fc.offer);
521
+ if (linked && linked.claimTxid === ev.proposalId) o = linked;
522
+ }
523
+ }
524
+ if (!o) {
525
+ if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
526
+ note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
527
+ return;
528
+ }
529
+ const b = bids.get(ev.proposalId);
530
+ if (b && ev.score === SCORE_CANCEL) {
531
+ if (who !== b.bidder) {
532
+ note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
533
+ return;
534
+ }
535
+ if (b.status !== "open") {
536
+ note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
537
+ return;
538
+ }
539
+ setBidStatus(b, "cancelled");
540
+ note(ev, ev.txid, "bidcancel", true);
541
+ }
542
+ return;
543
+ }
544
+ if (ev.score === SCORE_CANCEL) applyCancel(c, ev, o, who);
545
+ else if (ev.score === SCORE_FILL && isTokenWant(o.want)) applyTokenFill(c, ev, o, who);
546
+ else if (ev.score === SCORE_FILL && o.min !== void 0 && !isNameGive(o.give)) applyPartialFill(c, ev, o, who);
547
+ else if (ev.score === SCORE_FILL) applyFill(c, ev, o, who);
548
+ else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) applyLegacyClaim(c, ev, o, who);
549
+ }
550
+ function applyCancel(c, ev, o, who) {
551
+ const { note, releaseGive, setOfferStatus } = c;
552
+ if (who !== o.seller) {
553
+ note(ev, ev.txid, "cancel", false, "only seller may cancel");
554
+ return;
555
+ }
556
+ if (o.status !== "open") {
557
+ note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
558
+ return;
559
+ }
560
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
561
+ note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
562
+ return;
563
+ }
564
+ if (ev.height >= V14_HEIGHT) {
565
+ c.pendingCancels.push(() => {
566
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
567
+ note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
568
+ return;
569
+ }
570
+ if (o.status === "open") {
571
+ releaseGive(o);
572
+ setOfferStatus(o, "cancelled");
573
+ note(ev, ev.txid, "cancel", true);
574
+ } else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
575
+ });
576
+ } else {
577
+ releaseGive(o);
578
+ setOfferStatus(o, "cancelled");
579
+ note(ev, ev.txid, "cancel", true);
580
+ }
581
+ }
582
+ function applyTokenFill(c, ev, o, who) {
583
+ const { tokens, names, offerLock, bal, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
584
+ if (o.status !== "open") {
585
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
586
+ return;
587
+ }
588
+ if (o.taker && who !== o.taker) {
589
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
590
+ return;
591
+ }
592
+ if (ev.confidence !== CONF_TOKEN_FILL) {
593
+ note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
594
+ return;
595
+ }
596
+ if (!tokens.has(o.want.ticker)) {
597
+ note(ev, ev.txid, "fill", false, "want ticker does not exist");
598
+ return;
599
+ }
600
+ const amt = BigInt(o.want.amount);
601
+ const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
602
+ const buyer = bal(o.want.ticker, who);
603
+ if (buyer.available < amt + fee) {
604
+ note(ev, ev.txid, "fill", false, "insufficient want-token balance");
605
+ return;
606
+ }
607
+ const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
608
+ if (isNameGive(o.give) && !giveName) {
609
+ note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
610
+ return;
611
+ }
612
+ const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
613
+ if (!isNameGive(o.give) && giveLock === void 0) {
614
+ note(ev, ev.txid, "fill", false, "offer lock missing");
615
+ return;
616
+ }
617
+ buyer.available -= amt + fee;
618
+ bal(o.want.ticker, o.want.payto).available += amt;
619
+ if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
620
+ if (isNameGive(o.give)) deliverNameToBuyer(giveName, who, ev);
621
+ else releaseGiveLock(o, who, giveLock);
622
+ setOfferStatus(o, "filled");
623
+ o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
624
+ markBidDone(o, who);
625
+ note(ev, ev.txid, "fill", true);
626
+ }
627
+ function applyPartialFill(c, ev, o, who) {
628
+ const { offerLock, bal, note, setOfferStatus, markBidDone } = c;
629
+ if (o.status !== "open") {
630
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
631
+ return;
632
+ }
633
+ if (o.taker && who !== o.taker) {
634
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
635
+ return;
636
+ }
637
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
638
+ if (blk) {
639
+ note(ev, ev.txid, "fill", false, blk);
640
+ return;
641
+ }
642
+ const pt = ev.paidTo ?? {};
643
+ const want = BigInt(o.want.value);
644
+ const paidSoFar = BigInt(o.paid ?? "0");
645
+ const remaining = want - paidSoFar;
646
+ const X = ptAmt(pt[o.want.payto]);
647
+ const minV = BigInt(o.min);
648
+ const effMin = remaining < minV ? remaining : minV;
649
+ if (X < effMin) {
650
+ note(ev, ev.txid, "fill", false, "payment below offer min");
651
+ return;
652
+ }
653
+ const x = X < remaining ? X : remaining;
654
+ const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
655
+ if (ptAmt(pt[TREASURY_ADDR]) < fee) {
656
+ note(ev, ev.txid, "fill", false, "protocol fee unpaid");
657
+ return;
658
+ }
659
+ const giveTotal = BigInt(o.give.amount);
660
+ const newPaid = paidSoFar + x;
661
+ const deliveredSoFar = BigInt(o.delivered ?? "0");
662
+ const newDelivered = giveTotal * newPaid / want;
663
+ const out = newDelivered - deliveredSoFar;
664
+ if (out === 0n) {
665
+ note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
666
+ return;
667
+ }
668
+ const lock = offerLock.get(o.id);
669
+ if (lock === void 0) {
670
+ note(ev, ev.txid, "fill", false, "offer lock missing");
671
+ return;
672
+ }
673
+ bal(o.give.ticker, o.seller).locked -= out;
674
+ bal(o.give.ticker, who).available += out;
675
+ offerLock.set(o.id, lock - out);
676
+ o.paid = newPaid.toString();
677
+ o.delivered = newDelivered.toString();
678
+ const entry = { buyer: who, txid: ev.txid, height: ev.height, paid: x.toString(), fee: fee.toString(), got: out.toString() };
679
+ (o.fills ??= []).push(entry);
680
+ c.feesPaid += fee;
681
+ if (newPaid === want) {
682
+ setOfferStatus(o, "filled");
683
+ o.fill = entry;
684
+ offerLock.delete(o.id);
685
+ markBidDone(o, who);
686
+ }
687
+ note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
688
+ }
689
+ function applyFill(c, ev, o, who) {
690
+ const { names, offerLock, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
691
+ if (o.status !== "open") {
692
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
693
+ return;
694
+ }
695
+ if (o.taker && who !== o.taker) {
696
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
697
+ return;
698
+ }
699
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
700
+ if (blk) {
701
+ note(ev, ev.txid, "fill", false, blk);
702
+ return;
703
+ }
704
+ const pt = ev.paidTo ?? {};
705
+ const want = BigInt(o.want.value);
706
+ const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
707
+ const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
708
+ const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
709
+ const need = /* @__PURE__ */ new Map();
710
+ const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
711
+ addNeed(o.want.payto, want);
712
+ addNeed(TREASURY_ADDR, fee);
713
+ if (rebate > 0n) addNeed(o.seller, rebate);
714
+ let unpaid;
715
+ for (const [addr, amt] of need) if (ptAmt(pt[addr]) < amt) {
716
+ unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
717
+ break;
718
+ }
719
+ if (unpaid) {
720
+ note(ev, ev.txid, "fill", false, unpaid);
721
+ return;
722
+ }
723
+ const paid = ptAmt(pt[o.want.payto]);
724
+ if (isNameGive(o.give)) {
725
+ const n = names.get(o.give.name);
726
+ if (!n) {
727
+ note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
728
+ return;
729
+ }
730
+ deliverNameToBuyer(n, who, ev);
731
+ } else {
732
+ const amt = offerLock.get(o.id);
733
+ if (amt === void 0) {
734
+ note(ev, ev.txid, "fill", false, "offer lock missing");
735
+ return;
736
+ }
737
+ releaseGiveLock(o, who, amt);
738
+ }
739
+ c.feesPaid += fee;
740
+ setOfferStatus(o, "filled");
741
+ o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
742
+ markBidDone(o, who);
743
+ note(ev, ev.txid, "fill", true);
744
+ }
745
+ function applyLegacyClaim(c, ev, o, who) {
746
+ const { note, liveHoldCount } = c;
747
+ if (ev.height >= V28_HEIGHT) {
748
+ note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
749
+ return;
750
+ }
751
+ if (o.status !== "open") {
752
+ note(ev, ev.txid, "claim", false, `offer ${o.status}`);
753
+ return;
754
+ }
755
+ if (o.taker) {
756
+ note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
757
+ return;
758
+ }
759
+ if (isTokenWant(o.want)) {
760
+ note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
761
+ return;
762
+ }
763
+ if (claimHeld(o, ev.height)) {
764
+ note(ev, ev.txid, "claim", false, "offer already claimed (hold live)");
765
+ return;
766
+ }
767
+ if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + claimGrace(o) + CLAIM_COOLDOWN_BLOCKS) {
768
+ note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
769
+ return;
770
+ }
771
+ if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
772
+ note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
773
+ return;
774
+ }
775
+ o.claimedBy = who;
776
+ o.claimUntilHeight = ev.height + claimWindowAt(ev.height);
777
+ note(ev, ev.txid, "claim", true);
778
+ }
779
+
780
+ // src/resolve/names.ts
781
+ var V15_EPOCH = epochOf(V15_HEIGHT);
782
+ var paidThrough = (n) => n.paidThroughEpoch ?? V15_EPOCH + NAME_TERM_EPOCHS;
783
+ var lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
784
+ var inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
785
+ var earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
786
+ function applyNcommit(c, ev, rec) {
787
+ const { commits, note } = c;
788
+ const v11 = ev.height >= V11_HEIGHT;
789
+ if (!v11) {
790
+ note(ev, ev.id, "ncommit", false, "before v1.1 activation");
791
+ return;
792
+ }
793
+ const prev = commits.get(rec.commit);
794
+ if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
795
+ note(ev, ev.id, "ncommit", true);
796
+ }
797
+ function applyName(c, ev, rec, who, feeToTreasury) {
798
+ const { commits, names, recaptures, note, putName, voidOpenNameOffers } = c;
799
+ const v11 = ev.height >= V11_HEIGHT;
800
+ const v15 = ev.height >= V15_HEIGHT;
801
+ const v25 = ev.height >= V25_HEIGHT;
802
+ const v26 = ev.height >= V26_HEIGHT;
803
+ if (!v11) {
804
+ note(ev, ev.id, "name", false, "before v1.1 activation");
805
+ return;
806
+ }
807
+ const regWindow = v25 || v26 ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
808
+ let effHeight = ev.height;
809
+ if (rec.salt !== void 0) {
810
+ const ch = nameCommit(rec.name, rec.salt, who);
811
+ const cH = commits.get(ch);
812
+ if (cH === void 0 || cH >= ev.height || ev.height - cH > regWindow) {
813
+ note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
814
+ return;
815
+ }
816
+ effHeight = cH;
817
+ }
818
+ const cur = names.get(rec.name);
819
+ const epClaim = epochOf(ev.height);
820
+ const curActive = cur && cur.pending && cur.finalizeBy !== void 0 && ev.height > cur.finalizeBy ? void 0 : cur;
821
+ if (curActive && !curActive.pending && v15 && lapsed(curActive, epClaim)) {
822
+ if (v26) {
823
+ if (rec.salt === void 0) {
824
+ note(ev, ev.id, "name", false, "v2.6: recapture requires a commit-reveal (salt)");
825
+ return;
826
+ }
827
+ const cr = recaptures.get(rec.name);
828
+ const crActive = cr && ev.height > cr.finalizeBy ? void 0 : cr;
829
+ const better2 = !crActive || earlierAnchor(effHeight, ev.pos, ev.id, crActive);
830
+ if (!better2) {
831
+ note(ev, ev.id, "name", false, "recapture already reserved (earlier anchor wins)");
832
+ return;
833
+ }
834
+ let myR = 0;
835
+ for (const [nm, r] of recaptures) if (r.owner === who && nm !== rec.name && ev.height <= r.finalizeBy) myR++;
836
+ if (myR >= MAX_PENDING_REG) {
837
+ note(ev, ev.id, "name", false, "too many pending recaptures (max reached)");
838
+ return;
839
+ }
840
+ 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 });
841
+ note(ev, ev.id, "name", true, crActive ? "recapture reserved (displaced prior reservation)" : "recapture reserved (pending finalize)");
842
+ return;
843
+ }
844
+ const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(curActive) + NAME_GRACE_EPOCHS), ev.height);
845
+ if (feeToTreasury < fee) {
846
+ note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
847
+ return;
848
+ }
849
+ voidOpenNameOffers(rec.name, ev.height);
850
+ putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
851
+ c.feesPaid += fee;
852
+ note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
853
+ return;
854
+ }
855
+ if (v25) {
856
+ if (rec.salt === void 0) {
857
+ note(ev, ev.id, "name", false, "v2.5: registration requires a commit-reveal (salt)");
858
+ return;
859
+ }
860
+ const better2 = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
861
+ if (!better2) {
862
+ note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
863
+ return;
864
+ }
865
+ let myPending = 0;
866
+ for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== rec.name && n.finalizeBy !== void 0 && ev.height <= n.finalizeBy) myPending++;
867
+ if (myPending >= MAX_PENDING_REG) {
868
+ note(ev, ev.id, "name", false, "too many pending reservations (max reached)");
869
+ return;
870
+ }
871
+ if (curActive) {
872
+ voidOpenNameOffers(rec.name, ev.height);
873
+ }
874
+ 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 });
875
+ note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
876
+ return;
877
+ }
878
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
879
+ note(ev, ev.id, "name", false, "name registration fee unpaid");
880
+ return;
881
+ }
882
+ const cand = {
883
+ owner: who,
884
+ effHeight,
885
+ pos: ev.pos,
886
+ id: ev.id,
887
+ height: ev.height,
888
+ locked: false,
889
+ ...v15 ? { paidThroughEpoch: epClaim + NAME_TERM_EPOCHS } : {}
890
+ };
891
+ const better = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
892
+ if (!better) {
893
+ note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
894
+ return;
895
+ }
896
+ if (curActive) {
897
+ voidOpenNameOffers(rec.name, ev.height);
898
+ }
899
+ putName(rec.name, cand);
900
+ c.feesPaid += nameRegFee(rec.name, ev.height);
901
+ note(ev, ev.id, "name", true, curActive ? "displaced prior holder" : void 0);
902
+ }
903
+ function applyNfinalize(c, ev, rec, who, feeToTreasury) {
904
+ const { commits, names, recaptures, pendingNames, note, putName, voidOpenNameOffers } = c;
905
+ const v25 = ev.height >= V25_HEIGHT;
906
+ const v26 = ev.height >= V26_HEIGHT;
907
+ if (!v25) {
908
+ note(ev, ev.id, "nfinalize", false, "before v2.5 activation");
909
+ return;
910
+ }
911
+ const n = names.get(rec.name);
912
+ if (n && n.pending && n.owner === who) {
913
+ const cH = commits.get(nameCommit(rec.name, rec.salt, who));
914
+ if (cH === void 0 || cH !== n.effHeight) {
915
+ note(ev, ev.id, "nfinalize", false, "salt does not match the reservation commit");
916
+ return;
917
+ }
918
+ if (!(ev.height > n.effHeight + REG_COMMIT_MAX_BLOCKS)) {
919
+ note(ev, ev.id, "nfinalize", false, "too early \u2014 displacement contest not yet frozen");
920
+ return;
921
+ }
922
+ if (n.finalizeBy !== void 0 && ev.height > n.finalizeBy) {
923
+ note(ev, ev.id, "nfinalize", false, "reservation expired");
924
+ return;
925
+ }
926
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
927
+ note(ev, ev.id, "nfinalize", false, "name registration fee unpaid");
928
+ return;
929
+ }
930
+ delete n.pending;
931
+ delete n.finalizeBy;
932
+ pendingNames.delete(rec.name);
933
+ n.paidThroughEpoch = epochOf(ev.height) + NAME_TERM_EPOCHS;
934
+ c.feesPaid += nameRegFee(rec.name, ev.height);
935
+ note(ev, ev.id, "nfinalize", true);
936
+ return;
937
+ }
938
+ const r = v26 ? recaptures.get(rec.name) : void 0;
939
+ if (r && r.owner === who) {
940
+ const cH = commits.get(nameCommit(rec.name, rec.salt, who));
941
+ if (cH === void 0 || cH !== r.effHeight) {
942
+ note(ev, ev.id, "nfinalize", false, "salt does not match the recapture commit");
943
+ return;
944
+ }
945
+ if (!(ev.height > r.effHeight + REG_COMMIT_MAX_BLOCKS)) {
946
+ note(ev, ev.id, "nfinalize", false, "too early \u2014 recapture contest not yet frozen");
947
+ return;
948
+ }
949
+ if (ev.height > r.finalizeBy) {
950
+ note(ev, ev.id, "nfinalize", false, "recapture reservation expired");
951
+ return;
952
+ }
953
+ const cur = names.get(rec.name);
954
+ const ep = epochOf(ev.height);
955
+ if (!cur || cur.pending || !lapsed(cur, ep)) {
956
+ recaptures.delete(rec.name);
957
+ note(ev, ev.id, "nfinalize", false, "name is no longer lapsed");
958
+ return;
959
+ }
960
+ const fee = expiredClaimFee(rec.name, ep - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
961
+ if (feeToTreasury < fee) {
962
+ note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
963
+ return;
964
+ }
965
+ voidOpenNameOffers(rec.name, ev.height);
966
+ putName(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
967
+ recaptures.delete(rec.name);
968
+ c.feesPaid += fee;
969
+ note(ev, ev.id, "nfinalize", true, "recapture finalized (premium)");
970
+ return;
971
+ }
972
+ note(ev, ev.id, "nfinalize", false, "no matching pending reservation you own");
973
+ }
974
+ function applyNxfer(c, ev, rec, who) {
975
+ const { names, note } = c;
976
+ const v15 = ev.height >= V15_HEIGHT;
977
+ const n = names.get(rec.name);
978
+ if (!n || n.owner !== who) {
979
+ note(ev, ev.id, "nxfer", false, "not the name owner");
980
+ return;
981
+ }
982
+ if (n.pending) {
983
+ note(ev, ev.id, "nxfer", false, "name reservation not yet finalized");
984
+ return;
985
+ }
986
+ if (v15 && lapsed(n, epochOf(ev.height))) {
987
+ note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
988
+ return;
989
+ }
990
+ if (n.locked) {
991
+ note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
992
+ return;
993
+ }
994
+ n.owner = rec.to.toLowerCase();
995
+ n.addr = void 0;
996
+ n.profile = void 0;
997
+ note(ev, ev.id, "nxfer", true);
998
+ }
999
+ function applyNset(c, ev, rec, who) {
1000
+ const { names, note } = c;
1001
+ const v15 = ev.height >= V15_HEIGHT;
1002
+ const v23 = ev.height >= V23_HEIGHT;
1003
+ const n = names.get(rec.name);
1004
+ if (!n || n.owner !== who) {
1005
+ note(ev, ev.id, "nset", false, "not the name owner");
1006
+ return;
1007
+ }
1008
+ if (n.pending) {
1009
+ note(ev, ev.id, "nset", false, "name reservation not yet finalized");
1010
+ return;
1011
+ }
1012
+ if (v15 && lapsed(n, epochOf(ev.height))) {
1013
+ note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
1014
+ return;
1015
+ }
1016
+ if (v23 && rec.addr.toLowerCase() === ZERO_ADDR) n.addr = void 0;
1017
+ else n.addr = rec.addr.toLowerCase();
1018
+ note(ev, ev.id, "nset", true);
1019
+ }
1020
+ function applyNprofile(c, ev, rec, who) {
1021
+ const { names, note } = c;
1022
+ const v15 = ev.height >= V15_HEIGHT;
1023
+ const v19 = ev.height >= V19_HEIGHT;
1024
+ if (!v19) {
1025
+ note(ev, ev.id, "nprofile", false, "before v1.9 activation");
1026
+ return;
1027
+ }
1028
+ const n = names.get(rec.name);
1029
+ if (!n || n.owner !== who) {
1030
+ note(ev, ev.id, "nprofile", false, "not the name owner");
1031
+ return;
1032
+ }
1033
+ if (n.pending) {
1034
+ note(ev, ev.id, "nprofile", false, "name reservation not yet finalized");
1035
+ return;
1036
+ }
1037
+ if (v15 && lapsed(n, epochOf(ev.height))) {
1038
+ note(ev, ev.id, "nprofile", false, "lease lapsed \u2014 claim it instead");
1039
+ return;
1040
+ }
1041
+ n.profile = Object.keys(rec.p).length ? { ...rec.p } : void 0;
1042
+ note(ev, ev.id, "nprofile", true);
1043
+ }
1044
+ function applyNrenew(c, ev, rec, who, feeToTreasury) {
1045
+ const { names, note } = c;
1046
+ const v15 = ev.height >= V15_HEIGHT;
1047
+ if (!v15) {
1048
+ note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
1049
+ return;
1050
+ }
1051
+ const n = names.get(rec.name);
1052
+ const ep = epochOf(ev.height);
1053
+ if (n && n.pending) {
1054
+ note(ev, ev.id, "nrenew", false, "name reservation not yet finalized");
1055
+ return;
1056
+ }
1057
+ if (!n || lapsed(n, ep)) {
1058
+ note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
1059
+ return;
1060
+ }
1061
+ if (inGrace(n, ep) && who !== n.owner) {
1062
+ note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
1063
+ return;
1064
+ }
1065
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1066
+ note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
1067
+ return;
1068
+ }
1069
+ n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
1070
+ c.feesPaid += nameRegFee(rec.name, ev.height);
1071
+ note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
1072
+ }
1073
+
1074
+ // src/resolve/market.ts
1075
+ function applyOffer(c, ev, rec, who) {
1076
+ const { tokens, names, offers, offerLock, openOffers, bids, bal, note } = c;
1077
+ const v11 = ev.height >= V11_HEIGHT;
1078
+ const v12 = ev.height >= V12_HEIGHT;
1079
+ const v15 = ev.height >= V15_HEIGHT;
1080
+ const v16 = ev.height >= V16_HEIGHT;
1081
+ const wantIsToken = isTokenWant(rec.want);
1082
+ if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && !v12) {
1083
+ note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
1084
+ return;
1085
+ }
1086
+ if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
1087
+ note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
1088
+ return;
1089
+ }
1090
+ const payto = (rec.want.payto ?? who).toLowerCase();
1091
+ if (payto === TREASURY_ADDR) {
1092
+ note(ev, ev.id, "offer", false, "payto cannot be the protocol treasury");
1093
+ return;
1094
+ }
1095
+ if (!Number.isSafeInteger(ev.expiresEpoch)) {
1096
+ note(ev, ev.id, "offer", false, "expiresEpoch out of safe-integer range");
1097
+ return;
1098
+ }
1099
+ if (epochOf(ev.height) > ev.expiresEpoch) {
1100
+ note(ev, ev.id, "offer", false, "already expired at anchor");
1101
+ return;
1102
+ }
1103
+ if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - epochOf(ev.height) > MAX_OFFER_EPOCHS) {
1104
+ note(ev, ev.id, "offer", false, "v2.1: offer duration exceeds the max");
1105
+ return;
1106
+ }
1107
+ const give = rec.give;
1108
+ if (isNameGive(give)) {
1109
+ if (!v11) {
1110
+ note(ev, ev.id, "offer", false, "name offers need v1.1");
1111
+ return;
1112
+ }
1113
+ const n = names.get(give.name);
1114
+ if (!n || n.owner !== who) {
1115
+ note(ev, ev.id, "offer", false, "you don't own this name");
1116
+ return;
1117
+ }
1118
+ if (n.pending) {
1119
+ note(ev, ev.id, "offer", false, "name reservation not yet finalized");
1120
+ return;
1121
+ }
1122
+ if (n.locked) {
1123
+ note(ev, ev.id, "offer", false, "name already locked by another offer");
1124
+ return;
1125
+ }
1126
+ const saleEmbargo = ev.height >= V27_HEIGHT ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
1127
+ if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= saleEmbargo) {
1128
+ note(ev, ev.id, "offer", false, "name too young to sell (must out-age the reveal window)");
1129
+ return;
1130
+ }
1131
+ if (v15 && paidThrough(n) < ev.expiresEpoch) {
1132
+ note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
1133
+ return;
1134
+ }
1135
+ n.locked = true;
1136
+ } else {
1137
+ if (!tokens.has(give.ticker)) {
1138
+ note(ev, ev.id, "offer", false, "unknown ticker");
1139
+ return;
1140
+ }
1141
+ const amt = parseAmount(give.amount);
1142
+ const s = bal(give.ticker, who);
1143
+ if (s.available < amt) {
1144
+ note(ev, ev.id, "offer", false, "insufficient available balance");
1145
+ return;
1146
+ }
1147
+ s.available -= amt;
1148
+ s.locked += amt;
1149
+ offerLock.set(ev.id, amt);
1150
+ }
1151
+ const o = {
1152
+ id: ev.id,
1153
+ seller: who,
1154
+ give,
1155
+ want: wantIsToken ? { ticker: rec.want.ticker, amount: rec.want.amount, payto } : { value: rec.want.value, payto },
1156
+ ...rec.taker ? { taker: rec.taker.toLowerCase() } : {},
1157
+ ...rec.min !== void 0 ? { min: rec.min, paid: "0", delivered: "0", fills: [] } : {},
1158
+ ...rec.bid !== void 0 ? { bid: rec.bid } : {},
1159
+ status: "open",
1160
+ expiresEpoch: ev.expiresEpoch,
1161
+ height: ev.height,
1162
+ feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
1163
+ };
1164
+ offers.set(ev.id, o);
1165
+ openOffers.add(ev.id);
1166
+ const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
1167
+ if (linked) linked.offers.push(ev.id);
1168
+ note(ev, ev.id, "offer", true);
1169
+ }
1170
+ function applyBid(c, ev, rec, who) {
1171
+ const { bids, openBids, note } = c;
1172
+ const v12 = ev.height >= V12_HEIGHT;
1173
+ if (!v12) {
1174
+ note(ev, ev.id, "bid", false, "bids need v1.2");
1175
+ return;
1176
+ }
1177
+ if (!Number.isSafeInteger(ev.expiresEpoch)) {
1178
+ note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
1179
+ return;
1180
+ }
1181
+ if (epochOf(ev.height) > ev.expiresEpoch) {
1182
+ note(ev, ev.id, "bid", false, "already expired at anchor");
1183
+ return;
1184
+ }
1185
+ if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - epochOf(ev.height) > MAX_OFFER_EPOCHS) {
1186
+ note(ev, ev.id, "bid", false, "v2.1: bid duration exceeds the max");
1187
+ return;
1188
+ }
1189
+ bids.set(ev.id, {
1190
+ id: ev.id,
1191
+ bidder: who,
1192
+ want: rec.want,
1193
+ give: rec.give,
1194
+ status: "open",
1195
+ expiresEpoch: ev.expiresEpoch,
1196
+ height: ev.height,
1197
+ offers: []
1198
+ });
1199
+ openBids.add(ev.id);
1200
+ note(ev, ev.id, "bid", true);
1201
+ }
1202
+ function applyOcancel(c, ev, rec, who) {
1203
+ const { offers, note, releaseGive, setOfferStatus } = c;
1204
+ const v12 = ev.height >= V12_HEIGHT;
1205
+ if (!v12) {
1206
+ note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
1207
+ return;
1208
+ }
1209
+ const targets = [];
1210
+ for (const o of offers.values()) {
1211
+ if (o.status !== "open" || o.seller !== who) continue;
1212
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
1213
+ if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
1214
+ if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
1215
+ targets.push(o);
1216
+ }
1217
+ if (ev.height >= V14_HEIGHT) {
1218
+ c.pendingCancels.push(() => {
1219
+ let n = 0;
1220
+ for (const o of targets) {
1221
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
1222
+ if (o.status === "open") {
1223
+ releaseGive(o);
1224
+ setOfferStatus(o, "cancelled");
1225
+ n++;
1226
+ }
1227
+ }
1228
+ note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
1229
+ });
1230
+ } else {
1231
+ for (const o of targets) {
1232
+ releaseGive(o);
1233
+ setOfferStatus(o, "cancelled");
1234
+ }
1235
+ note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
1236
+ }
1237
+ }
1238
+ function applyFclaim(c, ev, rec, who) {
1239
+ const { offers, fclaims, note, liveHoldCount } = c;
1240
+ if (ev.height < V28_HEIGHT) {
1241
+ note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1242
+ return;
1243
+ }
1244
+ const target = offers.get(rec.offer);
1245
+ const E = ev.expiresEpoch;
1246
+ const deny = (why) => {
1247
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1248
+ note(ev, ev.id, "fclaim", false, why);
1249
+ };
1250
+ if (!Number.isSafeInteger(E)) {
1251
+ deny("expiresEpoch out of safe-integer range");
1252
+ return;
1253
+ }
1254
+ if (!target) {
1255
+ deny("unknown offer");
1256
+ return;
1257
+ }
1258
+ if (target.status !== "open") {
1259
+ deny(`offer ${target.status}`);
1260
+ return;
1261
+ }
1262
+ if (target.taker) {
1263
+ deny("taker-bound offer needs no claim");
1264
+ return;
1265
+ }
1266
+ if (isTokenWant(target.want)) {
1267
+ deny("claims are for CSD-priced offers");
1268
+ return;
1269
+ }
1270
+ if (E > effExpiry(target, ev.height)) {
1271
+ deny("hold would outlive the offer expiry");
1272
+ return;
1273
+ }
1274
+ if (E > epochOf(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1275
+ deny("hold too far ahead (anti-squat)");
1276
+ return;
1277
+ }
1278
+ if (epochOf(ev.height) > E) {
1279
+ deny("expiry already in the past");
1280
+ return;
1281
+ }
1282
+ if (claimHeld(target, ev.height)) {
1283
+ deny("offer already claimed (hold live)");
1284
+ return;
1285
+ }
1286
+ if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1287
+ deny("claim cooldown (you just held this offer)");
1288
+ return;
1289
+ }
1290
+ if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
1291
+ deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1292
+ return;
1293
+ }
1294
+ target.claimedBy = who;
1295
+ target.claimUntilHeight = (E + 1) * EPOCH_LEN;
1296
+ target.claimTxid = ev.id;
1297
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1298
+ note(ev, ev.id, "fclaim", true);
1299
+ }
1300
+
1301
+ // src/resolve/tokens.ts
1302
+ function applyDeploy(c, ev, rec, who, feeToTreasury) {
1303
+ const { tokens, note } = c;
1304
+ const v11 = ev.height >= V11_HEIGHT;
1305
+ if (tokens.has(rec.ticker)) {
1306
+ note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
1307
+ return;
1308
+ }
1309
+ if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
1310
+ note(ev, ev.id, "deploy", false, "deploy fee unpaid");
1311
+ return;
1312
+ }
1313
+ const supply = parseAmount(rec.supply);
1314
+ const mintLimit = rec.mint === "open" ? parseAmount(rec.mintLimit) : null;
1315
+ tokens.set(rec.ticker, {
1316
+ minted: 0n,
1317
+ supply,
1318
+ mintLimit,
1319
+ 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 }
1320
+ });
1321
+ if (v11) c.feesPaid += BigInt(DEPLOY_FEE);
1322
+ note(ev, ev.id, "deploy", true);
1323
+ }
1324
+ function applyMint(c, ev, rec, who) {
1325
+ const { tokens, bal, note } = c;
1326
+ const tok = tokens.get(rec.ticker);
1327
+ if (!tok) {
1328
+ note(ev, ev.id, "mint", false, "unknown ticker");
1329
+ return;
1330
+ }
1331
+ const remaining = tok.supply - tok.minted;
1332
+ if (remaining <= 0n) {
1333
+ note(ev, ev.id, "mint", false, "supply exhausted");
1334
+ return;
1335
+ }
1336
+ let credit;
1337
+ if (tok.meta.mint === "issuer") {
1338
+ if (who !== tok.meta.deployer) {
1339
+ note(ev, ev.id, "mint", false, "issuer-only mint");
1340
+ return;
1341
+ }
1342
+ const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
1343
+ if (req === null) {
1344
+ note(ev, ev.id, "mint", false, "issuer mint requires amount");
1345
+ return;
1346
+ }
1347
+ credit = req < remaining ? req : remaining;
1348
+ } else {
1349
+ const req = rec.amount !== void 0 ? parseAmount(rec.amount) : tok.mintLimit;
1350
+ const capped = req < tok.mintLimit ? req : tok.mintLimit;
1351
+ credit = capped < remaining ? capped : remaining;
1352
+ }
1353
+ tok.minted += credit;
1354
+ bal(rec.ticker, who).available += credit;
1355
+ note(ev, ev.id, "mint", true, credit.toString());
1356
+ }
1357
+ function applyTransfer(c, ev, rec, who) {
1358
+ const { tokens, bal, note } = c;
1359
+ if (!tokens.has(rec.ticker)) {
1360
+ note(ev, ev.id, "transfer", false, "unknown ticker");
1361
+ return;
1362
+ }
1363
+ const amt = parseAmount(rec.amount);
1364
+ const from = bal(rec.ticker, who);
1365
+ if (from.available < amt) {
1366
+ note(ev, ev.id, "transfer", false, "insufficient available balance");
1367
+ return;
1368
+ }
1369
+ from.available -= amt;
1370
+ bal(rec.ticker, rec.to.toLowerCase()).available += amt;
1371
+ note(ev, ev.id, "transfer", true);
1372
+ }
1373
+ function applyTmeta(c, ev, rec, who) {
1374
+ const { tokens, note } = c;
1375
+ const v15 = ev.height >= V15_HEIGHT;
1376
+ if (!v15) {
1377
+ note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
1378
+ return;
1379
+ }
1380
+ const tok = tokens.get(rec.ticker);
1381
+ if (!tok) {
1382
+ note(ev, ev.id, "tmeta", false, "unknown ticker");
1383
+ return;
1384
+ }
1385
+ if (who !== tok.meta.deployer) {
1386
+ note(ev, ev.id, "tmeta", false, "issuer-only metadata");
1387
+ return;
1388
+ }
1389
+ tok.meta.tmeta = rec.hash;
1390
+ note(ev, ev.id, "tmeta", true);
1391
+ }
1392
+
1393
+ // src/resolve.ts
1394
+ var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
1395
+ function resolve(events, tipHeight) {
1396
+ const ordered = [...events].sort(
1397
+ (a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
1398
+ a.kind === "propose" ? a.id : a.txid,
1399
+ b.kind === "propose" ? b.id : b.txid
1400
+ )
1401
+ );
1402
+ const eid = (e) => e.kind === "propose" ? e.id : e.txid;
1403
+ const seenIds = /* @__PURE__ */ new Set();
1404
+ const applied = [];
1405
+ for (const e of ordered) {
1406
+ if (e.height >= V29_HEIGHT) {
1407
+ const k = eid(e);
1408
+ if (seenIds.has(k)) continue;
1409
+ seenIds.add(k);
1410
+ }
1411
+ applied.push(e);
1412
+ }
1413
+ const c = createContext();
1414
+ const { tokens, balances, names, recaptures, offers, bids, fclaims, log, note, sweepExpired } = c;
1415
+ let pendingBlock = -1;
1416
+ const applyPendingCancels = () => {
1417
+ for (const f of c.pendingCancels) f();
1418
+ c.pendingCancels = [];
1419
+ };
516
1420
  for (const ev of applied) {
517
1421
  if (ev.height < ACTIVATION_HEIGHT) continue;
518
1422
  if (ev.height !== pendingBlock) {
@@ -520,14 +1424,6 @@ function resolve(events, tipHeight) {
520
1424
  pendingBlock = ev.height;
521
1425
  }
522
1426
  sweepExpired(ev.height);
523
- const v11 = ev.height >= V11_HEIGHT;
524
- const v12 = ev.height >= V12_HEIGHT;
525
- const v15 = ev.height >= V15_HEIGHT;
526
- const v16 = ev.height >= V16_HEIGHT;
527
- const v19 = ev.height >= V19_HEIGHT;
528
- const v23 = ev.height >= V23_HEIGHT;
529
- const v25 = ev.height >= V25_HEIGHT;
530
- const v26 = ev.height >= V26_HEIGHT;
531
1427
  const feeToTreasury = ev.kind === "propose" ? ptAmt((ev.paidTo ?? {})[TREASURY_ADDR]) : 0n;
532
1428
  if (ev.kind === "propose") {
533
1429
  const rec = parseRecord(ev.uri, ev.payloadHash);
@@ -536,812 +1432,55 @@ function resolve(events, tipHeight) {
536
1432
  continue;
537
1433
  }
538
1434
  const who = ev.proposer.toLowerCase();
539
- if (rec.t === "deploy") {
540
- if (tokens.has(rec.ticker)) {
541
- note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
542
- continue;
543
- }
544
- if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
545
- note(ev, ev.id, "deploy", false, "deploy fee unpaid");
546
- continue;
547
- }
548
- const supply = parseAmount(rec.supply);
549
- const mintLimit = rec.mint === "open" ? parseAmount(rec.mintLimit) : null;
550
- tokens.set(rec.ticker, {
551
- minted: 0n,
552
- supply,
553
- mintLimit,
554
- 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 }
555
- });
556
- if (v11) feesPaid += BigInt(DEPLOY_FEE);
557
- note(ev, ev.id, "deploy", true);
558
- } else if (rec.t === "mint") {
559
- const tok = tokens.get(rec.ticker);
560
- if (!tok) {
561
- note(ev, ev.id, "mint", false, "unknown ticker");
562
- continue;
563
- }
564
- const remaining = tok.supply - tok.minted;
565
- if (remaining <= 0n) {
566
- note(ev, ev.id, "mint", false, "supply exhausted");
567
- continue;
568
- }
569
- let credit;
570
- if (tok.meta.mint === "issuer") {
571
- if (who !== tok.meta.deployer) {
572
- note(ev, ev.id, "mint", false, "issuer-only mint");
573
- continue;
574
- }
575
- const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
576
- if (req === null) {
577
- note(ev, ev.id, "mint", false, "issuer mint requires amount");
578
- continue;
579
- }
580
- credit = req < remaining ? req : remaining;
581
- } else {
582
- const req = rec.amount !== void 0 ? parseAmount(rec.amount) : tok.mintLimit;
583
- const capped = req < tok.mintLimit ? req : tok.mintLimit;
584
- credit = capped < remaining ? capped : remaining;
585
- }
586
- tok.minted += credit;
587
- bal(rec.ticker, who).available += credit;
588
- note(ev, ev.id, "mint", true, credit.toString());
589
- } else if (rec.t === "transfer") {
590
- if (!tokens.has(rec.ticker)) {
591
- note(ev, ev.id, "transfer", false, "unknown ticker");
592
- continue;
593
- }
594
- const amt = parseAmount(rec.amount);
595
- const from = bal(rec.ticker, who);
596
- if (from.available < amt) {
597
- note(ev, ev.id, "transfer", false, "insufficient available balance");
598
- continue;
599
- }
600
- from.available -= amt;
601
- bal(rec.ticker, rec.to.toLowerCase()).available += amt;
602
- note(ev, ev.id, "transfer", true);
603
- } else if (rec.t === "ncommit") {
604
- if (!v11) {
605
- note(ev, ev.id, "ncommit", false, "before v1.1 activation");
606
- continue;
607
- }
608
- const prev = commits.get(rec.commit);
609
- if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
610
- note(ev, ev.id, "ncommit", true);
611
- } else if (rec.t === "name") {
612
- if (!v11) {
613
- note(ev, ev.id, "name", false, "before v1.1 activation");
614
- continue;
615
- }
616
- const regWindow = v25 || v26 ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
617
- let effHeight = ev.height;
618
- if (rec.salt !== void 0) {
619
- const ch = nameCommit(rec.name, rec.salt, who);
620
- const cH = commits.get(ch);
621
- if (cH === void 0 || cH >= ev.height || ev.height - cH > regWindow) {
622
- note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
623
- continue;
624
- }
625
- effHeight = cH;
626
- }
627
- const cur = names.get(rec.name);
628
- const epClaim = epochOf(ev.height);
629
- const curActive = cur && cur.pending && cur.finalizeBy !== void 0 && ev.height > cur.finalizeBy ? void 0 : cur;
630
- if (curActive && !curActive.pending && v15 && lapsed(curActive, epClaim)) {
631
- if (v26) {
632
- if (rec.salt === void 0) {
633
- note(ev, ev.id, "name", false, "v2.6: recapture requires a commit-reveal (salt)");
634
- continue;
635
- }
636
- const cr = recaptures.get(rec.name);
637
- const crActive = cr && ev.height > cr.finalizeBy ? void 0 : cr;
638
- const better2 = !crActive || earlierAnchor(effHeight, ev.pos, ev.id, crActive);
639
- if (!better2) {
640
- note(ev, ev.id, "name", false, "recapture already reserved (earlier anchor wins)");
641
- continue;
642
- }
643
- let myR = 0;
644
- for (const [nm, r] of recaptures) if (r.owner === who && nm !== rec.name && ev.height <= r.finalizeBy) myR++;
645
- if (myR >= MAX_PENDING_REG) {
646
- note(ev, ev.id, "name", false, "too many pending recaptures (max reached)");
647
- continue;
648
- }
649
- 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 });
650
- note(ev, ev.id, "name", true, crActive ? "recapture reserved (displaced prior reservation)" : "recapture reserved (pending finalize)");
651
- continue;
652
- }
653
- const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(curActive) + NAME_GRACE_EPOCHS), ev.height);
654
- if (feeToTreasury < fee) {
655
- note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
656
- continue;
657
- }
658
- voidOpenNameOffers(rec.name, ev.height);
659
- putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
660
- feesPaid += fee;
661
- note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
662
- continue;
663
- }
664
- if (v25) {
665
- if (rec.salt === void 0) {
666
- note(ev, ev.id, "name", false, "v2.5: registration requires a commit-reveal (salt)");
667
- continue;
668
- }
669
- const better2 = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
670
- if (!better2) {
671
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
672
- continue;
673
- }
674
- let myPending = 0;
675
- for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== rec.name && n.finalizeBy !== void 0 && ev.height <= n.finalizeBy) myPending++;
676
- if (myPending >= MAX_PENDING_REG) {
677
- note(ev, ev.id, "name", false, "too many pending reservations (max reached)");
678
- continue;
679
- }
680
- if (curActive) {
681
- voidOpenNameOffers(rec.name, ev.height);
682
- }
683
- 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 });
684
- note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
685
- continue;
686
- }
687
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
688
- note(ev, ev.id, "name", false, "name registration fee unpaid");
689
- continue;
690
- }
691
- const cand = {
692
- owner: who,
693
- effHeight,
694
- pos: ev.pos,
695
- id: ev.id,
696
- height: ev.height,
697
- locked: false,
698
- ...v15 ? { paidThroughEpoch: epClaim + NAME_TERM_EPOCHS } : {}
699
- };
700
- const better = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
701
- if (!better) {
702
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
703
- continue;
704
- }
705
- if (curActive) {
706
- voidOpenNameOffers(rec.name, ev.height);
707
- }
708
- putName(rec.name, cand);
709
- feesPaid += nameRegFee(rec.name, ev.height);
710
- note(ev, ev.id, "name", true, curActive ? "displaced prior holder" : void 0);
711
- } else if (rec.t === "nfinalize") {
712
- if (!v25) {
713
- note(ev, ev.id, "nfinalize", false, "before v2.5 activation");
714
- continue;
715
- }
716
- const n = names.get(rec.name);
717
- if (n && n.pending && n.owner === who) {
718
- const cH = commits.get(nameCommit(rec.name, rec.salt, who));
719
- if (cH === void 0 || cH !== n.effHeight) {
720
- note(ev, ev.id, "nfinalize", false, "salt does not match the reservation commit");
721
- continue;
722
- }
723
- if (!(ev.height > n.effHeight + REG_COMMIT_MAX_BLOCKS)) {
724
- note(ev, ev.id, "nfinalize", false, "too early \u2014 displacement contest not yet frozen");
725
- continue;
726
- }
727
- if (n.finalizeBy !== void 0 && ev.height > n.finalizeBy) {
728
- note(ev, ev.id, "nfinalize", false, "reservation expired");
729
- continue;
730
- }
731
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
732
- note(ev, ev.id, "nfinalize", false, "name registration fee unpaid");
733
- continue;
734
- }
735
- delete n.pending;
736
- delete n.finalizeBy;
737
- pendingNames.delete(rec.name);
738
- n.paidThroughEpoch = epochOf(ev.height) + NAME_TERM_EPOCHS;
739
- feesPaid += nameRegFee(rec.name, ev.height);
740
- note(ev, ev.id, "nfinalize", true);
741
- continue;
742
- }
743
- const r = v26 ? recaptures.get(rec.name) : void 0;
744
- if (r && r.owner === who) {
745
- const cH = commits.get(nameCommit(rec.name, rec.salt, who));
746
- if (cH === void 0 || cH !== r.effHeight) {
747
- note(ev, ev.id, "nfinalize", false, "salt does not match the recapture commit");
748
- continue;
749
- }
750
- if (!(ev.height > r.effHeight + REG_COMMIT_MAX_BLOCKS)) {
751
- note(ev, ev.id, "nfinalize", false, "too early \u2014 recapture contest not yet frozen");
752
- continue;
753
- }
754
- if (ev.height > r.finalizeBy) {
755
- note(ev, ev.id, "nfinalize", false, "recapture reservation expired");
756
- continue;
757
- }
758
- const cur = names.get(rec.name);
759
- const ep = epochOf(ev.height);
760
- if (!cur || cur.pending || !lapsed(cur, ep)) {
761
- recaptures.delete(rec.name);
762
- note(ev, ev.id, "nfinalize", false, "name is no longer lapsed");
763
- continue;
764
- }
765
- const fee = expiredClaimFee(rec.name, ep - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
766
- if (feeToTreasury < fee) {
767
- note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
768
- continue;
769
- }
770
- voidOpenNameOffers(rec.name, ev.height);
771
- putName(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
772
- recaptures.delete(rec.name);
773
- feesPaid += fee;
774
- note(ev, ev.id, "nfinalize", true, "recapture finalized (premium)");
775
- continue;
776
- }
777
- note(ev, ev.id, "nfinalize", false, "no matching pending reservation you own");
778
- } else if (rec.t === "nxfer") {
779
- const n = names.get(rec.name);
780
- if (!n || n.owner !== who) {
781
- note(ev, ev.id, "nxfer", false, "not the name owner");
782
- continue;
783
- }
784
- if (n.pending) {
785
- note(ev, ev.id, "nxfer", false, "name reservation not yet finalized");
786
- continue;
787
- }
788
- if (v15 && lapsed(n, epochOf(ev.height))) {
789
- note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
790
- continue;
791
- }
792
- if (n.locked) {
793
- note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
794
- continue;
795
- }
796
- n.owner = rec.to.toLowerCase();
797
- n.addr = void 0;
798
- n.profile = void 0;
799
- note(ev, ev.id, "nxfer", true);
800
- } else if (rec.t === "nset") {
801
- const n = names.get(rec.name);
802
- if (!n || n.owner !== who) {
803
- note(ev, ev.id, "nset", false, "not the name owner");
804
- continue;
805
- }
806
- if (n.pending) {
807
- note(ev, ev.id, "nset", false, "name reservation not yet finalized");
808
- continue;
809
- }
810
- if (v15 && lapsed(n, epochOf(ev.height))) {
811
- note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
812
- continue;
813
- }
814
- if (v23 && rec.addr.toLowerCase() === ZERO_ADDR) n.addr = void 0;
815
- else n.addr = rec.addr.toLowerCase();
816
- note(ev, ev.id, "nset", true);
817
- } else if (rec.t === "nprofile") {
818
- if (!v19) {
819
- note(ev, ev.id, "nprofile", false, "before v1.9 activation");
820
- continue;
821
- }
822
- const n = names.get(rec.name);
823
- if (!n || n.owner !== who) {
824
- note(ev, ev.id, "nprofile", false, "not the name owner");
825
- continue;
826
- }
827
- if (n.pending) {
828
- note(ev, ev.id, "nprofile", false, "name reservation not yet finalized");
829
- continue;
830
- }
831
- if (v15 && lapsed(n, epochOf(ev.height))) {
832
- note(ev, ev.id, "nprofile", false, "lease lapsed \u2014 claim it instead");
833
- continue;
834
- }
835
- n.profile = Object.keys(rec.p).length ? { ...rec.p } : void 0;
836
- note(ev, ev.id, "nprofile", true);
837
- } else if (rec.t === "offer") {
838
- const wantIsToken = isTokenWant(rec.want);
839
- if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && !v12) {
840
- note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
841
- continue;
842
- }
843
- if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
844
- note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
845
- continue;
846
- }
847
- const payto = (rec.want.payto ?? who).toLowerCase();
848
- if (payto === TREASURY_ADDR) {
849
- note(ev, ev.id, "offer", false, "payto cannot be the protocol treasury");
850
- continue;
851
- }
852
- if (!Number.isSafeInteger(ev.expiresEpoch)) {
853
- note(ev, ev.id, "offer", false, "expiresEpoch out of safe-integer range");
854
- continue;
855
- }
856
- if (epochOf(ev.height) > ev.expiresEpoch) {
857
- note(ev, ev.id, "offer", false, "already expired at anchor");
858
- continue;
859
- }
860
- if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - epochOf(ev.height) > MAX_OFFER_EPOCHS) {
861
- note(ev, ev.id, "offer", false, "v2.1: offer duration exceeds the max");
862
- continue;
863
- }
864
- const give = rec.give;
865
- if (isNameGive(give)) {
866
- if (!v11) {
867
- note(ev, ev.id, "offer", false, "name offers need v1.1");
868
- continue;
869
- }
870
- const n = names.get(give.name);
871
- if (!n || n.owner !== who) {
872
- note(ev, ev.id, "offer", false, "you don't own this name");
873
- continue;
874
- }
875
- if (n.pending) {
876
- note(ev, ev.id, "offer", false, "name reservation not yet finalized");
877
- continue;
878
- }
879
- if (n.locked) {
880
- note(ev, ev.id, "offer", false, "name already locked by another offer");
881
- continue;
882
- }
883
- const saleEmbargo = ev.height >= V27_HEIGHT ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
884
- if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= saleEmbargo) {
885
- note(ev, ev.id, "offer", false, "name too young to sell (must out-age the reveal window)");
886
- continue;
887
- }
888
- if (v15 && paidThrough(n) < ev.expiresEpoch) {
889
- note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
890
- continue;
891
- }
892
- n.locked = true;
893
- } else {
894
- if (!tokens.has(give.ticker)) {
895
- note(ev, ev.id, "offer", false, "unknown ticker");
896
- continue;
897
- }
898
- const amt = parseAmount(give.amount);
899
- const s = bal(give.ticker, who);
900
- if (s.available < amt) {
901
- note(ev, ev.id, "offer", false, "insufficient available balance");
902
- continue;
903
- }
904
- s.available -= amt;
905
- s.locked += amt;
906
- offerLock.set(ev.id, amt);
907
- }
908
- const o = {
909
- id: ev.id,
910
- seller: who,
911
- give,
912
- want: wantIsToken ? { ticker: rec.want.ticker, amount: rec.want.amount, payto } : { value: rec.want.value, payto },
913
- ...rec.taker ? { taker: rec.taker.toLowerCase() } : {},
914
- ...rec.min !== void 0 ? { min: rec.min, paid: "0", delivered: "0", fills: [] } : {},
915
- ...rec.bid !== void 0 ? { bid: rec.bid } : {},
916
- status: "open",
917
- expiresEpoch: ev.expiresEpoch,
918
- height: ev.height,
919
- feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
920
- };
921
- offers.set(ev.id, o);
922
- openOffers.add(ev.id);
923
- const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
924
- if (linked) linked.offers.push(ev.id);
925
- note(ev, ev.id, "offer", true);
926
- } else if (rec.t === "bid") {
927
- if (!v12) {
928
- note(ev, ev.id, "bid", false, "bids need v1.2");
929
- continue;
930
- }
931
- if (!Number.isSafeInteger(ev.expiresEpoch)) {
932
- note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
933
- continue;
934
- }
935
- if (epochOf(ev.height) > ev.expiresEpoch) {
936
- note(ev, ev.id, "bid", false, "already expired at anchor");
937
- continue;
938
- }
939
- if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - epochOf(ev.height) > MAX_OFFER_EPOCHS) {
940
- note(ev, ev.id, "bid", false, "v2.1: bid duration exceeds the max");
941
- continue;
942
- }
943
- bids.set(ev.id, {
944
- id: ev.id,
945
- bidder: who,
946
- want: rec.want,
947
- give: rec.give,
948
- status: "open",
949
- expiresEpoch: ev.expiresEpoch,
950
- height: ev.height,
951
- offers: []
952
- });
953
- openBids.add(ev.id);
954
- note(ev, ev.id, "bid", true);
955
- } else if (rec.t === "ocancel") {
956
- if (!v12) {
957
- note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
958
- continue;
959
- }
960
- const targets = [];
961
- for (const o of offers.values()) {
962
- if (o.status !== "open" || o.seller !== who) continue;
963
- if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
964
- if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
965
- if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
966
- targets.push(o);
967
- }
968
- if (ev.height >= V14_HEIGHT) {
969
- pendingCancels.push(() => {
970
- let n = 0;
971
- for (const o of targets) {
972
- if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
973
- if (o.status === "open") {
974
- releaseGive(o);
975
- setOfferStatus(o, "cancelled");
976
- n++;
977
- }
978
- }
979
- note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
980
- });
981
- } else {
982
- for (const o of targets) {
983
- releaseGive(o);
984
- setOfferStatus(o, "cancelled");
985
- }
986
- note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
987
- }
988
- } else if (rec.t === "nrenew") {
989
- if (!v15) {
990
- note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
991
- continue;
992
- }
993
- const n = names.get(rec.name);
994
- const ep = epochOf(ev.height);
995
- if (n && n.pending) {
996
- note(ev, ev.id, "nrenew", false, "name reservation not yet finalized");
997
- continue;
998
- }
999
- if (!n || lapsed(n, ep)) {
1000
- note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
1001
- continue;
1002
- }
1003
- if (inGrace(n, ep) && who !== n.owner) {
1004
- note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
1005
- continue;
1006
- }
1007
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1008
- note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
1009
- continue;
1010
- }
1011
- n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
1012
- feesPaid += nameRegFee(rec.name, ev.height);
1013
- note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
1014
- } else if (rec.t === "tmeta") {
1015
- if (!v15) {
1016
- note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
1017
- continue;
1018
- }
1019
- const tok = tokens.get(rec.ticker);
1020
- if (!tok) {
1021
- note(ev, ev.id, "tmeta", false, "unknown ticker");
1022
- continue;
1023
- }
1024
- if (who !== tok.meta.deployer) {
1025
- note(ev, ev.id, "tmeta", false, "issuer-only metadata");
1026
- continue;
1027
- }
1028
- tok.meta.tmeta = rec.hash;
1029
- note(ev, ev.id, "tmeta", true);
1030
- } else if (rec.t === "fclaim") {
1031
- if (ev.height < V28_HEIGHT) {
1032
- note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1033
- continue;
1034
- }
1035
- const target = offers.get(rec.offer);
1036
- const E = ev.expiresEpoch;
1037
- const deny = (why) => {
1038
- fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1039
- note(ev, ev.id, "fclaim", false, why);
1040
- };
1041
- if (!Number.isSafeInteger(E)) {
1042
- deny("expiresEpoch out of safe-integer range");
1043
- continue;
1044
- }
1045
- if (!target) {
1046
- deny("unknown offer");
1047
- continue;
1048
- }
1049
- if (target.status !== "open") {
1050
- deny(`offer ${target.status}`);
1051
- continue;
1052
- }
1053
- if (target.taker) {
1054
- deny("taker-bound offer needs no claim");
1055
- continue;
1056
- }
1057
- if (isTokenWant(target.want)) {
1058
- deny("claims are for CSD-priced offers");
1059
- continue;
1060
- }
1061
- if (E > effExpiry(target, ev.height)) {
1062
- deny("hold would outlive the offer expiry");
1063
- continue;
1064
- }
1065
- if (E > epochOf(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1066
- deny("hold too far ahead (anti-squat)");
1067
- continue;
1068
- }
1069
- if (epochOf(ev.height) > E) {
1070
- deny("expiry already in the past");
1071
- continue;
1072
- }
1073
- if (claimHeld(target, ev.height)) {
1074
- deny("offer already claimed (hold live)");
1075
- continue;
1076
- }
1077
- if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1078
- deny("claim cooldown (you just held this offer)");
1079
- continue;
1080
- }
1081
- let liveN = 0;
1082
- for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height) && (ev.height < V29_HEIGHT || x.status === "open")) liveN++;
1083
- if (liveN >= MAX_ACTIVE_CLAIMS) {
1084
- deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1085
- continue;
1086
- }
1087
- target.claimedBy = who;
1088
- target.claimUntilHeight = (E + 1) * EPOCH_LEN;
1089
- target.claimTxid = ev.id;
1090
- fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1091
- note(ev, ev.id, "fclaim", true);
1092
- }
1093
- } else {
1094
- const who = ev.attester.toLowerCase();
1095
- let o = offers.get(ev.proposalId);
1096
- if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
1097
- const fc = fclaims.get(ev.proposalId);
1098
- if (fc && fc.granted) {
1099
- const linked = offers.get(fc.offer);
1100
- if (linked && linked.claimTxid === ev.proposalId) o = linked;
1101
- }
1102
- }
1103
- if (!o) {
1104
- if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
1105
- note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
1106
- continue;
1107
- }
1108
- const b = bids.get(ev.proposalId);
1109
- if (b && ev.score === SCORE_CANCEL) {
1110
- if (who !== b.bidder) {
1111
- note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
1112
- continue;
1113
- }
1114
- if (b.status !== "open") {
1115
- note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
1116
- continue;
1117
- }
1118
- setBidStatus(b, "cancelled");
1119
- note(ev, ev.txid, "bidcancel", true);
1120
- }
1121
- continue;
1122
- }
1123
- if (ev.score === SCORE_CANCEL) {
1124
- if (who !== o.seller) {
1125
- note(ev, ev.txid, "cancel", false, "only seller may cancel");
1126
- continue;
1127
- }
1128
- if (o.status !== "open") {
1129
- note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
1130
- continue;
1131
- }
1132
- if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
1133
- note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
1134
- continue;
1135
- }
1136
- if (ev.height >= V14_HEIGHT) {
1137
- pendingCancels.push(() => {
1138
- if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
1139
- note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
1140
- return;
1141
- }
1142
- if (o.status === "open") {
1143
- releaseGive(o);
1144
- setOfferStatus(o, "cancelled");
1145
- note(ev, ev.txid, "cancel", true);
1146
- } else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
1147
- });
1148
- } else {
1149
- releaseGive(o);
1150
- setOfferStatus(o, "cancelled");
1151
- note(ev, ev.txid, "cancel", true);
1152
- }
1153
- } else if (ev.score === SCORE_FILL && isTokenWant(o.want)) {
1154
- if (o.status !== "open") {
1155
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1156
- continue;
1157
- }
1158
- if (o.taker && who !== o.taker) {
1159
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1160
- continue;
1161
- }
1162
- if (ev.confidence !== CONF_TOKEN_FILL) {
1163
- note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
1164
- continue;
1165
- }
1166
- if (!tokens.has(o.want.ticker)) {
1167
- note(ev, ev.txid, "fill", false, "want ticker does not exist");
1168
- continue;
1169
- }
1170
- const amt = BigInt(o.want.amount);
1171
- const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
1172
- const buyer = bal(o.want.ticker, who);
1173
- if (buyer.available < amt + fee) {
1174
- note(ev, ev.txid, "fill", false, "insufficient want-token balance");
1175
- continue;
1176
- }
1177
- const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
1178
- if (isNameGive(o.give) && !giveName) {
1179
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
1180
- continue;
1181
- }
1182
- const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
1183
- if (!isNameGive(o.give) && giveLock === void 0) {
1184
- note(ev, ev.txid, "fill", false, "offer lock missing");
1185
- continue;
1186
- }
1187
- buyer.available -= amt + fee;
1188
- bal(o.want.ticker, o.want.payto).available += amt;
1189
- if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
1190
- if (isNameGive(o.give)) deliverNameToBuyer(giveName, who, ev);
1191
- else releaseGiveLock(o, who, giveLock);
1192
- setOfferStatus(o, "filled");
1193
- o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
1194
- markBidDone(o, who);
1195
- note(ev, ev.txid, "fill", true);
1196
- } else if (ev.score === SCORE_FILL && o.min !== void 0 && !isNameGive(o.give)) {
1197
- if (o.status !== "open") {
1198
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1199
- continue;
1200
- }
1201
- if (o.taker && who !== o.taker) {
1202
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1203
- continue;
1204
- }
1205
- const blk = openFillReject(o, ev.height, who, ev.proposalId);
1206
- if (blk) {
1207
- note(ev, ev.txid, "fill", false, blk);
1208
- continue;
1209
- }
1210
- const pt = ev.paidTo ?? {};
1211
- const want = BigInt(o.want.value);
1212
- const paidSoFar = BigInt(o.paid ?? "0");
1213
- const remaining = want - paidSoFar;
1214
- const X = ptAmt(pt[o.want.payto]);
1215
- const minV = BigInt(o.min);
1216
- const effMin = remaining < minV ? remaining : minV;
1217
- if (X < effMin) {
1218
- note(ev, ev.txid, "fill", false, "payment below offer min");
1219
- continue;
1220
- }
1221
- const x = X < remaining ? X : remaining;
1222
- const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
1223
- if (ptAmt(pt[TREASURY_ADDR]) < fee) {
1224
- note(ev, ev.txid, "fill", false, "protocol fee unpaid");
1225
- continue;
1226
- }
1227
- const giveTotal = BigInt(o.give.amount);
1228
- const newPaid = paidSoFar + x;
1229
- const deliveredSoFar = BigInt(o.delivered ?? "0");
1230
- const newDelivered = giveTotal * newPaid / want;
1231
- const out = newDelivered - deliveredSoFar;
1232
- if (out === 0n) {
1233
- note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
1234
- continue;
1235
- }
1236
- const lock = offerLock.get(o.id);
1237
- if (lock === void 0) {
1238
- note(ev, ev.txid, "fill", false, "offer lock missing");
1239
- continue;
1240
- }
1241
- bal(o.give.ticker, o.seller).locked -= out;
1242
- bal(o.give.ticker, who).available += out;
1243
- offerLock.set(o.id, lock - out);
1244
- o.paid = newPaid.toString();
1245
- o.delivered = newDelivered.toString();
1246
- const entry = { buyer: who, txid: ev.txid, height: ev.height, paid: x.toString(), fee: fee.toString(), got: out.toString() };
1247
- (o.fills ??= []).push(entry);
1248
- feesPaid += fee;
1249
- if (newPaid === want) {
1250
- setOfferStatus(o, "filled");
1251
- o.fill = entry;
1252
- offerLock.delete(o.id);
1253
- markBidDone(o, who);
1254
- }
1255
- note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
1256
- } else if (ev.score === SCORE_FILL) {
1257
- if (o.status !== "open") {
1258
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1259
- continue;
1260
- }
1261
- if (o.taker && who !== o.taker) {
1262
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1263
- continue;
1264
- }
1265
- const blk = openFillReject(o, ev.height, who, ev.proposalId);
1266
- if (blk) {
1267
- note(ev, ev.txid, "fill", false, blk);
1268
- continue;
1269
- }
1270
- const pt = ev.paidTo ?? {};
1271
- const want = BigInt(o.want.value);
1272
- const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
1273
- const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
1274
- const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
1275
- const need = /* @__PURE__ */ new Map();
1276
- const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
1277
- addNeed(o.want.payto, want);
1278
- addNeed(TREASURY_ADDR, fee);
1279
- if (rebate > 0n) addNeed(o.seller, rebate);
1280
- let unpaid;
1281
- for (const [addr, amt] of need) if (ptAmt(pt[addr]) < amt) {
1282
- unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
1435
+ switch (rec.t) {
1436
+ case "deploy":
1437
+ applyDeploy(c, ev, rec, who, feeToTreasury);
1438
+ break;
1439
+ case "mint":
1440
+ applyMint(c, ev, rec, who);
1441
+ break;
1442
+ case "transfer":
1443
+ applyTransfer(c, ev, rec, who);
1444
+ break;
1445
+ case "tmeta":
1446
+ applyTmeta(c, ev, rec, who);
1447
+ break;
1448
+ case "ncommit":
1449
+ applyNcommit(c, ev, rec);
1450
+ break;
1451
+ case "name":
1452
+ applyName(c, ev, rec, who, feeToTreasury);
1453
+ break;
1454
+ case "nfinalize":
1455
+ applyNfinalize(c, ev, rec, who, feeToTreasury);
1456
+ break;
1457
+ case "nxfer":
1458
+ applyNxfer(c, ev, rec, who);
1459
+ break;
1460
+ case "nset":
1461
+ applyNset(c, ev, rec, who);
1462
+ break;
1463
+ case "nprofile":
1464
+ applyNprofile(c, ev, rec, who);
1465
+ break;
1466
+ case "nrenew":
1467
+ applyNrenew(c, ev, rec, who, feeToTreasury);
1468
+ break;
1469
+ case "offer":
1470
+ applyOffer(c, ev, rec, who);
1471
+ break;
1472
+ case "bid":
1473
+ applyBid(c, ev, rec, who);
1474
+ break;
1475
+ case "ocancel":
1476
+ applyOcancel(c, ev, rec, who);
1477
+ break;
1478
+ case "fclaim":
1479
+ applyFclaim(c, ev, rec, who);
1283
1480
  break;
1284
- }
1285
- if (unpaid) {
1286
- note(ev, ev.txid, "fill", false, unpaid);
1287
- continue;
1288
- }
1289
- const paid = ptAmt(pt[o.want.payto]);
1290
- if (isNameGive(o.give)) {
1291
- const n = names.get(o.give.name);
1292
- if (!n) {
1293
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
1294
- continue;
1295
- }
1296
- deliverNameToBuyer(n, who, ev);
1297
- } else {
1298
- const amt = offerLock.get(o.id);
1299
- if (amt === void 0) {
1300
- note(ev, ev.txid, "fill", false, "offer lock missing");
1301
- continue;
1302
- }
1303
- releaseGiveLock(o, who, amt);
1304
- }
1305
- feesPaid += fee;
1306
- setOfferStatus(o, "filled");
1307
- o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
1308
- markBidDone(o, who);
1309
- note(ev, ev.txid, "fill", true);
1310
- } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
1311
- if (ev.height >= V28_HEIGHT) {
1312
- note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
1313
- continue;
1314
- }
1315
- if (o.status !== "open") {
1316
- note(ev, ev.txid, "claim", false, `offer ${o.status}`);
1317
- continue;
1318
- }
1319
- if (o.taker) {
1320
- note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
1321
- continue;
1322
- }
1323
- if (isTokenWant(o.want)) {
1324
- note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
1325
- continue;
1326
- }
1327
- if (claimHeld(o, ev.height)) {
1328
- note(ev, ev.txid, "claim", false, "offer already claimed (hold live)");
1329
- continue;
1330
- }
1331
- if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + claimGrace(o) + CLAIM_COOLDOWN_BLOCKS) {
1332
- note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
1333
- continue;
1334
- }
1335
- let liveN = 0;
1336
- for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height) && (ev.height < V29_HEIGHT || x.status === "open")) liveN++;
1337
- if (liveN >= MAX_ACTIVE_CLAIMS) {
1338
- note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1339
- continue;
1340
- }
1341
- o.claimedBy = who;
1342
- o.claimUntilHeight = ev.height + claimWindowAt(ev.height);
1343
- note(ev, ev.txid, "claim", true);
1344
1481
  }
1482
+ } else {
1483
+ applyAttest(c, ev);
1345
1484
  }
1346
1485
  }
1347
1486
  applyPendingCancels();
@@ -1391,7 +1530,7 @@ function resolve(events, tipHeight) {
1391
1530
  for (const [nm, r] of [...recaptures.entries()].sort(([a], [b]) => ord(a, b))) recapturesOut[nm] = { owner: r.owner, effectiveHeight: r.effHeight, finalizeBy: r.finalizeBy };
1392
1531
  const fclaimsOut = {};
1393
1532
  for (const [txid, fc] of [...fclaims.entries()].sort(([a], [b]) => ord(a, b))) if (fc.granted) fclaimsOut[txid] = { offer: fc.offer, proposer: fc.proposer, expiresEpoch: fc.expiresEpoch, height: fc.height };
1394
- return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: feesPaid.toString() };
1533
+ return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: c.feesPaid.toString() };
1395
1534
  }
1396
1535
  function canonicalState(s) {
1397
1536
  const sortKeys = (v) => {
@@ -1708,10 +1847,10 @@ function bindOfferTerms(servedOffer, t, opts) {
1708
1847
  const om = o?.min;
1709
1848
  if ((om !== void 0 && om !== null) !== (t.min !== void 0)) return true;
1710
1849
  if (t.min !== void 0 && String(om) !== t.min) return true;
1711
- if (opts?.wantType) {
1850
+ if (opts?.wantType !== false) {
1712
1851
  if (t.wantType === void 0 || typeof o?.want?.ticker === "string" !== (t.wantType === "token")) return true;
1713
1852
  }
1714
- if (opts?.give) {
1853
+ if (opts?.give !== false) {
1715
1854
  const g = o?.give;
1716
1855
  if ((g?.ticker !== void 0 && g?.ticker !== null) !== (t.giveTicker !== void 0) || t.giveTicker !== void 0 && String(g?.ticker) !== t.giveTicker) return true;
1717
1856
  if ((g?.amount !== void 0 && g?.amount !== null) !== (t.giveAmount !== void 0) || t.giveAmount !== void 0 && String(g?.amount) !== t.giveAmount) return true;