How Transaction Costs are Calculated — SitePoint

This article on Ethereum Transaction Costs was originally published at Bruno’s Bitfalls website, and is reproduced here with permission.

When sending a Bitcoin transaction, its fee is proportionate to its size. The more inputs and outputs, the more expensive it is. Add to that the factor of pending transactions, and transaction fees can skyrocket based on those two factors alone.


It’s recommended you read the following materials before diving into the rest of this post in order to better understand the terminology we’ll be mentioning.

Basic knowledge of programming terms (variables, loops) might also come in handy.


With Ethereum, given that we’re talking about a programming language within the protocol, it’s possible to be very computationally demanding with very little text or code (something which would be very cheap in the BTC-verse). Let’s look at this loop, for example:

while (i++ < 1000) {
    j = j + i;
}

This loop means “for as long as i is smaller than 1000, increase it by 1 and then sum up i and j and write the result into j, then do it all again.” This loop will execute 1000 times if i is 0, or more if it’s a negative number.

To pay for this computational cost in a fair way — since it has to be executed on all miners’ machines at once and they spend their resources and time on it — the concept of gas was introduced. Gas is used to pay for the execution of these so-called smart contracts (Ethereum programs) inside the EVM. For example, i + j above is a summation operation which costs 3 gas every time it’s executed, so 3000 gas if executed 1000 times.

To explain gas properly, let’s first cover the EVM.

EVM

EVM stands for Ethereum Virtual Machine. But what is a virtual machine anyway?

Virtual Machine

A virtual machine is software running on a specific computer which contains another operating system completely encapsulated inside the main one. For example, a virtual machine allows you to run Windows inside Linux, Linux inside Windows, Windows on macOS like in the image below, or any other combination.

We use virtual machines to separate the environment in which we do our everyday computer use from the environment we work or program in. This lets us keep viruses at bay (they have no way of breaching the virtual machine and getting to the main operating system), helps prevent infinite loops from crashing our main operating system, and holds hard-drive corruptions like the infamous WannaCry ransomware at bay. Additionally, VMs let us use Windows games on Linux, for example, or allow us to program in different versions of the same programming language’s environment easily, without mixing them up.

EVM

The Ethereum Virtual Machine is built into the software running on the Ethereum protocol. It executes smart contracts — Ethereum programs written in the Solidity language. The EVM is contained in the full nodes of the Ethereum network, inside of which it executes these Ethereum-user-written programs.

Any miner of Ethereum simultaneously executes smart contract code. What this means is that Ethereum programs (dapps — decentralized apps) are executed on everyone’s computer at the same time (decentralized).

Execution of these programs isn’t free, however. Miners spend their own electricity, time, and hardware to do this. To pay them for their effort of executing computer instructions (like “store the value 5 into the variable X”), the concept of gas was introduced.

Gas, Ether, and GWei

Gas is a unit of cost for a particular operation a computer needs to execute, and it executes this instruction when we broadcast a transaction which contains an Ethereum program in order to run a dapp. For example, summing two numbers costs 3 gas. Multiplying them costs 5 gas. Storing a 256bit word into the blockchain costs 20000 gas, which means storing 1kb of data costs 640000 gas.

Just like the USD has cents, so too does ether have its own basic unit: wei. If we take wei as the basic unit of ether, we get the following table of definitions:

unit wei
wei 1
kwei / ada / femtotether 1.000
mwei / babbage / picoether 1.000.000
gwei / shannon / nanoether / nano 1.000.000.000
szabo / microether / micro 1.000.000.000.000
finney / milliether / milli 1.000.000.000.000.000
ether 1.000.000.000.000.000.000

Tip: use this converter to convert between Ether units.

According to this informative site, the current average price of gas is 10 GWei (10 gigawei). Seeing as 1 GWei is one billionth of an ether, the aforementioned cost of storing a 1kb word is 640000 * 10, which is 6.4 million GWei. That amounts to 0.0064 eth which, at a price of $450 per ether, amounts to around $2.88.

