Browse Source

Small refactor and documentation

Jorge Izquierdo 8 years ago
parent
commit
f812433706
1 changed files with 34 additions and 35 deletions
  1. 34 35
      contracts/token/VestedToken.sol

+ 34 - 35
contracts/token/VestedToken.sol

@@ -1,22 +1,23 @@
 pragma solidity ^0.4.8;
 
-
 import "./StandardToken.sol";
 import "./TransferableToken.sol";
 
 contract VestedToken is StandardToken, TransferableToken {
   struct TokenGrant {
-    address granter;
-    uint256 value;
+    address granter;     // 20 bytes
+    uint256 value;       // 32 bytes
     uint64 cliff;
     uint64 vesting;
-    uint64 start;
+    uint64 start;        // 3 * 8 = 24 bytes
     bool revokable;
-    bool burnsOnRevoke;
-  }
+    bool burnsOnRevoke;  // 2 * 1 = 2 bits? or 2 bytes?
+  } // total 78 bytes = 3 sstore per operation (32 per sstore)
 
   mapping (address => TokenGrant[]) public grants;
 
+  event NewTokenGrant(address indexed from, address indexed to, uint256 value, uint256 grantId);
+
   function grantVestedTokens(
     address _to,
     uint256 _value,
@@ -24,34 +25,32 @@ contract VestedToken is StandardToken, TransferableToken {
     uint64 _cliff,
     uint64 _vesting,
     bool _revokable,
-    bool _burnsOnRevoke) {
+    bool _burnsOnRevoke
+  ) public {
 
-    if (_cliff < _start) {
-      throw;
-    }
-    if (_vesting < _start) {
-      throw;
-    }
-    if (_vesting < _cliff) {
+    // Check for date inconsistencies that may cause unexpected behavior
+    if (_cliff < _start || _vesting < _cliff) {
       throw;
     }
 
-    grants[_to].push(
-      TokenGrant(
-        msg.sender,
-        _value,
-        _cliff,
-        _vesting,
-        _start,
-        _revokable,
-        _burnsOnRevoke
-      )
-    );
+    uint id = grants[_to].push(
+                TokenGrant(
+                  _revokable ? msg.sender : 0, // avoid storing an extra 20 bytes when it is non-revokable
+                  _value,
+                  _cliff,
+                  _vesting,
+                  _start,
+                  _revokable,
+                  _burnsOnRevoke
+                )
+              );
 
     transfer(_to, _value);
+
+    NewTokenGrant(msg.sender, _to, _value, id);
   }
 
-  function revokeTokenGrant(address _holder, uint _grantId) {
+  function revokeTokenGrant(address _holder, uint _grantId) public {
     TokenGrant grant = grants[_holder][_grantId];
 
     if (!grant.revokable) { // Check if grant was revokable
@@ -76,6 +75,15 @@ contract VestedToken is StandardToken, TransferableToken {
     Transfer(_holder, receiver, nonVested);
   }
 
+  function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
+    uint256 grantIndex = tokenGrantsCount(holder);
+    for (uint256 i = 0; i < grantIndex; i++) {
+      nonVested = safeAdd(nonVested, nonVestedTokens(grants[holder][i], time));
+    }
+
+    return min256(safeSub(balances[holder], nonVested), super.transferableTokens(holder, time));
+  }
+
   function tokenGrantsCount(address _holder) constant returns (uint index) {
     return grants[_holder].length;
   }
@@ -138,13 +146,4 @@ contract VestedToken is StandardToken, TransferableToken {
       date = max64(grants[holder][i].vesting, date);
     }
   }
-
-  function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
-    uint256 grantIndex = grants[holder].length;
-    for (uint256 i = 0; i < grantIndex; i++) {
-      nonVested = safeAdd(nonVested, nonVestedTokens(grants[holder][i], time));
-    }
-
-    return min256(safeSub(balances[holder], nonVested), super.transferableTokens(holder, time));
-  }
 }