Commit ba380127 authored by Chris's avatar Chris
Browse files

Solidity completed part 7

parent e6e3db35
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -190,6 +190,7 @@ uint256 costOfNFT = 0.05 ether;
- Solidity gives us the saame familiar if/else statements and for/while loops as you see in other languages
- this section covers enums, arrays, mappings
- lots of good information on how minting works by mutating the contract's state
- lots of examples: check the related .sol files

## Part 7

@@ -200,10 +201,28 @@ uint256 costOfNFT = 0.05 ether;
### Part 7 related .sol files

- [MyStructs.sol](./contracts/MyStructs.sol)
- [MyFunctions.sol](./contracts/MyFunctions.sol)

### Part 7 notes

- todo fill this out
- shows how to create structs in Solidity
- when you are working with variables, there is the `memory` keyword and the `storage` keyword that can be used
  - this seems to be separate from how scope works
  - `memory` is when something can be disposed of after the contract execution is complete
  - `storage` is when a variable should be stored on the blockchain
- section covers structs and data locations
- what are data locations?
  - everything that is stored on the blockchain has storage somewhere
  - three different data locations: storage, memory, and calldata
  - **storage data** would be anything stored on the blockchain. state variables
  - **memory data** would be anything ephemeral, it's needed for functionality but it doesn't have to remain on the blockchain
  - **calldata data** is good when you are adding a list of objects
  - primitive types don't need memory/storage keywords
  - "pure" function means a function that doesn't mutate any data on the blockchain
- Solidity compiler creates what is called an **ABI** to index function signatures
- view functions depend on data from outside of the function
- in Remix, when looking at a deployed smart contract, the orange buttons cost gas and the blue ones do not
- there is another scope for functions, `external`, that can only be called from outside of the contract

## Part 8

+29 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyFunctions {
    // state/storage variables
    uint256 myUint = 42;
    string myString = "Daniel";
    bool myBool = true;
    uint256[] myArr = [3, 3, 3];

    function myPureFunc(uint256 _x, uint256 _y) pure public returns (uint256 xy) {
        return _x + _y;
    }

    function myViewFunc() view private returns (string memory) {
        return myString;
    }

    function myUpdateFunc() external returns (string memory) {
        myString = "HashLips";
        string memory savedString = myViewFunc();
        return savedString;
    }

    function myReturnsFunc() view external returns (uint256, string memory, bool, uint256[] memory) {
        return (myUint, myString, myBool, myArr);
    }
}
 No newline at end of file
+58 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyStructs {
    struct NFT {
        string name;
        uint256 dna; // <-- what is dna??
    }

    mapping(uint256 => NFT) nftMapping; // this isn't used in this contract, but this is how you make a mapping to struct values
    NFT[] public nftList;

    function addNFT(string memory _name, uint256 _dna) public {
        // commented out is the long way to create an NFT object
        // NFT memory newNFT;
        // newNFT.name = _name;
        // newNFT.dna = _dna;

        // nftList.push(newNFT);

        // here is a better way:
        NFT memory newNFT = NFT(_name, _dna);
        nftList.push(newNFT);
    }

    function addNFTs(NFT[] calldata _nfts) public {
        for (uint256 i = 0; i < _nfts.length; i++) {
            NFT memory newNFT = _nfts[i];
            nftList.push(newNFT);
        }
    }

    function updateNFTStorage(uint256 _index, string memory _newName) public {
        NFT storage nftToBeUpdated = nftList[_index]; // so this is essentially creating a pointer
        // if you used memory here instead, it would create a new NFT object in memory that you would have to push to the array
        //  (see updateNFTMemory)
        nftToBeUpdated.name = _newName;
    }

    function updateNFTMemory(uint256 _index, string memory _newName) public {
        NFT memory nftToBeUpdated = nftList[_index];
        nftToBeUpdated.name = _newName;
        nftList[_index] = nftToBeUpdated;
    }

    function getNFT(uint256 _index) public view returns(NFT memory) {
        return nftList[_index];
    }

    function getNFTName(uint256 _index) public view returns(string memory) {
        return nftList[_index].name;
    }

    function myNoReturnFunc() external {
        myBool = false;
    } 
}
 No newline at end of file