@inversealtruism/cairnx-core 0.1.32 → 0.1.34

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/CONVENTION.md CHANGED
@@ -1,11 +1,15 @@
1
1
  # CairnX Convention v1
2
2
 
3
- **Status:** v2.1 (normative; supersedes the 2026-06-10 v1.0 draft) · **Domain:** `cairnx:v1` · **Base activation:** 29 860
3
+ **Status:** v2.7 (normative; supersedes the 2026-06-10 v1.0 draft) · **Domain:** `cairnx:v1` · **Base activation:** 29 860
4
4
 
5
5
  **Version ladder** (each gate non-retroactive — below it, behavior + canonical hashes are byte-identical to history):
6
6
  v1.1 @ 29 960 · v1.2 @ 30 300 · v1.3 @ 31 100 · v1.4 @ 31 400 · v1.5 @ 32 000 · v1.6 @ 33 600 (§19) ·
7
- v1.7 @ 34 000 (§20) · v1.9 @ 36 700 (§22) · v2.0 @ 38 400 (§23) · v1.8 @ 40 000 (§21) · v2.1 @ 40 100 (§24).
8
- Sections §19–§24 are the normative semantics for v1.6–v2.1; §5.1 is the byte-level canonical-JSON contract that binds all of them.
7
+ v1.7 @ 34 000 (§20) · v1.9 @ 36 700 (§22) · v2.0 @ 38 400 (§23) · v1.8 @ 40 000 (§21) · v2.1 @ 40 100 (§24) ·
8
+ v2.2 @ 41 300 (§25) · v2.4 @ 49 200 (§27) · v2.5 @ 51 000 (§28) · v2.6 @ 51 200 (§29) · v2.3 @ 52 000 (§26) ·
9
+ v2.7 @ 52 500 (§30).
10
+ Sections §19–§30 are the normative semantics for v1.6–v2.7; §5.1 is the byte-level canonical-JSON contract that binds all of them.
11
+ Activation order is by HEIGHT, not by version number (v1.8 activated after v2.0; v2.3 activates after v2.6): a
12
+ replayer gates each rule on its own height constant, never on "version X implies version Y is active".
9
13
 
10
14
  > **Canonical copy.** This spec ships in the published `@inversealtruism/cairnx-core` package; the source of
11
15
  > truth is `csd-sdk/packages/cairnx/CONVENTION.md`. Any in-repo mirror (e.g. the `cairnx` app repo) MUST be
@@ -815,3 +819,172 @@ epochs from its anchor:
815
819
  everyone. Below V21, `effExpiry = expires_epoch` — byte-identical history.
