@clonegod/ttd-bsc-common 3.1.46 → 3.1.49

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.
@@ -29,6 +29,7 @@ export declare class BscEnvArgs extends EnvArgs {
29
29
  log_max_size_mb: number;
30
30
  log_check_interval_min: number;
31
31
  log_dir: string;
32
+ block_stale_ms: number;
32
33
  chain_name: string;
33
34
  caller_balance_check_interval: string;
34
35
  caller_balance_low_threshold: string;
@@ -33,6 +33,7 @@ const constants_1 = require("../common/constants");
33
33
  log_max_size_mb: { env: 'LOG_MAX_SIZE_MB', type: 'number', default: 20, desc: '日志文件大小上限(MB)' },
34
34
  log_check_interval_min: { env: 'LOG_CHECK_INTERVAL_MIN', type: 'number', default: 10, desc: '日志检查间隔(分钟)' },
35
35
  log_dir: { env: 'LOG_DIR', type: 'string', default: 'logs', desc: '日志目录' },
36
+ block_stale_ms: { env: 'BLOCK_STALE_MS', type: 'number', default: 5000, desc: '收不到 block 多久视为 stale 主动重连(ms)。BSC 450ms 块设 5000;ETH 12s 块需调到 30000+;SOL 400ms slot 设 5000。' },
36
37
  chain_name: { env: 'CHAIN_NAME', type: 'string', default: '', desc: '链名称(Redis key 前缀),stream-trade 建议必填' },
37
38
  caller_balance_check_interval: { env: 'CALLER_BALANCE_CHECK_INTERVAL', type: 'string', default: '1h', desc: 'caller 余额检查周期,支持 8h/30m/45s/100ms 格式' },
38
39
  caller_balance_low_threshold: { env: 'CALLER_BALANCE_LOW_THRESHOLD', type: 'string', default: '0.01', desc: 'caller 低余额阈值(原生币单位),低于报 ERROR' },
@@ -35,6 +35,7 @@ export declare class QuotePriceVerify {
35
35
  private quoteCache;
36
36
  private reserveCache;
37
37
  private get enabled();
38
+ seedReserves(poolAddress: string, reserve0: string, reserve1: string): void;
38
39
  cacheQuote(poolAddress: string, priceId: string, source: string, askPrice: number, bidPrice: number, blockNumber: number, quoteAmountUsd: number, token0PriceUsd?: number, token1PriceUsd?: number): void;
39
40
  checkSwap(params: CheckSwapParams): void;
40
41
  checkSync(params: CheckSyncParams): void;
@@ -11,12 +11,25 @@ class QuotePriceVerify {
11
11
  get enabled() {
12
12
  return process.env.VERIFY_QUOTE_PRICE === 'true';
13
13
  }
14
+ seedReserves(poolAddress, reserve0, reserve1) {
15
+ if (!this.enabled)
16
+ return;
17
+ try {
18
+ this.reserveCache.set(poolAddress, {
19
+ reserve0: BigInt(reserve0),
20
+ reserve1: BigInt(reserve1),
21
+ });
22
+ }
23
+ catch { }
24
+ }
14
25
  cacheQuote(poolAddress, priceId, source, askPrice, bidPrice, blockNumber, quoteAmountUsd, token0PriceUsd = 0, token1PriceUsd = 0) {
15
26
  if (!this.enabled)
16
27
  return;
17
28
  this.quoteCache.set(poolAddress, {
18
- priceId, source, askPrice, bidPrice, blockNumber, quoteAmountUsd,
19
- verifiedBlock: 0,
29
+ priceId, source, askPrice, bidPrice,
30
+ referenceBlock: blockNumber,
31
+ verifiedReferenceBlock: 0,
32
+ quoteAmountUsd,
20
33
  token0PriceUsd, token1PriceUsd,
21
34
  });
22
35
  }
@@ -28,11 +41,11 @@ class QuotePriceVerify {
28
41
  const cached = this.quoteCache.get(poolAddress);
29
42
  if (!cached)
30
43
  return;
31
- if (blockNumber <= cached.blockNumber)
44
+ if (blockNumber !== cached.referenceBlock + 1)
32
45
  return;
33
- if (blockNumber <= cached.verifiedBlock)
46
+ if (cached.verifiedReferenceBlock === cached.referenceBlock)
34
47
  return;
35
- cached.verifiedBlock = blockNumber;
48
+ cached.verifiedReferenceBlock = cached.referenceBlock;
36
49
  const amt0 = BigInt(amount0);
37
50
  const amt1 = BigInt(amount1);
38
51
  if (amt0 === 0n && amt1 === 0n)
@@ -66,9 +79,9 @@ class QuotePriceVerify {
66
79
  const cached = this.quoteCache.get(poolAddress);
67
80
  if (!cached)
68
81
  return;
69
- if (blockNumber <= cached.blockNumber)
82
+ if (blockNumber !== cached.referenceBlock + 1)
70
83
  return;
71
- if (blockNumber <= cached.verifiedBlock)
84
+ if (cached.verifiedReferenceBlock === cached.referenceBlock)
72
85
  return;
73
86
  const newR0 = BigInt(reserve0);
74
87
  const newR1 = BigInt(reserve1);
@@ -81,7 +94,7 @@ class QuotePriceVerify {
81
94
  if ((delta0 > 0n && delta1 > 0n) || (delta0 < 0n && delta1 < 0n) || (delta0 === 0n && delta1 === 0n)) {
82
95
  return;
83
96
  }
84
- cached.verifiedBlock = blockNumber;
97
+ cached.verifiedReferenceBlock = cached.referenceBlock;
85
98
  let inputTokenAddress;
86
99
  let outputTokenAddress;
87
100
  let inputAmountUi;
@@ -133,7 +146,7 @@ class QuotePriceVerify {
133
146
  ? `${inputAmountUi.toFixed(4)} ${quoteToken.symbol} -> ${outputAmountUi.toFixed(4)} ${baseToken.symbol}`
134
147
  : `${inputAmountUi.toFixed(4)} ${baseToken.symbol} -> ${outputAmountUi.toFixed(4)} ${quoteToken.symbol}`;
135
148
  const quoteSrc = cached.source || '?';
136
- const quoteTag = `quote[${quoteSrc} blk:${cached.blockNumber}]`;
149
+ const quoteTag = `quote[${quoteSrc} blk:${cached.referenceBlock}]`;
137
150
  const swapBlk = swapBlockNumber || '?';
138
151
  const swapTx = txHash ? ` ${txHash.slice(0, 10)}` : '';
139
152
  const swapTag = `swap[blk:${swapBlk}${swapTx}]`;
@@ -145,7 +158,7 @@ class QuotePriceVerify {
145
158
  pool_address: poolAddress,
146
159
  pool_name: poolName,
147
160
  price_id: cached.priceId,
148
- quote_block: cached.blockNumber,
161
+ quote_block: cached.referenceBlock,
149
162
  swap_block: swapBlockNumber,
150
163
  tx_hash: txHash || '',
151
164
  side,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "3.1.46",
3
+ "version": "3.1.49",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",
@@ -14,7 +14,7 @@
14
14
  "push": "npm run build && npm publish"
15
15
  },
16
16
  "dependencies": {
17
- "@clonegod/ttd-core": "3.1.46",
17
+ "@clonegod/ttd-core": "3.1.48",
18
18
  "axios": "1.15.0",
19
19
  "dotenv": "^16.4.7",
20
20
  "ethers": "^5.8.0",