@clonegod/ttd-core 3.1.79 → 3.1.81
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/quote/depth_tier.d.ts +0 -6
- package/dist/quote/depth_tier.js +0 -32
- package/dist/token/price/get_base_token_price.d.ts +5 -0
- package/dist/token/price/get_base_token_price.js +113 -0
- package/dist/token/price/index.d.ts +1 -0
- package/dist/token/price/index.js +1 -0
- package/package.json +1 -1
|
@@ -39,9 +39,3 @@ 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;
|
package/dist/quote/depth_tier.js
CHANGED
|
@@ -3,7 +3,6 @@ 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;
|
|
7
6
|
function toOrderbookEntry(q) {
|
|
8
7
|
return {
|
|
9
8
|
price: q.price,
|
|
@@ -35,34 +34,3 @@ function getDepthPricePctLevels() {
|
|
|
35
34
|
_pctLevelsCache = levels;
|
|
36
35
|
return levels;
|
|
37
36
|
}
|
|
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
|
-
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.get_base_token_price_info = get_base_token_price_info;
|
|
13
|
+
require('dotenv').config();
|
|
14
|
+
const index_1 = require("../../index");
|
|
15
|
+
const defi_llama_1 = require("./defi_llama");
|
|
16
|
+
const gecko_terminal_1 = require("./gecko_terminal");
|
|
17
|
+
const price_cache_1 = require("./price_cache");
|
|
18
|
+
const ZERO_ADDR = '0x0000000000000000000000000000000000000000';
|
|
19
|
+
const WETH_ADDR = '0x4200000000000000000000000000000000000006';
|
|
20
|
+
function get_base_token_price_info(addresses, opts) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
var _a;
|
|
23
|
+
const source = (_a = opts === null || opts === void 0 ? void 0 : opts.source) !== null && _a !== void 0 ? _a : 'cache_only';
|
|
24
|
+
addresses = addresses.map(addr => addr.toLowerCase());
|
|
25
|
+
const wantsNativeEth = addresses.includes(ZERO_ADDR);
|
|
26
|
+
if (wantsNativeEth) {
|
|
27
|
+
addresses = addresses.map(a => a === ZERO_ADDR ? WETH_ADDR : a);
|
|
28
|
+
addresses = Array.from(new Set(addresses));
|
|
29
|
+
}
|
|
30
|
+
const result = new Map();
|
|
31
|
+
const cachedChannel = {
|
|
32
|
+
name: 'CachedPrice',
|
|
33
|
+
fetchFn: price_cache_1.fetchPriceFromCache,
|
|
34
|
+
batchSize: 100,
|
|
35
|
+
batchDelay: 1000,
|
|
36
|
+
};
|
|
37
|
+
const externalChannels = [
|
|
38
|
+
{
|
|
39
|
+
name: 'DefiLlama',
|
|
40
|
+
fetchFn: (address_list) => (0, defi_llama_1.fetchPriceFromDefiLlama)(index_1.CHAIN_ID.BASE, address_list),
|
|
41
|
+
batchSize: 10,
|
|
42
|
+
batchDelay: 1000,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'GeckoTerminal',
|
|
46
|
+
fetchFn: (address_list) => (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)(index_1.CHAIN_ID.BASE, address_list),
|
|
47
|
+
batchSize: 10,
|
|
48
|
+
batchDelay: 1000,
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
const PRICE_CHANNELS = source === 'cache_only' ? [cachedChannel] :
|
|
52
|
+
source === 'cache_first' ? [cachedChannel, ...externalChannels] :
|
|
53
|
+
[...externalChannels];
|
|
54
|
+
const externalHits = [];
|
|
55
|
+
try {
|
|
56
|
+
for (const channel of PRICE_CHANNELS) {
|
|
57
|
+
if (addresses.length === 0)
|
|
58
|
+
break;
|
|
59
|
+
(0, index_1.log_debug)(`[get_base_token_price_info] Processing ${addresses.length} tokens using ${channel.name}`);
|
|
60
|
+
const batches = (0, index_1.chunkArray)(addresses, channel.batchSize);
|
|
61
|
+
let remainingAddresses = [...addresses];
|
|
62
|
+
for (let i = 0; i < batches.length; i++) {
|
|
63
|
+
const batch = batches[i];
|
|
64
|
+
if (batch.length === 0)
|
|
65
|
+
continue;
|
|
66
|
+
try {
|
|
67
|
+
const channelResult = yield channel.fetchFn(batch);
|
|
68
|
+
for (const [address, priceInfo] of channelResult.entries()) {
|
|
69
|
+
result.set(address, priceInfo);
|
|
70
|
+
remainingAddresses = remainingAddresses.filter(addr => addr !== address);
|
|
71
|
+
if (channel.name !== 'CachedPrice') {
|
|
72
|
+
externalHits.push({ address, price: priceInfo.price, source: channel.name });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
(0, index_1.log_warn)(`[get_base_token_price_info] Error processing batch ${i + 1} with ${channel.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
78
|
+
}
|
|
79
|
+
if (i < batches.length - 1) {
|
|
80
|
+
yield (0, index_1.sleep)(channel.batchDelay);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
addresses = [...remainingAddresses];
|
|
84
|
+
if (addresses.length === 0)
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
if (externalHits.length > 0) {
|
|
88
|
+
yield (0, price_cache_1.cache_new_market_price_batch)(externalHits);
|
|
89
|
+
}
|
|
90
|
+
if (source === 'cache_only' && addresses.length > 0) {
|
|
91
|
+
(0, price_cache_1.warnPriceCacheMiss)(addresses);
|
|
92
|
+
}
|
|
93
|
+
if (source === 'force_fetch' && result.size === 0) {
|
|
94
|
+
throw new Error(`Unable to get price information for any token: ${addresses.join(', ')}`);
|
|
95
|
+
}
|
|
96
|
+
if (source !== 'cache_only' && addresses.length > 0) {
|
|
97
|
+
(0, index_1.log_warn)(`[get_base_token_price_info] Failed to get prices for ${addresses.length} tokens after trying all channels: ${addresses.join(', ')}`);
|
|
98
|
+
}
|
|
99
|
+
if (wantsNativeEth) {
|
|
100
|
+
const wethInfo = result.get(WETH_ADDR);
|
|
101
|
+
if (wethInfo)
|
|
102
|
+
result.set(ZERO_ADDR, wethInfo);
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (source === 'force_fetch') {
|
|
108
|
+
throw new Error(`Failed to get token price information: ${error instanceof Error ? error.message : String(error)}`);
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./get_bsc_token_price"), exports);
|
|
18
|
+
__exportStar(require("./get_base_token_price"), exports);
|
|
18
19
|
__exportStar(require("./get_eth_token_price"), exports);
|
|
19
20
|
__exportStar(require("./get_solana_token_price"), exports);
|
|
20
21
|
__exportStar(require("./get_tron_token_price"), exports);
|