@inversealtruism/cairnx-core 0.1.35 → 0.1.37

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.js CHANGED
@@ -30,6 +30,8 @@ var CLAIM_WINDOW_BLOCKS_V20 = 40;
30
30
  var CLAIM_FILL_GRACE_BLOCKS = 5;
31
31
  var MAX_ACTIVE_CLAIMS = 3;
32
32
  var CLAIM_COOLDOWN_BLOCKS = 15;
33
+ var FCLAIM_MAX_EPOCH_AHEAD = 2;
34
+ var FILL_TIP_MARGIN = 2;
33
35
  var CONF_TOKEN_FILL = 1e6;
34
36
  var TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
35
37
  var FEE_BPS = 100;
@@ -88,10 +90,13 @@ var MAX_PENDING_REG = 3;
88
90
  var FINALIZE_TIP_MARGIN = 2;
89
91
  var V26_HEIGHT = 46480;
90
92
  var V27_HEIGHT = 46520;
93
+ var V28_HEIGHT = 6e4;
91
94
  var epochOf = (height) => Math.floor(height / EPOCH_LEN);
92
95
  var claimWindowAt = (height) => height >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
93
96
  var claimWindowOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
94
- var claimGraceOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_FILL_GRACE_BLOCKS : 0;
97
+ var claimGraceOf = (claimUntilHeight, claimTxid) => claimTxid !== void 0 ? 0 : claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_FILL_GRACE_BLOCKS : 0;
98
+ var fclaimEpochFor = (tipHeight, offerExpiresEpoch) => Math.min(epochOf(tipHeight + CLAIM_WINDOW_BLOCKS_V20 + CLAIM_FILL_GRACE_BLOCKS), offerExpiresEpoch);
99
+ var fclaimHoldEnd = (expiresEpoch) => (expiresEpoch + 1) * EPOCH_LEN - 1;
95
100
  var offerExpiryHeightOf = (expiresEpoch, anchorHeight) => {
96
101
  const raw = (Number(expiresEpoch ?? 0) + 1) * EPOCH_LEN;
97
102
  if (anchorHeight >= V22_HEIGHT) return raw;
@@ -152,6 +157,7 @@ var BID_KEYS = /* @__PURE__ */ new Set(["v", "t", "want", "give", "memo", "ts"])
152
157
  var NAME_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
153
158
  var NFINALIZE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
154
159
  var NPROFILE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "p"]);
160
+ var FCLAIM_KEYS = /* @__PURE__ */ new Set(["v", "t", "offer"]);
155
161
  function parseRecord(uri, payloadHashHex) {
156
162
  if (new TextEncoder().encode(uri).length > MAX_RECORD_BYTES) return null;
157
163
  let obj;
@@ -305,6 +311,11 @@ function parseRecord(uri, payloadHashHex) {
305
311
  if (Object.keys(r).length !== 4) return null;
306
312
  return r;
307
313
  }
314
+ case "fclaim": {
315
+ if (!onlyKeys(r, FCLAIM_KEYS)) return null;
316
+ if (!isHash(r.offer)) return null;
317
+ return r;
318
+ }
308
319
  default:
309
320
  return null;
310
321
  }
@@ -330,6 +341,7 @@ var nameSet = (r) => buildRecord({ v: 1, t: "nset", ...r });
330
341
  var nameRenew = (r) => buildRecord({ v: 1, t: "nrenew", ...r });
331
342
  var tokenMeta = (r) => buildRecord({ v: 1, t: "tmeta", ...r });
332
343
  var nameProfile = (r) => buildRecord({ v: 1, t: "nprofile", ...r });
344
+ var fclaim = (r) => buildRecord({ v: 1, t: "fclaim", ...r });
333
345
 
334
346
  // src/resolve.ts
