What are Smart Contracts & How do they Work?

In order to properly understand what smart contracts are and why we would want to use them, we first need to zoom out and understand the earliest cryptocurrencies that existed before the concept of a smart contract had been invented.

The first cryptocurrencies that were created (starting in 2009) did not have smart contracts, and it was only later that the first fully functional smart contract blockchain (Ethereum) was launched to address some of the missing functionality that people who were using cryptocurrencies had discovered they wanted.

In the beginning, there was only Bitcoin. Satoshi Nakamoto in his infinite wisdom gave Bitcoin to us, and people across the world who gazed upon his work saw that it was good.

Bitcoin was the first digital currency in existence and was a reaction to the opaque power structures of the financial system and to the runaway monetary inflation that affected government-issued currencies, particularly following the 2008 global financial crisis.

It was the first open and transparent currency governed by mathematics and cryptography instead of by a small group of powerful men hidden behind closed doors. Using Bitcoin, it became possible for the first time to store and transfer value in an open and transparent way with complete freedom.

However, Bitcoin has some limitations that stem from the purposeful simplicity of its design. Whilst it is possible to make simple payments between people distributed all over the world, and also to create very simple applications (such as multi-signature wallets), it is not possible to create more complex applications.

Over time therefore the need for a more complex ‘programmable money’ arose, which could allow programmers to write any code and allow it to execute cryptocurrency-related transactions.

What are smart contracts?

From a technical point of view, smart contracts are code that is stored on the blockchain and can execute a set of carefully predefined actions in response to humans making transactions from their wallets, or when triggered by other smart contracts.

Ethereum introduced the concept of a ‘smart-contract’ blockchain, with significantly more flexibility for programmers. What this ultimately means is that any computer program which could be written and executed on a normal computer can also be able to be run on Ethereum (although it might be very expensive in practice to do this!) Consequently, this gave a previously unimaginable amount of potential to create entire financial ecosystems and innovate by designing a series of smart contracts that could interoperate with each other.

How do they work?

A smart contract can be thought of as a computer program that is stored and also runs on the blockchain. Smart contracts therefore often contain variations on ‘if… then…’ statements, with certain logic being executed based on various inputs and conditions.

Ethereum, which is today the most popular smart contract blockchain, has its own programming language called Solidity which is used by developers to create applications.

Once written, contracts are compiled into low-level ‘bytecode’ and are then ‘deployed’ to a specific Ethereum address where the program can be accessed by any user of Ethereum, and by other contracts.

In order to interact with that smart contract’s code, a person will call functions by sending transactions containing specific data to the contract’s Ethereum address. This can be done from a wallet such as Metamask.

What can smart contracts be used for?

The use cases for smart contracts are vast, and we have seen huge amounts of innovation take place already. The most popular uses today include tokens representing assets of many different varieties, decentralised finance (DeFi), and non-fungible-tokens (NFTs) representing artwork. However, there are likely many other applications that have not yet been imagined!

Many of the earliest use cases for smart contracts were very simple, however, over time the sophistication has increased. One of the most powerful elements of smart contracts is that they are able to interact with each other, and this has led to ecosystems growing up around them. For example, DeFi is beginning to create an alternative financial system, which began with only some of the simplest elements such as tokens and exchanges, but now includes insurance, complex derivatives, and more.

Simple examples of smart contracts capabilities include:

What are the benefits?

Transparent

Smart-contract code can be observed by anyone who looks at the blockchain, and the rules that govern them are therefore completely transparent. Because of the increased levels of scrutiny, this significantly increases the trust that can be placed onto those applications, compared with what might exist in closed-source applications, websites, or financial institutions. Rules can be observed to treat all equally rather than benefit certain groups of people over others. For example, a decentralised exchange can be shown provably to give everyone the same prices and same fees, whereas in the traditional financial world there have been several high profile cases where it was found to be the case that exchanges provided better terms to some of their customers, allowing them to ultimately make money at the expense of other customers.

