DayLimit.sol 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. pragma solidity ^0.4.8;
  2. import './ownership/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 {
  11. uint public dailyLimit;
  12. uint public spentToday;
  13. uint public lastDay;
  14. function DayLimit(uint _limit) {
  15. dailyLimit = _limit;
  16. lastDay = today();
  17. }
  18. // sets the daily limit. doesn't alter the amount already spent today
  19. function _setDailyLimit(uint _newLimit) internal {
  20. dailyLimit = _newLimit;
  21. }
  22. // resets the amount already spent today.
  23. function _resetSpentToday() internal {
  24. spentToday = 0;
  25. }
  26. // checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and
  27. // returns true. otherwise just returns false.
  28. function underLimit(uint _value) internal returns (bool) {
  29. // reset the spend limit if we're on a different day to last time.
  30. if (today() > lastDay) {
  31. spentToday = 0;
  32. lastDay = today();
  33. }
  34. // check to see if there's enough left - if so, subtract and return true.
  35. // overflow protection // dailyLimit check
  36. if (spentToday + _value >= spentToday && spentToday + _value <= dailyLimit) {
  37. spentToday += _value;
  38. return true;
  39. }
  40. return false;
  41. }
  42. // determines today's index.
  43. function today() private constant returns (uint) {
  44. return now / 1 days;
  45. }
  46. // simple modifier for daily limit.
  47. modifier limitedDaily(uint _value) {
  48. if (underLimit(_value)) {
  49. _;
  50. }
  51. }
  52. }