Blockchain Demystified: Smart Contracts

Ayoub NEJJARI
The TechCave

--

Since the word blockchain has come into existence, a lot of new, and most of the times, scary concepts appeared and many questions have been raised as to how this new “uncertain” model will change fintech and economy as claimed. It’s really confusing and sort of obscure for newcomers and even for some IT folks. But the good news is that it only requires a little bit of computer science knowledge, knowing a handful of economics principles, and reading a little bit about this technology to start getting comfortable around blockchain concepts.

In this article, I am going to explain one of the most important and promising features of blockchain technology, which, actually, is taking this whole thing to the next level in terms of cryptocurrencies and digital economy, and is also making it possible to apply blockchain beyond its current scope. This feature is the Smart Contract.

Retrieved from http://legalexecutiveinstitute.com/wp-content/uploads/2016/04/blockchain.jpg

Before I go on, it’s important to mention that this concept is not as old as the first blockchain platforms, namely Bitcoin. In fact, smart contracts are not possible in the Bitcoin blockchain so far. That’s because the way the Bitcoin blockchain was built and meant to operate. Ethereum was the first to introduce this concept and it is changing the blockchain realm in a remarkable way.

Let’s talk about Contracts first

Traditional contracts are beyond the scope of this article, but I just want to go over some important aspects about this model.

A contract is a verbal or written agreement between two or more individuals/organizations that states the expectations of both parties and how to resolve conflicts when things go wrong. It could be formal or informal. As long as it is an agreement that is legally enforceable by law, it’s called a contract.

Now, to have a better idea about this concept, let’s see it from a different angle. Here is Wikipedia’s definition:

A contract is a voluntary arrangement between two or more parties that is enforceable by law as a binding legal agreement. A contract arises when the parties agree that there is an agreement. Formation of a contract generally requires an offer, acceptance, consideration, and a mutual intent to be bound.

While contracts could be used in practically anything, we tend to hear this word a lot in the context of businesses. In which the definition of the notion could be reformed to something like this:

“A contract is an agreement to perform some work in exchange for money, or anything of value, as long as both parties accept the terms in the contract. The agreement could be enforced by law”

Now that we have a clear idea of what a contract is, let’s see why humans developed this model. Why do we need contracts anyway?

Retrieved from https://www.4ward365.com/wp-content/uploads/2016/09/partner_record_post.jpg

Actually, it’s really ironic to know that the very entity that contracts are meant to serve is the reason why contracts exist in the first place; humans.

Put simply, a contract is a way of protection for both parties that ensures everything in the agreement is clear and that what was agreed upon is what will happen. Here are some of the advantages of contracts:

  • Providing clarity in relationships and agreements as well as rights and duties of each party
  • Minimizing disputes and problems
  • Protection of properties
  • Security and a degree of certainty
  • etc.

These are just some of the reasons why we need contracts. And as to when do we need contracts? I think, whenever we feel a little bit uncertain or insecure about an agreement, we’re most likely to resort to contracting.

But wait! Why in the universe are courts filled with disputes between parties that have started their relationship, or business, with a contract? Why do we still see problems and conflicts that seem impossible to resolve even though everything is clear in formal agreements? Worse yet, Why most of the times, these disputes wind up doing harm more than they protect? Why sometimes trials end up unjust?

Well, the answer to all these questions is simple. Humans are not that just and righteous. There are degrees of course. But, let’s be honest, we’re selfish and sometimes could do evil things for the sake of protecting ourselves from punishment even though we deserve it. We’re wired to act like that, and unless we intentionally put the effort to adjust our behavior, there will be always a chance we could do pretty bad things, like:

  • Bluffing
  • Fraudulence
  • Forgery
  • Counterfeiting
  • and the list goes on…

Humans are pretty creative when it comes to bypassing laws.

What about computers, though?

Blockchain 2.0

Bitcoin and the other blockchains were designed to support almost all types of transactions in our traditional systems. From escrow transactions and bounded contracts, to financial transactions, like stock, private equity, mutual funds, etc. Actually, they were designed to do more, as we will see in a few.

It’s really difficult to link blockchain technology to something beyond cryptocurrencies. The reason for this is that, from the very first moment, we were introduced to blockchain technology through Bitcoin and Cryptocurrencies. Which makes more sense when we realize it actually was all about cryptocurrency at first. That’s the first phase, or better yet, the first application of blockchain technologies, and is referred to as Blockchain 1.0.

However, this innovation was designed to do more than that. Blockchain 2.0 is the set of capabilities, techniques, and technologies that allow us to go beyond cryptocurrency and apply blockchain in other aspects of communities. Blockchain 2.0 includes Smart Contracts, Smart Properties, Dapps (Decentralized applications), DAOs (Decentralized Autonomous Organizations), etc.

