Skip to content

APU-Blockchain-Crypto-Club/web3_101_solidity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Web3 101 Solidity Workshop

Welcome to the introductory workshop on Solidity 👨‍💻

Today, we'll explore the basics of smart contracts on the Ethereum blockchain and get our hands dirty with some real coding. Don't worry if you're completely new to this - we're going to start from the ground up. 😁

Introduction to Ethereum and Smart Contracts

What is Ethereum? Ethereum is a decentralized, open-source blockchain system featuring smart contract functionality. Often referred to as a "world computer", Ethereum provides a platform where developers can build and deploy decentralized applications (DApps). Unlike Bitcoin, which is primarily a digital currency, Ethereum is a broader platform for developing a wide range of decentralized applications.

Ethereum's native cryptocurrency, Ether (ETH), is used to facilitate transactions on the platform. It acts as a 'fuel' for operating the distributed application platform. Ethereum has become a hub for numerous decentralized finance (DeFi) applications, NFTs (Non-Fungible Tokens), and other blockchain-based projects.

What are Smart Contracts?

At the heart of Ethereum's functionality are smart contracts. A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. These contracts run on the blockchain, so they are stored on a public database and cannot be changed. The transactions that happen in a smart contract are processed by the blockchain, which means they can be sent automatically without a third party.

Key features of smart contracts:

- Autonomy: Once deployed, they operate without further intervention from the participants.
- Trustless: The decentralized and transparent nature of the blockchain means trust is built into the system.
- Backup: Data is duplicated across the blockchain, mitigating the risk of data loss.
- Safety: Encrypted and secure, reducing the risk of hacks or fraud.
- Speed: Automated processes significantly reduce the time of transactions.

Smart contracts are written in a programming language called Solidity, which is specifically designed for Ethereum to create these contracts.

Introduction to Solidity

What is Solidity?

Solidity is a high-level, object-oriented programming language designed specifically for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum. Developed by Ethereum's team, its syntax is heavily influenced by JavaScript, C++, and Python, making it relatively easy to understand for those familiar with any of these languages.

Key Features of Solidity

- Statically Typed: Solidity is a statically-typed language, meaning the data type of each variable (integer, string, array, etc.) must be specified.
- EVM Compatibility: Solidity is designed to compile code for the Ethereum Virtual Machine (EVM), the runtime environment for Ethereum smart contracts.
- Smart Contract Oriented: It offers various features like inheritance, libraries, and user-defined types to facilitate robust smart contract development.
- Security Focused: Given the immutable and high-value nature of blockchain, Solidity includes various features to handle security vulnerabilities and threats.

Basic Structure of a Solidity Contract 📝

// Specifies the Solidity version. This is to ensure that the contract does not get compiled with a newer compiler version that might introduce changes.
pragma solidity ^0.8.0;

// Contract declaration. Think of this like a class in object-oriented programming.
contract GMWorld {

    // State variable to store a number. Stored on the blockchain.
    uint private storedNumber;

    // Event declaration - used for logging activity. Clients can listen for these events.
    event NumberChanged(uint newNumber);

    // Constructor - called when the contract is deployed. Initializes contract state.
    constructor() {
        storedNumber = 5; // Setting an initial value
    }

    // Public function to store a number. The 'public' keyword makes it callable from outside the contract.
    function storeNumber(uint _number) public {
        storedNumber = _number;

        // Emitting an event to log the action.
        emit NumberChanged(_number);
    }

    // Public function to retrieve the stored number. 
    // 'view' means it only reads from the blockchain and does not modify the state.
    // 'returns' specifies the return type.
    function retrieveNumber() public view returns (uint) {
        return storedNumber;
    }
}

This example demonstrates a simple Solidity contract. It has a state variable, a constructor, two functions, and an event. The storeNumber function allows a number to be stored, and retrieveNumber allows it to be retrieved. The NumberChanged event is emitted whenever the number is changed, which can be useful for front-end applications interacting with this contract.

Check out more at https://solidity-by-example.org/

Buidl Your own Token Contract 🚀

// Specifies the version of Solidity.
pragma solidity ^0.8.0;

// SimpleToken contract that creates a basic ERC-20 token.
contract GMToken {
    string public name = "GMToken";
    string public symbol = "GMT";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    // Records balances of each account.
    mapping(address => uint256) public balanceOf;

    // Event that is emitted when a transfer is made.
    event Transfer(address indexed from, address indexed to, uint256 value);

    // Constructor sets the total supply and assigns it to transaction sender.
    constructor(uint256 _initialSupply) {
        totalSupply = _initialSupply * (10 ** uint256(decimals));
        balanceOf[msg.sender] = totalSupply;
        emit Transfer(address(0), msg.sender, totalSupply);
    }

    // Function to transfer tokens to a given address.
    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value, "Insufficient balance");
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }
}

Using These Samples

  1. Open up Remix, a no install Ethereum development environment in your browser.
  2. Copy the Solidity code samples. compile and then deploy the sample contracts.
  3. Interact with the contract's functions under the Deployed Contracts tab.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published