aboutsummaryrefslogtreecommitdiffstats
path: root/test/compilationTests/zeppelin/DayLimit.sol
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-07-05 18:28:15 +0800
committerchriseth <chris@ethereum.org>2017-07-05 18:39:55 +0800
commitac84b36144f746662e5ddb984d283e053c7d06ba (patch)
treef50ee438a384e60574a4c28ea32d2b6ae2315795 /test/compilationTests/zeppelin/DayLimit.sol
parent05a26fc98c1201057c618c536ca0537e456c9b15 (diff)
downloaddexon-solidity-ac84b36144f746662e5ddb984d283e053c7d06ba.tar.gz
dexon-solidity-ac84b36144f746662e5ddb984d283e053c7d06ba.tar.zst
dexon-solidity-ac84b36144f746662e5ddb984d283e053c7d06ba.zip
Added various contracts for testing.
Diffstat (limited to 'test/compilationTests/zeppelin/DayLimit.sol')
-rw-r--r--test/compilationTests/zeppelin/DayLimit.sol75
1 files changed, 75 insertions, 0 deletions
diff --git a/test/compilationTests/zeppelin/DayLimit.sol b/test/compilationTests/zeppelin/DayLimit.sol
new file mode 100644
index 00000000..3c8d5b0c
--- /dev/null
+++ b/test/compilationTests/zeppelin/DayLimit.sol
@@ -0,0 +1,75 @@
+pragma solidity ^0.4.11;
+
+/**
+ * @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 {
+
+ uint256 public dailyLimit;
+ uint256 public spentToday;
+ uint256 public lastDay;
+
+ /**
+ * @dev Constructor that sets the passed value as a dailyLimit.
+ * @param _limit uint256 to represent the daily limit.
+ */
+ function DayLimit(uint256 _limit) {
+ dailyLimit = _limit;
+ lastDay = today();
+ }
+
+ /**
+ * @dev sets the daily limit. Does not alter the amount already spent today.
+ * @param _newLimit uint256 to represent the new limit.
+ */
+ function _setDailyLimit(uint256 _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 may be expended.
+ * @param _value uint256 representing the amount of resource to spend.
+ * @return A boolean that is True if the resource was spended and false otherwise.
+ */
+ function underLimit(uint256 _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's index
+ * @return uint256 of today's index.
+ */
+ function today() private constant returns (uint256) {
+ return now / 1 days;
+ }
+
+ /**
+ * @dev Simple modifier for daily limit.
+ */
+ modifier limitedDaily(uint256 _value) {
+ if (!underLimit(_value)) {
+ throw;
+ }
+ _;
+ }
+}