Smart Contracts, an explanation with examples

Introduction

In 1996, Nick Szabo, a computer scientist, and an early cryptocurrency pioneer described smart contract as “a set of promises, specified in digital form, including protocols within which the parties perform on these promises” (Szabo, 1996).

That definition is everything I don’t understand about law combined with everything I don’t understand about computers. So let me try to break it down and talk in plain English from now on.

Contract

Let’s start with a contract. Larry Lessig, a Harvard Law School professor, describes a contract in plain English as:

promise or performance

given in exchange of

promise or performance

This definition maps out four different possibilities:

  1. A promise in exchange for a promise: I promise to pay you ₹ 1,000 if you promise to sing a song for me tomorrow. Both the actions will happen in future but both parties are binding themselves by the virtue of promise.
  2. A promise in exchange of a performance: I promise to pay you ₹ 1,000 if you sing a song now. Note that I am not interested in a promise, I want the actual performance now.
  3. A performance in exchange of a promise: I’ll sing if you promise to pay me ₹ 1,000. I will perform now on your promise to pay me later.
  4. A performance in exchange of performance: I’ll sing a song if you dance on it. I’ll perform and I expect your performance simultaneously.

The given in exchange for part is also critically important, it sets the quid pro quo context in the arrangement. I cannot ask you to sing a song without any consideration from my end, it can be asked as a favor but not as a part of contract. However, we can have a contract that I’ll pay you ₹ 1,000 to sing a song. In this case, both the parties will be obligated to honor their respective commitments.

Smart Contract

Coming back to the smart contract let’s look at the definition and try to break it down in plain English:

 “1a set of promises, 2specified in digital form, 3including protocols within which the parties 4perform on these promises”.

1A set of promises: These are the promises each party in the contract agrees to deliver upon. All parties will be required to honor their end of commitment and if not honored it will be considered as breach of contract.

2Specified in digital form: A typical contract is a form of a document. This document contains all the terms and conditions drafted in a widely used language and are agreed by all the involved parties by way of signing it. A smart contract however is in digital format and operates electronically. All the terms and conditions and even the outcomes are embedded in lines of code in a computer readable language (eg Solidity). Although most smart contracts would have an assisting whitepaper describing the process in English but lines of code is the source of truth.

3Including protocols: These are the protocols each party needs to adhere to while interacting with the contract. A contract can have many features, the protocols will define how those features can be accessed.

4Within which the parties perform: All the activities including performance are recorded in digital format. These performance may include transfer of money, transfer of goods, rendering of service and many others, all of these needs to be as per the protocols, recorded and stored within the contract in digital form. Some promises like promise to pay certain amount can also be automated to limit human intervention to ensure commitment.

Examples

Let’s look at three examples of contract and how they operate:

1. Share allocation in an IPO through ASBA – A traditional contract

Initial Public offering is a process when a company decides to issue its shares to public for the first time, this is also referred as ‘going public’. People (Investors) interested in investing in company’s shares can bid for same on a bidding platform. This is a promise given in place of a performance. Investors promise to pay the bid amount if the bidding platform allocates the shares to them. The platform on other hand runs a risk that applicant might not have the capability to pay if shares are allotted. To reduce the risk a trusted third-party is involved in the contract – Banks. Investors need to send an application to their banks along with their bid amount. This application is known as ‘Application Supported by Blocked Amount’ (ASBA). Banks then compiles the list of investors and their details (like PAN, bid amount, etc) and send it to the stock exchange.

Share allocation in an IPO through ASBA

The actual process is divided into two steps:

i. Application

Application to bidding platform

Investor submits the ASBA form to the bank and adds the bid amount to their bank accounts. In turn, the bank send investor’s bid with details to the bidding platform. Banks also blocks the bid amount in investors’ account.

ii. Allocation

Allocation of shares

At the time of allocation, the bidding platform allocates share to an investor i.e. transfer shares to investor’s demat account and bank transfers the bid amount (₹) to the bidding platform.

Adding banks as an intermediary converts the contract from promise in place of a performance to performance in place of performance. This benefits both the parties in the following manners:

For Bidding Platform: Platform trusts banks to ensure investors payment capacity and thus investors without having actual fund cannot enter the bid.

