Understanding Smart Contracts: A Simple Example

By Danielle

Imagine you're at a concert, and you've just bought a t-shirt. You hand over $20, and the vendor gives you a t-shirt and a receipt. This is a simple contract - an agreement between two parties, with clear terms and conditions. Now, let's explore how this simple contract can be represented in the blockchain world using smart contracts.

Global Variables in Solidity Smart Contracts - 1
Global Variables in Solidity Smart Contracts - 1

Smart contracts are self-executing contracts with the terms of the agreement directly written into lines of code. They automate the execution of an agreement as soon as predefined conditions are met. Let's dive into a simple example to understand how they work.

Contract
Contract

Smart Contract Basics

A smart contract typically consists of three main parts: variables, functions, and events. Variables store data, functions perform actions, and events log activities on the blockchain.

a form of agreement for an employee to work on the company's business plan
a form of agreement for an employee to work on the company's business plan

To understand these components, let's consider a simple smart contract for a vending machine that dispenses t-shirts. This contract will have variables for the price of the t-shirt, the number of t-shirts available, and the address of the contract owner. It will also have functions to buy a t-shirt and to update the t-shirt inventory.

Variables in Smart Contracts

a simple rental agreement form is shown in this image, it shows the company's name
a simple rental agreement form is shown in this image, it shows the company's name

Variables in smart contracts are used to store data. In our vending machine example, we might have variables like:

  • uint public price; - Stores the price of a t-shirt (in wei, the smallest unit of Ether).
  • uint public availableTShirts; - Stores the number of t-shirts available in the vending machine.
  • address public owner; - Stores the address of the contract owner.

Functions in Smart Contracts

the general contract agreement is shown in this document
the general contract agreement is shown in this document

Functions in smart contracts perform actions. Here are some functions for our vending machine:

  • function buyTShirt() public payable; - This function allows anyone to buy a t-shirt by sending the required amount of Ether. It will decrease the available t-shirts and send the purchased t-shirt to the buyer's address.
  • function updateInventory(uint _amount) external; - This function allows the contract owner to update the t-shirt inventory. It will increase the available t-shirts by the specified amount.

Deploying and Interacting with Smart Contracts

a printable form for a business agreement that is intended to be used as a template
a printable form for a business agreement that is intended to be used as a template

Smart contracts are deployed on the blockchain, typically on the Ethereum network. Once deployed, they can be interacted with using transactions. For example, to buy a t-shirt from our vending machine, a user would send a transaction with the required amount of Ether to the contract's address, invoking the buyTShirt function.

To update the t-shirt inventory, the contract owner would send a transaction with the updateInventory function, specifying the number of t-shirts to add.

an image of a signed document with the words contrat on it
an image of a signed document with the words contrat on it
Free Investment Contract Templates, Editable and Printable
Free Investment Contract Templates, Editable and Printable
Smart Contract Coding for Beginners
Smart Contract Coding for Beginners
a simple contract form is shown in this image
a simple contract form is shown in this image
Simple Contract Template
Simple Contract Template
Role of AI in Smart Contract Security
Role of AI in Smart Contract Security
a simple contract form is shown in this image
a simple contract form is shown in this image
Service Agreement Template Word Inspirational 8 9 Transport Agreement format In Word
Service Agreement Template Word Inspirational 8 9 Transport Agreement format In Word
📄 Simple Business Contract Template for Your Needs
📄 Simple Business Contract Template for Your Needs
Editable Investment Agreement Template DOC & PDF, Business Investment Contract, Legal Form Download
Editable Investment Agreement Template DOC & PDF, Business Investment Contract, Legal Form Download
41 Simple Construction Contract Templates [100% Free]
41 Simple Construction Contract Templates [100% Free]
Employment Contract
Employment Contract
the employment agreement is shown in this document
the employment agreement is shown in this document
a simple agreement for an individual party
a simple agreement for an individual party
the contract form is shown in blue and white
the contract form is shown in blue and white
Free Contract & Letter Templates for Every Need – 4allcontracts.om
Free Contract & Letter Templates for Every Need – 4allcontracts.om
FREE 10+ Temporary Employment Contract Samples in PDF | MS Word | Google Docs | Pages
FREE 10+ Temporary Employment Contract Samples in PDF | MS Word | Google Docs | Pages
the sample agreement for a company is shown in this document
the sample agreement for a company is shown in this document
Service Contract
Service Contract
Letter of employment contract sample
Letter of employment contract sample

Events in Smart Contracts

Events in smart contracts log activities on the blockchain. They can be used to trigger off-chain actions, like sending notifications or updating a database. Here's an event for our vending machine:

  • event TShirtBought(address indexed buyer, uint amount); - This event logs when a t-shirt is bought. It can be used to trigger a notification to the buyer or to update an off-chain inventory system.

In the rapidly evolving world of blockchain, smart contracts are transforming how we think about agreements and transactions. From simple vending machines to complex financial instruments, smart contracts are revolutionizing industries by enabling trustless, transparent, and automated agreements. As we continue to explore and understand their potential, we're likely to see even more innovative use cases emerge in the future.