@inversealtruism/cairnx-core 0.1.36 → 0.1.38

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
@@ -36,22 +36,27 @@ __export(index_exports, {
36
36
  DEPLOY_FEE: () => DEPLOY_FEE,
37
37
  DEPLOY_KEYS: () => DEPLOY_KEYS,
38
38
  DOMAIN: () => DOMAIN,
39
- EPOCH_LEN: () => import_csd_codec4.EPOCH_LEN,
39
+ EPOCH_LEN: () => import_csd_codec5.EPOCH_LEN,
40
+ FCLAIM_KEYS: () => FCLAIM_KEYS,
41
+ FCLAIM_MAX_EPOCH_AHEAD: () => FCLAIM_MAX_EPOCH_AHEAD,
40
42
  FEE_BPS: () => FEE_BPS,
41
43
  FEE_BPS_V16: () => FEE_BPS_V16,
42
44
  FEE_GATE_MARGIN_BLOCKS: () => FEE_GATE_MARGIN_BLOCKS,
45
+ FILL_TIP_MARGIN: () => FILL_TIP_MARGIN,
43
46
  FINALIZE_TIP_MARGIN: () => FINALIZE_TIP_MARGIN,
44
- HALVING_INTERVAL: () => import_csd_codec4.HALVING_INTERVAL,
47
+ GAP_NEEDED: () => GAP_NEEDED,
48
+ HALVING_INTERVAL: () => import_csd_codec5.HALVING_INTERVAL,
45
49
  HASH_RE: () => HASH_RE,
46
- INITIAL_REWARD: () => import_csd_codec4.INITIAL_REWARD,
50
+ INITIAL_REWARD: () => import_csd_codec5.INITIAL_REWARD,
47
51
  MAX_ACTIVE_CLAIMS: () => MAX_ACTIVE_CLAIMS,
48
52
  MAX_AMOUNT: () => MAX_AMOUNT,
49
53
  MAX_OFFER_EPOCHS: () => MAX_OFFER_EPOCHS,
50
54
  MAX_PENDING_REG: () => MAX_PENDING_REG,
51
55
  MAX_RECORD_BYTES: () => MAX_RECORD_BYTES,
56
+ MAX_SCAN: () => MAX_SCAN,
52
57
  MINT_KEYS: () => MINT_KEYS,
53
- MIN_FEE_ATTEST: () => import_csd_codec4.MIN_FEE_ATTEST,
54
- MIN_FEE_PROPOSE: () => import_csd_codec4.MIN_FEE_PROPOSE,
58
+ MIN_FEE_ATTEST: () => import_csd_codec5.MIN_FEE_ATTEST,
59
+ MIN_FEE_PROPOSE: () => import_csd_codec5.MIN_FEE_PROPOSE,
55
60
  NAME_FEE_LEN3_V24: () => NAME_FEE_LEN3_V24,
56
61
  NAME_FEE_LEN4_V24: () => NAME_FEE_LEN4_V24,
57
62
  NAME_FEE_LONG_V24: () => NAME_FEE_LONG_V24,
@@ -76,6 +81,7 @@ __export(index_exports, {
76
81
  REG_FINALIZE_GRACE_BLOCKS: () => REG_FINALIZE_GRACE_BLOCKS,
77
82
  RESERVED_NAMES: () => RESERVED_NAMES,
78
83
  SALT_RE: () => SALT_RE,
84
+ SCAN_MARGIN: () => SCAN_MARGIN,
79
85
  SCORE_CANCEL: () => SCORE_CANCEL,
80
86
  SCORE_CLAIM: () => SCORE_CLAIM,
81
87
  SCORE_FILL: () => SCORE_FILL,
@@ -99,12 +105,15 @@ __export(index_exports, {
99
105
  V25_HEIGHT: () => V25_HEIGHT,
100
106
  V26_HEIGHT: () => V26_HEIGHT,
101
107
  V27_HEIGHT: () => V27_HEIGHT,
108
+ V28_HEIGHT: () => V28_HEIGHT,
102
109
  ZERO_ADDR: () => ZERO_ADDR,
103
110
  bid: () => bid,
104
- blockReward: () => import_csd_codec4.blockReward,
111
+ bindOfferTerms: () => bindOfferTerms,
112
+ bindProvenOffer: () => bindProvenOffer,
113
+ blockReward: () => import_csd_codec5.blockReward,
105
114
  buildFeeHeight: () => buildFeeHeight,
106
115
  buildRecord: () => buildRecord,
107
- canonicalJson: () => import_csd_codec4.canonicalJson,
116
+ canonicalJson: () => import_csd_codec5.canonicalJson,
108
117
  canonicalState: () => canonicalState,
109
118
  claimGraceOf: () => claimGraceOf,
110
119
  claimWindowAt: () => claimWindowAt,
@@ -112,7 +121,12 @@ __export(index_exports, {
112
121
  deploy: () => deploy,
113
122
  epochOf: () => epochOf,
114
123
  expiredClaimFee: () => expiredClaimFee,
124
+ fclaim: () => fclaim,
125
+ fclaimEpochFor: () => fclaimEpochFor,
126
+ fclaimHoldEnd: () => fclaimHoldEnd,
127
+ feeBpsAt: () => feeBpsAt,
115
128
  fillIsSafe: () => fillIsSafe,
129
+ fillTargetId: () => fillTargetId,
116
130
  finalizeWinnerCheck: () => finalizeWinnerCheck,
117
131
  hasLiveClaim: () => hasLiveClaim,
118
132
  isName: () => isName,
@@ -136,16 +150,19 @@ __export(index_exports, {
136
150
  paidToFromOutputs: () => paidToFromOutputs,
137
151
  parseAmount: () => parseAmount,
138
152
  parseRecord: () => parseRecord,
139
- payloadHash: () => import_csd_codec4.payloadHash,
153
+ payloadHash: () => import_csd_codec5.payloadHash,
140
154
  pickPrimaryName: () => pickPrimaryName,
141
155
  previewFill: () => previewFill,
142
156
  primaryRankBefore: () => primaryRankBefore,
157
+ provenOfferTerms: () => provenOfferTerms,
158
+ replayLiveHold: () => replayLiveHold,
143
159
  requiredClaimDepth: () => requiredClaimDepth,
144
160
  requiredFillOutputs: () => requiredFillOutputs,
145
161
  resolve: () => resolve,
146
162
  tokenMeta: () => tokenMeta,
147
163
  tradeFee: () => tradeFee,
148
- transfer: () => transfer
164
+ transfer: () => transfer,
165
+ verifyFillSpv: () => verifyFillSpv
149
166
  });
150
167
  module.exports = __toCommonJS(index_exports);
151
168
 
@@ -181,6 +198,8 @@ var CLAIM_WINDOW_BLOCKS_V20 = 40;
181
198
  var CLAIM_FILL_GRACE_BLOCKS = 5;
182
199
  var MAX_ACTIVE_CLAIMS = 3;
183
200
  var CLAIM_COOLDOWN_BLOCKS = 15;
201
+ var FCLAIM_MAX_EPOCH_AHEAD = 2;
202
+ var FILL_TIP_MARGIN = 4;
184
203
  var CONF_TOKEN_FILL = 1e6;
185
204
  var TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
186
205
  var FEE_BPS = 100;
@@ -239,10 +258,13 @@ var MAX_PENDING_REG = 3;
239
258
  var FINALIZE_TIP_MARGIN = 2;
240
259
  var V26_HEIGHT = 46480;
241
260
  var V27_HEIGHT = 46520;
261
+ var V28_HEIGHT = 6e4;
242
262
  var epochOf = (height) => Math.floor(height / import_csd_codec.EPOCH_LEN);
243
263
  var claimWindowAt = (height) => height >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
244
264
  var claimWindowOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_WINDOW_BLOCKS_V20 : CLAIM_WINDOW_BLOCKS;
245
- var claimGraceOf = (claimUntilHeight) => claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_FILL_GRACE_BLOCKS : 0;
265
+ var claimGraceOf = (claimUntilHeight, claimTxid) => claimTxid !== void 0 ? 0 : claimUntilHeight - CLAIM_WINDOW_BLOCKS_V20 >= V20_HEIGHT ? CLAIM_FILL_GRACE_BLOCKS : 0;
266
+ var fclaimEpochFor = (tipHeight, offerExpiresEpoch) => Math.min(epochOf(tipHeight + CLAIM_WINDOW_BLOCKS_V20 + CLAIM_FILL_GRACE_BLOCKS), offerExpiresEpoch);
267
+ var fclaimHoldEnd = (expiresEpoch) => (expiresEpoch + 1) * import_csd_codec.EPOCH_LEN - 1;
246
268
  var offerExpiryHeightOf = (expiresEpoch, anchorHeight) => {
247
269
  const raw = (Number(expiresEpoch ?? 0) + 1) * import_csd_codec.EPOCH_LEN;
248
270
  if (anchorHeight >= V22_HEIGHT) return raw;
@@ -303,6 +325,7 @@ var BID_KEYS = /* @__PURE__ */ new Set(["v", "t", "want", "give", "memo", "ts"])
303
325
  var NAME_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
304
326
  var NFINALIZE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
305
327
  var NPROFILE_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "p"]);
328
+ var FCLAIM_KEYS = /* @__PURE__ */ new Set(["v", "t", "offer"]);
306
329
  function parseRecord(uri, payloadHashHex) {
307
330
  if (new TextEncoder().encode(uri).length > MAX_RECORD_BYTES) return null;
308
331
  let obj;
@@ -456,6 +479,11 @@ function parseRecord(uri, payloadHashHex) {
456
479
  if (Object.keys(r).length !== 4) return null;
457
480
  return r;
458
481
  }
482
+ case "fclaim": {
483
+ if (!onlyKeys(r, FCLAIM_KEYS)) return null;
484
+ if (!isHash(r.offer)) return null;
485
+ return r;
486
+ }
459
487
  default:
460
488
  return null;
461
489
  }
@@ -481,6 +509,7 @@ var nameSet = (r) => buildRecord({ v: 1, t: "nset", ...r });
481
509
  var nameRenew = (r) => buildRecord({ v: 1, t: "nrenew", ...r });
482
510
  var tokenMeta = (r) => buildRecord({ v: 1, t: "tmeta", ...r });
483
511
  var nameProfile = (r) => buildRecord({ v: 1, t: "nprofile", ...r });
512
+ var fclaim = (r) => buildRecord({ v: 1, t: "fclaim", ...r });
484
513
 
485
514
  // src/resolve.ts
486
515
  function ptAmt(v) {
@@ -502,6 +531,7 @@ function resolve(events, tipHeight) {
502
531
  const offers = /* @__PURE__ */ new Map();
503
532
  const offerLock = /* @__PURE__ */ new Map();
504
533
  const bids = /* @__PURE__ */ new Map();
534
+ const fclaims = /* @__PURE__ */ new Map();
505
535
  const log = [];
506
536
  let feesPaid = 0n;
507
537
  let pendingCancels = [];
@@ -565,18 +595,20 @@ function resolve(events, tipHeight) {
565
595
  const b = bids.get(o.bid);
566
596
  if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
567
597
  };
568
- const voidOpenNameOffers = (name) => {
598
+ const voidOpenNameOffers = (name, height) => {
569
599
  for (const o of offers.values()) if (o.status === "open" && isNameGive(o.give) && o.give.name === name) {
600
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, height)) continue;
570
601
  releaseGive(o);
571
602
  o.status = "cancelled";
572
603
  }
573
604
  };
574
605
  const earlierAnchor = (effHeight, pos, id, inc) => effHeight < inc.effHeight || effHeight === inc.effHeight && (pos < inc.pos || pos === inc.pos && id < inc.id);
575
- const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight) : 0;
606
+ const claimGrace = (o) => o.claimUntilHeight !== void 0 ? claimGraceOf(o.claimUntilHeight, o.claimTxid) : 0;
576
607
  const claimHeld = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight + claimGrace(o);
577
- const openFillReject = (o, height, who) => {
608
+ const openFillReject = (o, height, who, targetId) => {
578
609
  if (!(height >= V13_HEIGHT && !o.taker)) return void 0;
579
610
  if (height < V17_HEIGHT) return "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)";
611
+ if (height >= V28_HEIGHT && o.claimTxid !== void 0 && targetId === o.id) return "v2.8: fill the fclaim txid, not the offer";
580
612
  if (!(claimHeld(o, height) && who === o.claimedBy)) return "v1.7: open offer \u2014 claim it first (no live claim by you)";
581
613
  return void 0;
582
614
  };
@@ -741,7 +773,7 @@ function resolve(events, tipHeight) {
741
773
  note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
742
774
  continue;
743
775
  }
744
- voidOpenNameOffers(rec.name);
776
+ voidOpenNameOffers(rec.name, ev.height);
745
777
  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 });
746
778
  feesPaid += fee;
747
779
  note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
@@ -764,7 +796,7 @@ function resolve(events, tipHeight) {
764
796
  continue;
765
797
  }
766
798
  if (curActive) {
767
- voidOpenNameOffers(rec.name);
799
+ voidOpenNameOffers(rec.name, ev.height);
768
800
  }
769
801
  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 });
770
802
  note(ev, ev.id, "name", true, curActive ? "reserved (displaced prior reservation)" : "reserved (pending finalize)");
@@ -789,7 +821,7 @@ function resolve(events, tipHeight) {
789
821
  continue;
790
822
  }
791
823
  if (curActive) {
792
- voidOpenNameOffers(rec.name);
824
+ voidOpenNameOffers(rec.name, ev.height);
793
825
  }
794
826
  names.set(rec.name, cand);
795
827
  feesPaid += nameRegFee(rec.name, ev.height);
@@ -852,7 +884,7 @@ function resolve(events, tipHeight) {
852
884
  note(ev, ev.id, "nfinalize", false, "recapture premium unpaid (decaying)");
853
885
  continue;
854
886
  }
855
- voidOpenNameOffers(rec.name);
887
+ voidOpenNameOffers(rec.name, ev.height);
856
888
  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 });
857
889
  recaptures.delete(rec.name);
858
890
  feesPaid += fee;
@@ -1043,6 +1075,7 @@ function resolve(events, tipHeight) {
1043
1075
  const targets = [];
1044
1076
  for (const o of offers.values()) {
1045
1077
  if (o.status !== "open" || o.seller !== who) continue;
1078
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) continue;
1046
1079
  if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
1047
1080
  if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
1048
1081
  targets.push(o);
@@ -1050,10 +1083,13 @@ function resolve(events, tipHeight) {
1050
1083
  if (ev.height >= V14_HEIGHT) {
1051
1084
  pendingCancels.push(() => {
1052
1085
  let n = 0;
1053
- for (const o of targets) if (o.status === "open") {
1054
- releaseGive(o);
1055
- o.status = "cancelled";
1056
- n++;
1086
+ for (const o of targets) {
1087
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) continue;
1088
+ if (o.status === "open") {
1089
+ releaseGive(o);
1090
+ o.status = "cancelled";
1091
+ n++;
1092
+ }
1057
1093
  }
1058
1094
  note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
1059
1095
  });
@@ -1106,11 +1142,80 @@ function resolve(events, tipHeight) {
1106
1142
  }
1107
1143
  tok.meta.tmeta = rec.hash;
1108
1144
  note(ev, ev.id, "tmeta", true);
1145
+ } else if (rec.t === "fclaim") {
1146
+ if (ev.height < V28_HEIGHT) {
1147
+ note(ev, ev.id, "fclaim", false, "fclaim needs v2.8");
1148
+ continue;
1149
+ }
1150
+ const target = offers.get(rec.offer);
1151
+ const E = ev.expiresEpoch;
1152
+ const deny = (why) => {
1153
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: false });
1154
+ note(ev, ev.id, "fclaim", false, why);
1155
+ };
1156
+ if (!target) {
1157
+ deny("unknown offer");
1158
+ continue;
1159
+ }
1160
+ if (target.status !== "open") {
1161
+ deny(`offer ${target.status}`);
1162
+ continue;
1163
+ }
1164
+ if (target.taker) {
1165
+ deny("taker-bound offer needs no claim");
1166
+ continue;
1167
+ }
1168
+ if (isTokenWant(target.want)) {
1169
+ deny("claims are for CSD-priced offers");
1170
+ continue;
1171
+ }
1172
+ if (E > effExpiry(target, ev.height)) {
1173
+ deny("hold would outlive the offer expiry");
1174
+ continue;
1175
+ }
1176
+ if (E > epochOf(ev.height) + FCLAIM_MAX_EPOCH_AHEAD) {
1177
+ deny("hold too far ahead (anti-squat)");
1178
+ continue;
1179
+ }
1180
+ if (epochOf(ev.height) > E) {
1181
+ deny("expiry already in the past");
1182
+ continue;
1183
+ }
1184
+ if (claimHeld(target, ev.height)) {
1185
+ deny("offer already claimed (hold live)");
1186
+ continue;
1187
+ }
1188
+ if (target.claimedBy === who && target.claimUntilHeight !== void 0 && ev.height < target.claimUntilHeight + claimGrace(target) + CLAIM_COOLDOWN_BLOCKS) {
1189
+ deny("claim cooldown (you just held this offer)");
1190
+ continue;
1191
+ }
1192
+ let liveN = 0;
1193
+ for (const x of offers.values()) if (x.claimedBy === who && claimHeld(x, ev.height)) liveN++;
1194
+ if (liveN >= MAX_ACTIVE_CLAIMS) {
1195
+ deny(`max ${MAX_ACTIVE_CLAIMS} live claims per address`);
1196
+ continue;
1197
+ }
1198
+ target.claimedBy = who;
1199
+ target.claimUntilHeight = (E + 1) * import_csd_codec.EPOCH_LEN;
1200
+ target.claimTxid = ev.id;
1201
+ fclaims.set(ev.id, { offer: rec.offer, proposer: who, expiresEpoch: E, height: ev.height, granted: true });
1202
+ note(ev, ev.id, "fclaim", true);
1109
1203
  }
1110
1204
  } else {
1111
- const o = offers.get(ev.proposalId);
1112
1205
  const who = ev.attester.toLowerCase();
1206
+ let o = offers.get(ev.proposalId);
1207
+ if (!o && ev.height >= V28_HEIGHT && ev.score === SCORE_FILL) {
1208
+ const fc = fclaims.get(ev.proposalId);
1209
+ if (fc && fc.granted) {
1210
+ const linked = offers.get(fc.offer);
1211
+ if (linked && linked.claimTxid === ev.proposalId) o = linked;
1212
+ }
1213
+ }
1113
1214
  if (!o) {
1215
+ if (ev.height >= V28_HEIGHT && ev.score === SCORE_FILL && fclaims.has(ev.proposalId)) {
1216
+ note(ev, ev.txid, "fill", false, "fclaim not current (denied or superseded): no delivery, denied-fclaim fill");
1217
+ continue;
1218
+ }
1114
1219
  const b = bids.get(ev.proposalId);
1115
1220
  if (b && ev.score === SCORE_CANCEL) {
1116
1221
  if (who !== b.bidder) {
@@ -1135,8 +1240,16 @@ function resolve(events, tipHeight) {
1135
1240
  note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
1136
1241
  continue;
1137
1242
  }
1243
+ if (o.taker !== void 0 && o.height >= V28_HEIGHT) {
1244
+ note(ev, ev.txid, "cancel", false, "v2.8: taker-bound offers are firm quotes (uncancellable)");
1245
+ continue;
1246
+ }
1138
1247
  if (ev.height >= V14_HEIGHT) {
1139
1248
  pendingCancels.push(() => {
1249
+ if (ev.height >= V28_HEIGHT && o.claimTxid !== void 0 && claimHeld(o, ev.height)) {
1250
+ note(ev, ev.txid, "cancel", false, "v2.8: frozen (fclaim hold live)");
1251
+ return;
1252
+ }
1140
1253
  if (o.status === "open") {
1141
1254
  releaseGive(o);
1142
1255
  o.status = "cancelled";
@@ -1200,7 +1313,7 @@ function resolve(events, tipHeight) {
1200
1313
  note(ev, ev.txid, "fill", false, "taker-bound offer");
1201
1314
  continue;
1202
1315
  }
1203
- const blk = openFillReject(o, ev.height, who);
1316
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
1204
1317
  if (blk) {
1205
1318
  note(ev, ev.txid, "fill", false, blk);
1206
1319
  continue;
@@ -1260,7 +1373,7 @@ function resolve(events, tipHeight) {
1260
1373
  note(ev, ev.txid, "fill", false, "taker-bound offer");
1261
1374
  continue;
1262
1375
  }
1263
- const blk = openFillReject(o, ev.height, who);
1376
+ const blk = openFillReject(o, ev.height, who, ev.proposalId);
1264
1377
  if (blk) {
1265
1378
  note(ev, ev.txid, "fill", false, blk);
1266
1379
  continue;
@@ -1306,6 +1419,10 @@ function resolve(events, tipHeight) {
1306
1419
  markBidDone(o, who);
1307
1420
  note(ev, ev.txid, "fill", true);
1308
1421
  } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
1422
+ if (ev.height >= V28_HEIGHT) {
1423
+ note(ev, ev.txid, "claim", false, "v2.8: claims are fclaim proposals now");
1424
+ continue;
1425
+ }
1309
1426
  if (o.status !== "open") {
1310
1427
  note(ev, ev.txid, "claim", false, `offer ${o.status}`);
1311
1428
  continue;
@@ -1383,7 +1500,9 @@ function resolve(events, tipHeight) {
1383
1500
  for (const [id, b] of [...bids.entries()].sort(([a], [b2]) => ord(a, b2))) bidsOut[id] = b;
1384
1501
  const recapturesOut = {};
1385
1502
  for (const [nm, r] of [...recaptures.entries()].sort(([a], [b]) => ord(a, b))) recapturesOut[nm] = { owner: r.owner, effectiveHeight: r.effHeight, finalizeBy: r.finalizeBy };
1386
- return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, events: log, feesPaid: feesPaid.toString() };
1503
+ const fclaimsOut = {};
1504
+ 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 };
1505
+ return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, recaptures: recapturesOut, fclaims: fclaimsOut, events: log, feesPaid: feesPaid.toString() };
1387
1506
  }
1388
1507
  function canonicalState(s) {
1389
1508
  const sortKeys = (v) => {
@@ -1393,7 +1512,7 @@ function canonicalState(s) {
1393
1512
  }
1394
1513
  return v;
1395
1514
  };
1396
- const { events: _events, recaptures: _recaptures, ...data } = s;
1515
+ const { events: _events, recaptures: _recaptures, fclaims: _fclaims, ...data } = s;
1397
1516
  return JSON.stringify(sortKeys(data));
1398
1517
  }
1399
1518
 
@@ -1473,9 +1592,13 @@ function hasLiveClaim(offer2, me, tip) {
1473
1592
  if (tip < V17_HEIGHT) return false;
1474
1593
  if (offer2.claimedBy === void 0 || offer2.claimUntilHeight === void 0) return false;
1475
1594
  if (offer2.claimedBy.toLowerCase() !== me.toLowerCase()) return false;
1476
- const grace = claimGraceOf(offer2.claimUntilHeight);
1595
+ const grace = claimGraceOf(offer2.claimUntilHeight, offer2.claimTxid);
1477
1596
  return tip < offer2.claimUntilHeight + grace;
1478
1597
  }
1598
+ function fillTargetId(offer2, tip) {
1599
+ if (tip >= V28_HEIGHT && offer2.claimTxid !== void 0 && offer2.claimUntilHeight !== void 0 && tip < offer2.claimUntilHeight) return offer2.claimTxid;
1600
+ return offer2.id;
1601
+ }
1479
1602
  function fillIsSafe(offer2, me, pay, tip) {
1480
1603
  const preview = previewFill(offer2, pay);
1481
1604
  if (offer2.status !== "open") return { safe: false, reason: `offer is ${offer2.status}`, preview };
@@ -1523,6 +1646,133 @@ function pickPrimaryName(names, a) {
1523
1646
  return best ? best.name : null;
1524
1647
  }
1525
1648
 
1649
+ // src/verifyfill.ts
1650
+ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1651
+ var GAP_NEEDED = import_csd_codec.EPOCH_LEN * (FCLAIM_MAX_EPOCH_AHEAD + 1) - 1 + CLAIM_COOLDOWN_BLOCKS;
1652
+ var SCAN_MARGIN = import_csd_codec.EPOCH_LEN;
1653
+ var MAX_SCAN = GAP_NEEDED + SCAN_MARGIN;
1654
+ function bindRecord(ev) {
1655
+ const rec = parseRecord(ev.uri, ev.payloadHash);
1656
+ if (!rec) return null;
1657
+ if ((0, import_csd_codec4.payloadHash)(rec).toLowerCase() !== String(ev.payloadHash).toLowerCase()) return null;
1658
+ return rec;
1659
+ }
1660
+ function replayLiveHold(proven, offerId, fclaimTxid, me, evalHeight) {
1661
+ const events = proven.map(({ depth: _depth, ...e }) => e);
1662
+ const state = resolve(events, evalHeight);
1663
+ const offer2 = state.offers[offerId.toLowerCase()] ?? state.offers[offerId];
1664
+ const granted = state.fclaims[fclaimTxid.toLowerCase()] !== void 0 || state.fclaims[fclaimTxid] !== void 0;
1665
+ const routed = !!offer2 && offer2.claimTxid !== void 0 && offer2.claimTxid.toLowerCase() === fclaimTxid.toLowerCase();
1666
+ const heldByMe = !!offer2 && offer2.status === "open" && hasLiveClaim(offer2, me, evalHeight);
1667
+ return { state, offer: offer2, granted, routed, heldByMe };
1668
+ }
1669
+ async function verifyFillSpv(offerId, fclaimTxid, me, io, opts) {
1670
+ const no = (reason) => ({ safe: false, reason });
1671
+ let tip;
1672
+ try {
1673
+ tip = Number(await io.tip());
1674
+ } catch {
1675
+ return no("could not read a PoW-verified tip");
1676
+ }
1677
+ if (!Number.isFinite(tip)) return no("no PoW-verified tip");
1678
+ const depthOf = (ev) => Math.min(Number(ev.depth), tip - Number(ev.height) + 1);
1679
+ let hintIds;
1680
+ try {
1681
+ hintIds = await io.offerEventIds(offerId, fclaimTxid);
1682
+ } catch {
1683
+ return no("could not enumerate the offer's events");
1684
+ }
1685
+ const want = /* @__PURE__ */ new Set([offerId.toLowerCase(), fclaimTxid.toLowerCase(), ...(hintIds ?? []).map((x) => String(x).toLowerCase())]);
1686
+ const proven = [];
1687
+ for (const wid of want) {
1688
+ let ev;
1689
+ try {
1690
+ ev = await io.provenEvent(wid);
1691
+ } catch {
1692
+ ev = null;
1693
+ }
1694
+ if (ev && (ev.kind === "propose" ? ev.id : ev.txid).toLowerCase() === wid) proven.push(ev);
1695
+ }
1696
+ const isProp = (e) => e.kind === "propose";
1697
+ const offerEv = proven.find((e) => isProp(e) && e.id.toLowerCase() === offerId.toLowerCase());
1698
+ const fclaimEv = proven.find((e) => isProp(e) && e.id.toLowerCase() === fclaimTxid.toLowerCase());
1699
+ if (!offerEv) return no("offer not merkle-proven (no PoW-verified offer Propose)");
1700
+ if (!fclaimEv) return no("fclaim not merkle-proven (no PoW-verified fclaim Propose)");
1701
+ if (fclaimEv.height < V28_HEIGHT) return no("fclaim mined below the V28 gate (no fclaim lane there)");
1702
+ const offerRec = bindRecord(offerEv);
1703
+ const fclaimRec = bindRecord(fclaimEv);
1704
+ if (!offerRec || offerRec.t !== "offer") return no("offer record does not bind to its on-chain commitment");
1705
+ if (!fclaimRec || fclaimRec.t !== "fclaim") return no("fclaim record does not bind to its on-chain commitment");
1706
+ if (fclaimRec.offer.toLowerCase() !== offerId.toLowerCase()) return no("fclaim does not reference this offer");
1707
+ if (isTokenWant(offerRec.want)) return no("the fclaim lane is CSD-priced offers only");
1708
+ const wantValue = BigInt(offerRec.want.value);
1709
+ const E = Number(fclaimEv.expiresEpoch);
1710
+ const need = requiredClaimDepth(wantValue, offerEv.height).depth;
1711
+ if (!(depthOf(offerEv) >= need)) return no(`offer not buried deep enough yet (${depthOf(offerEv)} < ${need}) - wait`);
1712
+ if (!(depthOf(fclaimEv) >= need)) return no(`fclaim not buried deep enough yet (${depthOf(fclaimEv)} < ${need}) - wait`);
1713
+ const laneIds = /* @__PURE__ */ new Set([offerId.toLowerCase()]);
1714
+ for (const e of proven) {
1715
+ if (!isProp(e)) continue;
1716
+ const r2 = e.id.toLowerCase() === fclaimTxid.toLowerCase() ? fclaimRec : bindRecord(e);
1717
+ if (r2 && r2.t === "fclaim" && r2.offer.toLowerCase() === offerId.toLowerCase()) laneIds.add(e.id.toLowerCase());
1718
+ }
1719
+ for (const e of proven) {
1720
+ if (e.kind === "attest" && e.score === SCORE_FILL && laneIds.has(e.proposalId.toLowerCase()) && !(depthOf(e) >= need))
1721
+ return no(`an earlier fill-basis event is not buried deep enough yet (${depthOf(e)} < ${need}) - wait`);
1722
+ }
1723
+ const r = replayLiveHold(proven, offerId, fclaimTxid, me, tip);
1724
+ if (!r.offer) return no("the offer does not resolve from the proven events (unknown/rejected offer)");
1725
+ if (!Number.isInteger(opts.myLiveHoldsAtGrant) || opts.myLiveHoldsAtGrant < 0)
1726
+ return no("the caller must assert myLiveHoldsAtGrant (the buyer's concurrent other-offer live-hold count) for the cap check");
1727
+ 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)");
1728
+ if (!(r.routed && r.granted)) return no("this fclaim is not the live granted hold (denied or superseded) - refusing, the payment would burn");
1729
+ if (!r.heldByMe) return no("no live claim by you on an open offer (cancelled, lapsed, or not your hold) - refusing");
1730
+ let pay;
1731
+ try {
1732
+ pay = opts.pay !== void 0 ? BigInt(opts.pay) : wantValue;
1733
+ } catch {
1734
+ return no("invalid pay amount");
1735
+ }
1736
+ const dp = previewFill({ ...r.offer, status: "open" }, pay);
1737
+ if (!dp.deliverable || dp.got < 1n) return no("this fill would deliver 0 units - refusing (the CSD would be lost)");
1738
+ const holdEnd = fclaimHoldEnd(E);
1739
+ if (holdEnd < tip + FILL_TIP_MARGIN) return no(`too close to the hold deadline (holdEnd ${holdEnd}, tip ${tip}) - would strand`);
1740
+ return { safe: true, reason: "ok" };
1741
+ }
1742
+ var feeBpsAt = (height) => height >= V11_HEIGHT ? height >= V16_HEIGHT ? FEE_BPS_V16 : FEE_BPS : 0;
1743
+ function provenOfferTerms(offerRec, provenHeight) {
1744
+ const w = offerRec.want;
1745
+ return {
1746
+ height: Number(provenHeight),
1747
+ feeBps: feeBpsAt(Number(provenHeight)),
1748
+ value: w.value !== void 0 ? String(w.value) : void 0,
1749
+ taker: offerRec.taker !== void 0 ? String(offerRec.taker).toLowerCase() : void 0,
1750
+ bid: offerRec.bid !== void 0 ? String(offerRec.bid).toLowerCase() : void 0,
1751
+ min: offerRec.min !== void 0 ? String(offerRec.min) : void 0
1752
+ };
1753
+ }
1754
+ function bindOfferTerms(servedOffer, t) {
1755
+ const o = servedOffer;
1756
+ const s = (v) => v === void 0 || v === null ? "" : String(v).toLowerCase();
1757
+ if (Number(o?.height) !== t.height) return true;
1758
+ if (Number(o?.feeBps) !== t.feeBps) return true;
1759
+ if (t.value !== void 0 && String(o?.want?.value) !== t.value) return true;
1760
+ if (s(o?.taker) !== s(t.taker)) return true;
1761
+ if (s(o?.bid) !== s(t.bid)) return true;
1762
+ const om = o?.min;
1763
+ if ((om !== void 0 && om !== null) !== (t.min !== void 0)) return true;
1764
+ if (t.min !== void 0 && String(om) !== t.min) return true;
1765
+ return false;
1766
+ }
1767
+ function bindProvenOffer(offerEv) {
1768
+ const rec = bindRecord(offerEv);
1769
+ if (!rec || rec.t !== "offer") return null;
1770
+ const seller = String(offerEv.proposer).toLowerCase();
1771
+ const w = rec.want;
1772
+ const payto = w.payto && ADDR_RE.test(String(w.payto).toLowerCase()) ? String(w.payto).toLowerCase() : seller;
1773
+ return { payto, seller, terms: provenOfferTerms(rec, offerEv.height) };
1774
+ }
1775
+
1526
1776
  // src/paidto.ts
1527
1777
  function paidToFromOutputs(outputs) {
1528
1778
  const m = /* @__PURE__ */ Object.create(null);
@@ -1534,7 +1784,7 @@ function paidToFromOutputs(outputs) {
1534
1784
  }
1535
1785
 
1536
1786
  // src/index.ts
1537
- var import_csd_codec4 = require("@inversealtruism/csd-codec");
1787
+ var import_csd_codec5 = require("@inversealtruism/csd-codec");
1538
1788
  // Annotate the CommonJS export names for ESM import in node:
1539
1789
  0 && (module.exports = {
1540
1790
  ACTIVATION_HEIGHT,
@@ -1554,10 +1804,14 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1554
1804
  DEPLOY_KEYS,
1555
1805
  DOMAIN,
1556
1806
  EPOCH_LEN,
1807
+ FCLAIM_KEYS,
1808
+ FCLAIM_MAX_EPOCH_AHEAD,
1557
1809
  FEE_BPS,
1558
1810
  FEE_BPS_V16,
1559
1811
  FEE_GATE_MARGIN_BLOCKS,
1812
+ FILL_TIP_MARGIN,
1560
1813
  FINALIZE_TIP_MARGIN,
1814
+ GAP_NEEDED,
1561
1815
  HALVING_INTERVAL,
1562
1816
  HASH_RE,
1563
1817
  INITIAL_REWARD,
@@ -1566,6 +1820,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1566
1820
  MAX_OFFER_EPOCHS,
1567
1821
  MAX_PENDING_REG,
1568
1822
  MAX_RECORD_BYTES,
1823
+ MAX_SCAN,
1569
1824
  MINT_KEYS,
1570
1825
  MIN_FEE_ATTEST,
1571
1826
  MIN_FEE_PROPOSE,
@@ -1593,6 +1848,7 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1593
1848
  REG_FINALIZE_GRACE_BLOCKS,
1594
1849
  RESERVED_NAMES,
1595
1850
  SALT_RE,
1851
+ SCAN_MARGIN,
1596
1852
  SCORE_CANCEL,
1597
1853
  SCORE_CLAIM,
1598
1854
  SCORE_FILL,
@@ -1616,8 +1872,11 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1616
1872
  V25_HEIGHT,
1617
1873
  V26_HEIGHT,
1618
1874
  V27_HEIGHT,
1875
+ V28_HEIGHT,
1619
1876
  ZERO_ADDR,
1620
1877
  bid,
1878
+ bindOfferTerms,
1879
+ bindProvenOffer,
1621
1880
  blockReward,
1622
1881
  buildFeeHeight,
1623
1882
  buildRecord,
@@ -1629,7 +1888,12 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1629
1888
  deploy,
1630
1889
  epochOf,
1631
1890
  expiredClaimFee,
1891
+ fclaim,
1892
+ fclaimEpochFor,
1893
+ fclaimHoldEnd,
1894
+ feeBpsAt,
1632
1895
  fillIsSafe,
1896
+ fillTargetId,
1633
1897
  finalizeWinnerCheck,
1634
1898
  hasLiveClaim,
1635
1899
  isName,
@@ -1657,10 +1921,13 @@ var import_csd_codec4 = require("@inversealtruism/csd-codec");
1657
1921
  pickPrimaryName,
1658
1922
  previewFill,
1659
1923
  primaryRankBefore,
1924
+ provenOfferTerms,
1925
+ replayLiveHold,
1660
1926
  requiredClaimDepth,
1661
1927
  requiredFillOutputs,
1662
1928
  resolve,
1663
1929
  tokenMeta,
1664
1930
  tradeFee,
1665
- transfer
1931
+ transfer,
1932
+ verifyFillSpv
1666
1933
  });