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

fix(suite-native): do not include solana staking in balance #16570

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
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
Loading