Module 4 - Ethereum
Module 4 - Ethereum
Ethereum
Contracts,EVM,Ether- externally owned account (EOA), and a
Cryptocurrency,Decentralized Contract account
applications(Daaps),Decentralized
autonomous organizations(DAOs) Ethereum is a blockchain based distributed computing
What is Ethereum platform that enables developers to build and deploy their
decentralized applications.
You can use Solidity to create contracts for uses such as voting, crowd
funding, blind auctions, and multi-signature wallets.
The EthereumVirtual Machine (EVM)
The Ethereum Virtual Machine, also known as EVM, is the
runtime environment for smart contracts in Ethereum. The Ethereum
Virtual Machine focuses on providing security and executing untrusted
it provides a secure and reliable way to run decentralized
code by computers all over the world. applications (DApps) on the blockchain.
The EVM is required because it provides a common Example of EVM in action is the popular DApp, Uniswap.It's a decentralized
Solidity
that specifies the terms of the bet,such as the teams playing,the amount of the wager and the deadline for the bet. They would then
transfer their bet to the smart contract,which would hold it until end of the game.The smart contract would then automatically pay out
the winnings to the winner, based on the outcome of the game, without the need for a middleman to handle the transaction.
Solidity - Environment Setup
It explains how we can setup Solidity compiler on CentOS machine. If
you do not have a Linux machine then you can use our Online Compiler
for small contracts and for quickly learning Solidity.
Method 1 - npm / Node.js
This is the fastest way to install Solidity compiler on your CentoS
Machine.We have following steps to install Solidity Compiler.
Install Node.js
First make sure you have node.js available on your CentOS machine. If it
is not available then install it using the following commands.
# First install epel-release
$sudo yum install epel-release
# Now install nodejs
$sudo yum install nodejs
# Next install npm (Nodejs Package Manager )
$sudo yum install npm
Solidity
# Finally verify installation
$npm –version
If everything has been installed then you will see an output something like
this − 3.10.10
Install solc
Once you have Node.js package manager installed then you can proceed
to install Solidity compiler as below.
$sudonpm install -g solc
The above command will install solcjs program and will make it available
globally through out the system. Now you can test your Solidity compiler
by issuing following command.
$solcjs-version
If everything goes fine, then this will print something as follows.
0.5.2+commit.1df8f40c.Emscripten.clang
Now you are ready to use solcjs which has fewer features than the
standard Solidity compiler but it will give you a good starting point.
Solidity
Method 2 - Docker Image
You can pull a Docker image and start using it to start with Solidity
programming. Following are the simple steps. Following is the command
to pull a Solidity Docker Image.
$docker pull ethereum/solc:stable
Once a docker image is downloaded we can verify it using the following
command.
$docker run ethereum/solc:stable-version
This will print something as follows
$ docker run ethereum/solc:stable -version
solc, the solidity compiler command line interfaceVersion:
0.5.2+commit.1df8f40c.Linux.g++
Method 3: Binary Packages Installation
If you are willing to install full fledged compiler on your Linux machine,
then please check official website Installing the Solidity Compiler.
Solidity
Solidity - Basic Syntax
A Solidity source files can contain an any number of contract
definitions, import directives and pragma directives.
Let's start with a simple source file of Solidity.
Following is an example of a Solidity file.
pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
Solidity – Pragma and Contract
Pragma
InThe first
Solidity, line
a pragma is a is a pragma
statement directive
that tells the compiler which which
version oftells that
the Solidity the
language source
to code
use for compiling theis
contract. It's important to specify the correct version to ensure that the contract works as intended, as different versions of
written
Solidity may havefor Solidity
different
pragma solidity ^0.8.0;
featuresversion
and syntax. 0.4.0 or anything newer that does not break
Infunctionality up
this example, we're using to, but
^0.8.0 not
to specify including,
that version
our contract should 0.6.0.
be compiled using version 0.8.0 or higher, but lower
than version 0.9.0.The ^ symbol means that the contract can use any compatible minor version greater than or equal to
A pragma
0.8.0, directive is always local to a source file and if you import
but less than 0.9.0.
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 ^.
Solidity Contract The contract keyword is used to define a new contract in Solidity.
A Solidity contract is a collection of code (its functions) and
data (its state) that resides at a specific address on the
Ethereum block chain. The line uintstored Data declares a state
variable called stored Data of type.
Solidity Contract, Importing Files
uint and the functions set and get can be used to modify or retrieve the
value of the variable.
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".
Solidity - Keywords
Reserved Keywords
Following are the reserved keywords in Solidity.
abstract after alias apply
auto case catch copyof
default define final immutable
implements in inline let
macro match mutable null
of override partial promise
reference relocatable sealed sizeof
static supports switch try
typedef typeof unchecked
Solidity – First Application
We're using Remix IDE to Compile and Run our Solidity Code base.
Example:
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;
}
}
Solidity – First Application, Comments
Step 2 − Under CompileTab, 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.
Output
0: uint256: 3
Solidity - Comments
Solidity supports both C-style and C++-style comments, Thus, any text
between a // and the end of a line is treated as a comment and is ignored
by Solidity Compiler. Any text between the characters /* and */ is
treated as a comment.This may span multiple lines.
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++
Solidity - Types
/* 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;
}
Solidity -Types
While writing program in any language, you need to use various variables
to store various information. Variables are nothing but reserved
memory locations to store values. This means that when you create a
variable you reserve some space in memory.
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.
Solidity – General Value Types
GeneralValue Types
Solidity offers the programmer a rich assortment of built-in as well as
user defined data types. Following table lists down seven basic C++ data
types.
Type Keyword Values
Boolean bool true/false
Integer int/uint Signed and unsigned integers of varying
sizes.
Integer int8 to int256 Signed int from 8 bits to 256 bits. int256 is
the same as int.
Integer uint8 to uint256 Unsigned int from 8 bits to 256 bits.
uint256 is the same as uint.
Fixed Point fixed/unfixed Signed and unsigned fixed point numbers of
Numbers varying sizes.
Solidity
Note: You can also represent the signed and unsigned fixed-point
numbers as fixedMxN / ufixedMxN where M represents the number of
bits taken by type and N represents the decimal points. M should be
divisible by 8 and goes from 8 to 256. N can be from 0 to 80.
Type Keyword Values
Fixed Point Fixed / Signed and unsigned fixed point numbers of varying sizes.
Numbers unfixed
Fixed Point fixedMxN Signed fixed point number where M represents number of
Numbers bits taken by type and N represents the decimal points. M
should be divisible by 8 and goes from 8 to 256. N can be
from 0 to 80. fixed is same as fixed128x18.
Fixed Point ufixedMxN Unsigned fixed point number where M represents number
Numbers of bits taken by type and N represents the decimal points.
M should be divisible by 8 and goes from 8 to 256. N can be
from 0 to 80. ufixed is same as ufixed128x18.
Solidity – address, Variables
address
address holds the 20 byte value representing the size of an Ethereum
address. An address can be used to get the balance using .balance
method and can be used to transfer balance to another address using
.transfer method.
address x = 0x212; address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10)
x.transfer(10);
Solidity - Variables Inmanipulated
Solidity, variables are used to store data values that can be accessed and
throughout the code.
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 block chain.
Solidity Variables
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".
State Variable
Variables whose values are permanently stored in a contract storage.
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10; // Using State variable
}
}
LocalVariable
Variables whose values are available only within a function where it is
defined. Function parameters are always local to that function.
Solidity - Variables
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
}
}
Solidity Global Variables
These are special variables which exist in global workspace and provide
information about the block chain and transaction properties.
Name Returns
blockhash(uint blockNumber) returns Hash of the given block - only works for 256 most recent, excluding current, blocks
(bytes32)
pragma solidity ^0.5.0; the code. These functions are defined by the programmer and
can have parameters, return values, and visibility levels. Here is