816
820
  - **Client expiry-height closed form** (mirrors the resolver's height-gated sweep, for buy-safety guards):
817
821
  `offerExpiryHeight = min( (expires_epoch+1)·30 , max( V21_HEIGHT , (epochOf(anchor)+168+1)·30 ) )`.
822
+
823
+ ---
824
+
825
+ # CairnX Convention v2.2 (offer/bid duration cap removed, anchor-keyed)
826
+
827
+ **Activation height: 41 300** (`V22_HEIGHT`; non-retroactive; keyed on the offer/bid ANCHOR height, not the sweep height).
828
+
829
+ ## 25. The §24 cap applies only to the [V21, V22) anchor era
830
+
831
+ The §24 lifetime cap proved to be an infrastructure symptom, not a market need, and was lifted:
832
+
833
+ - **Anchor ≥ 41 300:** the raw `expires_epoch` binds. No creation gate, no capped `effExpiry`. The 1-week
834
+ suggestion survives only as CLIENT policy (the reference UI defaults to it); it is not consensus.
835
+ - **Anchor in [40 100, 41 300):** the §24 rules apply to that offer/bid FOREVER (creation gate at its
836
+ anchor, capped `effExpiry` in every later sweep). Era membership is fixed by the anchor, so every
837
+ replayer ages the same record identically regardless of when it replays.
838
+ - **Anchor < 40 100:** raw `expires_epoch` binds (pre-V21 history, unchanged).
839
+ - **Client closed form:** for anchor ≥ V22, `offerExpiryHeight = (expires_epoch+1)·30`; for the [V21, V22)
840
+ era use the §24 formula; below V21 use the raw form.
841
+ - **Adoption note:** a relaxation. A v2.1-only replayer rejects (no-ops) a long-dated offer the chain
842
+ accepts, so its book diverges from the canonical one from the first over-cap anchor onward. Every
843
+ replayer must run the v2.2 core before the tip crosses 41 300 (already long past; stated for the record).
844
+
845
+ ---
846
+
847
+ # CairnX Convention v2.3 (`nset` to the zero address clears the record)
848
+
849
+ **Activation height: 52 000** (`V23_HEIGHT`; non-retroactive; keyed on the `nset` event height).
850
+
851
+ ## 26. Clearing a name's payment target
852
+
853
+ `ZERO_ADDR = 0x` + 40 hex zeros.
854
+
855
+ - **Event height ≥ 52 000:** an owner `nset` whose `addr` equals `ZERO_ADDR` CLEARS the record: the
856
+ canonical state materializes the name with NO `addr` key (absent, not `null`). A cleared name resolves
857
+ to its OWNER address (the §9 fallback) and drops out of the reverse-resolution / primary-name candidate
858
+ set. This is the supported way to switch which of several names is an address's primary.
859
+ - **Event height < 52 000:** the identical record literally sets `addr` to the zero address (historical
860
+ behavior, byte-identical replay). The zero address has always been a VALID `addr`, so there is no
861
+ parser change in either direction.
862
+ - **Guards unchanged:** `nset` on a lapsed lease stays a no-op (§17); owner-only as before.
863
+ - **Adoption note (fund safety, the harder direction):** a stale (pre-v2.3) replayer shows a cleared name
864
+ still pointing at `0x000...0`. A payer trusting that stale view burns funds to the zero address. Wallets
865
+ MUST refuse to pay `0x000...0` regardless (the reference wallet hard-blocks it), and every replayer must
866
+ run the v2.3 core before the tip crosses 52 000.
867
+
868
+ ---
869
+
870
+ # CairnX Convention v2.4 (length-graded registration fee increase)
871
+
872
+ **Activation height: 49 200** (`V24_HEIGHT`; non-retroactive; keyed on the FEE-BEARING event's height).
873
+
874
+ ## 27. The fee curve
875
+
876
+ `nameRegFee(name, height)` selects the regime by the height of the event that carries the fee: the `name`
877
+ reveal below v2.5, the winner's `nfinalize` at/after v2.5 (§28), and every renewal at its own event height.
878
+
879
+ | name length | < 40 000 | ≥ 40 000 (v1.8) | ≥ 49 200 (v2.4) |
880
+ |---|---|---|---|
881
+ | ≤ 3 chars | 5 CSD | 6.7 CSD | 15 CSD |
882
+ | 4 chars | 2 CSD | 6.7 CSD | 10 CSD |
883
+ | 5 chars | 1 CSD | 3 CSD | 5 CSD |
884
+ | 6 - 9 chars | 0.5 CSD | 3 CSD | 5 CSD |
885
+ | ≥ 10 chars | 0.1 CSD | 3 CSD | 3 CSD |
886
+
887
+ - Underpayment is a **no-op** (`name registration fee unpaid`), and the §10 warning applies with force:
888
+ the attached payment outputs still settle on-chain. An underpaid registration or renewal is a burn.
889
+ - **Adoption note (BOTH directions are hard):** a stale replayer ACCEPTS an underpaid post-49 200
890
+ registration the chain rejects (it shows a phantom owner), and a stale wallet ATTACHES the old fee and
891
+ burns it. Every replayer and every fee-quoting client must run the v2.4 core before the tip crosses
892
+ 49 200.
893
+
894
+ ---
895
+
896
+ # CairnX Convention v2.5 (sealed-reservation registration)
897
+
898
+ **Activation height: 51 000** (`V25_HEIGHT`; non-retroactive; keyed on the reveal event height).
899
+
900
+ Root fix for the reveal fee-burn: pre-v2.5, every losing racer in a §9 commit-reveal race paid the FULL
901
+ registration fee for nothing (payment-without-delivery). At/after v2.5 the reveal is payment-free; only
902
+ the settled winner ever pays.
903
+
904
+ ## 28. Commit, payment-free reveal, freeze, winner-only finalize
905
+
906
+ - **Commit (unchanged):** a prior `ncommit` per §9. Back-dating is unchanged: the reveal's effective
907
+ anchor `effHeight` is its commit's height.
908
+ - **Reveal window shrinks:** at/after v2.5 a reveal must land within `REG_COMMIT_MAX_BLOCKS = 8` blocks
909
+ of its commit (below v2.5: 240). One constant serves as BOTH the reveal window and the displacement
910
+ freeze, so no back-dated challenger can appear after the freeze has passed. Direct no-salt registration
911
+ is rejected at/after v2.5 (`v2.5: registration requires a commit-reveal (salt)`): an unsealed
912
+ registration could not be finalized.
913
+ - **Reveal (`name` record, height ≥ 51 000):** carries NO registration fee. Creates a PENDING reservation,
914
+ materialized in canonical state as the name with `pending: true` and
915
+ `finalizeBy = effHeight + 8 + 20` (`REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS`).
916
+ - **Contest while pending:** a rival reveal with a strictly earlier `(effHeight, pos, id)` (numeric on
917
+ `effHeight` and `pos`, ordinal code-unit on `id`; earliest wins) DISPLACES the reservation; the displaced party has lost only its
918
+ ~0.25 CSD anchor. A FINALIZED name can never be displaced: any window-valid challenger has an
919
+ `effHeight` within 8 blocks of the current height, which is strictly later than a finalized name's
920
+ (its freeze has passed). Names owned `viaFill` are likewise never displaced.
921
+ - **Per-address cap:** at most `MAX_PENDING_REG = 3` concurrent un-finalized reservations per owner
922
+ (expired ones and a re-reveal of the same name do not count); excess reveals are no-ops.
923
+ - **Finalize (`nfinalize` record):** `{v:1, t:"nfinalize", name, salt}`. Valid iff ALL of: the name is
924
+ pending and owned by the sender; `salt` re-derives the SAME commit that anchored the reservation
925
+ (`sha256` per §9, binding name + salt + owner, and the commit's height must equal the reservation's
926
+ `effHeight`); the freeze has passed (`ev.height > effHeight + 8`); the reservation has not expired
927
+ (`ev.height ≤ finalizeBy`); and the payment to the treasury covers `nameRegFee(name, ev.height)`
928
+ (§27, priced at the FINALIZE height). On success the `pending`/`finalizeBy` keys become ABSENT, the
929
+ lease starts (`paidThroughEpoch = epochOf(ev.height) + NAME_TERM_EPOCHS`), and the name is NOT
930
+ `viaFill`.
931
+ - **Reservation expiry (lazy):** past `finalizeBy` an un-finalized reservation is swept exactly like a
932
+ lapsed lease (at each event height and at the tip+1 closing sweep): the name record is DELETED and the
933
+ name reopens. The would-be winner has lost only anchors, never the registration fee.
934
+ - **Client timing rule** (`FINALIZE_TIP_MARGIN = 2`, client-side only): sign the finalize at
935
+ `effHeight + 8 + 2` or later so one reorg cannot un-freeze the contest under a fee-bearing tx. The
936
+ 20-block grace exists so a slow inclusion still lands before `finalizeBy`.
937
+ - **Adoption note (hard gate):** a stale (pre-v2.5) wallet crossing 51 000 attaches the registration fee
938
+ to what is now a payment-free reveal; the resolver ignores the fee and the payment burns. Every
939
+ replayer AND every fee-attaching client must run the v2.5 core before the tip crosses 51 000.
940
+
941
+ ---
942
+
943
+ # CairnX Convention v2.6 (sealed-reservation recapture)
944
+
945
+ **Activation height: 51 200** (`V26_HEIGHT`; non-retroactive; keyed on the reveal event height).
946
+
947
+ The §28 cure applied to LAPSED-name recapture (§17), whose losing racer previously forfeited the full
948
+ decaying premium (up to hundreds of CSD).
949
+
950
+ ## 29. Recapture through the same sealed machinery
951
+
952
+ - **Reveal on a lapsed name (height ≥ 51 200):** payment-free. Creates a recapture reservation in an
953
+ INTERNAL `recaptures` map. The reservation is NOT materialized in canonical state, and the lapsed
954
+ record in `names` is left untouched (it is the premium basis; an abandoned reservation can never
955
+ bypass the premium). Below v2.6, recapture stays pay-now at reveal (§17, byte-identical).
956
+ - **Same windows and contest:** reveal window and freeze are `REG_COMMIT_MAX_BLOCKS = 8`;
957
+ `finalizeBy = effHeight + 8 + 20`; lowest `(effHeight, pos, id)` displaces among reservations; its own
958
+ `MAX_PENDING_REG = 3` per-address cap; overdue reservations are lazily swept.
959
+ - **Finalize:** the same `nfinalize` record, salt-bound to the recapture commit. The winner pays the §17
960
+ decaying premium priced at the FINALIZE height (slightly more decayed than at reveal: rounding favors
961
+ the payer). On success the name becomes a normal fresh lease under the new owner, NOT `viaFill`
962
+ (displacement-immune by the same freeze arithmetic as §28).
963
+ - **Replayer note:** because the reservation map is internal, pre-v2.6 canonical hashes are untouched,
964
+ but a third-party resolver MUST still replicate the map (reveal, displacement, expiry) to agree on
965
+ which later `nfinalize` succeeds. The map is defined by this section plus the reference resolver and
966
+ its Python mirror; the v2.6 conformance vectors pin it.
967
+ - **Adoption note (hard gate):** same shape as §28. A stale wallet attaches the premium to a
968
+ payment-free recapture reveal and burns it. All replayers and clients on the v2.6 core before 51 200.
969
+
970
+ ---
971
+
972
+ # CairnX Convention v2.7 (young-name sale embargo shrinks)
973
+
974
+ **Activation height: 52 500** (`V27_HEIGHT`; non-retroactive; keyed on the OFFER event height).
975
+
976
+ ## 30. Sale embargo: 240 blocks becomes 8
977
+
978
+ A name `offer` (§17 sale path) whose event height `H` satisfies `H − effHeight ≤ embargo` is a no-op
979
+ (`name too young to sell (must out-age the reveal window)`), where `effHeight` is the name's effective
980
+ anchor. `viaFill` names were never embargoed.
981
+
982
+ - **`H` < 52 500:** `embargo = COMMIT_MAX_BLOCKS` (240 blocks, ≈ 8 h). The pre-sealed rule: a fresh name
983
+ had to out-age every lurking back-dated reveal before it could be sold.
984
+ - **`H` ≥ 52 500:** `embargo = REG_COMMIT_MAX_BLOCKS` (8 blocks, ≈ 16 min). Under §28/§29 a finalized
985
+ name is displacement-immune the moment its freeze passes, so out-aging the old 240-block window is
986
+ redundant; 8 matches the freeze itself.
987
+ - The gate keys on the OFFER's height, so the same young name can be unsellable at 52 499 and sellable
988
+ at 52 501. Deterministic for every replayer; no name-age era bookkeeping is needed.
989
+ - **Adoption note:** a relaxation (fresh cores accept a sale a stale core no-ops), so every replayer must
990
+ run the v2.7 core before the tip crosses 52 500.
package/dist/index.cjs CHANGED
@@ -110,8 +110,12 @@ __export(index_exports, {
110
110
  deploy: () => deploy,
111
111
  epochOf: () => epochOf,
112
112
  expiredClaimFee: () => expiredClaimFee,
113
+ fillIsSafe: () => fillIsSafe,
114
+ finalizeWinnerCheck: () => finalizeWinnerCheck,
115
+ hasLiveClaim: () => hasLiveClaim,
113
116
  isName: () => isName,
114
117
  isNameGive: () => isNameGive,
118
+ isOpenClaimLane: () => isOpenClaimLane,
115
119
  isTokenWant: () => isTokenWant,
116
120
  makerRebate: () => makerRebate,
117
121
  mint: () => mint,
@@ -131,6 +135,7 @@ __export(index_exports, {
131
135
  parseAmount: () => parseAmount,
132
136
  parseRecord: () => parseRecord,
133
137
  payloadHash: () => import_csd_codec4.payloadHash,
138
+ previewFill: () => previewFill,
134
139
  requiredClaimDepth: () => requiredClaimDepth,
135
140
  resolve: () => resolve,
136
141
  tokenMeta: () => tokenMeta,
@@ -181,7 +186,7 @@ var DEPLOY_FEE = 1e8;
181
186
  var V18_HEIGHT = 4e4;
182
187
  var NAME_FEE_SHORT_V18 = 670000000n;
183
188
  var NAME_FEE_V18 = 300000000n;
184
- var V24_HEIGHT = 49200;
189
+ var V24_HEIGHT = 46400;
185
190
  var NAME_FEE_LEN3_V24 = 1500000000n;
186
191
  var NAME_FEE_LEN4_V24 = 1000000000n;
187
192
  var NAME_FEE_MID_V24 = 500000000n;
@@ -222,13 +227,13 @@ var HASH_RE = /^0x[0-9a-f]{64}$/;
222
227
  var SALT_RE = /^[0-9a-fA-F]{16,128}$/;
223
228
  var RESERVED_NAMES = /* @__PURE__ */ new Set(["csd", "treasury", "admin", "official", "root", "www", "support"]);
224
229
  var COMMIT_MAX_BLOCKS = 8 * import_csd_codec.EPOCH_LEN;
225
- var V25_HEIGHT = 51e3;
230
+ var V25_HEIGHT = 46440;
226
231
  var REG_COMMIT_MAX_BLOCKS = 8;
227
232
  var REG_FINALIZE_GRACE_BLOCKS = 20;
228
233
  var MAX_PENDING_REG = 3;
229
234
  var FINALIZE_TIP_MARGIN = 2;
230
- var V26_HEIGHT = 51200;
231
- var V27_HEIGHT = 52500;
235
+ var V26_HEIGHT = 46480;
236
+ var V27_HEIGHT = 46520;
232
237
  var epochOf = (height) => Math.floor(height / import_csd_codec.EPOCH_LEN);
233
238
  var claimWindowAt = (height) => height >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
234
239
  var claimWindowOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
@@ -1403,6 +1408,70 @@ function requiredClaimDepth(valueSats, height) {
1403
1408
  return { depth: max, reversalPct: Math.pow(r, max) * 100, capped: true };
1404
1409
  }
1405
1410
 
1411
+ // src/preflight.ts
1412
+ function previewFill(offer2, payRaw) {
1413
+ const pay = BigInt(payRaw);
1414
+ const zero = { deliverable: false, got: 0n, pay: 0n, fee: 0n, rebate: 0n };
1415
+ if (offer2.status !== "open") return { ...zero, reason: "not-open" };
1416
+ if (isTokenWant(offer2.want)) return { ...zero, reason: "not-csd-priced" };
1417
+ const want = BigInt(offer2.want.value);
1418
+ const feeBps = offer2.feeBps;
1419
+ if (offer2.min !== void 0 && !isNameGive(offer2.give)) {
1420
+ const paidSoFar = BigInt(offer2.paid ?? "0");
1421
+ const remaining = want - paidSoFar;
1422
+ const minV = BigInt(offer2.min);
1423
+ const effMin = remaining < minV ? remaining : minV;
1424
+ if (pay < effMin) return { ...zero, reason: "below-min" };
1425
+ const x = pay < remaining ? pay : remaining;
1426
+ const fee2 = feeBps ? tradeFee(x, feeBps) : 0n;
1427
+ const giveTotal = BigInt(offer2.give.amount);
1428
+ const newPaid = paidSoFar + x;
1429
+ const deliveredSoFar = BigInt(offer2.delivered ?? "0");
1430
+ const out = giveTotal * newPaid / want - deliveredSoFar;
1431
+ if (out === 0n) return { deliverable: false, reason: "zero-delivery", got: 0n, pay: x, fee: fee2, rebate: 0n };
1432
+ return { deliverable: true, reason: "ok", got: out, pay: x, fee: fee2, rebate: 0n };
1433
+ }
1434
+ if (pay < want) return { deliverable: false, reason: "below-min", got: 0n, pay, fee: 0n, rebate: 0n };
1435
+ const fee = feeBps ? tradeFee(want, feeBps) : 0n;
1436
+ const restingLiquidity = offer2.taker !== void 0 && offer2.bid !== void 0 || offer2.height >= V17_HEIGHT && offer2.taker === void 0;
1437
+ const rebate = offer2.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
1438
+ const got = isNameGive(offer2.give) ? 1n : BigInt(offer2.give.amount);
1439
+ return { deliverable: true, reason: "ok", got, pay: want, fee, rebate };
1440
+ }
1441
+ function isOpenClaimLane(offer2, tip) {
1442
+ return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
1443
+ }
1444
+ function hasLiveClaim(offer2, me, tip) {
1445
+ if (tip < V17_HEIGHT) return false;
1446
+ if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
1447
+ if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
1448
+ const grace = claimGraceOf(offer2.claimUntilHeight);
1449
+ return tip < offer2.claimUntilHeight + grace;
1450
+ }
1451
+ function fillIsSafe(offer2, me, pay, tip) {
1452
+ const preview = previewFill(offer2, pay);
1453
+ if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
1454
+ if (offer2.taker !== void 0 && offer2.taker.toLowerCase() !== me.toLowerCase())
1455
+ return { safe: false, reason: "taker-bound offer \u2014 not bound to you", preview };
1456
+ if (isOpenClaimLane(offer2, tip) && !hasLiveClaim(offer2, me, tip))
1457
+ return { safe: false, reason: "open CSD offer \u2014 claim it first and fill while your claim is live", preview };
1458
+ if (isTokenWant(offer2.want)) return { safe: true, reason: "token-priced \u2014 deliverability is the buyer's balance", preview };
1459
+ if (!preview.deliverable) {
1460
+ if (preview.reason === "below-min") return { safe: false, reason: "payment is below the offer minimum", preview };
1461
+ return { safe: false, reason: "this payment would deliver 0 tokens \u2014 refusing (the CSD would be lost)", preview };
1462
+ }
1463
+ return { safe: true, reason: "ok", preview };
1464
+ }
1465
+ function finalizeWinnerCheck(nameState, me, commitHeight) {
1466
+ const m = me.toLowerCase();
1467
+ if (!nameState) return { safe: false, reason: "no reservation on-chain (displaced, swept, or never accepted) \u2014 a finalize now would burn the fee" };
1468
+ if (nameState.owner?.toLowerCase() !== m) return { safe: false, reason: "an earlier committer won this name \u2014 you were outbid" };
1469
+ if (nameState.pending !== true) return { safe: false, reason: "already registered to you \u2014 no second finalize fee is needed" };
1470
+ if (Number(nameState.effectiveHeight) !== Number(commitHeight))
1471
+ return { safe: false, reason: "your reservation was displaced (effective height changed) \u2014 a finalize now would burn the fee" };
1472
+ return { safe: true, reason: "ok" };
1473
+ }
1474
+
1406
1475
  // src/paidto.ts
1407
1476
  function paidToFromOutputs(outputs) {
1408
1477
  const m = /* @__PURE__ */ Object.create(null);
@@ -1507,8 +1576,12 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1507
1576
  deploy,
1508
1577
  epochOf,
1509
1578
  expiredClaimFee,
1579
+ fillIsSafe,
1580
+ finalizeWinnerCheck,
1581
+ hasLiveClaim,
1510
1582
  isName,
1511
1583
  isNameGive,
1584
+ isOpenClaimLane,
1512
1585
  isTokenWant,
1513
1586
  makerRebate,
1514
1587
  mint,
@@ -1528,6 +1601,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1528
1601
  parseAmount,
1529
1602
  parseRecord,
1530
1603
  payloadHash,
1604
+ previewFill,
1531
1605
  requiredClaimDepth,
1532
1606
  resolve,
1533
1607
  tokenMeta,
package/dist/index.d.cts CHANGED
@@ -37,7 +37,7 @@ declare const DEPLOY_FEE = 100000000;
37
37
  declare const V18_HEIGHT = 40000;
38
38
  declare const NAME_FEE_SHORT_V18 = 670000000n;
39
39
  declare const NAME_FEE_V18 = 300000000n;
40
- declare const V24_HEIGHT = 49200;
40
+ declare const V24_HEIGHT = 46400;
41
41
  declare const NAME_FEE_LEN3_V24 = 1500000000n;
42
42
  declare const NAME_FEE_LEN4_V24 = 1000000000n;
43
43
  declare const NAME_FEE_MID_V24 = 500000000n;
@@ -63,13 +63,13 @@ declare const HASH_RE: RegExp;
63
63
  declare const SALT_RE: RegExp;
64
64
  declare const RESERVED_NAMES: Set<string>;
65
65
  declare const COMMIT_MAX_BLOCKS: number;
66
- declare const V25_HEIGHT = 51000;
66
+ declare const V25_HEIGHT = 46440;
67
67
  declare const REG_COMMIT_MAX_BLOCKS = 8;
68
68
  declare const REG_FINALIZE_GRACE_BLOCKS = 20;
69
69
  declare const MAX_PENDING_REG = 3;
70
70
  declare const FINALIZE_TIP_MARGIN = 2;
71
- declare const V26_HEIGHT = 51200;
72
- declare const V27_HEIGHT = 52500;
71
+ declare const V26_HEIGHT = 46480;
72
+ declare const V27_HEIGHT = 46520;
73
73
  declare const epochOf: (height: number) => number;
74
74
  declare const claimWindowAt: (height: number) => number;
75
75
  declare const claimWindowOf: (claimUntilHeight: number) => number;
@@ -430,9 +430,56 @@ declare function requiredClaimDepth(valueSats: number | bigint, height: number):
430
430
  capped: boolean;
431
431
  };
432
432
 
433
+ /** What a payment of `pay` (base units of CSD, the seller-paid amount to `want.payto`) would DELIVER
434
+ * against a CSD-priced offer, computed with the resolver's exact expressions. Returns the delivered
435
+ * token `got`, the treasury `fee`, and the maker `rebate` — plus a `deliverable` flag that is false
436
+ * exactly when the resolver would reject the fill AFTER the CSD moved (the C3 zero-delivery trap).
437
+ *
438
+ * Only meaningful for CSD-priced token offers (a `want.value`). For a name give, `got` is 1 (a name is
439
+ * indivisible) when `pay >= want.value`. Token-for-token offers (`want.ticker`) return deliverable:false
440
+ * with reason "not-csd-priced" — their deliverability depends on the buyer's balance, out of scope here. */
441
+ interface FillPreview {
442
+ deliverable: boolean;
443
+ reason?: "not-open" | "not-csd-priced" | "below-min" | "zero-delivery" | "ok";
444
+ got: bigint;
445
+ pay: bigint;
446
+ fee: bigint;
447
+ rebate: bigint;
448
+ }
449
+ /** Preview a fill of `offer` paying `payRaw` CSD base units to the seller. Pure; no I/O. */
450
+ declare function previewFill(offer: OfferState, payRaw: bigint | string | number): FillPreview;
451
+ /** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
452
+ * Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
453
+ declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
454
+ /** Does `me` currently hold a LIVE claim on `offer` at `tip`? Mirrors resolve.ts claimHeld + the
455
+ * who===claimedBy check (:160-169). For a pre-V17 open offer, open fills are banned entirely. */
456
+ declare function hasLiveClaim(offer: OfferState, me: string, tip: number): boolean;
457
+ /** The union verdict a client must clear BEFORE signing a fill of `offer` paying `pay`, as `me`, at `tip`.
458
+ * Closes C2/C3/C4: status open, deliverability >= 1 token, live-claim holdership for the open-CSD lane,
459
+ * and taker match. `safe:true` ⇒ the fill is guaranteed to be accepted by the deterministic gates a
460
+ * payer can verify at signing time (the irreducible timing residual — CLAIM_FILL_GRACE_BLOCKS late mine
461
+ * — is documented, not closable by a preflight). Returns the preview so the caller can size outputs. */
462
+ interface FillSafety {
463
+ safe: boolean;
464
+ reason: string;
465
+ preview: FillPreview;
466
+ }
467
+ declare function fillIsSafe(offer: OfferState, me: string, pay: bigint | string | number, tip: number): FillSafety;
468
+ /** C1: may `me` safely sign the fee-bearing registration `nfinalize` for `nameState` committed at
469
+ * `commitHeight`? The reg fee rides nfinalize; a finalize on a reservation that was displaced or has
470
+ * expired is REJECTED after the fee moved (a burn). Sound by the freeze arithmetic: once a client waits
471
+ * for `tip > effectiveHeight + REG_COMMIT_MAX_BLOCKS` (which the UI's finalizeReady already enforces) no
472
+ * new displacer can appear, so a re-fetch showing "still my live pending reservation at my commit height"
473
+ * guarantees the finalize lands. Pass the AUTHORITATIVE (freshly re-fetched) name record — never a cached
474
+ * one — as `nameState` (undefined = the resolver returns 404 / unregistered, which is a definitive loss). */
475
+ declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me: string, commitHeight: number): {
476
+ safe: boolean;
477
+ reason: string;
478
+ };
479
+
433
480
  declare function paidToFromOutputs(outputs: readonly {
434
481
  addr: string;
435
482
  value: number;
436
483
  }[]): Record<string, string>;
437
484
 
438
- export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, BID_KEYS, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_ATTACKER_Q, CLAIM_COOLDOWN_BLOCKS, CLAIM_FILL_GRACE_BLOCKS, CLAIM_MIN_DEPTH, CLAIM_WINDOW_BLOCKS, CLAIM_WINDOW_BLOCKS_V20, COMMIT_MAX_BLOCKS, COMMIT_REVEAL_MIN_DEPTH, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DEPLOY_KEYS, DOMAIN, type DeployRecord, FEE_BPS, FEE_BPS_V16, FINALIZE_TIP_MARGIN, type FillEntry, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_OFFER_EPOCHS, MAX_PENDING_REG, MAX_RECORD_BYTES, MINT_KEYS, type MintRecord, NAME_FEE_LEN3_V24, NAME_FEE_LEN4_V24, NAME_FEE_LONG_V24, NAME_FEE_MID_V24, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_KEYS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, NFINALIZE_KEYS, NPROFILE_KEYS, type NameCommitRecord, type NameFinalizeRecord, type NameProfileRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, OFFER_KEYS, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, PKEY, PROFILE_MAX_KEYS, PROFILE_MAX_VALUE_BYTES, type ProposeEvent, REBATE_BPS, REBATE_FLAT, REG_COMMIT_MAX_BLOCKS, REG_FINALIZE_GRACE_BLOCKS, RESERVED_NAMES, SALT_RE, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TRANSFER_KEYS, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, V19_HEIGHT, V20_HEIGHT, V21_HEIGHT, V22_HEIGHT, V23_HEIGHT, V24_HEIGHT, V25_HEIGHT, V26_HEIGHT, V27_HEIGHT, type Want, ZERO_ADDR, bid, buildRecord, canonicalState, claimGraceOf, claimWindowAt, claimWindowOf, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameFinalize, nameProfile, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, offerExpiryHeightOf, paidToFromOutputs, parseAmount, parseRecord, requiredClaimDepth, resolve, tokenMeta, tradeFee, transfer };
485
+ export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, BID_KEYS, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_ATTACKER_Q, CLAIM_COOLDOWN_BLOCKS, CLAIM_FILL_GRACE_BLOCKS, CLAIM_MIN_DEPTH, CLAIM_WINDOW_BLOCKS, CLAIM_WINDOW_BLOCKS_V20, COMMIT_MAX_BLOCKS, COMMIT_REVEAL_MIN_DEPTH, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DEPLOY_KEYS, DOMAIN, type DeployRecord, FEE_BPS, FEE_BPS_V16, FINALIZE_TIP_MARGIN, type FillEntry, type FillPreview, type FillSafety, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_OFFER_EPOCHS, MAX_PENDING_REG, MAX_RECORD_BYTES, MINT_KEYS, type MintRecord, NAME_FEE_LEN3_V24, NAME_FEE_LEN4_V24, NAME_FEE_LONG_V24, NAME_FEE_MID_V24, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_KEYS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, NFINALIZE_KEYS, NPROFILE_KEYS, type NameCommitRecord, type NameFinalizeRecord, type NameProfileRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, OFFER_KEYS, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, PKEY, PROFILE_MAX_KEYS, PROFILE_MAX_VALUE_BYTES, type ProposeEvent, REBATE_BPS, REBATE_FLAT, REG_COMMIT_MAX_BLOCKS, REG_FINALIZE_GRACE_BLOCKS, RESERVED_NAMES, SALT_RE, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TRANSFER_KEYS, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, V19_HEIGHT, V20_HEIGHT, V21_HEIGHT, V22_HEIGHT, V23_HEIGHT, V24_HEIGHT, V25_HEIGHT, V26_HEIGHT, V27_HEIGHT, type Want, ZERO_ADDR, bid, buildRecord, canonicalState, claimGraceOf, claimWindowAt, claimWindowOf, deploy, epochOf, expiredClaimFee, fillIsSafe, finalizeWinnerCheck, hasLiveClaim, isName, isNameGive, isOpenClaimLane, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameFinalize, nameProfile, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, offerExpiryHeightOf, paidToFromOutputs, parseAmount, parseRecord, previewFill, requiredClaimDepth, resolve, tokenMeta, tradeFee, transfer };
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ declare const DEPLOY_FEE = 100000000;
37
37
  declare const V18_HEIGHT = 40000;
38
38
  declare const NAME_FEE_SHORT_V18 = 670000000n;
39
39
  declare const NAME_FEE_V18 = 300000000n;
40
- declare const V24_HEIGHT = 49200;
40
+ declare const V24_HEIGHT = 46400;
41
41
  declare const NAME_FEE_LEN3_V24 = 1500000000n;
42
42
  declare const NAME_FEE_LEN4_V24 = 1000000000n;
43
43
  declare const NAME_FEE_MID_V24 = 500000000n;
@@ -63,13 +63,13 @@ declare const HASH_RE: RegExp;
63
63
  declare const SALT_RE: RegExp;
64
64
  declare const RESERVED_NAMES: Set<string>;
65
65
  declare const COMMIT_MAX_BLOCKS: number;
66
- declare const V25_HEIGHT = 51000;
66
+ declare const V25_HEIGHT = 46440;
67
67
  declare const REG_COMMIT_MAX_BLOCKS = 8;
68
68
  declare const REG_FINALIZE_GRACE_BLOCKS = 20;
69
69
  declare const MAX_PENDING_REG = 3;
70
70
  declare const FINALIZE_TIP_MARGIN = 2;
71
- declare const V26_HEIGHT = 51200;
72
- declare const V27_HEIGHT = 52500;
71
+ declare const V26_HEIGHT = 46480;
72
+ declare const V27_HEIGHT = 46520;
73
73
  declare const epochOf: (height: number) => number;
74
74
  declare const claimWindowAt: (height: number) => number;
75
75
  declare const claimWindowOf: (claimUntilHeight: number) => number;
@@ -430,9 +430,56 @@ declare function requiredClaimDepth(valueSats: number | bigint, height: number):
430
430
  capped: boolean;
431
431
  };
432
432
 
433
+ /** What a payment of `pay` (base units of CSD, the seller-paid amount to `want.payto`) would DELIVER
434
+ * against a CSD-priced offer, computed with the resolver's exact expressions. Returns the delivered
435
+ * token `got`, the treasury `fee`, and the maker `rebate` — plus a `deliverable` flag that is false
436
+ * exactly when the resolver would reject the fill AFTER the CSD moved (the C3 zero-delivery trap).
437
+ *
438
+ * Only meaningful for CSD-priced token offers (a `want.value`). For a name give, `got` is 1 (a name is
439
+ * indivisible) when `pay >= want.value`. Token-for-token offers (`want.ticker`) return deliverable:false
440
+ * with reason "not-csd-priced" — their deliverability depends on the buyer's balance, out of scope here. */
441
+ interface FillPreview {
442
+ deliverable: boolean;
443
+ reason?: "not-open" | "not-csd-priced" | "below-min" | "zero-delivery" | "ok";
444
+ got: bigint;
445
+ pay: bigint;
446
+ fee: bigint;
447
+ rebate: bigint;
448
+ }
449
+ /** Preview a fill of `offer` paying `payRaw` CSD base units to the seller. Pure; no I/O. */
450
+ declare function previewFill(offer: OfferState, payRaw: bigint | string | number): FillPreview;
451
+ /** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
452
+ * Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
453
+ declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
454
+ /** Does `me` currently hold a LIVE claim on `offer` at `tip`? Mirrors resolve.ts claimHeld + the
455
+ * who===claimedBy check (:160-169). For a pre-V17 open offer, open fills are banned entirely. */
456
+ declare function hasLiveClaim(offer: OfferState, me: string, tip: number): boolean;
457
+ /** The union verdict a client must clear BEFORE signing a fill of `offer` paying `pay`, as `me`, at `tip`.
458
+ * Closes C2/C3/C4: status open, deliverability >= 1 token, live-claim holdership for the open-CSD lane,
459
+ * and taker match. `safe:true` ⇒ the fill is guaranteed to be accepted by the deterministic gates a
460
+ * payer can verify at signing time (the irreducible timing residual — CLAIM_FILL_GRACE_BLOCKS late mine
461
+ * — is documented, not closable by a preflight). Returns the preview so the caller can size outputs. */
462
+ interface FillSafety {
463
+ safe: boolean;
464
+ reason: string;
465
+ preview: FillPreview;
466
+ }
467
+ declare function fillIsSafe(offer: OfferState, me: string, pay: bigint | string | number, tip: number): FillSafety;
468
+ /** C1: may `me` safely sign the fee-bearing registration `nfinalize` for `nameState` committed at
469
+ * `commitHeight`? The reg fee rides nfinalize; a finalize on a reservation that was displaced or has
470
+ * expired is REJECTED after the fee moved (a burn). Sound by the freeze arithmetic: once a client waits
471
+ * for `tip > effectiveHeight + REG_COMMIT_MAX_BLOCKS` (which the UI's finalizeReady already enforces) no
472
+ * new displacer can appear, so a re-fetch showing "still my live pending reservation at my commit height"
473
+ * guarantees the finalize lands. Pass the AUTHORITATIVE (freshly re-fetched) name record — never a cached
474
+ * one — as `nameState` (undefined = the resolver returns 404 / unregistered, which is a definitive loss). */
475
+ declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me: string, commitHeight: number): {
476
+ safe: boolean;
477
+ reason: string;
478
+ };
479
+
433
480
  declare function paidToFromOutputs(outputs: readonly {
434
481
  addr: string;
435
482
  value: number;
436
483
  }[]): Record<string, string>;
437
484
 
438
- export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, BID_KEYS, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_ATTACKER_Q, CLAIM_COOLDOWN_BLOCKS, CLAIM_FILL_GRACE_BLOCKS, CLAIM_MIN_DEPTH, CLAIM_WINDOW_BLOCKS, CLAIM_WINDOW_BLOCKS_V20, COMMIT_MAX_BLOCKS, COMMIT_REVEAL_MIN_DEPTH, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DEPLOY_KEYS, DOMAIN, type DeployRecord, FEE_BPS, FEE_BPS_V16, FINALIZE_TIP_MARGIN, type FillEntry, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_OFFER_EPOCHS, MAX_PENDING_REG, MAX_RECORD_BYTES, MINT_KEYS, type MintRecord, NAME_FEE_LEN3_V24, NAME_FEE_LEN4_V24, NAME_FEE_LONG_V24, NAME_FEE_MID_V24, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_KEYS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, NFINALIZE_KEYS, NPROFILE_KEYS, type NameCommitRecord, type NameFinalizeRecord, type NameProfileRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, OFFER_KEYS, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, PKEY, PROFILE_MAX_KEYS, PROFILE_MAX_VALUE_BYTES, type ProposeEvent, REBATE_BPS, REBATE_FLAT, REG_COMMIT_MAX_BLOCKS, REG_FINALIZE_GRACE_BLOCKS, RESERVED_NAMES, SALT_RE, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TRANSFER_KEYS, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, V19_HEIGHT, V20_HEIGHT, V21_HEIGHT, V22_HEIGHT, V23_HEIGHT, V24_HEIGHT, V25_HEIGHT, V26_HEIGHT, V27_HEIGHT, type Want, ZERO_ADDR, bid, buildRecord, canonicalState, claimGraceOf, claimWindowAt, claimWindowOf, deploy, epochOf, expiredClaimFee, isName, isNameGive, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameFinalize, nameProfile, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, offerExpiryHeightOf, paidToFromOutputs, parseAmount, parseRecord, requiredClaimDepth, resolve, tokenMeta, tradeFee, transfer };
485
+ export { ACTIVATION_HEIGHT, ADDR_RE, AMOUNT_RE, type AppliedEvent, type AttestEvent, BID_KEYS, type BalanceState, type BidRecord, type BidState, type BidStatus, type BuiltRecord, CLAIM_ATTACKER_Q, CLAIM_COOLDOWN_BLOCKS, CLAIM_FILL_GRACE_BLOCKS, CLAIM_MIN_DEPTH, CLAIM_WINDOW_BLOCKS, CLAIM_WINDOW_BLOCKS_V20, COMMIT_MAX_BLOCKS, COMMIT_REVEAL_MIN_DEPTH, CONF_TOKEN_FILL, type CairnXRecord, type CairnXState, type ChainEvent, DEPLOY_FEE, DEPLOY_KEYS, DOMAIN, type DeployRecord, FEE_BPS, FEE_BPS_V16, FINALIZE_TIP_MARGIN, type FillEntry, type FillPreview, type FillSafety, type Give, HASH_RE, MAX_ACTIVE_CLAIMS, MAX_AMOUNT, MAX_OFFER_EPOCHS, MAX_PENDING_REG, MAX_RECORD_BYTES, MINT_KEYS, type MintRecord, NAME_FEE_LEN3_V24, NAME_FEE_LEN4_V24, NAME_FEE_LONG_V24, NAME_FEE_MID_V24, NAME_FEE_SHORT_V18, NAME_FEE_V18, NAME_GRACE_EPOCHS, NAME_KEYS, NAME_PREMIUM_DECAY_EPOCHS, NAME_PREMIUM_START, NAME_RE, NAME_TERM_EPOCHS, NFINALIZE_KEYS, NPROFILE_KEYS, type NameCommitRecord, type NameFinalizeRecord, type NameProfileRecord, type NameRecord, type NameRenewRecord, type NameSetRecord, type NameState, type NameXferRecord, OFFER_KEYS, type OfferCancelAllRecord, type OfferRecord, type OfferState, type OfferStatus, PKEY, PROFILE_MAX_KEYS, PROFILE_MAX_VALUE_BYTES, type ProposeEvent, REBATE_BPS, REBATE_FLAT, REG_COMMIT_MAX_BLOCKS, REG_FINALIZE_GRACE_BLOCKS, RESERVED_NAMES, SALT_RE, SCORE_CANCEL, SCORE_CLAIM, SCORE_FILL, TICKER_RE, TRANSFER_KEYS, TREASURY_ADDR, type TokenMetaRecord, type TokenState, type TransferRecord, V11_HEIGHT, V12_HEIGHT, V13_HEIGHT, V14_HEIGHT, V15_HEIGHT, V16_HEIGHT, V17_HEIGHT, V18_HEIGHT, V19_HEIGHT, V20_HEIGHT, V21_HEIGHT, V22_HEIGHT, V23_HEIGHT, V24_HEIGHT, V25_HEIGHT, V26_HEIGHT, V27_HEIGHT, type Want, ZERO_ADDR, bid, buildRecord, canonicalState, claimGraceOf, claimWindowAt, claimWindowOf, deploy, epochOf, expiredClaimFee, fillIsSafe, finalizeWinnerCheck, hasLiveClaim, isName, isNameGive, isOpenClaimLane, isTokenWant, makerRebate, mint, nameClaim, nameCommit, nameCommitRecord, nameFinalize, nameProfile, nameRegFee, nameRenew, nameSet, nameXfer, offer, offerCancelAll, offerExpiryHeightOf, paidToFromOutputs, parseAmount, parseRecord, previewFill, requiredClaimDepth, resolve, tokenMeta, tradeFee, transfer };
package/dist/index.js CHANGED
@@ -40,7 +40,7 @@ var DEPLOY_FEE = 1e8;
40
40
  var V18_HEIGHT = 4e4;
