如何精确的计算交易时的矿工费用?

以太坊

#1

目前的以太坊交易矿工费用通常是给默认的price,自己调节,以太坊接口也可以获取预估的费用,但是这些还是存在一定的不足,如何才能更加精确的计算交易时的矿工费用,大家有没有好的想法呢?


#2
var sender = web3.eth.accounts[0];//sending the either
var receiver = web3.eth.accounts[1];//receiving the ether
var balance = web3.eth.getBalance(sender);//finding it's total balance
var transactionObject = {
  from: sender,
  to: receiver
}

var transactionFee = web3.eth.gasPrice * web3.eth.estimateGas(transactionObject);

transactionObject.value = balance - transactionFee;

web3.eth.sendTransaction(transactionObject, myCallbackFunction);

#3

#4

Hopefully by the end of this explanation, you’ll have a good understanding of the transaction fees that go along with Ether transactions. First of all, we have to understand what gas is ( SKIP THIS IF YOU ALREADY HAVE A GOOD UNDERSTANDING OF GAS ):

  • gas :This is actually a currency all on it’s own used by the whole Ethereum network. It’s used for the purposes of paying Miners for their work. Miners are people who working on “processing” the payments so-to-speak. Now… you may ask “why isn’t Ether used as this currency instead?” Why does there need to be a totally different currency and complicate our life more? Well it partially has to do with how Crypto currencies are always changing in value, and if you had to code into the Ethereum network to adjust for these values, things become more complicated. So the solution was to separate the gas from the Ether and then just look at the exchange rate for Ether when deciding how much the gas will cost you to do your transaction.

So now that we know what gas is, how do we use it? Well in the Ethereum network, the minimum amount of gas that is needed for a normal transaction to take place is 21000 gas. But, people have the option of putting in more gas. Now how it works is, the more gas you put, the more Miners are interested in mining your transaction, hence the time it takes for your transaction to happen is lower.

But lets assume you don’t care about transaction time, and currently for Ether it really doesn’t take long at all even if you use the minimum amount of gas . So lets say you choose to put 21000 as your gas value. Cool. But how much does that gas cost? Remember in the description of gas above, we said that the Ethereum Network looks at the gas/Ether exchange rate. So what happens here is that, when you picked your gas value, the exchange rate is found, and those 2 values are multiplied:

gas * (Ether price / gas) = Ether price = transaction fee

So lets refer to (Ether price/gas) as gasPrice . Then the equation is:

gas * gasPrice = transaction fee

Now we know how much the transaction fee is! (the smallest possible fee to be exact).

Now if you wanted to for instance withdraw all of your balance, you would first need to calculate the transaction fee, then subtract that from the balance you want to withdraw, and then do the withdrawl. Here is an example:

  • Example:
var sender = web3.eth.accounts[0];//sending the either
var receiver = web3.eth.accounts[1];//receiving the ether
var balance = web3.eth.getBalance(sender);//finding it's total balance
var transactionObject = {
  from: sender,
  to: receiver
}

var transactionFee = web3.eth.gasPrice * 21001;

transactionObject.value = balance - transactionFee;

web3.eth.sendTransaction(transactionObject, myCallbackFunction);

NOTE: you’ll often see the following code replacing the 21001 above:

web3.eth.estimateGas(transactionObject)

and the reason this is done is because of good practice. Transactions of Ether are not the only thing you can do with the Ethereum Network. Different actions cost different amounts of gas . It is good practice to use the estimateGas function in general.

Also you may have noticed I used 21001 instead of 21000 in the example above. I have noticed other people using this value, and I assume it’s because of some small details that maybe sometimes cause an issue with the transaction fee (what if the estimated gas price changes right after you find it for instance?), so that is why I use it too, but I think this is might be more useful to have as a question on it’s own on this website :slight_smile:


#5

web3有estimate gas方法可以估计这次交易需要花费多少gas,另外也可以参考
https://ethgasstation.info/