Fiat
Markets
Exchange
Futures
Finance
Promotion
More
Newbie
Log In
CoinEx Research

CoinEx Research: Basic Concepts of ETH You Need to Know

2021-05-06 01:33:19

In this article, I will introduce the block, transaction, smart contract, the essence of ERC20 transfer, and the difference between ETH and ERC20 transfer.

1. What is a block?

To begin with, what is a block? I believe you must have some knowledge about it. Blockchain, as its name implies, is the block connected with a chain. So what is the block of ETH made up of? These are parts of the ETH block that ETH users need to know: the block height, the unique hash, the hash of the parent block, the block timestamp, which is the time of block generation and the IP address of the miner who mines the block, the block size, and gas consumed, to name only a few. These are simple concepts whose names are self-explanatory. So I would like to cut unnecessary details. Now let’s focus on the following concepts of mining revenue and the address of uncle block.

1.1 About mining revenue

The first is mining revenue. That is why miners consume a lot of electricity to calculate the next block. Mining revenue is real ETH, and miners can sell the ETH they have mined for profit. The reward for each block is mainly composed of three parts, namely block reward, income from the transaction fee, and uncle reward.

1.2 Block reward

The block reward is the reward (reward for the block generated) for the miner who has mined this block. The specific amount of income is specified in the consensus. Modifying this fixed income requires a hard fork upgrade. So far, the fixed income has changed three times. At the very beginning, it was 5 ETH for each block from the height of 0 in Frontier, Ethereum’s first version.  At the height of 4.37 million blocks, with the "Byzantium" hard fork, the block reward for each block was reduced from 5 ETH to 3 ETH. The most recent upgrade is the "Constantinople" hard fork with a height of 7.28 million blocks. The block reward for each block has dropped from 3 ETH to 2 ETH, and has remained so till now. Secondly, there is income from the transaction fee. This simple term refers to the sum of all transaction fees in this block.

1.3 Uncle reward

The last is the uncle reward. What you need to know is that each block can contain up to two uncle blocks, or even none of them.

For every uncle block included, miners can get 1/32 of the block reward. Taking the current block reward of 2 ETH as an example, for every uncle block included, you can get 1/32 of 2 ETH, which is 0.0625 ETH. If it contains two uncle blocks, it is 0.125 ETH.

1.4 Address of uncle block

Now let’s talk about the concept of uncle block. This illustration can help you clearly understand the concept. For the time being, ETH follows the PoW mining mode, under which two miners may get new blocks at the same time in a very short time. In this case, the one that is not broadcast in time will become void for being mined a little bit later or due to the poor node connection.  In the BTC network, the void new block is called an orphan block, which generates no benefit in BTC. As blocks are generated much faster in ETH than in BTC, orphan blocks are much more likely to occur in ETH. To prevent the blocks laboriously mined from being orphan blocks that generate no income, ETH has introduced the uncle block. 

As shown in the figure, two blocks of height 2 are generated after the block of height 1. The green block becomes the real block of height 2, and the yellow block risks being abandoned. At this moment, the following block of height 3 will include this yellow block as its own uncle block. The name uncle block is a vivid metaphor as we all know the uncle is one’s father's brother. The block of height 3 will receive an extra uncle reward since it contains an uncle block. At the same time, the miner’s address of this uncle block will also receive rewards. The specific amount is related to the height of the block and the height of the uncle block, with the formula as below:

[(Height of uncle block+8-block height)*block reward for each block]/8.

Please note the character here is not “day” but the number “eight”.

Take the above figure as an example. The uncle block is at height 2, and the block is generated at height 3. That is 2 plus 8 minus 3 and then multiplied by 2, and finally divided by 8. The result is 1.75 ETH. It can be seen from this formula that the uncle block can only be a block within the above 8 heights at most.

2. What is a transaction?

Having known about the block, let's see what is a transaction. A transaction refers to a specific record in the decentralized ledger of the blockchain. It is mainly composed of the transaction's hash, block height, timestamp, From and To addresses, and the amount of ETH involved in this transaction, followed by attributes such as Input, Nonce, and transaction fees. The first three, i.e. hash, height, and timestamp are self-explanatory by the name. So we will focus on the following concepts.

2.1 From and To addresses and ETH amount

For a decentralized ledger, the transfer is the most basic function. We can take each transaction as a transfer of ETH, which consists of the sender, receiver, and transfer amount. The three correspond to the From address, the To address and ETH amount respectively. The From address is the address that actively sent the transaction. The To address can be an ordinary user address or a contact address. If it is an ordinary user address, the transaction is a simple ETH transfer. If it is a contract address, the transaction involves a relatively complicated contract call, and the final ETH amount is the transfer amount involved herein.

As a public chain that supports smart contracts, ETH allows users to additionally fill in Input data when sending transactions. The Input data here are the calling parameters when the contract is called. In an ordinary ETH transfer, Input is left blank. Theoretically, you can fill in whatever you want in Input as long as the previous data can call the contract correctly. 

Next is Nonce. Nonce, as it is known, is the transaction sequence of the From address, which is essentially the sum of packaged transactions sent from the From address. Transactions of the same From address must be packaged in the order of Nonce.So here comes an issue. If the preceding Nonce is not packaged, the following will never be packaged. The low transaction fee for the transaction sent by the user before has left it unpackaged for a long time, yet the user keeps sending new transactions and then may be confused about the reason behind the failure of packaging given such a high transaction fee. That is because the previous transaction fee is so low that it hinders all subsequent transactions. 

In that case, the user needs to accelerate or cancel the previous transaction, so that the subsequent ones will be packaged in turn. The transaction acceleration and cancellation mentioned above are also realized by Nonce. Before a transaction is packaged, the Nonce can be sent repeatedly. As long as the gas price of the newly sent transaction is higher than that of the previous one, the previous transaction can be overwritten. Generally, the gas price of the new transaction is at least 10% higher than the original, with the specific gap determined by the node configuration.

2.2 How to calculate the transaction fees?

Before explaining the calculation of the transaction fees, let me introduce some basic concepts. The first one is Gas, which is the resource that the EVM needs to consume when executing instructions. Gas Limit is the amount of Gas that the user is expected to consume when sending this transaction, and Gas Used, is the amount of Gas actually consumed when the transaction is executed. A transaction will not be executed if the Gas Limit is used up before its execution. Gas Price indicates how much ETH the user pays for 1 unit of gas.

Having known these basic concepts, you must be clear about the calculation of the transaction fee for each transaction. It is quite simple, that is, Gas Used multiplied by Gas Price. For example, an ETH transfer needs to consume 21,000 Gas, and the user is willing to pay 120 Gwei. By the way, 1 Gwei equals 10-9 ETH. So to transfer the ETH, the user needs to pay 21,000*120/10-9 ETH, which is 0.00252 ETH. It is noteworthy that the more complex the contract calls, the higher the gas to be consumed. Miners will prioritize the packaging of transactions with higher fees. 

3. What is a smart contract?

Before we get to know smart contracts, we need to familiarize ourselves with the Ethereum Virtual Machine, or the EVM. We know that ETH is a decentralized blockchain, so we may take EVM as a supercomputer running on ETH which is responsible for charging gas from users and then executing the code stored on the block.

The code stored on the block mentioned above is a smart contract.

3.1 Smart contract 

The smart contract is stored on the block, and the address of the smart contract is equivalent to the entry to calling the code stored on the block. Through the contract address, you can easily call the smart contract you want through the transaction.Take a mobile phone as an analogy. The BTC network is equivalent to a functional machine that is originally designed for specific functions such as transfer, while the ETH network equipped with smart contracts is equivalent to a smartphone running various Apps that can realize a series of functions. 

The essence of ERC20 transfer

We know that an ERC20 token is actually a smart contract, and ERC20 refers to the general specification of this type of smart contract. Now let’s look at the nature of ERC20 transfer from the perspective of smart contracts. As I just said, smart contracts are essentially the code stored on the block, so let’s take a look at the code of the ERC20 contract first. Below is only the logic of the transfer part.

First of all, see the second line, where an event is defined. An event is to inform the user what happened to this code, and in this case, it is a Transfer event. As the name suggests, a transfer behavior occurs. This Transfer event has three parameters, which are used to inform the user of the sender's address, the receiver's address, and the transfer amount respectively.

Next is the complete realization of the ERC20 contract transfer. This transfer function only has two parameters: the receiver’s address and the transfer amount, but no sender’s address. That is because the sender’s address does not need to be declared. The transfer is called by the sender’s address, which is msg.sender in the code.

First, this function will determine whether the sender has enough balance, and the transfer amount must not be negative but a number greater than 0. Otherwise, it will return False to indicate the transfer failure. 

After the condition is judged to be met, the transfer logic is officially started. The logic is very simple. First, the transfer amount is subtracted from the sender's balance, and then the transfer amount is added to the receiver's balance. Done. ERC20 transfer is quite simple: the sender's balance minus the transfer amount, and the receiver's balance plus the transfer amount. Then the transfer event is sent, and finally True is returned to indicate the success of the transfer. Let's see what the transfer function of the ERC20 contract is called. As I mentioned before, if the transaction is a contract call, the To address of the transaction is the contract address that is called. There is also an Input in the transaction, which is the calling parameter when the contract is called.

In the picture above is a call parameter of the ERC20 transfer. It can be divided into three parts, which are separated by red lines. The first paragraph of characters represents the summary of the transfer. A smart contract may have multiple functions, and the summary varies among functions. Therefore, different functions in the contract can be specified through this summary. The subsequent long string represents an address, which is the first parameter of the transfer, i.e. the recipient's address. The last long string represents a number, which is the transfer amount.

The picture below shows the results parsed by the explorer based on the original Input above so that you can see clearly the function called for this transaction and all the parameters involved. After the ERC20 transfer is successfully packaged, you can find such a Transfer event in the transaction details. Through this Transfer event, we can see how many coins the address whose number ends in 619 has transferred to the address whose number ends in 9b0. The number shown here needs to be divided by the precision of the token before we can get the real transfer amount. In this case, the token is ONES, and the precision is 18 bits, so this number represents 7,200 ONES.

Finally, we can parse this Transfer event to work out the final ERC20 transfer record, that is, an address whose number ends in 619 transfers 7,200 ONES to an address whose number ends in 9b0. The above reveals the essence of an ERC20 transfer analyzed from the perspective of smart contracts. 

The difference between ETH transfer and ERC20 transfer

Last but not the least, let's summarize the difference between ETH transfer and ERC20 transfer. As we mentioned earlier, each transaction is naturally an ETH transfer. Even a contract call can be considered an ETH transfer. The difference is that the recipient’s address is not an ordinary user address but a smart contract address, and the transfer amount may be 0. In addition, an ETH transfer could be an internal transaction as we call it. For example, an ETH transfer occurs when a smart contract is executed, which is reflected through an internal transaction. The ERC20 transfer is much simpler. It is essentially an event called Transfer in an ERC20 contract.

Are you familiar with ETH now?

Prev
Financial Derivatives with No Delivery Date: All Things You Need to Know about CoinEx Perpetual Contracts
Next
CoinEx Research: Research Report on XAYA, A Blockchain Game Platform