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

feat: improve stake balance calculation #12284

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 13 additions & 8 deletions app/components/UI/Stake/hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ const useBalance = () => {
? accountsByChainId[chainId]?.[selectedAddress]?.balance
: '0';

const stakedBalance = selectedAddress
const rawStakedBalance = selectedAddress
? accountsByChainId[chainId]?.[selectedAddress]?.stakedBalance || '0'
: '0';

const stakedBalance = hexToBN(rawStakedBalance).lte(hexToBN('1'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this balance is 1, we can unstake it with the bug fix for unstaking all staked balance

? '0'
: rawStakedBalance;

const balanceETH = useMemo(
() => renderFromWei(rawAccountBalance),
[rawAccountBalance],
Expand Down Expand Up @@ -62,13 +66,14 @@ const useBalance = () => {
);

const formattedStakedBalanceFiat = useMemo(
() => weiToFiat(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hexToBN(stakedBalance) as any,
conversionRate,
currentCurrency,
),
() =>
weiToFiat(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hexToBN(stakedBalance) as any,
conversionRate,
currentCurrency,
),
[currentCurrency, stakedBalance, conversionRate],
);

Expand Down
38 changes: 13 additions & 25 deletions app/components/Views/Wallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import { PortfolioBalance } from '../../UI/Tokens/TokenList/PortfolioBalance';
import useCheckNftAutoDetectionModal from '../../hooks/useCheckNftAutoDetectionModal';
import useCheckMultiRpcModal from '../../hooks/useCheckMultiRpcModal';
import { selectContractBalances } from '../../../selectors/tokenBalancesController';
import useBalance from '../../UI/Stake/hooks/useBalance';

const createStyles = ({ colors, typography }: Theme) =>
StyleSheet.create({
Expand Down Expand Up @@ -326,6 +327,8 @@ const Wallet = ({
*/
useCheckMultiRpcModal();

const { stakedBalanceWei, formattedStakedBalanceFiat } = useBalance();

/**
* Callback to trigger when pressing the navigation title.
*/
Expand Down Expand Up @@ -530,15 +533,10 @@ const Wallet = ({
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let balance: any = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stakedBalance: any = 0;

const assets = [
...(tokens || []),
];
const assets = [...(tokens || [])];

if (accountBalanceByChainId) {

balance = renderFromWei(accountBalanceByChainId.balance);
const nativeAsset = {
// TODO: Add name property to Token interface in controllers.
Expand All @@ -559,27 +557,17 @@ const Wallet = ({
} as any;
assets.push(nativeAsset);

let stakedAsset;
if (accountBalanceByChainId.stakedBalance) {
stakedBalance = renderFromWei(accountBalanceByChainId.stakedBalance);
stakedAsset = {
...nativeAsset,
nativeAsset,
name: 'Staked Ethereum',
isStaked: true,
balance: stakedBalance,
balanceFiat: weiToFiat(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hexToBN(accountBalanceByChainId.stakedBalance) as any,
conversionRate,
currentCurrency,
),
const stakedAsset = {
...nativeAsset,
nativeAsset,
name: 'Staked Ethereum',
isStaked: true,
balance: renderFromWei(stakedBalanceWei),
balanceFiat: formattedStakedBalanceFiat,
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
assets.push(stakedAsset);
}
} as any;
assets.push(stakedAsset);
}

return (
Expand Down
Loading