2. Solidity Basics

// SPDX-License-Identifier: MIT

// Specify solidity version here
// ^ means any version greater than specified one will work
// >=0.8.7 <0.9.0
pragma solidity ^0.8.7;

// EVM, Ethereum Virtual Machine
// We can deploy solidity code to all evm compatible blockchains
// Avalanche, Fantom, Polygon
contract SimpleStorage {
    // DATA TYPES
    // boolean, uint, int, string, address, bytes

    // This gets initialized to zero!
    uint256 public favNumber;

    // FUNCTIONS
    function store(uint256 _favNumber) public {
        favNumber = _favNumber;
    }
    // VISIBILITY SPECIFIERS
    // 1. public   : visible externally and internally, creates a getter ftn for storage/state vars
    // 2. private  : only visible in current contract, not visible in derived contracts
    // 3. external : only for ftns | only accessible externally not within contracts | can only be called internally via `this.ftn`
    // 4. internal : only visible internally or derived contracts. They cannot be accessed externally. (default visibility)
    // FUNCTION KEYWORDS WHICH DONT SPEND GAS
    // 1. view : ensure they read something from the blockchain state and do not modify
    // 2. pure : ensure they they disallow you to read or modify from blockchains state
    // Note that if a gas calling ftn calls view or pure ftn - then it will cost gas

    function getOnePlusOne() public pure returns(uint256) {
        return(1+1);
    }
    // STRUCTS
    // People public person = People({favNum: 2, name: "Joe"});

    struct People {
        uint256 favNum;
        string name;
    }

    // ARRAYS
    // 1. dynamic array
    // 2. fixed size array
    People[] public people;

    function addPerson(string memory _name, uint256 _favNumber) public {
        // People memory newPerson = People({favNum: _favNumber, name: _name});
        // people.push(newPerson);

        // People memory newPerson = People(_favNumber, _name);
        // people.push(newPerson);

        people.push(People(_favNumber, _name));
    }
    // Places you can store and access data
    // 1. calldata : variable only exists temporarily inside ftn | can be modified
    // 2. memory   : variable only exists temporarily inside ftn | can't be modified
    // 3. storage  : also exists outside function scope | default type | permenant vars that can be modified | can't be put as ftn params
    // These are only required for array, struct or mapping types
    // Others
    // 4. code
    // 5. logs
    // 6. stack
}

Last updated