Initial commit
This commit is contained in:
66
scripts/check-ranger-mappers.js
Normal file
66
scripts/check-ranger-mappers.js
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* ponytail: assert ranger mapper invariants (standalone — CRA ESM can't require()).
|
||||
* Keep in sync with src/lib/ranger/{helpers,mapTickers,depth}.js
|
||||
* Run: node scripts/check-ranger-mappers.js
|
||||
*/
|
||||
const assert = require("assert");
|
||||
|
||||
function generateSocketURI(baseUrl, streams) {
|
||||
const sorted = [...(streams || []).filter(Boolean)].sort();
|
||||
return `${String(baseUrl).replace(/\/+$/, "")}/?stream=${sorted.join("&stream=")}`;
|
||||
}
|
||||
|
||||
function marketIdToShahooStatKeys(marketId) {
|
||||
const id = String(marketId || "").toLowerCase();
|
||||
const match = id.match(/^([a-z0-9]+?)(cad|irt|usdt)$/);
|
||||
if (!match) return [];
|
||||
return [`${match[1].toUpperCase()}_${match[2].toUpperCase()}`];
|
||||
}
|
||||
|
||||
function shahooMarketToDenaId(marketKeyOrRoute) {
|
||||
const normalized = String(marketKeyOrRoute || "")
|
||||
.replace(/-/g, "_")
|
||||
.toUpperCase();
|
||||
const [base, quote] = normalized.split("_");
|
||||
if (!base || !quote) return null;
|
||||
return `${base.toLowerCase()}${quote.toLowerCase()}`;
|
||||
}
|
||||
|
||||
function marketPublicStreams(marketId, { incremental = true } = {}) {
|
||||
const id = String(marketId || "").toLowerCase();
|
||||
if (!id) return [];
|
||||
const depth = incremental ? `${id}.ob-inc` : `${id}.update`;
|
||||
return [`${id}.trades`, depth];
|
||||
}
|
||||
|
||||
function handleIncrementalUpdate(depthOld, newLevel, type) {
|
||||
const prev = depthOld || [];
|
||||
const index = prev.findIndex(([price]) => +price === +newLevel[0]);
|
||||
if (index === -1 && +newLevel[1]) {
|
||||
const data = [...prev, newLevel];
|
||||
return type === "asks"
|
||||
? data.sort((a, b) => +a[0] - +b[0])
|
||||
: data.sort((a, b) => +b[0] - +a[0]);
|
||||
}
|
||||
const result = [...prev];
|
||||
if (Number(newLevel[1]) !== 0) result[index] = newLevel;
|
||||
else result.splice(index, 1);
|
||||
return type === "asks"
|
||||
? result.sort((a, b) => +a[0] - +b[0])
|
||||
: result.sort((a, b) => +b[0] - +a[0]);
|
||||
}
|
||||
|
||||
assert.deepStrictEqual(marketIdToShahooStatKeys("btcirt"), ["BTC_IRT"]);
|
||||
assert.strictEqual(shahooMarketToDenaId("BTC-IRT"), "btcirt");
|
||||
assert.deepStrictEqual(marketPublicStreams("btcirt"), [
|
||||
"btcirt.trades",
|
||||
"btcirt.ob-inc",
|
||||
]);
|
||||
const uri = generateSocketURI("wss://x/public", ["b", "a"]);
|
||||
assert.ok(uri.endsWith("?stream=a&stream=b"));
|
||||
assert.deepStrictEqual(
|
||||
handleIncrementalUpdate([["100", "1"]], ["100", "0"], "asks"),
|
||||
[]
|
||||
);
|
||||
|
||||
console.log("check-ranger-mappers: ok");
|
||||
Reference in New Issue
Block a user