Forráskód Böngészése

limit funds improvements

Manuel Araoz 9 éve
szülő
commit
44138018dc

+ 18 - 0
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;
+    }
+    _;
+    
+  }
+
+}

+ 0 - 12
contracts/LimitFunds.sol

@@ -1,12 +0,0 @@
-pragma solidity ^0.4.4;
-contract LimitFunds {
-
-  uint LIMIT = 5000;
-
-  function() { throw; }
-
-  function deposit() {
-    if (this.balance > LIMIT) throw;
-  }
-
-}

+ 10 - 0
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 {
+  }
+
+}

+ 38 - 0
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)
+  });
+
+});