decentralized finance education

Code is Law? Smart Contracts Explained

Introduction

Have you ever heard the expression “code is law” where technology is used to enforce rules? In that case, do we even need lawyers? Or maybe we can live in a fully automated world where code dictates what we can and cannot do. With the current development of smart contracts, this futuristic scenario may be closer than we think.

What is a smart contract?

A smart contract is a piece of code that can be executed automatically and in a deterministic way. The smart contract code is usually stored and executed on the blockchain to make it trustless and secure. Smart contracts also have capabilities of receiving, storing and sending funds and even calling other smart contracts. They follow if-then semantics which makes them fairly easy to program.

Smart contracts aim at removing the human factor from decision making. The human factor is often proven to be the most error-prone and unreliable element of the standard, traditional contracts.

A vending machine comes very often as a good analogy to a smart contract as it shares some of the similarities. A typical vending machine is programmed in a way that allows certain actions and state transitions based on the input. It also works in a fully deterministic way. For example, if you want to buy a can of coke that costs $2 and you only have $1 no matter how many times you try you won’t be able to get the drink. On the other hand, if you insert $3, the machine will give you a can of coke and appropriate change. Even the change that is given is selected in a predefined and programmed way based on which coins are available and which coins the machine wants to get rid of first.

A smart contract can rely purely on the information available on the blockchain, for example, if you give me 10 tokens A, I’ll give you 10 tokens B (assuming both tokens are available on the same blockchain) or it can rely on external data sources, for example on the Ethereum or S&P500 price. The latter example makes smart contracts more difficult as they have to trust real-world data. The needed trust can be minimised by using oracle services, but even the oracle services have to be trusted. There are already a few projects that by using certain incentives make oracles more likely to provide correct data. Chainlink is a project that clearly stands out in this category.

Ethereum is a good example of a blockchain that supports smart contracts and make it possible for a programmer to implement their own smart contracts. A smart contract can be written in a programming language called Solidity which was created specifically for that purpose. In Ethereum, all the deployed smart contracts are immutable. This means that once deployed they cannot be modified which creates certain risks that we’re going to discuss later. Smart contracts on Ethereum are also decentralized which means there is no single machine controlling the contract. In fact, all the nodes of the Ethereum network store the same contract with exactly the same state.

Although Ethereum is currently the most popular general-purpose smart contract platform, it is not the only one and it has a few competitors. Some of them are Cardano, Tezos, EOS, Tron, but not all of them share the same characteristics.

The term smart contract was coined by a well-known cryptographer Nick Szabo in the early 1990s. The name although not the most self-explanatory stuck and it is commonly used, especially in the blockchain industry.

Smart contracts vs traditional contracts

To see the benefits of smart contracts, let’s compare a hypothetical smart contract to its equivalent in the traditional space.

Let’s say we want to write the following contract.

If Alice sends X number of tokens A and Bob sends the same number of tokens B, the tokens will be swapped and Alice will receive Bob’s tokens and Bob will receive Alice’s tokens.

In a non-smart contract world, one way of achieving that without Alice having to trust Bob and Bob having to trust Alice would be to create an escrow contract with a third party. The third-party would collect tokens A from Alice, wait for the same number of tokens B from Bob and send Alice and Bob their respective, swapped tokens. This approach already shows a few problems that Alice and Bob may be facing.

Trusting intermediaries. There is no guarantee that the third party will not run away (or lose) with the tokens after receiving funds from Alice and Bob. We have to rely on the reputation of the intermediary and potential insurance. This approach is not deterministic and if something goes wrong it may have different outputs depending on multiple factors including the jurisdiction where a potential case would be settled in. On the other hand, the smart contract would work in a fully automated and deterministic way making sure that both of the parties receive funds when they meet the initial criteria of depositing coins. Smart contracts can also hold funds within themselves which is just not possible to achieve in the traditional world.

Speed. Depending on the intermediary Alice and Bob may have to wait even up to a few days or weeks to settle the transition of tokens. What if they want to swap tokens on Sunday? Is the intermediary even operating? With smart contracts, these kinds of problems go away and a contract can be fulfilled seconds after the initial criteria are met.

Cost. Traditional contracts are not only expensive because of the intermediary that have to make their profit, there is also a huge risk of hidden costs for things like arbitration and enforcement if there are any problems with the contract.

