VestedToken.sol 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. pragma solidity ^0.4.8;
  2. import "./StandardToken.sol";
  3. import "./LimitedTransferToken.sol";
  4. /**
  5. * @title Vested token
  6. * @dev Tokens that can be vested for a group of addresses.
  7. */
  8. contract VestedToken is StandardToken, LimitedTransferToken {
  9. struct TokenGrant {
  10. address granter; // 20 bytes
  11. uint256 value; // 32 bytes
  12. uint64 cliff;
  13. uint64 vesting;
  14. uint64 start; // 3 * 8 = 24 bytes
  15. bool revokable;
  16. bool burnsOnRevoke; // 2 * 1 = 2 bits? or 2 bytes?
  17. } // total 78 bytes = 3 sstore per operation (32 per sstore)
  18. mapping (address => TokenGrant[]) public grants;
  19. event NewTokenGrant(address indexed from, address indexed to, uint256 value, uint256 grantId);
  20. /**
  21. * @dev Grant tokens to a specified address
  22. * @param _to address The address which the tokens will be granted to.
  23. * @param _value uint256 The amount of tokens to be granted.
  24. * @param _start uint64 Time of the beginning of the grant.
  25. * @param _cliff uint64 Time of the cliff period.
  26. * @param _vesting uint64 The vesting period.
  27. */
  28. function grantVestedTokens(
  29. address _to,
  30. uint256 _value,
  31. uint64 _start,
  32. uint64 _cliff,
  33. uint64 _vesting,
  34. bool _revokable,
  35. bool _burnsOnRevoke
  36. ) public {
  37. // Check for date inconsistencies that may cause unexpected behavior
  38. if (_cliff < _start || _vesting < _cliff) {
  39. throw;
  40. }
  41. uint count = grants[_to].push(
  42. TokenGrant(
  43. _revokable ? msg.sender : 0, // avoid storing an extra 20 bytes when it is non-revokable
  44. _value,
  45. _cliff,
  46. _vesting,
  47. _start,
  48. _revokable,
  49. _burnsOnRevoke
  50. )
  51. );
  52. transfer(_to, _value);
  53. NewTokenGrant(msg.sender, _to, _value, count - 1);
  54. }
  55. /**
  56. * @dev Revoke the grant of tokens of a specifed address.
  57. * @param _holder The address which will have its tokens revoked.
  58. * @param _grantId The id of the token grant.
  59. */
  60. function revokeTokenGrant(address _holder, uint _grantId) public {
  61. TokenGrant grant = grants[_holder][_grantId];
  62. if (!grant.revokable) { // Check if grant was revokable
  63. throw;
  64. }
  65. if (grant.granter != msg.sender) { // Only granter can revoke it
  66. throw;
  67. }
  68. address receiver = grant.burnsOnRevoke ? 0xdead : msg.sender;
  69. uint256 nonVested = nonVestedTokens(grant, uint64(now));
  70. // remove grant from array
  71. delete grants[_holder][_grantId];
  72. grants[_holder][_grantId] = grants[_holder][grants[_holder].length.sub(1)];
  73. grants[_holder].length -= 1;
  74. balances[receiver] = balances[receiver].add(nonVested);
  75. balances[_holder] = balances[_holder].sub(nonVested);
  76. Transfer(_holder, receiver, nonVested);
  77. }
  78. /**
  79. * @dev Calculate the total amount of transferable tokens of a holder at a given time
  80. * @param holder address The address of the holder
  81. * @param time uint64 The specific time.
  82. * @return An uint representing a holder's total amount of transferable tokens.
  83. */
  84. function transferableTokens(address holder, uint64 time) constant public returns (uint256) {
  85. uint256 grantIndex = tokenGrantsCount(holder);
  86. if (grantIndex == 0) return balanceOf(holder); // shortcut for holder without grants
  87. // Iterate through all the grants the holder has, and add all non-vested tokens
  88. uint256 nonVested = 0;
  89. for (uint256 i = 0; i < grantIndex; i++) {
  90. nonVested = SafeMath.add(nonVested, nonVestedTokens(grants[holder][i], time));
  91. }
  92. // Balance - totalNonVested is the amount of tokens a holder can transfer at any given time
  93. uint256 vestedTransferable = SafeMath.sub(balanceOf(holder), nonVested);
  94. // Return the minimum of how many vested can transfer and other value
  95. // in case there are other limiting transferability factors (default is balanceOf)
  96. return SafeMath.min256(vestedTransferable, super.transferableTokens(holder, time));
  97. }
  98. /**
  99. * @dev Check the amount of grants that an address has.
  100. * @param _holder The holder of the grants.
  101. * @return A uint representing the total amount of grants.
  102. */
  103. function tokenGrantsCount(address _holder) constant returns (uint index) {
  104. return grants[_holder].length;
  105. }
  106. /**
  107. * @dev Calculate amount of vested tokens at a specifc time.
  108. * @param tokens uint256 The amount of tokens grantted.
  109. * @param time uint64 The time to be checked
  110. * @param start uint64 A time representing the begining of the grant
  111. * @param cliff uint64 The cliff period.
  112. * @param vesting uint64 The vesting period.
  113. * @return An uint representing the amount of vested tokensof a specif grant.
  114. * transferableTokens
  115. * | _/-------- vestedTokens rect
  116. * | _/
  117. * | _/
  118. * | _/
  119. * | _/
  120. * | /
  121. * | .|
  122. * | . |
  123. * | . |
  124. * | . |
  125. * | . |
  126. * | . |
  127. * +===+===========+---------+----------> time
  128. * Start Clift Vesting
  129. */
  130. function calculateVestedTokens(
  131. uint256 tokens,
  132. uint256 time,
  133. uint256 start,
  134. uint256 cliff,
  135. uint256 vesting) constant returns (uint256)
  136. {
  137. // Shortcuts for before cliff and after vesting cases.
  138. if (time < cliff) return 0;
  139. if (time >= vesting) return tokens;
  140. // Interpolate all vested tokens.
  141. // As before cliff the shortcut returns 0, we can use just calculate a value
  142. // in the vesting rect (as shown in above's figure)
  143. // vestedTokens = tokens * (time - start) / (vesting - start)
  144. uint256 vestedTokens = SafeMath.div(
  145. SafeMath.mul(
  146. tokens,
  147. SafeMath.sub(time, start)
  148. ),
  149. SafeMath.sub(vesting, start)
  150. );
  151. return vestedTokens;
  152. }
  153. /**
  154. * @dev Get all information about a specifc grant.
  155. * @param _holder The address which will have its tokens revoked.
  156. * @param _grantId The id of the token grant.
  157. * @return Returns all the values that represent a TokenGrant(address, value, start, cliff,
  158. * revokability, burnsOnRevoke, and vesting) plus the vested value at the current time.
  159. */
  160. function tokenGrant(address _holder, uint _grantId) constant returns (address granter, uint256 value, uint256 vested, uint64 start, uint64 cliff, uint64 vesting, bool revokable, bool burnsOnRevoke) {
  161. TokenGrant grant = grants[_holder][_grantId];
  162. granter = grant.granter;
  163. value = grant.value;
  164. start = grant.start;
  165. cliff = grant.cliff;
  166. vesting = grant.vesting;
  167. revokable = grant.revokable;
  168. burnsOnRevoke = grant.burnsOnRevoke;
  169. vested = vestedTokens(grant, uint64(now));
  170. }
  171. /**
  172. * @dev Get the amount of vested tokens at a specific time.
  173. * @param grant TokenGrant The grant to be checked.
  174. * @param time The time to be checked
  175. * @return An uint representing the amount of vested tokens of a specific grant at a specific time.
  176. */
  177. function vestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  178. return calculateVestedTokens(
  179. grant.value,
  180. uint256(time),
  181. uint256(grant.start),
  182. uint256(grant.cliff),
  183. uint256(grant.vesting)
  184. );
  185. }
  186. /**
  187. * @dev Calculate the amount of non vested tokens at a specific time.
  188. * @param grant TokenGrant The grant to be checked.
  189. * @param time uint64 The time to be checked
  190. * @return An uint representing the amount of non vested tokens of a specifc grant on the
  191. * passed time frame.
  192. */
  193. function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  194. return grant.value.sub(vestedTokens(grant, time));
  195. }
  196. /**
  197. * @dev Calculate the date when the holder can trasfer all its tokens
  198. * @param holder address The address of the holder
  199. * @return An uint representing the date of the last transferable tokens.
  200. */
  201. function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
  202. date = uint64(now);
  203. uint256 grantIndex = grants[holder].length;
  204. for (uint256 i = 0; i < grantIndex; i++) {
  205. date = SafeMath.max64(grants[holder][i].vesting, date);
  206. }
  207. }
  208. }