Commit d24e0b37 authored by Chris's avatar Chris
Browse files

Solidity completed part 9

parent eaeef7f7
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyModifier {

    uint256 public myNum = 0;
    bool public paused = false;
    address public owner;

    constructor() {
        owner = address(msg.sender);
    }

    // modifier will allow for reuse of require()
    modifier isNotPaused(bool _bypass) {
        if (!_bypass) {
           require(paused == false, "paused must be false to execute this function");
        }
        _; // the rest of the code that should execute after the require
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "contract owner must execute this function");
        _;
    }

    function setPaused() public {
        paused = true;
    }

    function addToNum() public isNotPaused(true) onlyOwner {
        myNum++;
    }

    function subFromNum() public isNotPaused(false) onlyOwner {
        myNum--;
    }

}
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
@@ -248,8 +248,21 @@ errors and checks

### Part 9 related .sol files

- [MyModifier.sol](./contracts/MyModifier.sol)
- [MyEvents.sol](./contracts/MyEvents.sol)
- [MyConstructor.sol](./contracts/MyConstructor.sol)
- [MyInheritance.sol](./contracts/MyInheritance.sol)

### Part 9 notes

- modifiers help us facilitate functions
  - tell functions what they can and cannot do
  - like using a "require" statement but making it reusable for other functions as well
- an `indexed` variable will let you filter by that value in the logs
- inheritance: when a smart contract inherits from another one, it means that the child contract gets all of the attributes, methods, functionality that is public or internal
- you can inherit from multiple parent contracts in Solidity
- like in C#, the `virtual` function allows for a function to be overridden by child contracts

## Part 10

### Part 10 video link
+25 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyConstructor {

    string public name;

    // constructor is called exactly once and it runs on deploy
    // doesn't need a name && only one constructor can exist (but it is optional to have a constructor in a contract)

    // constructor() {
    //     name = "HashLips";
    // }

    // if you have a parameterized constructor, you have to fill the unknown parameter values out before you're allowed to deploy
    constructor(string memory _name) {
        name = _name;
    }
}

contract MySecondConstructorContract is MyConstructor {
    // this below code is to perform inheritance with a constructor override (in this case, it's just implemented for inheritance: no extra code is added)
    constructor(string memory _name) MyConstructor(_name) {}
}
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyEvents {

    event CreatedNFT(address indexed user, uint256 id, uint256 dna);

    function createNFT(uint256 _id, uint256 _dna) public {
        // create the NFT and save to the storage
        emit CreatedNFT(msg.sender, _id, _dna);

    }
    
    
}
 No newline at end of file
+24 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyInheritance_A {
    string internal name = "HashLips";
}

contract MyInheritance_B is MyInheritance_A {
    // B should have access to A's `name` variable
    // we can expose that internal variable by returning it from a public function
    function getName() public view virtual returns (string memory) {
        return name;
    }
}

contract MyInheritance_C is MyInheritance_B {
    // C has access to all of B's attributes
    // you would inherit by doing `contract MyInheritance_C is MyInheritance_A, MyInheritance_B {...}`

    function getName() public view virtual override returns (string memory) {
        return "Ha ha, you deployed the wrong contract! You will never know the name now.";
    }
}
 No newline at end of file
Loading