KEMBAR78
Lect 13 | PDF | Bitcoin | Computer Programming
0% found this document useful (0 votes)
14 views53 pages

Lect 13

Uploaded by

tomshopstrash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views53 pages

Lect 13

Uploaded by

tomshopstrash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Introduction to

Ethereum Blockchain
Content

● Ethereum Blockchain
● Solidity Programming language and its syntax
● Structure of a smart contract
● Global variables in solidity
Introduction to Ethereum

Ethereum is an open software platform based on blockchain technology that


enables anyone to build and deploy decentralized applications(dapps)
Introduction to Ethereum

⎼ Ethereum is a decentralized platform that enables the creation of


smart contracts and decentralized applications.It is founded by Vitalik
Buterin in 2015.
⎼ Ethereum's goal is to decentralize the internet and empower
individuals.
⎼ Ether (ETH) is the native cryptocurrency of the Ethereum platform,
used for transactions and computational services.
The Birth of Ethereum
What is Ethereum?

⎼ Ethereum is a blockchain that allows you to run programs in its


trusted environment.It contrasts with the Bitcoin blockchain, which
only allows you to manage cryptocurrency.
⎼ Ethereum has a virtual machine -- Ethereum Virtual Machine (EVM).
⎼ The EVM allows code to be verified and executed on the blockchain,
providing guarantee that it will be running in same way on everyone's
machine.
⎼ This code is contained in "smart contracts"
⎼ Ethereum maintains the state of the EVM on the blockchain.
⎼ All nodes process smart contracts to verify the integrity of the
contracts and their outputs.
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.
Ether

⎼ Native currency of the Ethereum blockchain.


⎼ It is used as payment for using the network.
Accounts

⎼ Global state of Ethereum comprises of many 'accounts'


⎼ Two kinds of accounts:
○ Externally owned accounts(EOAs) - owned and controlled by users
and have no code associated with it. Owner has private key to access
funds or contracts on the account.
○ Contract accounts - controlled by the associated contract code. The
contract code is executed through transactions sent by EOA's or
messages send by other contracts.
⎼ Each account has a 20-byte address and a state associated with it.
⎼ An EOA
○ can send messages to other EOA (ether transfer)
○ can send messages to other contract accounts (code invocation)
Transaction

⎼ Transactions are signed messages originated by an EOA.


⎼ Transactions to an EOA is value transfer.
⎼ Transactions to a contract resulted the code to be executed and the
payload is the input data.
⎼ Transaction has the following data:
○ nonce (replay attack protection)
○ to (destination address)
○ value (ETH amount to send)
○ data (contract code)
○ gas price (amount eth/unit gas)
○ startgas (maximum gas consumable)
○ v, r, s (ECDSA signature values)
How Ethereum differs from Bitcoin

● Ethereum differs from Bitcoin in its purpose


and capabilities.
● While Bitcoin primarily functions as digital
currency, Ethereum serves as a platform for
building decentralized applications.
● Ethereum’s Turing-complete programming
language allows for more complex
functionalities compared to Bitcoin's
scripting language.
How Ethereum differs from Bitcoin

Bitcoin Ethereum
● Purpose: Peer-to-peer electronic cash ● Purpose: Platform for decentralized
system. applications and smart contracts
● Technology: Blockchain with PoW ● Technology: Blockchain with EVM for smart
consensus. contracts, transitioning to PoS
● Consensus algorithm:Currently uses Proof ● Consensus algorithm:Transitioning from
of Work (PoW). PoW to Proof of Stake (PoS) with Ethereum
● Market Capitalization: Highest market cap 2.0
● Use Cases: Store of value, medium of ● Market Capitalization: Second-highest
exchange. market cap
● Use Cases: DeFi, NFTs, DApps
Ethereum Architecture

● Nodes: Ethereum consists of a network of


nodes, which are computers that run the
Ethereum software. There are two types of
nodes: full nodes and light nodes.
● Consensus mechanism: Ethereum
currently uses a Proof of Work (PoW)
consensus mechanism, but it is
transitioning to Proof of Stake (PoS) with
the Ethereum 2.0 upgrade.
● Gas and transactions: Gas is the fee
required to execute operations on the
Ethereum network. Transactions on
Ethereum require gas to be processed.
Ether and Gas

● Ether is the native cryptocurrency of the Ethereum Network


● Gas is a unit to measure the computational work done.
○ Introduced as it would have been unfair to base the transaction
fees on just the transaction length or keep it constant
○ Each operation has a gas cost
○ Every transaction mentions the gas price
○ The two together give the transaction fees in Ether
○ Two new scenarios -
■ Transaction running out of gas
■ Gas price too low/high
Smart Contracts and Decentralized Applications
(DApps)

