Solidity
pragma solidity ^0.4.18;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
contract Fallback is Ownable {
mapping(address => uint) public contributions;
function Fallback() public {
contributions[msg.sender] = 1000 * (1 ether);
}
function contribute() public payable {
require(msg.value < 0.001 ether);
contributions[msg.sender] += msg.value;
if(contributions[msg.sender] > contributions[owner]) {
owner = msg.sender;
}
}
function getContribution() public view returns (uint) {
return contributions[msg.sender];
}
function withdraw() public onlyOwner {
owner.transfer(this.balance);
}
function() payable public {
require(msg.value > 0 && contributions[msg.sender] > 0);
owner = msg.sender;
}
}
What I learned From It
每個合約裡最多能有一個未命名的公開 funtion,沒有參數、回傳值。
在幾種情況下會執行:
- 呼叫合約的函式不在 abi 裡。
- 對合約轉帳 (不帶 data 參數)。
只有 payable 的 Function 才能接收 Ether.
Reference: