From 3c5e49cbe92ce10273641a4b66074e30c4889960 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 6 Dec 2024 10:17:42 -0300 Subject: [PATCH] [REF] improve not enough funds/fee err msg - 1 --- locales/en/translation.json | 4 +- src/store/wallet/effects/send/send.ts | 57 ++++++++++++++++++++------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/locales/en/translation.json b/locales/en/translation.json index 965fb38c7..5eb9765fe 100644 --- a/locales/en/translation.json +++ b/locales/en/translation.json @@ -58,6 +58,8 @@ "Server response could not be verified": "Server response could not be verified", "Could not build transaction": "Could not build transaction", "You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.": "You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.", + "You are trying to send more funds than you have available.\n\nTrying to send:\nAvailable to send:": "You are trying to send more funds than you have available.\n\nTrying to send: {{formatRequiredAmount}}\nAvailable to send: {{formatAvailableAmount}}", + "You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.\n\nTrying to send:\nAvailable to send:": "You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.\n\nTrying to send: {{formatRequiredAmount}}\nAvailable to send: {{formatAvailableAmount}}", "Bitcore Wallet Service is under maintenance. Please check https://status.bitpay.com/.": "Bitcore Wallet Service is under maintenance. Please check https://status.bitpay.com/.", "Network error": "Network error", "Wallet service not found": "Wallet service not found", @@ -1598,7 +1600,7 @@ "Insufficient confirmed funds": "Insufficient confirmed funds", "You do not have enough confirmed funds to make this payment. Wait for your pending transactions to confirm or enable \"Use unconfirmed funds\" in Advanced Settings.": "You do not have enough confirmed funds to make this payment. Wait for your pending transactions to confirm or enable \"Use unconfirmed funds\" in Advanced Settings.", "Insufficient funds": "Insufficient funds", - "Insufficient funds in your linked wallet to cover the transaction fee.": "Insufficient funds in your linked {{linkedWalletAbbreviation}} wallet to cover the transaction fee.", + "Insufficient funds in your linked wallet to cover the transaction fee.\n\nRequired Gas:\nLinked Wallet Balance:": "Insufficient funds in your linked wallet {{linkedWalletAbbreviation}} to cover the transaction fee.\n\nRequired Gas: {{formatRequiredAmount}}\nLinked Wallet Balance: {{formatAvailableAmount}}", "No compatible wallets": "No compatible wallets", "You currently don't have any wallets capable of sending this payment. Would you like to import one?": "You currently don't have any wallets capable of sending this payment. Would you like to import one?", "Maybe Later": "Maybe Later", diff --git a/src/store/wallet/effects/send/send.ts b/src/store/wallet/effects/send/send.ts index 22df9da18..49499aee8 100644 --- a/src/store/wallet/effects/send/send.ts +++ b/src/store/wallet/effects/send/send.ts @@ -1881,7 +1881,7 @@ const formatAmounts = ( dispatch: any, wallet: any, amount: number, -): {formatAvailableAmount: string; formatSendingAmount: string} => { +): {formatAvailableAmount: string; formatRequiredAmount: string} => { const _formatAvailableAmount = dispatch( FormatAmountStr( wallet.currencyAbbreviation, @@ -1892,11 +1892,19 @@ const formatAmounts = ( ), ); + const _formatRequiredAmount = dispatch( + FormatAmountStr( + wallet.currencyAbbreviation, + wallet.chain, + wallet.tokenAddress, + amount, + false, + ), + ); + const formatAvailableAmount = `~${_formatAvailableAmount}`; - const formatSendingAmount = `~${amount.toFixed( - 2, - )} ${formatCurrencyAbbreviation(wallet.currencyAbbreviation)}`; - return {formatAvailableAmount, formatSendingAmount}; + const formatRequiredAmount = `~${_formatRequiredAmount}`; + return {formatAvailableAmount, formatRequiredAmount}; }; const generateInsufficientFundsError = ( @@ -1905,23 +1913,23 @@ const generateInsufficientFundsError = ( amount: number, onDismiss?: () => void, ) => { - const {formatAvailableAmount, formatSendingAmount} = formatAmounts( + const {formatAvailableAmount, formatRequiredAmount} = formatAmounts( dispatch, wallet, amount, ); let errMsg = IsEVMChain(wallet.chain) ? t( - 'You are trying to send more funds than you have available.\n\nTrying to send: {{formatSendingAmount}}\nAvailable to send: {{formatAvailableAmount}}', + 'You are trying to send more funds than you have available.\n\nTrying to send:\nAvailable to send:', { - formatSendingAmount, + formatRequiredAmount, formatAvailableAmount, }, ) : t( - 'You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.\n\nTrying to send: {{formatSendingAmount}}\nAvailable to send: {{formatAvailableAmount}}', + 'You are trying to send more funds than you have available. Make sure you do not have funds locked by pending transaction proposals.\n\nTrying to send:\nAvailable to send:', { - formatSendingAmount, + formatRequiredAmount, formatAvailableAmount, }, ); @@ -1999,7 +2007,12 @@ const processInsufficientFunds = async ( if (useConfirmedFunds && wallet.balance.sat >= amountSat + feeRatePerKb) { return generateInsufficientConfirmedFundsError(onDismiss); } else { - return generateInsufficientFundsError(dispatch, wallet, amount, onDismiss); + return generateInsufficientFundsError( + dispatch, + wallet, + amountSat, + onDismiss, + ); } }; @@ -2016,17 +2029,33 @@ const handleDefaultError = ( const keys = getState().WALLET.keys; const {wallet, amount} = tx; - const {feeLevel} = txp; + let toShowAmount = amount; + let linkedWallet: Wallet | undefined; + if (IsERCToken(wallet.currencyAbbreviation, wallet.chain) && err.message) { + const match = err.message.match(/RequiredFee:\s*([0-9]+)/); + if (match && match[1]) { + toShowAmount = Number(match[1]); + linkedWallet = getFullLinkedWallet(keys[wallet.keyId], wallet); + } + } + + const {formatAvailableAmount, formatRequiredAmount} = formatAmounts( + dispatch, + linkedWallet ? linkedWallet : wallet, + toShowAmount, + ); const title = IsEVMChain(wallet.chain) ? t('Not enough gas for transaction') : t('Insufficient funds for fee.'); const body = IsERCToken(wallet.currencyAbbreviation, wallet.chain) ? t( - 'Insufficient funds in your linked wallet to cover the transaction fee.', + 'Insufficient funds in your linked wallet to cover the transaction fee.\n\nRequired Gas:\nLinked Wallet Balance:', { linkedWalletAbbreviation: wallet.chain === 'matic' ? 'POL' : wallet.chain.toUpperCase(), + formatRequiredAmount, + formatAvailableAmount, }, ) : t( @@ -2048,7 +2077,7 @@ const handleDefaultError = ( navigationRef.navigate('BuyCryptoRoot', { amount: 100, fromWallet: IsERCToken(wallet.currencyAbbreviation, wallet.chain) - ? getFullLinkedWallet(keys[wallet.keyId], wallet) + ? linkedWallet : wallet, }); },