HasNoEther.sol 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. pragma solidity ^0.4.8;
  2. import "./Ownable.sol";
  3. /// @title Contracts that should not own Ether
  4. /// @author Remco Bloemen <remco@2π.com>
  5. ///
  6. /// This tries to block incoming ether to prevent accidental
  7. /// loss of Ether. Should Ether end up in the contrat, it will
  8. /// allow the owner to reclaim this ether.
  9. ///
  10. /// @notice Ether can still be send to this contract by:
  11. /// * calling functions labeled `payable`
  12. /// * `selfdestruct(contract_address)`
  13. /// * mining directly to the contract address
  14. contract HasNoEther is Ownable {
  15. /// Constructor that rejects incoming Ether
  16. /// @dev The flag `payabe` is added so we can access `msg.value`
  17. /// without compiler warning. If we leave out payable, then
  18. /// Solidity will allow inheriting contracts to implement a
  19. /// payable constructor. By doing it this way we prevent a
  20. /// payable constructor from working.
  21. /// Alternatively we could use assembly to access msg.value.
  22. function HasNoEther() payable {
  23. if(msg.value > 0) {
  24. throw;
  25. }
  26. }
  27. /// Disallow direct send by settings a default function without `payable`
  28. function() external {
  29. }
  30. /// Transfer all Ether owned by the contract to the owner
  31. /// @dev What if owner is itself a contract marked HasNoEther?
  32. function reclaimEther() external onlyOwner {
  33. if(!owner.send(this.balance)) {
  34. throw;
  35. }
  36. }
  37. }