Bitcoin Essentials 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

University Institute of Engineering

DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
Bachelor of Engineering (Computer Science &
Engineering)
Value Added Course-Blockchain Essentials and
Development of DApps
DAY 4
Prepared By :Er. Jaideep Kaur(E12885)
DISCOVER . LEARN . EMPOWER
Solidity Basics

1.2
Structure of Solidity Smart Contract

• Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations
of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. Furthermore,
contracts can inherit from other contracts.

• There are also special kinds of contracts called libraries and interfaces.

1.3
Solidity Types
Solidity is a statically typed language, which implies that the type of each of the variables should be specified. Data types allow the
compiler to check the correct usage of the variables. The declared types have some default values called Zero-State, for example for bool
the default value is False. Likewise other statically typed languages Solidity has Value types and Reference types which are defined
below:
Value Types
Value-type variables store their own data. These are the basic data types provided by solidity. These types of variables are always passed
by value. The variables are copied wherever they are used in function arguments or assignments. Value type data types in solidity are
listed below:
• Boolean: This data type accepts only two values True or False.
• Integer: This data type is used to store integer values, int and uint are used to declare signed and unsigned integers respectively.
• Fixed Point Numbers: These data types are not fully supported in solidity yet, as per the Solidity documentation. They can be declared
as fixed and unfixed for signed and unsigned fixed-point numbers of varying sizes respectively.
• Address: Address hold a 20-byte value which represents the size of an Ethereum address. An address can be used to get balance or to
transfer a balance by balance and transfer method respectively.
• Bytes: Although bytes are similar to strings, there are some differences between them. bytes used to store a fixed-sized character set
while the string is used to store the character set equal to or more than a byte. The length of bytes is from 1 to 32, while the string has a
dynamic length. Byte has the advantage that it uses less gas, so better to use when we know the length of data.
• Enums: It is used to create user-defined data types, used to assign a name to an integral constant which makes the contract more
readable, maintainable, and less prone to errors. Options of enums can be represented by unsigned integer values starting from 0.

4
Solidity Types (cont…)

5
Solidity Variables
Solidity supports three types of variables.
∙ State Variables − Variables whose values are permanently stored in a contract storage.
∙ Local Variables − Variables whose values are present till function is executing.
∙ Global Variables − Special variables exists in the global namespace used to get information about the
blockchain.
Solidity is a statically typed language, which means that the state or local variable type needs to be specified
during declaration. Each declared variable always have a default value based on its type. There is no concept of
"undefined" or "null".

6
State Variables

• Solidity is a statically typed language, which means that the type of each variable (state and
local)

• State variables are variables whose values are permanently stored in contract storage.
• State Variables are those variables that are defined outside a function whereas Local variables are
those variables that are defined inside a function.

1.7
State Variables Visibility

• public
Public state variables differ from internal ones only in that the compiler automatically generates getter
functions for them, which allows other contracts to read their values. When used within the same
contract, the external access (e.g. this.x) invokes the getter while internal access (e.g. x) gets the variable
value directly from storage. Setter functions are not generated so other contracts cannot directly modify
their values.
• internal
Internal state variables can only be accessed from within the contract they are defined in and in derived
contracts. They cannot be accessed externally. This is the default visibility level for state variables.
• private
Private state variables are like internal ones but they are not visible in derived contracts.

1.8
Local Variables
Variables whose values are available only within a function where it is defined. Function parameters are always local to that function.

pragma solidity ^0.5.0;


contract SolidityTest {

uint storedData; // State variable

function getResult() public view returns(uint){


uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return result; //access the local variable
}
}

1.9
Functions
Functions are the executable units of code. Functions are usually defined inside a contract, but they can also be
defined outside of contracts.

Syntax:

function function_name(parameter_lists) <visibility> <view|pure|payable> <returns>(parameter_list)


{

//Executable code
}

1.10
Functions Example

1.11
Exercise Questions

• Write a Smart Contract to print “Hello World”


• Write a Smart Contract in Solidity to demonstrate local and state variable
• Write a Smart Contract to implement Arithmetic operations.

12
Smart Contract to implement Arithmetic operations

1.13
Remix IDE

Smart contracts can be executed on Remix IDE .


Remix IDE, is a no-setup tool with a GUI for developing smart contracts.

GO TO
https://remix-project.org/

Choose IDE then Remix Online IDE

1.14
Remix Online IDE

Compile

Run and
Deploy

15
To create Smart Contracts
Under File Explorer go to contracts folder.
Right Click to create new File “file_name.sol”

16
After writing contract. Compile it to find errors.
After compilation Run and Deploy the smart contract on Blockchain.

17
On successful deployment a transaction will be created

18
Interact with smart contract

19
For each function call get and set. A new transaction is created

20
References:

• https://docs.soliditylang.org/en/v0.8.17/

• https://remix-project.org/

21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy