Wistia video thumbnail

What is a Smart Contract?

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). We use 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. But traditional contracts are often open to interpretation and not always properly enforced. With smart contracts, the idea is that we define our agreement wholly in code. This idea is enticing, but is representing all contracts in code even possible?

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.

Asymmetry of Trust

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 vending machine will cheat you by taking your money without dispensing a drink. Or in a modern machine, maybe it would steal your credit card information, but still dispense a drink so that you won’t suspect anything.

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.

You can’t inspect the code for your vending machine, but you can inspect the code (or at least the proofs) of a smart contract.

An Internet-of-Things Car Loan

Another example Szabo gives is the idea of a car loan - but in this case, the keys to the car are cryptographic keys which are controlled, initially, 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.

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 of the car were to fall far behind on his payments, the smart contract would automatically return control of the keys to the lender.

On the other hand, if the buyer keeps his payment schedule, when the loan balance is paid off, the control of the car given to the buyer, irrevocably. That is, the smart contract permanently, cryptographically, hands over the keys to the buyer and after this happens, neither the lender, nor the smart contract, can take back control of the car.

Smart Contracts Manage Digital Property

To generalize, the key idea here is that any property that can be represented digitally can be managed in code with smart contracts.

A Board Game

Another way to view smart contracts is as a board game. When you and I agree to play chess, we’re agreeing to a specific set of rules :

  • the board will have 64 squares

  • a pawn can move two spaces in it’s first move

  • a king can only move one space except for the special situation where you’re castling and so on

We can model chess in a smart contract and then sit down and play the game. Any move made within this game must abide by the rules – conversely any move within the defined rules is permissible.

A problem with this, however is that it’s very difficult to define every possible outcome of a situation in code.

Traditional contract law is dynamic and has been refined for thousands of years. This means it can be robust in the face of ambiguous or complex situations.

As Nick Szabo is fond of saying “a contract is a program that runs on the brains of lawyers.” The upside is that these brains are sophisticated, but the downside is that human execution of these programs is non-deterministic.

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.

In Bitcoin, a smart contract might be something as simple as a list of opcodes that check the keys of a multi-signature transaction.

In Ethereum with Solidity, a smart contract is essentially an instance of object. Our vending machine, for example, could be a smart contract.

From a programmer’s perspective this vending machine smart contract code is an object which holds its own state variables, such as the inventory of sodas, as well as methods which read or operate on that state.

It’s early days for smart contracts, and the scope of what contracts can be written in code remains to be seen. But we now have the tools to start experimenting, in code, on production decentralized systems.

Let’s start writing a smart contract in code.