Skip to Content
dApps

eth_estimateGas

トランザクションの完了に必要なガス量の見積もりを生成して返します。トランザクションはブロックチェーンに追加されません。


パラメーター

インデックス型説明
0objectトランザクションオブジェクト
1stringブロック番号またはタグ (任意)

トランザクションオブジェクト

フィールド型必須説明
fromstringいいえ送信者アドレス
tostringいいえ受取人アドレス
datastringいいえコントラクト呼び出しデータ
valuestringいいえwei 単位の値 (16 進数)
gasstringいいえガスリミット (16 進数)
gasPricestringいいえガス価格 (16 進数)

戻り値

string - 推定ガス量 (16 進数)


例

ETH 送金のガスを見積もる

const gasEstimate = await window.$onekey.ethereum.request({ method: 'eth_estimateGas', params: [{ from: '0xYourAddress...', to: '0xRecipientAddress...', value: '0xDE0B6B3A7640000' // 1 ETH }] }) console.log('Estimated gas:', parseInt(gasEstimate, 16)) // Simple ETH transfer: ~21000

コントラクト呼び出しのガスを見積もる

const gasEstimate = await window.$onekey.ethereum.request({ method: 'eth_estimateGas', params: [{ from: '0xYourAddress...', to: '0xContractAddress...', data: '0xa9059cbb...' // transfer(address,uint256) }] }) console.log('Estimated gas:', parseInt(gasEstimate, 16))

安全のための余裕を追加

async function estimateGasWithBuffer(tx, bufferPercent = 20) { const estimate = await window.$onekey.ethereum.request({ method: 'eth_estimateGas', params: [tx] }) const gas = parseInt(estimate, 16) const bufferedGas = Math.ceil(gas * (1 + bufferPercent / 100)) return '0x' + bufferedGas.toString(16) } const safeGasLimit = await estimateGasWithBuffer({ from: accounts[0], to: contractAddress, data: encodedData })

エラー

コードメッセージ説明
-32000実行がリバートトランザクションが失敗する見込み
-32000資金不足残高不足
-32602無効なパラメーター無効なトランザクションパラメーター

注意事項

  • 見積もりは正確とは限らず、実際のガス使用量は増減する場合があります
  • 安全のため、見積もりに余裕 (10-20%) を加えてください
  • 見積もりに失敗する場合、トランザクションも失敗する可能性が高いです
  • 見積もりには現在のブロックチェーンの状態が考慮されます
最終更新日