1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- pragma solidity ^0.4.8;
- /**
- * @title DayLimit
- * @dev Base contract that enables methods to be protected by placing a linear limit (specifiable) on a particular resource per calendar day. Is multiowned to allow the limit to be altered
- */
- contract DayLimit {
- uint public dailyLimit;
- uint public spentToday;
- uint public lastDay;
- /**
- * @dev Constructor that sets the passed value as a dailyLimit
- * @param _limit Uint to represent the daily limit.
- */
- function DayLimit(uint _limit) {
- dailyLimit = _limit;
- lastDay = today();
- }
- /**
- * @dev sets the daily limit. doesn't alter the amount already spent today
- * @param _newLimit Uint to represent the new limit.
- */
- function _setDailyLimit(uint _newLimit) internal {
- dailyLimit = _newLimit;
- }
- /**
- * @dev Resets the amount already spent today.
- */
- function _resetSpentToday() internal {
- spentToday = 0;
- }
- /**
- * @dev Checks to see if there is enough resource to spend today. If true, the resource is expended.
- * @param _value Uint representing the amout of resurce to spend.
- * @return A boolean that is True if the resource was spended and false otherwise.
- */
- function underLimit(uint _value) internal returns (bool) {
- // reset the spend limit if we're on a different day to last time.
- if (today() > lastDay) {
- spentToday = 0;
- lastDay = today();
- }
- // check to see if there's enough left - if so, subtract and return true.
- // overflow protection // dailyLimit check
- if (spentToday + _value >= spentToday && spentToday + _value <= dailyLimit) {
- spentToday += _value;
- return true;
- }
- return false;
- }
- /**
- * @dev Private function to determine today index
- * @return Uint of todays index.
- */
- function today() private constant returns (uint) {
- return now / 1 days;
- }
- /**
- * @dev Simple modifier for daily limit.
- */
- modifier limitedDaily(uint _value) {
- if (!underLimit(_value)) {
- throw;
- }
- _;
- }
- }
|