eth_estimateGas
生成并返回交易完成所需的 Gas 估算值。该调用不会将交易写入区块链。
-
Object— 交易调用对象(同eth_call):字段 类型 必填 说明 from DATA 否 发送方地址 to DATA 否 接收方地址 gas QUANTITY 否 Gas 上限 gasPrice QUANTITY 否 Gas 单价 value QUANTITY 否 wei 金额 data DATA 否 调用数据 -
QUANTITY|TAG—(可选)区块号,或"latest"。
QUANTITY — 预估 Gas 数量。
curl -X POST https://eth-mainnet.blockreq.com/v1/rpc/public \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "eth_estimateGas", "params": [{ "from": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "value": "0x0", "data": "0xa9059cbb0000000000000000000000001234567890abcdef1234567890abcdef1234567800000000000000000000000000000000000000000000000000000000000f4240" }], "id": 1 }'{ "jsonrpc": "2.0", "id": 1, "result": "0xc350"}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 gas = await provider.estimateGas({ to: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", data: "0xa9059cbb..."});console.log("Estimated gas:", gas.toString());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"))gas = w3.eth.estimate_gas({ "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "data": "0xa9059cbb..."})print(f"Estimated gas: {gas}")| Method | Cost |
|---|---|
| eth_estimateGas | 20 RU |