● What are smart contracts?

Smart contracts are self-executing contracts with the terms of the agreement
directly written into code. They automatically execute actions when certain
conditions are met.

● Ethereum Virtual Machine (EVM): The EVM is the runtime environment for
smart contracts on the Ethereum network. It enables anyone to run a program,
regardless of the programming language.

● Examples of DApps: Examples include Uniswap (a decentralized exchange),


Aave (a decentralized lending platform), and CryptoKitties (a game involving
collectible digital cats).
What is a smart contract?

● A smart contract is code that runs on the EVM.


● Smart contracts can accept and store ether, data, or a combination of both.
● Using the logic programmed into the contract, it can distribute that ether to other
accounts or even other smart contracts.
Decentralized applications(Dapps)

● Applications using smart contracts for their processing are called "decentralized
applications", or "dapps".
● The user interfaces for these dapps consist of familiar languages such as HTML,
CSS, and JavaScript.
● The application itself can be hosted on a traditional web server or on a
decentralized file service such as Swarm or IPFS.
● Dapps based solution available for:
○ Record keeping
○ Finance
○ Supply chains
○ Real estate
○ Marketplaces
Solidity Programming
Language
● Solidity is a high-level programming
language designed for implementing

What is Solidity? ●
smart contracts.
It is a statically typed object-
oriented(contract-oriented) language.
● Solidity is highly influenced by Python,
c++, and JavaScript which run on the
Ethereum Virtual Machine(EVM).
● Solidity supports complex user-defined
programming, libraries, and inheritance.
● Solidity is the primary language for
blockchains running platforms.
Features of Solidity

● Smart Contract Support: Solidity is specifically designed for creating


smart contracts, enabling developers to define rules for transactions.
● Security: Solidity includes features like function modifiers and error
handling to enhance security in smart contracts.
● Ethereum Integration: Solidity integrates seamlessly with Ethereum,
allowing developers to interact with Ethereum addresses and tokens.
● Libraries: Solidity supports the use of libraries to organize and reuse
code.
Solidity Compiler
● Ethereum Virtual Machine abbreviated as
EVM is a runtime environment for

Ethereum Virtual executing smart contracts in ethereum.


● It focuses widely on providing security
Machine(EVM)
and execution of untrusted code using an
international network of public nodes.
● EVM is specialized to prevent Denial-of-
service attack and confirms that the
program does not have any access to
each other’s state, also ensures that the
communication is established without
any potential interference.
Different methods of setting up Solidity compiler
environment

● Remix: Remix IDE is an application that provides plugins and a development


environment for smart contracts. Users can use this application online without
installing any software for the environment.
● Node.js/npm: This is the easiest way to set up the environment. Here, you will install a
solidity compiler called solc-js. However, solc-js offer limited functionalities for
accessing the compiler.
● Docker Image: Docker image offers simple steps in setting up the environment.
Docker images offer a template to build a container for running the Solidity compiler.
● Binary Packages: Binary packages are archive files that will have all directories and
files for installing the Solidity compiler on your device.
Understanding the Solidity Syntax

● Pragma: In Solidity, a pragma language will specify how the compiler will process
any type of input. Typically, the first line of code in Solidity based smart contracts
contains the pragma.
● Contract: uintstoredData component within the code will denote the Solidity
contract. This part will contain all the data and code needed for locating a
particular address within the blockchain.
● File Importing: Solidity offers similar support for file import systems like
JavaScript.
Hello World
solidity program
Write & compile HelloWorld program in Remix
Deploy & run HelloWorld program in Remix
Comments
Constructors

The constructor is a special function that is called when the contract is deployed to the
blockchain. It is used to initialize the contract and set its initial state. The constructor
typically takes any necessary arguments, such as the initial values for the contract’s
variables.
Import
State variables
State variables access
Data types
Structs
A Solidity struct is a user-defined data type that allows you to create complex data
structures to store multiple values of different types in a single variable. This can be
useful for organizing data and making it easier to access and manipulate.
Arrays
An array is a data type that allows you to store a collection of values of the same type.
Arrays can be declared using the type[] syntax, where type is the data type of the values
that the array will store. For example:
while (age < 21) {
Loops- While, Do return "you are not eligible to drive";
}
While, For loop
do {
return "you are not eligible to drive";
}
while (age < 21);
for(int i=0; i<= age; i++)
{
//statement
}
Inheritance
Solidity supports inheritance between smart contracts. Inheritance is the process of
defining multiple contracts that are related to each other through parent-child
relationships. Solidity supports multiple types of inheritance, including multiple
inheritance.
Polymorphism

