DayLimit.sol 1.7 KB

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