The basics of smart contracts

What smart contracts are and how they can be used

Is the age of paper contracts coming to an end?

Story time

In 1994, computer scientist and cryptographer Nick Szabo coined the term "smart contract", idealizing a digital equivalent of traditional contracts, with the objective of minimizing the need for intermediaries and trust between the parties involved.

“A smart contract is a computerized transaction protocol that executes the terms of a contract.” — Szabo, 1994

As mentioned in the quote, smart contracts are computerized protocols that act independently to execute specific functions. They are nothing more than code, following if-this-then-that logic, that is, when an event happens, it triggers a pre-determined reaction.

To better understand this, let's look at an example:

pragma solidity ^0.4.24; //Solidity language version
contract Hello {  //Contract name 
    //HelloWorld function returns "Hello World" message
function HelloWorld () public pure returns (string) {
return "Hello World";
}
}
  • Code written in Solidity, one of Ethereum's smart contract languages.
  • ‘//’ indicates a comment, which does not affect the code in any way, but helps the reader understand what the code does.

Don't worry if you do not understand the contract above, we will just use it as an example.

The contract, called "Hello” has only one function. This function defines that, when it is executed, the contract must return the message "Hello World". Check the diagram below:

If-this-then-that:

If the user calls the HelloWorld function
Then the contract returns the message "Hello World".

After being published to the network, the contract above will do only what it was originally programmed to do, and will not be able to be modified.

In the past, when Szabo idealized smart contracts, there was a big problem: databases. To be able to execute the designated functions, the contract needs to search for and store information in some sort of ledger, and up until the creation of blockchain, the implementation of smart contracts with traditional databases meant that they could be easily manipulated by the centralizing agent. In the example from above, for instance, the programmer who built the contract or the owner of the database could make the contract stop returning "Hello World" and instead return another message. They could also delete the contract and it would cease to exist.

However, with blockchain, this problem has now been solved. Implementing smart contracts on top of a distributed and immutable ledger guarantees the legitimacy of the data, and allows operations to be performed without the need of parties trusting each other or a third party mediator.

And it was with that in mind that, in 2013, Vitalik Buterin published the Ethereum Whitepaper. Uniting Szabo's research and ideas with Satoshi Nakamoto's new technology, Ethereum, which launched in 2015, became the first platform for the creation of smart contracts.

To better understand the importance of all this, we will use a loan as an example. Today, to borrow money, in most cases, we need to use an intermediary, which is in usually a bank. But there is also another intermediary, a regulator, which guarantees your rights as well as the bank's in relation to the loan. In this case, we need to trust both the bank and the regulator, expecting that these institutions will guarantee your rights. More often than not, the process works without any major problems, but it involves unnecessary friction, inefficiencies and high fees.

Now imagine that, to avoid high fees, you decide to borrow money from a friend. However, who assures that you will pay back, and what happens if you do not? Also, what if no friends want to lend you money?

Enter smart contracts. See below an example of a simple loan that happens through a smart contract.

Contract creation

  1. The parties establish the terms of the contract and verify that the code reflects these terms.
  2. The contract is published on the blockchain and cannot be modified by anyone afterwards.

The loan

  1. Lender deposits the money into the contract (not directly to the borrower).
  2. Borrower deposits the collateral (for example a digital asset) into the contract.
  3. The contract sends the money to the borrower (Only if the designated collateral was deposited in full).
  4. The contract initializes the countdown to the payment.

Ending the loan: Scenario 1

  1. The borrower pays to the contract the value of the loan plus interest in full.
  2. The contract sends the payment to the lender.
  3. The contract returns the collateral to the borrower.
  4. Optional: contract self-destructs.

Ending the loan: Scenario 2

  1. Borrower does not deposit the established value or the countdown ends.
  2. Contract sends collateral to lender.
  3. Optional: contract self-destructs.

As we can see, the contract substitutes the intermediaries and automates the process, as well as makes it more secure. Because of that, the loan can be executed safely, not only between friends, but between any two people in the world, due to the global aspect of blockchains. Instead of trusting a friend, bank, company or government, the trust is passed onto the code, which can be verified to guarantee legitimacy.

In addition to the processes described above, using a more complex smart contract would also allow you to include more advanced functions, such as nominating someone else to handle the payment, automatically updating credit score, distributing the payment in instalments or an insurance fund that automatically covers any losses resulting from the loan.

Main benefits

In short, smart contracts have the following main benefits:

  • Automation of processes: Contracts execute the necessary functions when requirements are met, shortening the time taken for complex operations to happen, as well as lowering the amount of effort needed to assure that these operations occur in the correct way.
  • Trust: There is no need to trust an organization or person to guarantee the proper execution of actions. The code takes care of security. This brings about new opportunities, such as loans between people who do not know each other.
  • Disintermediation: The complete elimination or reduction of intermediaries in processes allows for faster operations, more safety and smaller fees. It also prevents abuse of power, and limits the control of large organizations.
  • Immutability: The use of an immutable ledger facilitates auditing of the data, regardless if it is recent or not. A borrower cannot affirm that he/she paid the money back if that was not the case, because a quick check of the ledger will contradict the statement. Also, there is no risk of documents burning and disappearing forever.
  • Digitalization: Less paper, fewer queues, less hassle. Registries can be made, monitored and updated remotely.
  • Simplicity: Instead of writing 9 pages in paper, you just need 150 lines of code.
  • Safety: Due to all aforementioned reasons.

Examples of applications

Apart from loans, check the (non-exhaustive) list below to see where else we can utilize this technology, enjoying the benefits mentioned in the previous section:

  • Elections
  • Digital assets
  • Social media
  • Document registration
  • Auctions
  • Bets
  • Lottery
  • Supply chain
  • Insurance
  • Investment funds
  • IoT
  • Identity
  • Health records

Platforms

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public eosio::contract {
public:
using contract::contract;
      /// @abi action 
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
};
EOSIO_ABI( hello, (hi) )
  • HelloWorld contract, this time written for the EOSIO platform (aka EOS).

So where are these smart contracts created?

Nowadays, the largest platform for smart contract creation is Ethereum. The reason for this is that Ethereum is the oldest, most well-established and well-known and also the one with the largest amount of learning material available.

Other platforms with similar smart contract features are: Ethereum Classic, EOSIO, Lisk, Tron, NEO, Stellar, Ripple, NEM, QTUM, WAVES Platform.

Sources & Further reading

To read more about the topic, I recommend the following links:

  • Nick Szabo’s original document, published in 1994:

http://www.fon.hum.uva.nl/rob/Courses/InformationInSpeech/CDROM/Literature/LOTwinterschool2006/szabo.best.vwh.net/smart.contracts.html

  • Ethereum Whitepaper:

As always, please leave suggestions and feedback below.