From 44138018dc5ea15dae4a998272ff42bd6d982972 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 17 Nov 2016 18:34:53 -0300 Subject: [PATCH 1/2] limit funds improvements --- contracts/LimitBalance.sol | 18 ++++++++++ contracts/LimitFunds.sol | 12 ------- contracts/test-helpers/LimitBalanceMock.sol | 10 ++++++ test/LimitBalance.js | 38 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 contracts/LimitBalance.sol delete mode 100644 contracts/LimitFunds.sol create mode 100644 contracts/test-helpers/LimitBalanceMock.sol create mode 100644 test/LimitBalance.js diff --git a/contracts/LimitBalance.sol b/contracts/LimitBalance.sol new file mode 100644 index 000000000..f877512e0 --- /dev/null +++ b/contracts/LimitBalance.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.4; +contract LimitBalance { + + uint public limit; + + function LimitBalance(uint _limit) { + limit = _limit; + } + + modifier limitedPayable() { + if (this.balance + msg.value > limit) { + throw; + } + _; + + } + +} diff --git a/contracts/LimitFunds.sol b/contracts/LimitFunds.sol deleted file mode 100644 index 303444abc..000000000 --- a/contracts/LimitFunds.sol +++ /dev/null @@ -1,12 +0,0 @@ -pragma solidity ^0.4.4; -contract LimitFunds { - - uint LIMIT = 5000; - - function() { throw; } - - function deposit() { - if (this.balance > LIMIT) throw; - } - -} diff --git a/contracts/test-helpers/LimitBalanceMock.sol b/contracts/test-helpers/LimitBalanceMock.sol new file mode 100644 index 000000000..c0f05ba8b --- /dev/null +++ b/contracts/test-helpers/LimitBalanceMock.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.4.4; +import '../LimitBalance.sol'; + +// mock class using LimitBalance +contract LimitBalanceMock is LimitBalance(1000) { + + function limitedDeposit() payable limitedPayable { + } + +} diff --git a/test/LimitBalance.js b/test/LimitBalance.js new file mode 100644 index 000000000..2afcd2da2 --- /dev/null +++ b/test/LimitBalance.js @@ -0,0 +1,38 @@ +contract('LimitBalance', function(accounts) { + var lb; + + beforeEach(function() { + return LimitBalanceMock.new().then(function(deployed) { + lb = deployed; + }); + }); + + var LIMIT = 1000; + + it("should expose limit", function(done) { + return lb.limit() + .then(function(limit) { + assert.equal(limit, LIMIT); + }) + .then(done) + }); + + it("should allow sending below limit", function(done) { + var amount = 1; + return lb.limitedDeposit({value: amount}) + .then(function() { + assert.equal(web3.eth.getBalance(lb.address), amount); + }) + .then(done) + }); + + it("shouldnt allow sending above limit", function(done) { + var amount = 1100; + return lb.limitedDeposit({value: amount}) + .catch(function(error) { + if (error.message.search('invalid JUMP') == -1) throw error + }) + .then(done) + }); + +}); From 4cd8aa7900127bd3498249e9b9ed49979ca60769 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 18 Nov 2016 12:33:52 -0300 Subject: [PATCH 2/2] add more limit tests to LimitBalance --- contracts/LimitBalance.sol | 2 +- test/LimitBalance.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/contracts/LimitBalance.sol b/contracts/LimitBalance.sol index f877512e0..d8c29baec 100644 --- a/contracts/LimitBalance.sol +++ b/contracts/LimitBalance.sol @@ -8,7 +8,7 @@ contract LimitBalance { } modifier limitedPayable() { - if (this.balance + msg.value > limit) { + if (this.balance > limit) { throw; } _; diff --git a/test/LimitBalance.js b/test/LimitBalance.js index 2afcd2da2..ce75821a0 100644 --- a/test/LimitBalance.js +++ b/test/LimitBalance.js @@ -35,4 +35,30 @@ contract('LimitBalance', function(accounts) { .then(done) }); + it("should allow multiple sends below limit", function(done) { + var amount = 500; + return lb.limitedDeposit({value: amount}) + .then(function() { + assert.equal(web3.eth.getBalance(lb.address), amount); + return lb.limitedDeposit({value: amount}) + }) + .then(function() { + assert.equal(web3.eth.getBalance(lb.address), amount*2); + }) + .then(done) + }); + + it("shouldnt allow multiple sends above limit", function(done) { + var amount = 500; + return lb.limitedDeposit({value: amount}) + .then(function() { + assert.equal(web3.eth.getBalance(lb.address), amount); + return lb.limitedDeposit({value: amount+1}) + }) + .catch(function(error) { + if (error.message.search('invalid JUMP') == -1) throw error; + }) + .then(done) + }); + });