eth_getCode
Returns the bytecode at a given address.
When to Use This Method
Use eth_getCode
to:
- Verify Contract Deployment - Check if contract exists
- Get Contract Bytecode - Retrieve deployed contract code
- Distinguish EOAs from Contracts - EOAs return
0x
- Contract Verification - Compare deployed vs expected bytecode
- Security Audits - Analyze deployed contract code
Parameters
-
Address -
DATA
, 20 Bytes- Address to get code from
-
Block Parameter -
QUANTITY|TAG
latest
,earliest
,pending
, or block number
{
"jsonrpc": "2.0",
"method": "eth_getCode",
"params": [
"0x239e96c8f17C85c30100AC26F635Ea15f23E9c67",
"latest"
],
"id": 1
}
Returns
DATA
- The bytecode from the given address.
Implementation Examples
- JavaScript
- Python
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://api-zetachain-mainnet.n.dwellir.com/YOUR_API_KEY');
// Check if address is a contract
async function isContract(address) {
const code = await provider.getCode(address);
return code !== '0x';
}
// Get contract bytecode
const contractAddress = '0x239e96c8f17C85c30100AC26F635Ea15f23E9c67';
const bytecode = await provider.getCode(contractAddress);
if (bytecode !== '0x') {
console.log('Contract found, bytecode length:', bytecode.length);
} else {
console.log('No contract at this address (EOA)');
}
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-zetachain-mainnet.n.dwellir.com/YOUR_API_KEY'))
# Get contract code
address = '0x239e96c8f17C85c30100AC26F635Ea15f23E9c67'
code = w3.eth.get_code(address)
if code != b'':
print(f'Contract bytecode: {code.hex()}')
else:
print('Address is an EOA (not a contract)')
Related Methods
- eth_getStorageAt - Read contract storage
- eth_call - Execute contract function