Skip to content

Commit

Permalink
fix(suite-native): do not include solana staking in balance
Browse files Browse the repository at this point in the history
  • Loading branch information
PeKne committed Jan 24, 2025
1 parent 8aa03be commit 6abe311
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const AccountsListItem = ({
);

const fiatBalance = useSelector((state: NativeAccountsRootState) =>
selectAccountFiatBalance(state, account.key),
selectAccountFiatBalance(state, account.key, accountHasStaking),
);

const handleOnPress = useCallback(() => {
Expand Down
6 changes: 4 additions & 2 deletions suite-native/device-manager/src/components/WalletItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSelector } from 'react-redux';

import { TrezorDevice } from '@suite-common/suite-types';
import { selectSelectedDevice, selectDeviceByState } from '@suite-common/wallet-core';
import { selectDeviceTotalFiatBalanceNative } from '@suite-native/device';
import { selectDeviceTotalFiatBalanceByDeviceState } from '@suite-native/device';

import { WalletItemBase } from './WalletItemBase';

Expand All @@ -17,7 +17,9 @@ export const WalletItem = ({ onPress, deviceState, isSelectable = true }: Wallet
const selectedDevice = useSelector(selectSelectedDevice);
const fiatBalance = useSelector((state: any) =>
device?.state?.staticSessionId
? String(selectDeviceTotalFiatBalanceNative(state, device?.state?.staticSessionId))
? String(
selectDeviceTotalFiatBalanceByDeviceState(state, device?.state?.staticSessionId),
)
: undefined,
);

Expand Down
1 change: 1 addition & 0 deletions suite-native/device/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@trezor/connect": "workspace:*",
"@trezor/device-utils": "workspace:*",
"@trezor/styles": "workspace:*",
"@trezor/utils": "workspace:*",
"lottie-react-native": "^7.1.0",
"react": "18.2.0",
"react-native": "0.76.1",
Expand Down
55 changes: 35 additions & 20 deletions suite-native/device/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import {
selectAccountsByDeviceState,
} from '@suite-common/wallet-core';
import { isDeviceAcquired } from '@suite-common/suite-utils';
import { getTotalFiatBalance } from '@suite-common/wallet-utils';
import { getAccountFiatBalance } from '@suite-common/wallet-utils';
import { selectFiatCurrencyCode, SettingsSliceRootState } from '@suite-native/settings';
import { BigNumber } from '@trezor/utils';
import { Account, RatesByKey } from '@suite-common/wallet-types';
import { FiatCurrencyCode } from '@suite-common/suite-config';
import { doesCoinSupportStaking } from '@suite-native/staking';

import { isFirmwareVersionSupported } from './utils';

Expand Down Expand Up @@ -71,30 +75,41 @@ export const selectDeviceError = (
return device?.error;
};

// FIXME: this function can be removed and substituted with @suite-common/wallet-utils/getTotalFiatBalance when Solana supports staking on mobile.
const getTotalFiatBalanceNative = ({
deviceAccounts,
localCurrency,
rates,
}: {
deviceAccounts: Account[];
localCurrency: FiatCurrencyCode;
rates?: RatesByKey;
}) => {
let instanceBalance = new BigNumber(0);
deviceAccounts.forEach(a => {
const accountFiatBalance =
getAccountFiatBalance({
account: a,
localCurrency,
rates,
shouldIncludeStaking: doesCoinSupportStaking(a.symbol),
}) ?? '0';
instanceBalance = instanceBalance.plus(accountFiatBalance);
});

return instanceBalance.toFixed(2);
};

export const selectSelectedDeviceTotalFiatBalance = createMemoizedSelector(
[selectDeviceAccounts, selectCurrentFiatRates, selectFiatCurrencyCode],
(deviceAccounts, rates, localCurrency) => {
const fiatBalance = getTotalFiatBalance({
deviceAccounts,
localCurrency,
rates,
});

return fiatBalance.toFixed(2);
},
(deviceAccounts, rates, localCurrency) =>
getTotalFiatBalanceNative({ deviceAccounts, localCurrency, rates }),
);

export const selectDeviceTotalFiatBalanceNative = createMemoizedSelector(
export const selectDeviceTotalFiatBalanceByDeviceState = createMemoizedSelector(
[selectAccountsByDeviceState, selectCurrentFiatRates, selectFiatCurrencyCode],
(accounts, rates, localCurrency) => {
const fiatBalance = getTotalFiatBalance({
deviceAccounts: accounts,
localCurrency,
rates,
});

return fiatBalance.toFixed(2);
},
(deviceAccounts, rates, localCurrency) =>
getTotalFiatBalanceNative({ deviceAccounts, localCurrency, rates }),
);

// Unique symbols for all accounts that are on view only devices (excluding portfolio tracker)
Expand Down
3 changes: 2 additions & 1 deletion suite-native/device/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
{ "path": "../settings" },
{ "path": "../../packages/connect" },
{ "path": "../../packages/device-utils" },
{ "path": "../../packages/styles" }
{ "path": "../../packages/styles" },
{ "path": "../../packages/utils" }
],
"include": [".", "**/*.json"]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10292,6 +10292,7 @@ __metadata:
"@trezor/connect": "workspace:*"
"@trezor/device-utils": "workspace:*"
"@trezor/styles": "workspace:*"
"@trezor/utils": "workspace:*"
lottie-react-native: "npm:^7.1.0"
react: "npm:18.2.0"
react-native: "npm:0.76.1"
Expand Down

0 comments on commit 6abe311

Please sign in to comment.