adding insufficient balance error in the swap form

This commit is contained in:
2025-11-25 11:33:14 -04:00
parent 519a22847b
commit 8cc9d00521

View File

@@ -60,17 +60,34 @@ export function SwapForm() {
currentSlippage currentSlippage
); );
// Check if user has insufficient balance
const hasInsufficientBalance = useMemo(() => {
if (!selectedFromToken || !fromAmount || fromAmount === '') {
return false;
}
try {
const inputAmount = parseUnits(fromAmount, selectedFromToken.decimals);
return inputAmount > selectedFromToken.balance;
} catch {
return false;
}
}, [selectedFromToken, fromAmount]);
// Initialize swap hook // Initialize swap hook
const { executeSwap, estimateGas, isSwapping, gasEstimate, isEstimatingGas } = useSwap(); const { executeSwap, estimateGas, isSwapping, gasEstimate, isEstimatingGas } = useSwap();
// Update "You Receive" amount when swap calculation completes // Update "You Receive" amount when swap calculation completes
useEffect(() => { useEffect(() => {
if (hasInsufficientBalance) {
setToAmount('');
return;
}
if (swapAmounts && swapAmounts.length > 0 && selectedToToken) { if (swapAmounts && swapAmounts.length > 0 && selectedToToken) {
const swapResult = swapAmounts[0]; // Get the first (and should be only) result const swapResult = swapAmounts[0]; // Get the first (and should be only) result
const formattedAmount = formatUnits(swapResult.amountOut, selectedToToken.decimals); const formattedAmount = formatUnits(swapResult.amountOut, selectedToToken.decimals);
setToAmount(formattedAmount); setToAmount(formattedAmount);
} }
}, [swapAmounts, selectedToToken]); }, [swapAmounts, selectedToToken, hasInsufficientBalance]);
// Close dropdowns when clicking outside // Close dropdowns when clicking outside
useEffect(() => { useEffect(() => {
@@ -346,6 +363,13 @@ export function SwapForm() {
</div> </div>
)} )}
{/* Error message for insufficient balance */}
{hasInsufficientBalance && (
<div className="px-4 py-3 bg-destructive/10 border border-destructive/20 rounded-lg">
<p className="text-sm text-destructive">Insufficient balance</p>
</div>
)}
{/* Gas Estimate, Slippage, and Fees */} {/* Gas Estimate, Slippage, and Fees */}
{isConnected && fromAmount && toAmount && ( {isConnected && fromAmount && toAmount && (
<div className="px-4 py-2 bg-muted/30 rounded-lg space-y-2"> <div className="px-4 py-2 bg-muted/30 rounded-lg space-y-2">
@@ -384,10 +408,12 @@ export function SwapForm() {
<Button <Button
className="w-full h-14 text-lg" className="w-full h-14 text-lg"
onClick={() => setIsReviewModalOpen(true)} onClick={() => setIsReviewModalOpen(true)}
disabled={!isConnected || !fromAmount || !toAmount || !!poolsError} disabled={!isConnected || !fromAmount || !toAmount || !!poolsError || hasInsufficientBalance}
> >
{!isConnected {!isConnected
? t('swap.connectWalletToSwap') ? t('swap.connectWalletToSwap')
: hasInsufficientBalance
? 'Insufficient Balance'
: 'Review'} : 'Review'}
</Button> </Button>
</CardContent> </CardContent>