All Articles

Basics of a Smart Contract

Smart Contracts

Smart contracts are pieces of code that execute an action when certain conditions are met. 

Smart contracts are a piece of code that does something if something happens.

Ethereum smart contracts are written in Solidity, a computer language comparable to JavaScript. The developer of a smart contract defines the conditions under which the contract will be executed and the actions that will be performed.

Interacting with a smart contract

Smart contracts are a type of Ethereum account. This means that they have a balance and can be the subject of transactions. However, they are not controlled by the user, instead they are hosted on the network and executed according to code. User accounts can interact with a smart contract by sending transactions that fulfill a function in the smart contract.

Smart Contract Deployment

Deploying a smart contract is technically a transaction, so you need to pay for gas just like you need to pay for gas for a simple ETH transfer. However, the gas costs for deploying contracts are much higher.

Deletion

By default, smart contracts cannot be deleted and interactions with them are irreversible.

Main Elements of a Smart Contract

Main Elements of a Smart Contract:

  • Address Each smart contract has a unique address on the blockchain through which it can be accessed.
  • Functions The code that describes the actions performed by the contract, such as sending money or recording data. Functions are the actions a smart contract can perform. Imagine functions as buttons on a remote control that perform specific tasks when pressed.Set Function Like a button that sets a value. If you press this button and say "set value to 10," the contract will remember the number 10.Get Function Like a button that shows the current value. If you press this button, the contract will show you the currently stored number.
  • State Variables Data stored on the blockchain that can be modified by the smart contract's functions. State Variables are the data that the smart contract stores internally. They are like shelves in a closet where the contract stores information. This data remains in the contract even after it has finished running and can be modified by functions.
  • Events A mechanism for logging and tracking actions within the smart contract. Events are how a smart contract communicates that something has happened. Imagine this as notifications on your phone. When something important occurs, the smart contract creates an event to notify others. For example, if someone sends money, the contract may create an event so others can know about the transfer.
Key Concepts
  • Compiler Compiler is a program that translates code written in one programming language into another language that a computer or blockchain can understand and execute.
    In the case of smart contracts, you write code in the Solidity language, and the compiler translates it into bytecode that the Ethereum blockchain understands.
  • ABI (Application Binary Interface) ABI is a manual or guide for using a smart contract. It describes what functions are available in the contract, what data they accept, and what data they return. It's similar to a restaurant menu where the available dishes and their ingredients are listed.
  • EVM (Ethereum Virtual Machine) EVM is a special virtual machine that runs smart contract code on the Ethereum blockchain. It is like a small computer inside the blockchain that runs and executes programs (smart contracts). It ensures that all contracts are executed in the same way on all nodes in the network.
  • Transaction Transaction is action sent to the blockchain that changes its state. It's like a letter you send with instructions on what to do. For example, you can send a transaction to transfer money, call a smart contract function, or record data. When a transaction is sent, it is verified and added to the blockchain, where it can no longer be altered. This ensures that all actions are transparent and cannot be forged. When you call set or get function, you are sending a transaction to the blockchain with instructions on what to do.
Smart contract code

In the smart contract code, a data storage structure and a set of methods for working with it are described, as well as many other things that we'll figure out along the way.

So, here's a simple and clear contract:

What we have, in order from top to bottom.

The comment in the code is marked with “//”. The beginning of the code contains the name of the file in the comments. It's done for nothing.

Pragma 

Next comes the command to the compiler “pragma solidity ^0.4.18”. This is the language and version of the language in which the contract is written. Literally, it tells the compiler that the code should be translated from the solidity language with version strictly 0.4.x.

Pragma is like a note or instruction to the compiler, telling it how to handle your code. These instructions may include notes about language version, special settings, or optimizations.

Contract body

When you write a smart contract, you need to declare some variables that will store important data in your contract. These variables tell the computer what information to remember.

A variable is like a box that you can put something in to use later.

In this case, we have a box (variable) called age. The data type specifies what can be put in this box.

In this example, the uint (unsigned integer) data type is an unsigned integer. This means that the number cannot be negative.

We declare a variable age, which will store age. We write it like this: uint age. This means that we have a box called age into which we can put an integer, and this number cannot be negative (for example, 25, but not -5).

uint is an abbreviation for uint256.
256 is the size of the number in bits (units of information). The larger the number, the more space you need to store it.

In this case, uint and uint256 are the same thing.

Number Sizes

In Solidity (a programming language for smart contracts), you can use different sizes of numbers: from 8 to 256 bits, in 8-bit increments.

For example, uint8 is an unsigned integer of 8 bits, and uint256 is an unsigned integer of 256 bits. The larger the bits, the larger the number can be stored.

Signed and unsigned numbers

int8 is a signed number of 8 bits. It can be positive or negative (e.g. -128 to 127).
uint8 is an unsigned number of 8 bits. It can be positive only (from 0 to 255).

So we have uint age This tells us:

  • We have a box called age.
  • Only integers can be put in this box.
  • These numbers can only be positive integers.
  • This number can be very large because uint is uint256.
Methods

We are approaching the logic of a smart contract. In this example, these are two methods for working with the state of the age variable: getAge (get age) and setAge (set new value).

A method is like a command or action that your code can execute.

Methods start with function (this word tells the computer that a method (command) is about to be described). getAge is the name of the method. The name helps you understand what the method does. In this case, the method is called getAge, which means “get age”.

The parentheses after the method name indicate the arguments (additional details of the command) that the method accepts. In this example, the parentheses are empty (), which means that this method does not accept any arguments.

After getAge() comes the word returns(uint). This indicates that the method returns some value, and in this case it will be an integer without the (uint) sign.

Everything written inside the curly braces {} is the body of the method. This specifies what exactly the method does.

return age - this line says that the method returns the value of the age variable.

Method getAge

  • Function: Tells you what the command is about to be.
  • getAge(): The name of the command with no additional details.
  • returns (uint): Says that the command will return a positive integer. 
  • { return age; }: Says that the method returns the value of the variable age.

Method setAge

  • function: Says that a command is about to be issued.
  • setAge: The name of the command saying “set age”.
  • (uint newAge): The command takes a single positive integer and names it newAge.
  • { age = newAge; }: Says what to do: take newAge and assign it to the age variable.
Access modifiers

When we create methods (commands) or variables (data boxes) in code, we can specify who can use them and from where.

Default: If you don't specify anything else, methods (commands) are considered public. This means that they can be used by anyone.

Scope is exactly how you specify who can see and use a method (command) or variable (data box). There are special words for this: external, internal, public, and private.

Types of visibility

Types of visibility

  • Public Anyone can call a method or see the value of a variable.
  • Private Only code inside this contract can call this method and get the value of a variable.
  • External Only other contracts or transactions can call the method, the smart contract itself cannot.
  • Internal Only this smart contract and its successors can use a smart contract method or variable.

The inheritors are those that copy the functionality of the smart contract.

In code, there is a modifier public and constant at methods.
public means that anyone can call the methods to find out the age and
to set a new age.

In programming, when we add the word constant to a method, it means that this method does not modify the data. It only reads it.

  • function setAge without constant This is a method that can change the data.
  • function getAge with constant This is like a method that only reads the data but does not modify it.

We have broken down the code of the simplest smart contract. Now let's move on to the deployment of the smart contract. For this purpose, let's parse the smart contract of a token and the code of the token's deployment.

Start work

One click to start work with us

Same Articles

All Articles