What is a smart contract?
Web3 applications exist because of smart contracts. Smart contracts are small programmes that run on a blockchain network.
You can think of a smart contract as a program that automatically runs when certain conditions are met. For example, when a user buys an NFT (such as a piece of digital art) from an NFT marketplace, a smart contract automatically transfers the NFT to the user's wallet when the funds are received by the NFT's creator. The creator does not have to manually handle the transfer.
Smart contracts cannot be modified, stopped, or taken control of after being deployed making them great for building applications that require independence.
What can smart contracts do?
Smart contracts can be used in a variety of applications. For example:
-
Vehicle registration: Smart contracts can record the transfer of titles between a car seller and a car buyer. The California DMV — the state agency in California, USA, that registers motor vehicles and boats — is currently experimenting with recording titles in this way.
-
Escrow service: Smart contracts can act as a third-party in a financial transaction holding assets in escrow and allowing transactions to occur within two parties without the need for a middleman. For example, imagine you wanted to buy a piece of Tesla stock from a digital app but you do not want to pay upfront before getting the stock. Your payment for the stock can be held in an escrow and released to the seller once the smart contract has been notified that you have receieved your stock/share certificate.
-
Gaming: Smart contracts can also be used to manage in-game digital tokens on the blockchain. While playing games like Dogami, where users can earn tokens for completing tasks, these tokens can be stored on the blockchain. The stored tokens can be traded, purchased or simply used within the game. All of these activities can be managed using smart contracts.
-
Governance: Smart contracts can be used to manage voting processes, treasury funds and be used to enforce rules and regulations governing a group of people or an organization.
-
Supply chain management: Smart contracts can be used to track the movement of goods and materials across the supply chain which improves traceability. They can automate tasks like order fulfillment, payment processing and data sharing.
Smart contracts cannot be controlled by an external agent. They also cannot be modified making them the perfect intermediaries for transactions of this nature.
Smart contract accounts vs User accounts on Tezos
The Tezos blockchain allows two types of accounts: user accounts and smart contract accounts. The addresses for user accounts begin with tz1...
while the addresses for smart contract accounts begin with KT1...
Smart contract example on Tezos
Let's look at a simple example of a smart contract on Tezos. Remember that you have multiple language options for Tezos smart contracts. This example uses the SmartPy library for Python, so it uses Python syntax:
import SmartPy as sp
@sp.module
def main():
class StoreGreeting(sp.Contract):
def __init__(self):
self.data.value = "Starter greeting value."
@sp.entrypoint
def replace(self, params):
self.data.greeting = params.text
@sp.entrypoint
def append(self, params):
self.data.greeting += params.text
This contract, taken from the tutorial Deploy a smart contract with SmartPy, stores a text string on the blockchain and provides entrypoints to change it. These entrypoints are like functions or API endpoints because they allow users to run the smart contract's code.
In this case, the text string starts as "Starter greeting value." Users can call the replace
entrypoint and pass a string to replace the current value. They can also call the append
method and pass a string to add to the existing string.
Tezos automatically stores this string and the smart contract code on the blockchain and prevents anyone from changing it in any way other than by using the entrypoints. In this way, you can control how data and code is stored and managed without having to worry about people changing it in other ways.
In the next part, we'll look at how to build and deploy your own contract to the Tezos blockchain.
Quiz: Smart contracts
Smart contracts are programmes that run on the blockchain.
A: True B: False
The author of a smart contract can make changes to the contract after deploying it to the blockchain.
A: True B: False
Which of the following operations can NOT be performed by a smart contract?
A: Transfer the ownership of an NFT B: Transfer funds from own account to another C: Store a user's private keys D: Manage tokens within different applications
Entrypoints within a smart contract written in SmartPy are denoted with which of the following decorators?
A: @entrypoint B: @entrypoints C: @decorator D: @contract