@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.cjs CHANGED
@@ -302,27 +302,12 @@ var isName = (n) => typeof n === "string" && NAME_RE.test(n) && !RESERVED_NAMES.
302
302
  function nameCommit(name, salt, owner) {
303
303
  return (0, import_csd_codec2.payloadHash)({ t: "cairnx:name:commit:v1", name, salt, owner: owner.toLowerCase() });
304
304
  }
305
- function strWellFormed(s) {
306
- const wf = String.prototype.isWellFormed;
307
- if (typeof wf === "function") return wf.call(s);
308
- for (let i = 0; i < s.length; i++) {
309
- const c = s.charCodeAt(i);
310
- if (c >= 55296 && c <= 56319) {
311
- const n = s.charCodeAt(i + 1);
312
- if (!(n >= 56320 && n <= 57343)) return false;
313
- i++;
314
- } else if (c >= 56320 && c <= 57343) {
315
- return false;
316
- }
317
- }
318
- return true;
319
- }
320
305
  function isWellFormedDeep(v) {
321
- if (typeof v === "string") return strWellFormed(v);
306
+ if (typeof v === "string") return (0, import_csd_codec2.isWellFormedUtf16)(v);
322
307
  if (Array.isArray(v)) return v.every(isWellFormedDeep);
323
308
  if (v && typeof v === "object") {
324
309
  for (const [k, val] of Object.entries(v)) {
325
- if (!strWellFormed(k)) return false;
310
+ if (!(0, import_csd_codec2.isWellFormedUtf16)(k)) return false;
326
311
  if (!isWellFormedDeep(val)) return false;
327
312
  }
328
313
  }
@@ -523,29 +508,14 @@ var tokenMeta = (r) => buildRecord({ v: 1, t: "tmeta", ...r });
523
508
  var nameProfile = (r) => buildRecord({ v: 1, t: "nprofile", ...r });
524
509
  var fclaim = (r) => buildRecord({ v: 1, t: "fclaim", ...r });
525
510
 
526
- // src/resolve.ts
511
+ // src/resolve/context.ts
527
512
  function ptAmt(v) {
528
513
  return typeof v === "string" && AMOUNT_RE.test(v) ? BigInt(v) : 0n;
529
514
  }
530
- var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
531
- function resolve(events, tipHeight) {
532
- const ordered = [...events].sort(
533
- (a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
534
- a.kind === "propose" ? a.id : a.txid,
535
- b.kind === "propose" ? b.id : b.txid
536
- )
537
- );
538
- const eid = (e) => e.kind === "propose" ? e.id : e.txid;
539
- const seenIds = /* @__PURE__ */ new Set();
540
- const applied = [];
541
- for (const e of ordered) {
542
- if (e.height >= V29_HEIGHT) {
543
- const k = eid(e);
544
- if (seenIds.has(k)) continue;
545
- seenIds.add(k);
546
- }
547
- applied.push(e);
548
- }
515
+ 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;
516
+ var claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
517
+ var claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
518
+ function createContext() {
549
519
  const tokens = /* @__PURE__ */ new Map();
550
520
  const balances = /* @__PURE__ */ new Map();
551
521
  const names = /* @__PURE__ */ new Map();
@@ -574,13 +544,6 @@ function resolve(events, tipHeight) {
574
544
  };
575
545
  const fclaims = /* @__PURE__ */ new Map();
576
546
  const log = [];
577
- let feesPaid = 0n;
578
- let pendingCancels = [];
579
- let pendingBlock = -1;
580
- const applyPendingCancels = () => {
581
- for (const f of pendingCancels) f();
582
- pendingCancels = [];
583
- };
584
547
  const bal = (ticker, addr) => {
585
548
  let m = balances.get(ticker);
586
549
  if (!m) {
@@ -606,7 +569,6 @@ function resolve(events, tipHeight) {
606
569
  b.available += amt;
607
570
  }
608
571
  };
609
- const 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;
610
572
  const sweepExpired = (height) => {
611
573
  const ep = (0, import_csd_codec.epochOf)(height);
612
574
  for (const id of [...openOffers]) {
@@ -644,10 +606,6 @@ function resolve(events, tipHeight) {
644
606
  if (height > r.finalizeBy) recaptures.delete(nm);
645
607
  }
646
608
  };
647
- const V15_EPOCH = (0, import_csd_codec.epochOf)(V15_HEIGHT);
648
- const paidThrough = (n) => n.paidThroughEpoch ?? V15_EPOCH + NAME_TERM_EPOCHS;
649
- const lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
650
- const inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
651
609
  const markBidDone = (o, buyer) => {
652
610
  if (!o.bid) return;
653
611
  const b = bids.get(o.bid);
@@ -660,28 +618,10 @@ function resolve(events, tipHeight) {
660
618
  setOfferStatus(o, "cancelled");
661
619
  }
662
620
  };
663
- const earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
664
- const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
665
- const claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
666
- const openFillReject = (o, height, who, targetId) => {
667
- if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
668
- if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
669
- if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
670
- if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
671
- return void 0;
672
- };
673
- const deliverNameToBuyer = (n, who, ev) => {
674
- n.owner = who;
675
- n.locked = false;
676
- n.addr = void 0;
677
- n.profile = void 0;
678
- if (ev.height >= V13_HEIGHT) {
679
- n.effHeight = ev.height;
680
- n.pos = ev.pos;
681
- n.id = ev.txid;
682
- n.height = ev.height;
683
- n.viaFill = true;
684
- }
621
+ const liveHoldCount = (who, height) => {
622
+ let liveN = 0;
623
+ for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, height) && (height < V29_HEIGHT || x.status === "open")) liveN++;
624
+ return liveN;
685
625
  };
686
626
  const releaseGiveLock = (o, who, amt) => {
687
627
  const t = o.give.ticker;
@@ -689,6 +629,970 @@ function resolve(events, tipHeight) {
689
629
  bal(t, who).available += amt;
690
630
  offerLock.delete(o.id);
691
631
  };
632
+ return {
633
+ tokens,
634
+ balances,
635
+ names,
636
+ commits,
637
+ recaptures,
638
+ offers,
639
+ offerLock,
640
+ bids,
641
+ openOffers,
642
+ openBids,
643
+ pendingNames,
644
+ fclaims,
645
+ log,
646
+ setOfferStatus,
647
+ setBidStatus,
648
+ putName,
649
+ bal,
650
+ note,
651
+ releaseGive,
652
+ sweepExpired,
653
+ markBidDone,
654
+ voidOpenNameOffers,
655
+ liveHoldCount,
656
+ releaseGiveLock,
657
+ feesPaid: 0n,
658
+ // v1.4 fill-before-cancel: at V14_HEIGHT+ a cancel/ocancel's EFFECT is deferred to the block
659
+ // boundary, so any same-block fill wins — a seller can no longer snipe a buyer's in-flight
660
+ // payment by front-running the fill with a cheap cancel (cancel applies first in consensus
661
+ // order: proposals before attests, and a paid-up cancel attest can out-pos the fill). A fill
662
+ // that lands = seller got paid = a completed sale; the late cancel then no-ops. Pre-v14 blocks
663
+ // never populate pendingCancels, so all historical replay stays byte-identical.
664
+ pendingCancels: []
665
+ };
666
+ }
667
+
668
+ // src/resolve/attest.ts
669
+ var openFillReject = (o, height, who, targetId) => {
670
+ if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
671
+ if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
672
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
673
+ if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
674
+ return void 0;
675
+ };
676
+ var deliverNameToBuyer = (n, who, ev) => {
677
+ n.owner = who;
678
+ n.locked = false;
679
+ n.addr = void 0;
680
+ n.profile = void 0;
681
+ if (ev.height >= V13_HEIGHT) {
682
+ n.effHeight = ev.height;
683
+ n.pos = ev.pos;
684
+ n.id = ev.txid;
685
+ n.height = ev.height;
686
+ n.viaFill = true;
687
+ }
688
+ };
689
+ function applyAttest(c, ev) {
690
+ const { offers, fclaims, bids, note, setBidStatus } = c;
691
+ const who = ev.attester.toLowerCase();
692
+ let o = offers.get(ev.proposalId);
693
+ if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
694
+ const fc = fclaims.get(ev.proposalId);
695
+ if (fc && fc.granted) {
696
+ const linked = offers.get(fc.offer);
697
+ if (linked && linked.claimTxid === ev.proposalId) o = linked;
698
+ }
699
+ }
700
+ if (!o) {
701
+ if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
702
+ note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
703
+ return;
704
+ }
705
+ const b = bids.get(ev.proposalId);
706
+ if (b && ev.score === SCORE_CANCEL) {
707
+ if (who !== b.bidder) {
708
+ note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
709
+ return;
710
+ }
711
+ if (b.status !== "open") {
712
+ note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
713
+ return;
714
+ }
715
+ setBidStatus(b, "cancelled");
716
+ note(ev, ev.txid, "bidcancel", true);
717
+ }
718
+ return;
719
+ }
720
+ if (ev.score === SCORE_CANCEL) applyCancel(c, ev, o, who);
721
+ else if (ev.score === SCORE_FILL && isTokenWant(o.want)) applyTokenFill(c, ev, o, who);
722
+ else if (ev.score === SCORE_FILL && o.min !== void 0 && !isNameGive(o.give)) applyPartialFill(c, ev, o, who);
723
+ else if (ev.score === SCORE_FILL) applyFill(c, ev, o, who);
724
+ else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) applyLegacyClaim(c, ev, o, who);
725
+ }
726
+ function applyCancel(c, ev, o, who) {
727
+ const { note, releaseGive, setOfferStatus } = c;
728
+ if (who !== o.seller) {
729
+ note(ev, ev.txid, "cancel", false, "only seller may cancel");
730
+ return;
731
+ }
732
+ if (o.status !== "open") {
733
+ note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
734
+ return;
735
+ }
736
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
737
+ note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
738
+ return;
739
+ }
740
+ if (ev.height >= V14_HEIGHT) {
741
+ c.pendingCancels.push(() => {
742
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
743
+ note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
744
+ return;
745
+ }
746
+ if (o.status === "open") {
747
+ releaseGive(o);
748
+ setOfferStatus(o, "cancelled");
749
+ note(ev, ev.txid, "cancel", true);
750
+ } else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
751
+ });
752
+ } else {
753
+ releaseGive(o);
754
+ setOfferStatus(o, "cancelled");
755
+ note(ev, ev.txid, "cancel", true);
756
+ }
757
+ }
758
+ function applyTokenFill(c, ev, o, who) {
759
+ const { tokens, names, offerLock, bal, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
760
+ if (o.status !== "open") {
761
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
762
+ return;
763
+ }
764
+ if (o.taker && who !== o.taker) {
765
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
766
+ return;
767
+ }
768
+ if (ev.confidence !== CONF_TOKEN_FILL) {
769
+ note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
770
+ return;
771
+ }
772
+ if (!tokens.has(o.want.ticker)) {
773
+ note(ev, ev.txid, "fill", false, "want ticker does not exist");
774
+ return;
775
+ }
776
+ const amt = BigInt(o.want.amount);
777
+ const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
778
+ const buyer = bal(o.want.ticker, who);
779
+ if (buyer.available < amt + fee) {
780
+ note(ev, ev.txid, "fill", false, "insufficient want-token balance");
781
+ return;
782
+ }
783
+ const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
784
+ if (isNameGive(o.give) && !giveName) {
785
+ note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
786
+ return;
787
+ }
788
+ const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
789
+ if (!isNameGive(o.give) && giveLock === void 0) {
790
+ note(ev, ev.txid, "fill", false, "offer lock missing");
791
+ return;
792
+ }
793
+ buyer.available -= amt + fee;
794
+ bal(o.want.ticker, o.want.payto).available += amt;
795
+ if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
796
+ if (isNameGive(o.give)) deliverNameToBuyer(giveName, who, ev);
797
+ else releaseGiveLock(o, who, giveLock);
798
+ setOfferStatus(o, "filled");
799
+ o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
800
+ markBidDone(o, who);
801
+ note(ev, ev.txid, "fill", true);
802
+ }
803
+ function applyPartialFill(c, ev, o, who) {
804
+ const { offerLock, bal, note, setOfferStatus, markBidDone } = c;
805
+ if (o.status !== "open") {
806
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
807
+ return;
808
+ }
809
+ if (o.taker && who !== o.taker) {
810
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
811
+ return;
812
+ }
813
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
814
+ if (blk) {
815
+ note(ev, ev.txid, "fill", false, blk);
816
+ return;
817
+ }
818
+ const pt = ev.paidTo ?? {};
819
+ const want = BigInt(o.want.value);
820
+ const paidSoFar = BigInt(o.paid ?? "0");
821
+ const remaining = want - paidSoFar;
822
+ const X = ptAmt(pt[o.want.payto]);
823
+ const minV = BigInt(o.min);
824
+ const effMin = remaining < minV ? remaining : minV;
825
+ if (X < effMin) {
826
+ note(ev, ev.txid, "fill", false, "payment below offer min");
827
+ return;
828
+ }
829
+ const x = X < remaining ? X : remaining;
830
+ const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
831
+ if (ptAmt(pt[TREASURY_ADDR]) < fee) {
832
+ note(ev, ev.txid, "fill", false, "protocol fee unpaid");
833
+ return;
834
+ }
835
+ const giveTotal = BigInt(o.give.amount);
836
+ const newPaid = paidSoFar + x;
837
+ const deliveredSoFar = BigInt(o.delivered ?? "0");
838
+ const newDelivered = giveTotal * newPaid / want;
839
+ const out = newDelivered - deliveredSoFar;
840
+ if (out === 0n) {
841
+ note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
842
+ return;
843
+ }
844
+ const lock = offerLock.get(o.id);
845
+ if (lock === void 0) {
846
+ note(ev, ev.txid, "fill", false, "offer lock missing");
847
+ return;
848
+ }
849
+ bal(o.give.ticker, o.seller).locked -= out;
850
+ bal(o.give.ticker, who).available += out;
851
+ offerLock.set(o.id, lock - out);
852
+ o.paid = newPaid.toString();
853
+ o.delivered = newDelivered.toString();
854
+ const entry = { buyer: who, txid: ev.txid, height: ev.height, paid: x.toString(), fee: fee.toString(), got: out.toString() };
855
+ (o.fills ??= []).push(entry);
856
+ c.feesPaid += fee;
857
+ if (newPaid === want) {
858
+ setOfferStatus(o, "filled");
859
+ o.fill = entry;
860
+ offerLock.delete(o.id);
861
+ markBidDone(o, who);
862
+ }
863
+ note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
864
+ }
865
+ function applyFill(c, ev, o, who) {
866
+ const { names, offerLock, note, setOfferStatus, markBidDone, releaseGiveLock } = c;
867
+ if (o.status !== "open") {
868
+ note(ev, ev.txid, "fill", false, `offer ${o.status}`);
869
+ return;
870
+ }
871
+ if (o.taker && who !== o.taker) {
872
+ note(ev, ev.txid, "fill", false, "taker-bound offer");
873
+ return;
874
+ }
875
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
876
+ if (blk) {
877
+ note(ev, ev.txid, "fill", false, blk);
878
+ return;
879
+ }
880
+ const pt = ev.paidTo ?? {};
881
+ const want = BigInt(o.want.value);
882
+ const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
883
+ const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
884
+ const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
885
+ const need = /* @__PURE__ */ new Map();
886
+ const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
887
+ addNeed(o.want.payto, want);
888
+ addNeed(TREASURY_ADDR, fee);
889
+ if (rebate > 0n) addNeed(o.seller, rebate);
890
+ let unpaid;
891
+ for (const [addr, amt] of need) if (ptAmt(pt[addr]) < amt) {
892
+ unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
893
+ break;
894
+ }
895
+ if (unpaid) {
896
+ note(ev, ev.txid, "fill", false, unpaid);
897
+ return;
898
+ }
899
+ const paid = ptAmt(pt[o.want.payto]);
900
+ if (isNameGive(o.give)) {
901
+ const n = names.get(o.give.name);
902
+ if (!n) {
903
+ note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
904
+ return;
905
+ }
906
+ deliverNameToBuyer(n, who, ev);
907
+ } else {
908
+ const amt = offerLock.get(o.id);
909
+ if (amt === void 0) {
910
+ note(ev, ev.txid, "fill", false, "offer lock missing");
911
+ return;
912
+ }
913
+ releaseGiveLock(o, who, amt);
914
+ }
915
+ c.feesPaid += fee;
916
+ setOfferStatus(o, "filled");
917
+ o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
918
+ markBidDone(o, who);
919
+ note(ev, ev.txid, "fill", true);
920
+ }
921
+ function applyLegacyClaim(c, ev, o, who) {
922
+ const { note, liveHoldCount } = c;
923
+ if (ev.height >= V28_HEIGHT) {
924
+ note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
925
+ return;
926
+ }
927
+ if (o.status !== "open") {
928
+ note(ev, ev.txid, "claim", false, `offer ${o.status}`);
929
+ return;
930
+ }
931
+ if (o.taker) {
932
+ note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
933
+ return;
934
+ }
935
+ if (isTokenWant(o.want)) {
936
+ note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
937
+ return;
938
+ }
939
+ if (claimHeld(o, ev.height)) {
940
+ note(ev, ev.txid, "claim", false, "offer already claimed (hold live)");
941
+ return;
942
+ }
943
+ if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + claimGrace(o) + CLAIM_COOLDOWN_BLOCKS) {
944
+ note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
945
+ return;
946
+ }
947
+ if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
948
+ note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
949
+ return;
950
+ }
951
+ o.claimedBy = who;
952
+ o.claimUntilHeight = ev.height + claimWindowAt(ev.height);
953
+ note(ev, ev.txid, "claim", true);
954
+ }
955
+
956
+ // src/resolve/names.ts
957
+ var V15_EPOCH = (0, import_csd_codec.epochOf)(V15_HEIGHT);
958
+ var paidThrough = (n) => n.paidThroughEpoch ?? V15_EPOCH + NAME_TERM_EPOCHS;
959
+ var lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
960
+ var inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
961
+ var earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
962
+ function applyNcommit(c, ev, rec) {
963
+ const { commits, note } = c;
964
+ const v11 = ev.height >= V11_HEIGHT;
965
+ if (!v11) {
966
+ note(ev, ev.id, "ncommit", false, "before v1.1 activation");
967
+ return;
968
+ }
969
+ const prev = commits.get(rec.commit);
970
+ if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
971
+ note(ev, ev.id, "ncommit", true);
972
+ }
973
+ function applyName(c, ev, rec, who, feeToTreasury) {
974
+ const { commits, names, recaptures, note, putName, voidOpenNameOffers } = c;
975
+ const v11 = ev.height >= V11_HEIGHT;
976
+ const v15 = ev.height >= V15_HEIGHT;
977
+ const v25 = ev.height >= V25_HEIGHT;
978
+ const v26 = ev.height >= V26_HEIGHT;
979
+ if (!v11) {
980
+ note(ev, ev.id, "name", false, "before v1.1 activation");
981
+ return;
982
+ }
983
+ const regWindow = v25 || v26 ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
984
+ let effHeight = ev.height;
985
+ if (rec.salt !== void 0) {
986
+ const ch = nameCommit(rec.name, rec.salt, who);
987
+ const cH = commits.get(ch);
988
+ if (cH === void 0 || cH >= ev.height || ev.height - cH > regWindow) {
989
+ note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
990
+ return;
991
+ }
992
+ effHeight = cH;
993
+ }
994
+ const cur = names.get(rec.name);
995
+ const epClaim = (0, import_csd_codec.epochOf)(ev.height);
996
+ const curActive = cur && cur.pending && cur.finalizeBy !== void 0 && ev.height > cur.finalizeBy ? void 0 : cur;
997
+ if (curActive && !curActive.pending && v15 && lapsed(curActive, epClaim)) {
998
+ if (v26) {
999
+ if (rec.salt === void 0) {
1000
+ note(ev, ev.id, "name", false, "v2.6: recapture requires a commit-reveal (salt)");
1001
+ return;
1002
+ }
1003
+ const cr = recaptures.get(rec.name);
1004
+ const crActive = cr && ev.height > cr.finalizeBy ? void 0 : cr;
1005
+ const better2 = !crActive || earlierAnchor(effHeight, ev.pos, ev.id, crActive);
1006
+ if (!better2) {
1007
+ note(ev, ev.id, "name", false, "recapture already reserved (earlier anchor wins)");
1008
+ return;
1009
+ }
1010
+ let myR = 0;
1011
+ for (const [nm, r] of recaptures) if (r.owner === who && nm !== rec.name && ev.height <= r.finalizeBy) myR++;
1012
+ if (myR >= MAX_PENDING_REG) {
1013
+ note(ev, ev.id, "name", false, "too many pending recaptures (max reached)");
1014
+ return;
1015
+ }
1016
+ 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 });
1017
+ note(ev, ev.id, "name", true, crActive ? "recapture reserved (displaced prior reservation)" : "recapture reserved (pending finalize)");
1018
+ return;
1019
+ }
1020
+ const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(curActive) + NAME_GRACE_EPOCHS), ev.height);
1021
+ if (feeToTreasury < fee) {
1022
+ note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
1023
+ return;
1024
+ }
1025
+ voidOpenNameOffers(rec.name, ev.height);
1026
+ putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
1027
+ c.feesPaid += fee;
1028
+ note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
1029
+ return;
1030
+ }
1031
+ if (v25) {
1032
+ if (rec.salt === void 0) {
1033
+ note(ev, ev.id, "name", false, "v2.5: registration requires a commit-reveal (salt)");
1034
+ return;
1035
+ }
1036
+ const better2 = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
1037
+ if (!better2) {
1038
+ note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
1039
+ return;
1040
+ }
1041
+ let myPending = 0;
1042
+ for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== rec.name && n.finalizeBy !== void 0 && ev.height <= n.finalizeBy) myPending++;
1043
+ if (myPending >= MAX_PENDING_REG) {
1044
+ note(ev, ev.id, "name", false, "too many pending reservations (max reached)");
1045
+ return;
1046
+ }
1047
+ if (curActive) {
1048
+ voidOpenNameOffers(rec.name, ev.height);
1049
+ }
1050
+ 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 });
1051
+ note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
1052
+ return;
1053
+ }
1054
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1055
+ note(ev, ev.id, "name", false, "name registration fee unpaid");
1056
+ return;
1057
+ }
1058
+ const cand = {
1059
+ owner: who,
1060
+ effHeight,
1061
+ pos: ev.pos,
1062
+ id: ev.id,
1063
+ height: ev.height,
1064
+ locked: false,
1065
+ ...v15 ? { paidThroughEpoch: epClaim + NAME_TERM_EPOCHS } : {}
1066
+ };
1067
+ const better = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
1068
+ if (!better) {
1069
+ note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
1070
+ return;
1071
+ }
1072
+ if (curActive) {
1073
+ voidOpenNameOffers(rec.name, ev.height);
1074
+ }
1075
+ putName(rec.name, cand);
1076
+ c.feesPaid += nameRegFee(rec.name, ev.height);
1077
+ note(ev, ev.id, "name", true, curActive ? "displaced prior holder" : void 0);
1078
+ }
1079
+ function applyNfinalize(c, ev, rec, who, feeToTreasury) {
1080
+ const { commits, names, recaptures, pendingNames, note, putName, voidOpenNameOffers } = c;
1081
+ const v25 = ev.height >= V25_HEIGHT;
1082
+ const v26 = ev.height >= V26_HEIGHT;
1083
+ if (!v25) {
1084
+ note(ev, ev.id, "nfinalize", false, "before v2.5 activation");
1085
+ return;
1086
+ }
1087
+ const n = names.get(rec.name);
1088
+ if (n && n.pending && n.owner === who) {
1089
+ const cH = commits.get(nameCommit(rec.name, rec.salt, who));
1090
+ if (cH === void 0 || cH !== n.effHeight) {
1091
+ note(ev, ev.id, "nfinalize", false, "salt does not match the reservation commit");
1092
+ return;
1093
+ }
1094
+ if (!(ev.height > n.effHeight + REG_COMMIT_MAX_BLOCKS)) {
1095
+ note(ev, ev.id, "nfinalize", false, "too early \u2014 displacement contest not yet frozen");
1096
+ return;
1097
+ }
1098
+ if (n.finalizeBy !== void 0 && ev.height > n.finalizeBy) {
1099
+ note(ev, ev.id, "nfinalize", false, "reservation expired");
1100
+ return;
1101
+ }
1102
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1103
+ note(ev, ev.id, "nfinalize", false, "name registration fee unpaid");
1104
+ return;
1105
+ }
1106
+ delete n.pending;
1107
+ delete n.finalizeBy;
1108
+ pendingNames.delete(rec.name);
1109
+ n.paidThroughEpoch = (0, import_csd_codec.epochOf)(ev.height) + NAME_TERM_EPOCHS;
1110
+ c.feesPaid += nameRegFee(rec.name, ev.height);
1111
+ note(ev, ev.id, "nfinalize", true);
1112
+ return;
1113
+ }
1114
+ const r = v26 ? recaptures.get(rec.name) : void 0;
1115
+ if (r && r.owner === who) {
1116
+ const cH = commits.get(nameCommit(rec.name, rec.salt, who));
1117
+ if (cH === void 0 || cH !== r.effHeight) {
1118
+ note(ev, ev.id, "nfinalize", false, "salt does not match the recapture commit");
1119
+ return;
1120
+ }
1121
+ if (!(ev.height > r.effHeight + REG_COMMIT_MAX_BLOCKS)) {
1122
+ note(ev, ev.id, "nfinalize", false, "too early \u2014 recapture contest not yet frozen");
1123
+ return;
1124
+ }
1125
+ if (ev.height > r.finalizeBy) {
1126
+ note(ev, ev.id, "nfinalize", false, "recapture reservation expired");
1127
+ return;
1128
+ }
1129
+ const cur = names.get(rec.name);
1130
+ const ep = (0, import_csd_codec.epochOf)(ev.height);
1131
+ if (!cur || cur.pending || !lapsed(cur, ep)) {
1132
+ recaptures.delete(rec.name);
1133
+ note(ev, ev.id, "nfinalize", false, "name is no longer lapsed");
1134
+ return;
1135
+ }
1136
+ const fee = expiredClaimFee(rec.name, ep - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
1137
+ if (feeToTreasury < fee) {
1138
+ note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
1139
+ return;
1140
+ }
1141
+ voidOpenNameOffers(rec.name, ev.height);
1142
+ putName(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
1143
+ recaptures.delete(rec.name);
1144
+ c.feesPaid += fee;
1145
+ note(ev, ev.id, "nfinalize", true, "recapture finalized (premium)");
1146
+ return;
1147
+ }
1148
+ note(ev, ev.id, "nfinalize", false, "no matching pending reservation you own");
1149
+ }
1150
+ function applyNxfer(c, ev, rec, who) {
1151
+ const { names, note } = c;
1152
+ const v15 = ev.height >= V15_HEIGHT;
1153
+ const n = names.get(rec.name);
1154
+ if (!n || n.owner !== who) {
1155
+ note(ev, ev.id, "nxfer", false, "not the name owner");
1156
+ return;
1157
+ }
1158
+ if (n.pending) {
1159
+ note(ev, ev.id, "nxfer", false, "name reservation not yet finalized");
1160
+ return;
1161
+ }
1162
+ if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1163
+ note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
1164
+ return;
1165
+ }
1166
+ if (n.locked) {
1167
+ note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
1168
+ return;
1169
+ }
1170
+ n.owner = rec.to.toLowerCase();
1171
+ n.addr = void 0;
1172
+ n.profile = void 0;
1173
+ note(ev, ev.id, "nxfer", true);
1174
+ }
1175
+ function applyNset(c, ev, rec, who) {
1176
+ const { names, note } = c;
1177
+ const v15 = ev.height >= V15_HEIGHT;
1178
+ const v23 = ev.height >= V23_HEIGHT;
1179
+ const n = names.get(rec.name);
1180
+ if (!n || n.owner !== who) {
1181
+ note(ev, ev.id, "nset", false, "not the name owner");
1182
+ return;
1183
+ }
1184
+ if (n.pending) {
1185
+ note(ev, ev.id, "nset", false, "name reservation not yet finalized");
1186
+ return;
1187
+ }
1188
+ if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1189
+ note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
1190
+ return;
1191
+ }
1192
+ if (v23 && rec.addr.toLowerCase() === ZERO_ADDR) n.addr = void 0;
1193
+ else n.addr = rec.addr.toLowerCase();
1194
+ note(ev, ev.id, "nset", true);
1195
+ }
1196
+ function applyNprofile(c, ev, rec, who) {
1197
+ const { names, note } = c;
1198
+ const v15 = ev.height >= V15_HEIGHT;
1199
+ const v19 = ev.height >= V19_HEIGHT;
1200
+ if (!v19) {
1201
+ note(ev, ev.id, "nprofile", false, "before v1.9 activation");
1202
+ return;
1203
+ }
1204
+ const n = names.get(rec.name);
1205
+ if (!n || n.owner !== who) {
1206
+ note(ev, ev.id, "nprofile", false, "not the name owner");
1207
+ return;
1208
+ }
1209
+ if (n.pending) {
1210
+ note(ev, ev.id, "nprofile", false, "name reservation not yet finalized");
1211
+ return;
1212
+ }
1213
+ if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1214
+ note(ev, ev.id, "nprofile", false, "lease lapsed \u2014 claim it instead");
1215
+ return;
1216
+ }
1217
+ n.profile = Object.keys(rec.p).length ? { ...rec.p } : void 0;
1218
+ note(ev, ev.id, "nprofile", true);
1219
+ }
1220
+ function applyNrenew(c, ev, rec, who, feeToTreasury) {
1221
+ const { names, note } = c;
1222
+ const v15 = ev.height >= V15_HEIGHT;
1223
+ if (!v15) {
1224
+ note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
1225
+ return;
1226
+ }
1227
+ const n = names.get(rec.name);
1228
+ const ep = (0, import_csd_codec.epochOf)(ev.height);
1229
+ if (n && n.pending) {
1230
+ note(ev, ev.id, "nrenew", false, "name reservation not yet finalized");
1231
+ return;
1232
+ }
1233
+ if (!n || lapsed(n, ep)) {
1234
+ note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
1235
+ return;
1236
+ }
1237
+ if (inGrace(n, ep) && who !== n.owner) {
1238
+ note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
1239
+ return;
1240
+ }
1241
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1242
+ note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
1243
+ return;
1244
+ }
1245
+ n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
1246
+ c.feesPaid += nameRegFee(rec.name, ev.height);
1247
+ note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
1248
+ }
1249
+
1250
+ // src/resolve/market.ts
1251
+ function applyOffer(c, ev, rec, who) {
1252
+ const { tokens, names, offers, offerLock, openOffers, bids, bal, note } = c;
1253
+ const v11 = ev.height >= V11_HEIGHT;
1254
+ const v12 = ev.height >= V12_HEIGHT;
1255
+ const v15 = ev.height >= V15_HEIGHT;
1256
+ const v16 = ev.height >= V16_HEIGHT;
1257
+ const wantIsToken = isTokenWant(rec.want);
1258
+ if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && !v12) {
1259
+ note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
1260
+ return;
1261
+ }
1262
+ if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
1263
+ note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
1264
+ return;
1265
+ }
1266
+ const payto = (rec.want.payto ?? who).toLowerCase();
1267
+ if (payto === TREASURY_ADDR) {
1268
+ note(ev, ev.id, "offer", false, "payto cannot be the protocol treasury");
1269
+ return;
1270
+ }
1271
+ if (!Number.isSafeInteger(ev.expiresEpoch)) {
1272
+ note(ev, ev.id, "offer", false, "expiresEpoch out of safe-integer range");
1273
+ return;
1274
+ }
1275
+ if ((0, import_csd_codec.epochOf)(ev.height) > ev.expiresEpoch) {
1276
+ note(ev, ev.id, "offer", false, "already expired at anchor");
1277
+ return;
1278
+ }
1279
+ if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - (0, import_csd_codec.epochOf)(ev.height) > MAX_OFFER_EPOCHS) {
1280
+ note(ev, ev.id, "offer", false, "v2.1: offer duration exceeds the max");
1281
+ return;
1282
+ }
1283
+ const give = rec.give;
1284
+ if (isNameGive(give)) {
1285
+ if (!v11) {
1286
+ note(ev, ev.id, "offer", false, "name offers need v1.1");
1287
+ return;
1288
+ }
1289
+ const n = names.get(give.name);
1290
+ if (!n || n.owner !== who) {
1291
+ note(ev, ev.id, "offer", false, "you don't own this name");
1292
+ return;
1293
+ }
1294
+ if (n.pending) {
1295
+ note(ev, ev.id, "offer", false, "name reservation not yet finalized");
1296
+ return;
1297
+ }
1298
+ if (n.locked) {
1299
+ note(ev, ev.id, "offer", false, "name already locked by another offer");
1300
+ return;
1301
+ }
1302
+ const saleEmbargo = ev.height >= V27_HEIGHT ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
1303
+ if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= saleEmbargo) {
1304
+ note(ev, ev.id, "offer", false, "name too young to sell (must out-age the reveal window)");
1305
+ return;
1306
+ }
1307
+ if (v15 && paidThrough(n) < ev.expiresEpoch) {
1308
+ note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
1309
+ return;
1310
+ }
1311
+ n.locked = true;
1312
+ } else {
1313
+ if (!tokens.has(give.ticker)) {
1314
+ note(ev, ev.id, "offer", false, "unknown ticker");
1315
+ return;
1316
+ }
1317
+ const amt = parseAmount(give.amount);
1318
+ const s = bal(give.ticker, who);
1319
+ if (s.available < amt) {
1320
+ note(ev, ev.id, "offer", false, "insufficient available balance");
1321
+ return;
1322
+ }
1323
+ s.available -= amt;
1324
+ s.locked += amt;
1325
+ offerLock.set(ev.id, amt);
1326
+ }
1327
+ const o = {
1328
+ id: ev.id,
1329
+ seller: who,
1330
+ give,
1331
+ want: wantIsToken ? { ticker: rec.want.ticker, amount: rec.want.amount, payto } : { value: rec.want.value, payto },
1332
+ ...rec.taker ? { taker: rec.taker.toLowerCase() } : {},
1333
+ ...rec.min !== void 0 ? { min: rec.min, paid: "0", delivered: "0", fills: [] } : {},
1334
+ ...rec.bid !== void 0 ? { bid: rec.bid } : {},
1335
+ status: "open",
1336
+ expiresEpoch: ev.expiresEpoch,
1337
+ height: ev.height,
1338
+ feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
1339
+ };
1340
+ offers.set(ev.id, o);
1341
+ openOffers.add(ev.id);
1342
+ const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
1343
+ if (linked) linked.offers.push(ev.id);
1344
+ note(ev, ev.id, "offer", true);
1345
+ }
1346
+ function applyBid(c, ev, rec, who) {
1347
+ const { bids, openBids, note } = c;
1348
+ const v12 = ev.height >= V12_HEIGHT;
1349
+ if (!v12) {
1350
+ note(ev, ev.id, "bid", false, "bids need v1.2");
1351
+ return;
1352
+ }
1353
+ if (!Number.isSafeInteger(ev.expiresEpoch)) {
1354
+ note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
1355
+ return;
1356
+ }
1357
+ if ((0, import_csd_codec.epochOf)(ev.height) > ev.expiresEpoch) {
1358
+ note(ev, ev.id, "bid", false, "already expired at anchor");
1359
+ return;
1360
+ }
1361
+ if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - (0, import_csd_codec.epochOf)(ev.height) > MAX_OFFER_EPOCHS) {
1362
+ note(ev, ev.id, "bid", false, "v2.1: bid duration exceeds the max");
1363
+ return;
1364
+ }
1365
+ bids.set(ev.id, {
1366
+ id: ev.id,
1367
+ bidder: who,
1368
+ want: rec.want,
1369
+ give: rec.give,
1370
+ status: "open",
1371
+ expiresEpoch: ev.expiresEpoch,
1372
+ height: ev.height,
1373
+ offers: []
1374
+ });
1375
+ openBids.add(ev.id);
1376
+ note(ev, ev.id, "bid", true);
1377
+ }
1378
+ function applyOcancel(c, ev, rec, who) {
1379
+ const { offers, note, releaseGive, setOfferStatus } = c;
1380
+ const v12 = ev.height >= V12_HEIGHT;
1381
+ if (!v12) {
1382
+ note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
1383
+ return;
1384
+ }
1385
+ const targets = [];
1386
+ for (const o of offers.values()) {
1387
+ if (o.status !== "open" || o.seller !== who) continue;
1388
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
1389
+ if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
1390
+ if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
1391
+ targets.push(o);
1392
+ }
1393
+ if (ev.height >= V14_HEIGHT) {
1394
+ c.pendingCancels.push(() => {
1395
+ let n = 0;
1396
+ for (const o of targets) {
1397
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
1398
+ if (o.status === "open") {
1399
+ releaseGive(o);
1400
+ setOfferStatus(o, "cancelled");
1401
+ n++;
1402
+ }
1403
+ }
1404
+ note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
1405
+ });
1406
+ } else {
1407
+ for (const o of targets) {
1408
+ releaseGive(o);
1409
+ setOfferStatus(o, "cancelled");
1410
+ }
1411
+ note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
1412
+ }
1413
+ }
1414
+ function applyFclaim(c, ev, rec, who) {
1415
+ const { offers, fclaims, note, liveHoldCount } = c;
1416
+ if (ev.height < V28_HEIGHT) {
1417
+ note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1418
+ return;
1419
+ }
1420
+ const target = offers.get(rec.offer);
1421
+ const E = ev.expiresEpoch;
1422
+ const deny = (why) => {
1423
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1424
+ note(ev, ev.id, "fclaim", false, why);
1425
+ };
1426
+ if (!Number.isSafeInteger(E)) {
1427
+ deny("expiresEpoch out of safe-integer range");
1428
+ return;
1429
+ }
1430
+ if (!target) {
1431
+ deny("unknown offer");
1432
+ return;
1433
+ }
1434
+ if (target.status !== "open") {
1435
+ deny(`offer ${target.status}`);
1436
+ return;
1437
+ }
1438
+ if (target.taker) {
1439
+ deny("taker-bound offer needs no claim");
1440
+ return;
1441
+ }
1442
+ if (isTokenWant(target.want)) {
1443
+ deny("claims are for CSD-priced offers");
1444
+ return;
1445
+ }
1446
+ if (E > effExpiry(target, ev.height)) {
1447
+ deny("hold would outlive the offer expiry");
1448
+ return;
1449
+ }
1450
+ if (E > (0, import_csd_codec.epochOf)(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1451
+ deny("hold too far ahead (anti-squat)");
1452
+ return;
1453
+ }
1454
+ if ((0, import_csd_codec.epochOf)(ev.height) > E) {
1455
+ deny("expiry already in the past");
1456
+ return;
1457
+ }
1458
+ if (claimHeld(target, ev.height)) {
1459
+ deny("offer already claimed (hold live)");
1460
+ return;
1461
+ }
1462
+ if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1463
+ deny("claim cooldown (you just held this offer)");
1464
+ return;
1465
+ }
1466
+ if (liveHoldCount(who, ev.height) >= MAX_ACTIVE_CLAIMS) {
1467
+ deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1468
+ return;
1469
+ }
1470
+ target.claimedBy = who;
1471
+ target.claimUntilHeight = (E + 1) * import_csd_codec.EPOCH_LEN;
1472
+ target.claimTxid = ev.id;
1473
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1474
+ note(ev, ev.id, "fclaim", true);
1475
+ }
1476
+
1477
+ // src/resolve/tokens.ts
1478
+ function applyDeploy(c, ev, rec, who, feeToTreasury) {
1479
+ const { tokens, note } = c;
1480
+ const v11 = ev.height >= V11_HEIGHT;
1481
+ if (tokens.has(rec.ticker)) {
1482
+ note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
1483
+ return;
1484
+ }
1485
+ if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
1486
+ note(ev, ev.id, "deploy", false, "deploy fee unpaid");
1487
+ return;
1488
+ }
1489
+ const supply = parseAmount(rec.supply);
1490
+ const mintLimit = rec.mint === "open" ? parseAmount(rec.mintLimit) : null;
1491
+ tokens.set(rec.ticker, {
1492
+ minted: 0n,
1493
+ supply,
1494
+ mintLimit,
1495
+ 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 }
1496
+ });
1497
+ if (v11) c.feesPaid += BigInt(DEPLOY_FEE);
1498
+ note(ev, ev.id, "deploy", true);
1499
+ }
1500
+ function applyMint(c, ev, rec, who) {
1501
+ const { tokens, bal, note } = c;
1502
+ const tok = tokens.get(rec.ticker);
1503
+ if (!tok) {
1504
+ note(ev, ev.id, "mint", false, "unknown ticker");
1505
+ return;
1506
+ }
1507
+ const remaining = tok.supply - tok.minted;
1508
+ if (remaining <= 0n) {
1509
+ note(ev, ev.id, "mint", false, "supply exhausted");
1510
+ return;
1511
+ }
1512
+ let credit;
1513
+ if (tok.meta.mint === "issuer") {
1514
+ if (who !== tok.meta.deployer) {
1515
+ note(ev, ev.id, "mint", false, "issuer-only mint");
1516
+ return;
1517
+ }
1518
+ const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
1519
+ if (req === null) {
1520
+ note(ev, ev.id, "mint", false, "issuer mint requires amount");
1521
+ return;
1522
+ }
1523
+ credit = req < remaining ? req : remaining;
1524
+ } else {
1525
+ const req = rec.amount !== void 0 ? parseAmount(rec.amount) : tok.mintLimit;
1526
+ const capped = req < tok.mintLimit ? req : tok.mintLimit;
1527
+ credit = capped < remaining ? capped : remaining;
1528
+ }
1529
+ tok.minted += credit;
1530
+ bal(rec.ticker, who).available += credit;
1531
+ note(ev, ev.id, "mint", true, credit.toString());
1532
+ }
1533
+ function applyTransfer(c, ev, rec, who) {
1534
+ const { tokens, bal, note } = c;
1535
+ if (!tokens.has(rec.ticker)) {
1536
+ note(ev, ev.id, "transfer", false, "unknown ticker");
1537
+ return;
1538
+ }
1539
+ const amt = parseAmount(rec.amount);
1540
+ const from = bal(rec.ticker, who);
1541
+ if (from.available < amt) {
1542
+ note(ev, ev.id, "transfer", false, "insufficient available balance");
1543
+ return;
1544
+ }
1545
+ from.available -= amt;
1546
+ bal(rec.ticker, rec.to.toLowerCase()).available += amt;
1547
+ note(ev, ev.id, "transfer", true);
1548
+ }
1549
+ function applyTmeta(c, ev, rec, who) {
1550
+ const { tokens, note } = c;
1551
+ const v15 = ev.height >= V15_HEIGHT;
1552
+ if (!v15) {
1553
+ note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
1554
+ return;
1555
+ }
1556
+ const tok = tokens.get(rec.ticker);
1557
+ if (!tok) {
1558
+ note(ev, ev.id, "tmeta", false, "unknown ticker");
1559
+ return;
1560
+ }
1561
+ if (who !== tok.meta.deployer) {
1562
+ note(ev, ev.id, "tmeta", false, "issuer-only metadata");
1563
+ return;
1564
+ }
1565
+ tok.meta.tmeta = rec.hash;
1566
+ note(ev, ev.id, "tmeta", true);
1567
+ }
1568
+
1569
+ // src/resolve.ts
1570
+ var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
1571
+ function resolve(events, tipHeight) {
1572
+ const ordered = [...events].sort(
1573
+ (a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
1574
+ a.kind === "propose" ? a.id : a.txid,
1575
+ b.kind === "propose" ? b.id : b.txid
1576
+ )
1577
+ );
1578
+ const eid = (e) => e.kind === "propose" ? e.id : e.txid;
1579
+ const seenIds = /* @__PURE__ */ new Set();
1580
+ const applied = [];
1581
+ for (const e of ordered) {
1582
+ if (e.height >= V29_HEIGHT) {
1583
+ const k = eid(e);
1584
+ if (seenIds.has(k)) continue;
1585
+ seenIds.add(k);
1586
+ }
1587
+ applied.push(e);
1588
+ }
1589
+ const c = createContext();
1590
+ const { tokens, balances, names, recaptures, offers, bids, fclaims, log, note, sweepExpired } = c;
1591
+ let pendingBlock = -1;
1592
+ const applyPendingCancels = () => {
1593
+ for (const f of c.pendingCancels) f();
1594
+ c.pendingCancels = [];
1595
+ };
692
1596
  for (const ev of applied) {
693
1597
  if (ev.height < ACTIVATION_HEIGHT) continue;
694
1598
  if (ev.height !== pendingBlock) {
@@ -696,14 +1600,6 @@ function resolve(events, tipHeight) {
696
1600
  pendingBlock = ev.height;
697
1601
  }
698
1602
  sweepExpired(ev.height);
699
- const v11 = ev.height >= V11_HEIGHT;
700
- const v12 = ev.height >= V12_HEIGHT;
701
- const v15 = ev.height >= V15_HEIGHT;
702
- const v16 = ev.height >= V16_HEIGHT;
703
- const v19 = ev.height >= V19_HEIGHT;
704
- const v23 = ev.height >= V23_HEIGHT;
705
- const v25 = ev.height >= V25_HEIGHT;
706
- const v26 = ev.height >= V26_HEIGHT;
707
1603
  const feeToTreasury = ev.kind === "propose" ? ptAmt((ev.paidTo ?? {})[TREASURY_ADDR]) : 0n;
708
1604
  if (ev.kind === "propose") {
709
1605
  const rec = parseRecord(ev.uri, ev.payloadHash);
@@ -712,812 +1608,55 @@ function resolve(events, tipHeight) {
712
1608
  continue;
713
1609
  }
714
1610
  const who = ev.proposer.toLowerCase();
715
- if (rec.t === "deploy") {
716
- if (tokens.has(rec.ticker)) {
717
- note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
718
- continue;
719
- }
720
- if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
721
- note(ev, ev.id, "deploy", false, "deploy fee unpaid");
722
- continue;
723
- }
724
- const supply = parseAmount(rec.supply);
725
- const mintLimit = rec.mint === "open" ? parseAmount(rec.mintLimit) : null;
726
- tokens.set(rec.ticker, {
727
- minted: 0n,
728
- supply,
729
- mintLimit,
730
- 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 }
731
- });
732
- if (v11) feesPaid += BigInt(DEPLOY_FEE);
733
- note(ev, ev.id, "deploy", true);
734
- } else if (rec.t === "mint") {
735
- const tok = tokens.get(rec.ticker);
736
- if (!tok) {
737
- note(ev, ev.id, "mint", false, "unknown ticker");
738
- continue;
739
- }
740
- const remaining = tok.supply - tok.minted;
741
- if (remaining <= 0n) {
742
- note(ev, ev.id, "mint", false, "supply exhausted");
743
- continue;
744
- }
745
- let credit;
746
- if (tok.meta.mint === "issuer") {
747
- if (who !== tok.meta.deployer) {
748
- note(ev, ev.id, "mint", false, "issuer-only mint");
749
- continue;
750
- }
751
- const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
752
- if (req === null) {
753
- note(ev, ev.id, "mint", false, "issuer mint requires amount");
754
- continue;
755
- }
756
- credit = req < remaining ? req : remaining;
757
- } else {
758
- const req = rec.amount !== void 0 ? parseAmount(rec.amount) : tok.mintLimit;
759
- const capped = req < tok.mintLimit ? req : tok.mintLimit;
760
- credit = capped < remaining ? capped : remaining;
761
- }
762
- tok.minted += credit;
763
- bal(rec.ticker, who).available += credit;
764
- note(ev, ev.id, "mint", true, credit.toString());
765
- } else if (rec.t === "transfer") {
766
- if (!tokens.has(rec.ticker)) {
767
- note(ev, ev.id, "transfer", false, "unknown ticker");
768
- continue;
769
- }
770
- const amt = parseAmount(rec.amount);
771
- const from = bal(rec.ticker, who);
772
- if (from.available < amt) {
773
- note(ev, ev.id, "transfer", false, "insufficient available balance");
774
- continue;
775
- }
776
- from.available -= amt;
777
- bal(rec.ticker, rec.to.toLowerCase()).available += amt;
778
- note(ev, ev.id, "transfer", true);
779
- } else if (rec.t === "ncommit") {
780
- if (!v11) {
781
- note(ev, ev.id, "ncommit", false, "before v1.1 activation");
782
- continue;
783
- }
784
- const prev = commits.get(rec.commit);
785
- if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
786
- note(ev, ev.id, "ncommit", true);
787
- } else if (rec.t === "name") {
788
- if (!v11) {
789
- note(ev, ev.id, "name", false, "before v1.1 activation");
790
- continue;
791
- }
792
- const regWindow = v25 || v26 ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
793
- let effHeight = ev.height;
794
- if (rec.salt !== void 0) {
795
- const ch = nameCommit(rec.name, rec.salt, who);
796
- const cH = commits.get(ch);
797
- if (cH === void 0 || cH >= ev.height || ev.height - cH > regWindow) {
798
- note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
799
- continue;
800
- }
801
- effHeight = cH;
802
- }
803
- const cur = names.get(rec.name);
804
- const epClaim = (0, import_csd_codec.epochOf)(ev.height);
805
- const curActive = cur && cur.pending && cur.finalizeBy !== void 0 && ev.height > cur.finalizeBy ? void 0 : cur;
806
- if (curActive && !curActive.pending && v15 && lapsed(curActive, epClaim)) {
807
- if (v26) {
808
- if (rec.salt === void 0) {
809
- note(ev, ev.id, "name", false, "v2.6: recapture requires a commit-reveal (salt)");
810
- continue;
811
- }
812
- const cr = recaptures.get(rec.name);
813
- const crActive = cr && ev.height > cr.finalizeBy ? void 0 : cr;
814
- const better2 = !crActive || earlierAnchor(effHeight, ev.pos, ev.id, crActive);
815
- if (!better2) {
816
- note(ev, ev.id, "name", false, "recapture already reserved (earlier anchor wins)");
817
- continue;
818
- }
819
- let myR = 0;
820
- for (const [nm, r] of recaptures) if (r.owner === who && nm !== rec.name && ev.height <= r.finalizeBy) myR++;
821
- if (myR >= MAX_PENDING_REG) {
822
- note(ev, ev.id, "name", false, "too many pending recaptures (max reached)");
823
- continue;
824
- }
825
- 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 });
826
- note(ev, ev.id, "name", true, crActive ? "recapture reserved (displaced prior reservation)" : "recapture reserved (pending finalize)");
827
- continue;
828
- }
829
- const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(curActive) + NAME_GRACE_EPOCHS), ev.height);
830
- if (feeToTreasury < fee) {
831
- note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
832
- continue;
833
- }
834
- voidOpenNameOffers(rec.name, ev.height);
835
- putName(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
836
- feesPaid += fee;
837
- note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
838
- continue;
839
- }
840
- if (v25) {
841
- if (rec.salt === void 0) {
842
- note(ev, ev.id, "name", false, "v2.5: registration requires a commit-reveal (salt)");
843
- continue;
844
- }
845
- const better2 = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
846
- if (!better2) {
847
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
848
- continue;
849
- }
850
- let myPending = 0;
851
- for (const [nm, n] of names) if (n.pending && n.owner === who && nm !== rec.name && n.finalizeBy !== void 0 && ev.height <= n.finalizeBy) myPending++;
852
- if (myPending >= MAX_PENDING_REG) {
853
- note(ev, ev.id, "name", false, "too many pending reservations (max reached)");
854
- continue;
855
- }
856
- if (curActive) {
857
- voidOpenNameOffers(rec.name, ev.height);
858
- }
859
- 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 });
860
- note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
861
- continue;
862
- }
863
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
864
- note(ev, ev.id, "name", false, "name registration fee unpaid");
865
- continue;
866
- }
867
- const cand = {
868
- owner: who,
869
- effHeight,
870
- pos: ev.pos,
871
- id: ev.id,
872
- height: ev.height,
873
- locked: false,
874
- ...v15 ? { paidThroughEpoch: epClaim + NAME_TERM_EPOCHS } : {}
875
- };
876
- const better = !curActive || !curActive.viaFill && earlierAnchor(effHeight, ev.pos, ev.id, curActive);
877
- if (!better) {
878
- note(ev, ev.id, "name", false, curActive?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
879
- continue;
880
- }
881
- if (curActive) {
882
- voidOpenNameOffers(rec.name, ev.height);
883
- }
884
- putName(rec.name, cand);
885
- feesPaid += nameRegFee(rec.name, ev.height);
886
- note(ev, ev.id, "name", true, curActive ? "displaced prior holder" : void 0);
887
- } else if (rec.t === "nfinalize") {
888
- if (!v25) {
889
- note(ev, ev.id, "nfinalize", false, "before v2.5 activation");
890
- continue;
891
- }
892
- const n = names.get(rec.name);
893
- if (n && n.pending && n.owner === who) {
894
- const cH = commits.get(nameCommit(rec.name, rec.salt, who));
895
- if (cH === void 0 || cH !== n.effHeight) {
896
- note(ev, ev.id, "nfinalize", false, "salt does not match the reservation commit");
897
- continue;
898
- }
899
- if (!(ev.height > n.effHeight + REG_COMMIT_MAX_BLOCKS)) {
900
- note(ev, ev.id, "nfinalize", false, "too early \u2014 displacement contest not yet frozen");
901
- continue;
902
- }
903
- if (n.finalizeBy !== void 0 && ev.height > n.finalizeBy) {
904
- note(ev, ev.id, "nfinalize", false, "reservation expired");
905
- continue;
906
- }
907
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
908
- note(ev, ev.id, "nfinalize", false, "name registration fee unpaid");
909
- continue;
910
- }
911
- delete n.pending;
912
- delete n.finalizeBy;
913
- pendingNames.delete(rec.name);
914
- n.paidThroughEpoch = (0, import_csd_codec.epochOf)(ev.height) + NAME_TERM_EPOCHS;
915
- feesPaid += nameRegFee(rec.name, ev.height);
916
- note(ev, ev.id, "nfinalize", true);
917
- continue;
918
- }
919
- const r = v26 ? recaptures.get(rec.name) : void 0;
920
- if (r && r.owner === who) {
921
- const cH = commits.get(nameCommit(rec.name, rec.salt, who));
922
- if (cH === void 0 || cH !== r.effHeight) {
923
- note(ev, ev.id, "nfinalize", false, "salt does not match the recapture commit");
924
- continue;
925
- }
926
- if (!(ev.height > r.effHeight + REG_COMMIT_MAX_BLOCKS)) {
927
- note(ev, ev.id, "nfinalize", false, "too early \u2014 recapture contest not yet frozen");
928
- continue;
929
- }
930
- if (ev.height > r.finalizeBy) {
931
- note(ev, ev.id, "nfinalize", false, "recapture reservation expired");
932
- continue;
933
- }
934
- const cur = names.get(rec.name);
935
- const ep = (0, import_csd_codec.epochOf)(ev.height);
936
- if (!cur || cur.pending || !lapsed(cur, ep)) {
937
- recaptures.delete(rec.name);
938
- note(ev, ev.id, "nfinalize", false, "name is no longer lapsed");
939
- continue;
940
- }
941
- const fee = expiredClaimFee(rec.name, ep - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
942
- if (feeToTreasury < fee) {
943
- note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
944
- continue;
945
- }
946
- voidOpenNameOffers(rec.name, ev.height);
947
- putName(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
948
- recaptures.delete(rec.name);
949
- feesPaid += fee;
950
- note(ev, ev.id, "nfinalize", true, "recapture finalized (premium)");
951
- continue;
952
- }
953
- note(ev, ev.id, "nfinalize", false, "no matching pending reservation you own");
954
- } else if (rec.t === "nxfer") {
955
- const n = names.get(rec.name);
956
- if (!n || n.owner !== who) {
957
- note(ev, ev.id, "nxfer", false, "not the name owner");
958
- continue;
959
- }
960
- if (n.pending) {
961
- note(ev, ev.id, "nxfer", false, "name reservation not yet finalized");
962
- continue;
963
- }
964
- if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
965
- note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
966
- continue;
967
- }
968
- if (n.locked) {
969
- note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
970
- continue;
971
- }
972
- n.owner = rec.to.toLowerCase();
973
- n.addr = void 0;
974
- n.profile = void 0;
975
- note(ev, ev.id, "nxfer", true);
976
- } else if (rec.t === "nset") {
977
- const n = names.get(rec.name);
978
- if (!n || n.owner !== who) {
979
- note(ev, ev.id, "nset", false, "not the name owner");
980
- continue;
981
- }
982
- if (n.pending) {
983
- note(ev, ev.id, "nset", false, "name reservation not yet finalized");
984
- continue;
985
- }
986
- if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
987
- note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
988
- continue;
989
- }
990
- if (v23 && rec.addr.toLowerCase() === ZERO_ADDR) n.addr = void 0;
991
- else n.addr = rec.addr.toLowerCase();
992
- note(ev, ev.id, "nset", true);
993
- } else if (rec.t === "nprofile") {
994
- if (!v19) {
995
- note(ev, ev.id, "nprofile", false, "before v1.9 activation");
996
- continue;
997
- }
998
- const n = names.get(rec.name);
999
- if (!n || n.owner !== who) {
1000
- note(ev, ev.id, "nprofile", false, "not the name owner");
1001
- continue;
1002
- }
1003
- if (n.pending) {
1004
- note(ev, ev.id, "nprofile", false, "name reservation not yet finalized");
1005
- continue;
1006
- }
1007
- if (v15 && lapsed(n, (0, import_csd_codec.epochOf)(ev.height))) {
1008
- note(ev, ev.id, "nprofile", false, "lease lapsed \u2014 claim it instead");
1009
- continue;
1010
- }
1011
- n.profile = Object.keys(rec.p).length ? { ...rec.p } : void 0;
1012
- note(ev, ev.id, "nprofile", true);
1013
- } else if (rec.t === "offer") {
1014
- const wantIsToken = isTokenWant(rec.want);
1015
- if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && !v12) {
1016
- note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
1017
- continue;
1018
- }
1019
- if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
1020
- note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
1021
- continue;
1022
- }
1023
- const payto = (rec.want.payto ?? who).toLowerCase();
1024
- if (payto === TREASURY_ADDR) {
1025
- note(ev, ev.id, "offer", false, "payto cannot be the protocol treasury");
1026
- continue;
1027
- }
1028
- if (!Number.isSafeInteger(ev.expiresEpoch)) {
1029
- note(ev, ev.id, "offer", false, "expiresEpoch out of safe-integer range");
1030
- continue;
1031
- }
1032
- if ((0, import_csd_codec.epochOf)(ev.height) > ev.expiresEpoch) {
1033
- note(ev, ev.id, "offer", false, "already expired at anchor");
1034
- continue;
1035
- }
1036
- if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - (0, import_csd_codec.epochOf)(ev.height) > MAX_OFFER_EPOCHS) {
1037
- note(ev, ev.id, "offer", false, "v2.1: offer duration exceeds the max");
1038
- continue;
1039
- }
1040
- const give = rec.give;
1041
- if (isNameGive(give)) {
1042
- if (!v11) {
1043
- note(ev, ev.id, "offer", false, "name offers need v1.1");
1044
- continue;
1045
- }
1046
- const n = names.get(give.name);
1047
- if (!n || n.owner !== who) {
1048
- note(ev, ev.id, "offer", false, "you don't own this name");
1049
- continue;
1050
- }
1051
- if (n.pending) {
1052
- note(ev, ev.id, "offer", false, "name reservation not yet finalized");
1053
- continue;
1054
- }
1055
- if (n.locked) {
1056
- note(ev, ev.id, "offer", false, "name already locked by another offer");
1057
- continue;
1058
- }
1059
- const saleEmbargo = ev.height >= V27_HEIGHT ? REG_COMMIT_MAX_BLOCKS : COMMIT_MAX_BLOCKS;
1060
- if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= saleEmbargo) {
1061
- note(ev, ev.id, "offer", false, "name too young to sell (must out-age the reveal window)");
1062
- continue;
1063
- }
1064
- if (v15 && paidThrough(n) < ev.expiresEpoch) {
1065
- note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
1066
- continue;
1067
- }
1068
- n.locked = true;
1069
- } else {
1070
- if (!tokens.has(give.ticker)) {
1071
- note(ev, ev.id, "offer", false, "unknown ticker");
1072
- continue;
1073
- }
1074
- const amt = parseAmount(give.amount);
1075
- const s = bal(give.ticker, who);
1076
- if (s.available < amt) {
1077
- note(ev, ev.id, "offer", false, "insufficient available balance");
1078
- continue;
1079
- }
1080
- s.available -= amt;
1081
- s.locked += amt;
1082
- offerLock.set(ev.id, amt);
1083
- }
1084
- const o = {
1085
- id: ev.id,
1086
- seller: who,
1087
- give,
1088
- want: wantIsToken ? { ticker: rec.want.ticker, amount: rec.want.amount, payto } : { value: rec.want.value, payto },
1089
- ...rec.taker ? { taker: rec.taker.toLowerCase() } : {},
1090
- ...rec.min !== void 0 ? { min: rec.min, paid: "0", delivered: "0", fills: [] } : {},
1091
- ...rec.bid !== void 0 ? { bid: rec.bid } : {},
1092
- status: "open",
1093
- expiresEpoch: ev.expiresEpoch,
1094
- height: ev.height,
1095
- feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
1096
- };
1097
- offers.set(ev.id, o);
1098
- openOffers.add(ev.id);
1099
- const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
1100
- if (linked) linked.offers.push(ev.id);
1101
- note(ev, ev.id, "offer", true);
1102
- } else if (rec.t === "bid") {
1103
- if (!v12) {
1104
- note(ev, ev.id, "bid", false, "bids need v1.2");
1105
- continue;
1106
- }
1107
- if (!Number.isSafeInteger(ev.expiresEpoch)) {
1108
- note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
1109
- continue;
1110
- }
1111
- if ((0, import_csd_codec.epochOf)(ev.height) > ev.expiresEpoch) {
1112
- note(ev, ev.id, "bid", false, "already expired at anchor");
1113
- continue;
1114
- }
1115
- if (ev.height >= V21_HEIGHT && ev.height < V22_HEIGHT && ev.expiresEpoch - (0, import_csd_codec.epochOf)(ev.height) > MAX_OFFER_EPOCHS) {
1116
- note(ev, ev.id, "bid", false, "v2.1: bid duration exceeds the max");
1117
- continue;
1118
- }
1119
- bids.set(ev.id, {
1120
- id: ev.id,
1121
- bidder: who,
1122
- want: rec.want,
1123
- give: rec.give,
1124
- status: "open",
1125
- expiresEpoch: ev.expiresEpoch,
1126
- height: ev.height,
1127
- offers: []
1128
- });
1129
- openBids.add(ev.id);
1130
- note(ev, ev.id, "bid", true);
1131
- } else if (rec.t === "ocancel") {
1132
- if (!v12) {
1133
- note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
1134
- continue;
1135
- }
1136
- const targets = [];
1137
- for (const o of offers.values()) {
1138
- if (o.status !== "open" || o.seller !== who) continue;
1139
- if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
1140
- if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
1141
- if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
1142
- targets.push(o);
1143
- }
1144
- if (ev.height >= V14_HEIGHT) {
1145
- pendingCancels.push(() => {
1146
- let n = 0;
1147
- for (const o of targets) {
1148
- if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
1149
- if (o.status === "open") {
1150
- releaseGive(o);
1151
- setOfferStatus(o, "cancelled");
1152
- n++;
1153
- }
1154
- }
1155
- note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
1156
- });
1157
- } else {
1158
- for (const o of targets) {
1159
- releaseGive(o);
1160
- setOfferStatus(o, "cancelled");
1161
- }
1162
- note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
1163
- }
1164
- } else if (rec.t === "nrenew") {
1165
- if (!v15) {
1166
- note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
1167
- continue;
1168
- }
1169
- const n = names.get(rec.name);
1170
- const ep = (0, import_csd_codec.epochOf)(ev.height);
1171
- if (n && n.pending) {
1172
- note(ev, ev.id, "nrenew", false, "name reservation not yet finalized");
1173
- continue;
1174
- }
1175
- if (!n || lapsed(n, ep)) {
1176
- note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
1177
- continue;
1178
- }
1179
- if (inGrace(n, ep) && who !== n.owner) {
1180
- note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
1181
- continue;
1182
- }
1183
- if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
1184
- note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
1185
- continue;
1186
- }
1187
- n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
1188
- feesPaid += nameRegFee(rec.name, ev.height);
1189
- note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
1190
- } else if (rec.t === "tmeta") {
1191
- if (!v15) {
1192
- note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
1193
- continue;
1194
- }
1195
- const tok = tokens.get(rec.ticker);
1196
- if (!tok) {
1197
- note(ev, ev.id, "tmeta", false, "unknown ticker");
1198
- continue;
1199
- }
1200
- if (who !== tok.meta.deployer) {
1201
- note(ev, ev.id, "tmeta", false, "issuer-only metadata");
1202
- continue;
1203
- }
1204
- tok.meta.tmeta = rec.hash;
1205
- note(ev, ev.id, "tmeta", true);
1206
- } else if (rec.t === "fclaim") {
1207
- if (ev.height < V28_HEIGHT) {
1208
- note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1209
- continue;
1210
- }
1211
- const target = offers.get(rec.offer);
1212
- const E = ev.expiresEpoch;
1213
- const deny = (why) => {
1214
- fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1215
- note(ev, ev.id, "fclaim", false, why);
1216
- };
1217
- if (!Number.isSafeInteger(E)) {
1218
- deny("expiresEpoch out of safe-integer range");
1219
- continue;
1220
- }
1221
- if (!target) {
1222
- deny("unknown offer");
1223
- continue;
1224
- }
1225
- if (target.status !== "open") {
1226
- deny(`offer ${target.status}`);
1227
- continue;
1228
- }
1229
- if (target.taker) {
1230
- deny("taker-bound offer needs no claim");
1231
- continue;
1232
- }
1233
- if (isTokenWant(target.want)) {
1234
- deny("claims are for CSD-priced offers");
1235
- continue;
1236
- }
1237
- if (E > effExpiry(target, ev.height)) {
1238
- deny("hold would outlive the offer expiry");
1239
- continue;
1240
- }
1241
- if (E > (0, import_csd_codec.epochOf)(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1242
- deny("hold too far ahead (anti-squat)");
1243
- continue;
1244
- }
1245
- if ((0, import_csd_codec.epochOf)(ev.height) > E) {
1246
- deny("expiry already in the past");
1247
- continue;
1248
- }
1249
- if (claimHeld(target, ev.height)) {
1250
- deny("offer already claimed (hold live)");
1251
- continue;
1252
- }
1253
- if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1254
- deny("claim cooldown (you just held this offer)");
1255
- continue;
1256
- }
1257
- let liveN = 0;
1258
- for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height) && (ev.height < V29_HEIGHT || x.status === "open")) liveN++;
1259
- if (liveN >= MAX_ACTIVE_CLAIMS) {
1260
- deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1261
- continue;
1262
- }
1263
- target.claimedBy = who;
1264
- target.claimUntilHeight = (E + 1) * import_csd_codec.EPOCH_LEN;
1265
- target.claimTxid = ev.id;
1266
- fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1267
- note(ev, ev.id, "fclaim", true);
1268
- }
1269
- } else {
1270
- const who = ev.attester.toLowerCase();
1271
- let o = offers.get(ev.proposalId);
1272
- if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
1273
- const fc = fclaims.get(ev.proposalId);
1274
- if (fc && fc.granted) {
1275
- const linked = offers.get(fc.offer);
1276
- if (linked && linked.claimTxid === ev.proposalId) o = linked;
1277
- }
1278
- }
1279
- if (!o) {
1280
- if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
1281
- note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
1282
- continue;
1283
- }
1284
- const b = bids.get(ev.proposalId);
1285
- if (b && ev.score === SCORE_CANCEL) {
1286
- if (who !== b.bidder) {
1287
- note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
1288
- continue;
1289
- }
1290
- if (b.status !== "open") {
1291
- note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
1292
- continue;
1293
- }
1294
- setBidStatus(b, "cancelled");
1295
- note(ev, ev.txid, "bidcancel", true);
1296
- }
1297
- continue;
1298
- }
1299
- if (ev.score === SCORE_CANCEL) {
1300
- if (who !== o.seller) {
1301
- note(ev, ev.txid, "cancel", false, "only seller may cancel");
1302
- continue;
1303
- }
1304
- if (o.status !== "open") {
1305
- note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
1306
- continue;
1307
- }
1308
- if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
1309
- note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
1310
- continue;
1311
- }
1312
- if (ev.height >= V14_HEIGHT) {
1313
- pendingCancels.push(() => {
1314
- if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
1315
- note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
1316
- return;
1317
- }
1318
- if (o.status === "open") {
1319
- releaseGive(o);
1320
- setOfferStatus(o, "cancelled");
1321
- note(ev, ev.txid, "cancel", true);
1322
- } else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
1323
- });
1324
- } else {
1325
- releaseGive(o);
1326
- setOfferStatus(o, "cancelled");
1327
- note(ev, ev.txid, "cancel", true);
1328
- }
1329
- } else if (ev.score === SCORE_FILL && isTokenWant(o.want)) {
1330
- if (o.status !== "open") {
1331
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1332
- continue;
1333
- }
1334
- if (o.taker && who !== o.taker) {
1335
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1336
- continue;
1337
- }
1338
- if (ev.confidence !== CONF_TOKEN_FILL) {
1339
- note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
1340
- continue;
1341
- }
1342
- if (!tokens.has(o.want.ticker)) {
1343
- note(ev, ev.txid, "fill", false, "want ticker does not exist");
1344
- continue;
1345
- }
1346
- const amt = BigInt(o.want.amount);
1347
- const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
1348
- const buyer = bal(o.want.ticker, who);
1349
- if (buyer.available < amt + fee) {
1350
- note(ev, ev.txid, "fill", false, "insufficient want-token balance");
1351
- continue;
1352
- }
1353
- const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
1354
- if (isNameGive(o.give) && !giveName) {
1355
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
1356
- continue;
1357
- }
1358
- const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
1359
- if (!isNameGive(o.give) && giveLock === void 0) {
1360
- note(ev, ev.txid, "fill", false, "offer lock missing");
1361
- continue;
1362
- }
1363
- buyer.available -= amt + fee;
1364
- bal(o.want.ticker, o.want.payto).available += amt;
1365
- if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
1366
- if (isNameGive(o.give)) deliverNameToBuyer(giveName, who, ev);
1367
- else releaseGiveLock(o, who, giveLock);
1368
- setOfferStatus(o, "filled");
1369
- o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
1370
- markBidDone(o, who);
1371
- note(ev, ev.txid, "fill", true);
1372
- } else if (ev.score === SCORE_FILL && o.min !== void 0 && !isNameGive(o.give)) {
1373
- if (o.status !== "open") {
1374
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1375
- continue;
1376
- }
1377
- if (o.taker && who !== o.taker) {
1378
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1379
- continue;
1380
- }
1381
- const blk = openFillReject(o, ev.height, who, ev.proposalId);
1382
- if (blk) {
1383
- note(ev, ev.txid, "fill", false, blk);
1384
- continue;
1385
- }
1386
- const pt = ev.paidTo ?? {};
1387
- const want = BigInt(o.want.value);
1388
- const paidSoFar = BigInt(o.paid ?? "0");
1389
- const remaining = want - paidSoFar;
1390
- const X = ptAmt(pt[o.want.payto]);
1391
- const minV = BigInt(o.min);
1392
- const effMin = remaining < minV ? remaining : minV;
1393
- if (X < effMin) {
1394
- note(ev, ev.txid, "fill", false, "payment below offer min");
1395
- continue;
1396
- }
1397
- const x = X < remaining ? X : remaining;
1398
- const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
1399
- if (ptAmt(pt[TREASURY_ADDR]) < fee) {
1400
- note(ev, ev.txid, "fill", false, "protocol fee unpaid");
1401
- continue;
1402
- }
1403
- const giveTotal = BigInt(o.give.amount);
1404
- const newPaid = paidSoFar + x;
1405
- const deliveredSoFar = BigInt(o.delivered ?? "0");
1406
- const newDelivered = giveTotal * newPaid / want;
1407
- const out = newDelivered - deliveredSoFar;
1408
- if (out === 0n) {
1409
- note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
1410
- continue;
1411
- }
1412
- const lock = offerLock.get(o.id);
1413
- if (lock === void 0) {
1414
- note(ev, ev.txid, "fill", false, "offer lock missing");
1415
- continue;
1416
- }
1417
- bal(o.give.ticker, o.seller).locked -= out;
1418
- bal(o.give.ticker, who).available += out;
1419
- offerLock.set(o.id, lock - out);
1420
- o.paid = newPaid.toString();
1421
- o.delivered = newDelivered.toString();
1422
- const entry = { buyer: who, txid: ev.txid, height: ev.height, paid: x.toString(), fee: fee.toString(), got: out.toString() };
1423
- (o.fills ??= []).push(entry);
1424
- feesPaid += fee;
1425
- if (newPaid === want) {
1426
- setOfferStatus(o, "filled");
1427
- o.fill = entry;
1428
- offerLock.delete(o.id);
1429
- markBidDone(o, who);
1430
- }
1431
- note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
1432
- } else if (ev.score === SCORE_FILL) {
1433
- if (o.status !== "open") {
1434
- note(ev, ev.txid, "fill", false, `offer ${o.status}`);
1435
- continue;
1436
- }
1437
- if (o.taker && who !== o.taker) {
1438
- note(ev, ev.txid, "fill", false, "taker-bound offer");
1439
- continue;
1440
- }
1441
- const blk = openFillReject(o, ev.height, who, ev.proposalId);
1442
- if (blk) {
1443
- note(ev, ev.txid, "fill", false, blk);
1444
- continue;
1445
- }
1446
- const pt = ev.paidTo ?? {};
1447
- const want = BigInt(o.want.value);
1448
- const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
1449
- const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
1450
- const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
1451
- const need = /* @__PURE__ */ new Map();
1452
- const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
1453
- addNeed(o.want.payto, want);
1454
- addNeed(TREASURY_ADDR, fee);
1455
- if (rebate > 0n) addNeed(o.seller, rebate);
1456
- let unpaid;
1457
- for (const [addr, amt] of need) if (ptAmt(pt[addr]) < amt) {
1458
- unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
1611
+ switch (rec.t) {
1612
+ case "deploy":
1613
+ applyDeploy(c, ev, rec, who, feeToTreasury);
1614
+ break;
1615
+ case "mint":
1616
+ applyMint(c, ev, rec, who);
1617
+ break;
1618
+ case "transfer":
1619
+ applyTransfer(c, ev, rec, who);
1620
+ break;
1621
+ case "tmeta":
1622
+ applyTmeta(c, ev, rec, who);
1623
+ break;
1624
+ case "ncommit":
1625
+ applyNcommit(c, ev, rec);
1626
+ break;
1627
+ case "name":
1628
+ applyName(c, ev, rec, who, feeToTreasury);
1629
+ break;
1630
+ case "nfinalize":
1631
+ applyNfinalize(c, ev, rec, who, feeToTreasury);
1632
+ break;
1633
+ case "nxfer":
1634
+ applyNxfer(c, ev, rec, who);
1635
+ break;
1636
+ case "nset":
1637
+ applyNset(c, ev, rec, who);
1638
+ break;
1639
+ case "nprofile":
1640
+ applyNprofile(c, ev, rec, who);
1641
+ break;
1642
+ case "nrenew":
1643
+ applyNrenew(c, ev, rec, who, feeToTreasury);
1644
+ break;
1645
+ case "offer":
1646
+ applyOffer(c, ev, rec, who);
1647
+ break;
1648
+ case "bid":
1649
+ applyBid(c, ev, rec, who);
1650
+ break;
1651
+ case "ocancel":
1652
+ applyOcancel(c, ev, rec, who);
1653
+ break;
1654
+ case "fclaim":
1655
+ applyFclaim(c, ev, rec, who);
1459
1656
  break;
1460
- }
1461
- if (unpaid) {
1462
- note(ev, ev.txid, "fill", false, unpaid);
1463
- continue;
1464
- }
1465
- const paid = ptAmt(pt[o.want.payto]);
1466
- if (isNameGive(o.give)) {
1467
- const n = names.get(o.give.name);
1468
- if (!n) {
1469
- note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
1470
- continue;
1471
- }
1472
- deliverNameToBuyer(n, who, ev);
1473
- } else {
1474
- const amt = offerLock.get(o.id);
1475
- if (amt === void 0) {
1476
- note(ev, ev.txid, "fill", false, "offer lock missing");
1477
- continue;
1478
- }
1479
- releaseGiveLock(o, who, amt);
1480
- }
1481
- feesPaid += fee;
1482
- setOfferStatus(o, "filled");
1483
- o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
1484
- markBidDone(o, who);
1485
- note(ev, ev.txid, "fill", true);
1486
- } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
1487
- if (ev.height >= V28_HEIGHT) {
1488
- note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
1489
- continue;
1490
- }
1491
- if (o.status !== "open") {
1492
- note(ev, ev.txid, "claim", false, `offer ${o.status}`);
1493
- continue;
1494
- }
1495
- if (o.taker) {
1496
- note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
1497
- continue;
1498
- }
1499
- if (isTokenWant(o.want)) {
1500
- note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
1501
- continue;
1502
- }
1503
- if (claimHeld(o, ev.height)) {
1504
- note(ev, ev.txid, "claim", false, "offer already claimed (hold live)");
1505
- continue;
1506
- }
1507
- if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + claimGrace(o) + CLAIM_COOLDOWN_BLOCKS) {
1508
- note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
1509
- continue;
1510
- }
1511
- let liveN = 0;
1512
- for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height) && (ev.height < V29_HEIGHT || x.status === "open")) liveN++;
1513
- if (liveN >= MAX_ACTIVE_CLAIMS) {
1514
- note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1515
- continue;
1516
- }
1517
- o.claimedBy = who;
1518
- o.claimUntilHeight = ev.height + claimWindowAt(ev.height);
1519
- note(ev, ev.txid, "claim", true);
1520
1657
  }
