Commit 5a1a37ef authored by Chris's avatar Chris
Browse files

Solidity completed part 10

parent 72eadf9e
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -290,7 +290,26 @@ covers interfaces
- you can make constructors and functions payable if you want to send ether to them
  - allows for extra functionality
- if a constructor is payable, you have to send the ether with the contract deployment
- 
- `receive() external payable {}` and `fallback() external payable` are special functions:
  - they do not require a `function` keyword in front of declaration
  - receive is called, and body executed, once a person makes a transaction to the smart contract without including any data
    - data field is missing? execute `receive()`
  - fallback() is called if data is specified, but something is wrong with the data
    - no way for the function to execute, but data was included
- I guess you can think of fallback() and receive() as exception handling in a way, like a bad request
- safety net is a good idea if you are making a payable smart contract
- these functions only have so much gas to work with, so don't do anything big
- if there is no withdraw function, the ether will be stuck in the smart contract forever
- reference to Contracts portion of official docs
- different ways to transfer ether
  - transfer
  - send
  - call
- we already know how to accept ether into a smart contract, so let's see how to do the opposite
- `transfer()` and `send()` are not recommended in the latest version of Solidity
- `transfer()` appears to be a void function
- `send()` returns a boolean
- best practice is to use `call()` method

## Part 11

@@ -300,11 +319,4 @@ covers interfaces

### Part 11 notes

## Part 12

### Part 12 video link

### Part 12 related .sol files

### Part 12 notes
+27 −1
Original line number Diff line number Diff line
@@ -19,4 +19,30 @@ contract MyPayable {
        uint256 amount = address(this).balance;
        return amount; 
    }

    // function is payable because money is going out, _user is payable because money is going in
    function transferEth(address payable _user) public payable {
        _user.transfer(msg.value);
    }

    function sendEth(address payable _user) public payable {
        bool successfulSend = _user.send(msg.value);
        rquire(didSend, "the transaction failed to send");
    }

    function callEth(address payable _user) public payable {
        // if you want, you can actually omit the `data` variable by doing this:
        // (bool successfulSend, ) = _user.call{value: msg.value}("");
        // I want to keep data though, for example completeness:
        (bool successfulSend, bytes memory data) = _user.call{value: msg.value}("");
        require(didSend, "the transaction failed to send");
        
    }


    receive() external payable {}

    fallback() external payable {}


}