On a fundamental level, ‘smart contracts are virtual ‘if-then’ contracts: a set of code that automatically executes an action when certain conditions are met. Think of a vending machine; a vending machine is programmed to give you a bag of sweets that costs £1 automatically once you have inserted £1 and selected the bag of sweets. This is what is meant by an ‘if-then’ contract: if you pay the right amount and select what you want, then the machine will give it to you. If you don’t, it won’t.
Smart contracts are what we referred to in section 2b as the ‘decentralised applications’ supported by the Ethereum blockchain. The possibilities for what can be created are virtually endless, as the functionality of a smart contract is only limited by the complexity of the code it was written with. Anything can be coded as a smart contract, from the creation of a democratic voting network for shareholders of a company, to decentralised gambling and casino games, to simple exchanges of goods and services.
Let’s consider an exchange that’s a little more complicated than a vending machine. In the physical world, if I wanted to sell my used PS4 to Bob for £180, I would tell Bob ‘If you give me £180, then my PS4 is yours’. If Bob followed up on my offer, and gave me £180, I would be personally responsible for handing my physical PS4 over.
Two scenarios are now possible: one where I hand over the PS4 and the deal is complete, and another where I refuse, and Bob chases me up for his money and possibly seeks legal action. The only way to avoid the latter situation is to employ a middleman who would hold the PS4 on escrow for Bob if he pays the £180 on time, or returns it back to me if he does not. However, just like how the banks can act fraudulently with your funds, the middleman can’t necessarily be fully trusted with Bob’s funds here, even if Bob makes the payment on time.
Smart contracts on the Ethereum blockchain get rid of the need for middlemen. Here is a simplified version of how a smart contract might work: a contract can be created where John will agree to put £180 worth of ether into escrow. The contract will operate on the basis that If I give the PS4 by a specifically agreed time, then the ether in escrow will go to me, and if I don’t, then the ether in escrow is refunded to John. All of this happens instantaneously.
Ultimately, smart contracts ensure that no two parties can be in the possession of both sides of the transaction (in this case, the PS4 and the £180) at the same time. The transaction happens instantly and without the need to place trust in a third party.
The Ethereum blockchain removes the possibility for smart contracts to be fraudulently manipulated. Just like how it records the history of ether transactions, the blockchain records the ‘state’ of all smart contracts, and makes the information publicly available. In other words, the blockchain records whether smart contracts have been executed or are yet to execute, so that there can be no disagreements about what has happened.
To give one further example of how smart contracts can be useful, have you ever seen a Kickstarter campaign that promised a great idea, but you never felt like you could invest into it because you had no idea who was behind the project and whether they would actually carry it out? A smart contract could address this issue. For instance, a contract could operate by keeping all donated funds in escrow, and the money can only be used on the condition that a certain goal of a project is reached by a specific deadline. If the goal is reached, then the funds held in escrow can go towards the project, and if it doesn’t, everyone automatically gets their money back immediately.
Smart contracts usually transact using ether, but it’s possible you may have heard before that different cryptocurrencies also exist on the Ethereum blockchain (don’t worry if this is news to you). These are known as ERC-20 tokens.