1658
+ } else {
1659
+ applyAttest(c, ev);
1521
1660
  }
1522
1661
  }
1523
1662
  applyPendingCancels();
@@ -1567,7 +1706,7 @@ function resolve(events, tipHeight) {
1567
1706
  for (const [nm, r] of [...recaptures.entries()].sort(([a], [b]) => ord(a, b))) recapturesOut[nm] = { owner: r.owner, effectiveHeight: r.effHeight, finalizeBy: r.finalizeBy };
1568
1707
  const fclaimsOut = {};
1569
1708
  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 };
1570
- return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: feesPaid.toString() };
1709
+ return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: c.feesPaid.toString() };
1571
1710
  }
1572
1711
  function canonicalState(s) {
1573
1712
  const sortKeys = (v) => {
@@ -1884,10 +2023,10 @@ function bindOfferTerms(servedOffer, t, opts) {
1884
2023
  const om = o?.min;
1885
2024
  if ((om !== void 0 && om !== null) !== (t.min !== void 0)) return true;
1886
2025
  if (t.min !== void 0 && String(om) !== t.min) return true;
1887
- if (opts?.wantType) {
2026
+ if (opts?.wantType !== false) {
1888
2027
  if (t.wantType === void 0 || typeof o?.want?.ticker === "string" !== (t.wantType === "token")) return true;
1889
2028
  }
1890
- if (opts?.give) {
2029
+ if (opts?.give !== false) {
1891
2030
  const g = o?.give;
1892
2031
  if ((g?.ticker !== void 0 && g?.ticker !== null) !== (t.giveTicker !== void 0) || t.giveTicker !== void 0 && String(g?.ticker) !== t.giveTicker) return true;
1893
2032
  if ((g?.amount !== void 0 && g?.amount !== null) !== (t.giveAmount !== void 0) || t.giveAmount !== void 0 && String(g?.amount) !== t.giveAmount) return true;