diff --git a/backend/src/services/withdrawalService.ts b/backend/src/services/withdrawalService.ts index 545a3536..1225dd80 100644 --- a/backend/src/services/withdrawalService.ts +++ b/backend/src/services/withdrawalService.ts @@ -42,6 +42,19 @@ import { } from '../utils/usdtUnits'; import { assertHotWalletCanPay } from './hotLiquidityService'; +function assertPayoutAddress(network: 'TRON' | 'BSC', toAddress: string): void { + const a = toAddress.trim(); + if (network === 'BSC') { + if (!/^0x[a-fA-F0-9]{40}$/.test(a)) { + throw new Error('Invalid BEP20 address. Use a 0x… address on BNB Smart Chain.'); + } + return; + } + if (!/^T[1-9A-HJ-NP-Za-km-z]{33}$/.test(a)) { + throw new Error('Invalid TRC20 address. Use a T… address on TRON.'); + } +} + async function assertWithdrawalWithinLimits( userId: string, railWalletType: WalletType, @@ -145,6 +158,7 @@ export async function requestWithdrawal( throw err; } if (amountRaw <= 0n) throw new Error('Invalid amount'); + assertPayoutAddress(network, toAddress); const ledgerWalletType = await walletRepository.getWalletTypeByCode(USDT_TRC20_CODE); if (!ledgerWalletType) throw new Error('USDT ledger wallet type not configured'); diff --git a/backend/src/utils/safeError.ts b/backend/src/utils/safeError.ts index 0430fde4..7a298d4b 100644 --- a/backend/src/utils/safeError.ts +++ b/backend/src/utils/safeError.ts @@ -4,14 +4,32 @@ import { config } from '../config'; * Returns a client-safe error message. Internal details are hidden in production. */ export function clientErrorMessage(error: unknown, fallback = 'Request failed'): string { + const msg = error instanceof Error ? error.message : String(error); + const code = (error as { code?: string })?.code; + if (config.env !== 'production') { - return error instanceof Error ? error.message : String(error); + return msg; } + + const exposeByCode = new Set([ + 'WITHDRAWALS_DISABLED', + 'WITHDRAWAL_NETWORK_DISABLED', + 'HOT_WALLET_INSUFFICIENT_LIQUIDITY', + 'INSUFFICIENT_BALANCE', + 'DAILY_WITHDRAWAL_LIMIT_EXCEEDED', + 'WITHDRAWAL_2FA_REQUIRED', + ]); + if (code && exposeByCode.has(code) && msg.trim()) { + return msg; + } + if (error instanceof Error && error.message) { - const msg = error.message; if (/^(Invalid|Unauthorized|Forbidden|Not found|2FA|Account|Password|Email|Phone|Referrer|referral)/i.test(msg)) { return msg; } + if (/Withdrawals are disabled/i.test(msg)) { + return msg; + } } return fallback; } diff --git a/prediqo_mobile/lib/data/models/wallet_type_model.dart b/prediqo_mobile/lib/data/models/wallet_type_model.dart index 034a696c..aade6b59 100644 --- a/prediqo_mobile/lib/data/models/wallet_type_model.dart +++ b/prediqo_mobile/lib/data/models/wallet_type_model.dart @@ -34,8 +34,8 @@ class WalletTypeModel { network: json['network'] as String, decimals: json['decimals'] as int? ?? 6, contractAddress: json['contract_address'] as String?, - depositEnabled: json['depositEnabled'] as bool? ?? true, - withdrawEnabled: json['withdrawEnabled'] as bool? ?? true, + depositEnabled: json['depositEnabled'] as bool? ?? false, + withdrawEnabled: json['withdrawEnabled'] as bool? ?? false, predictionEnabled: json['predictionEnabled'] as bool? ?? false, ); } diff --git a/prediqo_mobile/lib/features/wallet/presentation/withdraw_screen.dart b/prediqo_mobile/lib/features/wallet/presentation/withdraw_screen.dart index e21261eb..1b607c02 100644 --- a/prediqo_mobile/lib/features/wallet/presentation/withdraw_screen.dart +++ b/prediqo_mobile/lib/features/wallet/presentation/withdraw_screen.dart @@ -81,8 +81,7 @@ class _WithdrawScreenState extends State { ); return; } - if (_selectedWalletType == null || - _addressController.text.isEmpty || + if (_addressController.text.isEmpty || _amountController.text.isEmpty || _totpController.text.length != 6) { ScaffoldMessenger.of(context).showSnackBar( @@ -91,6 +90,17 @@ class _WithdrawScreenState extends State { return; } final state = context.read().state; + final enabledRails = state.walletTypes.where((t) => t.withdrawEnabled == true).toList(); + final railCode = (_selectedWalletType != null && + enabledRails.any((t) => t.code == _selectedWalletType)) + ? _selectedWalletType! + : (enabledRails.isNotEmpty ? enabledRails.first.code : null); + if (railCode == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('No withdrawal network is enabled right now.')), + ); + return; + } final amountHuman = _amountController.text.trim(); // Always ledger micro-USDT (6), even when payout rail is BEP20. final amountRaw = humanAmountToRaw(amountHuman, _kLedgerUsdtDecimals); @@ -165,7 +175,7 @@ class _WithdrawScreenState extends State { if (confirmed == true && context.mounted) { context.read().add( WalletWithdrawRequested( - walletType: _selectedWalletType!, + walletType: railCode, toAddress: address, amount: amountRaw, totpCode: _totpController.text, @@ -212,9 +222,16 @@ class _WithdrawScreenState extends State { if (types.isEmpty) { return const Center(child: Text('No withdrawal networks enabled')); } - _selectedWalletType ??= types.first.code; - final selectedTypeList = types.where((t) => t.code == _selectedWalletType); - final selectedType = selectedTypeList.isEmpty ? types.first : selectedTypeList.first; + final selectedCode = (_selectedWalletType != null && + types.any((t) => t.code == _selectedWalletType)) + ? _selectedWalletType! + : types.first.code; + if (_selectedWalletType != selectedCode) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) setState(() => _selectedWalletType = selectedCode); + }); + } + final selectedType = types.firstWhere((t) => t.code == selectedCode); final networkLabel = CoinDisplay.networkLabel(selectedType.code); final feeRaw = CoinDisplay.scaleWithdrawalFeeRaw(_networkFeeRaw, _kLedgerUsdtDecimals); final feeHuman = rawAmountToHuman(feeRaw.toString(), _kLedgerUsdtDecimals); @@ -268,9 +285,7 @@ class _WithdrawScreenState extends State { const Text('Payout network'), const SizedBox(height: 8), DropdownButtonFormField( - initialValue: types.any((t) => t.code == _selectedWalletType) - ? _selectedWalletType - : types.first.code, + value: selectedCode, items: types .map( (t) => DropdownMenuItem( diff --git a/prediqo_web/src/features/wallet/components/WithdrawDialog.tsx b/prediqo_web/src/features/wallet/components/WithdrawDialog.tsx index ae756e94..94452f27 100644 --- a/prediqo_web/src/features/wallet/components/WithdrawDialog.tsx +++ b/prediqo_web/src/features/wallet/components/WithdrawDialog.tsx @@ -90,13 +90,15 @@ export function WithdrawDialog({ }, [networkFeeRaw]); useEffect(() => { - if (open) { - setRail(initialRail ?? withdrawRails[0]?.code ?? ''); - setToAddress(''); - setAmount(''); - setTotpCode(''); - setConfirmOpen(false); - } + if (!open) return; + const fallback = withdrawRails[0]?.code ?? ''; + const next = + initialRail && withdrawRails.some((n) => n.code === initialRail) ? initialRail : fallback; + setRail(next); + setToAddress(''); + setAmount(''); + setTotpCode(''); + setConfirmOpen(false); }, [open, initialRail, withdrawRails]); const selectedRail = useMemo(