1
Programming in Solidity
Blockchain Technologies
Lecture # 06
Introduction
2
Smart Contract
3
4
A Simple Smart Contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.7.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
5
6
Importing files
7
8
Compiling a contract using Remix IDE
pragma solidity ^0.5.0;
contract SolidityTest {
constructor() public{
}
function getResult() public view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
9
Steps
10
Structure of a contract
11
Variables
12
Types of variables
13
State Variables
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
// Using State variable
}
}
14
Local Variables
15
Example
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return result; //access the local variable
}
}
16
Global variables
17
18
Scope of variable
19
Operators
20
Strings
pragma solidity ^0.5.0;
contract SolidityTest {
bytes32 data = "test";
}
21
Loops
while (expression) {
Statement(s) to be executed if expression is true
}
do {
Statement(s) to be executed;
} while (expression);
22
Loops – contd…
for (initialization; test condition; iteration statement) {
Statement(s) to be executed if test condition is true
}
23
Conditions
if (expression) {
Statement(s) to be executed if expression is true
}
24
Arrays
25
Address
26
Example
address x = 0x212;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10)
x.transfer(10);
27
Functions
function bid() public payable { // Function
// ...
}
28
Struct
struct Voter { // Struct
uint weight;
bool voted;
address delegate;
uint vote;
}
29
Example
pragma solidity ^0.5.0;
contract test {
struct Book {
string title;
string author;
uint book_id;
}
Book book;
function setBook() public {
book = Book('Learn Java', 'TP', 1);
}
function getBookId() public view returns (uint) {
return book.book_id;
}
}
30
Enum types
enum State { Created, Locked, Inactive } // Enum
31
Example
pragma solidity ^0.5.0;
contract test {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize choice;
FreshJuiceSize constant defaultChoice = FreshJuiceSize.MEDIUM;
function setLarge() public {
choice = FreshJuiceSize.LARGE;
}
function getChoice() public view returns (FreshJuiceSize) {
return choice;
}
function getDefaultChoice() public pure returns (uint) {
return uint(defaultChoice);
}
}
32
Mapping
33
Example
pragma solidity ^0.5.0;
contract LedgerBalance {
mapping(address => uint) public balances;
function updateBalance(uint newBalance) public {
balances[msg.sender] = newBalance;
}
}
contract Updater {
function updateBalance() public returns (uint) {
LedgerBalance ledgerBalance = new LedgerBalance();
ledgerBalance.updateBalance(10);
return ledgerBalance.balances(address(this));
}
}
34
Ether units
35
Time units
36
Require
function requireStatement(uint _input) public view returns(string memory){
require(_input >= 0, "invalid uint8");
require(_input <= 255, "invalid uint8");
return "Input is Uint8";
}
37
Revert
if (sum < 0 || sum > 255) {
revert(" Overflow Exist");
} else {
return ("No Overflow", sum);
}
38
Constructor
constructor() <Access Modifier> {
}
39
Inheritance
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining contract
contract parent {
uint256 internal sum;
function setValue() external {
uint256 a = 10;
uint256 b = 20;
sum = a + b;
}
}
40
// Defining child contract
contract child is parent {
function getValue() external view returns (uint256) {
return sum;
}
}
// Defining calling contract
contract caller {
child cc = new child();
// Defining function to call setValue and getValue functions
function testInheritance() public returns (uint256) {
cc.setValue();
return cc.getValue();
}
}
41
Events
event HighestBidIncreased(address bidder, uint amount); // Event
42
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract MyContract {
event transaction(address payable, uint256);
function sendEther(address payable to) public payable {
to.transfer(msg.value);
emit transaction(to,msg.value);
}
}
43
Library
pragma solidity ^0.5.0;
library libraryName {
// struct, enum or constant variable declaration
// function definition with body
}
44
Example
pragma solidity ^0.8.7;
library MathLib {
struct MathConstant {
uint Pi; // π (Pi) ≈ 3.1415926535...
uint Phi; // Golden ratio ≈ 1.6180339887...
uint Tau; // Tau (2pi) ≈ 6.283185...
uint Omega; // Ω (Omega) ≈ 0.5671432904...
uint ImaginaryUnit; // i (Imaginary Unit) = √–1
uint EulerNb; // Euler number ≈ 2.7182818284590452...
uint PythagoraConst; // Pythagora constant (√2) ≈ 1.41421...
uint TheodorusConst; // Theodorus constant (√3) ≈ 1.73205...
}
function multiply(uint a, uint b) public view returns (uint, address) {
return (a * b, address(this));
}
}
45