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: Pass along decimal balance from asset-page to swaps UI #28707

Merged
merged 4 commits into from
Nov 26, 2024
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
20 changes: 13 additions & 7 deletions ui/pages/asset/components/asset-page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,26 @@ jest.mock('../../../hooks/useMultiPolling', () => ({
default: jest.fn(),
}));

const selectedAccountAddress = 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3';

describe('AssetPage', () => {
const mockStore = {
localeMessages: {
currentLocale: 'en',
},
metamask: {
tokenList: {},
tokenBalances: {},
tokenBalances: {
[selectedAccountAddress]: {
[CHAIN_IDS.MAINNET]: {},
},
},
marketData: {},
allTokens: {},
accountsByChainId: {
'0x1': {
'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3': {
address: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
[selectedAccountAddress]: {
address: selectedAccountAddress,
balance: '0x00',
},
},
Expand All @@ -80,9 +86,9 @@ describe('AssetPage', () => {
preferences: {},
internalAccounts: {
accounts: {
'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3': {
address: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
[selectedAccountAddress]: {
address: selectedAccountAddress,
id: selectedAccountAddress,
metadata: {
name: 'Test Account',
keyring: {
Expand All @@ -94,7 +100,7 @@ describe('AssetPage', () => {
type: EthAccountType.Eoa,
},
},
selectedAccount: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
selectedAccount: selectedAccountAddress,
},
keyrings: [
{
Expand Down
10 changes: 7 additions & 3 deletions ui/pages/asset/components/asset-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { useTokenBalances } from '../../../hooks/useTokenBalances';
import { useMultichainSelector } from '../../../hooks/useMultichainSelector';
import { getMultichainShouldShowFiat } from '../../../selectors/multichain';
import { getPortfolioUrl } from '../../../helpers/utils/portfolio';
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
import AssetChart from './chart/asset-chart';
import TokenButtons from './token-buttons';

Expand Down Expand Up @@ -151,6 +152,9 @@ const AssetPage = ({
? toChecksumHexAddress(asset.address)
: getNativeTokenAddress(chainId);

const tokenHexBalance =
selectedAccountTokenBalancesAcrossChains?.[chainId]?.[address as Hex];

const balance = calculateTokenBalance({
isNative: type === AssetType.native,
chainId,
Expand Down Expand Up @@ -187,10 +191,10 @@ const AssetPage = ({
tokenMarketDetails.allTimeHigh > 0 ||
tokenMarketDetails.allTimeLow > 0);

// this is needed in order to assign the correct balances to TokenButtons before sending/swapping
// without this, the balances we be populated as zero until the user refreshes the screen: https://github.com/MetaMask/metamask-extension/issues/28509
// this is needed in order to assign the correct balances to TokenButtons before navigating to send/swap screens

asset.balance = {
value: '', // decimal value not needed
value: hexToDecimal(tokenHexBalance),
display: String(balance),
fiat: String(tokenFiatAmount),
};
Expand Down
Loading