Paying for Transactions
Learn how Ethereum users pay for Ethereum transactions.
We've already mentioned that we need to pay a fee for interacting with an Ethereum network, and in this lesson, we'll look into how this fee is calculated and what it depends on. We'll also learn about the concept of gas and how it's used to calculate transaction fees in Ethereum.
Interacting with a smart contract
It turns out that we don’t need to pay a fee for all the operations within an Ethereum network; but first, let’s look into what types of operations we can perform. There are two different ways to interact with a smart contract: call and transaction.
The main difference between them is that a call can only read data from an Ethereum network and can't change the state of the network, can’t send payments, and can’t deploy new contracts. Transactions, on the other hand, can change the state of the network, for example, update the state variable of a state contract or deploy a new smart contract.
We need to pay only to execute a transaction, and transactions are recorded on the blockchain, while calls that are free are not stored in the blockchain history. Because transactions need to be included in a block, they take longer to execute.
A transaction on Ethereum can be an execution of a smart contract method, but it can also be a payment to an Ethereum address or a smart contract deployment. Moreover, we need to pay a fee for all of these operations.
Since calls only read data from a network, they can only be used with the view
and pure
functions, which can't change the state of the network.
Why can’t Ethereum be free, though? Because if it were free, a malicious user could upload a contract that runs forever and consumes all network resources. Because Ether costs money, requiring payment for computation prevents malicious actors from clogging the network.
Bytecode
To understand how we pay for smart contract execution, we first need to cover the concept of bytecode.
Ethereum nodes don't execute Solidity or Vyper code directly. Instead, a source code is first converted into a sequence of low-level instructions called bytecode instructions. These instructions include simple operations like: 👍
ADD: This is to add two numbers.
MUL: This is to multiply two numbers.
But the bytecode also has some Ethereum-specific instructions like:
SHA3: This is to compute a hash value.
CREATE: This is to deploy a new smart contract.
Having a low-level bytecode allows having multiple programming languages to compile source code into a unified smart contract representation. A source code is converted into bytecode using a compiler for a particular programming language, e.g., a Solidity compiler for converting Solidity code into bytecode.
Paying for computation
When a user’s transaction is executed, a user pays for every bytecode instruction that Ethereum has to execute during the transaction processing. Every bytecode instruction has a fixed price defined in Ethereum’s specification.
Since a fee for computation is paid in Ether, we might think that every instruction is also priced in Ether. However, it's more complicated than that. The price of each bytecode is measured in gas. Every transaction also has an exchange rate between gas and Ether, also known as the gas price.
Here's a sample of different bytecode instructions and their gas prices. Simple commands cost a relatively small amount of gas, but more complex operations cost much more.
Get hands-on with 1400+ tech skills courses.