@inversealtruism/cairnx-core 0.1.33 → 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 +120 -0
- package/dist/index.d.cts +81 -1
- package/dist/index.d.ts +81 -1
- package/dist/index.js +110 -0
- package/package.json +2 -2
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,
|
|
@@ -110,8 +112,12 @@ __export(index_exports, {
|
|
|
110
112
|
deploy: () => deploy,
|
|
111
113
|
epochOf: () => epochOf,
|
|
112
114
|
expiredClaimFee: () => expiredClaimFee,
|
|
115
|
+
fillIsSafe: () => fillIsSafe,
|
|
116
|
+
finalizeWinnerCheck: () => finalizeWinnerCheck,
|
|
117
|
+
hasLiveClaim: () => hasLiveClaim,
|
|
113
118
|
isName: () => isName,
|
|
114
119
|
isNameGive: () => isNameGive,
|
|
120
|
+
isOpenClaimLane: () => isOpenClaimLane,
|
|
115
121
|
isTokenWant: () => isTokenWant,
|
|
116
122
|
makerRebate: () => makerRebate,
|
|
117
123
|
mint: () => mint,
|
|
@@ -131,7 +137,11 @@ __export(index_exports, {
|
|
|
131
137
|
parseAmount: () => parseAmount,
|
|
132
138
|
parseRecord: () => parseRecord,
|
|
133
139
|
payloadHash: () => import_csd_codec4.payloadHash,
|
|
140
|
+
pickPrimaryName: () => pickPrimaryName,
|
|
141
|
+
previewFill: () => previewFill,
|
|
142
|
+
primaryRankBefore: () => primaryRankBefore,
|
|
134
143
|
requiredClaimDepth: () => requiredClaimDepth,
|
|
144
|
+
requiredFillOutputs: () => requiredFillOutputs,
|
|
135
145
|
resolve: () => resolve,
|
|
136
146
|
tokenMeta: () => tokenMeta,
|
|
137
147
|
tradeFee: () => tradeFee,
|
|
@@ -1403,6 +1413,106 @@ function requiredClaimDepth(valueSats, height) {
|
|
|
1403
1413
|
return { depth: max, reversalPct: Math.pow(r, max) * 100, capped: true };
|
|
1404
1414
|
}
|
|
1405
1415
|
|
|
1416
|
+
// src/preflight.ts
|
|
1417
|
+
function previewFill(offer2, payRaw) {
|
|
1418
|
+
const pay = BigInt(payRaw);
|
|
1419
|
+
const zero = { deliverable: false, got: 0n, pay: 0n, fee: 0n, rebate: 0n };
|
|
1420
|
+
if (offer2.status !== "open") return { ...zero, reason: "not-open" };
|
|
1421
|
+
if (isTokenWant(offer2.want)) return { ...zero, reason: "not-csd-priced" };
|
|
1422
|
+
const want = BigInt(offer2.want.value);
|
|
1423
|
+
const feeBps = offer2.feeBps;
|
|
1424
|
+
if (offer2.min !== void 0 && !isNameGive(offer2.give)) {
|
|
1425
|
+
const paidSoFar = BigInt(offer2.paid ?? "0");
|
|
1426
|
+
const remaining = want - paidSoFar;
|
|
1427
|
+
const minV = BigInt(offer2.min);
|
|
1428
|
+
const effMin = remaining < minV ? remaining : minV;
|
|
1429
|
+
if (pay < effMin) return { ...zero, reason: "below-min" };
|
|
1430
|
+
const x = pay < remaining ? pay : remaining;
|
|
1431
|
+
const fee2 = feeBps ? tradeFee(x, feeBps) : 0n;
|
|
1432
|
+
const giveTotal = BigInt(offer2.give.amount);
|
|
1433
|
+
const newPaid = paidSoFar + x;
|
|
1434
|
+
const deliveredSoFar = BigInt(offer2.delivered ?? "0");
|
|
1435
|
+
const out = giveTotal * newPaid / want - deliveredSoFar;
|
|
1436
|
+
if (out === 0n) return { deliverable: false, reason: "zero-delivery", got: 0n, pay: x, fee: fee2, rebate: 0n };
|
|
1437
|
+
return { deliverable: true, reason: "ok", got: out, pay: x, fee: fee2, rebate: 0n };
|
|
1438
|
+
}
|
|
1439
|
+
if (pay < want) return { deliverable: false, reason: "below-min", got: 0n, pay, fee: 0n, rebate: 0n };
|
|
1440
|
+
const fee = feeBps ? tradeFee(want, feeBps) : 0n;
|
|
1441
|
+
const restingLiquidity = offer2.taker !== void 0 && offer2.bid !== void 0 || offer2.height >= V17_HEIGHT && offer2.taker === void 0;
|
|
1442
|
+
const rebate = offer2.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
|
|
1443
|
+
const got = isNameGive(offer2.give) ? 1n : BigInt(offer2.give.amount);
|
|
1444
|
+
return { deliverable: true, reason: "ok", got, pay: want, fee, rebate };
|
|
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
|
+
};
|
|
1469
|
+
function isOpenClaimLane(offer2, tip) {
|
|
1470
|
+
return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
|
|
1471
|
+
}
|
|
1472
|
+
function hasLiveClaim(offer2, me, tip) {
|
|
1473
|
+
if (tip < V17_HEIGHT) return false;
|
|
1474
|
+
if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
|
|
1475
|
+
if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
|
|
1476
|
+
const grace = claimGraceOf(offer2.claimUntilHeight);
|
|
1477
|
+
return tip < offer2.claimUntilHeight + grace;
|
|
1478
|
+
}
|
|
1479
|
+
function fillIsSafe(offer2, me, pay, tip) {
|
|
1480
|
+
const preview = previewFill(offer2, pay);
|
|
1481
|
+
if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
|
|
1482
|
+
if (offer2.taker !== void 0 && offer2.taker.toLowerCase() !== me.toLowerCase())
|
|
1483
|
+
return { safe: false, reason: "taker-bound offer \u2014 not bound to you", preview };
|
|
1484
|
+
if (isOpenClaimLane(offer2, tip) && !hasLiveClaim(offer2, me, tip))
|
|
1485
|
+
return { safe: false, reason: "open CSD offer \u2014 claim it first and fill while your claim is live", preview };
|
|
1486
|
+
if (isTokenWant(offer2.want)) return { safe: true, reason: "token-priced \u2014 deliverability is the buyer's balance", preview };
|
|
1487
|
+
if (!preview.deliverable) {
|
|
1488
|
+
if (preview.reason === "below-min") return { safe: false, reason: "payment is below the offer minimum", preview };
|
|
1489
|
+
return { safe: false, reason: "this payment would deliver 0 tokens \u2014 refusing (the CSD would be lost)", preview };
|
|
1490
|
+
}
|
|
1491
|
+
return { safe: true, reason: "ok", preview };
|
|
1492
|
+
}
|
|
1493
|
+
function finalizeWinnerCheck(nameState, me, commitHeight) {
|
|
1494
|
+
const m = me.toLowerCase();
|
|
1495
|
+
if (!nameState) return { safe: false, reason: "no reservation on-chain (displaced, swept, or never accepted) \u2014 a finalize now would burn the fee" };
|
|
1496
|
+
if (nameState.owner?.toLowerCase() !== m) return { safe: false, reason: "an earlier committer won this name \u2014 you were outbid" };
|
|
1497
|
+
if (nameState.pending !== true) return { safe: false, reason: "already registered to you \u2014 no second finalize fee is needed" };
|
|
1498
|
+
if (Number(nameState.effectiveHeight) !== Number(commitHeight))
|
|
1499
|
+
return { safe: false, reason: "your reservation was displaced (effective height changed) \u2014 a finalize now would burn the fee" };
|
|
1500
|
+
return { safe: true, reason: "ok" };
|
|
1501
|
+
}
|
|
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
|
+
|
|
1406
1516
|
// src/paidto.ts
|
|
1407
1517
|
function paidToFromOutputs(outputs) {
|
|
1408
1518
|
const m = /* @__PURE__ */ Object.create(null);
|
|
@@ -1436,6 +1546,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
|
|
|
1436
1546
|
EPOCH_LEN,
|
|
1437
1547
|
FEE_BPS,
|
|
1438
1548
|
FEE_BPS_V16,
|
|
1549
|
+
FEE_GATE_MARGIN_BLOCKS,
|
|
1439
1550
|
FINALIZE_TIP_MARGIN,
|
|
1440
1551
|
HALVING_INTERVAL,
|
|
1441
1552
|
HASH_RE,
|
|
@@ -1498,6 +1609,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
|
|
|
1498
1609
|
ZERO_ADDR,
|
|
1499
1610
|
bid,
|
|
1500
1611
|
blockReward,
|
|
1612
|
+
buildFeeHeight,
|
|
1501
1613
|
buildRecord,
|
|
1502
1614
|
canonicalJson,
|
|
1503
1615
|
canonicalState,
|
|
@@ -1507,8 +1619,12 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
|
|
|
1507
1619
|
deploy,
|
|
1508
1620
|
epochOf,
|
|
1509
1621
|
expiredClaimFee,
|
|
1622
|
+
fillIsSafe,
|
|
1623
|
+
finalizeWinnerCheck,
|
|
1624
|
+
hasLiveClaim,
|
|
1510
1625
|
isName,
|
|
1511
1626
|
isNameGive,
|
|
1627
|
+
isOpenClaimLane,
|
|
1512
1628
|
isTokenWant,
|
|
1513
1629
|
makerRebate,
|
|
1514
1630
|
mint,
|
|
@@ -1528,7 +1644,11 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
|
|
|
1528
1644
|
parseAmount,
|
|
1529
1645
|
parseRecord,
|
|
1530
1646
|
payloadHash,
|
|
1647
|
+
pickPrimaryName,
|
|
1648
|
+
previewFill,
|
|
1649
|
+
primaryRankBefore,
|
|
1531
1650
|
requiredClaimDepth,
|
|
1651
|
+
requiredFillOutputs,
|
|
1532
1652
|
resolve,
|
|
1533
1653
|
tokenMeta,
|
|
1534
1654
|
tradeFee,
|
package/dist/index.d.cts
CHANGED
|
@@ -430,9 +430,89 @@ 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
|
+
/** 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;
|
|
477
|
+
/** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
|
|
478
|
+
* Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
|
|
479
|
+
declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
|
|
480
|
+
/** Does `me` currently hold a LIVE claim on `offer` at `tip`? Mirrors resolve.ts claimHeld + the
|
|
481
|
+
* who===claimedBy check (:160-169). For a pre-V17 open offer, open fills are banned entirely. */
|
|
482
|
+
declare function hasLiveClaim(offer: OfferState, me: string, tip: number): boolean;
|
|
483
|
+
/** The union verdict a client must clear BEFORE signing a fill of `offer` paying `pay`, as `me`, at `tip`.
|
|
484
|
+
* Closes C2/C3/C4: status open, deliverability >= 1 token, live-claim holdership for the open-CSD lane,
|
|
485
|
+
* and taker match. `safe:true` ⇒ the fill is guaranteed to be accepted by the deterministic gates a
|
|
486
|
+
* payer can verify at signing time (the irreducible timing residual — CLAIM_FILL_GRACE_BLOCKS late mine
|
|
487
|
+
* — is documented, not closable by a preflight). Returns the preview so the caller can size outputs. */
|
|
488
|
+
interface FillSafety {
|
|
489
|
+
safe: boolean;
|
|
490
|
+
reason: string;
|
|
491
|
+
preview: FillPreview;
|
|
492
|
+
}
|
|
493
|
+
declare function fillIsSafe(offer: OfferState, me: string, pay: bigint | string | number, tip: number): FillSafety;
|
|
494
|
+
/** C1: may `me` safely sign the fee-bearing registration `nfinalize` for `nameState` committed at
|
|
495
|
+
* `commitHeight`? The reg fee rides nfinalize; a finalize on a reservation that was displaced or has
|
|
496
|
+
* expired is REJECTED after the fee moved (a burn). Sound by the freeze arithmetic: once a client waits
|
|
497
|
+
* for `tip > effectiveHeight + REG_COMMIT_MAX_BLOCKS` (which the UI's finalizeReady already enforces) no
|
|
498
|
+
* new displacer can appear, so a re-fetch showing "still my live pending reservation at my commit height"
|
|
499
|
+
* guarantees the finalize lands. Pass the AUTHORITATIVE (freshly re-fetched) name record — never a cached
|
|
500
|
+
* one — as `nameState` (undefined = the resolver returns 404 / unregistered, which is a definitive loss). */
|
|
501
|
+
declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me: string, commitHeight: number): {
|
|
502
|
+
safe: boolean;
|
|
503
|
+
reason: string;
|
|
504
|
+
};
|
|
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
|
+
|
|
433
513
|
declare function paidToFromOutputs(outputs: readonly {
|
|
434
514
|
addr: string;
|
|
435
515
|
value: number;
|
|
436
516
|
}[]): Record<string, string>;
|
|
437
517
|
|
|
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 };
|
|
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
|
@@ -430,9 +430,89 @@ 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
|
+
/** 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;
|
|
477
|
+
/** Is the offer an OPEN (untaken) CSD-priced offer subject to the v1.7 claim-to-fill gate at `tip`?
|
|
478
|
+
* Such an offer may be filled ONLY by the holder of a LIVE claim (resolve.ts openFillReject:166-171). */
|
|
479
|
+
declare function isOpenClaimLane(offer: OfferState, tip: number): boolean;
|
|
480
|
+
/** Does `me` currently hold a LIVE claim on `offer` at `tip`? Mirrors resolve.ts claimHeld + the
|
|
481
|
+
* who===claimedBy check (:160-169). For a pre-V17 open offer, open fills are banned entirely. */
|
|
482
|
+
declare function hasLiveClaim(offer: OfferState, me: string, tip: number): boolean;
|
|
483
|
+
/** The union verdict a client must clear BEFORE signing a fill of `offer` paying `pay`, as `me`, at `tip`.
|
|
484
|
+
* Closes C2/C3/C4: status open, deliverability >= 1 token, live-claim holdership for the open-CSD lane,
|
|
485
|
+
* and taker match. `safe:true` ⇒ the fill is guaranteed to be accepted by the deterministic gates a
|
|
486
|
+
* payer can verify at signing time (the irreducible timing residual — CLAIM_FILL_GRACE_BLOCKS late mine
|
|
487
|
+
* — is documented, not closable by a preflight). Returns the preview so the caller can size outputs. */
|
|
488
|
+
interface FillSafety {
|
|
489
|
+
safe: boolean;
|
|
490
|
+
reason: string;
|
|
491
|
+
preview: FillPreview;
|
|
492
|
+
}
|
|
493
|
+
declare function fillIsSafe(offer: OfferState, me: string, pay: bigint | string | number, tip: number): FillSafety;
|
|
494
|
+
/** C1: may `me` safely sign the fee-bearing registration `nfinalize` for `nameState` committed at
|
|
495
|
+
* `commitHeight`? The reg fee rides nfinalize; a finalize on a reservation that was displaced or has
|
|
496
|
+
* expired is REJECTED after the fee moved (a burn). Sound by the freeze arithmetic: once a client waits
|
|
497
|
+
* for `tip > effectiveHeight + REG_COMMIT_MAX_BLOCKS` (which the UI's finalizeReady already enforces) no
|
|
498
|
+
* new displacer can appear, so a re-fetch showing "still my live pending reservation at my commit height"
|
|
499
|
+
* guarantees the finalize lands. Pass the AUTHORITATIVE (freshly re-fetched) name record — never a cached
|
|
500
|
+
* one — as `nameState` (undefined = the resolver returns 404 / unregistered, which is a definitive loss). */
|
|
501
|
+
declare function finalizeWinnerCheck(nameState: NameState | null | undefined, me: string, commitHeight: number): {
|
|
502
|
+
safe: boolean;
|
|
503
|
+
reason: string;
|
|
504
|
+
};
|
|
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
|
+
|
|
433
513
|
declare function paidToFromOutputs(outputs: readonly {
|
|
434
514
|
addr: string;
|
|
435
515
|
value: number;
|
|
436
516
|
}[]): Record<string, string>;
|
|
437
517
|
|
|
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 };
|
|
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
|
@@ -1262,6 +1262,106 @@ 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 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
|
+
};
|
|
1318
|
+
function isOpenClaimLane(offer2, tip) {
|
|
1319
|
+
return tip >= V13_HEIGHT && offer2.taker === void 0 && !isTokenWant(offer2.want);
|
|
1320
|
+
}
|
|
1321
|
+
function hasLiveClaim(offer2, me, tip) {
|
|
1322
|
+
if (tip < V17_HEIGHT) return false;
|
|
1323
|
+
if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
|
|
1324
|
+
if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
|
|
1325
|
+
const grace = claimGraceOf(offer2.claimUntilHeight);
|
|
1326
|
+
return tip < offer2.claimUntilHeight + grace;
|
|
1327
|
+
}
|
|
1328
|
+
function fillIsSafe(offer2, me, pay, tip) {
|
|
1329
|
+
const preview = previewFill(offer2, pay);
|
|
1330
|
+
if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
|
|
1331
|
+
if (offer2.taker !== void 0 && offer2.taker.toLowerCase() !== me.toLowerCase())
|
|
1332
|
+
return { safe: false, reason: "taker-bound offer \u2014 not bound to you", preview };
|
|
1333
|
+
if (isOpenClaimLane(offer2, tip) && !hasLiveClaim(offer2, me, tip))
|
|
1334
|
+
return { safe: false, reason: "open CSD offer \u2014 claim it first and fill while your claim is live", preview };
|
|
1335
|
+
if (isTokenWant(offer2.want)) return { safe: true, reason: "token-priced \u2014 deliverability is the buyer's balance", preview };
|
|
1336
|
+
if (!preview.deliverable) {
|
|
1337
|
+
if (preview.reason === "below-min") return { safe: false, reason: "payment is below the offer minimum", preview };
|
|
1338
|
+
return { safe: false, reason: "this payment would deliver 0 tokens \u2014 refusing (the CSD would be lost)", preview };
|
|
1339
|
+
}
|
|
1340
|
+
return { safe: true, reason: "ok", preview };
|
|
1341
|
+
}
|
|
1342
|
+
function finalizeWinnerCheck(nameState, me, commitHeight) {
|
|
1343
|
+
const m = me.toLowerCase();
|
|
1344
|
+
if (!nameState) return { safe: false, reason: "no reservation on-chain (displaced, swept, or never accepted) \u2014 a finalize now would burn the fee" };
|
|
1345
|
+
if (nameState.owner?.toLowerCase() !== m) return { safe: false, reason: "an earlier committer won this name \u2014 you were outbid" };
|
|
1346
|
+
if (nameState.pending !== true) return { safe: false, reason: "already registered to you \u2014 no second finalize fee is needed" };
|
|
1347
|
+
if (Number(nameState.effectiveHeight) !== Number(commitHeight))
|
|
1348
|
+
return { safe: false, reason: "your reservation was displaced (effective height changed) \u2014 a finalize now would burn the fee" };
|
|
1349
|
+
return { safe: true, reason: "ok" };
|
|
1350
|
+
}
|
|
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
|
+
|
|
1265
1365
|
// src/paidto.ts
|
|
1266
1366
|
function paidToFromOutputs(outputs) {
|
|
1267
1367
|
const m = /* @__PURE__ */ Object.create(null);
|
|
@@ -1303,6 +1403,7 @@ export {
|
|
|
1303
1403
|
EPOCH_LEN2 as EPOCH_LEN,
|
|
1304
1404
|
FEE_BPS,
|
|
1305
1405
|
FEE_BPS_V16,
|
|
1406
|
+
FEE_GATE_MARGIN_BLOCKS,
|
|
1306
1407
|
FINALIZE_TIP_MARGIN,
|
|
1307
1408
|
HALVING_INTERVAL,
|
|
1308
1409
|
HASH_RE,
|
|
@@ -1365,6 +1466,7 @@ export {
|
|
|
1365
1466
|
ZERO_ADDR,
|
|
1366
1467
|
bid,
|
|
1367
1468
|
blockReward2 as blockReward,
|
|
1469
|
+
buildFeeHeight,
|
|
1368
1470
|
buildRecord,
|
|
1369
1471
|
canonicalJson2 as canonicalJson,
|
|
1370
1472
|
canonicalState,
|
|
@@ -1374,8 +1476,12 @@ export {
|
|
|
1374
1476
|
deploy,
|
|
1375
1477
|
epochOf,
|
|
1376
1478
|
expiredClaimFee,
|
|
1479
|
+
fillIsSafe,
|
|
1480
|
+
finalizeWinnerCheck,
|
|
1481
|
+
hasLiveClaim,
|
|
1377
1482
|
isName,
|
|
1378
1483
|
isNameGive,
|
|
1484
|
+
isOpenClaimLane,
|
|
1379
1485
|
isTokenWant,
|
|
1380
1486
|
makerRebate,
|
|
1381
1487
|
mint,
|
|
@@ -1395,7 +1501,11 @@ export {
|
|
|
1395
1501
|
parseAmount,
|
|
1396
1502
|
parseRecord,
|
|
1397
1503
|
payloadHash2 as payloadHash,
|
|
1504
|
+
pickPrimaryName,
|
|
1505
|
+
previewFill,
|
|
1506
|
+
primaryRankBefore,
|
|
1398
1507
|
requiredClaimDepth,
|
|
1508
|
+
requiredFillOutputs,
|
|
1399
1509
|
resolve,
|
|
1400
1510
|
tokenMeta,
|
|
1401
1511
|
tradeFee,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversealtruism/cairnx-core",
|
|
3
|
-
"version": "0.1.
|
|
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"
|
|
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
|
}
|