Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

2.3. 如何计算Gas手续费

下面我们用实例讲解怎样计算以太坊在执行交易时花费的 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