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: PortfolioView swap native token bug #28639

Merged
merged 5 commits into from
Nov 25, 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
17 changes: 13 additions & 4 deletions ui/pages/asset/components/asset-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,21 @@ const AssetPage = ({
const selectedAccount = useSelector(getSelectedAccount);
const currency = useSelector(getCurrentCurrency);
const conversionRate = useSelector(getConversionRate);
const isBridgeChain = useSelector(getIsBridgeChain);
const isBuyableChain = useSelector(getIsNativeTokenBuyable);
const defaultSwapsToken = useSelector(getSwapsDefaultToken, isEqual);

const { chainId, type, symbol, name, image, decimals } = asset;

// These need to be specific to the asset and not the current chain
const defaultSwapsToken = useSelector(
(state) => getSwapsDefaultToken(state, chainId),
isEqual,
);
const isSwapsChain = useSelector((state) => getIsSwapsChain(state, chainId));
const isBridgeChain = useSelector((state) =>
getIsBridgeChain(state, chainId),
);

const account = useSelector(getSelectedInternalAccount, isEqual);
const isSwapsChain = useSelector(getIsSwapsChain);
const isSigningEnabled =
account.methods.includes(EthMethod.SignTransaction) ||
account.methods.includes(EthMethod.SignUserOperation);
Expand All @@ -132,7 +142,6 @@ const AssetPage = ({
const selectedAccountTokenBalancesAcrossChains =
tokenBalances[selectedAccount.address];

const { chainId, type, symbol, name, image, decimals } = asset;
const isMetaMetricsEnabled = useSelector(getParticipateInMetaMetrics);
const isMarketingEnabled = useSelector(getDataCollectionForMarketing);
const metaMetricsId = useSelector(getMetaMetricsId);
Expand Down
17 changes: 11 additions & 6 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,14 +1495,17 @@ export function getWeb3ShimUsageStateForOrigin(state, origin) {
* objects, per the above description.
*
* @param {object} state - the redux state object
* @param {string} overrideChainId - the chainId to override the current chainId
* @returns {SwapsEthToken} The token object representation of the currently
* selected account's ETH balance, as expected by the Swaps API.
*/

export function getSwapsDefaultToken(state) {
export function getSwapsDefaultToken(state, overrideChainId = null) {
const selectedAccount = getSelectedAccount(state);
const balance = selectedAccount?.balance;
const chainId = getCurrentChainId(state);
const currentChainId = getCurrentChainId(state);

const chainId = overrideChainId ?? currentChainId;
const defaultTokenObject = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId];

return {
Expand All @@ -1516,8 +1519,9 @@ export function getSwapsDefaultToken(state) {
};
}

export function getIsSwapsChain(state) {
const chainId = getCurrentChainId(state);
export function getIsSwapsChain(state, overrideChainId) {
const currentChainId = getCurrentChainId(state);
const chainId = overrideChainId ?? currentChainId;
const isNotDevelopment =
process.env.METAMASK_ENVIRONMENT !== 'development' &&
process.env.METAMASK_ENVIRONMENT !== 'testing';
Expand All @@ -1526,8 +1530,9 @@ export function getIsSwapsChain(state) {
: ALLOWED_DEV_SWAPS_CHAIN_IDS.includes(chainId);
}

export function getIsBridgeChain(state) {
const chainId = getCurrentChainId(state);
export function getIsBridgeChain(state, overrideChainId) {
const currentChainId = getCurrentChainId(state);
const chainId = overrideChainId ?? currentChainId;
return ALLOWED_BRIDGE_CHAIN_IDS.includes(chainId);
}

Expand Down
199 changes: 199 additions & 0 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1978,4 +1978,203 @@ describe('#getConnectedSitesList', () => {
expect(selectors.getSelectedEvmInternalAccount(state)).toBe(undefined);
});
});

describe('getSwapsDefaultToken', () => {
it('returns the token object for the current chainId when no overrideChainId is provided', () => {
const expectedToken = {
symbol: 'ETH',
name: 'Ether',
address: '0x0000000000000000000000000000000000000000',
decimals: 18,
balance: '966987986469506564059',
string: '966.988',
iconUrl: './images/black-eth-logo.svg',
};

const result = selectors.getSwapsDefaultToken(mockState);

expect(result).toStrictEqual(expectedToken);
});

it('returns the token object for the overridden chainId when overrideChainId is provided', () => {
const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');
const expectedToken = {
symbol: 'POL',
name: 'Polygon',
address: '0x0000000000000000000000000000000000000000',
decimals: 18,
balance: '966987986469506564059',
string: '966.988',
iconUrl: './images/pol-token.svg',
};

const result = selectors.getSwapsDefaultToken(
mockState,
CHAIN_IDS.POLYGON,
);

expect(result).toStrictEqual(expectedToken);
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
});
});

describe('getIsSwapsChain', () => {
it('returns true for an allowed chainId in production environment', () => {
process.env.METAMASK_ENVIRONMENT = 'production';

const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'testNetworkConfigurationId', // corresponds to mainnet RPC in mockState
},
};

const result = selectors.getIsSwapsChain(state);

expect(result).toBe(true);
});

it('returns true for an allowed chainId in development environment', () => {
process.env.METAMASK_ENVIRONMENT = 'development';

const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'goerli',
},
};

const result = selectors.getIsSwapsChain(state);

expect(result).toBe(true);
});

it('returns false for a disallowed chainId in production environment', () => {
process.env.METAMASK_ENVIRONMENT = 'production';

const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
networkConfigurationsByChainId: {
'0x8080': {
chainId: '0x8080',
name: 'Custom Mainnet RPC',
nativeCurrency: 'ETH',
defaultRpcEndpointIndex: 0,
rpcEndpoints: [
{
type: 'custom',
url: 'https://testrpc.com',
networkClientId: 'fooChain',
},
],
},
},
},
};

const result = selectors.getIsSwapsChain(state);

expect(result).toBe(false);
});

it('returns false for a disallowed chainId in development environment', () => {
process.env.METAMASK_ENVIRONMENT = 'development';

const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
networkConfigurationsByChainId: {
'0x8080': {
chainId: '0x8080',
name: 'Custom Mainnet RPC',
nativeCurrency: 'ETH',
defaultRpcEndpointIndex: 0,
rpcEndpoints: [
{
type: 'custom',
url: 'https://testrpc.com',
networkClientId: 'fooChain',
},
],
},
},
},
};

const result = selectors.getIsSwapsChain(state);

expect(result).toBe(false);
});

it('respects the overrideChainId parameter', () => {
process.env.METAMASK_ENVIRONMENT = 'production';

const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');

const result = selectors.getIsSwapsChain(mockState, '0x89');
expect(result).toBe(true);
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
});
});

describe('getIsBridgeChain', () => {
it('returns true for an allowed bridge chainId', () => {
const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'testNetworkConfigurationId', // corresponds to mainnet RPC in mockState
},
};

const result = selectors.getIsBridgeChain(state);

expect(result).toBe(true);
});

it('returns false for a disallowed bridge chainId', () => {
const state = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
networkConfigurationsByChainId: {
'0x8080': {
chainId: '0x8080',
name: 'Custom Mainnet RPC',
nativeCurrency: 'ETH',
defaultRpcEndpointIndex: 0,
rpcEndpoints: [
{
type: 'custom',
url: 'https://testrpc.com',
networkClientId: 'fooChain',
},
],
},
},
},
};

const result = selectors.getIsBridgeChain(state);

expect(result).toBe(false);
});

it('respects the overrideChainId parameter', () => {
const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');

const result = selectors.getIsBridgeChain(mockState, '0x89');

expect(result).toBe(true);
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
});
});
});
Loading