Permanent And Immutable

Once a smart-contract has been created and deployed it is permanent and cannot be changed. This gives confidence to those who interact with them that future changes cannot result in the introduction of new bugs that might lead to loss of funds due to hack or mistake.

It also means that applications that interact with them can be certain that the protocols built with smart contracts will continue to behave as expected forever.

Highly Flexible and Customizable

Ethereum and other smart contract platforms allow theoretically for any program to be written and deployed. This leads to almost unlimited potential for developers around the world to experiment and test their new ideas on a global scale without needing to first create the underlying infrastructure to host their ideas.

As a result, we have seen a huge amount of innovation around smart contracts and some fantastically successful protocols created in DeFi that now hold billions of dollars of assets after only a few years, and more are being built every day!

Always Online and Reliable

Since smart contract protocols are deployed on the blockchain they inherit the fact that blockchains are extremely resilient and never go offline. Whilst traditional websites often require maintenance, get switched off after a certain amount of time, or experience issues during upgrades that remove backward compatibility, smart contracts will exist as long as the blockchain continues to exist. This allows those who want to build on top of smart-contracts to know that they will only have to do the work once and won’t find after a certain amount of time that the system changes or stops working.

Deterministic

The outputs of smart contracts are deterministic and therefore always lead to provable results that can be replicated and shown to be fair. The outputs will be the same every time no matter who executes them.

Trustless

Blockchain ensures that transactions must respect the rules programmed into the smart contract, and therefore two or more parties can interact via them without needing to trust each other.

What are the limitations?

Real-World Data

Whilst smart contracts are great for interacting with other users of the blockchain and for transactions that involve assets that are already on the blockchain, to reach their full potential they need to access information about the real world.

For example, if we were building a smart contract to act as escrow agent for payment on a house that was being sold, we would need to have a reliable way to know that the house had been exchanged.

There are many projects that are working on providing ‘Oracle’ data in a secure and trustless way to allow applications to access this data and create logic around them. One popular example is ChainLink.

Lack of Upgradability

Since smart contracts cannot be changed after deployment if software bugs are present they are much harder to fix. This can lead to larger exploits by hackers and loss of funds in the worst case.

One solution to this is that many protocols now use a ‘proxy’ contract which acts as an interface to access the protocol, with the logic held in a second smart contract. Whilst the logic contract cannot be replaced or changed, new upgraded logic can be deployed and the proxy can then be notified to point only at the new contract.

Cost

Whilst theoretically it would be possible to build any computer program using smart contracts the limiting factor is usually cost. Since the contract has to be deployed, execute transactions, and store data on the blockchain, there are costs associated with each of these actions to avoid the overall state getting too large and prevent spam transactions.

In general, developers aim to do as much work as possible ‘off-chain’ and only use smart contracts for the core elements that truly require the benefits they offer.

How can you start experimenting and learn more?

The best way to dive in is to start building!The following resources are great ways to get started:

  1. Ethereum Smart-Contract Tutorials
  2. Start building with an online IDE remix.ethereum.org
  3. Read some code! A complicated real-world example if you’re feeling brave is the StarkEx decentralised settlement system that DeversiFi is built on. Or if you want a simpler starting point, check out an ERC20 token.

If you are a talented developer who would like to start building with smart-contracts and gain experience of working with them live on Ethereum then you should also check out the open job opportunities at DeversiFi: https://rhino.fi/careers/

About DeversiFi

DeversiFi makes DeFi easy. Swap, Invest and Send without paying Ethereum network fees.

Website: https://rhino.fi/

Twitter: https://twitter.com/deversifi

Discord: https://discord.gg/bfNDxZqPSvf

Latest Posts

The Ultimate USDT Bridge.

Bridge USDT across 20+ L1s and L2s for the lowest fees in DeFi, consistently. And thanks to our strategic alliance with Tether, we’ve got the best liquidity on the market too.

Earn 19% APY on your stablecoins