The following article covers smart contracts on Ethereum and is one in of a series articles covering The Basics of Ethereum.
What is a Smart Contract?
Smart contracts are computer programs that automatically execute all or parts of an agreement by a system that allows all of its participants to verify these programs’ correct execution (a system such as a blockchain). The contract is self-executing, meaning that the contract fulfils the specifications once the conditions in the contract are met.
Vas3k’s blog post provides a fantastic explanation on smart contracts in a very beginner friendly manner with analogies to real world examples.
Smart contracts on Ethereum
Ethereum is currently the largest smart contract system that is Turing-complete. This means that Ethereum smart contracts can be encoded to be computationally universal and thus be used to perform a general purpose. Ethereum smart contracts have been used for a variety of complex transaction types including:
- Shareholder/Stakeholder voting for organisations.
- Exchange protocols for digital assets/ERC-20 tokens.
- Blockchain based gaming.
The most popular smart contracts on Ethereum by daily active users is for use with Ethereum and ERC-20 tokens which represent can represent anything on the system (from companies, to NFTs).
Smart contracts on Ethereum are a type of account and an account on Ethereum is just an entity with an Ethereum address (for example, your ETH wallet). So you can think of smart contracts as another “wallet” (not entirely correct but just an analogy) but with rules that are triggered by certain transactions interacting with it. For more technical details on what a smart contract on Ethereum is, the documentation by Ethereum is very comprehensive. The sections below aim to provide more technical context on a smart contract.
In Ethereum there are two account types:
- Non executing (or external) which is any account which has a public address and is accessed via a private key. This can be stored anywhere.
- Contract which is a smart contract deployed to the network, controlled by code and has a contract address.
Both account types have the ability to receive, hold and send ETH and tokens. They can also interact with deployed smart contracts. Non executing accounts cost nothing to create but interactions between two non executing contracts is limited to just transfering ETH. Contract accounts have a cost to create because they are stored on the network and thus the cost of utilisation is to be accounted.
The communication between accounts on the Ethereum network is done through the sending and processing of transactions. Non executing accounts can initiate a transaction whilst contract accounts can only send transactions in response to receiving a transaction.
However contract accounts are much more dynamic, transactions received can trigger smart contract code which can execute many different actions, such as transferring tokens or even creating a new contract.
In Ethereum two transaction types occur:
- Tokens are sent from one address to a non executing account address.
- Tokens or input data is sent to a smart contract account address which is stored in the network’s state.
Transactions are broadcasted to all of the peer nodes in the network to signal availability for inclusion in a block, and can be in one of three states:
- Unconfirmed and not yet mined.
- Confirmed by a miner and considered to have been executed.
- Rejected as invalid by the miner.
Before it’s added to the next block, the transaction remains in a staging area called the mempool or txpool where it’s status is unconfirmed. Nodes then run a series of validity checks on these transactions:
- Ensuring that the funds are still available.
- The output is not exceeding the input.
- The signature is valid.
The transaction is rejected if it fails any of these tests and the transaction has the state invalid. Otherwise the transaction is confirmed and executed. Transactions require a gas fee and must be mined to become valid.
A submitted transaction includes the following information (adapted from the Ethereum documentation:
- to — the receiving address (For a non-executing account the transaction will transfer value. For a smart contract account, the transaction will execute the contract code).
- v, r, s — the identifier of the sender. This is generated to confirm the authorisation of the sender using the sender’s private key.
- value — amount of ETH (in WEI) to transfer from sender to recipient.
- data — optional field to include arbitrary data.
- nonce — borrowed from the account, a counter that indicates the number of transactions sent from the account. This ensures transactions are only processed once. In a contract account, this number represents the number of contracts created by the account
- gasLimit — the maximum amount of gas units that can be consumed by the transaction. More on this below.
- gasPrice — the fee the sender pays per unit of gas. More on this in the following article.