Smart Contracts

Finally!!

I am assuming this is not your first time to read about blockchain. Having said that, I’ll briefly brush up on some of the concepts we need to know in order to understand the Smart Contract concept better.

Important concepts:

  • Node: a node is anything that is part of the network and can perform transactions and tasks on the distributed ledger.
  • Distributed ledger: a shared database that is synchronized across multiple nodes and is updated and modified by mutual consent.
  • Blockchain: Can refer to the network of nodes or the distributed ledger across nodes in the network.
  • Transaction: is any operation performed on the distributed ledger. It usually refers to the transfer of valuable assets between nodes.
  • Mining: The process by which transactions are verified and added to the blockchain ledger.
  • Block: a block is a container data structure that contains a certain number of transactions that are permanently recored and stored in the blockchain ledger.
  • Blockchain Virtual Machine: A machine that involves/needs some or all nodes to perform tasks and is a runtime environment for smart contracts.

Before I define what a smart contract is, I just want to point out that a smart contract isn’t necessarily a “contract” as in a formal contractual document. It could be anything from simple operations to complex chains of transactions. Think of classes in OOP.

So, without saying anything else:

A Smart Contract is a self-executable, enforceable, cryptographically-hashed and immutable contract between two or more parties, or nodes in the blockchain language, that could be used in all types of applications such as financial transactions, public records, identification, intangible records, attestations, etc. A Smart Contract is automatically enforced by law, through code, without human interruption.

Let me explain.

Now with the blockchain technology we are able to store and transfer not only currencies, but any kind of valuable assets in a very secure, fast, and transparent mode thanks to the underlying architecture of the blockchain network. How?

Well, thanks to the super power of cryptography, which needs another long post, and with the distributed ledger that cannot allow forgery of any already-mined block unless approved by all nodes in the network, which is an extremely rare case. And with the autonomy, self-sufficiency, and trust-less nature of the system, which means there is no need of any kind of intermediary between the parties(middlemen will only increase the complexity and vulnerability of whatever goes on between the parties involved), we are able to store, identify and transfer anything of value in a way that’s not only more secure than our traditional systems, but also cheaper due to the elimination of third-party intermediaries.

Not only that. As Lawrence Lessig tells us “Code is law”. Thanks to the security and reliability provided by cryptography and with the nature of computer code that will execute no matter what, as long as the conditions specified are met, we are able to enforce specific rules and define how to resolve disputes and conflicts in specific conditions, or when things go wrong, by enforcing law through embedded rules in the contract. This will ensure everything in the contract will go smoothly and will significantly reduce the risk of things going wrong. And if, at all, that happened, the problem will be solved objectively and justly. Of course, this is not as easy as it sounds and there are complex situations where this model may not be valid. But it’s definitely better and more reliable in most cases.

Note:

Smart contracts could involve humans, which are represented by nodes that are called Externally Owned Accounts, or could be meant to operate on other smart contracts as well.

I think now that you have a clear idea about traditional contracts and the fact that we just learned about what a smart contract is, you certainly have identified some areas where smart contracts are better than the traditional model. But just in case, let’s go over the advantages of Smart Contracts and see what we can achieve with them.

Let’s start with the obvious ones:

1- Cryptographically-secured and immutable

A Smart contract, which is represented by a block of data and instructions and can involve more than one transaction, is encrypted and stored on the blockchain ledger and cannot be accessed by anyone other than those who have the private key. Even then, it is impossible to change or modify anything in the contract. The contract will execute on the blockchain virtual machine, e.g. Ethereum Virtual Machine (EVM), whenever the conditions specified in the contract are met.

2- Enforceable through code

Because the contract is in the form of code, It will be always enforced to execute. And in case something went wrong, everything will be resolved according to the dispute and conflict resolution rules embedded and enforced by the code.

3- No human interruption

This means reduced risk of forgery, fraudulence, or any kind of injustice caused by humans.

Let’s see some other advantages of Smart contracts:

4- Fast and Cheap

Creation and validation of contracts as well as recording and transferring assets become way faster and easier due to the elimination of intermediaries(=extra fees, time and efforts). And thus, business deals and transactions become seamless, straightforward and effortless.

5- A more secure and reliable System

Less human intermediaries means less subjectivity and biased decisions, which means less flaws in the system, reduced risk and vulnerability, and thus, increased security.

6- Smart Properties