For Investors: There is no need to pay upfront. The amount submitted along with the ASBA form is blocked in investors account but never leaves the account.

2. Lottery – A sample Ethereum Smart Contract

Lottery is a sample of a simple smart contract on Ethereum blockchain. This is like a real-world lottery system where n number of people can enter the lottery paying a minimum amount of money (Ether, a crypto currency like Bitcoin in this case). All the money collected is stored within the system in pool of money. This is a sample of performance given in place of a promise.

Lottery smart contract overview

This smart contract has two functionalities:

i. Entering the Lottery

Players can enter the lottery by transferring a minimum of 0.01 ETH (Ether) to the contract. Any player who tries to enter the lottery by transferring amount less than 0.01 ETH will be rejected. Once the amount is verified and approved players name or a unique identification (ID) will be stored in the contract and the amount transferred will be added to the pool of money within the contract.

Entering the Lottery

ii. Winner selection and declaration

After a pre-defined period, manager (the person who created the contract) will initiate winner selections process. The winner will be selected randomly from list of all the players in the lottery. Although manager initiates the selection, they can’t select the winner of their choice, it will always be a random process. Also, only the manager can initiate the selection. Once the winner is selected all the money available in pool of money withing the contract will be transferred to winner’s wallet.

Winner selection and declaration

The Lottery contract have all the properties of a smart contract:

  • a set of promises – Players entering the contract performs by paying the minimum amount, while contract promises that the lottery winner will win all the money collected.
  • specified in digital form – The whole contract is defined in form of a lines of code written in a computer readable language, Solidity. There might or might not be a supporting white paper to describe the contract but the code will serve as the source of truth. This means, all the parties cannot just rely on the whitepaper to describe the contract. If there is something coded in Solidity but interpreted differently in the white paper, the computer code will considered as correct. The code snippet is attached at the end of this article for the interested ones.
  • including protocols – The contract have following properties
    • manager – Every lottery will have a manager. A manager is the one who initiates the lottery contract.
    • Enter() – Using the enter feature, anyone can ether the lottery by depositing minimum 0.01 Ether into the contract. This money will be stored in the pool of money and players name will be stored in a list on contract. Anyone trying to enter the lottery depositing less than 0.01 ETH will be denied.
    • pickWinner() –  This feature will select one player from the list of players randomly and transfer all the collected funds to the winners wallet. Only the manager can initiate this feature, if anyone else requests this, the request will be denied.
    • within which the parties perform on these promises – All the promises, performances and features related to contract should exist on Ethereum Blockchain. The transactions like adding money to pool when someone enters the lottery are recorded on blockchain. Some transaction like transferring all the money in pool which are ensured by the contract are automated.
Lottery contract protocols

Although the players must pay upfront to enter the lottery, the money is going to the contract and not the manager. Manager cannot just take all the collected money and run away as there is no protocol for that. Thus, the Lottery smart contract ensures trust without involving a third party.

Using the Lottery contract as base, many more features can be added to make the contract more complex and serve more applications. The contract can be extended to different periods and ensuring there are no repeat winners until all have won at least once, this contract can serve as a Kitty Party Contract. Similarly, this can also be converted into a committee contract or poker contract to manager poker chips.

3. The DAO – A Decentralized Autonomous Organization smart contract

The DAO was an actual smart contract that existed on Ethereum blockchain and was written in Solidity. The DAO was launched on April 30th, 2016, and aimed to be an investor directed venture capital fund. The core functionality of DAO was that people from all over the world will pool their money into a venture capital fund, which will be used to fund smart contract-based ideas. There will be no director or fund manager and instead investors will decide which smart contract to fund. Each investor will vote, and the highest voted idea will be funded. This process will be repeated to fund various startups. The value generated from these funded smart contracts will be distributed among investors of DAO.

DAO smart contract overview

There were three core functionalities in the contract:

i. Entering the Fund

Entering the Fund

ii. Voting for the ideas

Voting for ideas

Different ideas will be pitted against each-other, and investors will vote for their favourite ones. The idea with highest number of votes will be declared as the winner.

iii. Funding the ideas

Funding the ideas

The winning idea gets the go ahead and all the ether collected in the pool will be transferred for its development. Further the value created from these ideas will be distributed among the all the investors whose money was used for the funding.

