Crypto Education

What are smart contracts?

An in-depth article explaining all you need to know about smart contracts.

Mycelium Network
Mycelium Network Media

--

Simplified Illustration of a Web 3 Transaction

Introduction

Smart contracts were first proposed by the computer scientist Nick Szabo in 1994. He was the inventor of the virtual currency ‘Bit Gold’, which was created in 1998, 10 years before the arrival of Bitcoin. He defined them as computerised transaction protocols that execute the terms of a contract.

Another definition is that they are a tamper-proof program that runs on a decentralised network, and executes its terms once predefined conditions have been satisfied.

Imagine a virtual vending machine or virtual ATM. This is a simplistic way of explaining what a smart contract is. A program which is a tool for automating transactions or processes that are designed to be secure from any form of intrusion. They allow trusted transactions and agreements to be carried out between anonymous parties without the need for an external enforcement mechanism.

Smart contracts are the same as traditional contracts in that they establish the terms of an agreement. Reducing paper consumption is far from the only use for a smart contract. They go one step further, in that they also execute the agreement as soon as the terms have been met. When they are designed properly they are truly ironclad. The result is an evolution from traditional terms, which are dependent on external factors to see execution, to a digital guarantee that execution will occur according to the code.

The term ‘smart’ comes from the fact that the contract’s terms are written as code and executed on a computer network. Once deployed to a decentralised network such as Avalanche, a contract cannot be updated or altered maliciously. They can define all kinds of processes and interactions to occur efficiently. For example, they are regularly used to execute financial transactions to the tune of billions of dollars without the need for an intermediary human or institution such as a Bank.

Once a contract has been audited to identify any potential problems that can be exploited, it will perform exactly as it has been defined. For this reason, they have become known as trustless technology, where trust is not placed on any party but rather on the contract itself.

How do smart contracts work?

Smart contracts function by following simple statements that are translated into code on a decentralised network. A contract follows predefined rule sets, for example: “if the requirements of condition A are met, execute function B”. Once predetermined conditions are verified and accomplished, a network of computers executes the actions defined in the contract. Such actions may include transferring money to determined parties, sending notifications, or registering a vehicle etc. The smart contract defines who can interact with it, how they can interact with it, when it can be interacted with and what inputs result in an output.

Once a given action (defined in the smart contract) is triggered to be performed, it is written to the next block of data in the network record. Once that data is added to the record, the action is performed and cannot be changed.

A single smart contract may have multiple conditions of operation and an application may use multiple different smart contracts to carry out an interconnected set of processes. Smart contracts have steadily become the building blocks of an entire ecosystem of DApps (decentralised applications) and represent a major focal point of decentralised computing development in general.

My-Chance, part of the Mycelium Network ecosystem, is an example of a DApp that functions through the use of smart contracts.

Benefits of smart contracts

Traditional digital agreements involve two or more parties which are unknown to one another. To minimise the risk that a participant won’t abide by the agreement, such agreements are often hosted and executed by a third party in the form of a centralised institution. While this situation mitigates some of the risks, it creates a situation where the intermediary may exert influence over the situation or behave in a manner that is not in line with the wishes of the participant. This could range from data leaks to the misappropriation of funds.

Smart contracts remove the need for third-party intermediaries. Photo by Pavel Danilyuk

Smart contracts have improved upon digital agreements by introducing new advances including:

  • Reliability- Interactions processed by a program deployed on a decentralised network mean there are no means for a party to manipulate the outcome. A tamper-proof contract guarantees execution according to the defined terms.
  • Security- Smart contracts deployed on a decentralised network have no central point of failure or attack. The removal of an intermediary means there is no means for misuse of funds.
  • Efficacy- With automated processes and the removal of intermediaries there are savings in time and economic costs.
  • Transparency- Parties of a smart contract may access information in real-time. Programs run immutable data which ensures no breach of conditions or mistakes can be made. The information within the contract and its record of execution remain available to view to all parties at all times.
  • Autonomy- Interaction with an immutable contract allows execution between parties involved without parties knowing each other.

Smart Contract Use Cases

Smart contracts are being used to create an ever-increasing variety of tools and services. What began as a means of transferring value is growing into ecosystems of DApps and financial tools, tokens management systems, logistics network automation, gaming platforms and beyond. They have allowed for the creation of DAOs with automated governance systems and proposed advances in the corporate legal system. As an emerging technology, the limitations are ever-decreasing. As oracles, which allow for integration of off-chain data, continue to provide new uses.

Some examples of smart contract infrastructure include:

Defi- Decentralised finance markets allow individuals to trade assets, borrow against assets or earn interest through lending. Over-collateralized lending protocols have allowed for increased utility for digital assets, allowing borrowers to gain working capital and lenders to receive the benefits of allocating capital. Risk is mitigated for lenders through price feeds, which track the valuation of assets and ensure loans are issued at a fair market price. Staked collateral is liquidated if its value drops below a certain threshold before repayment has been made.

