Infura开发手册

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

eth_estimateGas

估算交易需要的gas用量。

API请求

POST https://<network>.infura.io/v3/YOUR-PROJECT-ID

请求头:

Content-Type: application/json

请求载荷为交易调用对象,结构如下:

  • from:交易发起地址,可选
  • to:交易目标地址
  • gas:交易gas用量,可选
  • gasPrice:交易gas价格,可选
  • value:交易金额,可选
  • data:交易附件数据,可选

如果未指定gas上限,geth将使用待定块中的gas limit作为上限,因此返回的估算 结果可能有偏差。

请求示例:

JSON-RPC over HTTPS POST:

curl https://mainnet.infura.io/v3/YOUR-PROJECT-ID \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_estimateGas","params": [{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155","to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas": "0x76c0","gasPrice": "0x9184e72a000","value": "0x9184e72a","data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],"id":1}'

JSON-RPC over websockets:

wscat -c wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID
>{"jsonrpc":"2.0","method":"eth_estimateGas","params": [{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155","to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas": "0x76c0","gasPrice": "0x9184e72a000","value": "0x9184e72a","data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],"id":1}

API响应

返回需要消耗的gas用量。

响应结果示例:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "0x5cec"
}