diff --git a/.env.example b/.env.example index 2a49782d..9d1b1f46 100644 --- a/.env.example +++ b/.env.example @@ -67,11 +67,13 @@ USDT_TRC20_CONTRACT= # BSC (USDT BEP20) — public Binance dataseed often rejects eth_getLogs (-32005 limit exceeded). # Balances/receipts still work on dataseed; getLogs uses BSC_RPC_FALLBACKS automatically. BSC_RPC_URL=https://bsc-dataseed.binance.org -BSC_RPC_FALLBACKS=https://1rpc.io/bnb,https://rpc-bsc.48.club +BSC_RPC_FALLBACKS=https://rpc-bsc.48.club,https://1rpc.io/bnb BSC_CONFIRMATIONS=15 # Leave empty to use official USDT BEP20 from DB (0x55d3…7955) USDT_BEP20_CONTRACT= BSC_GETLOGS_BLOCK_RANGE=2000 +# Unpaid HD catch-up lookback (~3s/block). 200000 ≈ 7 days — must cover deposits missed during RPC outages. +BSC_UNPAID_LOOKBACK_BLOCKS=200000 # Optional Etherscan API V2 key (BSC may require a paid chain plan). Not required if RPC fallbacks work. # BSCSCAN_API_KEY= # Optional Etherscan-compatible tokentx base URL (leave empty unless you have a working host) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 94e70ca5..cc2f6bda 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -105,7 +105,7 @@ export const config = { * Extra RPCs tried for eth_getLogs when the primary returns -32005 / limit exceeded. * Public Binance dataseeds often ban getLogs; 1rpc / 48.club usually still work. */ - rpcFallbacks: (process.env.BSC_RPC_FALLBACKS || 'https://1rpc.io/bnb,https://rpc-bsc.48.club') + rpcFallbacks: (process.env.BSC_RPC_FALLBACKS || 'https://rpc-bsc.48.club,https://1rpc.io/bnb') .split(',') .map((s) => s.trim()) .filter(Boolean), @@ -127,10 +127,11 @@ export const config = { 10 ), /** - * When HD still holds USDT (missed credit), rescan only this many recent blocks - * via getLogs — not the full 7-day window (public RPCs time out). + * When HD still holds USDT (missed credit), rescan this many recent blocks via getLogs. + * Must be wide enough for deposits missed while public RPCs were rate-limiting + * (≈3s/block → 200000 ≈ 7 days). Used as floor with depositInitialLookbackBlocks. */ - unpaidLookbackBlocks: parseInt(process.env.BSC_UNPAID_LOOKBACK_BLOCKS || '30000', 10), + unpaidLookbackBlocks: parseInt(process.env.BSC_UNPAID_LOOKBACK_BLOCKS || '200000', 10), /** Optional Etherscan V2 / BscScan key for unpaid deposit fallback. */ explorerApiKey: process.env.BSCSCAN_API_KEY || process.env.ETHERSCAN_API_KEY || '', /** diff --git a/backend/src/services/blockchain/bscService.ts b/backend/src/services/blockchain/bscService.ts index 0bfe42f8..c8a0250a 100644 --- a/backend/src/services/blockchain/bscService.ts +++ b/backend/src/services/blockchain/bscService.ts @@ -21,8 +21,16 @@ const RPC_TIMEOUT_MS = 60_000; const MIN_GETLOGS_BLOCK_RANGE = 10; /** Pause between getLogs chunks / wallets to reduce public-RPC bans. */ const GETLOGS_GAP_MS = 350; +/** Skip RPCs that recently returned rate-limit / 403 (ms). */ +const RPC_COOLDOWN_MS = 10 * 60 * 1000; const providerByUrl = new Map(); +const rpcCooldownUntil = new Map(); + +function markRpcCooldown(url: string, reason: string): void { + rpcCooldownUntil.set(url, Date.now() + RPC_COOLDOWN_MS); + console.warn(`[bsc] cooldown ${RPC_COOLDOWN_MS / 1000}s for ${url} (${reason.slice(0, 80)})`); +} function providerFor(url: string): ethers.JsonRpcProvider { const key = url.trim(); @@ -44,16 +52,20 @@ function getProvider(): ethers.JsonRpcProvider { return providerFor(config.blockchain.bsc.rpcUrl); } -/** Primary RPC + configured fallbacks (deduped) for eth_getLogs only. */ +/** Primary RPC + configured fallbacks (deduped) for eth_getLogs only. Cool RPC last. */ function getLogsRpcUrls(): string[] { const primary = config.blockchain.bsc.rpcUrl.trim(); const fallbacks = config.blockchain.bsc.rpcFallbacks; - const out: string[] = []; + const all: string[] = []; for (const u of [primary, ...fallbacks]) { const t = u.trim(); - if (t && !out.includes(t)) out.push(t); + if (t && !all.includes(t)) all.push(t); } - return out; + const now = Date.now(); + const ready = all.filter((u) => (rpcCooldownUntil.get(u) ?? 0) <= now); + const cooling = all.filter((u) => (rpcCooldownUntil.get(u) ?? 0) > now); + // Prefer ready RPCs; only retry cooled ones if nothing else is left. + return ready.length > 0 ? ready : cooling; } export function resolveUsdtContract(dbContract: string | null | undefined): string { @@ -197,15 +209,20 @@ export async function getUsdtTransfersToAddress( const url = urls[i]!; try { const hits = await queryUsdtTransfersOnRpc(url, toAddress, contractAddress, fromBlock, endCap); - if (i > 0) { + if (url !== config.blockchain.bsc.rpcUrl.trim()) { console.warn( - `[bsc] eth_getLogs succeeded via fallback RPC #${i} (${url}) for ${toAddress}` + `[bsc] eth_getLogs ok via ${url} for ${toAddress} hits=${hits.length} blocks=${fromBlock}-${endCap}` + ); + } else if (hits.length > 0) { + console.log( + `[bsc] eth_getLogs ok via primary for ${toAddress} hits=${hits.length} blocks=${fromBlock}-${endCap}` ); } return hits; } catch (err) { lastErr = err; const msg = err instanceof Error ? err.message : String(err); + markRpcCooldown(url, msg); console.warn( `[bsc] eth_getLogs failed on ${url} for ${toAddress}: ${msg.slice(0, 180)} — trying next RPC` ); @@ -216,6 +233,43 @@ export async function getUsdtTransfersToAddress( : new Error(`eth_getLogs failed on all RPCs for ${toAddress}`); } +/** + * Newest-first getLogs for unpaid HDs: stop once cumulative Transfer value covers on-chain balance. + * Avoids scanning a full 7-day window when the deposit is only ~1–2 days old. + */ +export async function getUsdtTransfersToAddressUntilValue( + toAddress: string, + contractAddress: string, + fromBlock: number, + maxEndBlock: number, + minValue: bigint +): Promise { + const tipProvider = getProvider(); + const currentBlock = await tipProvider.getBlockNumber(); + const endCap = Math.min(maxEndBlock, currentBlock); + const startFloor = Math.max(0, fromBlock); + if (startFloor > endCap) return []; + + const chunk = Math.max(MIN_GETLOGS_BLOCK_RANGE, config.blockchain.bsc.getLogsBlockRange); + const byHash = new Map(); + let covered = 0n; + + for (let end = endCap; end >= startFloor && covered < minValue; ) { + const start = Math.max(startFloor, end - chunk + 1); + const part = await getUsdtTransfersToAddress(toAddress, contractAddress, start, end); + for (const t of part) { + if (!byHash.has(t.txHash)) { + byHash.set(t.txHash, t); + covered += t.value; + } + } + if (start <= startFloor) break; + end = start - 1; + } + + return [...byHash.values()].sort((a, b) => a.blockNumber - b.blockNumber); +} + async function fetchExplorerTokentx(url: string): Promise { const res = await fetch(url, { signal: AbortSignal.timeout(20_000) }); if (!res.ok) throw new Error(`explorer HTTP ${res.status}`); diff --git a/backend/src/services/depositMonitorService.ts b/backend/src/services/depositMonitorService.ts index fc5d7d55..e712610b 100644 --- a/backend/src/services/depositMonitorService.ts +++ b/backend/src/services/depositMonitorService.ts @@ -253,17 +253,22 @@ export async function processBscDeposits(): Promise { } // Receipt-based catch-up for HDs that still hold USDT (missed credit). - // Order: explorer (if keyed) → getLogs via primary+fallback RPCs on a short window. - // Public Binance dataseed often returns -32005 on eth_getLogs; fallbacks (1rpc, 48.club) work. - const unpaidLookback = config.blockchain.bsc.unpaidLookbackBlocks; + // Lookback must cover deposits missed while eth_getLogs was broken (days, not ~1 day). + // Newest-first scan stops once Transfer values cover the on-chain balance. + const unpaidLookback = Math.max( + config.blockchain.bsc.unpaidLookbackBlocks, + config.blockchain.bsc.depositInitialLookbackBlocks + ); for (const { address } of userWallets) { if (!unpaidAddresses.has(address.toLowerCase())) continue; try { const still = await bscService.getUsdtBalanceRaw(address, contract); if (still <= 0n) continue; + const recentFrom = Math.max(0, currentBlock - unpaidLookback); console.warn( - `[deposit] USDT_BEP20 unpaid recover for ${address} (on-chain=${still.toString()})` + `[deposit] USDT_BEP20 unpaid recover for ${address} (on-chain=${still.toString()}) ` + + `lookback=${unpaidLookback} fromBlock=${recentFrom} tip=${currentBlock}` ); const hashes = new Set(); @@ -276,12 +281,15 @@ export async function processBscDeposits(): Promise { } try { - const recentFrom = Math.max(0, currentBlock - unpaidLookback); - const recent = await bscService.getUsdtTransfersToAddress( + const recent = await bscService.getUsdtTransfersToAddressUntilValue( address, contract, recentFrom, - currentBlock + currentBlock, + still + ); + console.log( + `[deposit] USDT_BEP20 unpaid getLogs hits=${recent.length} for ${address}` ); for (const t of recent) hashes.add(t.txHash); } catch (e) { @@ -293,8 +301,8 @@ export async function processBscDeposits(): Promise { if (hashes.size === 0) { console.error( - `[deposit] USDT_BEP20 unpaid ${address}: no tx hashes (RPC getLogs + explorer empty). ` + - `Set BSC_RPC_FALLBACKS / BSCSCAN_API_KEY or use a paid BSC RPC.` + `[deposit] USDT_BEP20 unpaid ${address}: no tx hashes in blocks ${recentFrom}-${currentBlock}. ` + + `Set BSC_RPC_FALLBACKS (e.g. https://rpc-bsc.48.club) or BSCSCAN_API_KEY.` ); continue; } diff --git a/docker-compose.yml b/docker-compose.yml index 12dda771..37ad3334 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,11 +42,11 @@ x-backend-common: &backend-common USDT_TRC20_CONTRACT: ${USDT_TRC20_CONTRACT:-} BSC_RPC_URL: ${BSC_RPC_URL:-https://bsc-dataseed.binance.org} # Comma-separated RPCs used when primary eth_getLogs returns limit exceeded (-32005) - BSC_RPC_FALLBACKS: ${BSC_RPC_FALLBACKS:-https://1rpc.io/bnb,https://rpc-bsc.48.club} + BSC_RPC_FALLBACKS: ${BSC_RPC_FALLBACKS:-https://rpc-bsc.48.club,https://1rpc.io/bnb} BSC_CONFIRMATIONS: ${BSC_CONFIRMATIONS:-15} USDT_BEP20_CONTRACT: ${USDT_BEP20_CONTRACT:-} BSC_GETLOGS_BLOCK_RANGE: ${BSC_GETLOGS_BLOCK_RANGE:-2000} - BSC_UNPAID_LOOKBACK_BLOCKS: ${BSC_UNPAID_LOOKBACK_BLOCKS:-30000} + BSC_UNPAID_LOOKBACK_BLOCKS: ${BSC_UNPAID_LOOKBACK_BLOCKS:-200000} BSCSCAN_API_KEY: ${BSCSCAN_API_KEY:-} ETHERSCAN_API_KEY: ${ETHERSCAN_API_KEY:-} BSC_EXPLORER_API_URL: ${BSC_EXPLORER_API_URL:-} diff --git a/docs/DEPLOY-VPS.md b/docs/DEPLOY-VPS.md index d67221e8..1e949a69 100644 --- a/docs/DEPLOY-VPS.md +++ b/docs/DEPLOY-VPS.md @@ -71,11 +71,11 @@ nano .env # ou vim | `TRON_CONFIRMATIONS` | Ex. `20` | | `USDT_TRC20_CONTRACT` | Laisser **vide** en mainnet (contrat Tether par défaut en DB) | | `BSC_RPC_URL` | Mainnet : `https://bsc-dataseed.binance.org` (balances / receipts OK) | -| `BSC_RPC_FALLBACKS` | RPCs pour `eth_getLogs` si le primary renvoie `-32005` (défaut `https://1rpc.io/bnb,https://rpc-bsc.48.club`) | +| `BSC_RPC_FALLBACKS` | RPCs pour `eth_getLogs` si le primary renvoie `-32005` (défaut `https://rpc-bsc.48.club,https://1rpc.io/bnb`) | | `BSC_CONFIRMATIONS` | Ex. `15` | | `BSC_DEPOSIT_INITIAL_LOOKBACK_BLOCKS` | Lookback 1er scan / catch-up balance (défaut `200000` ≈ 7 j) | | `BSC_DEPOSIT_OVERLAP_LOOKBACK_BLOCKS` | Re-scan chevauchant chaque poll (défaut `5000`) | -| `BSC_UNPAID_LOOKBACK_BLOCKS` | Rattrapage si l’HD a encore de l’USDT (défaut `30000` ≈ 1 j) | +| `BSC_UNPAID_LOOKBACK_BLOCKS` | Rattrapage si l’HD a encore de l’USDT (défaut `200000` ≈ 7 j ; plancher avec le lookback initial) | | `BSCSCAN_API_KEY` / `ETHERSCAN_API_KEY` | Optionnel — Etherscan V2 (BSC peut exiger un plan payant) | | `BSC_EXPLORER_API_URL` | Optionnel — host tokentx Etherscan-compatible (laisser vide si inutilisé) | | `USDT_BEP20_CONTRACT` | Laisser **vide** en mainnet (USDT officiel BSC en DB) |