isContractAddress - SDK

Checks if the provided address is a smart contract.

Don’t have an API key?

Start using this method in your app today. Get started for free

Description

Checks if the provided address is a smart contract.

Parameters

NameTypeDescription
addressstringThe address to check type for.

Response

PropertyTypeDescription
Promise<boolean>booleanReturns true if the provided address is a smart contract, false otherwise.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

$npm install [email protected]

Request

Here is an example of how to make an isContractAddress request using the Alchemy SDK:

isContractAddress.js
1// Imports the Alchemy SDK
2const { Alchemy, Network } = require("alchemy-sdk");
3
4// Configures the Alchemy SDK
5const config = {
6 apiKey: "demo", // Replace with your API key
7 network: Network.ETH_MAINNET, // Replace with your network
8};
9
10// Creates an Alchemy object instance with the config to use for making requests
11const alchemy = new Alchemy(config);
12
13const main = async () => {
14 const result = await alchemy.core.isContractAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"); // Wrapped ETH address
15
16 // Logging the response to the console
17 console.log(result)
18}
19
20main();

Response

json
1true

Use Cases

Here are some common use cases for the isContractAddress method:

  • Verifying the contract status of an address: It’s important to know the type of an address (Externally Owned Address or Contract Address) in many blockchain related operations. This function allows developers to verify if an address is a smart contract.

  • Improving security: Before interacting with an address, verifying its type can add an extra layer of security to prevent potential exploits ( for example, reentrancy attacks ) or mistakes.

Related Methods

Here are the methods related to isContractAddress:

  • getBalance: Returns the balance of a given address as of the provided block.