diff --git a/backend/src/services/treasuryService.ts b/backend/src/services/treasuryService.ts index ea50834f..efc1fe4e 100644 --- a/backend/src/services/treasuryService.ts +++ b/backend/src/services/treasuryService.ts @@ -11,6 +11,42 @@ import * as depositSweepRepository from '../repositories/depositSweepRepository' import * as exchangeFundingRepository from '../repositories/exchangeFundingRepository'; import * as settingsRepository from '../repositories/settingsRepository'; import * as tronService from './blockchain/tronService'; +import * as bscService from './blockchain/bscService'; +import { USDT_BEP20_CODE, USDT_TRC20_CODE } from './walletCapabilities'; +import { MIN_BNB_WEI_FOR_WITHDRAWAL, MIN_TRX_SUN_FOR_WITHDRAWAL } from './hotLiquidityService'; + +async function readTronTokenBalance(address: string, contract: string): Promise { + try { + return (await tronService.getTrc20BalanceRaw(address, contract)).toString(); + } catch { + return null; + } +} + +async function readTronNativeSun(address: string): Promise { + try { + return (await tronService.getTrxBalanceSun(address)).toString(); + } catch { + return null; + } +} + +async function readBscUsdtBalance(address: string, dbContract: string | null): Promise { + try { + const contract = bscService.resolveUsdtContract(dbContract); + return (await bscService.getUsdtBalanceRaw(address, contract)).toString(); + } catch { + return null; + } +} + +async function readBscNativeWei(address: string): Promise { + try { + return (await bscService.getNativeBalanceWei(address)).toString(); + } catch { + return null; + } +} export async function getTreasuryOverview() { const types = await walletRepository.getWalletTypes(); @@ -28,30 +64,45 @@ export async function getTreasuryOverview() { let hotOnChainBalance: string | null = null; let coldOnChainBalance: string | null = null; + let hotNativeBalance: string | null = null; + let coldNativeBalance: string | null = null; + let nativeSymbol: string | null = null; + let nativeDecimals: number | null = null; + let minNativeForWithdraw: string | null = null; - try { - if (hot?.address && wt.network === 'TRON') { - const contract = - config.blockchain.tron.usdtTrc20Contract || wt.contract_address || ''; - if (contract) { - hotOnChainBalance = ( - await tronService.getTrc20BalanceRaw(hot.address, contract) - ).toString(); - } + if (wt.code === USDT_TRC20_CODE || wt.network === 'TRON') { + nativeSymbol = 'TRX'; + nativeDecimals = 6; + minNativeForWithdraw = MIN_TRX_SUN_FOR_WITHDRAWAL.toString(); + const contract = + config.blockchain.tron.usdtTrc20Contract || wt.contract_address || ''; + if (hot?.address) { + if (contract) hotOnChainBalance = await readTronTokenBalance(hot.address, contract); + hotNativeBalance = await readTronNativeSun(hot.address); } - if (cold?.address && wt.network === 'TRON') { - const contract = - config.blockchain.tron.usdtTrc20Contract || wt.contract_address || ''; - if (contract) { - coldOnChainBalance = ( - await tronService.getTrc20BalanceRaw(cold.address, contract) - ).toString(); - } + if (cold?.address) { + if (contract) coldOnChainBalance = await readTronTokenBalance(cold.address, contract); + coldNativeBalance = await readTronNativeSun(cold.address); + } + } else if (wt.code === USDT_BEP20_CODE || wt.network === 'BSC') { + nativeSymbol = 'BNB'; + nativeDecimals = 18; + minNativeForWithdraw = MIN_BNB_WEI_FOR_WITHDRAWAL.toString(); + if (hot?.address) { + hotOnChainBalance = await readBscUsdtBalance(hot.address, wt.contract_address); + hotNativeBalance = await readBscNativeWei(hot.address); + } + if (cold?.address) { + coldOnChainBalance = await readBscUsdtBalance(cold.address, wt.contract_address); + coldNativeBalance = await readBscNativeWei(cold.address); } - } catch { - // RPC errors — balances stay null } + const hotNativeOk = + hotNativeBalance != null && minNativeForWithdraw != null + ? BigInt(hotNativeBalance) >= BigInt(minNativeForWithdraw) + : null; + return { walletType: wt, coldWallet: cold @@ -60,6 +111,12 @@ export async function getTreasuryOverview() { hotWallet: hot ? { id: hot.id, address: hot.address, isActive: hot.is_active } : null, hotOnChainBalance, coldOnChainBalance, + hotNativeBalance, + coldNativeBalance, + nativeSymbol, + nativeDecimals, + minNativeForWithdraw, + hotNativeOk, }; }) ); diff --git a/prediqo_web/src/features/treasury/TreasuryAdminPage.tsx b/prediqo_web/src/features/treasury/TreasuryAdminPage.tsx index 1188a99d..8a19d76a 100644 --- a/prediqo_web/src/features/treasury/TreasuryAdminPage.tsx +++ b/prediqo_web/src/features/treasury/TreasuryAdminPage.tsx @@ -47,6 +47,12 @@ type TreasuryOverview = { hotWallet: { id: string; address: string } | null; hotOnChainBalance: string | null; coldOnChainBalance: string | null; + hotNativeBalance: string | null; + coldNativeBalance: string | null; + nativeSymbol: string | null; + nativeDecimals: number | null; + minNativeForWithdraw: string | null; + hotNativeOk: boolean | null; }>; recentPendingSweeps: number; recentFailedSweeps: number; @@ -78,8 +84,24 @@ type SweepRow = { function formatRaw(amount: string | null, decimals: number): string { if (amount == null) return '—'; - const n = Number(amount) / 10 ** decimals; - return Number.isFinite(n) ? n.toLocaleString(undefined, { maximumFractionDigits: 6 }) : amount; + try { + const raw = BigInt(amount); + const base = 10n ** BigInt(decimals); + const whole = raw / base; + const frac = raw % base; + if (frac === 0n) return whole.toString(); + const fracStr = frac.toString().padStart(decimals, '0').replace(/0+$/, ''); + return `${whole.toString()}.${fracStr}`; + } catch { + return amount; + } +} + +function explorerUrl(network: string, address: string): string | null { + if (!address || address === '—') return null; + if (network === 'BSC') return `https://bscscan.com/address/${address}`; + if (network === 'TRON') return `https://tronscan.org/#/address/${address}`; + return null; } export function TreasuryAdminPage() { @@ -197,12 +219,18 @@ export function TreasuryAdminPage() { {t('admin.treasury.sweepDisabledHint')} )} + {t('admin.treasury.gasHint')} + {overview.assets.map((asset) => { const wt = asset.walletType; const form = coldForms[wt.id] ?? { address: asset.coldWallet?.address ?? '', label: asset.coldWallet?.label ?? '', }; + const nativeDec = asset.nativeDecimals ?? 18; + const hotExplorer = asset.hotWallet?.address + ? explorerUrl(wt.network, asset.hotWallet.address) + : null; return ( @@ -216,12 +244,54 @@ export function TreasuryAdminPage() { balance: formatRaw(asset.coldOnChainBalance, wt.decimals), })} + {asset.nativeSymbol && asset.coldWallet?.address && ( + + {t('admin.treasury.coldGasOnChain', { + symbol: asset.nativeSymbol, + balance: formatRaw(asset.coldNativeBalance, nativeDec), + })} + + )} {t('admin.treasury.hotOnChain', { address: asset.hotWallet?.address ?? '—', balance: formatRaw(asset.hotOnChainBalance, wt.decimals), })} + {asset.nativeSymbol && asset.hotWallet?.address && ( + + + {t('admin.treasury.hotGasOnChain', { + symbol: asset.nativeSymbol, + balance: formatRaw(asset.hotNativeBalance, nativeDec), + })} + + {asset.hotNativeOk === true && ( + + )} + {asset.hotNativeOk === false && ( + + )} + {hotExplorer && ( + + )} + + )}