MoonPay logo
0 minsPublished on 12/12/2023

What are smart contracts and how do they work?

Learn the ins and outs of smart contracts, the revolutionary technology that's changing the way digital contracts function on the blockchain.

By Milly Fox-Jones

What are smart contracts blog banner

Smart contracts have emerged as a groundbreaking technology in Web3

These nifty lines of code revolutionize various aspects of decentralized applications (dApps) and enable seamless transactions of non-fungible tokens (NFTs)

But the power of smart contracts extends far beyond these use cases. 

This beginner’s guide unravels how smart contracts work and explains their transformative impact.

What are smart contracts?

A smart contract is a coded agreement designed to automatically execute a set of instructions, which will occur if and only if the proper conditions are met. These digital contracts are stored on the blockchain, making them secure, immutable, and trustless

Let’s start with the words themselves. Forget “smart” for a moment, and what you’re left with is “contract”. In its simplest form, a contract is an agreement between multiple parties. This agreement can be anything, but usually takes the form of some kind of crypto exchange

For example, a rental contract is an agreement whereby a tenant exchanges money to live in a property. An employment contract is when an employee exchanges his or her services for a steady paycheck. 

Contracts can come in different forms, but how does each party know the other will perform its end of the contract?. To address this question, often there is a trusted third party in the middle that ensures the parties fulfill their ends of the contract. 

For example, suppose you want to order food from MoonPizza. Now, this may not feel like a contract, but it is: you pay MoonPizza for food, which they deliver. You use an app, MoonFood, to get it. In this scenario, MoonFood is the trusted third party that ensures you pay and that MoonPizza delivers. 

While this setup works, it comes at a cost (literally): both you and MoonPizza may have to pay transaction fees for MoonFood’s services. This is where smart contracts come in.

Who created smart contracts?

Interestingly, the idea of smart contracts came about long before the blockchain.

The term “smart contract” was first coined in a 1994 paper by Nick Szabo, a computer scientist and cryptographer credited with developing one of Bitcoin's early predecessors, Bit Gold.

Szabo initially proposed that smart contracts be used for things like fraud reduction or enforcing contractual agreements. It was only later, in a 1996 paper, that he proposed they be used for cryptocurrencies.

An image of Nick Szabo, the first person to use the term "smart contract"
Nick Szabo first coined the term "smart contract" in a 1994 paper (Image source)

The smart contracts we use today, though, came about after the creation of Ethereum

Ethereum was the first programmable blockchain, meaning it introduced the concept of smart contracts to the world of dApps (decentralized applications)

Unlike traditional cryptocurrencies, Ethereum's programmable nature allows developers to create and deploy self-executing contracts that automatically execute predefined actions when specific conditions are met. 

This groundbreaking innovation opened up a whole new realm of possibilities, enabling the development of decentralized applications, tokenized digital assets, and decentralized finance (DeFi) protocols on a global scale for mainstream adoption.

How do smart contracts work?

Smart contracts use a mechanism known as ‘if this, then that’

To illustrate this, let’s go back to our earlier MoonPizza example. A smart contract could be set up that ensures if MoonPizza delivers you food, then a payment will be made. 

At the moment, MoonFood is the one making sure the contract is executed, but a smart contract automates this process and removes the need for MoonFood as a middleman.

But how? This is where blockchain comes in.

To fully understand smart contracts, you need to know a little bit about how blockchain technology works:

What is a blockchain?

A blockchain is a type of distributed ledger used to validate and record information digitally. Distributed ledgers are databases that are spread across multiple peer-to-peer devices. All of these devices can access the data and make updates or verifications in real time, almost like a shared document. 

Unlike a shared document, though, none of the information recorded on the blockchain can be changed or deleted.

How do smart contracts use blockchain technology?

So, an “if this, then that” is written as smart contract code on a blockchain. Then, the network of devices (nodes) on that blockchain carry out those instructions when their conditions have been met and confirmed. 

So, if the nodes get an alert that a MoonPizza delivery has arrived at your address, they will work to verify it and, if they do, the funds will be released. The blockchain is then updated to show that the contract has been completed and the smart contract is concluded and recorded.

Like all data on the blockchain, this record can’t be changed or removed once it’s been added, and it’s visible to everyone with permission to view that blockchain network. 

Smart contracts can be as simple or complicated as necessary, as long as they use an “if this, then that” mechanism.

What are the benefits of smart contracts?

Beyond the immutability afforded them by blockchain technology, there are many benefits of smart contracts. Here’s a quick run through of some of smart contracts’ main benefits. 

Accuracy, speed, and efficiency 

Since the conditions of smart contracts are written into code, the contract is automatically executed once these conditions are met. This, combined with the fact that smart contracts are entirely digital, removes any requirement for paperwork that needs to be manually processed. 

The result? A seamless, speedy, and error-proof smart contract that’s executed automatically.

Trust & transparency 

Using blockchain technology removes the need for a trusted third party to verify and validate data. Instead, a decentralized network of devices fulfills this role, only validating an action or transaction once the blockchain network nodes have confirmed it.

Once a smart contract has been completed, the information is then stored on the blockchain, visible to all. This means there can be no doubt that a party has edited or altered an agreement for their own benefit, because the whole record is there for everyone to see.

Security 

Again thanks to blockchain technology, it’s incredibly difficult to hack a smart contract. This is because all blockchain transaction records are encrypted, requiring an enormous amount of processing power to decrypt. 

A security lock
Smart contracts perform trustless transactions and enable greater security between two parties

Even if a party had that kind of processing power, the chronological nature of blockchain record storage means that it would have to alter the entire chain to amend a single record, and all of these changes would have to be validated by the other nodes in the network.

Cost saving