41
41
  var NAME_FEE_SHORT_V18 = 670000000n;
42
42
  var NAME_FEE_V18 = 300000000n;
43
- var V24_HEIGHT = 49200;
43
+ var V24_HEIGHT = 46400;
44
44
  var NAME_FEE_LEN3_V24 = 1500000000n;
45
45
  var NAME_FEE_LEN4_V24 = 1000000000n;
46
46
  var NAME_FEE_MID_V24 = 500000000n;
@@ -81,13 +81,13 @@ var HASH_RE = /^0x[0-9a-f]{64}$/;
81
81
  var SALT_RE = /^[0-9a-fA-F]{16,128}$/;
82
82
  var RESERVED_NAMES = /* @__PURE__ */ new Set(["csd", "treasury", "admin", "official", "root", "www", "support"]);
83
83
  var COMMIT_MAX_BLOCKS = 8 * EPOCH_LEN;
84
- var V25_HEIGHT = 51e3;
84
+ var V25_HEIGHT = 46440;
85
85
  var REG_COMMIT_MAX_BLOCKS = 8;
86
86
  var REG_FINALIZE_GRACE_BLOCKS = 20;
87
87
  var MAX_PENDING_REG = 3;
88
88
  var FINALIZE_TIP_MARGIN = 2;
89
- var V26_HEIGHT = 51200;
90
- var V27_HEIGHT = 52500;
89
+ var V26_HEIGHT = 46480;
90
+ var V27_HEIGHT = 46520;
91
91
  var epochOf = (height) => Math.floor(height / EPOCH_LEN);
92
92
  var claimWindowAt = (height) => height >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
93
93
  var claimWindowOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
@@ -1262,6 +1262,70 @@ function requiredClaimDepth(valueSats, height) {
1262
1262
  return { depth: max, reversalPct: Math.pow(r, max) * 100, capped: true };
1263
1263
  }
1264
1264
 
1265
+ // src/preflight.ts
1266
+ function previewFill(offer2, payRaw) {
1267
+ const pay = BigInt(payRaw);
1268
+ const zero = { deliverable: false, got: 0n, pay: 0n, fee: 0n, rebate: 0n };
1269
+ if (offer2.status !== "open") return { ...zero, reason: "not-open" };
1270
+ if (isTokenWant(offer2.want)) return { ...zero, reason: "not-csd-priced" };
1271
+ const want = BigInt(offer2.want.value);
1272
+ const feeBps = offer2.feeBps;
1273
+ if (offer2.min !== void 0 && !isNameGive(offer2.give)) {
1274
+ const paidSoFar = BigInt(offer2.paid ?? "0");
1275
+ const remaining = want - paidSoFar;
1276
+ const minV = BigInt(offer2.min);
1277
+ const effMin = remaining < minV ? remaining : minV;
1278
+ if (pay < effMin) return { ...zero, reason: "below-min" };
1279
+ const x = pay < remaining ? pay : remaining;
1280
+ const fee2 = feeBps ? tradeFee(x, feeBps) : 0n;
1281
+ const giveTotal = BigInt(offer2.give.amount);
1282
+ const newPaid = paidSoFar + x;
1283
+ const deliveredSoFar = BigInt(offer2.delivered ?? "0");
1284
+ const out = giveTotal * newPaid / want - deliveredSoFar;
1285
+ if (out === 0n) return { deliverable: false, reason: "zero-delivery", got: 0n, pay: x, fee: fee2, rebate: 0n };
1286
+ return { deliverable: true, reason: "ok", got: out, pay: x, fee: fee2, rebate: 0n };
1287
+ }
1288
+ if (pay < want) return { deliverable: false, reason: "below-min", got: 0n, pay, fee: 0n, rebate: 0n };
1289
+ const fee = feeBps ? tradeFee(want, feeBps) : 0n;
1290
+ const restingLiquidity = offer2.taker !== void 0 && offer2.bid !== void 0 || offer2.height >= V17_HEIGHT && offer2.taker === void 0;
1291
+ const rebate = offer2.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
1292
+ const got = isNameGive(offer2.give) ? 1n : BigInt(offer2.give.amount);
1293
+ return { deliverable: true, reason: "ok", got, pay: want, fee, rebate };
1294
+ }
1295
+ function isOpenClaimLane(offer2, tip) {
1296
+ return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
1297
+ }
1298
+ function hasLiveClaim(offer2, me, tip) {
1299
+ if (tip < V17_HEIGHT) return false;
1300
+ if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
1301
+ if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
1302
+ const grace = claimGraceOf(offer2.claimUntilHeight);
1303
+ return tip < offer2.claimUntilHeight + grace;
1304
+ }
1305
+ function fillIsSafe(offer2, me, pay, tip) {
1306
+ const preview = previewFill(offer2, pay);
1307
+ if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
1308
+ if (offer2.taker !== void 0 && offer2.taker.toLowerCase() !== me.toLowerCase())
1309
+ return { safe: false, reason: "taker-bound offer \u2014 not bound to you", preview };
1310
+ if (isOpenClaimLane(offer2, tip) && !hasLiveClaim(offer2, me, tip))
1311
+ return { safe: false, reason: "open CSD offer \u2014 claim it first and fill while your claim is live", preview };
1312
+ if (isTokenWant(offer2.want)) return { safe: true, reason: "token-priced \u2014 deliverability is the buyer's balance", preview };
1313
+ if (!preview.deliverable) {
1314
+ if (preview.reason === "below-min") return { safe: false, reason: "payment is below the offer minimum", preview };
1315
+ return { safe: false, reason: "this payment would deliver 0 tokens \u2014 refusing (the CSD would be lost)", preview };
1316
+ }
1317
+ return { safe: true, reason: "ok", preview };
1318
+ }
1319
+ function finalizeWinnerCheck(nameState, me, commitHeight) {
1320
+ const m = me.toLowerCase();
1321
+ if (!nameState) return { safe: false, reason: "no reservation on-chain (displaced, swept, or never accepted) \u2014 a finalize now would burn the fee" };
1322
+ if (nameState.owner?.toLowerCase() !== m) return { safe: false, reason: "an earlier committer won this name \u2014 you were outbid" };
1323
+ if (nameState.pending !== true) return { safe: false, reason: "already registered to you \u2014 no second finalize fee is needed" };
1324
+ if (Number(nameState.effectiveHeight) !== Number(commitHeight))
1325
+ return { safe: false, reason: "your reservation was displaced (effective height changed) \u2014 a finalize now would burn the fee" };
1326
+ return { safe: true, reason: "ok" };
1327
+ }
1328
+
1265
1329
  // src/paidto.ts
