DayLimit.sol 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. pragma solidity ^0.4.4;
  2. import './Shareable.sol';
  3. /*
  4. * DayLimit
  5. *
  6. * inheritable "property" contract that enables methods to be protected by placing a linear limit (specifiable)
  7. * on a particular resource per calendar day. is multiowned to allow the limit to be altered. resource that method
  8. * uses is specified in the modifier.
  9. */
  10. contract DayLimit is Shareable {
  11. // FIELDS
  12. uint public dailyLimit;
  13. uint public spentToday;
  14. uint public lastDay;
  15. // MODIFIERS
  16. // simple modifier for daily limit.
  17. modifier limitedDaily(uint _value) {
  18. if (underLimit(_value))
  19. _;
  20. }
  21. // CONSTRUCTOR
  22. // stores initial daily limit and records the present day's index.
  23. function DayLimit(uint _limit) {
  24. dailyLimit = _limit;
  25. lastDay = today();
  26. }
  27. // METHODS
  28. // (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
  29. function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external {
  30. dailyLimit = _newLimit;
  31. }
  32. // resets the amount already spent today. needs many of the owners to confirm
  33. function resetSpentToday() onlymanyowners(sha3(msg.data)) external {
  34. spentToday = 0;
  35. }
  36. // INTERNAL METHODS
  37. // checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and
  38. // returns true. otherwise just returns false.
  39. function underLimit(uint _value) internal onlyOwner returns (bool) {
  40. // reset the spend limit if we're on a different day to last time.
  41. if (today() > lastDay) {
  42. spentToday = 0;
  43. lastDay = today();
  44. }
  45. // check to see if there's enough left - if so, subtract and return true.
  46. // overflow protection // dailyLimit check
  47. if (spentToday + _value >= spentToday && spentToday + _value <= dailyLimit) {
  48. spentToday += _value;
  49. return true;
  50. }
  51. return false;
  52. }
  53. // determines today's index.
  54. function today() private constant returns (uint) {
  55. return now / 1 days;
  56. }
  57. }