Skip to content

Commit

Permalink
fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
t4sk committed Feb 3, 2020
1 parent f01fbc7 commit b82d3a6
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions video-scripts/fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
```
pragma solidity ^0.5.11;
/*
Fallback function
- unamed function
- cannot take any inputs, cannot return any outputs
- must be external
- executed when
- calling a function that does not exist (call video)
- receiving Ether (NOTE: not executed when calling another payable function) (must be payable)
- except coinbase (TODO: explain coinbase)
- sent via
- account (wallet), call (no gas limit)
- send / transfer (forwards 2300 gas)
- 2300 gas is not enough to
- Write to storage
- Create a contract
- Call an external function which consumes a large amount of gas
- Sending Ether
- TODO: briefly explain re-entrancy and how 2300 gas prevents it
*/
contract Fallback {
event Log(uint gas);
// NOTE: unnamed
// NOTE: no input no output
// NOTE: external
// NOTE: payable
function () external payable {
// TODO: gasleft
emit Log(gasleft());
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function callFallback(address payable _to) public payable {
// NOTE: recommended
// NOTE: forwards all gas
// NOTE: briefly explain re-entrancy
(bool sent,) = _to.call.value(msg.value)("");
require(sent, "Failed to send Ether");
}
function transferToFallback(address payable _to) public payable {
// NOTE: no longer recommended
// NOTE: forwards 2300 gas
_to.transfer(msg.value);
}
}
/*
DEMO
- callFallback (check gas)
- transferToFallback (check gas)
*/
```

0 comments on commit b82d3a6

Please sign in to comment.