The eth_getCode
method returns the compiled bytecode of a smart contract at a specified address. This essential method allows developers to verify contract implementations, perform security audits, and distinguish between regular accounts and smart contracts on the Ethereum blockchain.
This method retrieves the EVM bytecode deployed at a specific address at the requested block height.
The address of the smart contract
Block number in hex format or block tag
The bytecode from the given address as a hexadecimal string
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x60806040526004361061016a5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde03811461016f578063095ea7b3146101f957806318160ddd1461023957806323b872dd14610260578063313ce567146102a..." // Truncated for brevity
}
selfdestruct
Different types of contracts have recognizable bytecode patterns:
0x363d3d37363d73...
// Example: Verify if an address is a contract or EOA
async function isContract(address) {
const code = await provider.send('eth_getCode', [address, 'latest']);
return code !== '0x';
}
// Example: Verify proxy implementation
async function verifyProxyImplementation(proxyAddress, expectedImplementation) {
// For EIP-1967 proxies, the implementation address is stored at a specific slot
const implementationSlot = '0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc';
const storageValue = await provider.send('eth_getStorageAt', [proxyAddress, implementationSlot, 'latest']);
// Extract address from storage value (remove padding)
const implementationAddress = '0x' + storageValue.slice(26);
// Get implementation bytecode
const implementationCode = await provider.send('eth_getCode', [implementationAddress, 'latest']);
const expectedCode = await provider.send('eth_getCode', [expectedImplementation, 'latest']);
return {
isProxy: implementationCode !== '0x',
implementationAddress,
implementationMatches: implementationCode === expectedCode,
};
}
// Usage
const addressToCheck = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'; // UNI token
const isContractResult = await isContract(addressToCheck);
console.log(`${addressToCheck} is ${isContractResult ? 'a contract' : 'an EOA'}`);
// Simple contract type detector based on bytecode patterns
async function detectContractType(address) {
const code = await provider.send('eth_getCode', [address, 'latest']);
if (code === '0x') return 'Not a contract (EOA or self-destructed)';
// Check for proxy patterns
if (code.includes('363d3d37363d73')) return 'EIP-1167 Minimal Proxy';
if (code.includes('5c60806040527f360894')) return 'EIP-1967 Transparent Proxy';
// Check common ERC standards (simplified)
if (code.includes('6e0a6eecd9d3362a2dcfb7fac5fea5d4bd0a73001ddb12dd96e21df6ab138d72'))
return 'Likely ERC-20 Token';
if (code.includes('80ac58cd')) return 'Likely ERC-721 NFT';
if (code.includes('d9b67a26')) return 'Likely ERC-1155 Multi-Token';
return 'Standard contract (type unknown)';
}