@inversealtruism/cairnx-core 0.1.8
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/CONVENTION.md +530 -0
- package/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/index.cjs +1068 -0
- package/dist/index.d.cts +325 -0
- package/dist/index.d.ts +325 -0
- package/dist/index.js +990 -0
- package/package.json +41 -0
- package/test/vectors/VECTORS.md +17 -0
- package/test/vectors/cases.json +2908 -0
- package/test/vectors/replay-hashes.json +11 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1068 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ACTIVATION_HEIGHT: () => ACTIVATION_HEIGHT,
|
|
24
|
+
ADDR_RE: () => ADDR_RE,
|
|
25
|
+
AMOUNT_RE: () => AMOUNT_RE,
|
|
26
|
+
COMMIT_MAX_BLOCKS: () => COMMIT_MAX_BLOCKS,
|
|
27
|
+
CONF_TOKEN_FILL: () => CONF_TOKEN_FILL,
|
|
28
|
+
DEPLOY_FEE: () => DEPLOY_FEE,
|
|
29
|
+
DOMAIN: () => DOMAIN,
|
|
30
|
+
EPOCH_LEN: () => EPOCH_LEN,
|
|
31
|
+
FEE_BPS: () => FEE_BPS,
|
|
32
|
+
HASH_RE: () => HASH_RE,
|
|
33
|
+
MAX_AMOUNT: () => MAX_AMOUNT,
|
|
34
|
+
MAX_RECORD_BYTES: () => MAX_RECORD_BYTES,
|
|
35
|
+
NAME_GRACE_EPOCHS: () => NAME_GRACE_EPOCHS,
|
|
36
|
+
NAME_PREMIUM_DECAY_EPOCHS: () => NAME_PREMIUM_DECAY_EPOCHS,
|
|
37
|
+
NAME_PREMIUM_START: () => NAME_PREMIUM_START,
|
|
38
|
+
NAME_RE: () => NAME_RE,
|
|
39
|
+
NAME_TERM_EPOCHS: () => NAME_TERM_EPOCHS,
|
|
40
|
+
RESERVED_NAMES: () => RESERVED_NAMES,
|
|
41
|
+
SCORE_CANCEL: () => SCORE_CANCEL,
|
|
42
|
+
SCORE_FILL: () => SCORE_FILL,
|
|
43
|
+
TICKER_RE: () => TICKER_RE,
|
|
44
|
+
TREASURY_ADDR: () => TREASURY_ADDR,
|
|
45
|
+
V11_HEIGHT: () => V11_HEIGHT,
|
|
46
|
+
V12_HEIGHT: () => V12_HEIGHT,
|
|
47
|
+
V13_HEIGHT: () => V13_HEIGHT,
|
|
48
|
+
V14_HEIGHT: () => V14_HEIGHT,
|
|
49
|
+
V15_HEIGHT: () => V15_HEIGHT,
|
|
50
|
+
bid: () => bid,
|
|
51
|
+
buildRecord: () => buildRecord,
|
|
52
|
+
canonicalState: () => canonicalState,
|
|
53
|
+
deploy: () => deploy,
|
|
54
|
+
epochOf: () => epochOf,
|
|
55
|
+
expiredClaimFee: () => expiredClaimFee,
|
|
56
|
+
isName: () => isName,
|
|
57
|
+
isNameGive: () => isNameGive,
|
|
58
|
+
isTokenWant: () => isTokenWant,
|
|
59
|
+
mint: () => mint,
|
|
60
|
+
nameClaim: () => nameClaim,
|
|
61
|
+
nameCommit: () => nameCommit,
|
|
62
|
+
nameCommitRecord: () => nameCommitRecord,
|
|
63
|
+
nameRegFee: () => nameRegFee,
|
|
64
|
+
nameRenew: () => nameRenew,
|
|
65
|
+
nameSet: () => nameSet,
|
|
66
|
+
nameXfer: () => nameXfer,
|
|
67
|
+
offer: () => offer,
|
|
68
|
+
offerCancelAll: () => offerCancelAll,
|
|
69
|
+
parseAmount: () => parseAmount,
|
|
70
|
+
parseRecord: () => parseRecord,
|
|
71
|
+
resolve: () => resolve,
|
|
72
|
+
tokenMeta: () => tokenMeta,
|
|
73
|
+
tradeFee: () => tradeFee,
|
|
74
|
+
transfer: () => transfer
|
|
75
|
+
});
|
|
76
|
+
module.exports = __toCommonJS(index_exports);
|
|
77
|
+
|
|
78
|
+
// src/types.ts
|
|
79
|
+
var DOMAIN = "cairnx:v1";
|
|
80
|
+
var ACTIVATION_HEIGHT = 29860;
|
|
81
|
+
var V11_HEIGHT = 29960;
|
|
82
|
+
var V12_HEIGHT = 30300;
|
|
83
|
+
var V13_HEIGHT = 31100;
|
|
84
|
+
var V14_HEIGHT = 31400;
|
|
85
|
+
var V15_HEIGHT = 32e3;
|
|
86
|
+
var EPOCH_LEN = 30;
|
|
87
|
+
var NAME_TERM_EPOCHS = 8760;
|
|
88
|
+
var NAME_GRACE_EPOCHS = 720;
|
|
89
|
+
var NAME_PREMIUM_START = 20n;
|
|
90
|
+
var NAME_PREMIUM_DECAY_EPOCHS = 720;
|
|
91
|
+
function expiredClaimFee(name, epochsPastGraceEnd) {
|
|
92
|
+
const base = nameRegFee(name);
|
|
93
|
+
if (epochsPastGraceEnd >= NAME_PREMIUM_DECAY_EPOCHS) return base;
|
|
94
|
+
const left = BigInt(NAME_PREMIUM_DECAY_EPOCHS - epochsPastGraceEnd);
|
|
95
|
+
const mult = 1n + (NAME_PREMIUM_START - 1n) * left / BigInt(NAME_PREMIUM_DECAY_EPOCHS);
|
|
96
|
+
return base * mult;
|
|
97
|
+
}
|
|
98
|
+
var MAX_RECORD_BYTES = 512;
|
|
99
|
+
var MAX_AMOUNT = (1n << 96n) - 1n;
|
|
100
|
+
var SCORE_FILL = 100;
|
|
101
|
+
var SCORE_CANCEL = 0;
|
|
102
|
+
var CONF_TOKEN_FILL = 1e6;
|
|
103
|
+
var TREASURY_ADDR = "0x6b09ce74e6070ebc982ab0fb793a211c4d24f016";
|
|
104
|
+
var FEE_BPS = 100;
|
|
105
|
+
var DEPLOY_FEE = 1e8;
|
|
106
|
+
function nameRegFee(name) {
|
|
107
|
+
const n = name.length;
|
|
108
|
+
if (n <= 3) return 500000000n;
|
|
109
|
+
if (n === 4) return 200000000n;
|
|
110
|
+
if (n === 5) return 100000000n;
|
|
111
|
+
if (n <= 9) return 50000000n;
|
|
112
|
+
return 10000000n;
|
|
113
|
+
}
|
|
114
|
+
var tradeFee = (want) => (want * BigInt(FEE_BPS) + 9999n) / 10000n;
|
|
115
|
+
var TICKER_RE = /^[A-Z][A-Z0-9]{2,11}$/;
|
|
116
|
+
var ADDR_RE = /^0x[0-9a-f]{40}$/;
|
|
117
|
+
var AMOUNT_RE = /^(0|[1-9][0-9]*)$/;
|
|
118
|
+
var NAME_RE = /^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;
|
|
119
|
+
var HASH_RE = /^0x[0-9a-f]{64}$/;
|
|
120
|
+
var RESERVED_NAMES = /* @__PURE__ */ new Set(["csd", "treasury", "admin", "official", "root", "www", "support"]);
|
|
121
|
+
var COMMIT_MAX_BLOCKS = 8 * EPOCH_LEN;
|
|
122
|
+
var epochOf = (height) => Math.floor(height / EPOCH_LEN);
|
|
123
|
+
var isNameGive = (g) => typeof g.name === "string";
|
|
124
|
+
var isTokenWant = (w) => typeof w.ticker === "string";
|
|
125
|
+
|
|
126
|
+
// src/records.ts
|
|
127
|
+
var import_csd_codec = require("@inversealtruism/csd-codec");
|
|
128
|
+
function parseAmount(s, opts = {}) {
|
|
129
|
+
if (typeof s !== "string" || !AMOUNT_RE.test(s)) return null;
|
|
130
|
+
const v = BigInt(s);
|
|
131
|
+
if (v > MAX_AMOUNT) return null;
|
|
132
|
+
if (v === 0n && !opts.allowZero) return null;
|
|
133
|
+
return v;
|
|
134
|
+
}
|
|
135
|
+
var isAddr = (a) => typeof a === "string" && ADDR_RE.test(a);
|
|
136
|
+
var isTicker = (t) => typeof t === "string" && TICKER_RE.test(t);
|
|
137
|
+
var isHash = (h) => typeof h === "string" && HASH_RE.test(h);
|
|
138
|
+
var isName = (n) => typeof n === "string" && NAME_RE.test(n) && !RESERVED_NAMES.has(n);
|
|
139
|
+
function nameCommit(name, salt, owner) {
|
|
140
|
+
return (0, import_csd_codec.payloadHash)({ t: "cairnx:name:commit:v1", name, salt, owner: owner.toLowerCase() });
|
|
141
|
+
}
|
|
142
|
+
function strWellFormed(s) {
|
|
143
|
+
const wf = String.prototype.isWellFormed;
|
|
144
|
+
if (typeof wf === "function") return wf.call(s);
|
|
145
|
+
for (let i = 0; i < s.length; i++) {
|
|
146
|
+
const c = s.charCodeAt(i);
|
|
147
|
+
if (c >= 55296 && c <= 56319) {
|
|
148
|
+
const n = s.charCodeAt(i + 1);
|
|
149
|
+
if (!(n >= 56320 && n <= 57343)) return false;
|
|
150
|
+
i++;
|
|
151
|
+
} else if (c >= 56320 && c <= 57343) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
function isWellFormedDeep(v) {
|
|
158
|
+
if (typeof v === "string") return strWellFormed(v);
|
|
159
|
+
if (Array.isArray(v)) return v.every(isWellFormedDeep);
|
|
160
|
+
if (v && typeof v === "object") {
|
|
161
|
+
for (const [k, val] of Object.entries(v)) {
|
|
162
|
+
if (!strWellFormed(k)) return false;
|
|
163
|
+
if (!isWellFormedDeep(val)) return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
var onlyKeys = (r, allowed) => Object.keys(r).every((k) => allowed.has(k));
|
|
169
|
+
var DEPLOY_KEYS = /* @__PURE__ */ new Set(["v", "t", "ticker", "name", "decimals", "supply", "mint", "mintLimit"]);
|
|
170
|
+
var MINT_KEYS = /* @__PURE__ */ new Set(["v", "t", "ticker", "amount"]);
|
|
171
|
+
var TRANSFER_KEYS = /* @__PURE__ */ new Set(["v", "t", "ticker", "to", "amount", "memo", "ts"]);
|
|
172
|
+
var OFFER_KEYS = /* @__PURE__ */ new Set(["v", "t", "give", "want", "min", "bid", "taker", "memo", "ts"]);
|
|
173
|
+
var BID_KEYS = /* @__PURE__ */ new Set(["v", "t", "want", "give", "memo", "ts"]);
|
|
174
|
+
var NAME_KEYS = /* @__PURE__ */ new Set(["v", "t", "name", "salt"]);
|
|
175
|
+
function parseRecord(uri, payloadHashHex) {
|
|
176
|
+
if (new TextEncoder().encode(uri).length > MAX_RECORD_BYTES) return null;
|
|
177
|
+
let obj;
|
|
178
|
+
try {
|
|
179
|
+
obj = JSON.parse(uri);
|
|
180
|
+
} catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return null;
|
|
184
|
+
try {
|
|
185
|
+
if ((0, import_csd_codec.canonicalJson)(obj) !== uri) return null;
|
|
186
|
+
if ((0, import_csd_codec.payloadHash)(obj).toLowerCase() !== payloadHashHex.toLowerCase()) return null;
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
if (!isWellFormedDeep(obj)) return null;
|
|
191
|
+
const r = obj;
|
|
192
|
+
if (r.v !== 1 || typeof r.t !== "string") return null;
|
|
193
|
+
switch (r.t) {
|
|
194
|
+
case "deploy": {
|
|
195
|
+
if (!onlyKeys(r, DEPLOY_KEYS)) return null;
|
|
196
|
+
if (!isTicker(r.ticker)) return null;
|
|
197
|
+
if (r.name !== void 0 && (typeof r.name !== "string" || r.name.length > 32)) return null;
|
|
198
|
+
if (typeof r.decimals !== "number" || !Number.isInteger(r.decimals) || r.decimals < 0 || r.decimals > 8) return null;
|
|
199
|
+
if (parseAmount(r.supply) === null) return null;
|
|
200
|
+
if (r.mint !== "open" && r.mint !== "issuer") return null;
|
|
201
|
+
if (r.mint === "open" && parseAmount(r.mintLimit) === null) return null;
|
|
202
|
+
if (r.mint === "issuer" && r.mintLimit !== void 0) return null;
|
|
203
|
+
return r;
|
|
204
|
+
}
|
|
205
|
+
case "mint": {
|
|
206
|
+
if (!onlyKeys(r, MINT_KEYS)) return null;
|
|
207
|
+
if (!isTicker(r.ticker)) return null;
|
|
208
|
+
if (r.amount !== void 0 && parseAmount(r.amount) === null) return null;
|
|
209
|
+
return r;
|
|
210
|
+
}
|
|
211
|
+
case "transfer": {
|
|
212
|
+
if (!onlyKeys(r, TRANSFER_KEYS)) return null;
|
|
213
|
+
if (!isTicker(r.ticker) || !isAddr(r.to) || parseAmount(r.amount) === null) return null;
|
|
214
|
+
if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
|
|
215
|
+
if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
|
|
216
|
+
return r;
|
|
217
|
+
}
|
|
218
|
+
case "offer": {
|
|
219
|
+
if (!onlyKeys(r, OFFER_KEYS)) return null;
|
|
220
|
+
const g = r.give;
|
|
221
|
+
const w = r.want;
|
|
222
|
+
if (!g || !w || typeof g !== "object" || Array.isArray(g) || typeof w !== "object" || Array.isArray(w)) return null;
|
|
223
|
+
const gKeys = Object.keys(g).sort().join(",");
|
|
224
|
+
if (gKeys === "amount,ticker") {
|
|
225
|
+
if (!isTicker(g.ticker) || parseAmount(g.amount) === null) return null;
|
|
226
|
+
} else if (gKeys === "name") {
|
|
227
|
+
if (!isName(g.name)) return null;
|
|
228
|
+
} else return null;
|
|
229
|
+
const wKeys = Object.keys(w).filter((k) => k !== "payto").sort().join(",");
|
|
230
|
+
if (wKeys === "value") {
|
|
231
|
+
if (parseAmount(w.value, { allowZero: true }) === null) return null;
|
|
232
|
+
} else if (wKeys === "amount,ticker") {
|
|
233
|
+
if (!isTicker(w.ticker) || parseAmount(w.amount, { allowZero: true }) === null) return null;
|
|
234
|
+
if (gKeys === "amount,ticker" && w.ticker === g.ticker) return null;
|
|
235
|
+
if (r.min !== void 0) return null;
|
|
236
|
+
} else return null;
|
|
237
|
+
if (w.payto !== void 0 && !isAddr(w.payto)) return null;
|
|
238
|
+
if (r.min !== void 0) {
|
|
239
|
+
if (gKeys !== "amount,ticker" || wKeys !== "value") return null;
|
|
240
|
+
const mn = parseAmount(r.min);
|
|
241
|
+
if (mn === null || mn > parseAmount(w.value, { allowZero: true })) return null;
|
|
242
|
+
}
|
|
243
|
+
if (r.bid !== void 0 && !isHash(r.bid)) return null;
|
|
244
|
+
if (r.taker !== void 0 && !isAddr(r.taker)) return null;
|
|
245
|
+
if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
|
|
246
|
+
if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
|
|
247
|
+
return r;
|
|
248
|
+
}
|
|
249
|
+
case "ocancel": {
|
|
250
|
+
if (r.ticker !== void 0 && r.name !== void 0) return null;
|
|
251
|
+
if (r.ticker !== void 0 && !isTicker(r.ticker)) return null;
|
|
252
|
+
if (r.name !== void 0 && !isName(r.name)) return null;
|
|
253
|
+
const n = Object.keys(r).length;
|
|
254
|
+
if (n !== 2 + (r.ticker !== void 0 ? 1 : 0) + (r.name !== void 0 ? 1 : 0)) return null;
|
|
255
|
+
return r;
|
|
256
|
+
}
|
|
257
|
+
case "bid": {
|
|
258
|
+
if (!onlyKeys(r, BID_KEYS)) return null;
|
|
259
|
+
const w = r.want;
|
|
260
|
+
const g = r.give;
|
|
261
|
+
if (!w || !g || typeof w !== "object" || Array.isArray(w) || typeof g !== "object" || Array.isArray(g)) return null;
|
|
262
|
+
const wKeys = Object.keys(w).sort().join(",");
|
|
263
|
+
if (wKeys === "amount,ticker") {
|
|
264
|
+
if (!isTicker(w.ticker) || parseAmount(w.amount) === null) return null;
|
|
265
|
+
} else if (wKeys === "name") {
|
|
266
|
+
if (!isName(w.name)) return null;
|
|
267
|
+
} else return null;
|
|
268
|
+
if (Object.keys(g).sort().join(",") !== "value" || parseAmount(g.value) === null) return null;
|
|
269
|
+
if (r.memo !== void 0 && (typeof r.memo !== "string" || r.memo.length > 64)) return null;
|
|
270
|
+
if (r.ts !== void 0 && (typeof r.ts !== "number" || !Number.isSafeInteger(r.ts))) return null;
|
|
271
|
+
return r;
|
|
272
|
+
}
|
|
273
|
+
// ── names (v1.1) ──
|
|
274
|
+
case "ncommit": {
|
|
275
|
+
if (!isHash(r.commit)) return null;
|
|
276
|
+
if (Object.keys(r).length !== 3) return null;
|
|
277
|
+
return r;
|
|
278
|
+
}
|
|
279
|
+
case "name": {
|
|
280
|
+
if (!onlyKeys(r, NAME_KEYS)) return null;
|
|
281
|
+
if (!isName(r.name)) return null;
|
|
282
|
+
if (r.salt !== void 0 && (typeof r.salt !== "string" || !/^[0-9a-fA-F]{16,128}$/.test(r.salt))) return null;
|
|
283
|
+
return r;
|
|
284
|
+
}
|
|
285
|
+
case "nxfer": {
|
|
286
|
+
if (!isName(r.name) || !isAddr(r.to)) return null;
|
|
287
|
+
if (Object.keys(r).length !== 4) return null;
|
|
288
|
+
return r;
|
|
289
|
+
}
|
|
290
|
+
case "nset": {
|
|
291
|
+
if (!isName(r.name) || !isAddr(r.addr)) return null;
|
|
292
|
+
if (Object.keys(r).length !== 4) return null;
|
|
293
|
+
return r;
|
|
294
|
+
}
|
|
295
|
+
// ── v1.5 ──
|
|
296
|
+
case "nrenew": {
|
|
297
|
+
if (!isName(r.name)) return null;
|
|
298
|
+
if (Object.keys(r).length !== 3) return null;
|
|
299
|
+
return r;
|
|
300
|
+
}
|
|
301
|
+
case "tmeta": {
|
|
302
|
+
if (!isTicker(r.ticker)) return null;
|
|
303
|
+
if (typeof r.hash !== "string" || !/^0x[0-9a-f]{64}$/.test(r.hash)) return null;
|
|
304
|
+
if (Object.keys(r).length !== 4) return null;
|
|
305
|
+
return r;
|
|
306
|
+
}
|
|
307
|
+
default:
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function buildRecord(record) {
|
|
312
|
+
const uri = (0, import_csd_codec.canonicalJson)(record);
|
|
313
|
+
const ph = (0, import_csd_codec.payloadHash)(record);
|
|
314
|
+
const back = parseRecord(uri, ph);
|
|
315
|
+
if (back === null) throw new Error("record does not validate against CONVENTION.md");
|
|
316
|
+
return { record, uri, payloadHash: ph };
|
|
317
|
+
}
|
|
318
|
+
var deploy = (r) => buildRecord({ v: 1, t: "deploy", ...r });
|
|
319
|
+
var mint = (r) => buildRecord({ v: 1, t: "mint", ...r });
|
|
320
|
+
var transfer = (r) => buildRecord({ v: 1, t: "transfer", ...r });
|
|
321
|
+
var offer = (r) => buildRecord({ v: 1, t: "offer", ...r });
|
|
322
|
+
var bid = (r) => buildRecord({ v: 1, t: "bid", ...r });
|
|
323
|
+
var offerCancelAll = (r = {}) => buildRecord({ v: 1, t: "ocancel", ...r });
|
|
324
|
+
var nameCommitRecord = (r) => buildRecord({ v: 1, t: "ncommit", ...r });
|
|
325
|
+
var nameClaim = (r) => buildRecord({ v: 1, t: "name", ...r });
|
|
326
|
+
var nameXfer = (r) => buildRecord({ v: 1, t: "nxfer", ...r });
|
|
327
|
+
var nameSet = (r) => buildRecord({ v: 1, t: "nset", ...r });
|
|
328
|
+
var nameRenew = (r) => buildRecord({ v: 1, t: "nrenew", ...r });
|
|
329
|
+
var tokenMeta = (r) => buildRecord({ v: 1, t: "tmeta", ...r });
|
|
330
|
+
|
|
331
|
+
// src/resolve.ts
|
|
332
|
+
var ord = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
333
|
+
function resolve(events, tipHeight) {
|
|
334
|
+
const ordered = [...events].sort(
|
|
335
|
+
(a, b) => a.height - b.height || Number(a.kind === "attest") - Number(b.kind === "attest") || a.pos - b.pos || ord(
|
|
336
|
+
a.kind === "propose" ? a.id : a.txid,
|
|
337
|
+
b.kind === "propose" ? b.id : b.txid
|
|
338
|
+
)
|
|
339
|
+
);
|
|
340
|
+
const tokens = /* @__PURE__ */ new Map();
|
|
341
|
+
const balances = /* @__PURE__ */ new Map();
|
|
342
|
+
const names = /* @__PURE__ */ new Map();
|
|
343
|
+
const commits = /* @__PURE__ */ new Map();
|
|
344
|
+
const offers = /* @__PURE__ */ new Map();
|
|
345
|
+
const offerLock = /* @__PURE__ */ new Map();
|
|
346
|
+
const bids = /* @__PURE__ */ new Map();
|
|
347
|
+
const log = [];
|
|
348
|
+
let feesPaid = 0n;
|
|
349
|
+
let pendingCancels = [];
|
|
350
|
+
let pendingBlock = -1;
|
|
351
|
+
const applyPendingCancels = () => {
|
|
352
|
+
for (const f of pendingCancels) f();
|
|
353
|
+
pendingCancels = [];
|
|
354
|
+
};
|
|
355
|
+
const bal = (ticker, addr) => {
|
|
356
|
+
let m = balances.get(ticker);
|
|
357
|
+
if (!m) {
|
|
358
|
+
m = /* @__PURE__ */ new Map();
|
|
359
|
+
balances.set(ticker, m);
|
|
360
|
+
}
|
|
361
|
+
let b = m.get(addr);
|
|
362
|
+
if (!b) {
|
|
363
|
+
b = { available: 0n, locked: 0n };
|
|
364
|
+
m.set(addr, b);
|
|
365
|
+
}
|
|
366
|
+
return b;
|
|
367
|
+
};
|
|
368
|
+
const note = (e, id, kind, ok, why) => log.push({ height: e.height, pos: e.pos, id, kind, ok, ...why ? { note: why } : {} });
|
|
369
|
+
const releaseGive = (o) => {
|
|
370
|
+
if (isNameGive(o.give)) {
|
|
371
|
+
const n = names.get(o.give.name);
|
|
372
|
+
if (n) n.locked = false;
|
|
373
|
+
} else {
|
|
374
|
+
const amt = offerLock.get(o.id) ?? 0n;
|
|
375
|
+
const b = bal(o.give.ticker, o.seller);
|
|
376
|
+
b.locked -= amt;
|
|
377
|
+
b.available += amt;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const sweepExpired = (height) => {
|
|
381
|
+
const ep = epochOf(height);
|
|
382
|
+
for (const o of offers.values()) {
|
|
383
|
+
if (o.status === "open" && ep > o.expiresEpoch) {
|
|
384
|
+
releaseGive(o);
|
|
385
|
+
o.status = "expired";
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const b of bids.values()) {
|
|
389
|
+
if (b.status === "open" && ep > b.expiresEpoch) b.status = "expired";
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
const V15_EPOCH = epochOf(V15_HEIGHT);
|
|
393
|
+
const paidThrough = (n) => n.paidThroughEpoch ?? V15_EPOCH + NAME_TERM_EPOCHS;
|
|
394
|
+
const lapsed = (n, ep) => ep > paidThrough(n) + NAME_GRACE_EPOCHS;
|
|
395
|
+
const inGrace = (n, ep) => ep > paidThrough(n) && !lapsed(n, ep);
|
|
396
|
+
const markBidDone = (o, buyer) => {
|
|
397
|
+
if (!o.bid) return;
|
|
398
|
+
const b = bids.get(o.bid);
|
|
399
|
+
if (b && b.status === "open" && b.bidder === buyer) b.status = "done";
|
|
400
|
+
};
|
|
401
|
+
for (const ev of ordered) {
|
|
402
|
+
if (ev.height < ACTIVATION_HEIGHT) continue;
|
|
403
|
+
if (ev.height !== pendingBlock) {
|
|
404
|
+
applyPendingCancels();
|
|
405
|
+
pendingBlock = ev.height;
|
|
406
|
+
}
|
|
407
|
+
sweepExpired(ev.height);
|
|
408
|
+
const v11 = ev.height >= V11_HEIGHT;
|
|
409
|
+
const v12 = ev.height >= V12_HEIGHT;
|
|
410
|
+
const v15 = ev.height >= V15_HEIGHT;
|
|
411
|
+
const feeToTreasury = ev.kind === "propose" ? BigInt((ev.paidTo ?? {})[TREASURY_ADDR] ?? "0") : 0n;
|
|
412
|
+
if (ev.kind === "propose") {
|
|
413
|
+
const rec = parseRecord(ev.uri, ev.payloadHash);
|
|
414
|
+
if (!rec) {
|
|
415
|
+
note(ev, ev.id, "invalid", false, "unparseable/non-canonical record");
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
const who = ev.proposer.toLowerCase();
|
|
419
|
+
if (rec.t === "deploy") {
|
|
420
|
+
if (tokens.has(rec.ticker)) {
|
|
421
|
+
note(ev, ev.id, "deploy", false, "ticker taken (first anchor wins)");
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
if (v11 && feeToTreasury < BigInt(DEPLOY_FEE)) {
|
|
425
|
+
note(ev, ev.id, "deploy", false, "deploy fee unpaid");
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
const supply = parseAmount(rec.supply);
|
|
429
|
+
const mintLimit = rec.mint === "open" ? parseAmount(rec.mintLimit) : null;
|
|
430
|
+
tokens.set(rec.ticker, {
|
|
431
|
+
minted: 0n,
|
|
432
|
+
supply,
|
|
433
|
+
mintLimit,
|
|
434
|
+
meta: { ticker: rec.ticker, deployId: ev.id, deployer: who, name: rec.name, decimals: rec.decimals, supply: rec.supply, minted: "0", mint: rec.mint, mintLimit: rec.mintLimit, height: ev.height }
|
|
435
|
+
});
|
|
436
|
+
if (v11) feesPaid += BigInt(DEPLOY_FEE);
|
|
437
|
+
note(ev, ev.id, "deploy", true);
|
|
438
|
+
} else if (rec.t === "mint") {
|
|
439
|
+
const tok = tokens.get(rec.ticker);
|
|
440
|
+
if (!tok) {
|
|
441
|
+
note(ev, ev.id, "mint", false, "unknown ticker");
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
const remaining = tok.supply - tok.minted;
|
|
445
|
+
if (remaining <= 0n) {
|
|
446
|
+
note(ev, ev.id, "mint", false, "supply exhausted");
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
let credit;
|
|
450
|
+
if (tok.meta.mint === "issuer") {
|
|
451
|
+
if (who !== tok.meta.deployer) {
|
|
452
|
+
note(ev, ev.id, "mint", false, "issuer-only mint");
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
const req = rec.amount !== void 0 ? parseAmount(rec.amount) : null;
|
|
456
|
+
if (req === null) {
|
|
457
|
+
note(ev, ev.id, "mint", false, "issuer mint requires amount");
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
credit = req < remaining ? req : remaining;
|
|
461
|
+
} else {
|
|
462
|
+
const req = rec.amount !== void 0 ? parseAmount(rec.amount) : tok.mintLimit;
|
|
463
|
+
const capped = req < tok.mintLimit ? req : tok.mintLimit;
|
|
464
|
+
credit = capped < remaining ? capped : remaining;
|
|
465
|
+
}
|
|
466
|
+
tok.minted += credit;
|
|
467
|
+
bal(rec.ticker, who).available += credit;
|
|
468
|
+
note(ev, ev.id, "mint", true, credit.toString());
|
|
469
|
+
} else if (rec.t === "transfer") {
|
|
470
|
+
if (!tokens.has(rec.ticker)) {
|
|
471
|
+
note(ev, ev.id, "transfer", false, "unknown ticker");
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
const amt = parseAmount(rec.amount);
|
|
475
|
+
const from = bal(rec.ticker, who);
|
|
476
|
+
if (from.available < amt) {
|
|
477
|
+
note(ev, ev.id, "transfer", false, "insufficient available balance");
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
from.available -= amt;
|
|
481
|
+
bal(rec.ticker, rec.to.toLowerCase()).available += amt;
|
|
482
|
+
note(ev, ev.id, "transfer", true);
|
|
483
|
+
} else if (rec.t === "ncommit") {
|
|
484
|
+
if (!v11) {
|
|
485
|
+
note(ev, ev.id, "ncommit", false, "before v1.1 activation");
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
const prev = commits.get(rec.commit);
|
|
489
|
+
if (prev === void 0 || ev.height < prev) commits.set(rec.commit, ev.height);
|
|
490
|
+
note(ev, ev.id, "ncommit", true);
|
|
491
|
+
} else if (rec.t === "name") {
|
|
492
|
+
if (!v11) {
|
|
493
|
+
note(ev, ev.id, "name", false, "before v1.1 activation");
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
let effHeight = ev.height;
|
|
497
|
+
if (rec.salt !== void 0) {
|
|
498
|
+
const ch = nameCommit(rec.name, rec.salt, who);
|
|
499
|
+
const cH = commits.get(ch);
|
|
500
|
+
if (cH === void 0 || cH >= ev.height || ev.height - cH > COMMIT_MAX_BLOCKS) {
|
|
501
|
+
note(ev, ev.id, "name", false, "no valid in-window commit for this reveal");
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
effHeight = cH;
|
|
505
|
+
}
|
|
506
|
+
const cur = names.get(rec.name);
|
|
507
|
+
const epClaim = epochOf(ev.height);
|
|
508
|
+
if (cur && v15 && lapsed(cur, epClaim)) {
|
|
509
|
+
const fee = expiredClaimFee(rec.name, epClaim - (paidThrough(cur) + NAME_GRACE_EPOCHS));
|
|
510
|
+
if (feeToTreasury < fee) {
|
|
511
|
+
note(ev, ev.id, "name", false, "lapsed-name claim fee unpaid (decaying premium)");
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
for (const o of offers.values()) if (o.status === "open" && isNameGive(o.give) && o.give.name === rec.name) {
|
|
515
|
+
releaseGive(o);
|
|
516
|
+
o.status = "cancelled";
|
|
517
|
+
}
|
|
518
|
+
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 });
|
|
519
|
+
feesPaid += fee;
|
|
520
|
+
note(ev, ev.id, "name", true, "lapsed lease re-claimed (premium)");
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
if (feeToTreasury < nameRegFee(rec.name)) {
|
|
524
|
+
note(ev, ev.id, "name", false, "name registration fee unpaid");
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
const cand = {
|
|
528
|
+
owner: who,
|
|
529
|
+
effHeight,
|
|
530
|
+
pos: ev.pos,
|
|
531
|
+
id: ev.id,
|
|
532
|
+
height: ev.height,
|
|
533
|
+
locked: false,
|
|
534
|
+
...v15 ? { paidThroughEpoch: epClaim + NAME_TERM_EPOCHS } : {}
|
|
535
|
+
};
|
|
536
|
+
const better = !cur || !cur.viaFill && (effHeight < cur.effHeight || effHeight === cur.effHeight && (ev.pos < cur.pos || ev.pos === cur.pos && ev.id < cur.id));
|
|
537
|
+
if (!better) {
|
|
538
|
+
note(ev, ev.id, "name", false, cur?.viaFill ? "name taken (purchased \u2014 not displaceable)" : "name taken (earlier anchor wins)");
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (cur) {
|
|
542
|
+
for (const o of offers.values()) if (o.status === "open" && isNameGive(o.give) && o.give.name === rec.name) {
|
|
543
|
+
releaseGive(o);
|
|
544
|
+
o.status = "cancelled";
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
names.set(rec.name, cand);
|
|
548
|
+
feesPaid += nameRegFee(rec.name);
|
|
549
|
+
note(ev, ev.id, "name", true, cur ? "displaced prior holder" : void 0);
|
|
550
|
+
} else if (rec.t === "nxfer") {
|
|
551
|
+
const n = names.get(rec.name);
|
|
552
|
+
if (!n || n.owner !== who) {
|
|
553
|
+
note(ev, ev.id, "nxfer", false, "not the name owner");
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (v15 && lapsed(n, epochOf(ev.height))) {
|
|
557
|
+
note(ev, ev.id, "nxfer", false, "lease lapsed \u2014 claim it instead");
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
if (n.locked) {
|
|
561
|
+
note(ev, ev.id, "nxfer", false, "name is locked by an open offer");
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
n.owner = rec.to.toLowerCase();
|
|
565
|
+
n.addr = void 0;
|
|
566
|
+
note(ev, ev.id, "nxfer", true);
|
|
567
|
+
} else if (rec.t === "nset") {
|
|
568
|
+
const n = names.get(rec.name);
|
|
569
|
+
if (!n || n.owner !== who) {
|
|
570
|
+
note(ev, ev.id, "nset", false, "not the name owner");
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
if (v15 && lapsed(n, epochOf(ev.height))) {
|
|
574
|
+
note(ev, ev.id, "nset", false, "lease lapsed \u2014 claim it instead");
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
n.addr = rec.addr.toLowerCase();
|
|
578
|
+
note(ev, ev.id, "nset", true);
|
|
579
|
+
} else if (rec.t === "offer") {
|
|
580
|
+
const wantIsToken = isTokenWant(rec.want);
|
|
581
|
+
if ((wantIsToken || rec.min !== void 0 || rec.bid !== void 0) && !v12) {
|
|
582
|
+
note(ev, ev.id, "offer", false, "v1.2 offer shape before activation");
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
if (ev.height >= V13_HEIGHT && !wantIsToken && rec.taker === void 0) {
|
|
586
|
+
note(ev, ev.id, "offer", false, "v1.3: CSD-priced offers must be taker-bound (use bid/RFQ)");
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
const payto = (rec.want.payto ?? who).toLowerCase();
|
|
590
|
+
if (payto === TREASURY_ADDR) {
|
|
591
|
+
note(ev, ev.id, "offer", false, "payto cannot be the protocol treasury");
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
if (!Number.isSafeInteger(ev.expiresEpoch)) {
|
|
595
|
+
note(ev, ev.id, "offer", false, "expiresEpoch out of safe-integer range");
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (epochOf(ev.height) > ev.expiresEpoch) {
|
|
599
|
+
note(ev, ev.id, "offer", false, "already expired at anchor");
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
const give = rec.give;
|
|
603
|
+
if (isNameGive(give)) {
|
|
604
|
+
if (!v11) {
|
|
605
|
+
note(ev, ev.id, "offer", false, "name offers need v1.1");
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
const n = names.get(give.name);
|
|
609
|
+
if (!n || n.owner !== who) {
|
|
610
|
+
note(ev, ev.id, "offer", false, "you don't own this name");
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
if (n.locked) {
|
|
614
|
+
note(ev, ev.id, "offer", false, "name already locked by another offer");
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
if (ev.height >= V13_HEIGHT && !n.viaFill && ev.height - n.effHeight <= COMMIT_MAX_BLOCKS) {
|
|
618
|
+
note(ev, ev.id, "offer", false, "v1.3: name too young to sell (claim must out-age the commit window)");
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (v15 && paidThrough(n) < ev.expiresEpoch) {
|
|
622
|
+
note(ev, ev.id, "offer", false, "v1.5: lease ends inside the offer window (renew first)");
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
n.locked = true;
|
|
626
|
+
} else {
|
|
627
|
+
if (!tokens.has(give.ticker)) {
|
|
628
|
+
note(ev, ev.id, "offer", false, "unknown ticker");
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
const amt = parseAmount(give.amount);
|
|
632
|
+
const s = bal(give.ticker, who);
|
|
633
|
+
if (s.available < amt) {
|
|
634
|
+
note(ev, ev.id, "offer", false, "insufficient available balance");
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
s.available -= amt;
|
|
638
|
+
s.locked += amt;
|
|
639
|
+
offerLock.set(ev.id, amt);
|
|
640
|
+
}
|
|
641
|
+
const o = {
|
|
642
|
+
id: ev.id,
|
|
643
|
+
seller: who,
|
|
644
|
+
give,
|
|
645
|
+
want: wantIsToken ? { ticker: rec.want.ticker, amount: rec.want.amount, payto } : { value: rec.want.value, payto },
|
|
646
|
+
...rec.taker ? { taker: rec.taker.toLowerCase() } : {},
|
|
647
|
+
...rec.min !== void 0 ? { min: rec.min, paid: "0", delivered: "0", fills: [] } : {},
|
|
648
|
+
...rec.bid !== void 0 ? { bid: rec.bid } : {},
|
|
649
|
+
status: "open",
|
|
650
|
+
expiresEpoch: ev.expiresEpoch,
|
|
651
|
+
height: ev.height,
|
|
652
|
+
feeBps: v11 ? FEE_BPS : 0
|
|
653
|
+
};
|
|
654
|
+
offers.set(ev.id, o);
|
|
655
|
+
const linked = rec.bid !== void 0 ? bids.get(rec.bid) : void 0;
|
|
656
|
+
if (linked) linked.offers.push(ev.id);
|
|
657
|
+
note(ev, ev.id, "offer", true);
|
|
658
|
+
} else if (rec.t === "bid") {
|
|
659
|
+
if (!v12) {
|
|
660
|
+
note(ev, ev.id, "bid", false, "bids need v1.2");
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (!Number.isSafeInteger(ev.expiresEpoch)) {
|
|
664
|
+
note(ev, ev.id, "bid", false, "expiresEpoch out of safe-integer range");
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
if (epochOf(ev.height) > ev.expiresEpoch) {
|
|
668
|
+
note(ev, ev.id, "bid", false, "already expired at anchor");
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
bids.set(ev.id, {
|
|
672
|
+
id: ev.id,
|
|
673
|
+
bidder: who,
|
|
674
|
+
want: rec.want,
|
|
675
|
+
give: rec.give,
|
|
676
|
+
status: "open",
|
|
677
|
+
expiresEpoch: ev.expiresEpoch,
|
|
678
|
+
height: ev.height,
|
|
679
|
+
offers: []
|
|
680
|
+
});
|
|
681
|
+
note(ev, ev.id, "bid", true);
|
|
682
|
+
} else if (rec.t === "ocancel") {
|
|
683
|
+
if (!v12) {
|
|
684
|
+
note(ev, ev.id, "ocancel", false, "ocancel needs v1.2");
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
const targets = [];
|
|
688
|
+
for (const o of offers.values()) {
|
|
689
|
+
if (o.status !== "open" || o.seller !== who) continue;
|
|
690
|
+
if (rec.ticker !== void 0 && !(!isNameGive(o.give) && o.give.ticker === rec.ticker)) continue;
|
|
691
|
+
if (rec.name !== void 0 && !(isNameGive(o.give) && o.give.name === rec.name)) continue;
|
|
692
|
+
targets.push(o);
|
|
693
|
+
}
|
|
694
|
+
if (ev.height >= V14_HEIGHT) {
|
|
695
|
+
pendingCancels.push(() => {
|
|
696
|
+
let n = 0;
|
|
697
|
+
for (const o of targets) if (o.status === "open") {
|
|
698
|
+
releaseGive(o);
|
|
699
|
+
o.status = "cancelled";
|
|
700
|
+
n++;
|
|
701
|
+
}
|
|
702
|
+
note(ev, ev.id, "ocancel", true, `${n} cancelled (deferred past same-block fills)`);
|
|
703
|
+
});
|
|
704
|
+
} else {
|
|
705
|
+
for (const o of targets) {
|
|
706
|
+
releaseGive(o);
|
|
707
|
+
o.status = "cancelled";
|
|
708
|
+
}
|
|
709
|
+
note(ev, ev.id, "ocancel", true, `${targets.length} cancelled`);
|
|
710
|
+
}
|
|
711
|
+
} else if (rec.t === "nrenew") {
|
|
712
|
+
if (!v15) {
|
|
713
|
+
note(ev, ev.id, "nrenew", false, "nrenew needs v1.5");
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
const n = names.get(rec.name);
|
|
717
|
+
const ep = epochOf(ev.height);
|
|
718
|
+
if (!n || lapsed(n, ep)) {
|
|
719
|
+
note(ev, ev.id, "nrenew", false, "no live lease (lapsed names are claimed, not renewed)");
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
if (inGrace(n, ep) && who !== n.owner) {
|
|
723
|
+
note(ev, ev.id, "nrenew", false, "grace period: only the owner may renew");
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
if (feeToTreasury < nameRegFee(rec.name)) {
|
|
727
|
+
note(ev, ev.id, "nrenew", false, "renewal fee unpaid");
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
n.paidThroughEpoch = paidThrough(n) + NAME_TERM_EPOCHS;
|
|
731
|
+
feesPaid += nameRegFee(rec.name);
|
|
732
|
+
note(ev, ev.id, "nrenew", true, `paid through epoch ${n.paidThroughEpoch}`);
|
|
733
|
+
} else if (rec.t === "tmeta") {
|
|
734
|
+
if (!v15) {
|
|
735
|
+
note(ev, ev.id, "tmeta", false, "tmeta needs v1.5");
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
const tok = tokens.get(rec.ticker);
|
|
739
|
+
if (!tok) {
|
|
740
|
+
note(ev, ev.id, "tmeta", false, "unknown ticker");
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
if (who !== tok.meta.deployer) {
|
|
744
|
+
note(ev, ev.id, "tmeta", false, "issuer-only metadata");
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
tok.meta.tmeta = rec.hash;
|
|
748
|
+
note(ev, ev.id, "tmeta", true);
|
|
749
|
+
}
|
|
750
|
+
} else {
|
|
751
|
+
const o = offers.get(ev.proposalId);
|
|
752
|
+
const who = ev.attester.toLowerCase();
|
|
753
|
+
if (!o) {
|
|
754
|
+
const b = bids.get(ev.proposalId);
|
|
755
|
+
if (b && ev.score === SCORE_CANCEL) {
|
|
756
|
+
if (who !== b.bidder) {
|
|
757
|
+
note(ev, ev.txid, "bidcancel", false, "only bidder may cancel");
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
if (b.status !== "open") {
|
|
761
|
+
note(ev, ev.txid, "bidcancel", false, `bid ${b.status}`);
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
b.status = "cancelled";
|
|
765
|
+
note(ev, ev.txid, "bidcancel", true);
|
|
766
|
+
}
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
if (ev.score === SCORE_CANCEL) {
|
|
770
|
+
if (who !== o.seller) {
|
|
771
|
+
note(ev, ev.txid, "cancel", false, "only seller may cancel");
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (o.status !== "open") {
|
|
775
|
+
note(ev, ev.txid, "cancel", false, `offer ${o.status}`);
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (ev.height >= V14_HEIGHT) {
|
|
779
|
+
pendingCancels.push(() => {
|
|
780
|
+
if (o.status === "open") {
|
|
781
|
+
releaseGive(o);
|
|
782
|
+
o.status = "cancelled";
|
|
783
|
+
note(ev, ev.txid, "cancel", true);
|
|
784
|
+
} else note(ev, ev.txid, "cancel", false, "superseded by same-block fill (v1.4)");
|
|
785
|
+
});
|
|
786
|
+
} else {
|
|
787
|
+
releaseGive(o);
|
|
788
|
+
o.status = "cancelled";
|
|
789
|
+
note(ev, ev.txid, "cancel", true);
|
|
790
|
+
}
|
|
791
|
+
} else if (ev.score === SCORE_FILL && isTokenWant(o.want)) {
|
|
792
|
+
if (o.status !== "open") {
|
|
793
|
+
note(ev, ev.txid, "fill", false, `offer ${o.status}`);
|
|
794
|
+
continue;
|
|
795
|
+
}
|
|
796
|
+
if (o.taker && who !== o.taker) {
|
|
797
|
+
note(ev, ev.txid, "fill", false, "taker-bound offer");
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
if (ev.confidence !== CONF_TOKEN_FILL) {
|
|
801
|
+
note(ev, ev.txid, "fill", false, "token fill requires confidence marker");
|
|
802
|
+
continue;
|
|
803
|
+
}
|
|
804
|
+
if (!tokens.has(o.want.ticker)) {
|
|
805
|
+
note(ev, ev.txid, "fill", false, "want ticker does not exist");
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
const amt = BigInt(o.want.amount);
|
|
809
|
+
const fee = o.feeBps ? tradeFee(amt) : 0n;
|
|
810
|
+
const buyer = bal(o.want.ticker, who);
|
|
811
|
+
if (buyer.available < amt + fee) {
|
|
812
|
+
note(ev, ev.txid, "fill", false, "insufficient want-token balance");
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
const giveName = isNameGive(o.give) ? names.get(o.give.name) : void 0;
|
|
816
|
+
if (isNameGive(o.give) && !giveName) {
|
|
817
|
+
note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
const giveLock = isNameGive(o.give) ? void 0 : offerLock.get(o.id);
|
|
821
|
+
if (!isNameGive(o.give) && giveLock === void 0) {
|
|
822
|
+
note(ev, ev.txid, "fill", false, "offer lock missing");
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
buyer.available -= amt + fee;
|
|
826
|
+
bal(o.want.ticker, o.want.payto).available += amt;
|
|
827
|
+
if (fee > 0n) bal(o.want.ticker, TREASURY_ADDR).available += fee;
|
|
828
|
+
if (isNameGive(o.give)) {
|
|
829
|
+
giveName.owner = who;
|
|
830
|
+
giveName.locked = false;
|
|
831
|
+
giveName.addr = void 0;
|
|
832
|
+
if (ev.height >= V13_HEIGHT) {
|
|
833
|
+
giveName.effHeight = ev.height;
|
|
834
|
+
giveName.pos = ev.pos;
|
|
835
|
+
giveName.id = ev.txid;
|
|
836
|
+
giveName.height = ev.height;
|
|
837
|
+
giveName.viaFill = true;
|
|
838
|
+
}
|
|
839
|
+
} else {
|
|
840
|
+
bal(o.give.ticker, o.seller).locked -= giveLock;
|
|
841
|
+
bal(o.give.ticker, who).available += giveLock;
|
|
842
|
+
offerLock.delete(o.id);
|
|
843
|
+
}
|
|
844
|
+
o.status = "filled";
|
|
845
|
+
o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: amt.toString(), fee: fee.toString() };
|
|
846
|
+
markBidDone(o, who);
|
|
847
|
+
note(ev, ev.txid, "fill", true);
|
|
848
|
+
} else if (ev.score === SCORE_FILL && o.min !== void 0 && !isNameGive(o.give)) {
|
|
849
|
+
if (o.status !== "open") {
|
|
850
|
+
note(ev, ev.txid, "fill", false, `offer ${o.status}`);
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
if (o.taker && who !== o.taker) {
|
|
854
|
+
note(ev, ev.txid, "fill", false, "taker-bound offer");
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
if (ev.height >= V13_HEIGHT && !o.taker) {
|
|
858
|
+
note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
const pt = ev.paidTo ?? {};
|
|
862
|
+
const want = BigInt(o.want.value);
|
|
863
|
+
const paidSoFar = BigInt(o.paid ?? "0");
|
|
864
|
+
const remaining = want - paidSoFar;
|
|
865
|
+
const X = BigInt(pt[o.want.payto] ?? "0");
|
|
866
|
+
const minV = BigInt(o.min);
|
|
867
|
+
const effMin = remaining < minV ? remaining : minV;
|
|
868
|
+
if (X < effMin) {
|
|
869
|
+
note(ev, ev.txid, "fill", false, "payment below offer min");
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
const x = X < remaining ? X : remaining;
|
|
873
|
+
const fee = o.feeBps ? tradeFee(x) : 0n;
|
|
874
|
+
if (BigInt(pt[TREASURY_ADDR] ?? "0") < fee) {
|
|
875
|
+
note(ev, ev.txid, "fill", false, "protocol fee unpaid");
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
const giveTotal = BigInt(o.give.amount);
|
|
879
|
+
const newPaid = paidSoFar + x;
|
|
880
|
+
const deliveredSoFar = BigInt(o.delivered ?? "0");
|
|
881
|
+
const newDelivered = giveTotal * newPaid / want;
|
|
882
|
+
const out = newDelivered - deliveredSoFar;
|
|
883
|
+
if (out === 0n) {
|
|
884
|
+
note(ev, ev.txid, "fill", false, "fill too small to deliver any tokens");
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
const lock = offerLock.get(o.id);
|
|
888
|
+
if (lock === void 0) {
|
|
889
|
+
note(ev, ev.txid, "fill", false, "offer lock missing");
|
|
890
|
+
continue;
|
|
891
|
+
}
|
|
892
|
+
bal(o.give.ticker, o.seller).locked -= out;
|
|
893
|
+
bal(o.give.ticker, who).available += out;
|
|
894
|
+
offerLock.set(o.id, lock - out);
|
|
895
|
+
o.paid = newPaid.toString();
|
|
896
|
+
o.delivered = newDelivered.toString();
|
|
897
|
+
const entry = { buyer: who, txid: ev.txid, height: ev.height, paid: x.toString(), fee: fee.toString(), got: out.toString() };
|
|
898
|
+
(o.fills ??= []).push(entry);
|
|
899
|
+
feesPaid += fee;
|
|
900
|
+
if (newPaid === want) {
|
|
901
|
+
o.status = "filled";
|
|
902
|
+
o.fill = entry;
|
|
903
|
+
offerLock.delete(o.id);
|
|
904
|
+
markBidDone(o, who);
|
|
905
|
+
}
|
|
906
|
+
note(ev, ev.txid, "fill", true, `partial ${x}/${want}`);
|
|
907
|
+
} else if (ev.score === SCORE_FILL) {
|
|
908
|
+
if (o.status !== "open") {
|
|
909
|
+
note(ev, ev.txid, "fill", false, `offer ${o.status}`);
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
if (o.taker && who !== o.taker) {
|
|
913
|
+
note(ev, ev.txid, "fill", false, "taker-bound offer");
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
if (ev.height >= V13_HEIGHT && !o.taker) {
|
|
917
|
+
note(ev, ev.txid, "fill", false, "v1.3: open CSD-quoted fills disabled (offer must be taker-bound)");
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const pt = ev.paidTo ?? {};
|
|
921
|
+
const want = BigInt(o.want.value);
|
|
922
|
+
const paid = BigInt(pt[o.want.payto] ?? "0");
|
|
923
|
+
if (paid < want) {
|
|
924
|
+
note(ev, ev.txid, "fill", false, "payment below want.value");
|
|
925
|
+
continue;
|
|
926
|
+
}
|
|
927
|
+
const fee = o.feeBps ? tradeFee(want) : 0n;
|
|
928
|
+
const feeGot = BigInt(pt[TREASURY_ADDR] ?? "0");
|
|
929
|
+
if (feeGot < fee) {
|
|
930
|
+
note(ev, ev.txid, "fill", false, "protocol fee unpaid");
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
if (isNameGive(o.give)) {
|
|
934
|
+
const n = names.get(o.give.name);
|
|
935
|
+
if (!n) {
|
|
936
|
+
note(ev, ev.txid, "fill", false, "name vanished (consensus violation)");
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
n.owner = who;
|
|
940
|
+
n.locked = false;
|
|
941
|
+
n.addr = void 0;
|
|
942
|
+
if (ev.height >= V13_HEIGHT) {
|
|
943
|
+
n.effHeight = ev.height;
|
|
944
|
+
n.pos = ev.pos;
|
|
945
|
+
n.id = ev.txid;
|
|
946
|
+
n.height = ev.height;
|
|
947
|
+
n.viaFill = true;
|
|
948
|
+
}
|
|
949
|
+
} else {
|
|
950
|
+
const amt = offerLock.get(o.id);
|
|
951
|
+
if (amt === void 0) {
|
|
952
|
+
note(ev, ev.txid, "fill", false, "offer lock missing");
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
bal(o.give.ticker, o.seller).locked -= amt;
|
|
956
|
+
bal(o.give.ticker, who).available += amt;
|
|
957
|
+
offerLock.delete(o.id);
|
|
958
|
+
}
|
|
959
|
+
feesPaid += fee;
|
|
960
|
+
o.status = "filled";
|
|
961
|
+
o.fill = { buyer: who, txid: ev.txid, height: ev.height, paid: paid.toString(), fee: fee.toString() };
|
|
962
|
+
markBidDone(o, who);
|
|
963
|
+
note(ev, ev.txid, "fill", true);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
applyPendingCancels();
|
|
968
|
+
sweepExpired(tipHeight + 1);
|
|
969
|
+
const tokensOut = {};
|
|
970
|
+
for (const [t, tok] of [...tokens.entries()].sort(([a], [b]) => ord(a, b))) tokensOut[t] = { ...tok.meta, minted: tok.minted.toString() };
|
|
971
|
+
const balancesOut = {};
|
|
972
|
+
for (const [t, m] of [...balances.entries()].sort(([a], [b]) => ord(a, b))) {
|
|
973
|
+
const inner = {};
|
|
974
|
+
for (const [a, b] of [...m.entries()].sort(([x], [y]) => ord(x, y))) {
|
|
975
|
+
if (b.available === 0n && b.locked === 0n) continue;
|
|
976
|
+
inner[a] = { available: b.available.toString(), locked: b.locked.toString() };
|
|
977
|
+
}
|
|
978
|
+
if (Object.keys(inner).length) balancesOut[t] = inner;
|
|
979
|
+
}
|
|
980
|
+
const namesOut = {};
|
|
981
|
+
const tipV15 = tipHeight >= V15_HEIGHT;
|
|
982
|
+
const tipEpoch = epochOf(tipHeight);
|
|
983
|
+
for (const [nm, n] of [...names.entries()].sort(([a], [b]) => ord(a, b))) {
|
|
984
|
+
namesOut[nm] = {
|
|
985
|
+
name: nm,
|
|
986
|
+
owner: n.owner,
|
|
987
|
+
claimId: n.id,
|
|
988
|
+
height: n.height,
|
|
989
|
+
effectiveHeight: n.effHeight,
|
|
990
|
+
locked: n.locked,
|
|
991
|
+
...n.addr ? { addr: n.addr } : {},
|
|
992
|
+
...n.viaFill ? { viaFill: true } : {},
|
|
993
|
+
// lease fields exist only at v1.5+ tips so every pre-v1.5 canonical hash stays pinned
|
|
994
|
+
...tipV15 ? { paidThroughEpoch: paidThrough(n), ...lapsed(n, tipEpoch) ? { expired: true } : {} } : {}
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
const offersOut = {};
|
|
998
|
+
for (const [id, o] of [...offers.entries()].sort(([a], [b]) => ord(a, b))) offersOut[id] = o;
|
|
999
|
+
const bidsOut = {};
|
|
1000
|
+
for (const [id, b] of [...bids.entries()].sort(([a], [b2]) => ord(a, b2))) bidsOut[id] = b;
|
|
1001
|
+
return { tipHeight, tokens: tokensOut, balances: balancesOut, names: namesOut, offers: offersOut, bids: bidsOut, events: log, feesPaid: feesPaid.toString() };
|
|
1002
|
+
}
|
|
1003
|
+
function canonicalState(s) {
|
|
1004
|
+
const sortKeys = (v) => {
|
|
1005
|
+
if (Array.isArray(v)) return v.map(sortKeys);
|
|
1006
|
+
if (v && typeof v === "object") {
|
|
1007
|
+
return Object.fromEntries(Object.entries(v).sort(([a], [b]) => ord(a, b)).map(([k, x]) => [k, sortKeys(x)]));
|
|
1008
|
+
}
|
|
1009
|
+
return v;
|
|
1010
|
+
};
|
|
1011
|
+
const { events: _events, ...data } = s;
|
|
1012
|
+
return JSON.stringify(sortKeys(data));
|
|
1013
|
+
}
|
|
1014
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1015
|
+
0 && (module.exports = {
|
|
1016
|
+
ACTIVATION_HEIGHT,
|
|
1017
|
+
ADDR_RE,
|
|
1018
|
+
AMOUNT_RE,
|
|
1019
|
+
COMMIT_MAX_BLOCKS,
|
|
1020
|
+
CONF_TOKEN_FILL,
|
|
1021
|
+
DEPLOY_FEE,
|
|
1022
|
+
DOMAIN,
|
|
1023
|
+
EPOCH_LEN,
|
|
1024
|
+
FEE_BPS,
|
|
1025
|
+
HASH_RE,
|
|
1026
|
+
MAX_AMOUNT,
|
|
1027
|
+
MAX_RECORD_BYTES,
|
|
1028
|
+
NAME_GRACE_EPOCHS,
|
|
1029
|
+
NAME_PREMIUM_DECAY_EPOCHS,
|
|
1030
|
+
NAME_PREMIUM_START,
|
|
1031
|
+
NAME_RE,
|
|
1032
|
+
NAME_TERM_EPOCHS,
|
|
1033
|
+
RESERVED_NAMES,
|
|
1034
|
+
SCORE_CANCEL,
|
|
1035
|
+
SCORE_FILL,
|
|
1036
|
+
TICKER_RE,
|
|
1037
|
+
TREASURY_ADDR,
|
|
1038
|
+
V11_HEIGHT,
|
|
1039
|
+
V12_HEIGHT,
|
|
1040
|
+
V13_HEIGHT,
|
|
1041
|
+
V14_HEIGHT,
|
|
1042
|
+
V15_HEIGHT,
|
|
1043
|
+
bid,
|
|
1044
|
+
buildRecord,
|
|
1045
|
+
canonicalState,
|
|
1046
|
+
deploy,
|
|
1047
|
+
epochOf,
|
|
1048
|
+
expiredClaimFee,
|
|
1049
|
+
isName,
|
|
1050
|
+
isNameGive,
|
|
1051
|
+
isTokenWant,
|
|
1052
|
+
mint,
|
|
1053
|
+
nameClaim,
|
|
1054
|
+
nameCommit,
|
|
1055
|
+
nameCommitRecord,
|
|
1056
|
+
nameRegFee,
|
|
1057
|
+
nameRenew,
|
|
1058
|
+
nameSet,
|
|
1059
|
+
nameXfer,
|
|
1060
|
+
offer,
|
|
1061
|
+
offerCancelAll,
|
|
1062
|
+
parseAmount,
|
|
1063
|
+
parseRecord,
|
|
1064
|
+
resolve,
|
|
1065
|
+
tokenMeta,
|
|
1066
|
+
tradeFee,
|
|
1067
|
+
transfer
|
|
1068
|
+
});
|