MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites
https://metamask.io
Other
11.52k stars 4.7k forks source link

fetchErc20Decimals should not use getTokenStandardAndDetails #24374

Open dbrans opened 2 weeks ago

dbrans commented 2 weeks ago

Brian Bergeron wrote: I do suspect getTokenStandardAndDetails is used in some places it shouldn't. It should only be used when we have an address and no idea what kind of token it is. Because it has to cycle through all the standards. For example this code just wants ERC20 decimals:

async function fetchErc20Decimals(address: Hex): Promise<number> {
  try {
    const { decimals } = await getTokenStandardAndDetails(address);
    return decimals ? parseInt(decimals, 10) : ERC20_DEFAULT_DECIMALS;
  } catch {
    return ERC20_DEFAULT_DECIMALS;
  }
}

Which should be 1 RPC request since we already 'know' its an ERC20, but that could do up to 4 RPC requests (checking for NFTs and returning other ERC20 details other than decimals)