VestedToken.sol 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if (_cliff < _start || _vesting < _cliff) {
  41. throw;
  42. }
  43. if (tokenGrantsCount(_to) > MAX_GRANTS_PER_ADDRESS) throw; // To prevent a user being spammed and have his balance locked (out of gas attack when calculating vesting).
  44. uint256 count = grants[_to].push(
  45. TokenGrant(
  46. _revokable ? msg.sender : 0, // avoid storing an extra 20 bytes when it is non-revokable
  47. _value,
  48. _cliff,
  49. _vesting,
  50. _start,
  51. _revokable,
  52. _burnsOnRevoke
  53. )
  54. );
  55. transfer(_to, _value);
  56. NewTokenGrant(msg.sender, _to, _value, count - 1);
  57. }
  58. /**
  59. * @dev Revoke the grant of tokens of a specifed address.
  60. * @param _holder The address which will have its tokens revoked.
  61. * @param _grantId The id of the token grant.
  62. */
  63. function revokeTokenGrant(address _holder, uint256 _grantId) public {
  64. TokenGrant grant = grants[_holder][_grantId];
  65. if (!grant.revokable) { // Check if grant was revokable
  66. throw;
  67. }
  68. if (grant.granter != msg.sender) { // Only granter can revoke it
  69. throw;
  70. }
  71. address receiver = grant.burnsOnRevoke ? 0xdead : msg.sender;
  72. uint256 nonVested = nonVestedTokens(grant, uint64(now));
  73. // remove grant from array
  74. delete grants[_holder][_grantId];
  75. grants[_holder][_grantId] = grants[_holder][grants[_holder].length.sub(1)];
  76. grants[_holder].length -= 1;
  77. balances[receiver] = balances[receiver].add(nonVested);
  78. balances[_holder] = balances[_holder].sub(nonVested);
  79. Transfer(_holder, receiver, nonVested);
  80. }
  81. /**
  82. * @dev Calculate the total amount of transferable tokens of a holder at a given time
  83. * @param holder address The address of the holder
  84. * @param time uint64 The specific time.
  85. * @return An uint256 representing a holder's total amount of transferable tokens.
  86. */
  87. function transferableTokens(address holder, uint64 time) constant public returns (uint256) {
  88. uint256 grantIndex = tokenGrantsCount(holder);
  89. if (grantIndex == 0) return super.transferableTokens(holder, time); // shortcut for holder without grants
  90. // Iterate through all the grants the holder has, and add all non-vested tokens
  91. uint256 nonVested = 0;
  92. for (uint256 i = 0; i < grantIndex; i++) {
  93. nonVested = SafeMath.add(nonVested, nonVestedTokens(grants[holder][i], time));
  94. }
  95. // Balance - totalNonVested is the amount of tokens a holder can transfer at any given time
  96. uint256 vestedTransferable = SafeMath.sub(balanceOf(holder), nonVested);
  97. // Return the minimum of how many vested can transfer and other value
  98. // in case there are other limiting transferability factors (default is balanceOf)
  99. return Math.min256(vestedTransferable, super.transferableTokens(holder, time));
  100. }
  101. /**
  102. * @dev Check the amount of grants that an address has.
  103. * @param _holder The holder of the grants.
  104. * @return A uint256 representing the total amount of grants.
  105. */
  106. function tokenGrantsCount(address _holder) constant returns (uint256 index) {
  107. return grants[_holder].length;
  108. }
  109. /**
  110. * @dev Calculate amount of vested tokens at a specifc time.
  111. * @param tokens uint256 The amount of tokens grantted.
  112. * @param time uint64 The time to be checked
  113. * @param start uint64 A time representing the begining of the grant
  114. * @param cliff uint64 The cliff period.
  115. * @param vesting uint64 The vesting period.
  116. * @return An uint256 representing the amount of vested tokensof a specif grant.
  117. * transferableTokens
  118. * | _/-------- vestedTokens rect
  119. * | _/
  120. * | _/
  121. * | _/
  122. * | _/
  123. * | /
  124. * | .|
  125. * | . |
  126. * | . |
  127. * | . |
  128. * | . |
  129. * | . |
  130. * +===+===========+---------+----------> time
  131. * Start Clift Vesting
  132. */
  133. function calculateVestedTokens(
  134. uint256 tokens,
  135. uint256 time,
  136. uint256 start,
  137. uint256 cliff,
  138. uint256 vesting) constant returns (uint256)
  139. {
  140. // Shortcuts for before cliff and after vesting cases.
  141. if (time < cliff) return 0;
  142. if (time >= vesting) return tokens;
  143. // Interpolate all vested tokens.
  144. // As before cliff the shortcut returns 0, we can use just calculate a value
  145. // in the vesting rect (as shown in above's figure)
  146. // vestedTokens = tokens * (time - start) / (vesting - start)
  147. uint256 vestedTokens = SafeMath.div(
  148. SafeMath.mul(
  149. tokens,
  150. SafeMath.sub(time, start)
  151. ),
  152. SafeMath.sub(vesting, start)
  153. );
  154. return vestedTokens;
  155. }
  156. /**
  157. * @dev Get all information about a specifc grant.
  158. * @param _holder The address which will have its tokens revoked.
  159. * @param _grantId The id of the token grant.
  160. * @return Returns all the values that represent a TokenGrant(address, value, start, cliff,
  161. * revokability, burnsOnRevoke, and vesting) plus the vested value at the current time.
  162. */
  163. function tokenGrant(address _holder, uint256 _grantId) constant returns (address granter, uint256 value, uint256 vested, uint64 start, uint64 cliff, uint64 vesting, bool revokable, bool burnsOnRevoke) {
  164. TokenGrant grant = grants[_holder][_grantId];
  165. granter = grant.granter;
  166. value = grant.value;
  167. start = grant.start;
  168. cliff = grant.cliff;
  169. vesting = grant.vesting;
  170. revokable = grant.revokable;
  171. burnsOnRevoke = grant.burnsOnRevoke;
  172. vested = vestedTokens(grant, uint64(now));
  173. }
  174. /**
  175. * @dev Get the amount of vested tokens at a specific time.
  176. * @param grant TokenGrant The grant to be checked.
  177. * @param time The time to be checked
  178. * @return An uint256 representing the amount of vested tokens of a specific grant at a specific time.
  179. */
  180. function vestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  181. return calculateVestedTokens(
  182. grant.value,
  183. uint256(time),
  184. uint256(grant.start),
  185. uint256(grant.cliff),
  186. uint256(grant.vesting)
  187. );
  188. }
  189. /**
  190. * @dev Calculate the amount of non vested tokens at a specific time.
  191. * @param grant TokenGrant The grant to be checked.
  192. * @param time uint64 The time to be checked
  193. * @return An uint256 representing the amount of non vested tokens of a specifc grant on the
  194. * passed time frame.
  195. */
  196. function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  197. return grant.value.sub(vestedTokens(grant, time));
  198. }
  199. /**
  200. * @dev Calculate the date when the holder can trasfer all its tokens
  201. * @param holder address The address of the holder
  202. * @return An uint256 representing the date of the last transferable tokens.
  203. */
  204. function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
  205. date = uint64(now);
  206. uint256 grantIndex = grants[holder].length;
  207. for (uint256 i = 0; i < grantIndex; i++) {
  208. date = Math.max64(grants[holder][i].vesting, date);
  209. }
  210. }
  211. }