@clonegod/ttd-core 3.1.77 → 3.1.78

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.
@@ -39,3 +39,9 @@ export interface OrderbookEntry {
39
39
  export declare function toOrderbookEntry(q: QuoteEntry): OrderbookEntry;
40
40
  export declare const DEFAULT_TIER_PCT = 0.2;
41
41
  export declare function getDepthPricePctLevels(): number[];
42
+ export declare function assertTierPriceInvariant(poolName: string, side: 'ASK' | 'BID', tiers: Array<{
43
+ pct: number;
44
+ price: number;
45
+ amount: number;
46
+ amount_in: number;
47
+ }>, log_warn?: (msg: string) => void): void;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEFAULT_TIER_PCT = void 0;
4
4
  exports.toOrderbookEntry = toOrderbookEntry;
5
5
  exports.getDepthPricePctLevels = getDepthPricePctLevels;
6
+ exports.assertTierPriceInvariant = assertTierPriceInvariant;
6
7
  function toOrderbookEntry(q) {
7
8
  return {
8
9
  price: q.price,
@@ -34,3 +35,34 @@ function getDepthPricePctLevels() {
34
35
  _pctLevelsCache = levels;
35
36
  return levels;
36
37
  }
38
+ const _tierInvariantLastWarn = new Map();
39
+ function assertTierPriceInvariant(poolName, side, tiers, log_warn) {
40
+ if (!tiers || tiers.length === 0)
41
+ return;
42
+ const TOL_REL = 0.0001;
43
+ for (const t of tiers) {
44
+ if (!(t.price > 0) || !(t.amount > 0) || !(t.amount_in > 0))
45
+ continue;
46
+ const expected = side === 'ASK'
47
+ ? t.amount_in / t.amount
48
+ : t.amount / t.amount_in;
49
+ if (expected <= 0)
50
+ continue;
51
+ const relDiff = Math.abs(t.price - expected) / expected;
52
+ if (relDiff > TOL_REL) {
53
+ const key = `${poolName}_${side}_${t.pct}`;
54
+ const now = Date.now();
55
+ if ((now - (_tierInvariantLastWarn.get(key) || 0)) < 60000)
56
+ return;
57
+ _tierInvariantLastWarn.set(key, now);
58
+ const msg = `[tier-invariant] ${poolName} ${side} pct=${t.pct} price=${t.price.toFixed(10)} ` +
59
+ `≠ expected ${expected.toFixed(10)} (relDiff=${(relDiff * 10000).toFixed(2)}bps, amount_in=${t.amount_in}, amount=${t.amount}) ` +
60
+ `— check tier.price formula! Must be effective avg price including fee`;
61
+ if (log_warn)
62
+ log_warn(msg);
63
+ else
64
+ console.warn(msg);
65
+ return;
66
+ }
67
+ }
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "3.1.77",
3
+ "version": "3.1.78",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",