Skip to content

Commit

Permalink
bugfix(LIVE-11047): swap llm issue when trying to swap from sol with …
Browse files Browse the repository at this point in the history
…delegated account (#6044)

* fix(LIVE-11047): display spendable balance in swap account from input

* fix(LIVE-11047): changeset

* fix(LIVE-11047): clean up
  • Loading branch information
cng-ledger authored Jan 30, 2024
1 parent dabd4a8 commit 9f49142
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-maps-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

fix(LIVE-11047): display spendable balance in swap account from input in LLM
31 changes: 21 additions & 10 deletions apps/ledger-live-mobile/src/screens/Swap/Form/TxForm/From.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit 9f49142

Please sign in to comment.