VestedToken.sol 8.5 KB

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