@inversealtruism/cairnx-core 0.1.8 → 0.1.11

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
@@ -6,13 +6,15 @@ var V12_HEIGHT = 30300;
6
6
  var V13_HEIGHT = 31100;
7
7
  var V14_HEIGHT = 31400;
8
8
  var V15_HEIGHT = 32e3;
9
+ var V16_HEIGHT = 33600;
10
+ var V17_HEIGHT = 34e3;
9
11
  var EPOCH_LEN = 30;
10
12
  var NAME_TERM_EPOCHS = 8760;
11
13
  var NAME_GRACE_EPOCHS = 720;
12
14
  var NAME_PREMIUM_START = 20n;
13
15
  var NAME_PREMIUM_DECAY_EPOCHS = 720;
14
- function expiredClaimFee(name, epochsPastGraceEnd) {
15
- const base = nameRegFee(name);
16
+ function expiredClaimFee(name, epochsPastGraceEnd, height) {
17
+ const base = nameRegFee(name, height);
16
18
  if (epochsPastGraceEnd >= NAME_PREMIUM_DECAY_EPOCHS) return base;
17
19
  const left = BigInt(NAME_PREMIUM_DECAY_EPOCHS - epochsPastGraceEnd);
18
20
  const mult = 1n + (NAME_PREMIUM_START - 1n) * left / BigInt(NAME_PREMIUM_DECAY_EPOCHS);
@@ -22,11 +24,22 @@ var MAX_RECORD_BYTES = 512;
22
24
  var MAX_AMOUNT = (1n << 96n) - 1n;
23
25
  var SCORE_FILL = 100;
24
26
  var SCORE_CANCEL = 0;
27
+ var SCORE_CLAIM = 50;
28
+ var CLAIM_WINDOW_BLOCKS = 15;
29
+ var MAX_ACTIVE_CLAIMS = 3;
30
+ var CLAIM_COOLDOWN_BLOCKS = 15;
25
31
  var CONF_TOKEN_FILL = 1e6;
26
32
  var TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
27
33
  var FEE_BPS = 100;
34
+ var FEE_BPS_V16 = 150;
35
+ var REBATE_FLAT = 25000000n;
36
+ var REBATE_BPS = 50;
28
37
  var DEPLOY_FEE = 1e8;
29
- function nameRegFee(name) {
38
+ var V18_HEIGHT = 4e4;
39
+ var NAME_FEE_SHORT_V18 = 670000000n;
40
+ var NAME_FEE_V18 = 300000000n;
41
+ function nameRegFee(name, height) {
42
+ if (height >= V18_HEIGHT) return name.length <= 4 ? NAME_FEE_SHORT_V18 : NAME_FEE_V18;
30
43
  const n = name.length;
31
44
  if (n <= 3) return 500000000n;
32
45
  if (n === 4) return 200000000n;
@@ -34,7 +47,8 @@ function nameRegFee(name) {
34
47
  if (n <= 9) return 50000000n;
35
48
  return 10000000n;
36
49
  }
37
- var tradeFee = (want) => (want * BigInt(FEE_BPS) + 9999n) / 10000n;
50
+ var tradeFee = (want, bps = FEE_BPS) => (want * BigInt(bps) + 9999n) / 10000n;
51
+ var makerRebate = (value) => REBATE_FLAT + (value * BigInt(REBATE_BPS) + 9999n) / 10000n;
38
52
  var TICKER_RE = /^[A-Z][A-Z0-9]{2,11}$/;
39
53
  var ADDR_RE = /^0x[0-9a-f]{40}$/;
40
54
  var AMOUNT_RE = /^(0|[1-9][0-9]*)$/;
@@ -321,6 +335,7 @@ function resolve(events, tipHeight) {
321
335
  const b = bids.get(o.bid);
322
336
  if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
323
337
  };
338
+ const liveClaim = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight;
324
339
  for (const ev of ordered) {
325
340
  if (ev.height < ACTIVATION_HEIGHT) continue;
326
341
  if (ev.height !== pendingBlock) {
@@ -331,6 +346,7 @@ function resolve(events, tipHeight) {
331
346
  const v11 = ev.height >= V11_HEIGHT;
332
347
  const v12 = ev.height >= V12_HEIGHT;
333
348
  const v15 = ev.height >= V15_HEIGHT;
349
+ const v16 = ev.height >= V16_HEIGHT;
334
350
  const feeToTreasury = ev.kind === "propose" ? BigInt((ev.paidTo ?? {})[TREASURY_ADDR] ?? "0") : 0n;
335
351
  if (ev.kind === "propose") {
336
352
  const rec = parseRecord(ev.uri, ev.payloadHash);
@@ -429,7 +445,7 @@ function resolve(events, tipHeight) {
429
445
  const cur = names.get(rec.name);
430
446
  const epClaim = epochOf(ev.height);
431
447
  if (cur && v15 && lapsed(cur, epClaim)) {
432
- const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(cur) + NAME_GRACE_EPOCHS));
448
+ const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(cur) + NAME_GRACE_EPOCHS), ev.height);
433
449
  if (feeToTreasury < fee) {
434
450
  note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
435
451
  continue;
@@ -443,7 +459,7 @@ function resolve(events, tipHeight) {
443
459
  note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
444
460
  continue;
445
461
  }
446
- if (feeToTreasury < nameRegFee(rec.name)) {
462
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
447
463
  note(ev, ev.id, "name", false, "name registration fee unpaid");
448
464
  continue;
449
465
  }
@@ -468,7 +484,7 @@ function resolve(events, tipHeight) {
468
484
  }
469
485
  }
470
486
  names.set(rec.name, cand);
471
- feesPaid += nameRegFee(rec.name);
487
+ feesPaid += nameRegFee(rec.name, ev.height);
472
488
  note(ev, ev.id, "name", true, cur ? "displaced prior holder" : void 0);
473
489
  } else if (rec.t === "nxfer") {
474
490
  const n = names.get(rec.name);
@@ -505,7 +521,7 @@ function resolve(events, tipHeight) {
505
521
  note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
506
522
  continue;
507
523
  }
508
- if (ev.height >= V13_HEIGHT && !wantIsToken && rec.taker === void 0) {
524
+ if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
509
525
  note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
510
526
  continue;
511
527
  }
@@ -572,7 +588,7 @@ function resolve(events, tipHeight) {
572
588
  status: "open",
573
589
  expiresEpoch: ev.expiresEpoch,
574
590
  height: ev.height,
575
- feeBps: v11 ? FEE_BPS : 0
591
+ feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
576
592
  };
577
593
  offers.set(ev.id, o);
578
594
  const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
@@ -646,12 +662,12 @@ function resolve(events, tipHeight) {
646
662
  note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
647
663
  continue;
648
664
  }
649
- if (feeToTreasury < nameRegFee(rec.name)) {
665
+ if (feeToTreasury < nameRegFee(rec.name, ev.height)) {
650
666
  note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
651
667
  continue;
652
668
  }
653
669
  n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
654
- feesPaid += nameRegFee(rec.name);
670
+ feesPaid += nameRegFee(rec.name, ev.height);
655
671
  note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
656
672
  } else if (rec.t === "tmeta") {
657
673
  if (!v15) {
@@ -729,7 +745,7 @@ function resolve(events, tipHeight) {
729
745
  continue;
730
746
  }
731
747
  const amt = BigInt(o.want.amount);
732
- const fee = o.feeBps ? tradeFee(amt) : 0n;
748
+ const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
733
749
  const buyer = bal(o.want.ticker, who);
734
750
  if (buyer.available < amt + fee) {
735
751
  note(ev, ev.txid, "fill", false, "insufficient want-token balance");
@@ -778,8 +794,14 @@ function resolve(events, tipHeight) {
778
794
  continue;
779
795
  }
780
796
  if (ev.height >= V13_HEIGHT && !o.taker) {
781
- note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
782
- continue;
797
+ if (ev.height < V17_HEIGHT) {
798
+ note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
799
+ continue;
800
+ }
801
+ if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
802
+ note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
803
+ continue;
804
+ }
783
805
  }
784
806
  const pt = ev.paidTo ?? {};
785
807
  const want = BigInt(o.want.value);
@@ -793,7 +815,7 @@ function resolve(events, tipHeight) {
793
815
  continue;
794
816
  }
795
817
  const x = X < remaining ? X : remaining;
796
- const fee = o.feeBps ? tradeFee(x) : 0n;
818
+ const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
797
819
  if (BigInt(pt[TREASURY_ADDR] ?? "0") < fee) {
798
820
  note(ev, ev.txid, "fill", false, "protocol fee unpaid");
799
821
  continue;
@@ -837,22 +859,35 @@ function resolve(events, tipHeight) {
837
859
  continue;
838
860
  }
839
861
  if (ev.height >= V13_HEIGHT && !o.taker) {
840
- note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
841
- continue;
862
+ if (ev.height < V17_HEIGHT) {
863
+ note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
864
+ continue;
865
+ }
866
+ if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
867
+ note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
868
+ continue;
869
+ }
842
870
  }
843
871
  const pt = ev.paidTo ?? {};
844
872
  const want = BigInt(o.want.value);
845
- const paid = BigInt(pt[o.want.payto] ?? "0");
846
- if (paid < want) {
847
- note(ev, ev.txid, "fill", false, "payment below want.value");
848
- continue;
849
- }
850
- const fee = o.feeBps ? tradeFee(want) : 0n;
851
- const feeGot = BigInt(pt[TREASURY_ADDR] ?? "0");
852
- if (feeGot < fee) {
853
- note(ev, ev.txid, "fill", false, "protocol fee unpaid");
873
+ const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
874
+ const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
875
+ const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
876
+ const need = /* @__PURE__ */ new Map();
877
+ const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
878
+ addNeed(o.want.payto, want);
879
+ addNeed(TREASURY_ADDR, fee);
880
+ if (rebate > 0n) addNeed(o.seller, rebate);
881
+ let unpaid;
882
+ for (const [addr, amt] of need) if (BigInt(pt[addr] ?? "0") < amt) {
883
+ unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
884
+ break;
885
+ }
886
+ if (unpaid) {
887
+ note(ev, ev.txid, "fill", false, unpaid);
854
888
  continue;
855
889
  }
890
+ const paid = BigInt(pt[o.want.payto] ?? "0");
856
891
  if (isNameGive(o.give)) {
857
892
  const n = names.get(o.give.name);
858
893
  if (!n) {
@@ -884,6 +919,36 @@ function resolve(events, tipHeight) {
884
919
  o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
885
920
  markBidDone(o, who);
886
921
  note(ev, ev.txid, "fill", true);
922
+ } else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
923
+ if (o.status !== "open") {
924
+ note(ev, ev.txid, "claim", false, `offer ${o.status}`);
925
+ continue;
926
+ }
927
+ if (o.taker) {
928
+ note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
929
+ continue;
930
+ }
931
+ if (isTokenWant(o.want)) {
932
+ note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
933
+ continue;
934
+ }
935
+ if (liveClaim(o, ev.height)) {
936
+ note(ev, ev.txid, "claim", false, "offer already claimed (window live)");
937
+ continue;
938
+ }
939
+ if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + CLAIM_COOLDOWN_BLOCKS) {
940
+ note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
941
+ continue;
942
+ }
943
+ let liveN = 0;
944
+ for (const x of offers.values()) if (x.claimedBy === who && x.claimUntilHeight !== void 0 && ev.height < x.claimUntilHeight) liveN++;
945
+ if (liveN >= MAX_ACTIVE_CLAIMS) {
946
+ note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
947
+ continue;
948
+ }
949
+ o.claimedBy = who;
950
+ o.claimUntilHeight = ev.height + CLAIM_WINDOW_BLOCKS;
951
+ note(ev, ev.txid, "claim", true);
887
952
  }
888
953
  }
889
954
  }
@@ -938,22 +1003,31 @@ export {
938
1003
  ACTIVATION_HEIGHT,
939
1004
  ADDR_RE,
940
1005
  AMOUNT_RE,
1006
+ CLAIM_COOLDOWN_BLOCKS,
1007
+ CLAIM_WINDOW_BLOCKS,
941
1008
  COMMIT_MAX_BLOCKS,
942
1009
  CONF_TOKEN_FILL,
943
1010
  DEPLOY_FEE,
944
1011
  DOMAIN,
945
1012
  EPOCH_LEN,
946
1013
  FEE_BPS,
1014
+ FEE_BPS_V16,
947
1015
  HASH_RE,
1016
+ MAX_ACTIVE_CLAIMS,
948
1017
  MAX_AMOUNT,
949
1018
  MAX_RECORD_BYTES,
1019
+ NAME_FEE_SHORT_V18,
1020
+ NAME_FEE_V18,
950
1021
  NAME_GRACE_EPOCHS,
951
1022
  NAME_PREMIUM_DECAY_EPOCHS,
952
1023
  NAME_PREMIUM_START,
953
1024
  NAME_RE,
954
1025
  NAME_TERM_EPOCHS,
1026
+ REBATE_BPS,
1027
+ REBATE_FLAT,
955
1028
  RESERVED_NAMES,
956
1029
  SCORE_CANCEL,
1030
+ SCORE_CLAIM,
957
1031
  SCORE_FILL,
958
1032
  TICKER_RE,
959
1033
  TREASURY_ADDR,
@@ -962,6 +1036,9 @@ export {
962
1036
  V13_HEIGHT,
963
1037
  V14_HEIGHT,
964
1038
  V15_HEIGHT,
1039
+ V16_HEIGHT,
1040
+ V17_HEIGHT,
1041
+ V18_HEIGHT,
965
1042
  bid,
966
1043
  buildRecord,
967
1044
  canonicalState,
@@ -971,6 +1048,7 @@ export {
971
1048
  isName,
972
1049
  isNameGive,
973
1050
  isTokenWant,
1051
+ makerRebate,
974
1052
  mint,
975
1053
  nameClaim,
976
1054
  nameCommit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/cairnx-core",
3
- "version": "0.1.8",
3
+ "version": "0.1.11",
4
4
  "description": "CairnX — tokens, atomic DvP trades and leased .csd names on Compute Substrate, as a pure deterministic convention library (Ordinals/Runes class). Ships the normative CONVENTION.md and the conformance vectors.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -18,10 +18,11 @@
18
18
  "LICENSE",
19
19
  "README.md",
20
20
  "CONVENTION.md",
21
+ "MAKERS.md",
21
22
  "test/vectors"
22
23
  ],
23
24
  "dependencies": {
24
- "@inversealtruism/csd-codec": "0.1.8"
25
+ "@inversealtruism/csd-codec": "0.1.11"
25
26
  },
26
27
  "license": "MIT",
27
28
  "sideEffects": false,