HasNoEther.sol 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * @dev This tries to block incoming ether to prevent accidental loss of Ether.
  6. Should Ether end up in the contrat, it will allow the owner to reclaim
  7. this ether.
  8. * @notice Ether can still be send to this contract by:
  9. * calling functions labeled `payable`
  10. * `selfdestruct(contract_address)`
  11. * mining directly to the contract address
  12. */
  13. contract HasNoEther is Ownable {
  14. /**
  15. * @dev Constructor that rejects incoming Ether
  16. * @dev The flag `payable` 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. Alternatively we could
  21. use assembly to access msg.value.
  22. */
  23. function HasNoEther() payable {
  24. if(msg.value > 0) {
  25. throw;
  26. }
  27. }
  28. /**
  29. * @dev Disallow direct send by settings a default function without `payable`
  30. */
  31. function() external {
  32. }
  33. /**
  34. * @dev Transfer all Ether owned by the contract to the owner
  35. * @dev What if owner is itself a contract marked HasNoEther?
  36. */
  37. function reclaimEther() external onlyOwner {
  38. if(!owner.send(this.balance)) {
  39. throw;
  40. }
  41. }
  42. }