335
347
  function ptAmt(v) {
@@ -351,6 +363,7 @@ function resolve(events, tipHeight) {
351
363
  const offers = /* @__PURE__ */ new Map();
352
364
  const offerLock = /* @__PURE__ */ new Map();
353
365
  const bids = /* @__PURE__ */ new Map();
366
+ const fclaims = /* @__PURE__ */ new Map();
354
367
  const log = [];
355
368
  let feesPaid = 0n;
356
369
  let pendingCancels = [];
@@ -414,18 +427,20 @@ function resolve(events, tipHeight) {
414
427
  const b = bids.get(o.bid);
415
428
  if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
416
429
  };
417
- const voidOpenNameOffers = (name) => {
430
+ const voidOpenNameOffers = (name, height) => {
418
431
  for (const o of offers.values()) if (o.status === "open" && isNameGive(o.give) && o.give.name === name) {
432
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, height)) continue;
419
433
  releaseGive(o);
420
434
  o.status = "cancelled";
421
435
  }
422
436
  };
423
437
  const earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
424
- const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight) : 0;
438
+ const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
425
439
  const claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
426
- const openFillReject = (o, height, who) => {
440
+ const openFillReject = (o, height, who, targetId) => {
427
441
  if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
428
442
  if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
443
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
429
444
  if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
430
445
  return void 0;
431
446
  };
@@ -590,7 +605,7 @@ function resolve(events, tipHeight) {
590
605
  note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
591
606
  continue;
592
607
  }
593
- voidOpenNameOffers(rec.name);
608
+ voidOpenNameOffers(rec.name, ev.height);
594
609
  names.set(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, viaFill: true, paidThroughEpoch: epClaim + NAME_TERM_EPOCHS });
595
610
  feesPaid += fee;
596
611
  note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
@@ -613,7 +628,7 @@ function resolve(events, tipHeight) {
613
628
  continue;
614
629
  }
615
630
  if (curActive) {
616
- voidOpenNameOffers(rec.name);
631
+ voidOpenNameOffers(rec.name, ev.height);
617
632
  }
618
633
  names.set(rec.name, { owner: who, effHeight, pos: ev.pos, id: ev.id, height: ev.height, locked: false, pending: true, finalizeBy: effHeight + REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS });
619
634
  note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
@@ -638,7 +653,7 @@ function resolve(events, tipHeight) {
638
653
  continue;
639
654
  }
640
655
  if (curActive) {
641
- voidOpenNameOffers(rec.name);
656
+ voidOpenNameOffers(rec.name, ev.height);
642
657
  }
643
658
  names.set(rec.name, cand);
644
659
  feesPaid += nameRegFee(rec.name, ev.height);
@@ -701,7 +716,7 @@ function resolve(events, tipHeight) {
701
716
  note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
702
717
  continue;
703
718
  }
704
- voidOpenNameOffers(rec.name);
719
+ voidOpenNameOffers(rec.name, ev.height);
705
720
  names.set(rec.name, { owner: who, effHeight: r.effHeight, pos: r.pos, id: r.id, height: r.height, locked: false, paidThroughEpoch: ep + NAME_TERM_EPOCHS });
706
721
  recaptures.delete(rec.name);
707
722
  feesPaid += fee;
