From 9f49142241b44b010f740486c5ddbd42b1904665 Mon Sep 17 00:00:00 2001 From: C Ng <138497251+cng-ledger@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:43:53 +0000 Subject: [PATCH] bugfix(LIVE-11047): swap llm issue when trying to swap from sol with delegated account (#6044) * fix(LIVE-11047): display spendable balance in swap account from input * fix(LIVE-11047): changeset * fix(LIVE-11047): clean up --- .changeset/great-maps-warn.md | 5 +++ .../src/screens/Swap/Form/TxForm/From.tsx | 31 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 .changeset/great-maps-warn.md diff --git a/.changeset/great-maps-warn.md b/.changeset/great-maps-warn.md new file mode 100644 index 000000000000..92f6b3e1e35f --- /dev/null +++ b/.changeset/great-maps-warn.md @@ -0,0 +1,5 @@ +--- +"live-mobile": patch +--- + +fix(LIVE-11047): display spendable balance in swap account from input in LLM diff --git a/apps/ledger-live-mobile/src/screens/Swap/Form/TxForm/From.tsx b/apps/ledger-live-mobile/src/screens/Swap/Form/TxForm/From.tsx index 81dd7c7cd9f2..416883318e39 100644 --- a/apps/ledger-live-mobile/src/screens/Swap/Form/TxForm/From.tsx +++ b/apps/ledger-live-mobile/src/screens/Swap/Form/TxForm/From.tsx @@ -2,7 +2,11 @@ import React, { useCallback, useMemo } from "react"; import { useTranslation } from "react-i18next"; import { useNavigation } from "@react-navigation/native"; import { Flex, Text } from "@ledgerhq/native-ui"; -import { getAccountName, getAccountUnit } from "@ledgerhq/live-common/account/index"; +import { + getAccountName, + getAccountSpendableBalance, + getAccountUnit, +} from "@ledgerhq/live-common/account/index"; import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index"; import { useFetchCurrencyFrom, @@ -11,6 +15,7 @@ import { } from "@ledgerhq/live-common/exchange/swap/hooks/index"; import { SwapTransactionType } from "@ledgerhq/live-common/exchange/swap/types"; import { WarningSolidMedium } from "@ledgerhq/native-ui/assets/icons"; +import { Currency } from "@ledgerhq/types-cryptoassets"; import { Selector } from "./Selector"; import { AmountInput } from "./AmountInput"; import { SwapFormParamList } from "../../types"; @@ -20,6 +25,7 @@ import { useAnalytics } from "~/analytics"; import { sharedSwapTracking } from "../../utils"; import { flattenAccountsSelector } from "~/reducers/accounts"; import { useSelector } from "react-redux"; +import { AccountLike } from "@ledgerhq/types-live"; interface Props { provider?: string; @@ -36,22 +42,27 @@ export function From({ swapTx, provider, swapError, swapWarning, isSendMaxLoadin const { data: currenciesFrom } = useFetchCurrencyFrom(); const flattenedAccounts = useSelector(flattenAccountsSelector); const accounts = useSwapableAccounts({ accounts: flattenedAccounts }); + + const getAccountBalance = useCallback( + (inputs: { account?: AccountLike; currency?: Currency }) => { + if (!inputs.account || !inputs.currency) return ""; + const balance = getAccountSpendableBalance(inputs.account); + return formatCurrencyUnit(inputs.currency.units[0], balance, { + showCode: true, + }); + }, + [], + ); + const { name, balance, unit } = useMemo(() => { const { currency, account } = swapTx.swap.from; - return { account, name: account && getAccountName(account), - balance: - (account && - currency && - formatCurrencyUnit(currency.units[0], account.balance, { - showCode: true, - })) ?? - "", + balance: getAccountBalance({ account, currency }), unit: account && getAccountUnit(account), }; - }, [swapTx.swap.from]); + }, [swapTx.swap.from, getAccountBalance]); usePickDefaultAccount(accounts, swapTx.swap.from.account, swapTx.setFromAccount);