VestedToken.sol 8.6 KB

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