Browse Source

[TokenVesting] Fix vestedAmount calculation. Linearity test watches beneficiary balance over vestedAmount

Martín Triay 8 years ago
parent
commit
bd56163900
2 changed files with 7 additions and 4 deletions
  1. 4 2
      contracts/token/TokenVesting.sol
  2. 3 2
      test/TokenVesting.js

+ 4 - 2
contracts/token/TokenVesting.sol

@@ -83,7 +83,7 @@ contract TokenVesting is Ownable {
   }
 
   /**
-   * @dev Calculates the amount that has already vested.
+   * @dev Calculates the amount that has already vested but not released.
    * @param token ERC20 token which is being vested
    */
   function vestedAmount(ERC20Basic token) constant returns (uint256) {
@@ -96,8 +96,10 @@ contract TokenVesting is Ownable {
       uint256 totalBalance = currentBalance.add(released[token]);
 
       uint256 vested = totalBalance.mul(now - start).div(end - start);
+      uint256 unreleased = vested.sub(released[token]);
 
-      return Math.min256(currentBalance, vested);
+      // currentBalance can be 0 in case of a revoke
+      return Math.min256(currentBalance, unreleased);
     }
   }
 }

+ 3 - 2
test/TokenVesting.js

@@ -56,10 +56,11 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
       const now = this.cliff + i * (duration / checkpoints);
       await increaseTimeTo(now);
 
-      const vested = await this.vesting.vestedAmount(this.token.address);
+      await this.vesting.release(this.token.address);
+      const balance = await this.token.balanceOf(beneficiary);
       const expectedVesting = amount.mul(now - this.start).div(this.end - this.start).floor();
 
-      vested.should.bignumber.equal(expectedVesting);
+      balance.should.bignumber.equal(expectedVesting);
     }
   });