diff --git a/shared/lib/p2p-fiat.ts b/shared/lib/p2p-fiat.ts index 827a06f0..5a021528 100644 --- a/shared/lib/p2p-fiat.ts +++ b/shared/lib/p2p-fiat.ts @@ -1011,7 +1011,8 @@ export async function getInternalBalance(userId: string, token: CryptoToken): Pr /** * Request a withdrawal from internal balance to external wallet - * This creates a pending request that will be processed by backend service + * Calls the process-withdraw edge function which handles the full flow: + * limit check → lock balance → blockchain TX → complete withdrawal */ export async function requestWithdraw( userId: string, @@ -1030,31 +1031,24 @@ export async function requestWithdraw( throw new Error('Invalid wallet address'); } - toast.info('Processing withdrawal request...'); + toast.info('Processing withdrawal...'); - // Call the database function - const { data, error } = await supabase.rpc('request_withdraw', { - p_user_id: userId, - p_token: token, - p_amount: amount, - p_wallet_address: walletAddress + const { data, error } = await supabase.functions.invoke('process-withdraw', { + body: { userId, token, amount, walletAddress } }); if (error) throw error; - // Parse result - const result = typeof data === 'string' ? JSON.parse(data) : data; - - if (!result.success) { - throw new Error(result.error || 'Withdrawal request failed'); + if (!data?.success) { + throw new Error(data?.error || 'Withdrawal failed'); } - toast.success(`Withdrawal request submitted! ${amount} ${token} will be sent to your wallet.`); + toast.success(`${amount} ${token} sent to your wallet! TX: ${data.txHash?.slice(0, 10)}...`); - return result.request_id; + return data.txHash || ''; } catch (error: unknown) { console.error('Request withdraw error:', error); - const message = error instanceof Error ? error.message : 'Withdrawal request failed'; + const message = error instanceof Error ? error.message : 'Withdrawal failed'; toast.error(message); throw error; } diff --git a/web/src/components/p2p/InternalBalanceCard.tsx b/web/src/components/p2p/InternalBalanceCard.tsx index 38dae9dc..604d67a9 100644 --- a/web/src/components/p2p/InternalBalanceCard.tsx +++ b/web/src/components/p2p/InternalBalanceCard.tsx @@ -49,7 +49,7 @@ export function InternalBalanceCard({ onDeposit, onWithdraw }: InternalBalanceCa await fetchBalances(); }; - const formatBalance = (value: number, decimals: number = 4) => { + const formatBalance = (value: number, decimals: number = 2) => { return value.toLocaleString(undefined, { minimumFractionDigits: decimals, maximumFractionDigits: decimals @@ -122,8 +122,8 @@ export function InternalBalanceCard({ onDeposit, onWithdraw }: InternalBalanceCa
- -
+ +

{t('p2pBalance.available')}

{formatBalance(balance.available_balance)} @@ -131,8 +131,8 @@ export function InternalBalanceCard({ onDeposit, onWithdraw }: InternalBalanceCa

- -
+ +

{t('p2pBalance.lockedEscrow')}

{formatBalance(balance.locked_balance)} @@ -142,13 +142,13 @@ export function InternalBalanceCard({ onDeposit, onWithdraw }: InternalBalanceCa

-
+
{t('p2pBalance.totalDeposited')} - {formatBalance(balance.total_deposited, 2)} + {formatBalance(balance.total_deposited)}
-
+
{t('p2pBalance.totalWithdrawn')} - {formatBalance(balance.total_withdrawn, 2)} + {formatBalance(balance.total_withdrawn)}