Skip to content

Commit

Permalink
fix: gracefully handle addresses with no contract
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s committed Jan 10, 2025
1 parent f3d70d1 commit c54a5c9
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions ui/helpers/utils/token-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:`,
Expand All @@ -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:`,
Expand All @@ -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(
Expand Down

0 comments on commit c54a5c9

Please sign in to comment.