Unit 4-Solidity
Unit 4-Solidity
Associate Professor
Dept. of CS&E
Sri Jayachamarajendra College of
Engineering
JSS Science and Technology University
Solidity is statically typed, supports inheritance, libraries and complex user-defined types
programming language.
You can use Solidity to create contracts for uses such as voting, crowdfunding, blind
auctions, and multi-signature wallets.
The EVM specialised in preventing Denial-of-service attacks and ensures that programs do not have
access to each other's state, ensuring communication can be established without any potential interference.
The concept of smart contracts was first proposed by Nick Szabo in 1994. Szabo is a legal scholar and
cryptographer known for laying the groundwork for digital currency.
It is fine if you do not understand Smart Contract right now, we will go into more detail later.
Pragma
The first line is a pragma directive which tells that the source code is written for Solidity version 0.4.0 or anything newer that does not break functionality up to, but not
including, version 0.6.0.
A pragma directive is always local to a source file and if you import another file, the pragma from that file will not automatically apply to the importing file.
So a pragma for a file which will not compile earlier than version 0.4.0 and it will also not work on a compiler starting from version 0.5.0 will be written as follows −
pragma solidity ^0.4.0;
Here the second condition is added by using ^.
Importing Files
Though above example does not have an import statement but Solidity supports import statements that are very similar to those available in JavaScript.
The following statement imports all global symbols from "filename".
import "filename";
The following example creates a new global symbol symbolName whose members are all the global symbols from "filename".
import * as symbolName from "filename";
To import a file x from the same directory as the current file, use import "./x" as x;. If you use import "x" as x; instead, a different file could be referenced in a global "include
directory".
Line 1: Specifying SPDX license type, which is an addition after Solidity ^0.6.8;
whenever the source code of a smart contract is made available to the public,
these licenses can help resolve/avoid copyright issues. If you do not wish to
specify any license type, you can use a special value UNLICENSED or simply
skip the whole comment (it won’t result in an error, just a warning).
Line 2: On the first line we are declaring which Solidity compiler we want to
use. For instance, we are targeting any version between ≥ 0.4.0 and <0.7.0 .
Line 4: We are declaring a uint (Unsigned Integer) variable named storedData, this variable
will be used to store data.
Line 5-7: Next, we will add a set function, using which we will change the value of our
variable storeData. Here set function is accepting a parameter x whose value, we are storing
into storeData. In addition, the function is marked as public which means that the function
can be called by anyone.
Line 8-10: We will add a get function to retrieve the value of storeData variable. This function
is marked as view which tells Solidity compiler that this is a read-only function.
Other than that the get function also has returns (uint), which means that the function will
return a uint. Dr Prasanna B T, Assoc Prof, CSE, SJCE, Mysuru
Deploying the Smart contract
After writing a smart contract it needs to be deployed on the ethereum
network, we will deploy our smart contract using Remix. There are other
ways to deploy smart contracts, but to make it beginner-friendly, we will use
Remix. Remix is an online web Ethereum IDE. It’s simple and supports many
functionalities.
Output
0: uint256: 3
Example
The following example shows how to use comments in Solidity.
function getResult() public view returns(uint){
// This is a comment. It is similar to comments in C++
/*
*This is a multi-line comment in solidity
*It is very similar to comments in C Programming
*/
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
Output
0: uint256: 10
We're using Remix IDE to Compile and Run our Solidity Code base.
Step 1 − Copy the given code in Remix IDE Code Section.
Step 2 − Under Compile Tab, click Start to Compile button.
Step 3 − Under Run Tab, click Deploy button.
Step 4 − Under Run Tab, Select SolidityTest at 0x... in drop-down.
Step 5 − Click getResult Button to display the result.
blockhash(uint blockNumber) returns (bytes32) Hash of the given block - only works for 256 most recent, excluding
current, blocks
block.coinbase (address payable) Current block miner's address
block.difficulty (uint) Current block difficulty
block.gaslimit (uint) Current block gaslimit
block.number (uint) Current block number
block.timestamp (uint) Current block timestamp as seconds since unix epoch
1 + (Addition)
Adds two operands
Ex: A + B will give 30
2 - (Subtraction)
Subtracts the second operand from the first
Ex: A - B will give -10
3 * (Multiplication)
Multiply both operands
Ex: A * B will give 200
4 / (Division)
Divide the numerator by the denominator
Ex: B / A will give 2
5 % (Modulus)
Outputs the remainder of an integer division
Ex: B % A will give 0
6 ++ (Increment)
Increases an integer value by one
Ex: A++ will give 11
7 -- (Decrement)
Decreases an integer value by one
Ex: A-- will give 9
2 || (Logical OR)
If any of the two operands are non-
zero, then the condition becomes
true.
Ex: (A || B) is true.
3 ! (Logical NOT)
Reverses the logical state of its
operand. If a condition is true, then
the Logical NOT operator will make
it false.
Ex: ! (A && B) is false.
Note − Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, |= and ^=.
1 ? : (Conditional )
If Condition is true? Then value X : Otherwise value Y
2 do...while Loop
The do...while loop is similar to the while loop
except that the condition check happens at the
end of the loop.
3 For Loop
The for loop is the most compact form of
looping. It includes the following three important
parts.
4 Loop Control
Solidity provides full control to handle loops and
switch statements