Polymorphism means having multiple forms of functions.


Structure of smart
contract
Smart Contract

● Proposed by Nick Szabo, who coined the term, in 1994


● It is a computer protocol intended to digitally facilitate, verify, or enforce
the negotiation or performance of a contract
● It can accept and store ether, data, or a combination of both
● Programmable
● Written in a language called Solidity. Solidity syntax is similar to
JavaScript
Execution of Smart contract

● When an Ethereum block is “mined”,


○ The smart-contract deployments and function calls within that block get
executed on the node that mines the block
■ the new state changes to any storage spaces or transactions within
that smart-contract actually occur on that miner node
○ As the new block gets propagated to all the other nodes
■ Each node tries to independently verify the block,
■ Verifying includes doing those same state changes to their local copy
of the blockchain
○ It will fail if the smart-contract acts non-deterministically.
■ If the other nodes cannot come to a consensus about the state of
blockchain after the new block and its contracts get executed, the
network could literally halt.
Execution of Smart contract(2)

● EVM smart-contracts cannot access data outside the “memory”, and


“storage”
○ (we don’t want the smart-contract to be able to read or delete
the hard-drives of the nodes it runs on)
● Cannot query outside resources like with a JQuery.
● Do not have access to many library functions like for parsing JSON
structures or doing floating-point arithmetic,
○ it’s actually cost-prohibitive to do those sub-routines or store
much data in the Ethereum blockchain itself.
Execution of Smart contract(3)

● When you call a smart-contract that does some state-changing work or computation,
you will incur a gas “cost” for the work done by the smart contract
○ this gas cost is related to the amount of computational work required to
execute your function.
○ sort of a “micropayment for microcomputing” system, where you can expect
to pay a set amount of gas for a set amount of computation, forever
● The price of gas itself is meant to stay generally constant, meaning that when Ether
goes up on the global markets, the price of gas against Ether should go down.
● When you execute a function call to a smart-contract, you can get an estimation of the
amount of gas you must pay beforehand, but you must also specify the price (in ether
per gas) that you are willing to pay
Key components of smart contract

● State Variables: Variables that represent the state of the contract and can
be read or modified.
● Functions: Code that defines the behavior of the contract, including data
manipulation and interaction with other contracts.
● Events: Notifications that are emitted when specific conditions are met,
allowing external applications to react to changes in the contract state.
● Modifiers: Code that can be used to modify the behavior of functions,
such as adding access control or validation checks.
● Data Types: Integers, strings,
arrays, structs, mappings.
Solidity Syntax
Overview ● Control Structures: If-else
statements, for loops, while
loops.
● Function Types: Public, private,
internal, external.
● Error Handling: Require
statements for input validation.
Variables

In Solidity, variables are used to store data that can be accessed and modified by
functions within a smart contract. Solidity supports three types of variables: state
variables, local variables, and global variables.

a. Local variables

Local variables are only present while a function is executing. These variables are only
accessible within the function in which they are declared and are not permanently
stored in contract storage.
Variables

b. State variables

They are permanently stored in contract storage and can be accessed and modified by
functions within the contract. For example, the following code declares a state variable
called count of type uint256 and initializes it to 0:
c. Global variables

Global variables are special variables that exist in the global namespace and provide
information about the blockchain. These variables can be accessed from anywhere
within the contract and are useful for getting information such as the current block
number or the address of the contract.

To make a state variable accessible outside the contract, we can use the public
keyword before the variable declaration. This allows the variable to be accessed from
outside the contract using its name and the contract’s address. For example:
pragma solidity ^0.4.0;

contract SimpleStorage {
Functions uint storedData;

function set(uint x) public {


storedData = x;
}

function get() public view returns


(uint) {
return storedData;
}
}
Function types

● Public - Anyone can call this function


● Private -Only this contract can call this function.
● view -This function returns data and does not modify the contract's
data
● constant -This function returns data and does not modify the
contract's data
● pure -Function will not modify or even read the contract's data
● payable -When someone call this function they might send ether along
Events

Events are used to provide a record of important actions that have taken place
within the contract, such as the completion of a transaction. They can be used
to trigger external functions or to provide information to users of the contract.
Modifiers

These are used to modify the behavior of functions. They can be used to check for
certain conditions before allowing a function to execute. For example, a modifier
might be used to check that a certain requirement has been met before allowing a
function to be called, or to ensure that a function can only be called by certain users.

You might also like