2 Ethereum Vs Blockchain
2 Ethereum Vs Blockchain
Prepared By Dr.Saranya K G
• Bitcoin is created, stored, transacted, and
distributed using a decentralized, distributed
system known as Blockchain.
• A public ledger records all the transactions of
the Bitcoin and copies are retained on all the
servers around the world.
• It is not necessary to buy an entire bitcoin, one
can buy only a fraction of it if that is all
necessary.
• BTC is the abbreviation for Bitcoin.
Prepared By Dr.Saranya K G
Bitcoin vs Ethereum
Basis Bitcoin Ethereum
Definiti Bitcoin (abbreviation: Ethereum is a
on BTC; sign: ₿) is a decentralized global
decentralized digital software platform
currency that can be powered by blockchain
transferred on the technology. It is most
peer-to-peer bitcoin commonly known for its
network. native cryptocurrency,
ether (ETH).
History The word bitcoin was Ethereum was conceived
defined in a white paper in 2013 by programmer
published on 31 October Vitalik Buterin, and then
2008. The currency went live on 30 July 2015.
began use in 2009.
Prepared By Dr.Saranya K G
Basis Bitcoin Ethereum
The purpose of bitcoin was to The purpose of Ethereum was to
replace national currencies during utilize blockchain technology
Purpose the financial crisis of 2008. for maintaining a decentralized
payment network and storing
computer code.
Smart Although bitcoin do have smart Ethereum allows us to create
Contracts contracts, they are not as flexible or smart contracts. Smart contracts
complete as Ethereum smart are computer codes that is
contracts. Smart contracts in Bitcoin stored on a blockchain and
does not have all the functionality executed when the
that a programming language would predetermined terms and
give them. conditions are met.
Smart Smart contracts on Bitcoin are Smart contracts on Ethereum
Contract written in programming languages are written in programming
Program like Script, Clarity. languages like Solidity, Vyper,
ming etc.
Language
Prepared By Dr.Saranya K G
Basis Bitcoin Ethereum
Generally, bitcoin Ethereum
transactions are only transactions may
Transactions
for keeping notes. contain some
executable code.
Hash Bitcoin runs on Ethereum runs on
Algorithm the SHA-256 hash the Keccak-256 ha
algorithm. sh algorithm.
Prepared By Dr.Saranya K G
Basis Bitcoin Ethereum
Rewards Miner got nearly Miner got nearly 5 BTC
6.25 BTC on along with same
successfully additional rewards on
adding new successfully adding new
block in block in network.
network.
Assets of Bitcoin Assets of Ethereum is
Assets
is BTC. Ether.
Prepared By Dr.Saranya K G
Development Tools and Frameworks
Prepared By Dr.Saranya K G
Solidity language
• Solidity is a domain-specific language of choice
for programming contracts in Ethereum.
• Its syntax is closer to both JavaScript and C.
• It is a statically typed language, which means
that variable type checking in Solidity is carried
out at compile time. Each variable, either state
or local, must be specified with a type at compile
time.
• This is beneficial in the sense that any validation
and checking is completed at compile time and
certain types of bugs, such as
Prepared By Dr.Saranya K G
• interpretation of data types, can be caught
earlier in the development cycle instead of at
runtime, which could be costly, especially in the
case of the blockchain / smart contracts
paradigm.
• Other features of the language include
inheritance, libraries, and the ability to define
composite data types.
• Solidity is also called a contract-oriented
language.
• In Solidity, contracts are equivalent to the
concept of classes in other object-oriented
programming languages.
Prepared By Dr.Saranya K G
data types:
• value types
• reference types
Prepared By Dr.Saranya K G
Value types
• Boolean –
bool v = true;
bool v = false;
Integers
• Int – Signed integer – 8 bits to 256 bits
• Uint – Unsigned integer – 8 bits to 256 bits
Prepared By Dr.Saranya K G
Address
• This data type holds a 160-bit long (20 byte) value.
• This type has several members that can be used to
interact with and query the contracts.
• Balance: The balance member returns the balance
of the address in Wei.
• Send: This member is used to send an amount of
ether to an address (Ethereum's 160-bit address)
and returns true or false depending on the result of
the transaction, for example, the following:
• address to =
0x6414cc08d148dce9ebf5a2d0b7c220ed2d3203da
; address from = this;
• if (to.balance < 10 && from.balance > 50)
to.send(20);
Prepared By Dr.Saranya K G
• Call functions: The call, callcode, and
delegatecall calls are provided in order to
interact with functions that do not have ABI.
• These functions should be used with caution as
they are not safe to use due to the impact on
type safety and security of the contracts.
Prepared By Dr.Saranya K G
• Array value types (fixed size and dynamically
sized byte arrays): Solidity has fixed size and
dynamically sized byte arrays. Fixed size
keywords range from bytes1 to bytes32,
whereas dynamically sized keywords include
bytes and string.
• The bytes keyword is used for raw byte data
and string is used for strings encoded in UTF-8.
As these arrays are returned by the value,
calling them will incur gas cost. length is a
member of array value types and returns the
length of the byte array.
Prepared By Dr.Saranya K G
• An example of a static (fixed size) array is as
follows:
bytes32[10] bankAccounts;
• An example of a dynamically sized array is as
follows:
bytes32[] trades;
• Get length of trades by using the following
code:
trades.length;
Prepared By Dr.Saranya K G
Literals
Prepared By Dr.Saranya K G
Integer literals
Prepared By Dr.Saranya K G
String literals
• String literals specify a set of characters
written with double or single quotes. An
example is shown as follows:
'packt' "packt”
Prepared By Dr.Saranya K G
Hexadecimal literals
• Hexadecimal literals are prefixed with the
keyword hex and specified within double or
single quotation marks.
• An example is shown as follows:
(hex'AABBCC');
Prepared By Dr.Saranya K G
Enums
• This allows the creation of user-defined
types. An example is shown as follows:
enum Order {Filled, Placed, Expired };
Order private ord;
ord=Order.Filled;
Explicit conversion to and from all integer types
is allowed with enums.
Prepared By Dr.Saranya K G
Function types
internal and external functions.
Prepared By Dr.Saranya K G
Internal functions
• These can be used only within the context of
the current contract.
Prepared By Dr.Saranya K G
External functions
• External functions can be called via external function
calls.
• A function in solidity can be marked as a constant.
Constant functions cannot change anything in the
contract;
• they only return values when they are invoked and do
not cost any gas. The syntax to declare a function is
shown as follows:
function <nameofthefunction> (<parameter types>
<name of the variable>)
{internal|external} [constant] [payable] [returns
(<return types> <name of the variable>)]
Prepared By Dr.Saranya K G
Reference types
• As the name suggests, these types are passed
by reference.
• These are also called complex types.
Prepared By Dr.Saranya K G
Arrays
• Arrays represent a contiguous set of
elements of the same size and type laid out at
a memory location.
• The concept is the same as any other
programming language.
• Arrays have two members named length and
push:
• uint[] OrderIds;
Prepared By Dr.Saranya K G
Structs
• These constructs can be used to group a set
of dissimilar data types under a logical group.
These can be used to define new types
Prepared By Dr.Saranya K G
pragma solidity ^0.4.0;
contract TestStruct {
struct Trade
{
uint tradeid;
uint quantity;
uint price;
string trader;
}
//This struct can be initialized and used as below
Trade tStruct = Trade({tradeid:123, quantity:1, price:1,
trader:"equinox"});
}
Prepared By Dr.Saranya K G
Data location
• Data location specifies where a particular complex data
type will be stored.
• Depending on the default or annotation specified, the
location can be storage or memory.
• This is applicable to arrays and structs and can be specified
using the storage or memory keywords.
• As copying between memory and storage can be quite
expensive, specifying a location can be helpful to control
• the gas expenditure at times. Calldata is another memory
location that is used to store function arguments.
• Parameters of external functions use calldata memory.
• By default, parameters of functions are stored in memory,
whereas all other local variables make use of storage.
State variables, on the other hand, are required to use
storage.
Prepared By Dr.Saranya K G
Mappings
• Mappings are used for a key to value
mapping.
• This is a way to associate a value with a key.
• All values in this map are already initialized
with all zeroes, for example, the following:
• mapping (address => uint) offers;
Prepared By Dr.Saranya K G
Global variables
• Solidity provides a number of global variables
that are always available in the global
namespace. These
• variables provide information about blocks and
transactions.
• Additionally, cryptographic functions and address
related variables are available as well.
Prepared By Dr.Saranya K G
• A subset of available functions and variables is
shown as follows:
keccak256(...) returns (bytes32)
• This function is used to compute the Keccak-256
hash of the argument provided to the function:
ecrecover(bytes32 hash, uint8 v, bytes32 r,
bytes32 s) returns (address)
• This function returns the associated address of
the public key from the elliptic curve signature:
block.number
• This returns the current block number.
Prepared By Dr.Saranya K G
Control structures
• Control structures available in solidity
language are if...else, do, while, for, break,
continue, and return.
• They work exactly the same as other
languages such as C-language or JavaScript.
Prepared By Dr.Saranya K G
if:
if (x == 0)
y = 0;
else
z = 1;
Prepared By Dr.Saranya K G
do:
do{
x++;
} (while z>1);
Prepared By Dr.Saranya K G
while:
while(x > 0){
z++;
}
Prepared By Dr.Saranya K G
for, break, and continue:
for(uint8 x=0; x<=10; x++)
{
//perform some work
z++
if(z == 5) break;
}
It will continue the work similarly, but when the
condition is met, the loop will start again.
Prepared By Dr.Saranya K G
return:
return 0;
• It will stop the execution and return value of
0.
Prepared By Dr.Saranya K G
Events
• Events in Solidity can be used to log certain
events in EVM logs.
• These are quite useful when external interfaces
are required to be notified of any change or
event in the contract.
• These logs are stored on the blockchain in
transaction logs.
• Logs cannot be accessed from the contracts but
are used as a mechanism to notify change of
• state or the occurrence of an event (meeting a
condition) in the contract.
Prepared By Dr.Saranya K G
pragma solidity ^0.4.0;
contract valueChecker
{
uint8 price=10;
event valueEvent(bool returnValue);
function Matcher(uint8 x) public returns (bool)
{
if (x>=price)
{
valueEvent(true);
return true;
}
}
}
• valueEvent event will return true if the x parameter passed
to function Matcher is equal to or greater than 10:
Prepared By Dr.Saranya K G
• Inheritance
• Libraries
Prepared By Dr.Saranya K G
Functions
• Functions in Solidity are modules of code that
are associated with a contract.
• Functions are declared with a name, optional
parameters, access modifier, optional
constant keyword, and optional return type.
Prepared By Dr.Saranya K G
function orderMatcher (uint x)
private constant returns(bool return value)
Prepared By Dr.Saranya K G
• In the preceding example, function is the
keyword used to declare the function.
orderMatcher is the function name,
• uint x is an optional parameter, private is the
access modifier or specifier that controls
access to the function
• from external contracts, constant is an
optional keyword used to specify that this
function does not change
• anything in the contract but is used only to
retrieve values from the contract and returns
(bool return value) is the optional return type
of the function.
Prepared By Dr.Saranya K G
How to define a function:
The syntax of defining a function is shown as
follows:
function <name of the function>(<parameters>)
<visibility specifier> returns
(<return data type> <name of the variable>)
{
<function body>
}
Prepared By Dr.Saranya K G
Function signature:
• Functions in Solidity are identified by its
signature, which is the first four bytes of the
Keccak-256 hash of its full signature string.
This is also visible in Remix IDE, as shown in
the following screenshot.
• f9d55e21 is the first four bytes of 32-byte
Keccak-256 hash of the function named
Matcher.
Prepared By Dr.Saranya K G
Function hash as shown in Remix IDE
In this example function, Matcher has the signature hash of d99c89cb. This information
is useful in order to build interfaces.
Prepared By Dr.Saranya K G
Input parameters of a function:
Input parameters of a function are declared in
the form of <data type>
<parameter name>. This example clarifies the
concept where uint x and uint y are input
parameters of the
checkValues function:
Prepared By Dr.Saranya K G
contract myContract
{
function checkValues(uint x, uint y)
{
}
}
Prepared By Dr.Saranya K G
Output parameters of a function:
• Output parameters of a function are declared in the
form of <data type>
• <parameter name>. This example shows a simple
function returning a uint value:
contract myContract
{
function getValue() returns (uint z)
{
z=x+y;
}
}
Prepared By Dr.Saranya K G
• A function can return multiple values. In the
preceding example function, getValue only
returns one value, but a function can return
up to 14 values of different data types.
• The names of the unused return parameters
can be omitted optionally.
Prepared By Dr.Saranya K G