Merge pull request #119 from adklempner/refactorDayLimit
Decouple Shareable from DayLimit
This commit is contained in:
@ -11,7 +11,7 @@ import './Shareable.sol';
|
||||
* on a particular resource per calendar day. is multiowned to allow the limit to be altered. resource that method
|
||||
* uses is specified in the modifier.
|
||||
*/
|
||||
contract DayLimit is Shareable {
|
||||
contract DayLimit {
|
||||
// FIELDS
|
||||
|
||||
uint public dailyLimit;
|
||||
@ -38,13 +38,13 @@ contract DayLimit is Shareable {
|
||||
|
||||
// METHODS
|
||||
|
||||
// (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
|
||||
function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external {
|
||||
// (re)sets the daily limit. doesn't alter the amount already spent today.
|
||||
function _setDailyLimit(uint _newLimit) internal {
|
||||
dailyLimit = _newLimit;
|
||||
}
|
||||
|
||||
// resets the amount already spent today. needs many of the owners to confirm
|
||||
function resetSpentToday() onlymanyowners(sha3(msg.data)) external {
|
||||
// resets the amount already spent today.
|
||||
function _resetSpentToday() internal {
|
||||
spentToday = 0;
|
||||
}
|
||||
|
||||
@ -53,14 +53,14 @@ contract DayLimit is Shareable {
|
||||
|
||||
// checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and
|
||||
// returns true. otherwise just returns false.
|
||||
function underLimit(uint _value) internal onlyOwner returns (bool) {
|
||||
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
|
||||
// overflow protection // dailyLimit check
|
||||
if (spentToday + _value >= spentToday && spentToday + _value <= dailyLimit) {
|
||||
spentToday += _value;
|
||||
return true;
|
||||
|
||||
@ -83,6 +83,14 @@ contract MultisigWallet is Multisig, Shareable, DayLimit {
|
||||
}
|
||||
}
|
||||
|
||||
function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external {
|
||||
_setDailyLimit(_newLimit);
|
||||
}
|
||||
|
||||
function resetSpentToday() onlymanyowners(sha3(msg.data)) external {
|
||||
_resetSpentToday();
|
||||
}
|
||||
|
||||
|
||||
// INTERNAL METHODS
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import "../DayLimit.sol";
|
||||
contract DayLimitMock is DayLimit {
|
||||
uint public totalSpending;
|
||||
|
||||
function DayLimitMock(uint _value, address[] _owners, uint _required) DayLimit(_value) Shareable(_owners, _required) {
|
||||
function DayLimitMock(uint _value) DayLimit(_value) {
|
||||
totalSpending = 0;
|
||||
}
|
||||
|
||||
@ -12,4 +12,12 @@ contract DayLimitMock is DayLimit {
|
||||
totalSpending += _value;
|
||||
}
|
||||
|
||||
function setDailyLimit(uint _newLimit) external {
|
||||
_setDailyLimit(_newLimit);
|
||||
}
|
||||
|
||||
function resetSpentToday() external {
|
||||
_resetSpentToday();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,14 +2,14 @@ contract('DayLimit', function(accounts) {
|
||||
|
||||
it('should construct with the passed daily limit', async function() {
|
||||
let initLimit = 10;
|
||||
let dayLimit = await DayLimitMock.new(initLimit, accounts, 2);
|
||||
let dayLimit = await DayLimitMock.new(initLimit);
|
||||
let dailyLimit = await dayLimit.dailyLimit();
|
||||
assert.equal(initLimit, dailyLimit);
|
||||
});
|
||||
|
||||
it('should be able to spend if daily limit is not reached', async function() {
|
||||
let limit = 10;
|
||||
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||
let dayLimit = await DayLimitMock.new(limit);
|
||||
|
||||
await dayLimit.attemptSpend(8);
|
||||
let spentToday = await dayLimit.spentToday();
|
||||
@ -22,7 +22,7 @@ contract('DayLimit', function(accounts) {
|
||||
|
||||
it('should prevent spending if daily limit is reached', async function() {
|
||||
let limit = 10;
|
||||
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||
let dayLimit = await DayLimitMock.new(limit);
|
||||
|
||||
await dayLimit.attemptSpend(8);
|
||||
let spentToday = await dayLimit.spentToday();
|
||||
@ -35,7 +35,7 @@ contract('DayLimit', function(accounts) {
|
||||
|
||||
it('should allow spending if daily limit is reached and then set higher', async function() {
|
||||
let limit = 10;
|
||||
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||
let dayLimit = await DayLimitMock.new(limit);
|
||||
|
||||
await dayLimit.attemptSpend(8);
|
||||
let spentToday = await dayLimit.spentToday();
|
||||
@ -53,7 +53,7 @@ contract('DayLimit', function(accounts) {
|
||||
|
||||
it('should allow spending if daily limit is reached and then amount spent is reset', async function() {
|
||||
let limit = 10;
|
||||
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||
let dayLimit = await DayLimitMock.new(limit);
|
||||
|
||||
await dayLimit.attemptSpend(8);
|
||||
let spentToday = await dayLimit.spentToday();
|
||||
|
||||
Reference in New Issue
Block a user