Lição 2

Ethereum and Solidity Basics

Ethereum is a blockchain-based platform that supports smart contracts and decentralized applications (dApps).

Overview of Ethereum

Ethereum is a blockchain-based open-source, distributed software platform that has emerged as a major challenger to Bitcoin. Ethereum was first proposed in 2013 by cryptocurrency researcher Vitalik Buterin, who suggested adding a scripting language for programming to Bitcoin. It was launched on July 30, 2015. Ethereum’s development was sponsored by an online crowdsale, a type of crowdsourcing in which cryptocurrency tokens were issued.

Read More: What is Ethereum?

Ethereum’s native cryptocurrency is called Ether (ETH), and it features a programming language known as Solidity. Blockchain, the underlying technology of Ethereum, is a distributed ledger that maintains a permanent, tamper-proof list of records. This decentralized architecture enables developers to create a wide range of decentralized applications (dApps), taking advantage of the platform’s robust ecosystem and versatility.

One of the core features of Ethereum is its support for smart contracts, which are digital contracts that execute automatically based on predefined conditions as we mentioned in the Lesson 1. These smart contracts enable a trustless environment where transactions can be conducted securely and transparently without the need for intermediaries. Miners on the Ethereum network were producing Ether tokens, which serve not only as currency but also as a means to pay for usage fees on the platform; after “The Merge”, Ethereum has transitioned to a Proof-of-Stake (PoS) mechanism, where validators (not miners) validate transactions and create new blocks. Instead of producing Ether tokens, validators earn transaction fees and block rewards in Ether for their participation in the network. Ether still serves as a means to pay for usage fees on the platform, but its issuance and distribution are different from the previous Proof-of-Work (PoW) mechanism.

Ethereum’s ability to host dApps and smart contracts has attracted significant interest from developers and organizations, making it a leading platform for building innovative solutions in various industries, including finance, supply chain, and gaming, among others. As the Ethereum ecosystem continues to evolve, it remains at the forefront of blockchain technology, driving innovation and fostering a thriving community of developers and users alike.

Read More: What is The Merge?

Read More: What is DApp?

Introduction to Solidity programming language

Solidity is a high-level, statically-typed programming language specifically designed for writing smart contracts on the Ethereum blockchain. Developed by Ethereum’s core team, Solidity’s syntax is influenced by JavaScript, making it familiar and accessible to a wide range of developers. The language is Turing-complete, allowing developers to create complex logic and implement various functionalities in their smart contracts. Solidity compiles into Ethereum Virtual Machine (EVM) bytecode, which is executed on the Ethereum network.

In Solidity, developers can define custom data structures, create user-defined functions, and implement access control mechanisms, among other features. With Solidity, it is possible to create a wide array of decentralized applications, including decentralized finance (DeFi) platforms, non-fungible tokens (NFTs), decentralized autonomous organizations (DAOs), and more.

Structure of a Solidity contract: Example

A Solidity contract is a self-contained piece of code that consists of variables, functions, events, and modifiers, among other elements. The basic structure of a Solidity contract is as follows:

  1. Pragma directive: This line specifies the compatible Solidity compiler version for the smart contract. For example:

    Solidity
    pragma solidity ^0.8.0;
    
  2. Contract definition: This line declares the contract and its name. \
    For instance:

    Solidity
    contract SimpleToken {
    
  3. State variables: These are variables that store the contract’s state on blockchain. For example, you can store the total supply of a token:

    Solidity
    uint256 public totalSupply;
    
  4. Functions: Functions define the contract’s behavior and can be called by external users or other contracts. Here’s a simple function to transfer tokens:

    Solidity
    function transfer(address recipient, uint256 amount) public {
     // ... transfer logic ...
    }
    
  5. Events: Events are used to log specific actions in the contract and can be monitored by external users or contracts. For instance, a Transfer event in a token contract:

    Solidity
    event Transfer(address indexed from, address indexed to, uint256 value);
    
  6. Modifiers: Modifiers are used to modify the behavior of functions, usually for access control or precondition checks. For example, a modifier to check if the sender has enough tokens to transfer:

    JavaScript
    modifier hasEnoughTokens(uint256 amount) {
     require(balanceOf[msg.sender] >= amount, "Insufficient balance");
     _;
    }
    
  7. Constructor: The constructor is a special function that initializes the contract’s state variables when it is deployed. For example, initializing the total supply of a token:

    JavaScript
    constructor(uint256 _totalSupply) {
     totalSupply = _totalSupply;
     balanceOf[msg.sender] = _totalSupply;
    }
    

Putting it all together, a simple Solidity contract for a token might look like this:

TypeScript
pragma solidity ^0.8.0;

contract SimpleToken {
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 value);

    modifier hasEnoughTokens(uint256 amount) {
        require(balanceOf[msg.sender] >= amount, "Insufficient balance");
        _;
    }
}

Highlights
Ethereum is a blockchain-based platform that supports smart contracts and decentralized applications (dApps).
Ethereum’s native cryptocurrency is Ether (ETH).
Solidity is a high-level programming language used for writing smart contracts on Ethereum.
Smart contracts on Ethereum execute automatically based on predefined conditions.
Ethereum has transitioned to a Proof-of-Stake (PoS) mechanism for validating transactions and creating new blocks.
Solidity allows developers to define data structures, create functions, implement access control, and more.
Ethereum’s ecosystem attracts developers and organizations from various industries.
Solidity contracts consist of pragmas, contract definition, state variables, functions, events, modifiers, and constructors.
Solidity contracts can be used for creating DeFi platforms, NFTs, DAOs, and more.

Isenção de responsabilidade
* O investimento em criptomoedas envolve grandes riscos. Prossiga com cautela. O curso não se destina a servir de orientação para investimentos.
* O curso foi criado pelo autor que entrou para o Gate Learn. As opiniões compartilhadas pelo autor não representam o Gate Learn.
Catálogo
Lição 2

Ethereum and Solidity Basics

Ethereum is a blockchain-based platform that supports smart contracts and decentralized applications (dApps).

Overview of Ethereum

Ethereum is a blockchain-based open-source, distributed software platform that has emerged as a major challenger to Bitcoin. Ethereum was first proposed in 2013 by cryptocurrency researcher Vitalik Buterin, who suggested adding a scripting language for programming to Bitcoin. It was launched on July 30, 2015. Ethereum’s development was sponsored by an online crowdsale, a type of crowdsourcing in which cryptocurrency tokens were issued.

Read More: What is Ethereum?

Ethereum’s native cryptocurrency is called Ether (ETH), and it features a programming language known as Solidity. Blockchain, the underlying technology of Ethereum, is a distributed ledger that maintains a permanent, tamper-proof list of records. This decentralized architecture enables developers to create a wide range of decentralized applications (dApps), taking advantage of the platform’s robust ecosystem and versatility.

One of the core features of Ethereum is its support for smart contracts, which are digital contracts that execute automatically based on predefined conditions as we mentioned in the Lesson 1. These smart contracts enable a trustless environment where transactions can be conducted securely and transparently without the need for intermediaries. Miners on the Ethereum network were producing Ether tokens, which serve not only as currency but also as a means to pay for usage fees on the platform; after “The Merge”, Ethereum has transitioned to a Proof-of-Stake (PoS) mechanism, where validators (not miners) validate transactions and create new blocks. Instead of producing Ether tokens, validators earn transaction fees and block rewards in Ether for their participation in the network. Ether still serves as a means to pay for usage fees on the platform, but its issuance and distribution are different from the previous Proof-of-Work (PoW) mechanism.

Ethereum’s ability to host dApps and smart contracts has attracted significant interest from developers and organizations, making it a leading platform for building innovative solutions in various industries, including finance, supply chain, and gaming, among others. As the Ethereum ecosystem continues to evolve, it remains at the forefront of blockchain technology, driving innovation and fostering a thriving community of developers and users alike.