Reusability. The same smart contract that is responsible for swapping Alice and Bob’s tokens could be used by anyone else who wants to swap tokens. In the traditional world, they would all have to sign separate contracts and pay their respective fees to the intermediary.

Fraud. This is yet another hidden cost this time for the intermediary itself. The intermediary would have to make sure that both Alice’s and Bob’s tokens are legitimate before initializing a swap. Fraud is very common in traditional finance and most companies have huge teams working purely on preventing fraud. With smart contracts, the tokens can be verified on the blockchain and with digital signatures, it’s clear straight away if both Alice and Bob are eligible for spending their tokens.

What are the use cases?

Smart contracts have a growing number of use cases ranging from payments and decentralized finance to supply chain and crowdfunding. Smart contracts are also the basic building blocks for decentralized applications or Dapps. Let’s talk about a few examples of smart contracts.

Decentralized finance or DeFi is one of the new industries that rely heavily on smart contracts. Some of the things that have been already built in this space include.

Decentralized stable coin

With a clever use of smart contracts and certain incentives we can create a stable coin that is pegged to the US Dollar without having to store dollars in the real world. MakerDAO is one of the companies that makes it possible.

Automated liquidity provisioning

A set of smart contracts can allow users to provide liquidity and swap tokens in a complete permissionless and decentralized fashion. Uniswap or Kyber Network are good examples of such protocols.

Another use case is providing more transparency to the supply chains where protocols like OrginTrail comes to play.

When it comes to crowdfunding you can imagine a contract that unlocks funds as soon as certain goals are met and verified by the community.

This doesn’t stop here. Time for a bit more futuristic examples.

What if smart contracts could facilitate things like ride-sharing, apartment rentals and much more?

How about charity? You can imagine a fully automated fund that would send money directly to the people who need them the most without any intermediaries. For example, the fund could determine that certain region was struck by a hurricane and redirect funds to that part of the world. For now, it sounds quite impossible, but all the necessary elements to make something like this happen are being built while we speak.

The use cases for smart contracts are almost infinite, but before we can achieve all of that we have to tackle a few problems.

What are the risks?

One of the main risks when it comes to smart contracts is something that haunts every other software – bugs.

The best example of a bug in a smart contract is the DAO hack that resulted in millions of dollars worth of Ether lost as the attacker was able to drain funds from the smart contract. This caused Ethereum to hard fork and created a lot of disagreement in the Ethereum community.

Since the DAO hack the Ethereum community came up with a lot of extra security measures. These days pretty much all popular smart contracts have gone through a security audit, often by multiple teams. Besides that, there is a trend for using formal verification methods to prove that certain contracts will always behave in an expected way.

Protocol changes.

Even if a smart contract doesn’t have any bugs and has been audited we still cannot guarantee that a change on the platform level will not cause any problems. We can imagine a situation where an upgrade to the Ethereum itself my cause certain smart contracts to start behaving differently to what was expected before.

Real world issues that cannot be easily communicated by oracle services.

It’s true that oracle services can provide a reliable way of getting information from the real world into the blockchain. For example, as we mentioned earlier they can provide an Ethereum or S&P500 price. Now imagine that you rented an apartment or a car and made some accidental damage. How would a smart contract without any human intervention possibly know about it? There are multiple examples like that when it is hard to imagine how something unexpected that happens in the real world can be visible to smart contracts.

Besides all the above there are also other risks involved in things like regulation or tax but I believe that they can all be eventually solved.

So can we actually replace lawyers with code?

Hmm not quite, at least not right now.

In the future, I can certainly see more and more contracts being automated, especially when it comes to finance, but even in a fully automated world, lawyers can provide valuable knowledge that can be translated into code.

Besides that, there is a lot of regulatory challenges when it comes to laws around the crypto industry itself that will keep lawyers very busy for a while.

Nevertheless, if I was a lawyer I’d start thinking about learning a little bit more about smart contracts and coding as I believe they will play a big role in the future.

Summary

Let’s quickly summarise both the pros and cons of smart contracts.

ProsCons
fully automatedsoftware bugs
deterministic resultsprotocol changes
trustlessunclear regulation
fastunclear tax
precise
secure
cost efficient
transparent

Even though smart contracts carry certain risks, we have to remember that we are still very early and most of the current problems are solvable.

Extra

If you have any questions about smart contracts or any suggestions for the next post please comment down below.