VestedToken.sol 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  11. uint256 value;
  12. uint64 cliff;
  13. uint64 vesting;
  14. uint64 start;
  15. }
  16. mapping (address => TokenGrant[]) public grants;
  17. /**
  18. * @dev Grant tokens to a specified address
  19. * @param _to address The address which the tokens will be granted to.
  20. * @param _value uint256 The amount of tokens to be granted.
  21. * @param _start uint64 Time of the beginning of the grant.
  22. * @param _cliff uint64 Time of the cliff period.
  23. * @param _vesting uint64 The vesting period.
  24. */
  25. function grantVestedTokens(
  26. address _to,
  27. uint256 _value,
  28. uint64 _start,
  29. uint64 _cliff,
  30. uint64 _vesting) {
  31. if (_cliff < _start) {
  32. throw;
  33. }
  34. if (_vesting < _start) {
  35. throw;
  36. }
  37. if (_vesting < _cliff) {
  38. throw;
  39. }
  40. TokenGrant memory grant = TokenGrant(msg.sender, _value, _cliff, _vesting, _start);
  41. grants[_to].push(grant);
  42. transfer(_to, _value);
  43. }
  44. /**
  45. * @dev Revoke the grant of tokens of a specifed address.
  46. * @param _holder The address which will have its tokens revoked.
  47. * @param _grantId The id of the token grant.
  48. */
  49. function revokeTokenGrant(address _holder, uint _grantId) {
  50. TokenGrant grant = grants[_holder][_grantId];
  51. if (grant.granter != msg.sender) {
  52. throw;
  53. }
  54. uint256 nonVested = nonVestedTokens(grant, uint64(now));
  55. // remove grant from array
  56. delete grants[_holder][_grantId];
  57. grants[_holder][_grantId] = grants[_holder][grants[_holder].length - 1];
  58. grants[_holder].length -= 1;
  59. balances[msg.sender] = balances[msg.sender].add(nonVested);
  60. balances[_holder] = balances[_holder].sub(nonVested);
  61. Transfer(_holder, msg.sender, nonVested);
  62. }
  63. /**
  64. * @dev Check the amount of grants that an address has.
  65. * @param _holder The holder of the grants.
  66. * @return A uint representing the total amount of grants.
  67. */
  68. function tokenGrantsCount(address _holder) constant returns (uint index) {
  69. return grants[_holder].length;
  70. }
  71. /**
  72. * @dev Get all information about a specifc grant.
  73. * @param _holder The address which will have its tokens revoked.
  74. * @param _grantId The id of the token grant.
  75. * @return Returns all the values that represent a TokenGrant(address, value, start, cliff
  76. * and vesting) plus the vested value at the current time.
  77. */
  78. function tokenGrant(address _holder, uint _grantId) constant returns (address granter, uint256 value, uint256 vested, uint64 start, uint64 cliff, uint64 vesting) {
  79. TokenGrant grant = grants[_holder][_grantId];
  80. granter = grant.granter;
  81. value = grant.value;
  82. start = grant.start;
  83. cliff = grant.cliff;
  84. vesting = grant.vesting;
  85. vested = vestedTokens(grant, uint64(now));
  86. }
  87. /**
  88. * @dev Get the amount of vested tokens at a specific time.
  89. * @param grant TokenGrant The grant to be checked.
  90. * @param time The time to be checked
  91. * @return An uint representing the amount of vested tokens of a specific grant at a specific time.
  92. */
  93. function vestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  94. return calculateVestedTokens(
  95. grant.value,
  96. uint256(time),
  97. uint256(grant.start),
  98. uint256(grant.cliff),
  99. uint256(grant.vesting)
  100. );
  101. }
  102. /**
  103. * @dev Calculate amount of vested tokens at a specifc time.
  104. * @param tokens uint256 The amount of tokens grantted.
  105. * @param time uint64 The time to be checked
  106. * @param start uint64 A time representing the begining of the grant
  107. * @param _cliff uint64 The cliff period.
  108. * @param _vesting uint64 The vesting period.
  109. * @return An uint representing the amount of vested tokensof a specif grant.
  110. */
  111. function calculateVestedTokens(
  112. uint256 tokens,
  113. uint256 time,
  114. uint256 start,
  115. uint256 cliff,
  116. uint256 vesting) constant returns (uint256 vestedTokens)
  117. {
  118. if (time < cliff) {
  119. return 0;
  120. }
  121. if (time >= vesting) {
  122. return tokens;
  123. }
  124. uint256 cliffTokens = tokens.mul(cliff.sub(start)).div(vesting.sub(start));
  125. vestedTokens = cliffTokens;
  126. uint256 vestingTokens = tokens.sub(cliffTokens);
  127. vestedTokens = vestedTokens.add(vestingTokens.mul(time.sub(cliff)).div(vesting.sub(cliff)));
  128. }
  129. /**
  130. * @dev Calculate the amount of non vested tokens at a specific time.
  131. * @param grant TokenGrant The grant to be checked.
  132. * @param time uint64 The time to be checked
  133. * @return An uint representing the amount of non vested tokens of a specifc grant on the
  134. * passed time frame.
  135. */
  136. function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  137. return grant.value.sub(vestedTokens(grant, time));
  138. }
  139. /**
  140. * @dev Calculate the date when the holder can trasfer all its tokens
  141. * @param holder address The address of the holder
  142. * @return An uint representing the date of the last transferable tokens.
  143. */
  144. function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
  145. date = uint64(now);
  146. uint256 grantIndex = grants[holder].length;
  147. for (uint256 i = 0; i < grantIndex; i++) {
  148. date = SafeMath.max64(grants[holder][i].vesting, date);
  149. }
  150. }
  151. /**
  152. * @dev Calculate the total amount of transferable tokens of a holder at a given time
  153. * @param holder address The address of the holder
  154. * @param time uint64 The specific time.
  155. * @return An uint representing a holder's total amount of transferable tokens.
  156. */
  157. function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
  158. uint256 grantIndex = grants[holder].length;
  159. for (uint256 i = 0; i < grantIndex; i++) {
  160. uint256 current = nonVestedTokens(grants[holder][i], time);
  161. nonVested = nonVested.add(current);
  162. }
  163. return SafeMath.min256(balances[holder].sub(nonVested), super.transferableTokens(holder, time));
  164. }
  165. }