Skip to content

Commit

Permalink
fix: undefined market data selector (#26264)
Browse files Browse the repository at this point in the history
## **Description**

Fixes an issue when `Show balance and token price checker` is turned off
in settings, certain actions like sending, importing, or swapping a
token crash metamask.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26264?quickstart=1)

## **Related issues**

Fixes: #26243

## **Manual testing steps**

1. Go into settings > security and privacy
2. Turn off `Show balance and token price checker`
3. Switch chains
4. Click import tokens on the tokens tab. Modal should popup without
error
5. Click swap and enter tokens + amount. Swap rate should fetch
successfully.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
bergeron authored and dawnseeker8 committed Aug 12, 2024
1 parent c266c78 commit cceb4f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/scripts/controllers/swaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export default class SwapsController extends BaseController<
): Promise<[string | null, Record<string, Quote>] | Record<string, never>> {
const { marketData } = this._getTokenRatesState();
const chainId = this._getCurrentChainId();
const tokenConversionRates = marketData[chainId];
const tokenConversionRates = marketData?.[chainId] ?? {};

const { customGasPrice, customMaxPriorityFeePerGas } =
this.state.swapsState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getTokenList,
getCurrentNetwork,
getTestNetworkBackgroundColor,
contractExchangeRateSelector,
getTokenExchangeRates,
} from '../../../selectors';
import {
addImportedTokens,
Expand Down Expand Up @@ -137,7 +137,7 @@ export const ImportTokensModal = ({ onClose }) => {
const accounts = useSelector(getInternalAccounts);
const tokens = useSelector((state) => state.metamask.tokens);
const rpcPrefs = useSelector(getRpcPrefsForCurrentProvider);
const contractExchangeRates = useSelector(contractExchangeRateSelector);
const contractExchangeRates = useSelector(getTokenExchangeRates);

const [customAddress, setCustomAddress] = useState('');
const [customAddressError, setCustomAddressError] = useState(null);
Expand Down
16 changes: 2 additions & 14 deletions ui/selectors/confirm-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
checkNetworkAndAccountSupports1559,
getCurrentChainId,
getMetaMaskAccounts,
getTokenExchangeRates,
} from './selectors';
import {
getUnapprovedTransactions,
Expand Down Expand Up @@ -143,19 +144,6 @@ export const txDataSelector = (state) => state.confirmTransaction.txData;
const tokenDataSelector = (state) => state.confirmTransaction.tokenData;
const tokenPropsSelector = (state) => state.confirmTransaction.tokenProps;

const contractExchangeRatesSelector = (state) => {
const chainId = getCurrentChainId(state);
const contractMarketData = state.metamask.marketData?.[chainId];

return Object.entries(contractMarketData).reduce(
(acc, [address, marketData]) => {
acc[address] = marketData?.price ?? null;
return acc;
},
{},
);
};

const tokenDecimalsSelector = createSelector(
tokenPropsSelector,
(tokenProps) => tokenProps && tokenProps.decimals,
Expand Down Expand Up @@ -208,7 +196,7 @@ export const sendTokenTokenAmountAndToAddressSelector = createSelector(
);

export const contractExchangeRateSelector = createSelector(
contractExchangeRatesSelector,
(state) => getTokenExchangeRates(state),
tokenAddressSelector,
(contractExchangeRates, tokenAddress) => {
return contractExchangeRates[
Expand Down

0 comments on commit cceb4f4

Please sign in to comment.