A Beginners Guide to Smart Contracts
- The term “smart contract” refers to computer programs that are stored and operate natively on a blockchain network;
- Smart contracts can be used to automate blockchain-based transactions, removing the need for a middleman and allowing for peer-to-peer markets to flourish;
- Smart contracts are only as good as their code, as such, only thoroughly audited and tested smart contracts should be trusted;
What are Smart Contracts?
A smart contract is a computer program that operates on the blockchain, allowing for secure digital transactions without having to use a third party. Agreement terms between the buyer and seller are written into lines of code, making the contract self-executing and irreversible. Similar to traditional contracts, they can be used to exchange anything of value such as money, property and shares. When used on a decentralized blockchain network, the code verifies, enforces and executes trackable transactions between anonymous parties. It does so without the need for a traditional legal system or an external centralized authority.
Why Smart Contracts are Important
Almost every industry uses contracts, all of which require some kind of middleman. Smart contracts are important because they remove the need for a third party and execute the terms of the contract in a completely trustless manner, with the result of the contract being stored on the blockchain. This allows processes to be streamlined, secure, and more efficient in terms of both time and money.
How Do Smart Contracts Work?
A smart contract is a computer code stored on the blockchain. The code is executed when the smart contract receives a specific set of inputs. It’s like telling a computer, “when x happens, do y”. Smart contracts control the outcome, without any third party’s involvement, which allows for automation.
Nick Szabo made one of the best analogies for smart contracts before they even existed when he compared smart contracts to a vending machine. If you pay $1, you get a candy bar. The role that would typically be carried out by a third party such as a clerk at a convenience store is now carried out by the software in a vending machine. Smart contracts work the same way - a given output only happens once the proper conditions are verifiably met. The word verifiable is crucial here. The smart contract can read the data on the blockchain, and can verify that all conditions (e.g. $1 is paid) are met. The candybar is only dispensed once the conditions are verified.
Building A Better Vending Machine
One may find themself thinking, “why is blockchain technology required for smart contracts? How is this different from an ordinary computer program?” It’s true that an ordinary, non-blockchain-based computer program could streamline processes, but it does so in a far less secure and reliable manner. As we have stated before, the data stored in a block is immutable. Once a transaction is confirmed on the blockchain, it is forever set in stone and cannot be undone.
Computer programs that operate on non-blockchain based data systems often reference data stored elsewhere - either on a harddrive, external server, or the cloud. This data can be altered by third parties, and is done so regularly. This presents a significant vulnerability when handling monetary transaction data. Both cloud and private servers also experience downtimes due to natural events, upgrades, and system failures. How many times have you seen a vending machine with an “out of order” sign on it?
Blockchain technology removes the vulnerability and instability from the system. Its decentralized nature ensures that there is never any system downtime, and its blockchain based data storage mechanism guarantees proper functionality. It is an indestructible, un-hackable vending machine that never turns off and is owned by no one.
Smart contracts also enable trustless transactions. If you buy a candy bar from a vending machine, you are trusting that whoever set up the machine did so correctly, and that the candybar you ordered is indeed the candybar you will receive. In reality, sometimes machines take your money and fail to dispense your order, or dispense the wrong order. This cannot happen with smart contracts.
The code that comprises smart contracts is all publicly available on the blockchain. If you are unsure about the contract you are interacting with, you are free to peruse the lines of code on your own and come to a conclusion. This does require some skill to do so on your own, but its open-source nature has led to multiple organizations called auditing firms that review code on behalf of the greater community of users and offer guidance based on their findings. Oftentimes it becomes quickly apparent if a smart contract is trustworthy or not. Remember, smart contracts always do exactly as they are programmed to.
Smart Contract Oracles
Smart contracts also allow people to build programs that leverage real-world events by using blockchain oracles. An oracle is simply an interface that gives a smart contract its inputs, and can be either hardware or software. It provides smart contracts with data from the outside world. For example, an Oracle could tell a smart contract who the winner of a sporting event is, allowing the automation of sports betting. Another example is crop insurance. An oracle can tell a smart contract if a certain area of the world is experiencing a drought. Once this input is confirmed to the contract, the contract could then automatically process the insurance payout. In legacy industries, payouts such as this can take weeks or months. With blockchain-powered smart contracts, the payout could occur in less than a minute.
Decentralized Finance has applied smart contract technology to a variety of financial applications. Blockchain-based lending and borrowing has become a popular application, with interest rates compounding continuously (rather than monthly or quarterly). Smart contracts have the capacity to disrupt virtually all industries.
Smart Contract Risks
A smart contract is only as good as its code, and it will do exactly what it is programmed to do. The immutability of smart contract code is both a blessing and a curse. If an error is included in the contract that causes users of the contract to lose funds, funds sent to the wrong place, or so on - these effects cannot be reversed. While faulty code can be easily spotted in simple contracts, increased complexity allows more room for human error.
A disturbing trend that has been seen throughout DeFi have been exploits that siphon funds held by smart contracts. However, in almost every case it’s not the contract itself being exploited but rather the oracle data feed. These exploits usually occur by manipulating the price of an asset on a particular oracle data feed that a contract uses, and then trading funds at these artificially inflated (or deflated) prices. Oracle providers have been mitigating this vulnerability and strengthening their data feeds to more effectively shield them from manipulation.
Smart Contracts have the capacity to revolutionize every industry. By automating the terms of a contract in the secure environment of a blockchain, nearly every process that currently involves a middleman can be streamlined and made more efficient, impacting everything from accounting to supply chain management and manufacturing, insurance and banking to gaming. This has already been largely applied to financial applications, and new lending/borrowing protocols have been created that would not be possible without smart contract technology.