From Contract to Smart Contract in Ethereum

Published January 10th, 2016 edit replace rm!

Smart Contracts as defined by Nick Szabo always fascinated me. I experimented with early versions of this, but Ethereum has finally come up with what looks like the first practical implementation.

This is the first in a series of articles about implementing Smart Contracts on Ethereum in the real world.

What are Smart Contracts?

Smart Contracts are essentially contractual clauses that execute and enforce themselves through use of code (programming code not legal code).

In Ethereum a contract is a piece of code that lives on the blockchain. The contract is created as part of a transaction and run by every single node on the blockchain.

The rights and right holders are stored as data and code on the blockchain itself.

Rights holders can access their rights by calling functions that are predefined as part of the Smart Contract.

Each function call is in itself a Ethereum transaction and the code within is also executed by every single node on the Ethereum network.

A Smart Contract as defined in Ethereum is not necessarily a contract in the strict legal sense of the world, although it may be.

In this article I won’t get into the underlying architecture of the Ethereum blockchain, but mainly focus on Smart Contracts

A simple Escrow Contract

An Escrow is traditionally a legal contract where an agent holds funds on behalf of for example a buyer and seller.

Lets say I want to buy a car from Alice. Alice lives in another city and will ship the car to me.

I don’t want to send the money to her without receiving the car, as I might not receive the car.

Alice doesn’t want to send the car to me until she receives my payment.

So we’re kind of stuck in a standoff. Enter a trusted third party known as the Escrow agent. These are typically lawyers, banks or specialized service providers.

Now I enter into an Escrow agreement with both the agent and Alice. I send my payment to the Escrow agent Bob, who holds on to the money until I confirm I receive the car. At this point he releases the funds to Alice.

I Pelle (The Buyer) hereby deposit $10,000 with Bob (the Agent) who will store it securely in his bank account until such a time that Pelle Braendgaard verifies that he has received delivery of a 1996 Mazda Protegé (the Car) from Alice (the Seller) at which point he authorizes the Agent to disburse the entirety of the funds to the Seller. If the Seller does not deliver the Car to the Buyer by April 1st the Agent will return the entirety of his funds to the Buyer.

Yes there is a bunch of other legal boiler plate involved, but that is the gist of it.

First iteration of rewriting the escrow contract as a Smart Contract

So let us create a very simple implementation of this in Solidity which has become the primary language for developing smart contracts in Ethereum.

Don’t worry if you’re not a programmer, you won’t need to understand the code to follow along:

contract Escrow {
  address buyer;
  address seller;
  address agent;

  function Escrow(address _agent, address _seller) {
    // In this simple example, the person sending money is the buyer and sets up the initial contract
    buyer = msg.sender;
    agent = _agent;
    seller = _seller;
  }

  function release() {
    if (msg.sender == agent)
      suicide(seller); // Send all funds to seller
    else
      throw;
  }
  function cancel() {
    if (msg.sender == agent)
      suicide(buyer); // Cancel escrow and return all funds to buyer
    else
      throw;
  }
}

Source code for Simple Escrow Contract

This contract is setup by the buyer and involves 2 other parties and allows our savvy escrow agent to either release the funds to the seller or cancel it and return funds to the seller.

The Buyer creates the contract through a web app and sends it on to the network with the ethereum addresses of the agent and Seller and the amount of Ether needed to buy whatever he is buying.

Ether is the primary currency on the Ethereum network, similar to how Bitcoin is the primary currency of the Bitcoin network.

Ethereum allows you to create other currencies, but for now we will keep it simple.

How Smart is this simple example?

So how Smart is this Smart Contract? Well, the Agent can’t access the funds directly. He can only release it to the seller or return it to the buyer. That is one improvement over the traditional process.

The Smart Contract doesn’t mention what the conditions are for releasing or cancelling it either.

The real world contract has an expiry clause. This can be implemented in Ethereum but is a little complex, so I’m leaving that out for now.

Why do we need an agent?

Ideally we want to get rid of the agent in a Smart Contract. We have managed to reduce trust in him by minimizing what he can do with the funds, yet it would be nice if the trigger can be released by something else.

If the other side of the transaction is done entirely on the Ethereum network it is definitely possible to replace the Agent with another Smart Contract.

When I start writing about implementing virtual currencies and other kinds of assets I will return to this with an agent less exchange example.

Is it even a contract?

Lets first look at what a contract is. According to Wikipedia:

a contract (or informally known as an agreement in some jurisdictions) is an agreement having a lawful object entered into voluntarily by two or more parties, each of whom intends to create one or more legal obligations between them. The elements of a contract are "offer" and "acceptance" by "competent persons" having legal capacity who exchange "consideration" to create "mutuality of obligation."

On it’s own our Smart Contract does not fit the above definition. The creation of the Smart Contract could be seen as an offer from the Buyer, but it does not handle acceptance of neither the agent nor the Seller.

It does not even consideration of the Seller to the Buyer (delivering the car) nor Consideration to the Agent (Payment for his services).

Acceptance of the smart contract by the agent could be seen as happening if he either releases or cancels the funds.

None of this means that it can’t work. As long as their is some out of band communication about what the actual contract is we’re still good and the smart contract did it’s job.

The real contract could be implement as a traditional web app with the operator as the agent (like EBay), or as a dApp - the term used for a new kind of distributed web application run on a combination of the web browser and Ethereum.

The important part is that all parties need to understand somehow what the contract is and that the Smart Contract enforces the escrow part of it.

In my next article I will cover some techniques about how to turn the above Smart Contract into a real Contract and also discuss why you might want that.

Next article in series How to make sure a Smart Contract is also a Contract

About me

Pelle gravatar 160

My name is Pelle Braendgaard. Pronounce it like Pelé the footballer (no relation). CEO of Notabene where we are building FATF Crypto Travel Rule compliance software.

Most new articles by me are posted on our blog about Crypto markets, regulation and compliance

More about me:

Current projects and startups:

Other under Blockchain

Popular articles

Topics: