Solidity Course

#2 - Ethereum

Miguel Palhas / Hugo Gonçalves

Ethereum

What is Ethereum?

Ethereum is a decentralized, open-source blockchain with smart contract functionality.

 

Ethereum is also the most actively used blockchain.

 

The native cryptocurrency of the Ethereum network is Ether (ETH or Ξ). ETH is the second-largest cryptocurrency by market capitalization.
 

Ethereum is developed based on EIPs - Ethereum Improvement Proposals.

 

Ethereum is currently undergoing a fundamental re-design of the ecosystem, including a change to a Proof of Stake algorithm, commonly known as Ethereum 2.0.

Why was Ethereum a game-changer?

Bitcoin's primary use cases are store of value and serving as decentralized digital currency.

 

While Ether can also be used for the same purposes, the Ethereum network is in itself a decentralized computing platform: Anyone can  create smart contracts and distribute them to the network.

 

Applications that use the blockchain as a data source, using smart contracts to connect to it, are called distributed apps or DApps.

Transaction Models

Transaction Models

For blockchains, a transaction model is how a transaction (tx) is represented in the system.

 

Across the different blockchains you'll commonly find two:

 

          1. Account Based - used by Ethereum

          2. Unspent Transaction Outputs - used by Bitcoin

Ethereum's Account Tx Model

There are two types of accounts: user and contract accounts.

Each account has a balance and can send transactions using that balance.

 

Accounts are identified by a single address, a 40 hexadecimal digit string.

 i.e. 0x588b38CAF871AbE020a49E6617A10ed2B71E61cB

 

A transaction is comprised of the sender's address, the receiver's address and the amount being transferred. Fee information and others are also included.

 

Sending txs decreases the account's balance. Inversely, Receiving txs increases the account's balance.

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

We are ignoring fees and other ETH functionalities. Keeping it simple.

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

From: 0x123...

To: 0xabc...

Amount: 1 ETH

We are ignoring fees and other ETH functionalities. Keeping it simple.

Send Transaction

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

From: 0x123...

To: 0xabc...

Amount: 1 ETH

Alice's Account

Balance: 4 ETH

Bob's Account

Balance: 1 ETH

Address: 0x123...

Address: 0xabc...

We are ignoring fees and other ETH functionalities. Keeping it simple.

-1 ETH

+1 ETH

Send Transaction

Bitcoin's Unspent Transaction Ouputs Tx Model

A Bitcoin transaction is comprised of inputs and outputs.


Outputs are the result of transactions.


Each output has a destination address and an amount. They also store the transaction hash on which they were created.


Until they are used as inputs on a new transaction, they are considered unspent outputs.


The balance of a given Bitcoin address is the sum of all unspent outputs the address has.

Transaction Fees

How are miners rewarded for running nodes?

For each transaction, a certain amount of fees needs to be paid, for it to be included in a block.

 

When miners mine a block, they get newly-minted coins as a reward, plus fees from each of the transactions included in said block.

 

Transactions with higher fees will be included faster in blocks, as miners will obviously prefer transactions with higher fees.

 

The fee system can be different between blockchains.

 

Keep in mind that third-party services may require you to pay other fees to send transactions, besides the transactions fees.

Ethereum's Gas System

In Ethereum, fees are called gas.

 

Gas is the unit that measures the use of computational power in the Ethereum network.

 

For now, miners get all of the gas paid by the transactions included in the blocks they mine.

Ethereum's Gas System

There are two factors set by the sender on each transaction that determine the gas paid for a transaction: gasPrice & gasLimit

 

gasLimit defines the maximum amount of gas the transaction will use.

More complex operations ➔ More gas needs to be paid.

For example, sending Ether between user addresses costs a standard of 21000 gas.

 

gasPrice is how much Ether you want to pay per each unit of gas. It's the equivalent of the miner fee in Bitcoin.

Usually measured in gwei/gas (1 gwei = 0.000_000_001 Ether)

 

Ethereum's Gas System - A methaphor

Imagine you are driving from Braga to Lisbon. Roughly 364 km.

 

You know you need at most 25L of gas to do the trip ➔ this is the gas limit.

 

However, to fill up your tank with 25L you pay 1.45€/L ➔ this is the gas price.

 

You might end up spending the whole 25L or not.

tx cost = gasSpent × gasPrice

Smart Contracts

Smart Contracts Explained

Smart contracts consist of a set of operations that are executed once the predefined conditions are met.

 

In the blockchain, smart contracts are programs stored on the blockchain itself. This means the (byte)code is public.

 

These programs can be interacted with by sending messages to the smart contract (in the form of transactions) through the network.

 

Smart contracts can also interact with other smart contracts on the network.

Ethereum Smart Contracts

Ethereum smart contracts are written in Solidity, a scripting programming language created for this specific purpose.

 

Smart contracts can be deployed by sending an Ethereum transaction without recipient and the bytecode of the contract attached.

 

Once deployed, the smart contract will have a dedicated address on the blockchain.

 

To run a function on a smart contract, you need to send a message to the contract's address, specifying the function you want to run and the relevant inputs.

Solidity Course

#2 - Ethereum

Miguel Palhas / Hugo Gonçalves

Solidity Course 02

By Hugo Gonçalves

Solidity Course 02

  • 159