Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: LLM currency unit settings + drop all remaining usage of Account#unit #6791

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/cli/src/commands/blockchain/estimateMaxSpendable.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { concat, from } from "rxjs";
import { concatMap } from "rxjs/operators";
import { getAccountBridge } from "@ledgerhq/live-common/bridge/index";
import { getAccountUnit } from "@ledgerhq/live-common/account/index";
import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index";
import { getAccountCurrency } from "@ledgerhq/live-common/account/index";
import { scan, scanCommonOpts } from "../../scan";
import type { ScanCommonOpts } from "../../scan";
import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName";
import { AccountLike } from "@ledgerhq/types-live";

const format = (account, value) => {
const unit = getAccountUnit(account);
const format = (account: AccountLike, value) => {
const unit = getAccountCurrency(account).units[0];
const name = getDefaultAccountName(account);
const amount = formatCurrencyUnit(unit, value, {
showCode: true,
Expand Down
16 changes: 9 additions & 7 deletions apps/cli/src/commands/live/balanceHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import {
import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index";
import { scan, scanCommonOpts } from "../../scan";
import type { ScanCommonOpts } from "../../scan";
import type { PortfolioRange } from "@ledgerhq/types-live";
import type { Account, BalanceHistory, PortfolioRange } from "@ledgerhq/types-live";
import { getDefaultAccountNameForCurrencyIndex } from "@ledgerhq/live-wallet/accountName";

const histoFormatters = {
const histoFormatters: Record<string, (histo: BalanceHistory, account: Account) => unknown> = {
default: (histo, account) =>
histo
.map(
({ date, value }) =>
date.toISOString() +
" " +
formatCurrencyUnit(account.unit, new BigNumber(value), {
formatCurrencyUnit(account.currency.units[0], new BigNumber(value), {
showCode: true,
disableRounding: true,
}),
Expand All @@ -33,19 +33,21 @@ const histoFormatters = {
"".padStart(22) +
getDefaultAccountNameForCurrencyIndex(account) +
": " +
formatCurrencyUnit(account.unit, account.balance, {
formatCurrencyUnit(account.currency.units[0], account.balance, {
showCode: true,
disableRounding: true,
}) +
"\n" +
asciichart.plot(
history.map(h => h.value.div(new BigNumber(10).pow(account.unit.magnitude)).toNumber()),
history.map(
h => h.value / new BigNumber(10).pow(account.currency.units[0].magnitude).toNumber(),
),
{
height: 10,
format: value =>
formatCurrencyUnit(
account.unit,
new BigNumber(value).times(new BigNumber(10).pow(account.unit.magnitude)),
account.currency.units[0],
new BigNumber(value).times(new BigNumber(10).pow(account.currency.units[0].magnitude)),
{
showCode: true,
disableRounding: true,
Expand Down
15 changes: 9 additions & 6 deletions apps/cli/src/commands/ptx/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
} from "@ledgerhq/live-common/exchange/swap/types";
import { initSwap, getExchangeRates } from "@ledgerhq/live-common/exchange/swap/index";
import { getAccountBridge } from "@ledgerhq/live-common/bridge/index";
import { getAccountUnit } from "@ledgerhq/live-common/account/index";
import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index";
import invariant from "invariant";
import { Account, SignedOperation, SubAccount } from "@ledgerhq/types-live";
Expand Down Expand Up @@ -89,11 +88,15 @@ const exec = async (opts: SwapJobOpts) => {
}

console.log("\t:id:\t\t", fromAccount.id);
const formattedAmount = formatCurrencyUnit(getAccountUnit(fromAccount), fromAccount.balance, {
disableRounding: true,
alwaysShowSign: false,
showCode: true,
});
const formattedAmount = formatCurrencyUnit(
getAccountCurrency(fromAccount).units[0],
fromAccount.balance,
{
disableRounding: true,
alwaysShowSign: false,
showCode: true,
},
);

console.log("\t:balance:\t", fromAccount.spendableBalance.toString(), ` [ ${formattedAmount} ]`);

Expand Down
1 change: 0 additions & 1 deletion apps/cli/src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ export function scan(arg: ScanCommonOpts): Observable<Account> {
id,
derivationMode,
currency,
unit: currency.units[0],
index,
freshAddress: xpubOrAddress,
freshAddressPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import InputCurrency from "~/renderer/components/InputCurrency";
import IconTransfer from "~/renderer/icons/Transfer";
import { counterValueCurrencySelector } from "~/renderer/reducers/settings";
import TranslatedError from "./TranslatedError";
import { useAccountUnit } from "../hooks/useAccountUnit";

const ErrorContainer = styled(Box)<{
hasError: unknown;
Expand Down Expand Up @@ -71,7 +72,8 @@ export default function RequestAmount({
validTransactionWarning,
}: Props) {
const fiatCurrency = useSelector(counterValueCurrencySelector);
const { cryptoUnit, fiatAmount, fiatUnit, calculateCryptoAmount } = useSendAmount({
const cryptoUnit = useAccountUnit(account);
const { fiatAmount, fiatUnit, calculateCryptoAmount } = useSendAmount({
account,
fiatCurrency,
cryptoAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const renderItem = (item: { data: OptionType }) => {
</Box>

<Text ff="Inter|Regular">
{balance} {"EGLD"} {/* FIXME Should be getAccountUnit(account).code */}
{balance} {"EGLD"}
</Text>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Item = (item: { data: DelegationType }) => {
</Box>

<Text ff="Inter|Regular">
{amount} {"EGLD"} {/* FIXME Should be getAccountUnit(account).code */}
{amount} {"EGLD"}
</Text>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const renderItem = (item: { data: EnhancedUnbonding }) => {
</Box>

<Text ff="Inter|Regular">
{balance} {"EGLD"} {/* FIXME Should be getAccountUnit(account).code */}
{balance} {"EGLD"}
</Text>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import {
} from "~/renderer/screens/exchange/Swap2/Form/SwapWebView";
import { rateSelector } from "~/renderer/actions/swap";
import { getEnv } from "@ledgerhq/live-env";
import {
getAccountUnit,
getFeesCurrency,
getMainAccount,
} from "@ledgerhq/live-common/account/index";
import { getFeesCurrency, getMainAccount } from "@ledgerhq/live-common/account/index";
import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index";
import BigNumber from "bignumber.js";
import { walletSelector } from "~/renderer/reducers/wallet";
import { useMaybeAccountUnit } from "../useAccountUnit";

export type UseSwapLiveAppHookProps = {
manifestID: string | null;
Expand Down Expand Up @@ -51,10 +48,11 @@ export const useSwapLiveAppHook = (props: UseSwapLiveAppHookProps) => {
swapTransaction.swap.from.account &&
getMainAccount(swapTransaction.swap.from.account, swapTransaction.swap.from.parentAccount);
const estimatedFeesUnit = mainFromAccount && getFeesCurrency(mainFromAccount);

const unit = useMaybeAccountUnit(mainFromAccount);
const estimatedFees = useMemo(() => {
const unit = mainFromAccount && getAccountUnit(mainFromAccount);
return unit && BigNumber(formatCurrencyUnit(unit, swapTransaction.status.estimatedFees));
}, [mainFromAccount, swapTransaction.status.estimatedFees]);
}, [swapTransaction.status.estimatedFees, unit]);

const walletState = useSelector(walletSelector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ const ethMockAccount: Account = {
operations: [],
operationsCount: 0,
pendingOperations: [],
unit: {
name: "ether",
code: "ETH",
magnitude: 18,
},
currency: eth,
lastSyncDate: new Date("2023-02-14T11:01:19.252Z"),
swapHistory: [],
Expand Down Expand Up @@ -86,11 +81,6 @@ const polygonMockAccount: Account = {
operations: [],
operationsCount: 0,
pendingOperations: [],
unit: {
name: "matic",
code: "MATIC",
magnitude: 18,
},
currency: polygon,
lastSyncDate: new Date("2023-02-14T11:01:19.252Z"),
swapHistory: [],
Expand Down
9 changes: 7 additions & 2 deletions apps/ledger-live-mobile/e2e/specs/delegate/cosmos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import DeviceAction from "../../models/DeviceAction";
import { knownDevice } from "../../models/devices";
import { BigNumber } from "bignumber.js";
import { formattedAmount } from "../../models/common";
import { getAccountCurrency } from "@ledgerhq/live-common/account/index";

let portfolioPage: PortfolioPage;
let stakePage: StakePage;
Expand Down Expand Up @@ -58,8 +59,12 @@ describe("Cosmos delegate flow", () => {

const [assestsDelagated, assestsRemaining] = await stakePage.setAmount(delegatedPercent);
expect(await stakePage.cosmosDelegationSummaryValidator()).toEqual("Ledger");
expect(assestsRemaining).toEqual(formattedAmount(testedAccount.unit, remainingAmount));
expect(assestsDelagated).toEqual(formattedAmount(testedAccount.unit, delegatedAmount, true));
expect(assestsRemaining).toEqual(
formattedAmount(getAccountCurrency(testedAccount).units[0], remainingAmount),
);
expect(assestsDelagated).toEqual(
formattedAmount(getAccountCurrency(testedAccount).units[0], delegatedAmount, true),
);

await stakePage.summaryContinue();
await deviceAction.selectMockDevice();
Expand Down
7 changes: 5 additions & 2 deletions apps/ledger-live-mobile/e2e/specs/send/currencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCryptoCurrencyById,
setSupportedCurrencies,
} from "@ledgerhq/live-common/currencies/index";
import { getAccountCurrency } from "@ledgerhq/live-common/account/index";
import { loadAccounts, loadBleState, loadConfig } from "../../bridge/server";
import PortfolioPage from "../../models/wallet/portfolioPage";
import SendPage from "../../models/trade/sendPage";
Expand Down Expand Up @@ -64,9 +65,11 @@ describe("Send flow", () => {
const halfBalance = account.balance.div(2);
const amount =
// half of the balance, formatted with the same unit as what the input should use
formatCurrencyUnit(account.unit, halfBalance, { useGrouping: false });
formatCurrencyUnit(getAccountCurrency(account).units[0], halfBalance, {
useGrouping: false,
});

const amountWithCode = formattedAmount(account.unit, halfBalance);
const amountWithCode = formattedAmount(getAccountCurrency(account).units[0], halfBalance);

await portfolioPage.openViaDeeplink();
await sendPage.openViaDeeplink();
Expand Down
7 changes: 4 additions & 3 deletions apps/ledger-live-mobile/src/components/AccountCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { ReactNode } from "react";
import { getAccountSpendableBalance } from "@ledgerhq/live-common/account/index";
import { getAccountCurrency, getAccountUnit } from "@ledgerhq/live-common/account/helpers";
import { getAccountCurrency } from "@ledgerhq/live-common/account/helpers";
import { getTagDerivationMode } from "@ledgerhq/coin-framework/derivation";
import { AccountLike, Account, DerivationMode } from "@ledgerhq/types-live";
import { Flex, Tag, Text } from "@ledgerhq/native-ui";
Expand All @@ -13,6 +13,7 @@ import CurrencyIcon from "./CurrencyIcon";
import CurrencyUnitValue from "./CurrencyUnitValue";
import CounterValue from "./CounterValue";
import { useMaybeAccountName } from "~/reducers/wallet";
import { useMaybeAccountUnit } from "~/hooks/useAccountUnit";

export type Props = CardProps & {
account?: AccountLike | null;
Expand Down Expand Up @@ -41,9 +42,9 @@ const AccountCard = ({
const accountNameFromStore = useMaybeAccountName(account);
const accountName = overridesName || accountNameFromStore;
const parentName = useMaybeAccountName(parentAccount);
if (!account) return null;
const unit = useMaybeAccountUnit(account);
if (!account || !unit) return null;
const currency = getAccountCurrency(account);
const unit = getAccountUnit(account);
const tag =
account.type === "Account" &&
account?.derivationMode !== undefined &&
Expand Down
7 changes: 4 additions & 3 deletions apps/ledger-live-mobile/src/components/AccountGraphCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
BalanceHistoryWithCountervalue,
} from "@ledgerhq/types-live";
import { Unit, Currency } from "@ledgerhq/types-cryptoassets";
import { getAccountCurrency, getAccountUnit } from "@ledgerhq/live-common/account/index";
import { getAccountCurrency } from "@ledgerhq/live-common/account/index";
import { Box, Flex, Text, Transitions, InfiniteLoader, GraphTabs, Tag } from "@ledgerhq/native-ui";
import { useTranslation } from "react-i18next";
import { getCurrencyColor } from "@ledgerhq/live-common/currencies/index";
Expand All @@ -31,6 +31,7 @@ import { BaseNavigatorStackParamList } from "./RootNavigator/types/BaseNavigator
import { GraphPlaceholder } from "./Graph/Placeholder";
import { tokensWithUnsupportedGraph } from "./Graph/tokensWithUnsupportedGraph";
import { useAccountName, useMaybeAccountName } from "~/reducers/wallet";
import { useAccountUnit } from "~/hooks/useAccountUnit";

const { width } = getWindowDimensions();

Expand Down Expand Up @@ -80,7 +81,7 @@ function AccountGraphCard({
parentAccount,
}: Props) {
const currency = getAccountCurrency(account);

const unit = useAccountUnit(account);
const { colors } = useTheme();
const { t } = useTranslation();

Expand Down Expand Up @@ -143,7 +144,7 @@ function AccountGraphCard({
onSwitchAccountCurrency={onSwitchAccountCurrency}
counterValueUnit={counterValueCurrency.units[0]}
useCounterValue={useCounterValue}
cryptoCurrencyUnit={getAccountUnit(account)}
cryptoCurrencyUnit={unit}
item={hoveredItem || history[history.length - 1]}
valueChange={valueChange}
parentAccount={parentAccount}
Expand Down
7 changes: 6 additions & 1 deletion apps/ledger-live-mobile/src/components/AccountSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { accountsSelector } from "~/reducers/accounts";
import { walletSelector } from "~/reducers/wallet";
import { accountNameWithDefaultSelector } from "@ledgerhq/live-wallet/store";

const SEARCH_KEYS = ["name", "account.unit.code", "account.token.name", "account.token.ticker"];
const SEARCH_KEYS = [
"name",
"account.currency.units[0].code",
"account.token.name",
"account.token.ticker",
];

type Props = {
list: AccountLike[];
Expand Down
5 changes: 3 additions & 2 deletions apps/ledger-live-mobile/src/components/DelegationDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import type { ComponentType } from "react";
import { View, StyleSheet, ScrollView } from "react-native";
import type { StyleProp, ViewStyle } from "react-native";
import { getAccountCurrency, getAccountUnit } from "@ledgerhq/live-common/account/index";
import { getAccountCurrency } from "@ledgerhq/live-common/account/index";
import { getCurrencyColor } from "@ledgerhq/live-common/currencies/index";
import type { AccountLike } from "@ledgerhq/types-live";
// TODO move to component
Expand All @@ -19,6 +19,7 @@ import CurrencyUnitValue from "./CurrencyUnitValue";
import CounterValue from "./CounterValue";
import CurrencyIcon from "./CurrencyIcon";
import { normalize } from "~/helpers/normalizeSize";
import { useAccountUnit } from "~/hooks/useAccountUnit";

const { height } = getWindowDimensions();
type Props = {
Expand Down Expand Up @@ -50,7 +51,7 @@ export default function DelegationDrawer({
}: Props) {
const currency = getAccountCurrency(account);
const color = getCurrencyColor(currency);
const unit = getAccountUnit(account);
const unit = useAccountUnit(account);
const iconWidth = normalize(64);
return (
<QueuedDrawer style={styles.modal} isRequestingToBeOpened={isOpen} onClose={onClose}>
Expand Down
3 changes: 3 additions & 0 deletions apps/ledger-live-mobile/src/components/DeviceAction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import DeviceActionProgress from "../DeviceActionProgress";
import { PartialNullable } from "~/types/helpers";
import ModalLock from "../ModalLock";
import { walletSelector } from "~/reducers/wallet";
import { settingsStoreSelector } from "~/reducers/settings";

type LedgerError = InstanceType<LedgerErrorConstructor<{ [key: string]: unknown }>>;

Expand Down Expand Up @@ -233,6 +234,7 @@ export function DeviceActionDefaultRendering<R, H extends Status, P>({
}, [error, onError]);

const walletState = useSelector(walletSelector);
const settingsState = useSelector(settingsStoreSelector);

if (displayUpgradeWarning && appAndVersion) {
return renderWarningOutdated({
Expand Down Expand Up @@ -403,6 +405,7 @@ export function DeviceActionDefaultRendering<R, H extends Status, P>({
amountExpectedTo: status.amountExpectedTo,
estimatedFees: status.estimatedFees,
walletState,
settingsState,
});
}

Expand Down
Loading
Loading