Commit 6b2439a5 authored by Chris's avatar Chris
Browse files

Solidity part 3 complete

parent 7cc209f2
Loading
Loading
Loading
Loading
+35 −1
Original line number Diff line number Diff line
@@ -65,4 +65,38 @@ Covers variable types

## Part 3

continuing usage of variables with more interesting concepts
continuing usage of variables with more interesting concepts, covering the `address` type

### Video link

https://www.youtube.com/watch?v=AFma5JYgIIE&list=PLvfQp12V0hS2PQd9-X-E2AjmXj1o05WOo&index=4

### Notes

- address types were created, but they should properly be typecasted (are they really primitives then...?)
  - `address public myAddress = address(...)`
- basically it's a good idea to typecast things into other thigns
  - if you have an int8 and you want to make it into an int256, you just assign the int256 variable to int256(the int8 value)
- the `address` type has methods:
  - `uint256 balanceOfMyContract = myContractAddress.balance`
- you can declare an address variable as `payable`
  - `address payable myContractAddress = payable(...)`
  - `myContractAddress.transfer(value)`
- `this` keyword
  - refers to this contract's address
  - `address myContractAddress = address(this)`
- a state variable is any variable declared within the smart contract and not public
  - stays with the smart contract and live forever on the blockchain as bytecode
  - the VM can update and change state, which can occur via functions
- a local variable is any variable declared within a function: they live and breathe inside the function and are not part of the contract's state
- global variables are variables inside `block` and `msg` objects
  - smart contract has access to global variables because these variables exist on the blockchain
  - `uint256 time = block.timestamp; // state variable saving a global variable`
  - `address senderAddress = msg.sender;`
- more chatter on variable access: you can access state vars in functions, parent doesn't necessarily always have access inside a function unless it's being returned
- sometimes better to start coding, finding variable access is a feel thing too (according to this video lol)
- smart contracts can access some of other contracts' variables
- when you have one smart contract inheriting another, you use the `is` keyword
  - inheritance is done by merging two smart contracts together at compilation time
- child contracts will not have access to `private` variables of the parent contract, but it will have access to `internal` ones
+22 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyVariables {
    uint256 public number = 20; // state variable
    bool private paused = true; // state variable
    uint256 internal time = block.timestamp; // state variable saving a global variable

    function myFunc() view public {
        // below lines are commented because they're unused currently
        // uint256 localNumber = 20; // local variable
        // bool localPaused = true; // local variable
        // address theSenderAddress = msg.sender;
    }

}

contract INeedVariables is MyVariables {
    uint256 public theAwesomeVar = 100;
    uint256 public theTime = time;
}
 No newline at end of file