Introduction To Solidity
Elegberun Olugbenga
Posted on June 7, 2021
In the previous article we discussed about smart contracts and tokens. In this article I will be taking a look at solidity the language for programming smart contracts. Solidity is a high-level programming language designed for implementing smart contracts. It is statically-typed object-oriented(contract-oriented) language. Solidity is highly influenced by Python, c++, and JavaScript which runs on the Ethereum Virtual Machine(EVM). In this article we will provide an introduction into the solidity language.
The first thing we need is an IDE to write our solidity code. One of the most popular development environments for programming solidity is the Remix IDE and it is what we will be using in this tutorial. Luckily we can access it online here.
.SOL
Solidity Files are saved with the .sol extension to indicate that it is a solidity file.
PRAGMA
- - The first line of a solidity file is the pragma statement. It indicates the solidity version that is being used. It helps ensure compatibility in code files.
pragma solidity ^0.8.2;
CONTRACT
This keyword is used to create a smart contract. By convention the name of the contract is usually the name of the solidity file. Every function and variable declaration in the file will be encapsulated within the smart contract.
contract Test{
Functions and Data
}
VARIABLES
variables are reserved memory locations to store value.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
examples of variables are-- integer,string,bool.
ADDRESS
This is a variable type that holds the 20 byte value representing the size of an Ethereum address.
address x = 0x212;
MAPPING -
A mapping holds a reference to a value. Below is the syntax. They act as hash tables which consist of key types and corresponding value type pairs.
mapping(_KeyType => _ValueType)
mapping(address => uint) public balances;
We can assign a value to a key adress like this
balances[keyAddress] = value;
Solidity supports. State,local and global variables.
- State Variables − Variables whose values are permanently stored in a contract storage.
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10; // Using State variable
}
}
Here we declared an integer variable called storedData. And we assign a value to it in the constructor of the contract.This value will be available throughout the contract context.
FUNCTION
A function is a group of resuable code that can be used anywhere in your application. They perform a specific task. The most common way to define a function in Solidity is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
Functions can be specified as being external, public, internal or private, where the default is public.
- Public: Public functions are part of the contract interface and can be either called internally or via messages.
- Internal: Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it).
Private: Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.
Functions can be declared view in which case they promise not to modify the state. Read only functions
function function-name(parameter-list) scope returns() {
//statements
}
Example
contract BlogDemo {
function addNumbers() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return result;
}
}
In this example we named our function addNumbers, it is declared as public view Which means it does not modify any contract state, It just adds two numbers together.It returns an integer and it does not take in any parameters.
- function with multiple return Parameters.
contract BlogDemo {
function addNumbers() public view returns(uint sum, uint product){
uint a = 1; // local variable
uint b = 2;
sum = a + b;
product = a * b;
}
}
This function will return both the product and sum.
- require keyword. The require keyword in a Solidity function guarantees validity of conditions that cannot be detected before execution. It checks inputs, contract state variables and return values from calls to external contracts. If I wanted to execute a function only if a particular condition is met, I add the required keyword.
contract BlogDemo {
uint value1 = 5;
uint value2 = 4;
function addNumbers() public view returns(uint sum, uint product){
require( value1 > value2 ,'5 is not greater than 4')
uint a = 1; // local variable
uint b = 2;
sum = a + b;
product = a * b;
}
}
}
This function will only execute if value1 is greater than value 2. if the condition is not met It will return the error message ('5 is not greater than 4').
Modifiers
Modifier allow control to the behaviour of a function. They can be used in a vareity of scenarios. Like for example checking who has access to a function before executing that function.
contract Test {
address testAddress;
constructor() {
testAddress = msg.sender;
}
// Check if the function is called by the owner of the contract
modifier onlyOwner() {
if (msg.sender == testAddress) {
_;
}
}
The function body is inserted where the special symbol "_;" appears in the definition of a modifier. So if condition of modifier is satisfied while calling this function, the function is executed and otherwise, an exception is thrown.
We can then use this function modifier as a condition checker in other functions. For example to only execute the function if it is called by the sender.
// Can only be called by the owner cause I am using the onlyOwner modifier
function test() public onlyOwner {
}
Constructors
A constructor is an optional function declared with the constructor keyword which is executed only upon contract creation. Constructor functions can be either public or internal. If there is no constructor, the contract will assume the default constructor.
contructor() public {}
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10; // Using State variable
}
}
Events.
An event stores arguments passed to it in the transaction logs of the blockchain. If you want to store something like transfer information. You could do so using an event.
Event Syntax
event Transfer(address indexed from, address indexed to, uint _value);
To write to an event. You emit that event. To write to event Transfer. I emit it using the following syntax.
//Emit an event
emit Transfer(msg.sender, receiverAddress, msg.value);
In this article we have explained some common syntax and terms in the solidity language. In the next article in the series we will be building our own smart contract using solidity and deploying to the Binance Smart Chain.
Follow me here and across my social media for more content like this Twitter Linkedin
Posted on June 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.