What Are Smart Contracts and How Do They Work? 

Learn all about smart contracts on Ethereum and other blockchains, how they work, and why they matter for crypto.

Jan 10, 2023
|

Deep Dive Smart Contracts Opt

Introduction to Smart Contracts

The term ‘smart contract’ was first introduced by computer scientist and cryptographer Nick Szabo in the 1990s. In his publication, Szabo defined smart contract as “a set of promises, specified in digital form, including protocols within which the parties perform on these promises.”

Smart contracts are fundamental building blocks of the blockchain and crypto space. As blockchains gained in popularity, smart contracts began to flourish — especially with the emergence of Ethereum, one of the most popular blockchains that supports smart contracts. 

Key Takeaways:

  • A smart contract is a self-executing computer programme that runs on a blockchain. When the rules of a smart contract are met for a transaction, the digital contract executes that transaction.
  • Some potential advantages of smart contracts include automation, transparency, and security, amongst others.
  • Risks of smart contracts may include weak regulation and potential vulnerability.
  • There are endless real-world use cases of smart contracts today, including finance, supply chain, and more.

How Smart Contracts Work

In the blockchain space, a smart contract is a self-executing computer programme that runs on a blockchain. This type of contract is intended to automatically execute and control events and actions according to the specified terms of the code. This code contains a set of rules and data that follows the logic ‘if/when x happens, then execute y’, wherein x is an event and y is an action. It’s like a regular application that implements some business rules; only it uses a blockchain as an execution environment. An analogy we can use is how a vending machine would work: With the right inputs, a certain output is guaranteed. 

Smart Contract Illustration

In simple terms, we can look at smart contracts as a digitalised way of executing contracts in the blockchain. Similar to traditional digital agreements, smart contracts involve two or more contracting parties and are technically legally enforceable. However, while there are some parallels that can be drawn between traditional and smart contracts, they are not the same. Smart contracts as a tool can help bring us closer to a more decentralised world, and they are not intended to replace standard agreements.

Ethereum Smart Contracts

The diagram below depicts how a smart contract system works, taking the Ethereum blockchain as an example. Each contract is identified by a unique address. The terms of the contract are written in code; once the contract is deployed onto the blockchain, the contract code cannot be changed. 

Smart contracts are executed by the blockchain nodes as a result of processing transactions that are submitted by a user (e.g., performing a token swap from ETH to CRO). Blockchain nodes are devices, such as computers, that authorise and validate transactions, and act as a communication hub with other nodes in the network.

These transactions are then executed by every blockchain node in the network to verify their outputs. If verified by all nodes, the contract’s state is then updated accordingly. The transactions are automated and self-executed through the code, which ideally, if all runs according to plan, removes the need for a third party to execute the contract.

Based on the transaction it receives, the contract can perform various functions, including reading or writing to its private storage, storing money into its account balance, sending or receiving messages or money to or from users or other contracts, or even creating new contracts.

Smart Contract Flow

Anatomy of Smart Contracts

A smart contract is a programme consisting of data and functions that can auto-execute upon receiving a transaction request on a blockchain. Here’s an overview of what makes up a smart contract in the Ethereum blockchain.

Data

Any contract data must be assigned to a location, either to storage or memory. The data can be the number of tokens, outputs of functions, and so on. It’s costly to modify storage in a smart contract, so a developer needs to consider where the corresponding data should live. 

Functions

Functions define actions to perform or the common business logic. In the most simplistic terms, functions can get or set information in response to incoming transactions. There are two different types of function calls: internal and external. Internal functions and state variables can only be accessed internally (i.e., from within the current contract or the contracts deriving from it). External functions are part of the contract interface, which means they can be called from other contracts and via transactions.

Events

Events inform services outside of the blockchain, such as decentralised apps (dapps), to notify users of the current state of the contract or an event that happened. Events are similar to functions, wherein they also accept and store arguments; but event data are saved in the transaction’s log, which are inaccessible to smart contracts.

Events can be called or dispatched via an ‘emit’ keyword. Although events and logs are technically part of smart contracts, it is important to note that smart contracts cannot read event data.

Advantages and Disadvantages of Smart Contracts

Now that the basics are covered, let’s look at the benefits and limitations of smart contracts.

