@inversealtruism/cairnx-core 0.1.8 → 0.1.9
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/MAKERS.md +143 -0
- package/dist/index.cjs +99 -18
- package/dist/index.d.cts +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +89 -18
- package/package.json +9 -8
- package/test/vectors/cases.json +4080 -2818
package/dist/index.js
CHANGED
|
@@ -6,6 +6,8 @@ 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;
|
|
@@ -22,9 +24,16 @@ 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
38
|
function nameRegFee(name) {
|
|
30
39
|
const n = name.length;
|
|
@@ -34,7 +43,8 @@ function nameRegFee(name) {
|
|
|
34
43
|
if (n <= 9) return 50000000n;
|
|
35
44
|
return 10000000n;
|
|
36
45
|
}
|
|
37
|
-
var tradeFee = (want) => (want * BigInt(
|
|
46
|
+
var tradeFee = (want, bps = FEE_BPS) => (want * BigInt(bps) + 9999n) / 10000n;
|
|
47
|
+
var makerRebate = (value) => REBATE_FLAT + (value * BigInt(REBATE_BPS) + 9999n) / 10000n;
|
|
38
48
|
var TICKER_RE = /^[A-Z][A-Z0-9]{2,11}$/;
|
|
39
49
|
var ADDR_RE = /^0x[0-9a-f]{40}$/;
|
|
40
50
|
var AMOUNT_RE = /^(0|[1-9][0-9]*)$/;
|
|
@@ -321,6 +331,7 @@ function resolve(events, tipHeight) {
|
|
|
321
331
|
const b = bids.get(o.bid);
|
|
322
332
|
if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
|
|
323
333
|
};
|
|
334
|
+
const liveClaim = (o, height) => o.claimedBy !== void 0 && o.claimUntilHeight !== void 0 && height < o.claimUntilHeight;
|
|
324
335
|
for (const ev of ordered) {
|
|
325
336
|
if (ev.height < ACTIVATION_HEIGHT) continue;
|
|
326
337
|
if (ev.height !== pendingBlock) {
|
|
@@ -331,6 +342,7 @@ function resolve(events, tipHeight) {
|
|
|
331
342
|
const v11 = ev.height >= V11_HEIGHT;
|
|
332
343
|
const v12 = ev.height >= V12_HEIGHT;
|
|
333
344
|
const v15 = ev.height >= V15_HEIGHT;
|
|
345
|
+
const v16 = ev.height >= V16_HEIGHT;
|
|
334
346
|
const feeToTreasury = ev.kind === "propose" ? BigInt((ev.paidTo ?? {})[TREASURY_ADDR] ?? "0") : 0n;
|
|
335
347
|
if (ev.kind === "propose") {
|
|
336
348
|
const rec = parseRecord(ev.uri, ev.payloadHash);
|
|
@@ -505,7 +517,7 @@ function resolve(events, tipHeight) {
|
|
|
505
517
|
note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
|
|
506
518
|
continue;
|
|
507
519
|
}
|
|
508
|
-
if (ev.height >= V13_HEIGHT && !wantIsToken && rec.taker === void 0) {
|
|
520
|
+
if (ev.height >= V13_HEIGHT && ev.height < V17_HEIGHT && !wantIsToken && rec.taker === void 0) {
|
|
509
521
|
note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
|
|
510
522
|
continue;
|
|
511
523
|
}
|
|
@@ -572,7 +584,7 @@ function resolve(events, tipHeight) {
|
|
|
572
584
|
status: "open",
|
|
573
585
|
expiresEpoch: ev.expiresEpoch,
|
|
574
586
|
height: ev.height,
|
|
575
|
-
feeBps: v11 ? FEE_BPS : 0
|
|
587
|
+
feeBps: v11 ? v16 ? FEE_BPS_V16 : FEE_BPS : 0
|
|
576
588
|
};
|
|
577
589
|
offers.set(ev.id, o);
|
|
578
590
|
const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
|
|
@@ -729,7 +741,7 @@ function resolve(events, tipHeight) {
|
|
|
729
741
|
continue;
|
|
730
742
|
}
|
|
731
743
|
const amt = BigInt(o.want.amount);
|
|
732
|
-
const fee = o.feeBps ? tradeFee(amt) : 0n;
|
|
744
|
+
const fee = o.feeBps ? tradeFee(amt, o.feeBps) : 0n;
|
|
733
745
|
const buyer = bal(o.want.ticker, who);
|
|
734
746
|
if (buyer.available < amt + fee) {
|
|
735
747
|
note(ev, ev.txid, "fill", false, "insufficient want-token balance");
|
|
@@ -778,8 +790,14 @@ function resolve(events, tipHeight) {
|
|
|
778
790
|
continue;
|
|
779
791
|
}
|
|
780
792
|
if (ev.height >= V13_HEIGHT && !o.taker) {
|
|
781
|
-
|
|
782
|
-
|
|
793
|
+
if (ev.height < V17_HEIGHT) {
|
|
794
|
+
note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
|
|
798
|
+
note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
783
801
|
}
|
|
784
802
|
const pt = ev.paidTo ?? {};
|
|
785
803
|
const want = BigInt(o.want.value);
|
|
@@ -793,7 +811,7 @@ function resolve(events, tipHeight) {
|
|
|
793
811
|
continue;
|
|
794
812
|
}
|
|
795
813
|
const x = X < remaining ? X : remaining;
|
|
796
|
-
const fee = o.feeBps ? tradeFee(x) : 0n;
|
|
814
|
+
const fee = o.feeBps ? tradeFee(x, o.feeBps) : 0n;
|
|
797
815
|
if (BigInt(pt[TREASURY_ADDR] ?? "0") < fee) {
|
|
798
816
|
note(ev, ev.txid, "fill", false, "protocol fee unpaid");
|
|
799
817
|
continue;
|
|
@@ -837,22 +855,35 @@ function resolve(events, tipHeight) {
|
|
|
837
855
|
continue;
|
|
838
856
|
}
|
|
839
857
|
if (ev.height >= V13_HEIGHT && !o.taker) {
|
|
840
|
-
|
|
841
|
-
|
|
858
|
+
if (ev.height < V17_HEIGHT) {
|
|
859
|
+
note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
if (!(liveClaim(o, ev.height) && who === o.claimedBy)) {
|
|
863
|
+
note(ev, ev.txid, "fill", false, "v1.7: open offer \u2014 claim it first (no live claim by you)");
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
842
866
|
}
|
|
843
867
|
const pt = ev.paidTo ?? {};
|
|
844
868
|
const want = BigInt(o.want.value);
|
|
845
|
-
const
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
if (
|
|
853
|
-
|
|
869
|
+
const fee = o.feeBps ? tradeFee(want, o.feeBps) : 0n;
|
|
870
|
+
const restingLiquidity = o.taker !== void 0 && o.bid !== void 0 || o.height >= V17_HEIGHT && o.taker === void 0;
|
|
871
|
+
const rebate = o.height >= V16_HEIGHT && restingLiquidity ? makerRebate(want) : 0n;
|
|
872
|
+
const need = /* @__PURE__ */ new Map();
|
|
873
|
+
const addNeed = (a, v) => need.set(a, (need.get(a) ?? 0n) + v);
|
|
874
|
+
addNeed(o.want.payto, want);
|
|
875
|
+
addNeed(TREASURY_ADDR, fee);
|
|
876
|
+
if (rebate > 0n) addNeed(o.seller, rebate);
|
|
877
|
+
let unpaid;
|
|
878
|
+
for (const [addr, amt] of need) if (BigInt(pt[addr] ?? "0") < amt) {
|
|
879
|
+
unpaid = addr === TREASURY_ADDR ? "protocol fee unpaid" : rebate > 0n && addr === o.seller ? "maker rebate unpaid (v1.6)" : "payment below want.value";
|
|
880
|
+
break;
|
|
881
|
+
}
|
|
882
|
+
if (unpaid) {
|
|
883
|
+
note(ev, ev.txid, "fill", false, unpaid);
|
|
854
884
|
continue;
|
|
855
885
|
}
|
|
886
|
+
const paid = BigInt(pt[o.want.payto] ?? "0");
|
|
856
887
|
if (isNameGive(o.give)) {
|
|
857
888
|
const n = names.get(o.give.name);
|
|
858
889
|
if (!n) {
|
|
@@ -884,6 +915,36 @@ function resolve(events, tipHeight) {
|
|
|
884
915
|
o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
|
|
885
916
|
markBidDone(o, who);
|
|
886
917
|
note(ev, ev.txid, "fill", true);
|
|
918
|
+
} else if (ev.height >= V17_HEIGHT && ev.score === SCORE_CLAIM) {
|
|
919
|
+
if (o.status !== "open") {
|
|
920
|
+
note(ev, ev.txid, "claim", false, `offer ${o.status}`);
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
if (o.taker) {
|
|
924
|
+
note(ev, ev.txid, "claim", false, "taker-bound offer needs no claim");
|
|
925
|
+
continue;
|
|
926
|
+
}
|
|
927
|
+
if (isTokenWant(o.want)) {
|
|
928
|
+
note(ev, ev.txid, "claim", false, "claims are for CSD-priced offers (token offers are no-op-safe)");
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
if (liveClaim(o, ev.height)) {
|
|
932
|
+
note(ev, ev.txid, "claim", false, "offer already claimed (window live)");
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
if (o.claimedBy === who && o.claimUntilHeight !== void 0 && ev.height < o.claimUntilHeight + CLAIM_COOLDOWN_BLOCKS) {
|
|
936
|
+
note(ev, ev.txid, "claim", false, "claim cooldown (you just held this offer)");
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
let liveN = 0;
|
|
940
|
+
for (const x of offers.values()) if (x.claimedBy === who && x.claimUntilHeight !== void 0 && ev.height < x.claimUntilHeight) liveN++;
|
|
941
|
+
if (liveN >= MAX_ACTIVE_CLAIMS) {
|
|
942
|
+
note(ev, ev.txid, "claim", false, `max ${MAX_ACTIVE_CLAIMS} live claims per address`);
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
o.claimedBy = who;
|
|
946
|
+
o.claimUntilHeight = ev.height + CLAIM_WINDOW_BLOCKS;
|
|
947
|
+
note(ev, ev.txid, "claim", true);
|
|
887
948
|
}
|
|
888
949
|
}
|
|
889
950
|
}
|
|
@@ -938,13 +999,17 @@ export {
|
|
|
938
999
|
ACTIVATION_HEIGHT,
|
|
939
1000
|
ADDR_RE,
|
|
940
1001
|
AMOUNT_RE,
|
|
1002
|
+
CLAIM_COOLDOWN_BLOCKS,
|
|
1003
|
+
CLAIM_WINDOW_BLOCKS,
|
|
941
1004
|
COMMIT_MAX_BLOCKS,
|
|
942
1005
|
CONF_TOKEN_FILL,
|
|
943
1006
|
DEPLOY_FEE,
|
|
944
1007
|
DOMAIN,
|
|
945
1008
|
EPOCH_LEN,
|
|
946
1009
|
FEE_BPS,
|
|
1010
|
+
FEE_BPS_V16,
|
|
947
1011
|
HASH_RE,
|
|
1012
|
+
MAX_ACTIVE_CLAIMS,
|
|
948
1013
|
MAX_AMOUNT,
|
|
949
1014
|
MAX_RECORD_BYTES,
|
|
950
1015
|
NAME_GRACE_EPOCHS,
|
|
@@ -952,8 +1017,11 @@ export {
|
|
|
952
1017
|
NAME_PREMIUM_START,
|
|
953
1018
|
NAME_RE,
|
|
954
1019
|
NAME_TERM_EPOCHS,
|
|
1020
|
+
REBATE_BPS,
|
|
1021
|
+
REBATE_FLAT,
|
|
955
1022
|
RESERVED_NAMES,
|
|
956
1023
|
SCORE_CANCEL,
|
|
1024
|
+
SCORE_CLAIM,
|
|
957
1025
|
SCORE_FILL,
|
|
958
1026
|
TICKER_RE,
|
|
959
1027
|
TREASURY_ADDR,
|
|
@@ -962,6 +1030,8 @@ export {
|
|
|
962
1030
|
V13_HEIGHT,
|
|
963
1031
|
V14_HEIGHT,
|
|
964
1032
|
V15_HEIGHT,
|
|
1033
|
+
V16_HEIGHT,
|
|
1034
|
+
V17_HEIGHT,
|
|
965
1035
|
bid,
|
|
966
1036
|
buildRecord,
|
|
967
1037
|
canonicalState,
|
|
@@ -971,6 +1041,7 @@ export {
|
|
|
971
1041
|
isName,
|
|
972
1042
|
isNameGive,
|
|
973
1043
|
isTokenWant,
|
|
1044
|
+
makerRebate,
|
|
974
1045
|
mint,
|
|
975
1046
|
nameClaim,
|
|
976
1047
|
nameCommit,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversealtruism/cairnx-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
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,15 @@
|
|
|
18
18
|
"LICENSE",
|
|
19
19
|
"README.md",
|
|
20
20
|
"CONVENTION.md",
|
|
21
|
+
"MAKERS.md",
|
|
21
22
|
"test/vectors"
|
|
22
23
|
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
26
|
+
"test": "tsx test/vectors.test.ts && tsx test/name-decoy.test.ts"
|
|
27
|
+
},
|
|
23
28
|
"dependencies": {
|
|
24
|
-
"@inversealtruism/csd-codec": "
|
|
29
|
+
"@inversealtruism/csd-codec": "workspace:*"
|
|
25
30
|
},
|
|
26
31
|
"license": "MIT",
|
|
27
32
|
"sideEffects": false,
|
|
@@ -33,9 +38,5 @@
|
|
|
33
38
|
"url": "git+https://github.com/InverseAltruism/csd-sdk.git",
|
|
34
39
|
"directory": "packages/cairnx"
|
|
35
40
|
},
|
|
36
|
-
"homepage": "https://cairn-substrate.com/trade"
|
|
37
|
-
|
|
38
|
-
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
39
|
-
"test": "tsx test/vectors.test.ts && tsx test/name-decoy.test.ts"
|
|
40
|
-
}
|
|
41
|
-
}
|
|
41
|
+
"homepage": "https://cairn-substrate.com/trade"
|
|
42
|
+
}
|