What is a smart contract? It is one of the key terms when it comes to cryptocurrencies and blockchains. A smart contract is essentially a piece of program code stored on a blockchain. This article has smart contracts explained. We’ll go through the key features of these distributed programs.
Blockchains and cryptocurrencies
To understand smart contracts, you must a basic understanding of blockchains & cryptocurrencies.
In short, a blockchain is a distributed ledger. The English abbreviation DLT says it all: Distributed Ledger Technology. Blockchains are databases of cryptocurrencies and store the transaction data of the network.
There are different types of blockchains. Bitcoin was created in 2009 and it has a blockchain with certain limitations. Bitcoin’s blockchain needs to be maintained with physical mining devices (miners), which are no longer needed in high-capacity blockchains.
There are also different types of cryptocurrencies. The best way is to split them into three main categories: platforms, tokens, and currencies.
Platforms are called ecosystems and Dapp operating systems. The best-known example of a platform is Ethereum. Its main rivals are Cardano, Binance Smart Chain, and Polkadot. These projects are designed to support Dapps and smart contracts.
Tokens are created inside of such platforms. Hence, they don’t have their own blockchains. Tokens have utility or governance functions inside a specific program. Popular tokens include Chainlink, Aave, and Uniswap.
The currency category includes Bitcoin and its rivals (Litecoin, Bitcoin Cash, Digibyte). However, be careful of making a claim that Bitcoin doesn’t support smart contracts at all. Some things can be built also on top of Bitcoin, but it really isn’t designed for that.
The development of Bitcoin is progressing slowly. The core features are almost impossible to change. Hence, many platforms were designed in 2014-2016 to support smart contract programming. New ones are coming out to the market every year.
In this article, we are studying smart contracts through Ethereum. It is the best-known platform and therefore a natural choice.
What is a smart contract?
The term smart contract is actually misleading. It isn’t especially smart and it isn’t necessarily a contract either. You often see smart contracts explained as “programmable contracts stored on a blockchain”. This is too generic and doesn’t tell the whole truth.
A smart contract is a piece of code written in Solidity language. This is the language developers use to create Ethereum-compatible smart contracts. Other platforms support different languages.
This is a simple example of a code written in Solidity (source).
After the program code is written, it must be compiled into bytecode. This is the format Ethereum Virtual Machine (EVM) understands.
EVM is a program stored on every node of the Ethereum network. Its task is to process the smart contract code and execute the desired transactions.
When a smart contract gets a command request, Ethereum miners step in. Smart contracts are processed every time a new block is created onto the blockchain. This means each miner has a copy of each smart contract and they run it in their EVM.
It also means that each miner will always get the same outcome of processing a smart contract. It is not possible that there would be different versions of a smart contract (or the EVM) on the blockchain.
Dapps vs Smart Contracts
When we talk about smart contracts, the word Dapp is often mentioned. You have seen it in this article too. What is the difference between a Dapp and a smart contract?
Dapps are applications just like “traditional apps” you use on your mobile or laptop. Think about Facebook, Twitter, or your web browser. These are popular applications we use daily.
The big difference is that Dapps are distributed apps, meaning, they are built on the blockchain. A Dapp can be a desktop app, mobile app, or web browser app.
Smart contracts are programs, which execute commands on the blockchain. A Dapp is an application with a GUI – graphical user interface. A Dapp makes it possible for end-users to communicate with smart contracts.
The Dapp is the application you actually see and use. It is the front-end. The “engine room” consists of smart contracts, which are executed by the end-user by clicking buttons.
The word protocol is often used in the same context. The sentence “most popular DeFi protocols” means the same as “most popular DeFi Dapps”.
Smart contract auditing and bugs
If you have created a smart contract, you don’t just throw it out on the Ethereum MainNet. This is what Ethereum TestNets are for. You can run the program without the risk of losing anyone’s money.
Once a smart contract is published on the blockchain, it cannot be changed. There is no undo or admin command to change it afterward. If there were, the whole idea of an immutable contract would disappear.
When we talk about popular Dapps, they have billions of dollars of user funds locked in. Hence, it’s absolutely critical that the program code is flawless and bug-free. Anyone can see the program code of a blockchain Dapp and seek vulnerabilities.
Unfortunately, bugs are found by malicious actors all the time.
— Crypto Mak 🌐 (@crypto__mak) May 31, 2021
Sometimes programmers make mistakes, sometimes the attacker exploits a price oracle. There are multiple ways a Dapp can be attacked.
These problems are prevented with code audits. There are companies that are specialized in code inspections. Here is an example of a code audit made by OpenZeppelin. The biggest Dapps are usually audited by several companies.
Ethereum account types
Let’s dive a bit deeper. So far you have learned that a smart contract is simply a program code, which is run by Ethereum miners. It is only as smart as the developer is who created it. It’s not necessarily a contract either.
Smart contracts are made possible by Ethereum’s account model. For example, Bitcoin is using a UTXO model and has no user accounts. There are just addresses and transactions in the Bitcoin network.
Ethereum works differently. There are two types of accounts: an EOA (Externally Owned Account) and a Contract Account.
An EOA is a user-controlled account. Access is granted through the user’s private key, just like in the Bitcoin network. An EOA has an Ether (ETH) balance and it can send & receive transactions. There is no program code attached to it and it doesn’t cost any ETH to create one.
A contract account is also called a smart contract account because it is, in fact, a smart contract. Such an account has also an Ethereum address. A contract account can also send & receive transactions, which can be Ether or other information.
The difference is that it is not free to create or use a contract account. Every line of program code that EVMs must process requires the computing power of the network. Therefore, program developers must pay a fee. This is called gas.
The more complex programs (smart contracts) you build, the more gas is required. It makes sense, right? It also limits spamming and malicious use of the network.
When we talk about the Ethereum platform, gas is paid in Ether (ETH). There are some platforms, which have created another token for this purpose, named also gas. For example, NEO has its native NEO token and also a separate gas token, which both can be bought from cryptocurrency exchanges.
Key features of smart contracts
Smart contracts have several key features, which have been mostly covered already. These are:
- Smart contracts are open source programs
- The program code is stored on a blockchain
- The program code cannot be changed once it’s published
- A smart contract is executing all commands automatically
- The program code, logic, and results of a smart contract are visible to all
- Smart contracts cannot be altered or stopped from running
A smart contract is a distributed program stored on a blockchain, which is open for all, immutable, and secure at the same time.
Of course, anyone could build their own private blockchain, which is controlled by a centralized entity. If the network is not truly distributed, it ruins the whole idea of a blockchain and smart contracts. Why wouldn’t you then use a traditional database and programs instead?
Examples of using smart contracts
Let’s go through some of the real-life examples of smart contracts. How they can be used and executed?
In general, there are endless use cases for smart contracts. All real-life instruments can be tokenized onto a blockchain and managed with smart contracts.
The benefits are obvious: savings in costs, speed, and reliability. This is, of course, only uf the smart contracts are stored on a truly decentralized blockchain.
Smart contracts help to remove the middlemen from any financial process. This makes the whole process faster, simpler, and more reliable. There is no more room for human error and each party can be always sure that the desired outcomes are performed.
The easiest example is trading or commerce. You can think of buying a house, a car, a bunch of stocks, or other financial instruments. This is where you see the term tokenization, which means converting real-life objects to tokens on a blockchain.
Think about your car, for example. The ownership history is currently stored in a centralized database. In the future, it could be in the blockchain. You could have the proof of ownership in a token format.
If you want to sell your car, it could be done through a smart contract. When a desired amount of Ether would be sent to a certain smart contract, it would change the ownership automatically and move the car token to a new person.
Prediction markets are already using smart contracts. Traditionally, these markets have been dominated by centralized entities, which leave little or no room for complaints in case of errors.
There are already online platforms, where betting is done P2P on the blockchain. This could be the way of the future. All prediction markets could be blockchain-based, and results would be always 100% accurate and payouts automated.
Smart contracts could help to remove the middleman in the betting industry.
Insurances are often used as examples of how smart contracts could revolutionize the world we know. There are cases all the time, where the insurance company refuses to pay claims to the person thinking he was be covered.
With smart contracts, there would be no arguments or lawyers needed. All terms of the insurance would be stored on the blockchain, where they are visible to all parties and immutable. If something happens, the smart contract would be triggered automatically.
DeFi & NFT
DeFi (Decentralized Finance) and NFT (Non-Fungible Token) sectors finally hit the mainstream in 2020-2021. This financial revolution couldn’t be possible without smart contracts.
The most popular DeFi protocols have over 20 billion dollars of liquidity. The entire DeFi market has over 150 billion dollars of user funds locked in. It has grown 100x in less than two years.
DeFinitely a signal that bull market continues, back to 20 Billy, Aave fam pic.twitter.com/oR0vu8CuuW
— stani.eth 👻 =(⬤_⬤)= 👻 (@StaniKulechov) May 30, 2021
DeFi applications include decentralized exchanges (Uniswap, PancakeSwap), liquidity protocols (Aave, Compound), and yield aggregators (Yearn.Finance).
These are all 100% automated Dapps. Everything is run with smart contracts. It means there is no KYC and no middlemen between the end-user and the protocol. Investors are also full in charge of their funds all the time.
The crypto boom of 2017 was so massive thanks to ICOs. They helped to boost Ethereum and other tokens created on the platform. An ICO is crowdfunding done in the crypto world. A new project raises funds by selling part of its tokens to the public.
Smart contracts are used in ICOs all the time. If you want to participate in an ICO, you simply send Ether to a contract address. A smart contract is then triggered and sends you back X number of new tokens.
Let’s go through oracles as well, which are critical components of the blockchain world.
The big limitation of a smart contract is that it cannot access any outside information. It can only operate with the information that exists on the blockchain. For example, you cannot program a smart contract to fetch weather information, football results, or stock prices.
This is why there are oracles. They provide data feeds from the outside world. There lies a problem, though. If a smart contract is using an oracle provided by a third party, decentralization benefits are gone.
An oracle could contain false information or it could go offline. This would jeopardize the whole smart contract.
That is the problem Chainlink will solve.
Chainlink has created a distributed network of oracles with incentives to provide correct information all the time. This is essentially software built on Ethereum, but it will expand to other platforms as well. At the moment, every popular Dapp is using Chainlink’s oracles.
This article explained smart contracts and Dapps. Now you understand why the term smart contract is misleading. There is nothing “smart” or no “contract” built into it.
A smart contract is simply a block of Solidity code, which can be programmed to perform certain tasks. It is only as smart as the developer is who created it. It is not necessarily a contract either.
There are currently several platforms created to support smart contracts and Dapps. Ethereum is the best-known one, but Binance Smart Chain, Zilliqa, TRON, and many others offer similar support.
It’s important to understand that the benefits of smart contracts come from a truly distributed environment. If a blockchain is controlled by a simple entity, the whole concept loses its meaning.
The oracle problem is also a very important one to comprehend. Smart contracts need oracles to access the outside world. Oracles must be also decentralized to make sure the smart contract information is not corrupted.