Transactions
A transaction is an agreement between a buyer and a seller, a supplier and a consumer, or a provider and a consumer that there will be an exchange of assets, products, or services for currency, cryptocurrency, or some other asset, either in the present or in the future. Ethereum helps in executing the transaction. Following are the three types of transactions that can be executed in Ethereum:
- Transfer of Ether from one account to another: The accounts can be externally owned accounts or contract accounts. Following are the possible cases:
- An externally owned account sending Ether to another externally owned account in a transaction
- An externally owned account sending Ether to a contract account in a transaction
- A contract account sending Ether to another contract account in a transaction
- A contract account sending Ether to an externally owned account in a transaction
- Deployment of a smart contract: An externally owned account can deploy a contract using a transaction in EVM.
- Using or invoking a function within a contract: Executing a function in a contract that changes state is considered a transaction in Ethereum. If executing a function does not change a state, it does not require a transaction.
A transaction has some of the following important properties related to it:
- The
from
account property denotes the account that is originating the transaction and represents an account that is ready to send some gas or Ether. Both gas and Ether concepts were discussed earlier in this chapter. Thefrom
account can be externally owned or a contract account. - The
to
account property refers to an account that is receiving Ether or benefits in lieu of an exchange. For transactions related to deployment of contract, theto
field is empty. It can be externally owned or a contract account. - The
value
account property refers to the amount of Ether that is transferred from one account to another. - The
input
account property refers to the compiled contract bytecode and is used during contract deployment in EVM. It is also used for storing data related to smart contract function calls along with its parameters. A typical transaction in Ethereum where a contract function is invoked is shown here. In the following screenshot, notice theinput
field containing the function call to contract along with its parameters:
- The
blockHash
account property refers to the hash of block to which this transaction belongs. - The
blockNumber
account property is the block in which this transaction belongs. - The
gas
account property refers to the amount of gas supplied by the sender who is executing this transaction. - The
gasPrice
account property refers to the price per gas the sender was willing to pay in wei (we have already learned about wei in the Ether section in this chapter). Total gas is computed at gas units * gas price. - The
hash
account property refers to the hash of the transaction. - The
nonce
account property refers to the number of transactions made by the sender prior to the current transaction. - The
transactionIndex
account property refers to the serial number of the current transactions in the block. - The
value
account property refers to the amount of Ether transferred in wei. - The
v
,r
, ands
account properties relate to digital signatures and the signing of the transaction.
A typical transaction in Ethereum, where an externally owned account sends some Ether to another externally owned account, is shown here. Notice the input
field is not used here. Since two Ethers were sent in transaction, the value
field is showing the value accordingly in wei as shown in the following screenshot:
One method to send Ether from an externally owned account to another externally owned account is shown in the following code snippet using web3
JavaScript framework, which will be covered later in this book:
web.eth.sendTransaction({from: web.eth.accounts[0], to: "0x9d2a327b320da739ed6b0da33c3809946cc8cf6a", value: web.toWei(2, 'ether')})
A typical transaction in Ethereum where a contract is deployed is shown in the following screenshot. In the following screenshot, notice the input
field containing the bytecode of contract: