getCode - SDK

Returns the contract code of the provided address at the block. If there is no contract deployed, the result is 0x.

Don’t have an API key?

Start using this method in your app today.

Description

Returns the contract code of the provided address at the block. If there is no contract deployed, the result is 0x.

Parameters

NameTypeDescription
addressOrNamestringThe address or ENS name of the account to get the code for.
blockTagstringThe optional block number or hash. Defaults to latest if unspecified.

blockTag parameters

  • pending - A sample next block built by the client on top of latest and containing the set of transactions usually taken from local mempool. Intuitively, you can think of these as blocks that have not been mined yet.
  • latest - The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions.
  • safe - The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is “unlikely” to be re-orged. Only available on Ethereum Goerli.
  • finalized - The most recent crypto-economically secure block, that has been accepted by >2/3 of validators. Cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is very unlikely to be re-orged. Only available on Ethereum Goerli.
  • earliest - The lowest numbered block the client has available. Intuitively, you can think of this as the first block created.

Response

PropertyTypeDescription
Promise<string>stringReturns the code.

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 alchemy-sdk
yarn add alchemy-sdk

Request

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

// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {
    let address = "registrar.firefly.eth"

    //Call the method to retrieve code of address
    let response = await alchemy.core.getCode(address)

    //Logging the response to the console
    console.log(response)
};

main();

Response

0x606060405236156100885763fffffffffffffffffffffff1916600160a060020a0383161790555b50565b600654600554600160a060020a033016315b909192565b6004545b90565b80516001820190600080808060048510806106af5750601485115b156106ba5760006000fd5b600093505b8484101561072a57855160ff16925060618310806106e05750607a8360ff16115b80156106fc575060308360ff1610806106fc575060398360ff16115b5b801561070d57508260ff16602d14155b156107185760006000fd5b6001909501945b6001909301926106bf565b60045434101561073a5760006000fd5b866040518082805190602001908083835b6020831061076a5780518252601f19909201916020918201910161074b565b51815160209384036101000a60001901801990921691161790526040805192909401829003822060035483528282018190528451928390038501832060008054948401819052865160e060020a6302571be3028152600481018390529651929a509098509650600160a060020a0390921694506302571be393602480820194509192919082900301818787803b15156107ff57fe5b60325a03f1151561080c57fe5b505060405151600160a060020a031691909114905061082b5760006000fd5b60008054600354604080517f06ab5923000000000000000000000000000000000000000000000000000000008152600481019290925260248201869052600160a060020a03308116604484015290519216926306ab59239260648084019382900301818387803b151561089a57fe5b60325a03f115156108a757fe5b505060008054600154604080517f1896f70a0000000000000000000000000

Code Sandbox

You can test out the getCode method using the code sandbox below:

Use Cases

The everyday use case for the getCode method is to return the contract code of an address on the blockchain. However, here are some other possible use cases for the getCode method:

  • Contract verification: Developers can use the getCode method to verify that the bytecode of a deployed contract matches the expected bytecode. This can be used to ensure that a contract has not been tampered with or to verify that a contract has been deployed correctly.

  • Contract debugging: If a contract is not behaving as expected, developers can use getCode to retrieve the bytecode and examine it for errors or unexpected behavior.

  • Contract analysis: Researchers or security auditors might use getCode to analyze the bytecode of a contract for vulnerabilities or other issues.

  • Contract migration: If a contract needs to be migrated to a new address, developers can use getCode to retrieve the bytecode of the existing contract and then deploy it to the new address.

Overall, getCode is a useful tool for developers who are building on the EVM networks and need to interact with deployed contracts. It can be used for a variety of purposes, from debugging and analysis to migration and verification.

Related Methods

Here are the methods related to getCode:

  • getBlock: Returns the block from the network based on the provided block number or hash.
  • getBlockNumber: Returns the block number of the most recently mined block.
ReadMe