Supply Chain- The incorporation of smart contracts can improve several supply chain demands, ranging from the sourcing of goods to the delivery processes. Hybrid contract oracles can link web APIs and real-world sensors such as GPS and condition monitors. This allows for greater transparency, producing a source of truth about materials as well as a reduction in overheads with the automatic triggering of payments or penalty clauses.

GameFi- In the emerging world of GameFi platforms players can purchase special items or earn rewards as they progress through the game. NFTs can represent a variety of in-game objects including tools, weaponry, avatars, vehicles and virtual lands. Non-custodial ownership gives players more control over their assets and has seen the emergence of secondary marketplaces for items.

Examples of blockchain games. Source: DappRadar

Many games require randomness to ensure fairness in the gaming mechanics. Games can achieve this through contracts accessing a tools such as Chainlink’s VRF (Verifiable Random Function). A random number generator that utilises cryptography to ensure publicly auditable data security.

No-Loss Prize Bonds- My-Chance is an innovative no-loss savings game. A DApp that pools users’ deposits to generate interest through over-collateralised lending markets. The accrued interest is used to generate prizes, which are distributed using Chainlinks VRF to select winners. Prizes also generate revenue for social causes, a win-win situation for participants. Additionally, users can withdraw their deposits at any time, without any loss. By utilising a transparent and verifiable source of randomness to select winners, participants can be sure of the reliability and fairness of the process.

Simplified Illustration of My-Chance Prize Bond System Implemented using smart contracts

Limitations of smart contracts

Smart contracts, while being an amazing tool, come with their limitations also.

One of the biggest limitations of smart contracts is that they are impossible to change, however, this is also their greatest strength. To implement changes to an application or platform, a new contract or new contracts must be deployed. While smart contracts aim to eliminate third-party involvement, there is still a need for developers to consult with third parties such as lawyers during the development process in order to understand the legal terms and translate them into code.

One of the inherent limitations of smart contracts was that they run on isolated networks. This led to the creation of hybrid smart contracts. Similar to a device with no internet, smart contracts are limited without real-world connectivity. Hybrid smart contracts use secure middleware known as an oracle. Oracles allow for the integration of network-based code and off-network infrastructure such as price data feeds for exchange mechanisms. Another example is a logistics-based smart contract using delivery notification to trigger a payment.

How to interact with a smart contract?

Using a browser-based wallet such as Core you can retain custody of your digital assets and items and interact with programs using both the front and back end of the program. To use a decentralised application you must pay a small network processing fee known as ‘gas’. On the Avalanche network, the processing fees are paid using AVAX. Many browser wallets allow you to purchase digital currency by card, or you can trade traditional currency through a centralised exchange and then send it to your wallet address.

Once your wallet has ‘gas in the tank’ interacting with decentralised programs is straightforward. You can then navigate to the chosen site’s homepage, and from there you can connect your wallet to the application. Once connected, select the action you wish to execute and then confirm that you wish to proceed. Once you confirm, the action is processed by the network and completed.

For example, if you wish to trade a stable currency for another digital asset:

  1. You access a reputable exchange.
  2. Connect your browser wallet.
  3. Select which asset from your wallet you wish to exchange and which asset you wish to receive.
  4. You confirm if you wish to proceed with the transaction.
  5. The transaction is processed and the asset you have given permission to exchange is traded.

Another straightforward example is the process of creating an NFT.

  1. Navigate through your browser to the chosen site.
  2. Connect your wallet and select which kind of NFT you wish to create. The creation process is known as minting.
  3. The program will ask for permission to access your wallet. Once permission is granted you select mint and the creation process is initiated.
  4. The designated assets are withdrawn from your wallet and the NFT is created and deposited to your wallet as the transaction is processed.

Even if the front end of an application is offline you can access the contract using a network explorer such as snowtrace. Here you can locate the application by inputting its address in the search bar. Once you have located the program, you can again connect your wallet and select the Write Contract function. The program itself is never offline and can always be interacted with.

Imagine you wish to withdraw your funds from a bank but their front end is not operational, with smart contracts you can go directly to the backend. It requires a little bit more understanding but it means you’re equipped to deal with situations in a way not available with web 2 programming/applications.

Conclusion

Smart contracts are a new technology, which is developing and evolving from day to day. People continue to develop more sophisticated and intelligent smart contracts. Web3, DApps and DeFi are in a continuing process of growth. While they may have started with some limitations, the potential of this technology to reshape how we conduct our lives is vast.

It must be noted that smart contracts can be created and added to a blockchain by anybody with the requisite knowledge. As such, one must always be sure they are aware of how a contract will perform. Competent developers will have their contracts audited by an independent body to ensure the code is sound and leaves no room for exploitation. Always do your own research.

--

--