Read More: What is The Merge?

Read More: What is DApp?

Introduction to Solidity programming language

Solidity is a high-level, statically-typed programming language specifically designed for writing smart contracts on the Ethereum blockchain. Developed by Ethereum’s core team, Solidity’s syntax is influenced by JavaScript, making it familiar and accessible to a wide range of developers. The language is Turing-complete, allowing developers to create complex logic and implement various functionalities in their smart contracts. Solidity compiles into Ethereum Virtual Machine (EVM) bytecode, which is executed on the Ethereum network.

In Solidity, developers can define custom data structures, create user-defined functions, and implement access control mechanisms, among other features. With Solidity, it is possible to create a wide array of decentralized applications, including decentralized finance (DeFi) platforms, non-fungible tokens (NFTs), decentralized autonomous organizations (DAOs), and more.

Structure of a Solidity contract: Example

A Solidity contract is a self-contained piece of code that consists of variables, functions, events, and modifiers, among other elements. The basic structure of a Solidity contract is as follows:

  1. Pragma directive: This line specifies the compatible Solidity compiler version for the smart contract. For example:

    Solidity
    pragma solidity ^0.8.0;
    
  2. Contract definition: This line declares the contract and its name. \
    For instance:

    Solidity
    contract SimpleToken {
    
  3. State variables: These are variables that store the contract’s state on blockchain. For example, you can store the total supply of a token:

    Solidity
    uint256 public totalSupply;
    
  4. Functions: Functions define the contract’s behavior and can be called by external users or other contracts. Here’s a simple function to transfer tokens:

    Solidity
    function transfer(address recipient, uint256 amount) public {
     // ... transfer logic ...
    }
    
  5. Events: Events are used to log specific actions in the contract and can be monitored by external users or contracts. For instance, a Transfer event in a token contract:

    Solidity
    event Transfer(address indexed from, address indexed to, uint256 value);
    
  6. Modifiers: Modifiers are used to modify the behavior of functions, usually for access control or precondition checks. For example, a modifier to check if the sender has enough tokens to transfer:

    JavaScript
    modifier hasEnoughTokens(uint256 amount) {
     require(balanceOf[msg.sender] >= amount, "Insufficient balance");
     _;
    }
    
  7. Constructor: The constructor is a special function that initializes the contract’s state variables when it is deployed. For example, initializing the total supply of a token:

    JavaScript
    constructor(uint256 _totalSupply) {
     totalSupply = _totalSupply;
     balanceOf[msg.sender] = _totalSupply;
    }
    

Putting it all together, a simple Solidity contract for a token might look like this:

TypeScript
pragma solidity ^0.8.0;

contract SimpleToken {
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 value);

    modifier hasEnoughTokens(uint256 amount) {
        require(balanceOf[msg.sender] >= amount, "Insufficient balance");
        _;
    }
}

Highlights
Ethereum is a blockchain-based platform that supports smart contracts and decentralized applications (dApps).
Ethereum’s native cryptocurrency is Ether (ETH).
Solidity is a high-level programming language used for writing smart contracts on Ethereum.
Smart contracts on Ethereum execute automatically based on predefined conditions.
Ethereum has transitioned to a Proof-of-Stake (PoS) mechanism for validating transactions and creating new blocks.
Solidity allows developers to define data structures, create functions, implement access control, and more.
Ethereum’s ecosystem attracts developers and organizations from various industries.
Solidity contracts consist of pragmas, contract definition, state variables, functions, events, modifiers, and constructors.
Solidity contracts can be used for creating DeFi platforms, NFTs, DAOs, and more.

Isenção de responsabilidade
* O investimento em criptomoedas envolve grandes riscos. Prossiga com cautela. O curso não se destina a servir de orientação para investimentos.
* O curso foi criado pelo autor que entrou para o Gate Learn. As opiniões compartilhadas pelo autor não representam o Gate Learn.