Browse Source

Merge pull request #78 from AugustoL/master

DelayedClaimable contract extension of Claimable fix #64
Manuel Aráoz 8 years ago
parent
commit
d54e56f970
2 changed files with 112 additions and 0 deletions
  1. 30 0
      contracts/DelayedClaimable.sol
  2. 82 0
      test/DelayedClaimble.js

+ 30 - 0
contracts/DelayedClaimable.sol

@@ -0,0 +1,30 @@
+pragma solidity ^0.4.4;
+import './Ownable.sol';
+import './Claimable.sol';
+
+/*
+ * DelayedClaimable
+ * Extension for the Claimable contract, where the ownership needs to be claimed before/after certain block number
+ */
+
+contract DelayedClaimable is Ownable, Claimable {
+
+  uint public claimBeforeBlock;
+  uint public claimAfterBlock;
+
+  function setClaimBlocks(uint _claimBeforeBlock, uint _claimAfterBlock) onlyOwner {
+    if (_claimAfterBlock > claimBeforeBlock)
+        throw;
+    claimBeforeBlock = _claimBeforeBlock;
+    claimAfterBlock = _claimAfterBlock;
+  }
+
+  function claimOwnership() onlyPendingOwner {
+    if ((block.number > claimBeforeBlock) || (block.number < claimAfterBlock))
+        throw;
+    owner = pendingOwner;
+    pendingOwner = 0x0;
+    claimBeforeBlock = 0;
+  }
+
+}

+ 82 - 0
test/DelayedClaimble.js

@@ -0,0 +1,82 @@
+contract('DelayedClaimable', function(accounts) {
+  var delayedClaimable;
+
+  beforeEach(function() {
+    return DelayedClaimable.new().then(function(deployed) {
+      delayedClaimable = deployed;
+    });
+  });
+
+  it("Changes pendingOwner after transfer succesfull", function(done) {
+    return delayedClaimable.transferOwnership(accounts[2])
+      .then(function(){
+        return delayedClaimable.setClaimBlocks(1000,0);
+      })
+      .then(function(){
+        return delayedClaimable.claimBeforeBlock();
+      })
+      .then(function(claimBeforeBlock) {
+        assert.isTrue(claimBeforeBlock == 1000);
+        return delayedClaimable.claimAfterBlock();
+      })
+      .then(function(claimAfterBlock) {
+        assert.isTrue(claimAfterBlock == 0);
+        return delayedClaimable.pendingOwner();
+      })
+      .then(function(pendingOwner) {
+        assert.isTrue(pendingOwner === accounts[2]);
+        return delayedClaimable.claimOwnership({from: accounts[2]});
+      })
+      .then(function() {
+        return delayedClaimable.owner();
+      })
+      .then(function(owner) {
+        assert.isTrue(owner === accounts[2]);
+      })
+      .then(done);
+  });
+
+  it("Changes pendingOwner after transfer fails", function(done) {
+    return delayedClaimable.transferOwnership(accounts[1])
+      .then(function(){
+        return delayedClaimable.setClaimBlocks(11000,10000);
+      })
+      .then(function(){
+        return delayedClaimable.claimBeforeBlock();
+      })
+      .then(function(claimBeforeBlock) {
+        assert.isTrue(claimBeforeBlock == 11000);
+        return delayedClaimable.claimAfterBlock();
+      })
+      .then(function(claimAfterBlock) {
+        assert.isTrue(claimAfterBlock == 10000);
+        return delayedClaimable.pendingOwner();
+      })
+      .then(function(pendingOwner) {
+        assert.isTrue(pendingOwner === accounts[1]);
+        return delayedClaimable.claimOwnership({from: accounts[1]});
+      })
+      .catch(function(error) {
+        if (error.message.search('invalid JUMP') == -1) throw error;
+      })
+      .then(function() {
+        return delayedClaimable.owner();
+      })
+      .then(function(owner) {
+        assert.isTrue(owner != accounts[1]);
+      })
+      .then(done);
+  });
+
+  it("Set claimBeforeBlock and claimAfterBlock invalid values fail", function(done) {
+    return delayedClaimable.transferOwnership(accounts[1])
+      .then(function(){
+        return delayedClaimable.setClaimBlocks(1000,10000);
+      })
+      .catch(function(error) {
+        if (error.message.search('invalid JUMP') == -1) throw error;
+      })
+      .then(done);
+  });
+
+});