Confirm a token contract against the official address with viem
A wallet "import token" prompt, or a claim page that shows a token contract, is offering you an address. The only address that matters is the one on the project's own documentation for that chain. A look-alike ticker, or
A wallet "import token" prompt, or a claim page that shows a token contract, is offering you an address. The only address that matters is the one on the project's own documentation for that chain. A look-alike ticker, or the right address on the wrong network, is a different asset.
This walkthrough uses viem to check a candidate address against an official one: checksum it, confirm the RPC is on the intended chain, confirm there is contract code, and read the ERC-20 name, symbol, and decimals. It does not send a transaction, and it does not ask you to connect a wallet. This article is published by CryptoEarnLab, the site I write for.
The example uses Circle's USDC addresses because Circle publishes them in one official table. The steps and the script below apply only to EVM chains and ERC-20 tokens. They cannot verify a Solana mint, or a token on another non-EVM network; those need tools built for that network and token standard. For airdrops, the official contract or mint address is still one of the things you check when tokens are distributed; CryptoEarnLab's page on how an airdrop records the official token contract or mint address walks through that step in the claim process. The code in this article covers the EVM/ERC-20 case only.
Start from the issuer's page, not from the import prompt
Copy the address from the project's current documentation, for the specific chain you intend to use. Circle's USDC contract addresses page, checked on 5 December 2026, lists a different contract on each supported chain. On that date, Ethereum mainnet USDC is listed as 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, and Ethereum Sepolia USDC is listed as 0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238. Those are not interchangeable. Circle's docs also say to look the address up per chain and not to hardcode it, so treat the copy in the script below as a snapshot and re-read the official page before you rely on it.
The same page warns that mainnet tokens have financial value, and that testnet USDC has no financial value and is not backed by dollars. A Sepolia "USDC" contract is not a claim on the mainnet token, even if the ticker looks the same.
What the script checks
confirm-token.mjs:
import {
createPublicClient,
http,
getAddress,
isAddressEqual,
erc20Abi,
} from 'viem'
import { mainnet } from 'viem/chains'
// Snapshot of Circle's Ethereum mainnet USDC address, copied from
// https://developers.circle.com/stablecoins/usdc-contract-addresses
// on 5 December 2026. Recheck that page; do not treat this file as the
// official source.
const OFFICIAL = getAddress(
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
)
const rpcUrl = process.env.ETHEREUM_RPC_URL
if (!rpcUrl) throw new Error('Set ETHEREUM_RPC_URL')
const candidate = getAddress(process.argv[2] ?? OFFICIAL)
const client = createPublicClient({
chain: mainnet,
transport: http(rpcUrl),
})
const chainId = await client.getChainId()
if (chainId !== mainnet.id) {
throw new Error(
`Expected chain ${mainnet.id}, but this endpoint reports ${chainId}`,
)
}
if (!isAddressEqual(candidate, OFFICIAL)) {
throw new Error('Candidate does not match the official address in this file')
}
const bytecode = await client.getCode({ address: candidate })
if (!bytecode) {
throw new Error('No contract code at this address on this chain')
}
const metadataKeys = ['name', 'symbol', 'decimals']
const settled = await Promise.allSettled(
metadataKeys.map((functionName) =>
client.readContract({ address: candidate, abi: erc20Abi, functionName }),
),
)
const metadata = {}
for (const [i, result] of settled.entries()) {
const key = metadataKeys[i]
if (result.status === 'fulfilled') {
metadata[key] = result.value
} else {
const reason = result.reason instanceof Error ? result.reason.message : result.reason
console.log(`Optional ERC-20 ${key}() is missing or failed:`, reason)
metadata[key] = undefined
}
}
console.log({ address: candidate, chainId, ...metadata })
getAddress checksums the string, following viem's getAddress docs. A typo that is not a valid address fails there. isAddressEqual, per the isAddressEqual docs, compares the checksummed forms, so a lower-case paste of the same official address still matches, and a different address does not.
getChainId asks the endpoint which chain it is serving. Passing chain: mainnet only tells viem which chain you intend. If ETHEREUM_RPC_URL actually points at another network, the script stops. That matters because the official USDC address on Ethereum is not the official address on Sepolia, or on any other chain in Circle's table.
getCode calls eth_getCode, as documented in the getCode docs. It returns the bytecode, or undefined if there is none. An empty result means you are looking at an ordinary account on that chain, not a token contract.
readContract then calls the read-only ERC-20 methods. EIP-20 defines name, symbol, and decimals as optional: each "can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present." ethereum.org's ERC-20 page lists the same three methods, but they are not required to confirm the official address. The script treats that metadata as a supporting consistency check against the official docs, and it continues if one of those calls fails. Two unrelated contracts can choose the same ticker.
Run it with an RPC URL you chose, and optionally with a candidate address:
ETHEREUM_RPC_URL="https://your-ethereum-rpc.example" node confirm-token.mjs
ETHEREUM_RPC_URL="https://your-ethereum-rpc.example" node confirm-token.mjs 0xCandidate
The example URL is a placeholder. The HTTP transport docs recommend passing an explicit URL rather than relying on a default public endpoint.
What this does not prove
A matching address, bytecode, and ticker only show that you are looking at the contract the official page named, on the chain the endpoint reports. They do not show that a later approval is safe, that a claim page is genuine, or that you should import the token into a wallet that already holds other assets.
If the candidate does not match, stop. Do not "fix" it by importing the look-alike anyway. If you are confirming a token you were offered in an airdrop or claim flow, go back to the project's own docs and start from the address published there.
This article was drafted with AI assistance and reviewed by the CryptoEarnLab team.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.