From c51233f854048ee3fd7144d394a1f0f99970e49e Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 15 Feb 2024 09:25:28 -0300 Subject: [PATCH] [FIX] paypro handling and show all wallets in confirm view --- src/navigation/wallet/screens/send/SendTo.tsx | 23 +-- src/store/wallet/utils/wallet.ts | 182 ++++++++++-------- 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/src/navigation/wallet/screens/send/SendTo.tsx b/src/navigation/wallet/screens/send/SendTo.tsx index 5c925bf19e..c66f436aa1 100644 --- a/src/navigation/wallet/screens/send/SendTo.tsx +++ b/src/navigation/wallet/screens/send/SendTo.tsx @@ -396,29 +396,10 @@ const SendTo = () => { GetInvoiceCurrency(currencyAbbreviation).toLowerCase(), chain, ); - const OptSelected = payProOptions.paymentOptions.find( - (option: PayProPaymentOption) => option.selected, - ); - - let selected: PayProPaymentOption | undefined; - if (OptSelected) { - if (invoiceCurrency === OptSelected.currency) { - selected = OptSelected; - } else { - logger.warn( - 'PayPro opt selected (v3) and wallet selected network/coin mismatch', - ); - return Promise.resolve({ - isValid: false, - invalidReason: 'invalidCurrency', - }); - } - } else { - selected = payProOptions.paymentOptions.find( + const selected: PayProPaymentOption | undefined = payProOptions.paymentOptions.find( (option: PayProPaymentOption) => invoiceCurrency === option.currency, ); - } if (selected) { const isValid = dispatch(checkCoinAndNetwork(selected, true)); @@ -426,7 +407,7 @@ const SendTo = () => { return Promise.resolve({isValid: true, invalidReason: undefined}); } else { logger.warn( - 'PayPro (v4) and wallet selected network/coin mismatch', + 'PayPro and wallet selected network/coin invalid', ); return Promise.resolve({ isValid: false, diff --git a/src/store/wallet/utils/wallet.ts b/src/store/wallet/utils/wallet.ts index 37bba2f874..b52efa1ec3 100644 --- a/src/store/wallet/utils/wallet.ts +++ b/src/store/wallet/utils/wallet.ts @@ -30,7 +30,11 @@ import { } from '../../../utils/helper-methods'; import {WALLET_DISPLAY_LIMIT} from '../../../navigation/tabs/home/components/Wallet'; import {Network} from '../../../constants'; -import {GetInvoiceCurrency, PayProOptions} from '../effects/paypro/paypro'; +import { + GetInvoiceCurrency, + PayProOptions, + PayProPaymentOption, +} from '../effects/paypro/paypro'; import {Effect} from '../..'; import { CoinbaseAccountProps, @@ -517,92 +521,108 @@ export const BuildKeysAndWalletsList = ({ rates: Rates; dispatch: AppDispatch; }) => { - const selectedPaymentOptions = payProOptions?.paymentOptions?.filter( - option => option.selected, - ); - const paymentOptions = selectedPaymentOptions?.length - ? selectedPaymentOptions - : payProOptions?.paymentOptions; + const paymentOptions = payProOptions?.paymentOptions; + + const isWalletCompatible = ( + wallet: Wallet, + network: string | undefined, + paymentOptions: PayProPaymentOption[] | undefined, + ) => { + if (paymentOptions?.length) { + return paymentOptions.some( + ({currency, network: optionNetwork}) => + getCurrencyCodeFromCoinAndChain( + GetInvoiceCurrency(wallet.currencyAbbreviation).toLowerCase(), + wallet.chain, + ) === currency && wallet.network === optionNetwork, + ); + } + if (network) { + return network === wallet.network; + } + return true; + }; + + const transformWallet = ( + walletObj: Wallet, + defaultAltCurrencyIsoCode: string, + rates: Rates, + dispatch: AppDispatch, + ) => { + const { + currencyAbbreviation, + hideWallet, + balance, + network, + chain, + credentials, + walletName, + tokenAddress, + } = walletObj; + const {walletName: fallbackName} = credentials; + return { + ...walletObj, + cryptoBalance: balance.crypto, + fiatBalance: formatFiatAmount( + convertToFiat( + dispatch( + toFiat( + balance.sat, + defaultAltCurrencyIsoCode, + currencyAbbreviation, + chain, + rates, + tokenAddress, + ), + ), + hideWallet, + network, + ), + defaultAltCurrencyIsoCode, + ), + cryptoLockedBalance: balance.cryptoLocked, + fiatLockedBalance: formatFiatAmount( + convertToFiat( + dispatch( + toFiat( + balance.satLocked, + defaultAltCurrencyIsoCode, + currencyAbbreviation, + chain, + rates, + tokenAddress, + ), + ), + hideWallet, + network, + ), + defaultAltCurrencyIsoCode, + ), + network, + walletName: walletName || fallbackName, + }; + }; + return Object.keys(keys) .map(keyId => { const keyObj = keys[keyId]; return { key: keyId, keyName: keyObj.keyName || 'My Key', - wallets: keys[keyId].wallets - .filter(wallet => !wallet.hideWallet) - .filter(wallet => { - if (paymentOptions?.length) { - return paymentOptions.some( - ({currency, network: optionNetwork}) => { - return ( - getCurrencyCodeFromCoinAndChain( - GetInvoiceCurrency( - wallet.currencyAbbreviation, - ).toLowerCase(), - wallet.chain, - ) === currency && wallet.network === optionNetwork - ); - }, - ); - } - if (network) { - return network === wallet.network; - } - return true; - }) - .map(walletObj => { - const { - currencyAbbreviation, - hideWallet, - balance, - network, - chain, - credentials: {walletName: fallbackName}, - walletName, - tokenAddress, - } = walletObj; - return merge(cloneDeep(walletObj), { - cryptoBalance: balance.crypto, - fiatBalance: formatFiatAmount( - convertToFiat( - dispatch( - toFiat( - balance.sat, - defaultAltCurrencyIsoCode, - currencyAbbreviation, - chain, - rates, - tokenAddress, - ), - ), - hideWallet, - network, - ), - defaultAltCurrencyIsoCode, - ), - cryptoLockedBalance: balance.cryptoLocked, - fiatLockedBalance: formatFiatAmount( - convertToFiat( - dispatch( - toFiat( - balance.satLocked, - defaultAltCurrencyIsoCode, - currencyAbbreviation, - chain, - rates, - tokenAddress, - ), - ), - hideWallet, - network, - ), - defaultAltCurrencyIsoCode, - ), - network, - walletName: walletName || fallbackName, - }); - }), + wallets: keyObj.wallets + .filter( + wallet => + !wallet.hideWallet && + isWalletCompatible(wallet, network, paymentOptions), + ) + .map(walletObj => + transformWallet( + walletObj, + defaultAltCurrencyIsoCode, + rates, + dispatch, + ), + ), }; }) .filter(key => key.wallets.length);