Smart contracts can save money. The acts of processing paperwork, verifying that certain actions have been taken, and contract execution are all costly. Smart contracts automate all these processes, removing the costs associated with them. 

What are the risks of smart contracts?

While smart contracts are self-executing and self-verifying, mitigating a lot of human error, humans still have to write the code that enables this, and humans can make mistakes.

Bugs

Poorly written code can feature bugs that pose two main risks. The first is that the smart contract could be rendered useless: if the code has bugs that cause it to malfunction in any way, the smart contract's execution will be at great risk and it won’t complete its job. 

The second risk is that the bugs can make a smart contract vulnerable to attacks, which can result in parties losing a lot of money.

Updates

Even the best computer code in the world can’t account for humans changing their minds. With a traditional contract, updates can be made to the original – it requires a bit of paperwork (the two parties must sign an agreement, for example), but it can be done.

This isn’t the case with a smart contract since the blockchain record is immutable. Instead, a whole new smart contract has to be written to interact with the original record in order to add or amend anything. 

While this certainly has its uses, it can be time consuming and can create a whole host of vulnerabilities (more code means more opportunities to make a mistake).

Legality

Since smart contracts are still fairly new, there is a lack of clear rules, regulation and governance. This creates a gray area for smart contracts. If something goes wrong, who is responsible?

This question could present a risk to the parties to a smart contract if something goes wrong. This is something that will hopefully become clearer with time, and some regulators like the ones in the EU are already taking steps to address certain issues.

Specificity

Since there’s no human involved in the smart contract execution process to interpret the data, the terms and definitions must be incredibly accurate in the smart contract code. If they are, the smart contract executes. If they aren’t, the smart contract either won’t execute, or will execute incorrectly – the consequences of this can be exceptionally damaging.

How can smart contracts be used?

Earlier we mentioned that smart contracts are mostly used for decentralized applications (dApps) and seamless transactions of non-fungible tokens (NFTs), but they have many more applications beyond Web3.

Insurance

A perfect example of how smart contracts can be used in familiar situations is insurance. Currently, there are a lot of intermediaries to go through to claim insurance. Let’s use flight delay insurance as an example.

Say you purchase flight delay insurance for your MoonAir flight through MoonSurance. The contract terms of this insurance policy state that if the flight is delayed by 90 minutes or more, you’ll get the cost of your flight back. 

You’re still at the airport at 3.30pm, but your flight was meant to take off at 2pm. Now, you have to contact MoonSurance, who will have to confirm the delay time with MoonAir (who may also have to provide evidence), and will then have to contact their finance provider to release the funds back to you. Boy, that’s a lot of steps. 

Now, let’s make it more efficient. Suppose smart contracts were used for the insurance payment instead. Same situation: your flight still hasn’t taken off by 3.30pm when it was meant to take off at 2pm. 

The smart contract could instead use oracles such as Chainlink, a mechanism that connects smart contracts to external data sources.

A diagram of how Chainlink process on- and off-chain data to support smart contracts
Chainlink is an oracle network that supports smart contracts with real-world data

Through oracles, smart contracts can be provided with external data such as flight times, that inform it of the flight’s delay.

Since the smart contract is set up with the mechanism if flight is delayed by 90 minutes or more, then release funds, the cost of your flight is automatically released to you before you’re even on the plane.

No intermediary parties involved and no waiting weeks for a refund. Just instant transactions that get you your funds quickly.

Voting

Another example is voting systems. Using smart contracts for an election means no party can manipulate the data—it’s all recorded, visible to everyone, and can’t be changed.

Decentralized autonomous organizations (DAOs) are a popular example in Web3 of how smart contracts can be used to decentralize the voting process via governance tokens.

Crowdfunding 

For any given crowdfunding project, there are three main parties: the project team, the facilitator (usually GoFundMe or Kickstarter), and the contributors. At the moment, Kickstarter works like this: a project has a goal and if that goal is reached, the funds are released to the project. 

If the goal is not reached, however, the funds are released back to the contributors and the project doesn’t get anything. All of this relies on Kickstarter as the middleman to complete the fund transfer and say whether or not a project’s goal has been reached.

With a smart contract, however, Kickstarter’s services wouldn’t be needed. Instead, a smart contract would be written that says “if X goal is reached, then release funds to the project. If X goal is not reached, then release funds to contributors”.

The future of smart contracts

Smart contracts have a wide range of applications that are already being adopted.

For example, video games company Ubisoft is using smart contracts to let users buy and sell NFTs. Dutch bank ING is using them as a trade-settlement system. Even the Swedish government trialed a blockchain-based land title registry using smart contracts for real estate transactions.

Smart contracts are poised to revolutionize industries across the board, unlocking vast potential for innovation and efficiency beyond our current imagination.

It's only a matter of time before the world embraces smart contracts to streamline processes and establish a new standard for trust, transparency, and security, all in seamless and automated transactions.

Start your crypto journey

Now that you've learned more about how smart contracts work, you may want to start exploring them for yourself.

Before you can use a smart contract, you'll first need to possess the necessary cryptocurrency to perform transactions on the relevant blockchain network. 

Many smart contract applications run on Ethereum, making it one of the most popular digital currency choices for smart contracts.

A screenshot of MoonPay's widget to buy Ethereum (ETH)
MoonPay makes it easy to buy and sell ETH

You can buy Ethereum via MoonPay or through any of our partner wallet applications to get started with Ethereum smart contracts. Just enter the amount of ETH you wish to purchase and follow the steps to complete your order.

MoonPay also makes it easy to sell Ethereum when you decide it's time to cash out. Simply enter the amount of ETH you'd like to sell and enter the details where you want to receive your funds.

Milly Fox-Jones
Written byMilly Fox-Jones

You might also like