Web3 by Example: Address Check

We can check if the address is a valid account or if it is a contract address.

isAddress returns true if address is valid (in any supported format).

getCode returns the contract code of address. If there is no contract currently deployed, the result is 0x.


const { ethers } = require("ethers");
(async () => {
const address = "0x67CF3bF40b2b3b3D68F6c361AEf81F8AEb2dB637";
const isValid = ethers.utils.isAddress(address);
console.log("isValid", isValid);
const provider = new ethers.providers.AlchemyProvider();
const weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const code = await provider.getCode(weth);
const isContract = code !== "0x";
console.log("isContract", isContract);


➜ node address.js
isValid true
isContract true

Next example: Querying Blocks