Smart Contracts in Solidity
TCE Online Course - Blockchain 1
Session Outcomes
Compile and execute a simple solidity contract in Remix
Click icon to add picture
TCE Online Course – Blockchain 2
Smart Contracts
“A set of promises, specified in digital form, including protocols within
which the parties perform on the other promises.”
Computer programs Click icon to add picture
Immutable
Deterministic
EVM context
TCE Online Course – Blockchain 3
Solidity
Solidity is the most popular programming language used to write smart
contracts to run the Ethereum blockchain
It is a high level language which when
Click icon compiled gets converted to EVM
to add picture
(Ethereum Virtual Machine) byte code
TCE Online Course – Blockchain 4
First Contract – To add two numbers
pragma solidity ^0.5.0;
#include <stdio.h> contract Add
void main() {
{ int public a;
int a=10, b=20,c; int public b;
c=a+b; int public c;
print(“%d”, c); function mathop(int a, int b) public
} returns (int, int, int, int)
{
a=10; b=20; c=a+b;
return c;}
TCE Online Course – Blockchain
} 5
Compilation and Execution in Remix
Demonstration
TCE Online Course – Blockchain 6
Improvisations
Passing arguments to functions
Function Returning multiple values
TCE Online Course – Blockchain 7
Activity Time
• Extend the program to work for addition, subtraction,
multiplication for three Click
unsigned integers
icon to add picture
TCE Online Course – Blockchain 8
Data types and Operators in Solidity
TCE Online Course - Blockchain 9
Session Outcomes
Use various datatypes and operators in Solidity for writing simple
contracts
Click icon to add picture
TCE Online Course – Blockchain 10
Data types
TCE Online Course – Blockchain 11
Program to illustrate ‘Bool’
pragma solidity ^0.5.0;
contract Big
{
bool public c;
function CalcBig(int a, int b) public returns (bool)
{
Click icon to add picture
if (a>b)
{
c=true;
}
else
{
c=false;
}
return (c);
}
}
TCE Online Course – Blockchain 12
Data types - String
pragma solidity ^0.5.0;
contract Big
{
string public s;
function CalcBig(int a, int b) public returns (string memory)
{
if (a>b) Click icon to add picture
{
s="GREATER";
}
else
{
s="SMALLER";
}
return (s);
}
}
TCE Online Course – Blockchain 13
Data types - Address
pragma solidity ^0.6.8;
contract MyContract
{
address public owner;
constructor() public
{
owner=msg.sender; Click icon to add picture
}
function getOwner( ) public view returns (address)
{
return owner;
}
function getBalance( ) public view returns(uint256)
{
return owner.balance;
}
TCE }
Online Course – Blockchain 14
Activity Time
• Extend the program to work for finding the biggest of three
unsigned integers Click icon to add picture
TCE Online Course – Blockchain 15
Operators in Solidity
Arithmetic Operators (+,-,*,/, %, ++, --)
Comparison Operators (>, >=, <, <=,==,!=)
Click icon to add picture
Logical (or Relational) Operators (&&,||,!)
Assignment Operators(=,+=,-=,*=, /=, %=)
Conditional (or ternary) Operators (?:)
Bitwise Operators (&,|, ^, ~,<<,<<<)
TCE Online Course – Blockchain 16
Program to demonstrate Ternary Operators
pragma solidity ^0.5.0;
contract Big
{
bool public c; Click icon to add picture
function CalcBig(int a, int b) public returns (bool)
{
c=(a>b)?true:false ;
return (c);
}
}
TCE Online Course – Blockchain 17
Loops in Solidity
TCE Online Course - Blockchain 18
Session Outcomes
Use various looping constructs like while(), do…while() and for() in
solidity
Click icon to add picture
TCE Online Course – Blockchain 19
While loop
TCE Online Course – Blockchain 20
Do while Loop
Click icon to add picture
TCE Online Course – Blockchain 21
For Loop
for (initialization; test condition; iteration
statement)
{ Click icon to add picture
Statement(s) to be executed if test condition is true
}
TCE Online Course – Blockchain 22
Demonstration of do while loop
pragma solidity ^0.5.0;
contract sumofseries
{
int public sum;
function Calc(int n) public returns (int)
{ Click icon to add picture
sum=0;
int i=0;
do{
sum=sum + i;
i=i+1;
}while (i<=n);
return sum;
}
}
TCE Online Course – Blockchain 23
Activity Time
• Modify the sum of series program with “for” and “while”
loops. Click icon to add picture
TCE Online Course – Blockchain 24
Decision Making in Solidity
TCE Online Course - Blockchain 25
Session Outcomes
Use simple if, “if else”, “if else if” for decision making in solidity contracts
Click icon to add picture
TCE Online Course – Blockchain 26
Simple if
if (expression)
{
Statement(s) to be executed if expression is true
}
if (a>b)
{
c=true;
}
TCE Online Course – Blockchain 27
If else
if (expression)
{
Statement(s) to be executed if expression is true
}
else
{
Statement(s) to be executed if expression isClick
falseicon to add picture
}
if (a>b)
{
c=true;
}
else
{
c=false;
}
TCE Online Course – Blockchain 28
if else if
if (expression 1) {
Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true
Click icon to add picture
} else {
Statement(s) to be executed if no expression is
true
}
if( a > b && a > c) {
big = a;
} else if( b > a && b > c ){
big = b;
} else {
big = c;
}
TCE Online Course – Blockchain 29
Activity Time
• Write a contract to display the grade in a subject based on the
obtained score. Click icon to add picture
TCE Online Course – Blockchain 30
Arrays in Solidity
TCE Online Course - Blockchain 31
Session Outcomes
Use arrays for writing simple contracts in solidity
Click icon to add picture
TCE Online Course – Blockchain 32
Introduction to Arrays
• An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.
• Instead of declaring individual variables, such as number0, number1, ..., and
number99, we declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables.
• A specific element in an array is accessed by an index.
TCE Online Course – Blockchain 33
Declaration and Initialization of Arrays
• type arrayName [ arraySize ];
• int a[10];
• type[] arrayName;
• uint[] arr;
• uint arr[] = [1, 2, 3];
• uint size = 3;
• uint stud[] = new uint[](size);
TCE Online Course – Blockchain 34
Static Array
pragma solidity ^0.5.0; return sum;
contract array }
{ }
uint[] public arr = [12,13,17,4,50];
uint public sum;
function sumofarr() public returns (uint)
Click icon to add picture
{
sum=0;
uint i=0;
do{
sum = sum + arr[i];
i=i+1;
}while (i<=4);
TCE Online Course – Blockchain 35
Dynamic Array
pragma solidity ^0.6.8; // Function to return length of dynamic array
contract DynamicArray{ function getLength() public view returns (uint)
{
// Declaring state variable return arr.length;
int[] arr; }
// Function to add data in dynamic array // Function
Click icon to add picture to return sum of elements of dynamic array
function addData(int num) public function getSum() public view returns(int)
{ {
arr.push(num); uint i;
} int sum = 0;
// Function to get data of dynamic array for(i = 0; i < arr.length; i++)
function getData() public returns(int[] memory) sum = sum + arr[i];
{ return sum;
return arr; }
} }
TCE Online Course – Blockchain 36
Dynamic Array - Output
TCE Online Course – Blockchain 37
Activity Time
• Write a smart contract to find the biggest and smallest
element in an array using static and
Click icon to add picture
dynamic arrays
TCE Online Course – Blockchain 38