iv. Opt out provision

The investors who didn’t vote for the winning idea will be the minority and among these there might be some investors who won’t want their contribution to be used for funding of the winning idea. For instance, Investor 3 in previous voting illustration didn’t vote for Idea 2 (the winning idea) and would prefer to opt out instead of funding idea 2 from their part of the pool. To keep the interest of such minorities, opt out feature was added.

Opt out provision

In the opt out feature, any investors who wants to opt out of the contract can take their money out of the collective pool and after a defined period get the amount back in their wallet. Continuing from previous example, if an investor initiates the feature, it had following steps:

  1. Contract looks into the pool of money register to determine investor’s contribution.
  2. Contract then creates a child pool and transfers investor’s contribution into it. Investor 3 has exclusive voting rights for this pool.
  3. Contract updates the collective pool register by reducing the investor’s contribution to zero.
  4. The money stays in the child pool for 27 days. After that, investor can request to move the segregated money back to their wallet.

This way any investor can pull their contribution back from the contract and won’t be the part of the profit/loss sharing.

The DAO contract was launched on April 30, 2016, on Ethereum network and was a huge success. The contract invited investors to invest for a period of 28 days and attracted 1100 investors contributing 12.7 million Ether (~USD 150 million, at that time) to the contract. This was one of the largest crowdfunded campaign in the world. The contract held 14% of total ether circulated in the market by end of May 2016. The DAO had a dream start and attracted a lot of media attention which could have served as a good platform for future smart contracts.

However, on July 17, 2016, a hacker found a loophole in Opt Out feature of the contract. The code for opt out was written in a way that it transferred the money first (step ii) and updated the collective pool register later (step iii). The code exploited this vulnerability, they initiated the opt out request, code determined their contribution, transferred it to a child DAO and before code could update the collective pool register, hacker initiated the request again. In this way code was never able to update the pool register which means as per the pool register hacker still had their investment in the pool, even though in actual it was transferred to child DAO. When code was called again it transferred the same amount to again the child DAO, without updating the register. Hacker initiated the request several times recursively and was able to drain approx. 3.6 million Ether (~USD 50 million, at that time) from the DAO contract. The Ethereum community noticed this abnormal transfer from the DAO fund

In the open letter, the attacker claimed that code is law in the DAO and what he did was allowed by the code, thus, his actions were legitimate. The attacker continued by saying that “a soft or hard fork would amount to seizure [his] legitimate and rightful ether, claimed legally through the terms of a smart contract”.

The Ethereum community had 27 days to decide on whether to do nothing what the attacker wanted of do a soft/hard fork and return the funds to rightful owners. The community eventually decided to do a hard fork, which was completed by July 20, and funds were returned to the original investors.

Conclusion

A contract is a promise or a performance given in exchange of a promise or a performance. A well-structured contract will define obligations and expectations of all the parties involved. In order to reduce the risk that a party might not honor its obligation a trusted third party like banks/court/government can be involved as an intermediary like in IPO allocation example.

A smart contract is a digital form of the contract in which all the terms and conditions are defined by computer code. This digitalization enables parties to run the code on a blockchain which would eliminate the need of including a trusted third party in some of the contracts like in lottery example.

The smart contract considers code as the law, and thus all the involved parties must understand the code and its vulnerabilities to fully comprehend terms and conditions of the contract. In case there is a loophole, a hacker might attack the contract and since code is law, their actions will be considered as legitimate. This is why some consider a better term for smart contract should be ‘dumb contract’.

Appendix

The code for Lottery contract

contract lottery{
    address public manager;
    address[] public players;
    function lottery() public{
        manager = msg.sender;
    }
    function enter() public payable {
        require(msg.value > .01 ether);
        players.push(msg.sender);
    }
    function pickWinner() public restricted{
        uint index = random() % players.length;
        players[index].transfer(this.balance);
        players = new address[](0);
    }
    function getPlayers () public view returns (address[]){
        return players;
    }
    function random() private view returns (uint){
        return uint(keccak256(block.difficulty, now, players));   
    }
    modifier restricted() {
        require(msg.sender == manager);
        _;
    }
}

Leave a Comment