@inversealtruism/cairnx-core 0.1.34 → 0.1.35

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
@@ -39,6 +39,7 @@ __export(index_exports, {
39
39
  EPOCH_LEN: () => import_csd_codec4.EPOCH_LEN,
40
40
  FEE_BPS: () => FEE_BPS,
41
41
  FEE_BPS_V16: () => FEE_BPS_V16,
42
+ FEE_GATE_MARGIN_BLOCKS: () => FEE_GATE_MARGIN_BLOCKS,
42
43
  FINALIZE_TIP_MARGIN: () => FINALIZE_TIP_MARGIN,
43
44
  HALVING_INTERVAL: () => import_csd_codec4.HALVING_INTERVAL,
44
45
  HASH_RE: () => HASH_RE,
@@ -101,6 +102,7 @@ __export(index_exports, {
101
102
  ZERO_ADDR: () => ZERO_ADDR,
102
103
  bid: () => bid,
103
104
  blockReward: () => import_csd_codec4.blockReward,
105
+ buildFeeHeight: () => buildFeeHeight,
104
106
  buildRecord: () => buildRecord,
105
107
  canonicalJson: () => import_csd_codec4.canonicalJson,
106
108
  canonicalState: () => canonicalState,
@@ -135,8 +137,11 @@ __export(index_exports, {
135
137
  parseAmount: () => parseAmount,
136
138
  parseRecord: () => parseRecord,
137
139
  payloadHash: () => import_csd_codec4.payloadHash,
140
+ pickPrimaryName: () => pickPrimaryName,
138
141
  previewFill: () => previewFill,
142
+ primaryRankBefore: () => primaryRankBefore,
139
143
  requiredClaimDepth: () => requiredClaimDepth,
144
+ requiredFillOutputs: () => requiredFillOutputs,
140
145
  resolve: () => resolve,
141
146
  tokenMeta: () => tokenMeta,
142
147
  tradeFee: () => tradeFee,
@@ -1438,6 +1443,29 @@ function previewFill(offer2, payRaw) {
1438
1443
  const got = isNameGive(offer2.give) ? 1n : BigInt(offer2.give.amount);
1439
1444
  return { deliverable: true, reason: "ok", got, pay: want, fee, rebate };
1440
1445
  }
1446
+ function requiredFillOutputs(offer2, payRaw) {
1447
+ if (isTokenWant(offer2.want)) return [];
1448
+ const p = previewFill(offer2, payRaw);
1449
+ if (!p.deliverable) return null;
1450
+ const need = /* @__PURE__ */ new Map();
1451
+ const add = (a, v) => {
1452
+ if (v > 0n) {
1453
+ const k = a.toLowerCase();
1454
+ need.set(k, (need.get(k) ?? 0n) + v);
1455
+ }
1456
+ };
1457
+ add(offer2.want.payto, p.pay);
1458
+ add(TREASURY_ADDR, p.fee);
1459
+ add(offer2.seller, p.rebate);
1460
+ return [...need].map(([to, value]) => ({ to, value }));
1461
+ }
1462
+ var FEE_GATE_MARGIN_BLOCKS = 5;
1463
+ var NAME_FEE_GATES = [V18_HEIGHT, V24_HEIGHT];
1464
+ var buildFeeHeight = (tip) => {
1465
+ const t = Number(tip);
1466
+ for (const g of NAME_FEE_GATES) if (t < g && t >= g - FEE_GATE_MARGIN_BLOCKS) return g;
1467
+ return t;
1468
+ };
1441
1469
  function isOpenClaimLane(offer2, tip) {
1442
1470
  return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
1443
1471
  }
@@ -1472,6 +1500,19 @@ function finalizeWinnerCheck(nameState, me, commitHeight) {
1472
1500
  return { safe: true, reason: "ok" };
1473
1501
  }
1474
1502
 
1503
+ // src/primary.ts
1504
+ var primaryRankBefore = (a, b) => a.effectiveHeight < b.effectiveHeight || a.effectiveHeight === b.effectiveHeight && a.claimId < b.claimId;
1505
+ function pickPrimaryName(names, a) {
1506
+ const q = String(a).toLowerCase();
1507
+ let best = null;
1508
+ for (const n of names) {
1509
+ if (n.owner !== q || n.addr !== q) continue;
1510
+ if (n.expired === true || n.locked) continue;
1511
+ if (!best || primaryRankBefore(n, best)) best = n;
1512
+ }
1513
+ return best ? best.name : null;
1514
+ }
1515
+
1475
1516
  // src/paidto.ts
1476
1517
  function paidToFromOutputs(outputs) {
1477
1518
  const m = /* @__PURE__ */ Object.create(null);
@@ -1505,6 +1546,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1505
1546
  EPOCH_LEN,
1506
1547
  FEE_BPS,
1507
1548
  FEE_BPS_V16,
1549
+ FEE_GATE_MARGIN_BLOCKS,
1508
1550
  FINALIZE_TIP_MARGIN,
1509
1551
  HALVING_INTERVAL,
1510
1552
  HASH_RE,
@@ -1567,6 +1609,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1567
1609
  ZERO_ADDR,
1568
1610
  bid,
1569
1611
  blockReward,
1612
+ buildFeeHeight,
1570
1613
  buildRecord,
1571
1614
  canonicalJson,
1572
1615
  canonicalState,
@@ -1601,8 +1644,11 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1601
1644
  parseAmount,
1602
1645
  parseRecord,
1603
1646
  payloadHash,
1647
+ pickPrimaryName,
1604
1648
  previewFill,
1649
+ primaryRankBefore,
1605
1650
  requiredClaimDepth,
1651
+ requiredFillOutputs,
1606
1652
  resolve,
1607
1653
  tokenMeta,
1608
1654
  tradeFee,
package/dist/index.d.cts CHANGED
@@ -448,6 +448,32 @@ interface FillPreview {
448
448
  }
449
449
  /** Preview a fill of `offer` paying `payRaw` CSD base units to the seller. Pure; no I/O. */
450
450
  declare function previewFill(offer: OfferState, payRaw: bigint | string | number): FillPreview;
451
+ /** The exact CSD outputs a fill of `offer` paying `payRaw` must carry to clear the resolver's value
452
+ * gate — the addr→sum map of resolve.ts:699-712 (whole: payto + treasury fee + maker rebate) and
453
+ * :643-649 (partial: clamped payment + fee on the clamped amount, NO rebate), as a build-ready output
454
+ * list. Amounts are ACCUMULATED per address (payto === seller is the common case; the resolver checks
455
+ * the per-address SUM, so two merged entries must merge here too), insertion order payto → treasury →
456
+ * seller, zero-value entries omitted (a 0 output is unbuildable and the resolver requires nothing).
457
+ *
458
+ * Returns [] for a token-priced offer (a token⇄token fill carries NO CSD outputs) and null when the
459
+ * payment is undeliverable — gate with fillIsSafe FIRST; this sizes outputs, it does not re-judge
460
+ * claims/takers. Until 2026-07-06 this map was hand-mirrored in the wallet's fillOffer, the cairnx
461
+ * service txbuild, and TWICE in the cairn trade UI — four copies of fund-loss-class math with no
462
+ * cross-repo lock. This is now the only place it lives; test/preflight.test.ts pins it against the
463
+ * real resolver (outputs accepted; any single-unit per-address underpayment refused). */
464
+ declare function requiredFillOutputs(offer: OfferState, payRaw: bigint | string | number): {
465
+ to: string;
466
+ value: bigint;
467
+ }[] | null;
468
+ /** Name-fee build heuristic (app-side, NOT a resolver rule): a name-fee OUTPUT built just below a fee
469
+ * gate but mined at/after it underpays the new tier → resolver rejects → the treasury-fee UTXO is
470
+ * forfeit. Within FEE_GATE_MARGIN_BLOCKS below ANY upcoming fee gate, price the BUILD at that gate:
471
+ * overpay is always accepted (the resolver gate is strict `<`), so this only ever PREVENTS a forfeit,
472
+ * never causes one. Display paths must pass the SAME buildFeeHeight(tip) so the quoted price matches
473
+ * what gets signed. Owns the gate list: a future fee tier (V28+) is added HERE, once — the wallet and
474
+ * the trade UI import this (they each hand-carried a copy of this function + list until 2026-07-06). */
475
+ declare const FEE_GATE_MARGIN_BLOCKS = 5;
476
+ declare const buildFeeHeight: (tip: number) => number;
451
477
  /** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
452
478
  * Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
453
479
  declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
@@ -477,9 +503,16 @@ declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me
477
503
  reason: string;
478
504
  };
479
505
 
506
+ /** The resolver's total order over primary-name candidates: does `a` outrank `b`?
507
+ * Lowest effectiveHeight wins; ties break by ordinal (code-unit) claimId. Exported separately because
508
+ * the trade UI's primary-SWITCH planner needs the comparator against a target, not the selection. */
509
+ declare const primaryRankBefore: (a: NameState, b: NameState) => boolean;
510
+ /** The primary .csd name for address `a`, or null. Pure; no I/O. `a` may be any case. */
511
+ declare function pickPrimaryName(names: Iterable<NameState>, a: string): string | null;
512
+
480
513
  declare function paidToFromOutputs(outputs: readonly {
481
514
  addr: string;
482
515
  value: number;
483
516
  }[]): Record<string, string>;
484
517
 
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 };
518
+ 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, FEE_GATE_MARGIN_BLOCKS, 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, buildFeeHeight, 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, pickPrimaryName, previewFill, primaryRankBefore, requiredClaimDepth, requiredFillOutputs, resolve, tokenMeta, tradeFee, transfer };
package/dist/index.d.ts CHANGED
@@ -448,6 +448,32 @@ interface FillPreview {
448
448
  }
449
449
  /** Preview a fill of `offer` paying `payRaw` CSD base units to the seller. Pure; no I/O. */
450
450
  declare function previewFill(offer: OfferState, payRaw: bigint | string | number): FillPreview;
451
+ /** The exact CSD outputs a fill of `offer` paying `payRaw` must carry to clear the resolver's value
452
+ * gate — the addr→sum map of resolve.ts:699-712 (whole: payto + treasury fee + maker rebate) and
453
+ * :643-649 (partial: clamped payment + fee on the clamped amount, NO rebate), as a build-ready output
454
+ * list. Amounts are ACCUMULATED per address (payto === seller is the common case; the resolver checks
455
+ * the per-address SUM, so two merged entries must merge here too), insertion order payto → treasury →
456
+ * seller, zero-value entries omitted (a 0 output is unbuildable and the resolver requires nothing).
457
+ *
458
+ * Returns [] for a token-priced offer (a token⇄token fill carries NO CSD outputs) and null when the
459
+ * payment is undeliverable — gate with fillIsSafe FIRST; this sizes outputs, it does not re-judge
460
+ * claims/takers. Until 2026-07-06 this map was hand-mirrored in the wallet's fillOffer, the cairnx
461
+ * service txbuild, and TWICE in the cairn trade UI — four copies of fund-loss-class math with no
462
+ * cross-repo lock. This is now the only place it lives; test/preflight.test.ts pins it against the
463
+ * real resolver (outputs accepted; any single-unit per-address underpayment refused). */
464
+ declare function requiredFillOutputs(offer: OfferState, payRaw: bigint | string | number): {
465
+ to: string;
466
+ value: bigint;
467
+ }[] | null;
468
+ /** Name-fee build heuristic (app-side, NOT a resolver rule): a name-fee OUTPUT built just below a fee
469
+ * gate but mined at/after it underpays the new tier → resolver rejects → the treasury-fee UTXO is
470
+ * forfeit. Within FEE_GATE_MARGIN_BLOCKS below ANY upcoming fee gate, price the BUILD at that gate:
471
+ * overpay is always accepted (the resolver gate is strict `<`), so this only ever PREVENTS a forfeit,
472
+ * never causes one. Display paths must pass the SAME buildFeeHeight(tip) so the quoted price matches
473
+ * what gets signed. Owns the gate list: a future fee tier (V28+) is added HERE, once — the wallet and
474
+ * the trade UI import this (they each hand-carried a copy of this function + list until 2026-07-06). */
475
+ declare const FEE_GATE_MARGIN_BLOCKS = 5;
476
+ declare const buildFeeHeight: (tip: number) => number;
451
477
  /** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
452
478
  * Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
453
479
  declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
@@ -477,9 +503,16 @@ declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me
477
503
  reason: string;
478
504
  };
479
505
 
506
+ /** The resolver's total order over primary-name candidates: does `a` outrank `b`?
507
+ * Lowest effectiveHeight wins; ties break by ordinal (code-unit) claimId. Exported separately because
508
+ * the trade UI's primary-SWITCH planner needs the comparator against a target, not the selection. */
509
+ declare const primaryRankBefore: (a: NameState, b: NameState) => boolean;
510
+ /** The primary .csd name for address `a`, or null. Pure; no I/O. `a` may be any case. */
511
+ declare function pickPrimaryName(names: Iterable<NameState>, a: string): string | null;
512
+
480
513
  declare function paidToFromOutputs(outputs: readonly {
481
514
  addr: string;
482
515
  value: number;
483
516
  }[]): Record<string, string>;
484
517
 
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 };
518
+ 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, FEE_GATE_MARGIN_BLOCKS, 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, buildFeeHeight, 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, pickPrimaryName, previewFill, primaryRankBefore, requiredClaimDepth, requiredFillOutputs, resolve, tokenMeta, tradeFee, transfer };
package/dist/index.js CHANGED
@@ -1292,6 +1292,29 @@ function previewFill(offer2, payRaw) {
1292
1292
  const got = isNameGive(offer2.give) ? 1n : BigInt(offer2.give.amount);
1293
1293
  return { deliverable: true, reason: "ok", got, pay: want, fee, rebate };
1294
1294
  }
1295
+ function requiredFillOutputs(offer2, payRaw) {
1296
+ if (isTokenWant(offer2.want)) return [];
1297
+ const p = previewFill(offer2, payRaw);
1298
+ if (!p.deliverable) return null;
1299
+ const need = /* @__PURE__ */ new Map();
1300
+ const add = (a, v) => {
1301
+ if (v > 0n) {
1302
+ const k = a.toLowerCase();
1303
+ need.set(k, (need.get(k) ?? 0n) + v);
1304
+ }
1305
+ };
1306
+ add(offer2.want.payto, p.pay);
1307
+ add(TREASURY_ADDR, p.fee);
1308
+ add(offer2.seller, p.rebate);
1309
+ return [...need].map(([to, value]) => ({ to, value }));
1310
+ }
1311
+ var FEE_GATE_MARGIN_BLOCKS = 5;
1312
+ var NAME_FEE_GATES = [V18_HEIGHT, V24_HEIGHT];
1313
+ var buildFeeHeight = (tip) => {
1314
+ const t = Number(tip);
1315
+ for (const g of NAME_FEE_GATES) if (t < g && t >= g - FEE_GATE_MARGIN_BLOCKS) return g;
1316
+ return t;
1317
+ };
1295
1318
  function isOpenClaimLane(offer2, tip) {
1296
1319
  return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
1297
1320
  }
@@ -1326,6 +1349,19 @@ function finalizeWinnerCheck(nameState, me, commitHeight) {
1326
1349
  return { safe: true, reason: "ok" };
1327
1350
  }
1328
1351
 
1352
+ // src/primary.ts
1353
+ var primaryRankBefore = (a, b) => a.effectiveHeight < b.effectiveHeight || a.effectiveHeight === b.effectiveHeight && a.claimId < b.claimId;
1354
+ function pickPrimaryName(names, a) {
1355
+ const q = String(a).toLowerCase();
1356
+ let best = null;
1357
+ for (const n of names) {
1358
+ if (n.owner !== q || n.addr !== q) continue;
1359
+ if (n.expired === true || n.locked) continue;
1360
+ if (!best || primaryRankBefore(n, best)) best = n;
1361
+ }
1362
+ return best ? best.name : null;
1363
+ }
1364
+
1329
1365
  // src/paidto.ts
1330
1366
  function paidToFromOutputs(outputs) {
1331
1367
  const m = /* @__PURE__ */ Object.create(null);
@@ -1367,6 +1403,7 @@ export {
1367
1403
  EPOCH_LEN2 as EPOCH_LEN,
1368
1404
  FEE_BPS,
1369
1405
  FEE_BPS_V16,
1406
+ FEE_GATE_MARGIN_BLOCKS,
1370
1407
  FINALIZE_TIP_MARGIN,
1371
1408
  HALVING_INTERVAL,
1372
1409
  HASH_RE,
@@ -1429,6 +1466,7 @@ export {
1429
1466
  ZERO_ADDR,
1430
1467
  bid,
1431
1468
  blockReward2 as blockReward,
1469
+ buildFeeHeight,
1432
1470
  buildRecord,
1433
1471
  canonicalJson2 as canonicalJson,
1434
1472
  canonicalState,
@@ -1463,8 +1501,11 @@ export {
1463
1501
  parseAmount,
1464
1502
  parseRecord,
1465
1503
  payloadHash2 as payloadHash,
1504
+ pickPrimaryName,
1466
1505
  previewFill,
1506
+ primaryRankBefore,
1467
1507
  requiredClaimDepth,
1508
+ requiredFillOutputs,
1468
1509
  resolve,
1469
1510
  tokenMeta,
1470
1511
  tradeFee,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/cairnx-core",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
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 && tsx test/preflight.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 && tsx test/primary.test.ts"
49
49
  }
50
50
  }