Smart contracts have become a familiar word with the rise in popularity of cryptocurrencies and blockchain technology, but most people know little about what they are.
(Note: This article is a part of our completely free in-depth Crypto Course for non-technical people.)
What is a Smart Contract?
Smart contracts are a term coined by Nick Szabo, and he defined them as “a set of promises, specified in digital form, including protocols within which the parties perform on these promises”.
The definition from Wikipedia (edited to make it less technical and remove overlap with legal terms):
“A smart contract is a computer program which is intended to automatically execute, control or document relevant events and actions according to the terms of an agreement.
The objectives of smart contracts are the reduction of need in trusted intermediators, arbitrations and enforcement costs, fraud losses, as well as the reduction of malicious and accidental exceptions.“
It’s a mouthful of words that makes little sense to people who don’t already know what a smart contract is, so if you didn’t understand it from the definition, don’t worry – it means you’re normal.
Let’s motivate the idea with an example.
You go to the shop to buy some noodles.
The shop looks like this – you pay first and then the guy hands you your stuff.
You pay him for the instant noodles, and you find that the shopkeeper just sits back down on his chair and does nothing.
“Give me the noodles!”
“Sure, that’ll be ₹20.”
“I just paid you!”
“When? No, you didn’t.”
Other than calling the police or starting a fight, you would have no recourse.
The problem here was that you had to trust a human to do his part after you did yours.
While people generally do their part in an implied contract like this one (the contract here was “I give you the stuff after you pay me”), relying on humans to be honest is not an ironclad solution and people get cheated every once in a while.
(Note: Smart contract is just a marketing term for computer programs. It has absolutely nothing to do with a legal contract. Do not relate smart contracts with contracts in a legal sense.)
You would preferably want to take away this element of having to trust this shopkeeper to hold his end of the bargain.
You want to use a system that takes your money and then gives you what you want for sure (a type of contract where you don’t have to trust anybody).
Here, it would be a vending machine.
You can buy as many canned slow poisons as you want without having to trust anybody.
You put your money in, you get the stuff you want. You don’t put your money in, you don’t get the stuff you want. You put extra money in, you get change.
You don’t have to trust anybody – it’s an automatic machine that does as it’s programmed to do and nothing else.
(To those of you thinking, what if the machine is malfunctioning – these are all vagaries of the real world. In the world of code, you don’t have to think about this. The program will execute exactly as written.)
The vending machine is a real world example of a smart contract.
It is a software (in this case, real world machinery) executing its code (in this case, giving you the item when you put in money).
That’s it: A smart contract is just a fancy word for a piece of code that automatically executes something in a predetermined way. This piece of code is stored and executed in a distributed/decentralized manner (more on that later).
They are mainly used to reduce the need for a trusted intermediary, disputes, and frauds.
I repeat: it has nothing to do with contracts in the legal sense.
In fact, smart contracts aren’t smart at all. They are just pieces of code that execute exactly as written. For example, if a well-known billionaire is extremely thirsty and offers to pay a million dollars for a drink, it would be smart to take it. But… the vending machine won’t take it, because it hasn’t been explicitly programmed to do so.
The Need for a Distributed Global Computer
Let’s explore a rather centralized example of a smart contract. This is just to motivate smart contracts on blockchains.
Your friend bets you 1 bitcoin that you can’t run 5 km in 30 minutes.
You don’t trust that your friend will actually pay up if he lost, so you create a computer program.
The program generates a random Bitcoin wallet, and both of you send in 1 bitcoin to its address (the wallet has 2 bitcoin now).
The program’s logic is simple: It is linked to your phone’s GPS and is able to track your location. If it finds that you moved more than 5 kilometers in 30 minutes, it will make a transaction that sends the entire 2 bitcoin to your address, and if not, it will send them to your friend’s address.
This way, you can ensure that you get paid if you meet the requirements of the contract.
(It’s possible for you to cheat by using a car – but like I said, smart contracts are very dumb; they do what they have been explicitly coded to do, and nothing else.)
Why is this a poor implementation of a smart contract?
Centralization. You both still need to trust each other enough to let one of you deploy the code without changing it to their advantage.
It would be best if we implement smart contracts in a decentralized/distributed way so we no longer need to trust each other at all.
Limitations of Smart Contracts
If you noticed, the smart contract above only solved the problem of you and him not having to trust each other for getting paid according to the rules of the contract.
It did not solve the problem of trusting a GPS for accurate real-world data. This is just a limitation of smart contracts. Since smart contracts are just computer programs, any time you take in data from “the real-world”, you end up having to trust some source to provide you with accurate information.
This problem is somewhat mitigated by using multiple trusted sources, but it never goes away entirely.
Note: The technical term for these “trusted sources” that provide external information to smart contracts is oracle. Although oracles introduce some element of trust, they also add a lot of value by dramatically expanding the use cases of smart contracts.
Contracts that do not require any real-world data are fully trustless. We will see an example of such a contract below.
Another limitation (which is also a feature) is that smart contracts are immutable i.e. once deployed, the code can’t be changed.
This makes them trustless as you don’t have to worry about a malicious actor changing the code, but in case there are any bugs in the code, there is no way to fix them.
You have to redeploy the entire code again with the bugs fixed. This is why it’s important to thoroughly test smart contracts before deploying them.
Smart Contracts in the Blockchain
When most people refer to smart contracts, they’re referring to smart contracts stored on a blockchain with smart contract functionality (like Ethereum).
Decentralization allows smart contracts to be fully trustless (except when taking in real-world data using an oracle as explained above) with no middleman or intermediary. This is very abstract, so let’s take the concrete example of the Ethereum blockchain.
Ethereum is a global computer that allows you to deploy programs (aka smart contracts) on it. This computer is always on (you can’t turn it off) and is completely decentralized. The code of the smart contracts is visible to everybody.
With such a computer, you can deal with completely unknown people without ever needing a trusted intermediary.
The smart contract code is completely public, so you can audit it before using it. Once deployed, the contract can’t be altered (immutable). The Ethereum computer can’t be turned off. In other words, you know what the code is and you know for sure that the code will execute.
This allows you to use smart contracts to do trustless agreements with anyone across the world, without ever having to spend a minute worrying about whether or not the other person is honest.
Smart Contract Example
Let’s say that you are raising money for charity, and a big philanthropist agrees to make “matching donations for 30 days up to X amount”.
In other words, when any person makes a donation to you, the philanthropist will make an additional donation of the same amount to motivate people to donate more as their contributions are effectively doubled. This agreement will work for all donations made within 30 days. The total donation by the philanthropist is capped to ₹X.
Problem is, when you tell people about the matching donations, many of them don’t believe you, and you still run the risk of the philanthropist backing out of his agreement so you can’t make any commitments from his money until you have actually received it.
Here is where a smart contract can help.
You write a contract on the Ethereum network with these specifications:
- Accepts ether amounts from everybody (on Ethereum, smart contracts can accept ether on their contract account and spend it based on how they are coded. Don’t worry too much about the details now, this will be covered later in the course.)
- The contract knows the Ethereum address of the philanthropist
- The contract maintains two separate variables, one to track total receipts from the philanthropist’s address, and the other for all other receipts
- If at the end of 30 days, the non-philanthropist receipts are less than the receipt from the philanthropist, then the non-philanthropist receipt amount is multiplied by two and sent to the charity’s address, and the remaining balance is refunded to the philanthropist
- Otherwise (when non-philanthropist receipts > receipt from the philanthropist), the entire amount goes to the charity.
With this contract (after the philanthropist has funded it), you are guaranteed to receive the matching donations (i.e. you can now make real world commitments with that money), and any other donors can donate knowing for sure that their donations will be matched.
For those with a technical background, an implementation of this smart contract can be found here (solidity).
As you can see, smart contracts present a lot of value to the business and commercial world as they allow you to “make deals” in the form of code with anybody across the world in a completely trustless way.
The video below explains another example pertaining to crowdfunding:
Proof of Demystification
Let’s read the (edited) Wikipedia definition of smart contracts again (you should be able to understand it now):
“A smart contract is a computer program which is intended to automatically execute, control or document relevant events and actions according to the terms of an agreement.
The objectives of smart contracts are the reduction of need in trusted intermediators, arbitrations and enforcement costs, fraud losses, as well as the reduction of malicious and accidental exceptions.“
I hope this article gave you a better understanding of smart contracts and what they are and how they work.
Further reading (required): What is Ethereum and How it Works (in simple terms)
If you have questions, let me know in the comments below.
Your man,
Harsh Strongman