Personal Notes-Basics of Solidity-1.0.1

Importing Contracts Into Other Contracts.

Simple Storage

//SPDX-License-Identifier: MIT
pragma solidity 0.8.7; //pragma mentions the version of solidity this contract is using.

contract SimpleStorage { 
    //contract is a keyword 
    //SimpleStorage is the name of the contract

    //common datatype
    /*
    *boolean
    *uint -> uint8, uint16, uint32,.... multiples of 8
    *int
    *address
    *bytes
    *string
    */
    uint public favoriteNumber; //public allows us to view favoriteNumber;


     //functions are methods that will executes a series of instructions when called.
    function store(uint256 _favoriteNumber) public virtual {
        favoriteNumber=_favoriteNumber;
    }

    //Function Visibility Specifiers
    /* 
    * public : visible externally and internally => creates a getter funtions automatically
    * private: only visible in the current contract.
    * external: is visible to only functions
    * internally: only visible internally
    */

    //everytime we change the state, it is considered as a transaction.

    //understanding view, and pure

    //view is used to just display a state
    // you cannot update the function if its declared as view
    function retrieve() public view returns(uint256){
        return favoriteNumber;
    }

    /*
    A pure function is a funtion that executes a instruction that 
    does not read state variables
    */
    function add() pure public returns(uint16){
        return (1+1);
    }

    // keep in mind, calling a function costs gas, even if its pure/view


    //Arrays and Structs

    //A structure is a collection of states under a common name
    struct People {
        uint256 favoriteNumber;
        string name;
    }

    //assigning a value to a structure 
    //this displays a struct called person.
    People public person =People({
        favoriteNumber:21,
        name:"Aaron Rebelo"
    });
    //saving struct values
    /*
    People({
        favoriteNumber:21,
        name:"Aaron Rebelo"
    });

    */

    //Array is a way to store object of information
    //syntax: dataType[]

    //an array of type People called people
    People[] public people;
    uint256[] public values;


    // A mapping is a data structure where a key is "mapped" to a single value
    mapping(string => uint256) public nameToFavoriteNunber;

    function addPerson(uint256 _favoriteNumber, string memory _name) public {
        // we need to add memory to string, as string is actually an array of bytes.
        people.push(People({favoriteNumber:_favoriteNumber,name:_name}));


        // A mapping is a data structure where a key is "mapped" to a single value
        nameToFavoriteNunber[_name] = _favoriteNumber;
    }


    //Memory,Storage, And Calldata


    //There are 6 places you can store data in solidity
    /*
    * Stack
    * Memory -> variable will exist only temporarly => can modify value
    * Storage
    * Calldata -> variable will exist only temporarly => cannot modify value
    * Code
    * Logs
    */




}

StorageFactory.sol

//SPDX-License-Identifier:MIT

pragma solidity ^0.8.0;

import "./SimpleStorage.sol";

contract StorageFactory{
    //create a variable of type SimpleStorage(which is a contract)
 //   SimpleStorage public simpleStorage;//initial value is set to 0x000..

    SimpleStorage[] public simpleStorageArray; //stores the address of the contracts

    function createSimpleStorage() public {
      SimpleStorage simpleStorage = new SimpleStorage();
        simpleStorageArray.push(simpleStorage);
    }
    //to interact with the contract we always need 2 things.
        // Address
        // ABI - Aplication Binary Interface

    function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
        SimpleStorage simpleStorage = simpleStorageArray[_simpleStorageIndex];
        simpleStorage.store(_simpleStorageNumber);
    }

    function sfGet(uint256 _simpleStorageIndex) public view returns(uint256) {
        SimpleStorage simpleStorageArr = simpleStorageArray[_simpleStorageIndex];
        return simpleStorageArr.retrieve();
    }


}

Extra.sol

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7; //pragma mentions the version of solidity this contract is using.

//Inheritance
//I want to inherit all the functionality of Storage Factory

import "./SimpleStorage.sol";

contract ExtraStorage is SimpleStorage {


    //if we want to override an existing function

    //virtual => in order to override a function, it should be a virtual function
    //add virtual keyword to parent function
    //override

    function store(uint256 _favoriteNumber) public override {
        favoriteNumber=_favoriteNumber+5;
    }


}