主页 > imtoken钱包官方版下载教程 > 以太坊中的 gas、gasPrice 和 gasLimit 是什么?
以太坊中的 gas、gasPrice 和 gasLimit 是什么?
imtoken钱包官方版下载教程 2023-02-23 07:52:54
1、什么是煤气?
注意:无论您执行的命令是成功还是失败,您都需要支付计算费用。 即使失败了,节点已经验证并执行了你的交易(计算),所以你必须支付与成功执行相同的费用。
2.什么是gasPrice?
最后,值得一提的是,Gas Price 越高,您提交的交易被矿工接受的速度就越快。 但通常人们不愿意支付更多的费用,那么Gas Price应该设置多少才能保证交易在可接受的时间内在区块链上得到确认呢?
我们可以定义交易的gas price,具体数值可以参考ETH加油站。 在这里可以看到以太坊区块链上最近完成的交易对应的消费单价、记录时间、等待时间、平均手续费等。 根据自己的交易需求以太坊gas费用实时查询gwei以太坊gas费用实时查询gwei,可以参考右下方Safelow/Standard/Fast三种速度对应的gasPrice。
3、如何设置gas和gasPrice?
eth.sendTransaction({
from: 0x4fd9555dd1309e7a2a356ea7e92c1b41cb7bb144,
to: 0xf7deba629960e4f31f1aba61927f6f1e966c4462,
value: web3.toWei(1, "ether"),
gas: 60000,
gasPrice: web3.toWei(40,'gwei'),
nonce: 30
});
参考链接:
以太坊转账gas费太低,一直pending怎么办
4. 什么是gasLimit? 5.手续费不足(insufficient fund)的解决办法
> eth.sendTransaction({from:eth.accounts[1],to:"0x4161514855682c94e3cbcb4808eb8766cf889e17",value:web3.toWei(1,"ether")})
Error: insufficient funds for gas * price + value
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at <anonymous>:1:1
//使用eth.estimateGas()方法来估计gas消耗量
> gas = eth.estimateGas({from:from, to: to, value: web3.toWei(2,"ether")})
21000
//获取gasPrice,单位为 wei
> eth.gasPrice
1000000000
//计算交易手续费TxFee
> fee = gas*eth.gasPrice
21000000000000
//计算gas*gasPrice + 2 ether
> parseInt(fee) + parseInt(web3.toWei(2,"ether"))
2000021000000000000
//查看账户余额
> eth.getBalance(eth.accounts[0])
201000042000000000000
//账户余额远大于gas*gasPrice + value,可以执行转账
> eth.sendTransaction({from:eth.accounts[1],to:"0x4161514855682c94e3cbcb4808eb8766cf889e17",value:web3.toWei(1,"ether")})
INFO [03-24|11:49:52.745] Setting new local account address=0x74f3141E22E415dD9eDAae9CD1EF26E4EF704984
INFO [03-24|11:49:52.746] Submitted transaction fullhash=0x6c82be3da7181b7d2c44398f35453e2d21e16a2cb376b29556d1fb30c4addb51 recipient=0x4161514855682C94e3CBcb4808eb8766cf889e17
"0x6c82be3da7181b7d2c44398f35453e2d21e16a2cb376b29556d1fb30c4addb51"
计算方法参考链接:
估计以太坊交易耗气量
以太坊中的以太币和代币单位
以太坊费用不足(资金不足)
参考链接:
以太坊的 gas、gas price 和 gas limit 是多少?
以太坊中的 Gas、GasPrice 和 GasLimit 是什么?
以太坊的交易和交易费用