From c54a5c95a44d247e396d1cee6d9592951b4e69bf Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Fri, 10 Jan 2025 23:54:07 +0100 Subject: [PATCH] fix: gracefully handle addresses with no contract --- ui/helpers/utils/token-util.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/ui/helpers/utils/token-util.js b/ui/helpers/utils/token-util.js index 143cdf220ed3..92efd04bf555 100644 --- a/ui/helpers/utils/token-util.js +++ b/ui/helpers/utils/token-util.js @@ -12,9 +12,16 @@ import { formatCurrency } from './confirm-tx.util'; const DEFAULT_SYMBOL = ''; async function getSymbolFromContract(tokenAddress) { - const token = util.getContractAtAddress(tokenAddress); + const contract = util.getContractAtAddress(tokenAddress); + try { - return await token.symbol(); + const code = await contract.provider.getCode(tokenAddress); + + if (code === '0x') { + return ''; + } + + return await contract.symbol(); } catch (error) { log.warn( `symbol() call for token at address ${tokenAddress} resulted in error:`, @@ -25,9 +32,16 @@ async function getSymbolFromContract(tokenAddress) { } async function getNameFromContract(tokenAddress) { - const token = util.getContractAtAddress(tokenAddress); + const contract = util.getContractAtAddress(tokenAddress); + try { - return await token.name(); + const code = await contract.provider.getCode(tokenAddress); + + if (code === '0x') { + return ''; + } + + return await contract.name(); } catch (error) { log.warn( `name() call for token at address ${tokenAddress} resulted in error:`, @@ -38,10 +52,16 @@ async function getNameFromContract(tokenAddress) { } async function getDecimalsFromContract(tokenAddress) { - const token = util.getContractAtAddress(tokenAddress); + const contract = util.getContractAtAddress(tokenAddress); try { - const decimalsBN = await token.decimals(); + const code = await contract.provider.getCode(tokenAddress); + + if (code === '0x') { + return '0'; + } + + const decimalsBN = await contract.decimals(); return decimalsBN?.toString(); } catch (error) { log.warn(