Week 3
Week 3
Smart Contracts
Week – 3
Pritom Rajkhowa
Consensus Model
Consen Model
sus
Block Creator 2
Block Creator 3
Bob paid Alice LD20
{Signature}
Block Creator 2
Block Creator 3
Previous Hash
7567454092
Block Creator 2
SHA256
Block Creator 3
Block Creator 1
Block Creator 2
Block Creator 3
Previous Hash
Reword LD10
7567454092
Block Creator 2
SHA256
Block Creator 3
Conflicting Transaction
TRUST CENTRAL
AUTHORITY
COMPUTATION
WORK
Eve want to fraud
Bob with fake
transaction
Transaction
85665780
Eve want to fraud
Bob with fake
transaction
Transaction
85665780
Proof of Work
Stake
Validator
How to create a MetaMask Wallet
Click on “Click here to reveal secret words” to show the seed phrase.
• MetaMask requires that you store your seed phrase in a safe place. It is the only way to recover your funds
should your device crash or your browser reset. We recommend you write it down. The most common
method is to write your 12-word phrase on a piece of paper and store it safely in a place where only you
have access. Note: if you lose your seed phrase, MetaMask can’t help you recover your wallet and your
funds will be lost forever.
• Never share your seed phrase or your private key to anyone or any site, unless you want them to have full
control over your funds.
Click on “Next”.
Step 6: Seed phrase confirmation
Confirm your secret backup phrase by clicking on each word in the order in which the words were presented
on the previous screen. Click on “Confirm” to proceed.
Congratulations! Your MetaMask wallet has been set up successfully.
You can now access your wallet by clicking on the MetaMask icon at the top-right-end corner of your preferred browser.
Blockchain Demo:
https://andersbrownworth.com/blockchain/hash
https://andersbrownworth.com/blockchain/block
https://andersbrownworth.com/blockchain/blockchain
https://andersbrownworth.com/blockchain/distributed
https://andersbrownworth.com/blockchain/tokens
https://andersbrownworth.com/blockchain/public-private-keys/keys
Elliptic Curve Digital Signature Algorithm
https://eth-converter.com/
How Blockchain works
User Requested
Transaction is Transaction will be validated
User Once the transaction is verified by Transaction Is
Broadcasted to by any node in this Network
Requests A multiple Nodes in the Blockchain completed
Blockchain through some algorithms and
transaction peer-to-peer network then new block gets added to
smart contract chain and replicated across all the
network
nodes
Ethereum Blockchain framework
Ethereum is a decentralized platform that runs smart
contracts: applications that run exactly as programmed
without any possibility of downtime, censorship, fraud or
third-party interference.
Smart Contract
Agreements are written as Smart Contracts.
Financial derivatives
Insurance premiums
Breach contracts
Property law
Credit enforcement
Financial services
Legal processes
Crowdfunding agreements
And more
What Is Solidity?
Solidity is a type of object-oriented
programming language. This
programing language is developed
specifically for smart contracts. This
type of language is widely used in
creating smart contracts features in
blockchain platforms. It’s influenced
by C++, JavaScript and Python.
Solidity also uses Ethereum Virtual
Machine to function properly.
Ethereum
File Importing: Solidity offers similar support for file import systems like
JavaScript.
Value Types in Solidity
• Variables are only locations that are reserved for storing values.
Thus, users can reserve any amount of memory for a certain
variable.
However, Solidity allows contracts to interact with one another and call
a contract to perform a specific logic.
Import
The import keyword helps import other Solidity files and we can access its code
within the current Solidity file and code. This helps us write modular Solidity code.
import <filename> ;
Import 'CommonLibrary.sol';
What is a Contract in Solidity?
Immutable variables are like constants. Values of immutable variables can be set inside the constructor but cannot be
modified afterwards.
Reading and Writing to a State Variable
Gas Limit
There are 2 upper bounds to the amount of gas you can spend
• gas limit (max amount of gas you're willing to use for your transaction, set by you)
• block gas limit (max amount of gas allowed in a block, set by the network)
If / Else
Solidity supports conditional statements if, else if and else.
For and While Loop
Solidity supports for, while, and do while loops.
Don't write loops that are unbounded as this can hit the gas limit, causing your
transaction to fail.
For the reason above, while and do while loops are rarely used.
Mapping
The keyType can be any built-in value type, bytes, string, or any contract.
Declaring Arrays
To declare an array of fixed size in Solidity, the programmer specifies the type of
the elements and the number of elements required by an array as follows −
type arrayName [ arraySize ];
uint balance[10];
To declare an array of dynamic size in Solidity, the programmer specifies the type of the elements as follows −
type[] arrayName;
Array
Initializing Arrays
You can initialize Solidity array elements either one by one or using a single
statement as follows −
uint balance[3] = [1, 2, 3];
The number of values between braces [ ] can not be larger than the number of
elements that we declare for the array between square brackets [ ]. Following
is an example to assign a single element of the array −
If you omit the size of the array, an array just big enough to hold the initialization
is created. Therefore, if you write −
uint balance[] = [1, 2, 3];
You will create exactly the same array as you did in the previous example.
balance[2] = 5;
The above statement assigns element number 3rd in the array a value of 5.
Creating dynamic memory arrays
Dynamic memory arrays are created using new keyword.
uint size = 3;
uint balance[] = new uint[](size);
The above statement will take 3rd element from the array and assign the value to salary
variable. Following is an example, which will use all the above-mentioned three concepts
viz. declaration, assignment and accessing arrays −
Members
• length − length returns the size of the array. length can be used to change the size of
dynamic array be setting it.
• push − push allows to append an element to a dynamic storage array at the end. It returns
the new length of the array.
Enum
Struct types are used to represent a record. Suppose you want to keep track of yourbooks in a library.
You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
Defining a Struct
To define a Struct, you must use the struct keyword. The struct keyword
defines a new data type, with more than one member. The format of
the struct statement is as follows −
Structure Example
Data Locations - Storage, Memory
and Calldata
Modifiers are code that can be run before and / or after a function call.
Modifiers can be used to:
• Restrict access
• Validate inputs
• Guard against reentrancy hack
Events
Events allow logging to the Ethereum blockchain. Some use cases for
events are:
• Listening for events and updating user interface
• A cheap form of storage
Constructor
Functions and state variables have to declare whether they are accessible by other contracts.
Functions can be declared as
• public - any contract and account can call
• private - only inside the contract that defines the function
• internal- only inside contract that inherits an internal function
• external - only other contracts and accounts can call
State variables can be declared as public, private, or internal but not external.
Function Visibility Specifier in Solidity
Function in Solidity is a set of code that performs a specific task. The function
provides reusability of code in smart contracts. In Solidity, functions are defined
using the function keyword. The syntax of the function definition in Solidity is:
Function visibility in Solidity defines the visibility of the function to other functions
within the contract, or in other contracts. Function visibility helps to identify where in
the contract or in other contracts, functions can be used. It is specified by the developer
of the smart contract. The default variable visibility in Solidity is internal. The default
function visibility in Solidity is public.
How do Function Visibility Modifiers and Inheritance Work
Together?
Calling Parent Contracts