1266
1330
  function paidToFromOutputs(outputs) {
1267
1331
  const m = /* @__PURE__ */ Object.create(null);
@@ -1374,8 +1438,12 @@ export {
1374
1438
  deploy,
1375
1439
  epochOf,
1376
1440
  expiredClaimFee,
1441
+ fillIsSafe,
1442
+ finalizeWinnerCheck,
1443
+ hasLiveClaim,
1377
1444
  isName,
1378
1445
  isNameGive,
1446
+ isOpenClaimLane,
1379
1447
  isTokenWant,
1380
1448
  makerRebate,
1381
1449
  mint,
@@ -1395,6 +1463,7 @@ export {
1395
1463
  parseAmount,
1396
1464
  parseRecord,
1397
1465
  payloadHash2 as payloadHash,
1466
+ previewFill,
1398
1467
  requiredClaimDepth,
1399
1468
  resolve,
1400
1469
  tokenMeta,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/cairnx-core",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -45,6 +45,6 @@
45
45
  "homepage": "https://cairn-substrate.com/trade",
46
46
  "scripts": {
47
47
  "build": "tsup src/index.ts --format esm,cjs --dts --clean",
48
- "test": "tsx test/vectors.test.ts && tsx test/name-decoy.test.ts && tsx test/client-helpers.test.ts && tsx test/paidto.test.ts"
48
+ "test": "tsx test/vectors.test.ts && tsx test/name-decoy.test.ts && tsx test/client-helpers.test.ts && tsx test/paidto.test.ts && tsx test/preflight.test.ts"
49
49
  }
50
50
  }