Skip to content

Commit c00e047

Browse files
dev-pvltomasklim
authored andcommitted
fix(suite): add staking to the total balance
1 parent 87a1ef7 commit c00e047

File tree

6 files changed

+112
-6
lines changed

6 files changed

+112
-6
lines changed

packages/suite/src/components/wallet/WalletLayout/AccountsMenu/AccountItemsGroup.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Account } from 'src/types/wallet';
44
import { borders, spacingsPx } from '@trezor/theme';
55
import { useSelector } from 'src/hooks/suite';
66
import { selectCurrentFiatRates } from '@suite-common/wallet-core';
7-
import { getAccountAutocompoundBalance, getTokensFiatBalance } from '@suite-common/wallet-utils';
7+
import { getAccountTotalStakingBalance, getTokensFiatBalance } from '@suite-common/wallet-utils';
88
import { selectLocalCurrency } from 'src/reducers/wallet/settingsReducer';
99
import { selectRouteName } from 'src/reducers/suite/routerReducer';
1010

@@ -52,7 +52,7 @@ export const AccountItemsGroup = ({
5252
tokens,
5353
dataTestKey,
5454
}: AccountItemsGroupProps) => {
55-
const autocompoundBalance = getAccountAutocompoundBalance(account);
55+
const stakingBalance = getAccountTotalStakingBalance(account);
5656

5757
const routeName = useSelector(selectRouteName);
5858
const localCurrency = useSelector(selectLocalCurrency);
@@ -85,7 +85,7 @@ export const AccountItemsGroup = ({
8585
account={account}
8686
type="staking"
8787
isSelected={selected && routeName === 'wallet-staking'}
88-
formattedBalance={autocompoundBalance}
88+
formattedBalance={stakingBalance}
8989
isGroup
9090
isGroupSelected={selected}
9191
/>

packages/suite/src/views/dashboard/components/PortfolioCard/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ const Wrapper = styled.div`
4646
`;
4747

4848
const PortfolioCard = memo(() => {
49-
const currentRiatRates = useSelector(selectCurrentFiatRates);
49+
const currentFiatRates = useSelector(selectCurrentFiatRates);
5050
const localCurrency = useSelector(selectLocalCurrency);
5151
const { discovery, getDiscoveryStatus, isDiscoveryRunning } = useDiscovery();
5252
const accounts = useFastAccounts();
5353
const { dashboardGraphHidden } = useSelector(s => s.suite.flags);
5454
const dispatch = useDispatch();
5555

5656
const isDeviceEmpty = useMemo(() => accounts.every(a => a.empty), [accounts]);
57-
const fiatAmount = getTotalFiatBalance(accounts, localCurrency, currentRiatRates).toString();
57+
const fiatAmount = getTotalFiatBalance(accounts, localCurrency, currentFiatRates).toString();
5858

5959
const discoveryStatus = getDiscoveryStatus();
6060

suite-common/wallet-utils/src/__fixtures__/stakingUtils.ts

+70
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,73 @@ export const getAccountAutocompoundBalanceFixtures = [
143143
expectedBalance: '0',
144144
},
145145
];
146+
147+
export const getAccountTotalStakingBalanceFixtures = [
148+
{
149+
description: 'Ethereum account with valid Everstake pool',
150+
account: {
151+
networkType: 'ethereum',
152+
misc: {
153+
stakingPools: [
154+
{
155+
name: 'Everstake',
156+
autocompoundBalance: '1000000000000000000', // 1 Ether in wei
157+
claimableAmount: '500000000000000000', // 0.5 Ether in wei
158+
depositedBalance: '3000000000000000000', // 3 Ether in wei
159+
pendingBalance: '100000000000000000', // 0.1 Ether in wei
160+
pendingDepositedBalance: '200000000000000000', // 0.2 Ether in wei
161+
restakedReward: '150000000000000000', // 0.15 Ether in wei
162+
withdrawTotalAmount: '500000000000000000', // 0.5 Ether in wei
163+
},
164+
],
165+
},
166+
},
167+
expectedBalance: '1.8', // 1 + 0.1 + 0.2 + 0.5 Ether
168+
},
169+
{
170+
description: 'Ethereum account with zero balances',
171+
account: {
172+
networkType: 'ethereum',
173+
misc: {
174+
stakingPools: [
175+
{
176+
name: 'Everstake',
177+
autocompoundBalance: '0',
178+
claimableAmount: '0',
179+
depositedBalance: '0',
180+
pendingBalance: '0',
181+
pendingDepositedBalance: '0',
182+
restakedReward: '0',
183+
withdrawTotalAmount: '0',
184+
},
185+
],
186+
},
187+
},
188+
expectedBalance: '0',
189+
},
190+
{
191+
description: 'Ethereum account without Everstake pool',
192+
account: {
193+
networkType: 'ethereum',
194+
misc: {
195+
stakingPools: [],
196+
},
197+
},
198+
expectedBalance: '0',
199+
},
200+
{
201+
description: 'Non-Ethereum network with Everstake pool',
202+
account: {
203+
networkType: 'bitcoin',
204+
misc: {
205+
stakingPools: [
206+
{
207+
name: 'Everstake',
208+
autocompoundBalance: '1000000000000000000',
209+
},
210+
],
211+
},
212+
},
213+
expectedBalance: '0',
214+
},
215+
];

suite-common/wallet-utils/src/__tests__/stakingUtils.test.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { Account } from '@suite-common/wallet-types';
22

3-
import { getAccountAutocompoundBalance, getAccountEverstakeStakingPool } from '../stakingUtils';
3+
import {
4+
getAccountAutocompoundBalance,
5+
getAccountTotalStakingBalance,
6+
getAccountEverstakeStakingPool,
7+
} from '../stakingUtils';
48
import {
59
getAccountAutocompoundBalanceFixtures,
610
getAccountEverstakeStakingPoolFixtures,
11+
getAccountTotalStakingBalanceFixtures,
712
} from '../__fixtures__/stakingUtils';
813

914
describe('getAccountEverstakeStakingPool', () => {
@@ -23,3 +28,12 @@ describe('getAccountAutocompoundBalance', () => {
2328
});
2429
});
2530
});
31+
32+
describe('getAccountTotalStakingBalance', () => {
33+
getAccountTotalStakingBalanceFixtures.forEach(({ description, account, expectedBalance }) => {
34+
it(description, () => {
35+
const result = getAccountTotalStakingBalance(account as unknown as Account);
36+
expect(result).toEqual(expectedBalance);
37+
});
38+
});
39+
});

suite-common/wallet-utils/src/accountUtils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636

3737
import { toFiatCurrency } from './fiatConverterUtils';
3838
import { getFiatRateKey } from './fiatRatesUtils';
39+
import { getAccountTotalStakingBalance } from './stakingUtils';
3940

4041
export const isEthereumAccountSymbol = (symbol: NetworkSymbol) => symbol === 'eth';
4142

@@ -590,6 +591,12 @@ export const getTokensFiatBalance = (
590591
return totalBalance.toFixed();
591592
};
592593

594+
export const getStakingFiatBalance = (account: Account, rate: number | undefined) => {
595+
const balanceInEther = getAccountTotalStakingBalance(account);
596+
597+
return toFiatCurrency(balanceInEther, rate, 2);
598+
};
599+
593600
export const getAccountFiatBalance = (
594601
account: Account,
595602
localCurrency: string,
@@ -600,6 +607,7 @@ export const getAccountFiatBalance = (
600607
localCurrency as FiatCurrencyCode,
601608
);
602609
const coinFiatRate = rates?.[coinFiatRateKey];
610+
603611
if (!coinFiatRate?.rate) return null;
604612

605613
let totalBalance = new BigNumber(0);
@@ -610,8 +618,12 @@ export const getAccountFiatBalance = (
610618
// sum fiat value of all tokens
611619
const tokensBalance = getTokensFiatBalance(account, localCurrency, rates, account.tokens);
612620

621+
// account staking balance
622+
const stakingBalance = getStakingFiatBalance(account, coinFiatRate.rate);
623+
613624
totalBalance = totalBalance.plus(accountBalance ?? 0);
614625
totalBalance = totalBalance.plus(tokensBalance ?? 0);
626+
totalBalance = totalBalance.plus(stakingBalance ?? 0);
615627

616628
return totalBalance.toFixed();
617629
};

suite-common/wallet-utils/src/stakingUtils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ export const getAccountAutocompoundBalance = (account?: Account) => {
4242

4343
return pool?.autocompoundBalance ?? '0';
4444
};
45+
46+
export const getAccountTotalStakingBalance = (account?: Account) => {
47+
const pool = getAccountEverstakeStakingPool(account);
48+
49+
return new BigNumber(pool?.autocompoundBalance ?? '0')
50+
.plus(pool?.pendingBalance ?? '0')
51+
.plus(pool?.pendingDepositedBalance ?? '0')
52+
.plus(pool?.withdrawTotalAmount ?? '0')
53+
.toFixed();
54+
};

0 commit comments

Comments
 (0)