eth_getCode - Optimism
Example code for the eth_getCode json-rpc method. Сomplete guide on how to use eth_getCode json-rpc in GetBlock.io Web3 documentation.
This method retrieves the EVM bytecode stored at a specific address. If the address is an externally owned account (EOA) or an empty address, it returns '0x'. This is useful for verifying contract deployments, analyzing contracts, and checking whether an address is a contract or an EOA. The bytecode is the compiled version of the smart contract's source code.
Parameters
address
DATA, 20 Bytes
The address to retrieve bytecode from.
Yes
block
QUANTITY|TAG
Block number in hex, or 'latest', 'earliest', 'pending'.
Yes
Request
curl --location 'https://shared.us-east-1.getblock.io/<ACCESS_TOKEN>' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"method": "eth_getCode",
"params": [
"0x4200000000000000000000000000000000000006",
"latest"
],
"id": "getblock.io"
}'import axios from 'axios'
let data = JSON.stringify({
"jsonrpc": "2.0",
"method": "eth_getCode",
"params": [
"0x4200000000000000000000000000000000000006",
"latest"
],
"id": "getblock.io"
};
let config = {
method: "post",
maxBodyLength: Infinity,
url: "https://shared.us-east-1.getblock.io/<ACCESS_TOKEN>",
headers: {
"Content-Type": "application/json",
},
data: data,
};
axios
.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
Response
A successful response returns the following:
Response Parameters
id: A unique request identifier, matching theidsent in the request body.jsonrpc: Specifies the use of JSON-RPC version 2.0.result: The bytecode at the address as a hexadecimal string. Returns'0x'for EOAs or empty addresses.
Use Case
The eth_getCode method is commonly used for:
Contract verification
EOA vs contract detection
Security analysis
Bytecode comparison
Error handling
404
Not Found
Missing or invalid ACCESS_TOKEN.
-32602
Invalid argument
The Address not 20 bytes or missing prefix
block hash isn't accurate or incomplete
keyword missing
Integration with Web3
The eth_getCode method helps developers to:
Detect whether a user is interacting with a contract or a wallet
Validate deployed bytecode for audit and debugging purposes
Build tools that identify proxies, multicall contracts, routers, and libraries
Support trustless dapps that rely on on-chain introspection
Ensure safe interactions by verifying contract code before sending transactions
Last updated
Was this helpful?