With smart contracts, assets and all kinds of properties become blockchain encoded. And thus, properties become easy to register, transfer and exchange through smart contracts. The owner can easily sell the asset by transferring its private key to the other party via a smart contract. Which could be automated.

7- A new level of Artificial Intelligence

If we combine smart contracts with artificial intelligence capabilities, and with IoT, unimaginable things could happen. We could create fully fledged autonomous, and automated organizations and even communities that empower and simplify a lot of tasks and operations handled inefficiently in traditional organizations and communities. Actually, the combination of these technologies is actually where the concepts DAOs(Decentralized Autonomous Organizations), DACs(Decentralized Autonomous Corporations), and DASs(Decentralized Autonomous Societies) came from.

8- Automatic markets

Well, if you combine the above characteristics and capabilities this should not surprise you. Now with the blockchain, we could create fully fledged and fully functioning automated trading systems as well as markets of automatically transacted resources(electricity, gas, etc).

9- Automatic Legal Systems

Societies and communities have now the ability to create their own private blockchains. This raises many questions as to how we can impose laws and regulations within our society on blockchains to maintain order. Even though blockchain networks and protocols are not there yet, integrating our legal systems into the blockchain is possible more than ever before. In fact, the development of blockchain technologies is heading that way very fast.

Retrieved from https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAo9AAAAJDVmZDZhM2U0LWM1MDktNDY3OC04NThkLTU0NjA0ZDM5ZDc3YQ.jpg

So, yeah! That’s the potential of blockchain and smart contracts. Actually, there could be more in the near future as blockchain infrastructures develop and mature over time.

Now, let’s go over two examples that illustrates what smart contracts could do just to have a better idea.

1- Real Estate Contract:

Let’s consider a real estate contract in which the ownership of a property transfers to the buyer by the closing date set in the contract. Of course, this contract would be legally binding.

Now I don’t have to mention the limitations of this approach as we’ve already mentioned some in the above explanation.

If we were to apply the blockchain approach instead, a smart contact is created and signed(digitally through the public key of each node) by both parties. This smart contract would be framed and restricted by the terms set in the agreement and obligations of each party through code instructions and it would include all information about the deal, the price, the closing date, etc. Upon the closing date, the owner ship will automatically be transfered to the buyer by transferring its private key. and Everything will be registered in the public ledger securely. If any thing wrong were to happen, the conflict resolution instructions would solve that automatically.

You can imagine almost all kinds of contracts and agreements using this approach and how we can avoid many issues. Remember “code is law”, and everything is encrypted and registered, and so the likelihood of one of the parties counterfeiting, or not feeling like complying with the terms is zero.

2- The Smart Will

Another example where smart contracts could be applied is a Will or a Testament. A transaction, or a set of transactions, could be created and activated on the blockchain. The transaction is meant to execute upon the death of the testator, which is represented by an event that is trigged by a notification from a program that keeps track of a death registry database. Upon the execution of the contract, the predefined amount of money in the contract is automatically transferred to the beneficiary’s bank account. Also, the transfer of the ownership of properties will automatically be handled by the smart contract.

Other examples would be smoother and more robust taxes and bills payment systems, securing smart devices, crowdfunding, public records, etc.

It’s worth noting that executing smart contracts on whatever blockchain you’re on, in most cases it would be Ethereum, is not free. In order to activate and execute smart contracts on the blockchain, you pay a very small amount of money called gas. That’s because executing operations on a blockchain requires a certain amount of resources, especially if the number of transactions is high and the operations are complex. Nonetheless, the amount is really small. As of this writing, 1000 units of gas is worth $0.00435 in Ethereum.

Smart contracts are just another application of blockchain, and it’s only going to get more interesting. If this was your first time reading about smart contracts, then it’s okay to feel a little bit dizzy and confused. It takes time to get familiar with this model. But once you get familiar with it, and understand its potential and what it it can do for us as individuals and as communities, believe me, you wouldn’t want to go back.

Here is an example of a simple smart contract from Solidity(Smart contracts language) docs:

pragma solidity ^0.4.0;

contract Coin {
// The keyword "public" makes those variables
// readable from outside.
address public minter;
mapping (address => uint) public balances;

// Events allow light clients to react on
// changes efficiently.
event Sent(address from, address to, uint amount);

// This is the constructor whose code is
// run only when the contract is created.
function Coin() public {
minter = msg.sender;
}

function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;
}

function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
Sent(msg.sender, receiver, amount);
}
}

If you liked the article and it was useful for you, please help me to make it reach to a wider audience by sharing it and clapping as many times as you can, but make sure you don’t hurt your hand. ^^

Follow me to learn more about Blockchain in future posts.

--

--