VestedToken.sol 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Represents time of the begining of the grant.
  22. * @param _cliff uint64 Represents the cliff period.
  23. * @param _vesting uint64 Represents 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 address The address which will have its tokens revoked.
  47. * @param _grantId uint 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 address 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 address The address which will have its tokens revoked.
  74. * @param _grantId uint The id of the token grant.
  75. * @return Returns all the values that represent a TokenGrant(address, value,
  76. start, cliff 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 specifc time.
  89. * @param grant TokenGrant The grant to be checked.
  90. * @param time uint64 The time to be checked
  91. * @return An uint representing the amount of vested tokens of a specifc grant
  92. on specifc time.
  93. */
  94. function vestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  95. return calculateVestedTokens(
  96. grant.value,
  97. uint256(time),
  98. uint256(grant.start),
  99. uint256(grant.cliff),
  100. uint256(grant.vesting)
  101. );
  102. }
  103. /**
  104. * @dev Calculate amount of vested tokens at a specifc time.
  105. * @param tokens uint256 The amount of tokens grantted.
  106. * @param time uint64 The time to be checked
  107. * @param start uint64 A time representing the begining of the grant
  108. * @param _cliff uint64 Represents the cliff period.
  109. * @param _vesting uint64 Represents the vesting period.
  110. * @return An uint representing the amount of vested tokensof a specif grant.
  111. */
  112. function calculateVestedTokens(
  113. uint256 tokens,
  114. uint256 time,
  115. uint256 start,
  116. uint256 cliff,
  117. uint256 vesting) constant returns (uint256 vestedTokens)
  118. {
  119. if (time < cliff) {
  120. return 0;
  121. }
  122. if (time >= vesting) {
  123. return tokens;
  124. }
  125. uint256 cliffTokens = tokens.mul(cliff.sub(start)).div(vesting.sub(start));
  126. vestedTokens = cliffTokens;
  127. uint256 vestingTokens = tokens.sub(cliffTokens);
  128. vestedTokens = vestedTokens.add(vestingTokens.mul(time.sub(cliff)).div(vesting.sub(cliff)));
  129. }
  130. /**
  131. * @dev Calculate the amount of non vested tokens at a specific time.
  132. * @param grant TokenGrant The grant to be checked.
  133. * @param time uint64 The time to be checked
  134. * @return An uint representing the amount of non vested tokens of a specifc grant
  135. on the passed time frame.
  136. */
  137. function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
  138. return grant.value.sub(vestedTokens(grant, time));
  139. }
  140. /**
  141. * @dev Calculate the date when the holder can trasfer all its tokens
  142. * @param holder address The address of the holder
  143. * @return An uint representing the date of the last transferable tokens.
  144. */
  145. function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
  146. date = uint64(now);
  147. uint256 grantIndex = grants[holder].length;
  148. for (uint256 i = 0; i < grantIndex; i++) {
  149. date = SafeMath.max64(grants[holder][i].vesting, date);
  150. }
  151. }
  152. /**
  153. * @dev Calculate the total amount of transferable tokens of a holder at a given time
  154. * @param holder address The address of the holder
  155. * @param time uint64 The specific time.
  156. * @return An uint representing a holder's total amount of transferable tokens.
  157. */
  158. function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
  159. uint256 grantIndex = grants[holder].length;
  160. for (uint256 i = 0; i < grantIndex; i++) {
  161. uint256 current = nonVestedTokens(grants[holder][i], time);
  162. nonVested = nonVested.add(current);
  163. }
  164. return SafeMath.min256(balances[holder].sub(nonVested), super.transferableTokens(holder, time));
  165. }
  166. }