@@ -892,6 +907,7 @@ function resolve(events, tipHeight) {
892
907
  const targets = [];
893
908
  for (const o of offers.values()) {
894
909
  if (o.status !== "open" || o.seller !== who) continue;
910
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
895
911
  if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
896
912
  if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
897
913
  targets.push(o);
@@ -899,10 +915,13 @@ function resolve(events, tipHeight) {
899
915
  if (ev.height >= V14_HEIGHT) {
900
916
  pendingCancels.push(() => {
901
917
  let n = 0;
902
- for (const o of targets) if (o.status === "open") {
903
- releaseGive(o);
904
- o.status = "cancelled";
905
- n++;
918
+ for (const o of targets) {
919
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
920
+ if (o.status === "open") {
921
+ releaseGive(o);
922
+ o.status = "cancelled";
923
+ n++;
924
+ }
906
925
  }
907
926
  note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
908
927
  });
@@ -955,11 +974,80 @@ function resolve(events, tipHeight) {
955
974
  }
956
975
  tok.meta.tmeta = rec.hash;
957
976
  note(ev, ev.id, "tmeta", true);
977
+ } else if (rec.t === "fclaim") {
978
+ if (ev.height < V28_HEIGHT) {
979
+ note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
980
+ continue;
981
+ }
982
+ const target = offers.get(rec.offer);
983
+ const E = ev.expiresEpoch;
984
+ const deny = (why) => {
985
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
986
+ note(ev, ev.id, "fclaim", false, why);
987
+ };
988
+ if (!target) {
989
+ deny("unknown offer");
990
+ continue;
991
+ }
992
+ if (target.status !== "open") {
993
+ deny(`offer ${target.status}`);
994
+ continue;
995
+ }
996
+ if (target.taker) {
997
+ deny("taker-bound offer needs no claim");
998
+ continue;
999
+ }
1000
+ if (isTokenWant(target.want)) {
1001
+ deny("claims are for CSD-priced offers");
1002
+ continue;
1003
+ }
1004
+ if (E > effExpiry(target, ev.height)) {
1005
+ deny("hold would outlive the offer expiry");
1006
+ continue;
1007
+ }
1008
+ if (E > epochOf(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1009
+ deny("hold too far ahead (anti-squat)");
1010
+ continue;
1011
+ }
1012
+ if (epochOf(ev.height) > E) {
1013
+ deny("expiry already in the past");
1014
+ continue;
1015
+ }
1016
+ if (claimHeld(target, ev.height)) {
1017
+ deny("offer already claimed (hold live)");
1018
+ continue;
1019
+ }
1020
+ if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1021
+ deny("claim cooldown (you just held this offer)");
1022
+ continue;
1023
+ }
1024
+ let liveN = 0;
1025
+ for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height)) liveN++;
1026
+ if (liveN >= MAX_ACTIVE_CLAIMS) {
1027
+ deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1028
+ continue;
1029
+ }
1030
+ target.claimedBy = who;
1031
+ target.claimUntilHeight = (E + 1) * EPOCH_LEN;
1032
+ target.claimTxid = ev.id;
1033
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1034
+ note(ev, ev.id, "fclaim", true);
958
1035
  }
959
1036
  } else {
960
- const o = offers.get(ev.proposalId);
961
1037
  const who = ev.attester.toLowerCase();
1038
+ let o = offers.get(ev.proposalId);
1039
+ if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
1040
+ const fc = fclaims.get(ev.proposalId);
1041
+ if (fc && fc.granted) {
1042
+ const linked = offers.get(fc.offer);
1043
+ if (linked && linked.claimTxid === ev.proposalId) o = linked;
1044
+ }
1045
+ }
962
1046
  if (!o) {
1047
+ if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
1048
+ note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
1049
+ continue;
1050
+ }
963
1051
  const b = bids.get(ev.proposalId);
964
1052
  if (b && ev.score === SCORE_CANCEL) {
965
1053
  if (who !== b.bidder) {
@@ -984,8 +1072,16 @@ function resolve(events, tipHeight) {
984
1072
  note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
985
1073
  continue;
986
1074
  }
1075
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
1076
+ note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
1077
+ continue;
1078
+ }
987
1079
  if (ev.height >= V14_HEIGHT) {
988
1080
  pendingCancels.push(() => {
1081
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
1082
+ note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
1083
+ return;
1084
+ }
989
1085
  if (o.status === "open") {
990
1086
  releaseGive(o);
991
1087
  o.status = "cancelled";
@@ -1049,7 +1145,7 @@ function resolve(events, tipHeight) {
1049
1145
  note(ev, ev.txid, "fill", false, "taker-bound offer");
1050
1146
  continue;
1051
1147
  }
1052
- const blk = openFillReject(o, ev.height, who);
1148
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
1053
1149
  if (blk) {
1054
1150
  note(ev, ev.txid, "fill", false, blk);
1055
1151
  continue;
@@ -1109,7 +1205,7 @@ function resolve(events, tipHeight) {
1109
1205
  note(ev, ev.txid, "fill", false, "taker-bound offer");
1110
1206
  continue;
1111
1207
  }
1112
- const blk = openFillReject(o, ev.height, who);
1208
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
1113
1209
  if (blk) {
1114
1210
  note(ev, ev.txid, "fill", false, blk);
1115
1211
  continue;
@@ -1155,6 +1251,10 @@ function resolve(events, tipHeight) {
1155
1251
  markBidDone(o, who);
1156
1252
  note(ev, ev.txid, "fill", true);
1157
1253
  } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
1254
+ if (ev.height >= V28_HEIGHT) {
1255
+ note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
1256
+ continue;
1257
+ }
1158
1258
  if (o.status !== "open") {
1159
1259
  note(ev, ev.txid, "claim", false, `offer ${o.status}`);
1160
1260
  continue;
@@ -1232,7 +1332,9 @@ function resolve(events, tipHeight) {
1232
1332
  for (const [id, b] of [...bids.entries()].sort(([a], [b2]) => ord(a, b2))) bidsOut[id] = b;
1233
1333
  const recapturesOut = {};
1234
1334
  for (const [nm, r] of [...recaptures.entries()].sort(([a], [b]) => ord(a, b))) recapturesOut[nm] = { owner: r.owner, effectiveHeight: r.effHeight, finalizeBy: r.finalizeBy };
1235
- return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, events: log, feesPaid: feesPaid.toString() };
1335
+ const fclaimsOut = {};
1336
+ for (const [txid, fc] of [...fclaims.entries()].sort(([a], [b]) => ord(a, b))) if (fc.granted) fclaimsOut[txid] = { offer: fc.offer, proposer: fc.proposer, expiresEpoch: fc.expiresEpoch, height: fc.height };
1337
+ return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: feesPaid.toString() };
1236
1338
  }
1237
1339
  function canonicalState(s) {
1238
1340
  const sortKeys = (v) => {
@@ -1242,7 +1344,7 @@ function canonicalState(s) {
1242
1344
  }
1243
1345
  return v;
1244
1346
  };
1245
- const { events: _events, recaptures: _recaptures, ...data } = s;
1347
+ const { events: _events, recaptures: _recaptures, fclaims: _fclaims, ...data } = s;
1246
1348
  return JSON.stringify(sortKeys(data));
1247
1349
  }
1248
1350
 
@@ -1322,9 +1424,13 @@ function hasLiveClaim(offer2, me, tip) {
1322
1424
  if (tip < V17_HEIGHT) return false;
1323
1425
  if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
1324
1426
  if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
1325
- const grace = claimGraceOf(offer2.claimUntilHeight);
1427
+ const grace = claimGraceOf(offer2.claimUntilHeight, offer2.claimTxid);
1326
1428
  return tip < offer2.claimUntilHeight + grace;
1327
1429
  }
1430
+ function fillTargetId(offer2, tip) {
1431
+ if (tip >= V28_HEIGHT && offer2.claimTxid !== void 0 && offer2.claimUntilHeight !== void 0 && tip < offer2.claimUntilHeight) return offer2.claimTxid;
1432
+ return offer2.id;
1433
+ }
1328
1434
  function fillIsSafe(offer2, me, pay, tip) {
1329
1435
  const preview = previewFill(offer2, pay);
1330
1436
  if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
@@ -1339,13 +1445,23 @@ function fillIsSafe(offer2, me, pay, tip) {
1339
1445
  }
1340
1446
  return { safe: true, reason: "ok", preview };
1341
1447
  }
1342
- function finalizeWinnerCheck(nameState, me, commitHeight) {
1448
+ function finalizeWinnerCheck(nameState, me, commitHeight, tip) {
1343
1449
  const m = me.toLowerCase();
1344
1450
  if (!nameState) return { safe: false, reason: "no reservation on-chain (displaced, swept, or never accepted) \u2014 a finalize now would burn the fee" };
1345
1451
  if (nameState.owner?.toLowerCase() !== m) return { safe: false, reason: "an earlier committer won this name \u2014 you were outbid" };
1346
1452
  if (nameState.pending !== true) return { safe: false, reason: "already registered to you \u2014 no second finalize fee is needed" };
1347
1453
  if (Number(nameState.effectiveHeight) !== Number(commitHeight))
1348
1454
  return { safe: false, reason: "your reservation was displaced (effective height changed) \u2014 a finalize now would burn the fee" };
1455
+ if (tip !== void 0 && tip !== null && Number.isFinite(Number(tip))) {
1456
+ const t = Number(tip);
1457
+ const eff = Number(nameState.effectiveHeight);
1458
+ const freezeEnd = eff + REG_COMMIT_MAX_BLOCKS;
1459
+ const closeAt = eff + REG_COMMIT_MAX_BLOCKS + REG_FINALIZE_GRACE_BLOCKS - FINALIZE_TIP_MARGIN;
1460
+ if (t <= freezeEnd + FINALIZE_TIP_MARGIN)
1461
+ return { safe: false, reason: `too early \u2014 the displacement contest is not frozen yet (finalizable after block ${freezeEnd + FINALIZE_TIP_MARGIN}, chain tip ${t}); the resolver would reject the finalize after the fee moved, burning it` };
1462
+ if (t > closeAt)
1463
+ return { safe: false, reason: `this reservation's finalize window has closed (safe until block ${closeAt}, chain tip ${t}); a finalize now would mine past the deadline and burn the fee` };
1464
+ }
1349
1465
  return { safe: true, reason: "ok" };
1350
1466
  }
1351
1467
 
@@ -1362,6 +1478,100 @@ function pickPrimaryName(names, a) {
1362
1478
  return best ? best.name : null;
1363
1479
  }
1364
1480
 
1481
+ // src/verifyfill.ts
1482
+ import { payloadHash as payloadHash2 } from "@inversealtruism/csd-codec";
1483
+ var GAP_NEEDED = EPOCH_LEN * (FCLAIM_MAX_EPOCH_AHEAD + 1) - 1 + CLAIM_COOLDOWN_BLOCKS;
1484
+ var SCAN_MARGIN = EPOCH_LEN;
1485
+ var MAX_SCAN = GAP_NEEDED + SCAN_MARGIN;
1486
+ function bindRecord(ev) {
1487
+ const rec = parseRecord(ev.uri, ev.payloadHash);
1488
+ if (!rec) return null;
1489
+ if (payloadHash2(rec).toLowerCase() !== String(ev.payloadHash).toLowerCase()) return null;
1490
+ return rec;
1491
+ }
1492
+ function replayLiveHold(proven, offerId, fclaimTxid, me, evalHeight) {
1493
+ const events = proven.map(({ depth: _depth, ...e }) => e);
1494
+ const state = resolve(events, evalHeight);
1495
+ const offer2 = state.offers[offerId.toLowerCase()] ?? state.offers[offerId];
1496
+ const granted = state.fclaims[fclaimTxid.toLowerCase()] !== void 0 || state.fclaims[fclaimTxid] !== void 0;
1497
+ const routed = !!offer2 && offer2.claimTxid !== void 0 && offer2.claimTxid.toLowerCase() === fclaimTxid.toLowerCase();
1498
+ const heldByMe = !!offer2 && offer2.status === "open" && hasLiveClaim(offer2, me, evalHeight);
1499
+ return { state, offer: offer2, granted, routed, heldByMe };
1500
+ }
1501
+ async function verifyFillSpv(offerId, fclaimTxid, me, io, opts) {
1502
+ const no = (reason) => ({ safe: false, reason });
1503
+ let tip;
1504
+ try {
1505
+ tip = Number(await io.tip());
1506
+ } catch {
1507
+ return no("could not read a PoW-verified tip");
1508
+ }
1509
+ if (!Number.isFinite(tip)) return no("no PoW-verified tip");
1510
+ const depthOf = (ev) => Math.min(Number(ev.depth), tip - Number(ev.height) + 1);
1511
+ let hintIds;
1512
+ try {
1513
+ hintIds = await io.offerEventIds(offerId, fclaimTxid);
1514
+ } catch {
1515
+ return no("could not enumerate the offer's events");
1516
+ }
1517
+ const want = /* @__PURE__ */ new Set([offerId.toLowerCase(), fclaimTxid.toLowerCase(), ...(hintIds ?? []).map((x) => String(x).toLowerCase())]);
1518
+ const proven = [];
1519
+ for (const wid of want) {
1520
+ let ev;
1521
+ try {
1522
+ ev = await io.provenEvent(wid);
1523
+ } catch {
1524
+ ev = null;
1525
+ }
1526
+ if (ev && (ev.kind === "propose" ? ev.id : ev.txid).toLowerCase() === wid) proven.push(ev);
1527
+ }
1528
+ const isProp = (e) => e.kind === "propose";
1529
+ const offerEv = proven.find((e) => isProp(e) && e.id.toLowerCase() === offerId.toLowerCase());
1530
+ const fclaimEv = proven.find((e) => isProp(e) && e.id.toLowerCase() === fclaimTxid.toLowerCase());
1531
+ if (!offerEv) return no("offer not merkle-proven (no PoW-verified offer Propose)");
1532
+ if (!fclaimEv) return no("fclaim not merkle-proven (no PoW-verified fclaim Propose)");
1533
+ if (fclaimEv.height < V28_HEIGHT) return no("fclaim mined below the V28 gate (no fclaim lane there)");
1534
+ const offerRec = bindRecord(offerEv);
1535
+ const fclaimRec = bindRecord(fclaimEv);
1536
+ if (!offerRec || offerRec.t !== "offer") return no("offer record does not bind to its on-chain commitment");
1537
+ if (!fclaimRec || fclaimRec.t !== "fclaim") return no("fclaim record does not bind to its on-chain commitment");
1538
+ if (fclaimRec.offer.toLowerCase() !== offerId.toLowerCase()) return no("fclaim does not reference this offer");
1539
+ if (isTokenWant(offerRec.want)) return no("the fclaim lane is CSD-priced offers only");
1540
+ const wantValue = BigInt(offerRec.want.value);
1541
+ const E = Number(fclaimEv.expiresEpoch);
1542
+ const need = requiredClaimDepth(wantValue, offerEv.height).depth;
1543
+ if (!(depthOf(offerEv) >= need)) return no(`offer not buried deep enough yet (${depthOf(offerEv)} < ${need}) - wait`);
1544
+ if (!(depthOf(fclaimEv) >= need)) return no(`fclaim not buried deep enough yet (${depthOf(fclaimEv)} < ${need}) - wait`);
1545
+ const laneIds = /* @__PURE__ */ new Set([offerId.toLowerCase()]);
1546
+ for (const e of proven) {
1547
+ if (!isProp(e)) continue;
1548
+ const r2 = e.id.toLowerCase() === fclaimTxid.toLowerCase() ? fclaimRec : bindRecord(e);
1549
+ if (r2 && r2.t === "fclaim" && r2.offer.toLowerCase() === offerId.toLowerCase()) laneIds.add(e.id.toLowerCase());
1550
+ }
1551
+ for (const e of proven) {
1552
+ if (e.kind === "attest" && e.score === SCORE_FILL && laneIds.has(e.proposalId.toLowerCase()) && !(depthOf(e) >= need))
1553
+ return no(`an earlier fill-basis event is not buried deep enough yet (${depthOf(e)} < ${need}) - wait`);
1554
+ }
1555
+ const r = replayLiveHold(proven, offerId, fclaimTxid, me, tip);
1556
+ if (!r.offer) return no("the offer does not resolve from the proven events (unknown/rejected offer)");
1557
+ if (!Number.isInteger(opts.myLiveHoldsAtGrant) || opts.myLiveHoldsAtGrant < 0)
1558
+ return no("the caller must assert myLiveHoldsAtGrant (the buyer's concurrent other-offer live-hold count) for the cap check");
1559
+ if (opts.myLiveHoldsAtGrant >= MAX_ACTIVE_CLAIMS) return no("you held the max active claims when this fclaim was granted - the resolver denied it on the cap, refusing (the payment would burn)");
1560
+ if (!(r.routed && r.granted)) return no("this fclaim is not the live granted hold (denied or superseded) - refusing, the payment would burn");
1561
+ if (!r.heldByMe) return no("no live claim by you on an open offer (cancelled, lapsed, or not your hold) - refusing");
1562
+ let pay;
1563
+ try {
1564
+ pay = opts.pay !== void 0 ? BigInt(opts.pay) : wantValue;
1565
+ } catch {
1566
+ return no("invalid pay amount");
1567
+ }
1568
+ const dp = previewFill({ ...r.offer, status: "open" }, pay);
1569
+ if (!dp.deliverable || dp.got < 1n) return no("this fill would deliver 0 units - refusing (the CSD would be lost)");
1570
+ const holdEnd = fclaimHoldEnd(E);
1571
+ if (holdEnd < tip + FILL_TIP_MARGIN) return no(`too close to the hold deadline (holdEnd ${holdEnd}, tip ${tip}) - would strand`);
1572
+ return { safe: true, reason: "ok" };
1573
+ }
1574
+
1365
1575
  // src/paidto.ts
1366
1576
  function paidToFromOutputs(outputs) {
1367
1577
  const m = /* @__PURE__ */ Object.create(null);
@@ -1375,7 +1585,7 @@ function paidToFromOutputs(outputs) {
1375
1585
  // src/index.ts
1376
1586
  import {
1377
1587
  canonicalJson as canonicalJson2,
1378
- payloadHash as payloadHash2,
1588
+ payloadHash as payloadHash3,
1379
1589
  blockReward as blockReward2,
1380
1590
  INITIAL_REWARD as INITIAL_REWARD2,
1381
1591
  HALVING_INTERVAL,
@@ -1401,10 +1611,14 @@ export {
1401
1611
  DEPLOY_KEYS,
1402
1612
  DOMAIN,
1403
1613
  EPOCH_LEN2 as EPOCH_LEN,
1614
+ FCLAIM_KEYS,
1615
+ FCLAIM_MAX_EPOCH_AHEAD,
1404
1616
  FEE_BPS,
1405
1617
  FEE_BPS_V16,
1406
1618
  FEE_GATE_MARGIN_BLOCKS,
1619
+ FILL_TIP_MARGIN,
1407
1620
  FINALIZE_TIP_MARGIN,
1621
+ GAP_NEEDED,
1408
1622
  HALVING_INTERVAL,
1409
1623
  HASH_RE,
1410
1624
  INITIAL_REWARD2 as INITIAL_REWARD,
@@ -1413,6 +1627,7 @@ export {
1413
1627
  MAX_OFFER_EPOCHS,
1414
1628
  MAX_PENDING_REG,
1415
1629
  MAX_RECORD_BYTES,
1630
+ MAX_SCAN,
1416
1631
  MINT_KEYS,
1417
1632
  MIN_FEE_ATTEST,
1418
1633
  MIN_FEE_PROPOSE,
@@ -1440,6 +1655,7 @@ export {
1440
1655
  REG_FINALIZE_GRACE_BLOCKS,
1441
1656
  RESERVED_NAMES,
1442
1657
  SALT_RE,
1658
+ SCAN_MARGIN,
1443
1659
  SCORE_CANCEL,
1444
1660
  SCORE_CLAIM,
1445
1661
  SCORE_FILL,
@@ -1463,6 +1679,7 @@ export {
1463
1679
  V25_HEIGHT,
1464
1680
  V26_HEIGHT,
1465
1681
  V27_HEIGHT,
1682
+ V28_HEIGHT,
1466
1683
  ZERO_ADDR,
1467
1684
  bid,
1468
1685
  blockReward2 as blockReward,
@@ -1476,7 +1693,11 @@ export {
1476
1693
  deploy,
1477
1694
  epochOf,
1478
1695
  expiredClaimFee,
1696
+ fclaim,
1697
+ fclaimEpochFor,
1698
+ fclaimHoldEnd,
1479
1699
  fillIsSafe,
1700
+ fillTargetId,
1480
1701
  finalizeWinnerCheck,
1481
1702
  hasLiveClaim,
1482
1703
  isName,
@@ -1500,14 +1721,16 @@ export {
1500
1721
  paidToFromOutputs,
1501
1722
  parseAmount,
1502
1723
  parseRecord,
1503
- payloadHash2 as payloadHash,
1724
+ payloadHash3 as payloadHash,
1504
1725
  pickPrimaryName,
1505
1726
  previewFill,
1506
1727
  primaryRankBefore,
1728
+ replayLiveHold,
1507
1729
  requiredClaimDepth,
1508
1730
  requiredFillOutputs,
1509
1731
  resolve,
1510
1732
  tokenMeta,
1511
1733
  tradeFee,
1512
- transfer
1734
+ transfer,
1735
+ verifyFillSpv
1513
1736
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/cairnx-core",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
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 && tsx test/primary.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 && tsx test/fclaim.test.ts && tsx test/fclaim-v28.test.ts && tsx test/verifyfill.test.ts"
49
49
  }
50
50
  }