Advantages

  • Automation: Smart contracts work autonomously.
  • Transparency: Actions in smart contracts are publicly visible insofar as they occur on-chain.
  • Security: Transaction records stored on the blockchain are encrypted and immutable.
  • Accuracy: All information regarding the contract is expressed in a conditional format using the ‘if-then’ statements, and the business logics are performed by machines, which can also prevent some mistakes that might otherwise arise from manual work.
  • Speed and efficiency: Smart contracts run on the Internet with fast execution speed and no intermediaries.
  • Lower cost: Executing smart contracts may involve lower cost than executing traditional contracts manually, in addition to avoiding time delays and associated fees (though, this is as yet relatively untested).

Disadvantages

  • Weak regulation: There are no clear regulations regarding smart contracts.
  • Immutable: Once deployed, the original smart contracts cannot be adjusted, even though they might not work as intended.
  • Potential vulnerability: Smart contracts may lead to a risk of hacking or exploits due to bugs, vulnerabilities, or oversights in the code.

Applications of Smart Contracts

Smart contracts have become the fundamental building blocks of blockchain applications today, and below are some examples of these applications.

Decentralised Finance (DeFi)

The crypto sector is in many ways reliant on smart contracts. One good example is decentralised finance (DeFi), where booming numbers of smart contract applications have been seen since 2020. DeFi uses smart contracts to implement various traditional financial services — lending and borrowing, exchanges, derivatives, insurance, etc. — so as to enjoy the benefits of a non-intermediated, trustless, and cost-efficient economy.

The use cases for smart contracts in the financial sector are not limited to DeFi. There are many other scenarios where smart contracts can shine, such as trading, settlement, etc.

Digital Identity

Smart contracts enable self-sovereign identity (SSI), a key digital concept in Web3 that enables users to control the information they use to prove their identity to websites and online applications. SSI uses smart contracts to provide a seamless, user-centred Internet for individuals, who own and control their personal data (the counterparties do not need to hold users’ sensitive data to verify transactions). This further streamlines processes by facilitating frictionless ‘know your customer’ (KYC) procedures.

Supply Chain

Supply chain visibility could be enhanced with the help of smart contracts — providing goods tracking across brands, retailers, logistics, and counterparties.

Insurance Claims 

Another often-cited potential use case for smart contracts is in the insurance space, which could one day help to prevent or reduce insurance fraud by making claims verification and management easier for customers while providing efficient reinsurance systems. 

Real Estate 

Smart contracts may find good use cases in real estate, as they can help facilitate reliable transactions that require trust and transparency from all parties involved. Smart contracts might come into play by speeding up the property ownership change process, streamlining the facilitation of rental and lease agreements, and ensuring secure peer-to-peer transactions overall.

Conclusion — Are Smart Contracts Essential for Web3?

While smart contracts can be quite nuanced, they are an innovation worth learning about. However, when interacting with smart contracts, make sure to use their corresponding user interfaces to complete transactions unless you have the technical knowledge to interact with the smart contract directly. Otherwise, without the right knowledge, one possible mistake would be sending funds to the contract address directly, which would not trigger the contract’s functions, and the funds might be lost — forever!

Due Diligence and Do Your Own Research

All examples listed in this article are for informational purposes only. You should not construe any such information or other material as legal, tax, investment, financial, cyber-security, or other advice. Nothing contained herein shall constitute a solicitation, recommendation, endorsement, or offer by Crypto.com to invest, buy, or sell any coins, tokens, or other crypto assets. Returns on the buying and selling of crypto assets may be subject to tax, including capital gains tax, in your jurisdiction. Any descriptions of Crypto.com products or features are merely for illustrative purposes and do not constitute an endorsement, invitation, or solicitation. 

Past performance is not a guarantee or predictor of future performance. The value of crypto assets can increase or decrease, and you could lose all or a substantial amount of your purchase price. When assessing a crypto asset, it’s essential for you to do your research and due diligence to make the best possible judgement, as any purchases shall be your sole responsibility.

Tags

smart contracts

Share with Friends

Ready to start your crypto journey?

Get your step-by-step guide to setting up an account with Crypto.com

By clicking the Get Started button you acknowledge having read the Privacy Notice of Crypto.com where we explain how we use and protect your personal data.
Mobile phone screen displaying total balance with Crypto.com App

Common Keywords: 

Ethereum / Dogecoin / Dapp / Tokens