What is a Smart Contract? An Intro to Ethereum and Solidity
What is a smart contract? It's like a regular contract, but defined by code (and in Ethereum with Solidity, a smart contract is essentially an instance of a class).
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Smart Contracts
What is a "smart contract"?
Answer: it's like a regular contract, but defined by code.
But let's take a step back, what is a "contract" anyway? Normally when we think of contracts we think of a written document where we sign on the bottom line. But the idea of a contract is a bit more general.
Fundamentally a contract is an agreement between two parties. And we agree on and participate in contracts all the time.
Maybe it's an agreement to buy a car and make payments to the lender every month. Or maybe we make an agreement with our library that we can borrow a book and we'll return it on time or face a fine of 10 cents per day.
Maybe it's an agreement to pay monthly for fire insurance and if our house burns down the insurance company will pay us a lump sum. Or maybe we have an implicit social contract with our friends that if we go to dinner, we'll all split the bill evenly.
Another place where contracts are used extensively are financial products.
Stocks are a good example of this. Owning a share of a business gives you certain shareholder's rights. You might receive a portion of business profits through dividends, have a right to information about the company's financials, or have voting rights, etc.
These rights are defined by a company's shareholders agreement, which themselves are shaped by the governing law in a particular jurisdiction. And even with that, enforcement of any particular clause depends on the decisions of the lawyers, lawmakers, and judges involved.
The idea with a smart contract is that we're totally defining the rules of our agreement in code.
We should look at some examples.
Nick Szabo, the inventor of Bit Gold, developed the concept of smart contracts. In his essay, titled "The Idea of Smart Contracts", he gives two examples: a vending machine, and a car loan with a twist.
Let's start with the Vending Machine
Vending Machine
You can think of a vending machine as a mechanical contract between a thirsty buyer and a beverage creator.
The buyer puts in a certain amount of money, and the machine dispenses a drink and change, if necessary. There's even a rollback mechanism if the buyer requests a refund or the drink can't be delivered.
The vending machine acts as an escrow between the buyer and seller. And while the amount at stake is small, there is a moment when the machine controls both the drink and the funds.
The buyer and seller agree to this arrangement because there's an understanding that the machine will act according to its programming and treat both sides fairly.
This lesson preview is part of the Intro to Programming Ethereum ĐApps course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Intro to Programming Ethereum ĐApps, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:05] So you might be asking, "What is a smart contract?" And the answer is, it's like a regular contract but defined by code.
[00:06 - 00:15] But let's take a step back. What is a contract anyway? Normally when we think of contracts, we think of a piece of paper where you sign on the dotted line, but the idea of a contract is a lot more general than that.
[00:16 - 00:30] Fundamentally, a contract is an agreement between two parties, and we agree on and participate in contracts all the time. Maybe it's an agreement to buy a car and pay the lender every month, or maybe we'll make an agreement with our library that we can borrow a book and we'll return it on time or else we'll pay a fee of $0.10 per day.
[00:31 - 00:42] Maybe it's an agreement to pay monthly for fire insurance, and if our house burns down, the insurance company agrees to pay us a lump sum. Or maybe we have an implicit social contract with our friends, that when we go to dinner, we'll all split the bill evenly.
[00:43 - 00:47] Another place where contracts are used extensively is financial products. Stocks are a good example of this.
[00:48 - 00:57] Owning shares in a business gives you certain rights as a shareholder. You might receive a portion of the business's profits through dividends, have access to their financials, or hold certain voting rights, etc.
[00:58 - 01:10] These rights are defined by a company's shareholders agreement, which itself is shaped by the governing laws in a particular jurisdiction. And even then, enforcement of any particular clause depends on the lawyers and judges and lawmakers involved.
[01:11 - 01:17] The idea with a smart contract is that we're totally defining the rules of our agreement in code. We should look at some examples.
[01:18 - 01:25] Nick Sabo, the inventor of BitGold, developed the concept of smart contracts. In his essay titled "The Idea of Smart Contracts," he gives two examples.
[01:26 - 01:30] One, the idea of a vending machine, and two, a car loan with a twist. Let's start with a vending machine.
[01:31 - 01:40] You can think of a vending machine as a mechanical contract between a thirsty buyer and a beverage manufacturer. The buyer puts in a certain amount of money, and the machine dispenses a drink and change if necessary.
[01:41 - 01:48] There's even a rollback mechanism if the buyer requests a refund or the drink can't be delivered. The vending machine acts as an escrow between the buyer and the seller.
[01:49 - 02:02] And although what's at stake is small, there is a moment where the vending machine controls both the drink and the funds. The buyer and seller both agree to this arrangement, because there's an understanding that the machine will act according to its programming and treat both sides fairly.
[02:03 - 02:14] Of course, one weakness of the vending machine metaphor is that the vending machine is controlled by the drink manufacturer. Which, as a buyer, means that there is a chance that the machine will cheat you by taking your money and not dispensing a drink.
[02:15 - 02:23] In smart contracts, however, this asymmetry of trust can be removed. That is, in the ideal smart contract, there's no scenario where one party can defraud the other.
[02:24 - 02:33] You can't inspect the code of your vending machine, but you can inspect the code, or at least the proofs, of your smart contracts. Another example Sabo gives is the idea of a car loan.
[02:34 - 02:48] But in this case, the keys to the car are cryptographic keys, and these keys are initially controlled by the smart contract. The idea is that the terms of the loan are encoded in the smart contract, and the contract gives control of the car to whoever rightly owns it.
[02:49 - 03:00] For instance, the buyer needs to make payments each month, and if he pays on time, he keeps control of the car. However, if the buyer were to fall far behind on his payments, the smart contract would automatically return control of the keys to the lender .
[03:01 - 03:16] On the other hand, if the buyer keeps his payment schedule, when the loan balances paid off, the smart contract will return control of the car to him irrevocably. That is, the smart contract would permanently, cryptographically, give control of the keys to the car to the buyer.
[03:17 - 03:26] To generalize, the key idea here is that any property which can be represented digitally can be managed in code with smart contracts. Another way to view smart contracts is as a board game.
[03:27 - 03:38] When you and I agree to play chess, we're agreeing to a specific set of rules, such as the board will have 64 spaces or a pawn can move two spaces on his first turn. We can model chess in a smart contract and then sit down and play the game.
[03:39 - 03:45] Any move made within this game must abide by the rules. And conversely, any move that's within the defined rules is permissible.
[03:46 - 03:57] When we agree to abide by a contracting code, whatever is logically valid within that code, that's what we'll hold to no matter what. However, a problem with this approach is that many contracts are a lot more open-ended than the rules of chess.
[03:58 - 04:04] And it can be very difficult to define every possible outcome. Traditional contract law is dynamic and has been refined through thousands of years.
[04:05 - 04:13] And this means it can remain robust in the face of complex and ambiguous situations. Nixable is fond of saying that a contract is a program that runs on the brains of lawyers.
[04:14 - 04:20] And the upside is that these brains are very sophisticated. But the downside is that these human executions are non-deterministic.
[04:21 - 04:27] And this idea of contracts as a program is really at the heart of what we're trying to do. When we talk about smart contracts, it's really just code.
[04:28 - 04:41] In Bitcoin, a smart contract might be something as simple as a list of operations that check for a required number of signatures. In Ethereum with Solidity, a smart contract is essentially an instance of an object.
[04:42 - 04:49] Our vending machine, for example, could be a smart contract. From a programmer's perspective, this vending machine's smart contract code is an object.
[04:50 - 04:57] It holds its own state variables, such as the current inventory of the sodas. And it also has methods which can read and write that state.
[04:58 - 05:04] It's early days for smart contracts. And the scope of what contracts can be written in code remains to be seen.
[05:05 - 05:13] But we now have the tools to start experimenting, in code on production decentralized systems. Let's start writing a smart contract in code.
[05:14 - 05:29] [Music]