The text from the beginning of the above table all the way to >>this specific marker<< is around 1kb. So storing this little bit of text into the blockchain actually costs $2.88. However, that’s just the storage cost! Our smart contract could also have some logic, like summing or multiplying and then storing numbers, or triggers that activate on a specific mined block etc. Obviously, storing data into the blockchain itself is incredibly expensive. Storage is better off in BigchainDB or IPFS, while blockchain is a better solution for global processing and verification of that data.

Why Gas?

Why pay costs in gas and not ether directly?

All the gas prices of all the possible operations the EVM can perform are hard-coded in the Ethereum protocol and in the clients (programs) we connect to it, like Geth, Eth, Parity, etc. If the code listed them in ether, then we’d have to update the code every time ether’s value fluctuated to keep the price of computing efforts in a normal range and keep the system usable, which is obviously unsustainable.

By adding this gas layer on top of the costs, and paying for gas with GWei, we’re given the option to alter the amount of gas to use in a transaction and the amount of money to pay for it. It’s fully under our control, without throwing the system off balance.

This leads us to our last section.

Limit/Cost vs Price

Gas limit is the maximum amount of gas we’re willing to spend on a transaction. Most software we use to broadcast Ethereum transactions has the ability to auto-estimate the amount of gas that’ll be necessary to execute a function. It’ll usually suggest a figure right off the bat. For example, simple monetary A->B transactions usually need only 21000 gas. More complex ones which call specific smart contract functions might run into hundreds of thousands or even millions of gas. The spent amount of gas is called gas cost.

We, as a user, can modify the amount of gas we want to spend on a transaction and reduce it, but if the transaction runs out of gas during execution, we lose the gas we sent in. It’s been spent and the transaction is rejected. On the other hand, if we provide more gas than is needed, the rest is refunded to us. Hence, it’s always better to send more gas than you might need to execute a transaction.

Gas cost is the GWei price per unit of gas.

Thus, the total cost of an Ethereum transaction is actually the amount of necessary gas multiplied by the price in GWei per gas unit. This is the maximum transaction fee we’ll pay; any extra gas is refunded, so fees are often vastly overestimated.

Let’s look at an example.

25 Gwei

In the above image, we’re executing a transaction which, due to its complexity, estimates it’ll need 135963 gas. With a 25 GWei price, the maximum transaction fee we’ll pay is $1.57, which is trivial when looking at the total amount we’re sending (0.38 eth = $178). If we raise the price of gas to 250 GWei, the transaction becomes proportionately more expensive:

250 GWei

A higher tx fee effectively encourages miners to process our transaction before others in the queue, thereby speeding up execution. If we’re not in a rush, we can leave the price of gas at its starting value and the transaction’s confirmation shouldn’t take more than 10 minutes. But if we’re in a rush and want it done in seconds (if, for example, we’re dealing with an auction), it’s easy to increase the price of gas and sacrifice some money for a guarantee of success.

Conclusion

Gas is the unit of work expended per computational operation in the Ethereum Virtual Machine. It’s paid for in ether, the token of the Ethereum protocol, and each computational operation has a different gas cost. The gas price (in GWei or 1 billionth of an ether) varies according to the network congestion and the user’s preference for a speedy confirmation.

For a smart contract which we want to deploy on the Ethereum network, two terms are important: gas limit, and gas price. Gas limit is the maximum amount of gas we’re willing to spend on executing the transaction. The amount of gas actually required (known only once the transaction has been executed) is called gas cost. Gas price is the price per unit of gas, expressed in GWei (or billionths of ether). The total cost of a transaction will be the product of gas cost and gas price, while the maximum transaction fee will be the product of gas limit and gas price. The difference is refunded to the transaction’s sender to keep the system fair and usable.

http://platform.twitter.com/widgets.js

About admin 10621 Articles
Coinmarketnewstoday.com | Bitcoin and Altcoin News | Last Minute News about Blockchain Technology | Upcoming Events and Exchange News Around the World

Be the first to comment

Leave a Reply

Your email address will not be published.


*