eth_getCode
返回指定地址的代码内容。可用于判断一个地址是否为智能合约地址。
DATA(20 字节)— 目标地址。QUANTITY|TAG— 区块号,或"latest"、"earliest"、"pending"。
DATA — 该地址上的代码。EOA(非合约地址)返回 0x。
# 查询 USDC 合约代码curl -X POST https://eth-mainnet.blockreq.com/v1/rpc/public \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "eth_getCode", "params": ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "latest"], "id": 1 }'{ "jsonrpc": "2.0", "id": 1, "result": "0x6080604052..."}若返回值为
"0x",表示该地址是 EOA(外部拥有账户),不是合约。
JavaScript (ethers.js v6)
Section titled “JavaScript (ethers.js v6)”import { JsonRpcProvider } from "ethers";const provider = new JsonRpcProvider("https://eth-mainnet.blockreq.com/v1/rpc/YOUR_API_KEY");const code = await provider.getCode("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");console.log("Is contract:", code !== "0x");Python (web3.py)
Section titled “Python (web3.py)”from web3 import Web3w3 = Web3(Web3.HTTPProvider("https://eth-mainnet.blockreq.com/v1/rpc/YOUR_API_KEY"))code = w3.eth.get_code("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")print(f"Is contract: {len(code) > 0}")| Method | Cost |
|---|---|
| eth_getCode | 10 RU |