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