@inversealtruism/cairnx-core 0.1.8 → 0.1.11

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/MAKERS.md ADDED
@@ -0,0 +1,143 @@
1
+ # Run a CairnX market maker
2
+
3
+ CairnX has **no AMM and no escrow** — native CSD can't be pooled (CONVENTION §6). Liquidity is whoever
4
+ runs a *maker loop*. This guide is everything you need to join as a market maker, end to end. Anyone
5
+ may run one, against any token, with their own key. It builds only on the public package
6
+ [`@inversealtruism/cairnx-core`](https://www.npmjs.com/package/@inversealtruism/cairnx-core).
7
+
8
+ ## How the market works (RFQ)
9
+
10
+ Post-v1.3 the CSD-priced market **is** an RFQ loop (CONVENTION §13/§15):
11
+
12
+ 1. A buyer anchors a **bid** — "I'll pay *N* CSD for *X*" (public, on-chain).
13
+ 2. A **maker** (you) answers with a **taker-bound offer**: you give *X*, want *N* CSD, `taker` = the
14
+ bidder, tagged `bid: <bidId>`. Only that bidder can fill it — so there is **no same-block fill race**
15
+ and a losing buyer can never forfeit a payment.
16
+ 3. The bidder **fills** it: one atomic transaction pays you and delivers *X*, or nothing happens.
17
+
18
+ Open (non-taker-bound) CSD offers are rejected by consensus in `[v1.3, v1.7)` — that race-safety is *why*
19
+ making is RFQ. **v1.7** (`V17_HEIGHT`) re-allows open CSD asks but makes them race-safe a second way —
20
+ see *Claim-to-fill* below.
21
+
22
+ ## Why make a market (v1.6 maker rebate)
23
+
24
+ From **v1.6** (`V16_HEIGHT`), every **bid-answered whole fill** pays the maker a **rebate**, taker-funded,
25
+ routed to you (`o.seller`):
26
+
27
+ ```
28
+ maker rebate = 0.25 CSD + 0.5% of the trade value # flat reimburses your 0.25 CSD posting cost
29
+ ```
30
+
31
+ The taker also pays **1.5%** to the protocol treasury (separate). So on a 1 CSD fill you receive the
32
+ 1 CSD price **+ 0.255 CSD rebate**. Net maker economics:
33
+
34
+ ```
35
+ net = Σ rebates earned − Σ anchor (propose) fees spent # 0.25 CSD per offer/cancel you post
36
+ ```
37
+
38
+ Each *filled* answer reimburses its own posting cost; the 0.5% margin covers answers that don't fill.
39
+ Fund a key once and a healthy market funds itself — that's the whole point of v1.6. Live maker P&L for
40
+ the reference bot is published at `/cairnx/mm-health` (and shown on the **Makers** tab at `/trade`).
41
+
42
+ ## Claim-to-fill (v1.7) — open CSD asks, race-safe
43
+
44
+ From **v1.7** (`V17_HEIGHT`) a maker may post an **open** CSD ask (no `taker`) again. Anyone can buy it,
45
+ but a fill is gated on a **claim** — a payment-free `SCORE_CLAIM` attest that reserves the offer for the
46
+ first claimer (by consensus order) for a short window (`CLAIM_WINDOW_BLOCKS` ≈ 30 min). Only the live
47
+ claimer may then fill, so two buyers never race a payment: a losing same-block claim is a no-op costing
48
+ only the 0.05 CSD attest fee. The **maker rebate extends to open-ask fills** — an open ask filled at/after
49
+ v1.7 pays you the same `0.25 CSD + 0.5%`, taker-funded.
50
+
51
+ ⚠ **Buyer safety — claim before you fill.** A fill tx on an open offer you do **not** hold a live claim on
52
+ is **rejected as a fill by consensus, but your payment outputs are still spent** (you paid for nothing).
53
+ Always, in order:
54
+
55
+ ```js
56
+ import { buildClaimTx, buildFillTx, fillPayments } from "cairnx/txbuild"; // reference builders
57
+
58
+ // 1. CLAIM — payment-free attest; reserves the open offer for you.
59
+ const claim = buildClaimTx({ offerId, utxos, priv });
60
+ // submit, then wait for confirmation and re-read state:
61
+ // the offer must show claimedBy == yourAddr && tip < claimUntilHeight
62
+ // 2. FILL — only once your claim is live. Pay price + treasury fee + the maker rebate to o.seller:
63
+ const payments = fillPayments(offer.want.payto, offer.want.value,
64
+ { feeBps: offer.feeBps, rebate: true, seller: offer.seller }); // seller REQUIRED when rebate:true
65
+ const fill = buildFillTx({ offerId, utxos, priv, payments });
66
+ ```
67
+
68
+ `fillPayments({rebate:true})` throws if you omit `seller` — the rebate must route to `o.seller` or the
69
+ resolver no-ops the unmatched output and the fill is lost. A claim is for **CSD-priced open offers only**;
70
+ taker-bound offers (the RFQ path above) fill directly with no claim. Grief bounds: an address holds at most
71
+ `MAX_ACTIVE_CLAIMS` live claims and cannot re-grab a just-lapsed offer for `CLAIM_COOLDOWN_BLOCKS`.
72
+
73
+ ## Quickstart
74
+
75
+ ```bash
76
+ npm i @inversealtruism/cairnx-core @inversealtruism/csd-client
77
+ ```
78
+
79
+ **1. Create a dedicated maker key** (never reuse your main key):
80
+
81
+ ```bash
82
+ # any CSD wallet/CLI; you need its 32-byte privkey + 20-byte addr20
83
+ ```
84
+
85
+ **2. Fund it** with the token inventory you'll sell + a little CSD for anchor fees (0.25 CSD per
86
+ quote). The reference bot halts loudly below a configurable CSD float so it never silently fails.
87
+
88
+ **3. Answer bids.** Each pass: read open bids, compute a robust reference price, and for each bid that
89
+ clears `ref + your margin`, anchor a taker-bound offer:
90
+
91
+ ```js
92
+ import { offer } from "@inversealtruism/cairnx-core";
93
+ import { CsdClient } from "@inversealtruism/csd-client";
94
+
95
+ const client = new CsdClient({ baseUrl: "https://<node-or-proxy>/api/rpc" });
96
+ const bids = await fetch("https://<host>/cairnx/bids?status=open").then(r => r.json());
97
+
98
+ for (const b of bids) {
99
+ if (!worthAnswering(b)) continue; // your pricing: only answer ≥ ref + margin
100
+ const rec = offer({
101
+ give: b.want, // the token/amount the bidder wants
102
+ want: { value: b.give.value }, // the CSD they offered (or less, to compete)
103
+ taker: b.bidder, // bind it to the bidder → race-free
104
+ bid: b.id, // link it (resolvers/UIs + the rebate)
105
+ });
106
+ // anchor `rec.uri` / `rec.payloadHash` as a Propose tx (fee ≥ 0.25 CSD), then submit.
107
+ // when the bidder fills, you receive: price + (0.25 + 0.5%) rebate; treasury gets 1.5%.
108
+ }
109
+ ```
110
+
111
+ `offer()` returns `{ record, uri, payloadHash }` — the canonical bytes to anchor. Build the Propose tx
112
+ with your CSD tx tooling (e.g. `@inversealtruism/csd-tx` `buildPropose`, or the reference
113
+ `buildAnchorTx`), sign with your maker key, and submit to the node `/tx/submit`.
114
+
115
+ ## Safety rails the reference bot ships (and you should keep)
116
+
117
+ - **Robust reference price** — a volume-weighted median over a trailing window, excluding self-fills /
118
+ dust, with a per-pass move clamp and an optional absolute band. A poisoned print can't teleport your
119
+ quotes; a suspicious reference *halts the pass loudly* instead of quoting a bad number.
120
+ - **Per-UTC-day anchor budget** — a forced quote/cancel storm runs out of fuel, loudly.
121
+ - **Min-float halt** — below a CSD threshold the maker stops and surfaces "underfunded" rather than
122
+ silently failing submits.
123
+ - **Short answer expiry + outstanding-answer cap** — a dead maker degrades gracefully; one wave of bids
124
+ can't lock your whole book. Answers expire (consensus-enforced) and release their inventory lock.
125
+ - **One anchor per pass, persisted in-flight state** — the substrate has no child-pays-for-parent, so
126
+ never chain a second anchor off an unconfirmed change output; a restart can't double-post.
127
+ - **Skip on a stale view** — never act when your indexer trails the node tip.
128
+
129
+ ## Honest limits
130
+
131
+ - CSD has no liquid USD price; "value" math is in base units. CSD↔token is RFQ (2 signatures over a few
132
+ blocks), never one-click — that's the no-escrow tradeoff, not a bug.
133
+ - Competition is on price and latency: answer under the bid, answer first. The only money a *losing*
134
+ maker spends is its own anchor fee — the cost of quoting.
135
+ - Self-dealing the rebate is net-negative (you'd still owe the treasury fee + network fee), so it's not
136
+ an exploit.
137
+
138
+ ## Reference
139
+
140
+ - **Protocol:** [`CONVENTION.md`](https://github.com/InverseAltruism/csd-sdk/blob/master/packages/cairnx/CONVENTION.md) — §13 RFQ, §15 taker-binding, §8/§10 fees, §6 no-escrow.
141
+ - **Resolver + record builders:** [`@inversealtruism/cairnx-core`](https://github.com/InverseAltruism/csd-sdk/tree/master/packages/cairnx) (this package) — `offer`, `bid`, `deploy`, `mint`, `parseRecord`, `resolve`, `canonicalState`.
142
+ - **Read endpoints:** `/cairnx/bids`, `/cairnx/offers`, `/cairnx/quotes` (indicative ladder), `/cairnx/mm-health` (live maker P&L), `/cairnx/state` (byte-canonical state).
143
+ - **Determinism:** the resolver is byte-deterministic and cross-language co-signed (see `conformance/`); two honest resolvers converge — that's the trust model.
package/dist/index.cjs CHANGED
@@ -23,22 +23,31 @@ __export(index_exports, {
23
23
  ACTIVATION_HEIGHT: () => ACTIVATION_HEIGHT,
24
24
  ADDR_RE: () => ADDR_RE,
25
25
  AMOUNT_RE: () => AMOUNT_RE,
26
+ CLAIM_COOLDOWN_BLOCKS: () => CLAIM_COOLDOWN_BLOCKS,
27
+ CLAIM_WINDOW_BLOCKS: () => CLAIM_WINDOW_BLOCKS,
26
28
  COMMIT_MAX_BLOCKS: () => COMMIT_MAX_BLOCKS,
27
29
  CONF_TOKEN_FILL: () => CONF_TOKEN_FILL,
28
30
  DEPLOY_FEE: () => DEPLOY_FEE,
29
31
  DOMAIN: () => DOMAIN,
30
32
  EPOCH_LEN: () => EPOCH_LEN,
31
33
  FEE_BPS: () => FEE_BPS,
34
+ FEE_BPS_V16: () => FEE_BPS_V16,
32
35
  HASH_RE: () => HASH_RE,
36
+ MAX_ACTIVE_CLAIMS: () => MAX_ACTIVE_CLAIMS,
33
37
  MAX_AMOUNT: () => MAX_AMOUNT,
34
38
  MAX_RECORD_BYTES: () => MAX_RECORD_BYTES,
39
+ NAME_FEE_SHORT_V18: () => NAME_FEE_SHORT_V18,
40
+ NAME_FEE_V18: () => NAME_FEE_V18,
35
41
  NAME_GRACE_EPOCHS: () => NAME_GRACE_EPOCHS,
36
42
  NAME_PREMIUM_DECAY_EPOCHS: () => NAME_PREMIUM_DECAY_EPOCHS,
37
43
  NAME_PREMIUM_START: () => NAME_PREMIUM_START,
38
44
  NAME_RE: () => NAME_RE,
39
45
  NAME_TERM_EPOCHS: () => NAME_TERM_EPOCHS,
46
+ REBATE_BPS: () => REBATE_BPS,
47
+ REBATE_FLAT: () => REBATE_FLAT,
40
48
  RESERVED_NAMES: () => RESERVED_NAMES,
41
49
  SCORE_CANCEL: () => SCORE_CANCEL,
50
+ SCORE_CLAIM: () => SCORE_CLAIM,
42
51
  SCORE_FILL: () => SCORE_FILL,
43
52
  TICKER_RE: () => TICKER_RE,
44
53
  TREASURY_ADDR: () => TREASURY_ADDR,
@@ -47,6 +56,9 @@ __export(index_exports, {
47
56
  V13_HEIGHT: () => V13_HEIGHT,
48
57
  V14_HEIGHT: () => V14_HEIGHT,
49
58
  V15_HEIGHT: () => V15_HEIGHT,
59
+ V16_HEIGHT: () => V16_HEIGHT,
60
+ V17_HEIGHT: () => V17_HEIGHT,
61
+ V18_HEIGHT: () => V18_HEIGHT,
50
62
  bid: () => bid,
51
63
  buildRecord: () => buildRecord,
52
64
  canonicalState: () => canonicalState,
@@ -56,6 +68,7 @@ __export(index_exports, {
56
68
  isName: () => isName,
57
69
  isNameGive: () => isNameGive,
58
70
  isTokenWant: () => isTokenWant,
71
+ makerRebate: () => makerRebate,
59
72
  mint: () => mint,
60
73
  nameClaim: () => nameClaim,
61
74
  nameCommit: () => nameCommit,
@@ -83,13 +96,15 @@ var V12_HEIGHT = 30300;
83
96
  var V13_HEIGHT = 31100;
84
97
  var V14_HEIGHT = 31400;
85
98
  var V15_HEIGHT = 32e3;
99
+ var V16_HEIGHT = 33600;
100
+ var V17_HEIGHT = 34e3;
86
101
  var EPOCH_LEN = 30;
87
102
  var NAME_TERM_EPOCHS = 8760;
88
103
  var NAME_GRACE_EPOCHS = 720;
89
104
  var NAME_PREMIUM_START = 20n;
90
105
  var NAME_PREMIUM_DECAY_EPOCHS = 720;
91
- function expiredClaimFee(name, epochsPastGraceEnd) {
92
- const base = nameRegFee(name);
106
+ function expiredClaimFee(name, epochsPastGraceEnd, height) {
107
+ const base = nameRegFee(name, height);
93
108
  if (epochsPastGraceEnd >= NAME_PREMIUM_DECAY_EPOCHS) return base;
94
109
  const left = BigInt(NAME_PREMIUM_DECAY_EPOCHS - epochsPastGraceEnd);
95
110
  const mult = 1n + (NAME_PREMIUM_START - 1n) * left / BigInt(NAME_PREMIUM_DECAY_EPOCHS);
@@ -99,11 +114,22 @@ var MAX_RECORD_BYTES = 512;
99
114
  var MAX_AMOUNT = (1n << 96n) - 1n;
100
115
  var SCORE_FILL = 100;
101
116
  var SCORE_CANCEL = 0;
117
+ var SCORE_CLAIM = 50;
118
+ var CLAIM_WINDOW_BLOCKS = 15;
119
+ var MAX_ACTIVE_CLAIMS = 3;
120
+ var CLAIM_COOLDOWN_BLOCKS = 15;
102
121
  var CONF_TOKEN_FILL = 1e6;
103
122
  var TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
104
123
  var FEE_BPS = 100;
124
+ var FEE_BPS_V16 = 150;
125
+ var REBATE_FLAT = 25000000n;
126
+ var REBATE_BPS = 50;
105
127
  var DEPLOY_FEE = 1e8;
106
- function nameRegFee(name) {
128
+ var V18_HEIGHT = 4e4;
129
+ var NAME_FEE_SHORT_V18 = 670000000n;
130
+ var NAME_FEE_V18 = 300000000n;
131
+ function nameRegFee(name, height) {
132
+ if (height >= V18_HEIGHT) return name.length <= 4 ? NAME_FEE_SHORT_V18 : NAME_FEE_V18;
107
133
  const n = name.length;
108
134
  if (n <= 3) return 500000000n;
109
135
  if (n === 4) return 200000000n;
@@ -111,7 +137,8 @@ function nameRegFee(name) {
111
137
  if (n <= 9) return 50000000n;
112
138
  return 10000000n;
113
139
  }
114
- var tradeFee = (want) => (want * BigInt(FEE_BPS) + 9999n) / 10000n;
140
+ var tradeFee = (want, bps = FEE_BPS) => (want * BigInt(bps) + 9999n) / 10000n;
141
+ var makerRebate = (value) => REBATE_FLAT + (value * BigInt(REBATE_BPS) + 9999n) / 10000n;
115
142
  var TICKER_RE = /^[A-Z][A-Z0-9]{2,11}$/;
116
143
  var ADDR_RE = /^0x[0-9a-f]{40}$/;
117
144
  var AMOUNT_RE = /^(0|[1-9][0-9]*)$/;
@@ -398,6 +425,7 @@ function resolve(events, tipHeight) {
398
425
  const b = bids.get(o.bid);
399
426
  if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
400
427
  };
428
+ const liveClaim = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight;
401
429
  for (const ev of ordered) {
402
430
  if (ev.height < ACTIVATION_HEIGHT) continue;
403
431
  if (ev.height !== pendingBlock) {
@@ -408,6 +436,7 @@ function resolve(events, tipHeight) {
408
436
  const v11 = ev.height >= V11_HEIGHT;
409
437
  const v12 = ev.height >= V12_HEIGHT;
410
438
  const v15 = ev.height >= V15_HEIGHT;
439
+ const v16 = ev.height >= V16_HEIGHT;
411
440
  const feeToTreasury = ev.kind === "propose" ? BigInt((ev.paidTo ?? {})[TREASURY_ADDR] ?? "0") : 0n;
412
441
  if (ev.kind === "propose") {
413
442
  const rec = parseRecord(ev.uri, ev.payloadHash);
@@ -506,7 +535,7 @@ function resolve(events, tipHeight) {
506
535
  const cur = names.get(rec.name);
507
536
  const epClaim = epochOf(ev.height);
508
537
  if (cur && v15 && lapsed(cur, epClaim)) {
509
- const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(cur) + NAME_GRACE_EPOCHS));
538
+ const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
510
539
  if (feeToTreasury < fee) {
511
540
  note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
512
541
  continue;
@@ -520,7 +549,7 @@ function resolve(events, tipHeight) {
520
549
  note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
521
550
  continue;
522
551
  }
523
- if (feeToTreasury < nameRegFee(rec.name)) {
552
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
524
553
  note(ev, ev.id, "name", false, "name registration fee unpaid");
525
554
  continue;
526
555
  }
@@ -545,7 +574,7 @@ function resolve(events, tipHeight) {
545
574
  }
546
575
  }
547
576
  names.set(rec.name, cand);
548
- feesPaid += nameRegFee(rec.name);
577
+ feesPaid += nameRegFee(rec.name, ev.height);
549
578
  note(ev, ev.id, "name", true, cur ? "displaced prior holder" : void 0);
550
579
  } else if (rec.t === "nxfer") {
551
580
  const n = names.get(rec.name);
@@ -582,7 +611,7 @@ function resolve(events, tipHeight) {
582
611
  note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
583
612
  continue;
584
613
  }
585
- if (ev.height >= V13_HEIGHT && !wantIsToken && rec.taker === void 0) {
614
+ if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
586
615
  note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
587
616
  continue;
588
617
  }
@@ -649,7 +678,7 @@ function resolve(events, tipHeight) {
649
678
  status: "open",
650
679
  expiresEpoch: ev.expiresEpoch,
651
680
  height: ev.height,
652
- feeBps: v11 ? FEE_BPS : 0
681
+ feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
653
682
  };
654
683
  offers.set(ev.id, o);
655
684
  const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
@@ -723,12 +752,12 @@ function resolve(events, tipHeight) {
723
752
  note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
724
753
  continue;
725
754
  }
726
- if (feeToTreasury < nameRegFee(rec.name)) {
755
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
727
756
  note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
728
757
  continue;
729
758
  }
730
759
  n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
731
- feesPaid += nameRegFee(rec.name);
760
+ feesPaid += nameRegFee(rec.name, ev.height);
732
761
  note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
733
762
  } else if (rec.t === "tmeta") {
734
763
  if (!v15) {
@@ -806,7 +835,7 @@ function resolve(events, tipHeight) {
806
835
  continue;
807
836
  }
808
837
  const amt = BigInt(o.want.amount);
809
- const fee = o.feeBps ? tradeFee(amt) : 0n;
838
+ const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
810
839
  const buyer = bal(o.want.ticker, who);
811
840
  if (buyer.available < amt + fee) {
812
841
  note(ev, ev.txid, "fill", false, "insufficient want-token balance");
@@ -855,8 +884,14 @@ function resolve(events, tipHeight) {
855
884
  continue;
856
885
  }
857
886
  if (ev.height >= V13_HEIGHT && !o.taker) {
858
- note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
859
- continue;
887
+ if (ev.height < V17_HEIGHT) {
888
+ note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
889
+ continue;
890
+ }
891
+ if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
892
+ note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
893
+ continue;
894
+ }
860
895
  }
861
896
  const pt = ev.paidTo ?? {};
862
897
  const want = BigInt(o.want.value);
@@ -870,7 +905,7 @@ function resolve(events, tipHeight) {
870
905
  continue;
871
906
  }
872
907
  const x = X < remaining ? X : remaining;
873
- const fee = o.feeBps ? tradeFee(x) : 0n;
908
+ const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
874
909
  if (BigInt(pt[TREASURY_ADDR] ?? "0") < fee) {
875
910
  note(ev, ev.txid, "fill", false, "protocol fee unpaid");
876
911
  continue;
@@ -914,22 +949,35 @@ function resolve(events, tipHeight) {
914
949
  continue;
915
950
  }
916
951
  if (ev.height >= V13_HEIGHT && !o.taker) {
917
- note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
918
- continue;
952
+ if (ev.height < V17_HEIGHT) {
953
+ note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
954
+ continue;
955
+ }
956
+ if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
957
+ note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
958
+ continue;
959
+ }
919
960
  }
920
961
  const pt = ev.paidTo ?? {};
921
962
  const want = BigInt(o.want.value);
922
- const paid = BigInt(pt[o.want.payto] ?? "0");
923
- if (paid < want) {
924
- note(ev, ev.txid, "fill", false, "payment below want.value");
925
- continue;
926
- }
927
- const fee = o.feeBps ? tradeFee(want) : 0n;
928
- const feeGot = BigInt(pt[TREASURY_ADDR] ?? "0");
929
- if (feeGot < fee) {
930
- note(ev, ev.txid, "fill", false, "protocol fee unpaid");
963
+ const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
964
+ const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
965
+ const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
966
+ const need = /* @__PURE__ */ new Map();
967
+ const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
968
+ addNeed(o.want.payto, want);
969
+ addNeed(TREASURY_ADDR, fee);
970
+ if (rebate > 0n) addNeed(o.seller, rebate);
971
+ let unpaid;
972
+ for (const [addr, amt] of need) if (BigInt(pt[addr] ?? "0") < amt) {
973
+ unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
974
+ break;
975
+ }
976
+ if (unpaid) {
977
+ note(ev, ev.txid, "fill", false, unpaid);
931
978
  continue;
932
979
  }
980
+ const paid = BigInt(pt[o.want.payto] ?? "0");
933
981
  if (isNameGive(o.give)) {
934
982
  const n = names.get(o.give.name);
935
983
  if (!n) {
@@ -961,6 +1009,36 @@ function resolve(events, tipHeight) {
961
1009
  o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
962
1010
  markBidDone(o, who);
963
1011
  note(ev, ev.txid, "fill", true);
1012
+ } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
1013
+ if (o.status !== "open") {
1014
+ note(ev, ev.txid, "claim", false, `offer ${o.status}`);
1015
+ continue;
1016
+ }
1017
+ if (o.taker) {
1018
+ note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
1019
+ continue;
1020
+ }
1021
+ if (isTokenWant(o.want)) {
1022
+ note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
1023
+ continue;
1024
+ }
1025
+ if (liveClaim(o, ev.height)) {
1026
+ note(ev, ev.txid, "claim", false, "offer already claimed (window live)");
1027
+ continue;
1028
+ }
1029
+ if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + CLAIM_COOLDOWN_BLOCKS) {
1030
+ note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
1031
+ continue;
1032
+ }
1033
+ let liveN = 0;
1034
+ for (const x of offers.values()) if (x.claimedBy === who && x.claimUntilHeight !== void 0 && ev.height < x.claimUntilHeight) liveN++;
1035
+ if (liveN >= MAX_ACTIVE_CLAIMS) {
1036
+ note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1037
+ continue;
1038
+ }
1039
+ o.claimedBy = who;
1040
+ o.claimUntilHeight = ev.height + CLAIM_WINDOW_BLOCKS;
1041
+ note(ev, ev.txid, "claim", true);
964
1042
  }
965
1043
  }
966
1044
  }
@@ -1016,22 +1094,31 @@ function canonicalState(s) {
1016
1094
  ACTIVATION_HEIGHT,
1017
1095
  ADDR_RE,
1018
1096
  AMOUNT_RE,
1097
+ CLAIM_COOLDOWN_BLOCKS,
1098
+ CLAIM_WINDOW_BLOCKS,
1019
1099
  COMMIT_MAX_BLOCKS,
1020
1100
  CONF_TOKEN_FILL,
1021
1101
  DEPLOY_FEE,
1022
1102
  DOMAIN,
1023
1103
  EPOCH_LEN,
1024
1104
  FEE_BPS,
1105
+ FEE_BPS_V16,
1025
1106
  HASH_RE,
1107
+ MAX_ACTIVE_CLAIMS,
1026
1108
  MAX_AMOUNT,
1027
1109
  MAX_RECORD_BYTES,
1110
+ NAME_FEE_SHORT_V18,
1111
+ NAME_FEE_V18,
1028
1112
  NAME_GRACE_EPOCHS,
1029
1113
  NAME_PREMIUM_DECAY_EPOCHS,
1030
1114
  NAME_PREMIUM_START,
1031
1115
  NAME_RE,
1032
1116
  NAME_TERM_EPOCHS,
1117
+ REBATE_BPS,
1118
+ REBATE_FLAT,
1033
1119
  RESERVED_NAMES,
1034
1120
  SCORE_CANCEL,
1121
+ SCORE_CLAIM,
1035
1122
  SCORE_FILL,
1036
1123
  TICKER_RE,
1037
1124
  TREASURY_ADDR,
@@ -1040,6 +1127,9 @@ function canonicalState(s) {
1040
1127
  V13_HEIGHT,
1041
1128
  V14_HEIGHT,
1042
1129
  V15_HEIGHT,
1130
+ V16_HEIGHT,
1131
+ V17_HEIGHT,
1132
+ V18_HEIGHT,
1043
1133
  bid,
1044
1134
  buildRecord,
1045
1135
  canonicalState,
@@ -1049,6 +1139,7 @@ function canonicalState(s) {
1049
1139
  isName,
1050
1140
  isNameGive,
1051
1141
  isTokenWant,
1142
+ makerRebate,
1052
1143
  mint,
1053
1144
  nameClaim,
1054
1145
  nameCommit,
package/dist/index.d.cts CHANGED
@@ -5,6 +5,8 @@ declare const V12_HEIGHT = 30300;
5
5
  declare const V13_HEIGHT = 31100;
6
6
  declare const V14_HEIGHT = 31400;
7
7
  declare const V15_HEIGHT = 32000;
8
+ declare const V16_HEIGHT = 33600;
9
+ declare const V17_HEIGHT = 34000;
8
10
  declare const EPOCH_LEN = 30;
9
11
  declare const NAME_TERM_EPOCHS = 8760;
10
12
  declare const NAME_GRACE_EPOCHS = 720;
@@ -12,17 +14,28 @@ declare const NAME_PREMIUM_START = 20n;
12
14
  declare const NAME_PREMIUM_DECAY_EPOCHS = 720;
13
15
  /** Fee to claim a name whose lease lapsed (grace over): nameRegFee × a linearly-decaying
14
16
  * premium. Pure in (name, epochsPastGraceEnd) — deterministic for every resolver. */
15
- declare function expiredClaimFee(name: string, epochsPastGraceEnd: number): bigint;
17
+ declare function expiredClaimFee(name: string, epochsPastGraceEnd: number, height: number): bigint;
16
18
  declare const MAX_RECORD_BYTES = 512;
17
19
  declare const MAX_AMOUNT: bigint;
18
20
  declare const SCORE_FILL = 100;
19
21
  declare const SCORE_CANCEL = 0;
22
+ declare const SCORE_CLAIM = 50;
23
+ declare const CLAIM_WINDOW_BLOCKS = 15;
24
+ declare const MAX_ACTIVE_CLAIMS = 3;
25
+ declare const CLAIM_COOLDOWN_BLOCKS = 15;
20
26
  declare const CONF_TOKEN_FILL = 1000000;
21
27
  declare const TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
22
28
  declare const FEE_BPS = 100;
29
+ declare const FEE_BPS_V16 = 150;
30
+ declare const REBATE_FLAT = 25000000n;
31
+ declare const REBATE_BPS = 50;
23
32
  declare const DEPLOY_FEE = 100000000;
24
- declare function nameRegFee(name: string): bigint;
25
- declare const tradeFee: (want: bigint) => bigint;
33
+ declare const V18_HEIGHT = 40000;
34
+ declare const NAME_FEE_SHORT_V18 = 670000000n;
35
+ declare const NAME_FEE_V18 = 300000000n;
36
+ declare function nameRegFee(name: string, height: number): bigint;
37
+ declare const tradeFee: (want: bigint, bps?: number) => bigint;
38
+ declare const makerRebate: (value: bigint) => bigint;
26
39
  declare const TICKER_RE: RegExp;
27
40
  declare const ADDR_RE: RegExp;
28
41
  declare const AMOUNT_RE: RegExp;
@@ -230,6 +243,10 @@ interface OfferState {
230
243
  fills?: FillEntry[];
231
244
  /** v1.2: the bid this offer responds to */
232
245
  bid?: string;
246
+ /** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse —
247
+ * a past claimUntilHeight just means no live claim (the fields persist as the last-claim record). */
248
+ claimedBy?: string;
249
+ claimUntilHeight?: number;
233
250
  fill?: FillEntry;
234
251
  }
235
252
  type BidStatus = "open" | "cancelled" | "expired" | "done";
@@ -322,4 +339,4 @@ declare function resolve(events: ChainEvent[], tipHeight: number): CairnXState;
322
339
  * history.) Keys sort in ordinal (code-unit) order at every depth. */
323
340
  declare function canonicalState(s: CairnXState): string;
324
341
 
325
- export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, COMMIT_MAX_BLOCKS, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DOMAIN, type DeployRecord, EPOCH_LEN, FEE_BPS, type FillEntry, type Give, HASH_RE, MAX_AMOUNT, MAX_RECORD_BYTES, type MintRecord, NAME_GRACE_EPOCHS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, type NameCommitRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, type ProposeEvent, RESERVED_NAMES, SCORE_CANCEL, SCORE_FILL, TICKER_RE, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, type Want, bid, buildRecord, canonicalState, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, mint, nameClaim, nameCommit, nameCommitRecord, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, parseAmount, parseRecord, resolve, tokenMeta, tradeFee, transfer };
342
+ export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_COOLDOWN_BLOCKS, CLAIM_WINDOW_BLOCKS, COMMIT_MAX_BLOCKS, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DOMAIN, type DeployRecord, EPOCH_LEN, FEE_BPS, FEE_BPS_V16, type FillEntry, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_RECORD_BYTES, type MintRecord, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, type NameCommitRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, type ProposeEvent, REBATE_BPS, REBATE_FLAT, RESERVED_NAMES, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, type Want, bid, buildRecord, canonicalState, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, parseAmount, parseRecord, resolve, tokenMeta, tradeFee, transfer };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,8 @@ declare const V12_HEIGHT = 30300;
5
5
  declare const V13_HEIGHT = 31100;
6
6
  declare const V14_HEIGHT = 31400;
7
7
  declare const V15_HEIGHT = 32000;
8
+ declare const V16_HEIGHT = 33600;
9
+ declare const V17_HEIGHT = 34000;
8
10
  declare const EPOCH_LEN = 30;
9
11
  declare const NAME_TERM_EPOCHS = 8760;
10
12
  declare const NAME_GRACE_EPOCHS = 720;
@@ -12,17 +14,28 @@ declare const NAME_PREMIUM_START = 20n;
12
14
  declare const NAME_PREMIUM_DECAY_EPOCHS = 720;
13
15
  /** Fee to claim a name whose lease lapsed (grace over): nameRegFee × a linearly-decaying
14
16
  * premium. Pure in (name, epochsPastGraceEnd) — deterministic for every resolver. */
15
- declare function expiredClaimFee(name: string, epochsPastGraceEnd: number): bigint;
17
+ declare function expiredClaimFee(name: string, epochsPastGraceEnd: number, height: number): bigint;
16
18
  declare const MAX_RECORD_BYTES = 512;
17
19
  declare const MAX_AMOUNT: bigint;
18
20
  declare const SCORE_FILL = 100;
19
21
  declare const SCORE_CANCEL = 0;
22
+ declare const SCORE_CLAIM = 50;
23
+ declare const CLAIM_WINDOW_BLOCKS = 15;
24
+ declare const MAX_ACTIVE_CLAIMS = 3;
25
+ declare const CLAIM_COOLDOWN_BLOCKS = 15;
20
26
  declare const CONF_TOKEN_FILL = 1000000;
21
27
  declare const TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
22
28
  declare const FEE_BPS = 100;
29
+ declare const FEE_BPS_V16 = 150;
30
+ declare const REBATE_FLAT = 25000000n;
31
+ declare const REBATE_BPS = 50;
23
32
  declare const DEPLOY_FEE = 100000000;
24
- declare function nameRegFee(name: string): bigint;
25
- declare const tradeFee: (want: bigint) => bigint;
33
+ declare const V18_HEIGHT = 40000;
34
+ declare const NAME_FEE_SHORT_V18 = 670000000n;
35
+ declare const NAME_FEE_V18 = 300000000n;
36
+ declare function nameRegFee(name: string, height: number): bigint;
37
+ declare const tradeFee: (want: bigint, bps?: number) => bigint;
38
+ declare const makerRebate: (value: bigint) => bigint;
26
39
  declare const TICKER_RE: RegExp;
27
40
  declare const ADDR_RE: RegExp;
28
41
  declare const AMOUNT_RE: RegExp;
@@ -230,6 +243,10 @@ interface OfferState {
230
243
  fills?: FillEntry[];
231
244
  /** v1.2: the bid this offer responds to */
232
245
  bid?: string;
246
+ /** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse —
247
+ * a past claimUntilHeight just means no live claim (the fields persist as the last-claim record). */
248
+ claimedBy?: string;
249
+ claimUntilHeight?: number;
233
250
  fill?: FillEntry;
234
251
  }
235
252
  type BidStatus = "open" | "cancelled" | "expired" | "done";
@@ -322,4 +339,4 @@ declare function resolve(events: ChainEvent[], tipHeight: number): CairnXState;
322
339
  * history.) Keys sort in ordinal (code-unit) order at every depth. */
323
340
  declare function canonicalState(s: CairnXState): string;
324
341
 
325
- export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, COMMIT_MAX_BLOCKS, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DOMAIN, type DeployRecord, EPOCH_LEN, FEE_BPS, type FillEntry, type Give, HASH_RE, MAX_AMOUNT, MAX_RECORD_BYTES, type MintRecord, NAME_GRACE_EPOCHS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, type NameCommitRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, type ProposeEvent, RESERVED_NAMES, SCORE_CANCEL, SCORE_FILL, TICKER_RE, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, type Want, bid, buildRecord, canonicalState, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, mint, nameClaim, nameCommit, nameCommitRecord, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, parseAmount, parseRecord, resolve, tokenMeta, tradeFee, transfer };
342
+ export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_COOLDOWN_BLOCKS, CLAIM_WINDOW_BLOCKS, COMMIT_MAX_BLOCKS, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DOMAIN, type DeployRecord, EPOCH_LEN, FEE_BPS, FEE_BPS_V16, type FillEntry, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_RECORD_BYTES, type MintRecord, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, type NameCommitRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, type ProposeEvent, REBATE_BPS, REBATE_FLAT, RESERVED_NAMES, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, type Want, bid, buildRecord, canonicalState, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, parseAmount, parseRecord, resolve, tokenMeta, tradeFee, transfer };