知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
下面我們用實例講解怎樣計算以太坊在執行交易時花費的 gas 費用。
> miner.start(1) null
準備兩個賬號
> eth.getBalance(eth.accounts[3]) 1000000000000000000 > eth.getBalance(eth.accounts[5]) 0
開始計算 gas 費用
> var estimateGas = eth.estimateGas({from:eth.accounts[1], to: eth.accounts[2], value: web3.toWei(1)}) undefined > console.log(estimateGas) 21000 undefined > > var cost = estimateGas * gasPrice undefined > console.log(cost) 378000000000000 undefined > web3.fromWei(cost) "0.000378"
gas 花費 0.000378 ETH