pragma solidity ^0.8.10;
contract Fallback {
event Log(uint gas);
// Fallback function must be declared as external.
fallback() external payable {
// send / transfer (forwards 2300 gas to this fallback function)
// call (forwards all of the gas)
emit Log(gasleft());
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function transferToFallback(address payable _to) public payable {
_to.transfer(msg.value);
}
function callFallback(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
Fallback Function 有以下的特性:
可視性只能為external
當我們沒有任何payable 的 Function 符合調用,那就會觸發例外處理(exception),除非我們擁有 Fallback function
Fallback Function 就像catch,當我們沒有和任何payable function互動,或沒有任何函式符合交易的encoded data field,就會觸發
pragma solidity ^0.8.11;
contract FunctionsExample{
mapping(address => uint) public balanceReceived;
address payable owner;
constructor() public {
owner = payable(msg.sender);
}
function getOwner () public view returns(address){
return owner;
}
function convertWeiToEther(uint _amountInWei) public pure returns(uint){
return _amountInWei / 1 ether;
// alternatively:
// return _amountInWei / 10**18;
// pure function call only interacte with the variables in this scope like _amountInWei, but not the state variables outside the scope
}
function destroyContract() public {
require(msg.sender == owner, "You are not the owner");
selfdestruct(owner);
}
function receiveMoney() public payable {
assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]);
balanceReceived[msg.sender] += msg.value;
}
function withdrawMoney(address payable _to, uint _amount) public {
require(balanceReceived[msg.sender] <= _amount, "You don't have enough ether");
assert(balanceReceived[msg.sender] >= balanceReceived[msg.sender] - _amount);
balanceReceived[msg.sender] -= _amount;
_to.transfer(_amount);
}
fallback () external payable{
receiveMoney();
// fallback function will have input fill(in remix IDE) even we didn't declare any input arguments in function
// because the fallback function is triggered automatically no matter have arguments or not.
// arguments data is in msg.data
}
}