/** * Deposit scanner and crediting service. * Credits a unified USDT ledger (USDT_TRC20, 6 decimals) and per-network inventory. * Custody scans remain on TRON (TRC20) and BSC (BEP20) deposit addresses. */ import { config } from '../config'; import { query, getClient, beginAuthorizedTransaction } from '../db/connection'; import * as walletRepository from '../repositories/walletRepository'; import * as ledgerRepository from '../repositories/ledgerRepository'; import * as transactionRepository from '../repositories/transactionRepository'; import * as depositScannerRepository from '../repositories/depositScannerRepository'; import * as networkInventoryRepository from '../repositories/networkInventoryRepository'; import * as auditLogRepository from '../repositories/auditLogRepository'; import { enqueueSweepAfterDeposit } from './sweepService'; import { queueUserAdminStatsRefresh } from './userAdminStatsService'; import * as referralService from './referralService'; import { USDT_BEP20_CODE, USDT_TRC20_CODE } from './walletCapabilities'; import * as tronService from './blockchain/tronService'; import * as bscService from './blockchain/bscService'; import * as settingsRepository from '../repositories/settingsRepository'; import * as depositHdSnapshotRepository from '../repositories/depositHdSnapshotRepository'; import { BEP20_USDT_DECIMALS, LEDGER_USDT_DECIMALS, scaleChainToLedger, type CustodyNetwork, } from '../utils/usdtUnits'; import { computeBscCommittedScannerBlock, computeBscScanFromBlock, } from '../utils/bscDepositScan'; const TRON_CONFIRMATIONS = config.blockchain.tron.confirmations; const BSC_CONFIRMATIONS = config.blockchain.bsc.confirmations; /** Scans TRON + BSC for new USDT deposits and credits confirmed transfers. */ export async function processAllDeposits(): Promise { await processTronDeposits(); await processBscDeposits(); } /** Scans TRON for new USDT TRC20 deposits and credits confirmed transfers. */ export async function processTronDeposits(): Promise { const settings = await settingsRepository.getSettings(); if (!settings.network_usdt_trc20_deposit_enabled) return; const custodyType = await walletRepository.getWalletTypeByCode(USDT_TRC20_CODE); const ledgerType = custodyType; if (!custodyType || !ledgerType) return; const currentBlock = await tronService.getCurrentBlock(); const state = await depositScannerRepository.getState(custodyType.id); let fromBlock = state ? BigInt(state.last_block) + 1n : 0n; if (fromBlock > BigInt(currentBlock)) { fromBlock = 0n; } const userWallets = await query>( 'SELECT address, user_id, created_at FROM user_wallets WHERE wallet_type_id = $1', [custodyType.id] ); if (userWallets.length > 0) { const hasAnyUsdtDeposit = await query<{ n: string }[]>( `SELECT 1 as n FROM transactions WHERE wallet_type_id = $1 AND type = 'deposit' LIMIT 1`, [ledgerType.id] ).then((r) => r.length > 0); if (!hasAnyUsdtDeposit && fromBlock > 0n) { fromBlock = 0n; if (process.env.NODE_ENV !== 'production') { console.log('[deposit] USDT_TRC20: no deposit txs in DB yet, scanning from start'); } } } let scannerMinTimestamp = 0; if (fromBlock > 0n) { const blockTs = await tronService.getBlockTimestamp(Number(fromBlock)); if (blockTs != null) scannerMinTimestamp = blockTs; } const trc20Contract = config.blockchain.tron.usdtTrc20Contract || custodyType.contract_address || ''; if (!trc20Contract) return; if (process.env.NODE_ENV !== 'production') { console.log( `[deposit] USDT_TRC20 (id=${custodyType.id}): ${userWallets.length} wallets, fromBlock=${fromBlock}` ); } for (const { address, user_id, created_at } of userWallets) { const walletCreatedMs = new Date(created_at).getTime(); const minTimestamp = Math.max(scannerMinTimestamp, walletCreatedMs); const transfers = await tronService.getTrc20TransfersToAddress( address, trc20Contract, minTimestamp ); if (process.env.NODE_ENV !== 'production' && transfers.length > 0) { console.log(`[deposit] USDT TRC20 ${address}: ${transfers.length} transfer(s)`); } for (const t of transfers) { const amount = tronService.parseTrc20TokenAmount(t.value); if (amount == null) continue; if (t.confirmations >= TRON_CONFIRMATIONS) { try { await creditDeposit({ userId: user_id, ledgerWalletTypeId: ledgerType.id, custodyWalletTypeId: custodyType.id, network: 'TRON', sourceWalletCode: USDT_TRC20_CODE, ledgerAmount: amount, chainAmount: amount, blockchainTxId: t.txId, fromAddress: t.from, toAddress: address, confirmations: t.confirmations, requiredConfirmations: TRON_CONFIRMATIONS, }); } catch (e) { console.error(`[deposit] TRC20 credit failed tx=${t.txId}:`, e); } } } } for (const { address, user_id } of userWallets) { try { const bal = await tronService.getTrc20BalanceRaw(address, trc20Contract); await depositHdSnapshotRepository.upsertSnapshot({ userId: user_id, walletTypeId: custodyType.id, address, onChainAmount: bal, }); } catch (e) { console.error(`[deposit] USDT_TRC20: balance snapshot failed for ${address}:`, e); } } await depositScannerRepository.updateState(custodyType.id, BigInt(currentBlock)); } /** Scans BSC for new USDT BEP20 deposits; credits unified ledger (6 dec) + BSC inventory. */ export async function processBscDeposits(): Promise { const settings = await settingsRepository.getSettings(); if (!settings.network_usdt_bep20_deposit_enabled) { console.warn('[deposit] USDT_BEP20: deposit flag disabled — skipping scan'); return; } const custodyType = await walletRepository.getWalletTypeByCode(USDT_BEP20_CODE); const ledgerType = await walletRepository.getWalletTypeByCode(USDT_TRC20_CODE); if (!custodyType || !ledgerType) { console.warn('[deposit] USDT_BEP20: wallet type(s) missing — skipping scan'); return; } const currentBlock = await bscService.getCurrentBlock(); const state = await depositScannerRepository.getState(custodyType.id); const lastBlock = state ? Number(state.last_block) : null; const userWallets = await query>( 'SELECT address, user_id, created_at FROM user_wallets WHERE wallet_type_id = $1', [custodyType.id] ); // Never advance the cursor with zero wallets — that permanently skips history // once the first deposit address is created after a tip checkpoint. if (userWallets.length === 0) { console.warn('[deposit] USDT_BEP20: no deposit wallets yet — scanner idle'); return; } const initialLookback = config.blockchain.bsc.depositInitialLookbackBlocks; const overlapLookback = config.blockchain.bsc.depositOverlapLookbackBlocks; let fromBlock = computeBscScanFromBlock({ lastBlock, currentBlock, initialLookback, overlapLookback, }); const contract = bscService.resolveUsdtContract(custodyType.contract_address); const unpaidAddresses = new Set(); for (const { address, user_id } of userWallets) { try { const bal = await bscService.getUsdtBalanceRaw(address, contract); await depositHdSnapshotRepository.upsertSnapshot({ userId: user_id, walletTypeId: custodyType.id, address, onChainAmount: bal, }); if (bal > 0n) unpaidAddresses.add(address.toLowerCase()); } catch (e) { console.error(`[deposit] USDT_BEP20: balance check failed for ${address}:`, e); } } if (unpaidAddresses.size > 0) { console.warn( `[deposit] USDT_BEP20: ${unpaidAddresses.size} HD address(es) still hold USDT — will recover via logs + explorer` ); } console.log( `[deposit] USDT_BEP20 (id=${custodyType.id}): ${userWallets.length} wallets, fromBlock=${fromBlock}, tip=${currentBlock}` ); for (const { address, user_id } of userWallets) { try { const transfers = await bscService.getUsdtTransfersToAddress( address, contract, fromBlock, currentBlock ); if (transfers.length > 0) { console.log(`[deposit] USDT_BEP20 ${address}: ${transfers.length} transfer(s) in window`); } for (const t of transfers) { const confirmations = currentBlock - t.blockNumber + 1; if (confirmations < BSC_CONFIRMATIONS) continue; const ledgerAmount = scaleChainToLedger(t.value, BEP20_USDT_DECIMALS, LEDGER_USDT_DECIMALS); if (ledgerAmount <= 0n) continue; try { await creditDeposit({ userId: user_id, ledgerWalletTypeId: ledgerType.id, custodyWalletTypeId: custodyType.id, network: 'BSC', sourceWalletCode: USDT_BEP20_CODE, ledgerAmount, chainAmount: t.value, blockchainTxId: t.txHash, fromAddress: t.from, toAddress: address, confirmations, requiredConfirmations: BSC_CONFIRMATIONS, }); } catch (e) { console.error(`[deposit] BEP20 credit failed tx=${t.txHash}:`, e); } } } catch (e) { console.error(`[deposit] USDT_BEP20 getLogs failed for ${address}:`, e); } } // Receipt-based catch-up (same path as creditBscDepositByTx): public eth_getLogs // often times out on a 7-day window, so unpaid HDs never get credited without this. const unpaidLookback = config.blockchain.bsc.unpaidLookbackBlocks; for (const { address } of userWallets) { if (!unpaidAddresses.has(address.toLowerCase())) continue; try { const still = await bscService.getUsdtBalanceRaw(address, contract); if (still <= 0n) continue; const hashes = new Set(); try { const recentFrom = Math.max(0, currentBlock - unpaidLookback); const recent = await bscService.getUsdtTransfersToAddress( address, contract, recentFrom, currentBlock ); for (const t of recent) hashes.add(t.txHash); } catch (e) { console.warn( `[deposit] USDT_BEP20 unpaid getLogs failed for ${address}:`, e instanceof Error ? e.message : e ); } if (hashes.size === 0) { const viaExplorer = await bscService.listIncomingUsdtTxHashesViaExplorer(address, contract); for (const h of viaExplorer) hashes.add(h); } for (const txHash of hashes) { const result = await creditBscDepositByTxHash(txHash); if (result.credited) { console.log(`[deposit] USDT_BEP20 recovered unpaid via tx=${txHash} user=${result.userId}`); } else if (result.reason && !/already|idempotent/i.test(result.reason)) { console.warn(`[deposit] USDT_BEP20 unpaid recover skipped tx=${txHash}: ${result.reason}`); } } } catch (e) { console.error(`[deposit] USDT_BEP20 unpaid recover failed for ${address}:`, e); } } const committed = computeBscCommittedScannerBlock(currentBlock, BSC_CONFIRMATIONS); await depositScannerRepository.updateState(custodyType.id, BigInt(committed)); } /** * Manually credit a confirmed BSC USDT transfer by tx hash (ops recovery). * Safe to re-run: insertDepositIfNew is idempotent on blockchain_tx_id. */ export async function creditBscDepositByTxHash(txHash: string): Promise<{ credited: boolean; userId?: string; ledgerAmount?: string; reason?: string; }> { const settings = await settingsRepository.getSettings(); if (!settings.network_usdt_bep20_deposit_enabled) { return { credited: false, reason: 'network_usdt_bep20_deposit_enabled is false' }; } const custodyType = await walletRepository.getWalletTypeByCode(USDT_BEP20_CODE); const ledgerType = await walletRepository.getWalletTypeByCode(USDT_TRC20_CODE); if (!custodyType || !ledgerType) { return { credited: false, reason: 'wallet types missing' }; } const receipt = await bscService.getTransactionReceipt(txHash); if (!receipt) return { credited: false, reason: 'transaction not found' }; if (receipt.status !== 1) return { credited: false, reason: 'transaction failed on-chain' }; const contract = bscService.resolveUsdtContract(custodyType.contract_address).toLowerCase(); const usdtTransfers = receipt.transfers.filter((t) => t.contract.toLowerCase() === contract); if (usdtTransfers.length === 0) { return { credited: false, reason: 'no USDT Transfer log for configured BEP20 contract' }; } let wallet: { address: string; user_id: string } | undefined; let transfer = usdtTransfers[0]; for (const t of usdtTransfers) { const rows = await query>( `SELECT address, user_id FROM user_wallets WHERE wallet_type_id = $1 AND lower(address) = lower($2)`, [custodyType.id, t.to] ); if (rows[0]) { wallet = rows[0]; transfer = t; break; } } if (!wallet) { return { credited: false, reason: `no USDT_BEP20 user_wallet for to=${usdtTransfers.map((t) => t.to).join(',')}`, }; } const currentBlock = await bscService.getCurrentBlock(); const confirmations = currentBlock - receipt.blockNumber + 1; if (confirmations < BSC_CONFIRMATIONS) { return { credited: false, reason: `need ${BSC_CONFIRMATIONS} confirmations, have ${confirmations}`, }; } const ledgerAmount = scaleChainToLedger(transfer.value, BEP20_USDT_DECIMALS, LEDGER_USDT_DECIMALS); if (ledgerAmount <= 0n) return { credited: false, reason: 'amount too small after scale' }; await creditDeposit({ userId: wallet.user_id, ledgerWalletTypeId: ledgerType.id, custodyWalletTypeId: custodyType.id, network: 'BSC', sourceWalletCode: USDT_BEP20_CODE, ledgerAmount, chainAmount: transfer.value, blockchainTxId: txHash, fromAddress: transfer.from, toAddress: wallet.address, confirmations, requiredConfirmations: BSC_CONFIRMATIONS, }); return { credited: true, userId: wallet.user_id, ledgerAmount: ledgerAmount.toString(), }; } async function creditDeposit(opts: { userId: string; ledgerWalletTypeId: number; custodyWalletTypeId: number; network: CustodyNetwork; sourceWalletCode: string; ledgerAmount: bigint; chainAmount: bigint; blockchainTxId: string; fromAddress: string; toAddress: string; confirmations: number; requiredConfirmations: number; }): Promise { const account = await ledgerRepository.getOrCreateAccount( opts.userId, opts.ledgerWalletTypeId, 'main' ); const client = await getClient(); let tx: Awaited> = null; try { await beginAuthorizedTransaction(client); tx = await transactionRepository.insertDepositIfNew(client, { userId: opts.userId, walletTypeId: opts.ledgerWalletTypeId, amount: opts.ledgerAmount, blockchainTxId: opts.blockchainTxId, fromAddress: opts.fromAddress, toAddress: opts.toAddress, confirmations: opts.confirmations, requiredConfirmations: opts.requiredConfirmations, metadata: { network: opts.network, source_wallet_type: opts.sourceWalletCode, chain_amount: opts.chainAmount.toString(), ledger_amount: opts.ledgerAmount.toString(), }, }); if (!tx) { await client.query('COMMIT'); return; } await ledgerRepository.creditUsingClient( client, account.id, opts.ledgerAmount, 'deposit', 'transaction', tx.id ); await networkInventoryRepository.creditUsingClient( client, opts.userId, opts.network, opts.ledgerAmount ); await client.query('COMMIT'); console.log( `[deposit] credited ${opts.sourceWalletCode} user=${opts.userId} ledger=${opts.ledgerAmount.toString()} tx=${opts.blockchainTxId}` ); } catch (e) { await client.query('ROLLBACK'); throw e; } finally { client.release(); } if (!tx) return; await referralService.processFirstDepositReferralCommissions({ userId: opts.userId, depositTransactionId: tx.id, depositAmount: opts.ledgerAmount, walletTypeId: opts.ledgerWalletTypeId, }); await auditLogRepository.create({ userId: opts.userId, action: 'deposit_confirmed', resourceType: 'transaction', resourceId: tx.id, metadata: { amount: opts.ledgerAmount.toString(), chainAmount: opts.chainAmount.toString(), network: opts.network, sourceWalletType: opts.sourceWalletCode, blockchainTxId: opts.blockchainTxId, }, }); // Sweep uses custody type + on-chain amount await enqueueSweepAfterDeposit( opts.userId, opts.custodyWalletTypeId, opts.chainAmount, tx.id ); queueUserAdminStatsRefresh(opts.userId); }