From f4c1ee3fc8f46d98479687b9b4b0b2ae8ecd87bd Mon Sep 17 00:00:00 2001 From: Eric Corson Date: Thu, 31 Oct 2024 07:14:07 +0000 Subject: [PATCH] fix: stop deducting fees twice in available amount (#1225) Fixes the second of two bugs causing the TransferModule to incorrectly detect that there is not enough balance. Also display 0 if available amount minus fees is less than 0. --- .../src/App/Transfer/TransferModule.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/namadillo/src/App/Transfer/TransferModule.tsx b/apps/namadillo/src/App/Transfer/TransferModule.tsx index 2e7e5a63a..6a8f739d7 100644 --- a/apps/namadillo/src/App/Transfer/TransferModule.tsx +++ b/apps/namadillo/src/App/Transfer/TransferModule.tsx @@ -97,7 +97,7 @@ export const TransferModule = ({ const [sourceIbcChannel, setSourceIbcChannel] = useState(""); const [destinationIbcChannel, setDestinationIbcChannel] = useState(""); - const availableAmount = useMemo(() => { + const availableAmountMinusFees = useMemo(() => { const { selectedAsset, availableAmount } = source; if ( @@ -107,9 +107,12 @@ export const TransferModule = ({ return undefined; } - return transactionFee && selectedAsset.base === transactionFee.token.base ? + const minusFees = + transactionFee && selectedAsset.base === transactionFee.token.base ? availableAmount.minus(transactionFee.amount) : availableAmount; + + return BigNumber.max(minusFees, 0); }, [source.selectedAsset, source.availableAmount, transactionFee]); const validationResult = useMemo((): ValidationResult => { @@ -126,8 +129,8 @@ export const TransferModule = ({ } else if (!transactionFee) { return "NoTransactionFee"; } else if ( - !availableAmount || - availableAmount.lt(amount.plus(transactionFee.amount)) + !availableAmountMinusFees || + amount.gt(availableAmountMinusFees) ) { return "NotEnoughBalance"; } else if (!destination.wallet) { @@ -135,7 +138,7 @@ export const TransferModule = ({ } else { return "Ok"; } - }, [amount, source, destination, transactionFee, availableAmount]); + }, [amount, source, destination, transactionFee, availableAmountMinusFees]); const onSubmit = (e: React.FormEvent): void => { e.preventDefault(); @@ -229,7 +232,7 @@ export const TransferModule = ({ return "Define an amount to transfer"; } - if (!availableAmount) { + if (!availableAmountMinusFees) { return "Wallet amount not available"; } @@ -255,7 +258,7 @@ export const TransferModule = ({ asset={source.selectedAsset} isLoadingAssets={source.isLoadingAssets} chain={parseChainInfo(source.chain, source.isShielded)} - availableAmount={availableAmount} + availableAmount={availableAmountMinusFees} amount={amount} openProviderSelector={onChangeWallet(source)} openChainSelector={