@clonegod/ttd-bsc-common 3.0.48 → 3.0.50
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.
|
@@ -2,6 +2,7 @@ export interface CheckSwapParams {
|
|
|
2
2
|
poolAddress: string;
|
|
3
3
|
poolName: string;
|
|
4
4
|
blockNumber: number;
|
|
5
|
+
txHash: string;
|
|
5
6
|
amount0: string;
|
|
6
7
|
amount1: string;
|
|
7
8
|
token0Address: string;
|
|
@@ -33,8 +34,10 @@ export interface CheckSyncParams {
|
|
|
33
34
|
export declare class QuoteAccuracyChecker {
|
|
34
35
|
private quoteCache;
|
|
35
36
|
private reserveCache;
|
|
37
|
+
private pendingLogs;
|
|
36
38
|
private get enabled();
|
|
37
|
-
|
|
39
|
+
getAndClearLog(poolAddress: string): string | null;
|
|
40
|
+
cacheQuote(poolAddress: string, priceId: string, source: string, askPrice: number, bidPrice: number, blockNumber: number, quoteAmountUsd: number, token0PriceUsd?: number, token1PriceUsd?: number): void;
|
|
38
41
|
checkSwap(params: CheckSwapParams): void;
|
|
39
42
|
checkSync(params: CheckSyncParams): void;
|
|
40
43
|
private compareAndLog;
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QuoteAccuracyChecker = void 0;
|
|
4
|
-
const ttd_core_1 = require("@clonegod/ttd-core");
|
|
5
4
|
const trade_direction_1 = require("../../utils/trade_direction");
|
|
6
5
|
class QuoteAccuracyChecker {
|
|
7
6
|
constructor() {
|
|
8
7
|
this.quoteCache = new Map();
|
|
9
8
|
this.reserveCache = new Map();
|
|
9
|
+
this.pendingLogs = new Map();
|
|
10
10
|
}
|
|
11
11
|
get enabled() {
|
|
12
12
|
return process.env.QUOTE_ACCURACY_LOG === 'true';
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
getAndClearLog(poolAddress) {
|
|
15
|
+
const log = this.pendingLogs.get(poolAddress);
|
|
16
|
+
if (log)
|
|
17
|
+
this.pendingLogs.delete(poolAddress);
|
|
18
|
+
return log || null;
|
|
19
|
+
}
|
|
20
|
+
cacheQuote(poolAddress, priceId, source, askPrice, bidPrice, blockNumber, quoteAmountUsd, token0PriceUsd = 0, token1PriceUsd = 0) {
|
|
15
21
|
if (!this.enabled)
|
|
16
22
|
return;
|
|
17
23
|
this.quoteCache.set(poolAddress, {
|
|
18
|
-
priceId, askPrice, bidPrice, blockNumber, quoteAmountUsd,
|
|
24
|
+
priceId, source, askPrice, bidPrice, blockNumber, quoteAmountUsd,
|
|
19
25
|
verifiedBlock: 0,
|
|
20
26
|
token0PriceUsd, token1PriceUsd,
|
|
21
27
|
});
|
|
@@ -23,7 +29,7 @@ class QuoteAccuracyChecker {
|
|
|
23
29
|
checkSwap(params) {
|
|
24
30
|
if (!this.enabled)
|
|
25
31
|
return;
|
|
26
|
-
const { poolAddress, poolName, blockNumber, amount0, amount1, poolInfo } = params;
|
|
32
|
+
const { poolAddress, poolName, blockNumber, txHash, amount0, amount1, poolInfo } = params;
|
|
27
33
|
const { token0Address, token1Address, token0Decimals, token1Decimals } = params;
|
|
28
34
|
const cached = this.quoteCache.get(poolAddress);
|
|
29
35
|
if (!cached)
|
|
@@ -56,7 +62,7 @@ class QuoteAccuracyChecker {
|
|
|
56
62
|
else {
|
|
57
63
|
return;
|
|
58
64
|
}
|
|
59
|
-
this.compareAndLog(poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, cached.token0PriceUsd, cached.token1PriceUsd, token0Address);
|
|
65
|
+
this.compareAndLog(poolAddress, poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, cached.token0PriceUsd, cached.token1PriceUsd, token0Address, txHash);
|
|
60
66
|
}
|
|
61
67
|
checkSync(params) {
|
|
62
68
|
if (!this.enabled)
|
|
@@ -98,9 +104,9 @@ class QuoteAccuracyChecker {
|
|
|
98
104
|
inputAmountUi = Number(delta1) / Math.pow(10, token1Decimals);
|
|
99
105
|
outputAmountUi = Number(-delta0) / Math.pow(10, token0Decimals);
|
|
100
106
|
}
|
|
101
|
-
this.compareAndLog(poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, cached.token0PriceUsd, cached.token1PriceUsd, token0Address);
|
|
107
|
+
this.compareAndLog(poolAddress, poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, cached.token0PriceUsd, cached.token1PriceUsd, token0Address);
|
|
102
108
|
}
|
|
103
|
-
compareAndLog(poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, token0PriceUsd, token1PriceUsd, token0Address) {
|
|
109
|
+
compareAndLog(poolAddress, poolName, poolInfo, cached, inputTokenAddress, outputTokenAddress, inputAmountUi, outputAmountUi, token0PriceUsd, token1PriceUsd, token0Address, txHash) {
|
|
104
110
|
if (inputAmountUi <= 0 || outputAmountUi <= 0)
|
|
105
111
|
return;
|
|
106
112
|
let swapUsd = 0;
|
|
@@ -110,11 +116,6 @@ class QuoteAccuracyChecker {
|
|
|
110
116
|
? inputAmountUi * token0PriceUsd
|
|
111
117
|
: inputAmountUi * token1PriceUsd;
|
|
112
118
|
}
|
|
113
|
-
const minUsd = cached.quoteAmountUsd * 0.5;
|
|
114
|
-
const maxUsd = cached.quoteAmountUsd * 2;
|
|
115
|
-
if (swapUsd > 0 && (swapUsd < minUsd || swapUsd > maxUsd)) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
119
|
const direction = (0, trade_direction_1.resolveTradeDirection)(poolInfo, true);
|
|
119
120
|
const baseToken = direction.baseToken;
|
|
120
121
|
const quoteToken = direction.quoteToken;
|
|
@@ -130,10 +131,17 @@ class QuoteAccuracyChecker {
|
|
|
130
131
|
const absDiffBps = Math.abs(diffBps);
|
|
131
132
|
const status = absDiffBps < 10 ? '✅' : absDiffBps < 30 ? '⚠️' : '❌';
|
|
132
133
|
const usdStr = swapUsd > 0 ? `$${swapUsd.toFixed(0)}` : '$?';
|
|
134
|
+
const minUsd = cached.quoteAmountUsd * 0.5;
|
|
135
|
+
const maxUsd = cached.quoteAmountUsd * 2;
|
|
136
|
+
const inRange = swapUsd <= 0 || (swapUsd >= minUsd && swapUsd <= maxUsd);
|
|
137
|
+
const rangeTag = inRange ? '' : ' [out]';
|
|
133
138
|
const tradeFlow = isBuy
|
|
134
|
-
? `${inputAmountUi.toFixed(
|
|
135
|
-
: `${inputAmountUi.toFixed(
|
|
136
|
-
|
|
139
|
+
? `${inputAmountUi.toFixed(4)} ${quoteToken.symbol} -> ${outputAmountUi.toFixed(4)} ${baseToken.symbol}`
|
|
140
|
+
: `${inputAmountUi.toFixed(4)} ${baseToken.symbol} -> ${outputAmountUi.toFixed(4)} ${quoteToken.symbol}`;
|
|
141
|
+
const txTag = txHash ? ` tx:${txHash.slice(0, 10)}` : '';
|
|
142
|
+
const srcTag = cached.source ? ` [${cached.source}]` : '';
|
|
143
|
+
const msg = ` ↳ [QuoteAccuracy] vs blk:${cached.blockNumber}${srcTag} ${side} ${usdStr} (${tradeFlow}), ${isBuy ? 'ask' : 'bid'}=${refPrice.toFixed(12)} vs exec=${execPriceNum.toFixed(12)}, diff=${diffBps > 0 ? '+' : ''}${diffBps.toFixed(1)}bps ${status}${rangeTag}${txTag}`;
|
|
144
|
+
this.pendingLogs.set(poolAddress, msg);
|
|
137
145
|
}
|
|
138
146
|
}
|
|
139
147
|
exports.QuoteAccuracyChecker = QuoteAccuracyChecker;
|