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: undefined market data selector #26264

Merged
merged 3 commits into from
Aug 2, 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
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think the previous selector made sense. It only returned the exchange rate of the current token being transacted. Whereas the variable is accessed like an object containing rates for multiple tokens.


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 @@ -153,19 +154,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;
},
{},
);
};
Comment on lines -156 to -167
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This chunk was identical to the getTokenExchangeRates selector, except it did not include a ?? {} to handle the undefined case. So I just made it re-use that selector instead.


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

export const contractExchangeRateSelector = createSelector(
contractExchangeRatesSelector,
(state) => getTokenExchangeRates(state),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You'd think getTokenExchangeRates would work here instead of a lambda, but the tests don't like it. I suspect that's why the original code duplicated the selector.

 FAIL  app/scripts/metamask-controller.actions.test.js
  ● Test suite failed to run

    Selector creators expect all input-selectors to be functions, instead received the following types: [undefined, function]

      206 | );
      207 |
    > 208 | export const contractExchangeRateSelector = createSelector(
          |                                                           ^
      209 |   getTokenExchangeRates,

I've encountered this situation a few times, and never discovered why it happens. I think you can also fix it by stubbing the selector in the tests. But the lambda seems to work too.

Copy link
Member

Choose a reason for hiding this comment

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

This error is caused by the function being undefined at the point in time this is executed. This could happen because the function is defined later in the module (as a variable, so not hoisted to the top like function declarations are), but more often I've seen this happen due to a circular dependency between the current file and the file we're importing the selector from.

One of the tech debt tasks in-progress now is an effort to remove all circular dependencies in our codebase and implement a lint rule preventing their re-introduction, so hopefully we can resolve this soon. But in the meantime this solution works.

tokenAddressSelector,
(contractExchangeRates, tokenAddress) => {
return contractExchangeRates[
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code below could probably look up via the checksummed address instead of the O(n) case insensitive search

Expand Down