TokenVesting.sol 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. pragma solidity ^0.4.11;
  2. import './ERC20Basic.sol';
  3. import './SafeERC20.sol';
  4. import '../ownership/Ownable.sol';
  5. import '../math/Math.sol';
  6. import '../math/SafeMath.sol';
  7. /**
  8. * @title TokenVesting
  9. * @dev A token holder contract that can release its token balance gradually like a
  10. * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
  11. * owner.
  12. */
  13. contract TokenVesting is Ownable {
  14. using SafeMath for uint256;
  15. using SafeERC20 for ERC20Basic;
  16. event Released(uint256 amount);
  17. event Revoked();
  18. // beneficiary of tokens after they are released
  19. address public beneficiary;
  20. uint256 public cliff;
  21. uint256 public start;
  22. uint256 public duration;
  23. bool public revocable;
  24. mapping (address => uint256) public released;
  25. mapping (address => bool) public revoked;
  26. /**
  27. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  28. * _beneficiary, gradually in a linear fashion until _start + _duration. By then all
  29. * of the balance will have vested.
  30. * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
  31. * @param _cliff duration in seconds of the cliff in which tokens will begin to vest
  32. * @param _duration duration in seconds of the period in which the tokens will vest
  33. * @param _revocable whether the vesting is revocable or not
  34. */
  35. function TokenVesting(address _beneficiary, uint256 _start, uint256 _cliff, uint256 _duration, bool _revocable) {
  36. require(_beneficiary != 0x0);
  37. require(_cliff <= _duration);
  38. beneficiary = _beneficiary;
  39. revocable = _revocable;
  40. duration = _duration;
  41. cliff = _start.add(_cliff);
  42. start = _start;
  43. }
  44. /**
  45. * @notice Transfers vested tokens to beneficiary.
  46. * @param token ERC20 token which is being vested
  47. */
  48. function release(ERC20Basic token) public {
  49. uint256 vested = vestedAmount(token);
  50. uint256 unreleased = releasableAmount(token);
  51. require(unreleased > 0);
  52. token.safeTransfer(beneficiary, unreleased);
  53. released[token] = released[token].add(unreleased);
  54. Released(unreleased);
  55. }
  56. /**
  57. * @notice Allows the owner to revoke the vesting. Tokens already vested
  58. * remain in the contract, the rest are returned to the owner.
  59. * @param token ERC20 token which is being vested
  60. */
  61. function revoke(ERC20Basic token) public onlyOwner {
  62. require(revocable);
  63. require(!revoked[token]);
  64. uint256 balance = token.balanceOf(this);
  65. uint256 unreleased = releasableAmount(token);
  66. uint256 refund = balance.sub(unreleased);
  67. revoked[token] = true;
  68. token.safeTransfer(owner, refund);
  69. Revoked();
  70. }
  71. /**
  72. * @dev Calculates the amount that has already vested but hasn't been released yet.
  73. * @param token ERC20 token which is being vested
  74. */
  75. function releasableAmount(ERC20Basic token) public constant returns (uint256) {
  76. return vestedAmount(token).sub(released[token]);
  77. }
  78. /**
  79. * @dev Calculates the amount that has already vested.
  80. * @param token ERC20 token which is being vested
  81. */
  82. function vestedAmount(ERC20Basic token) public constant returns (uint256) {
  83. uint256 currentBalance = token.balanceOf(this);
  84. uint256 totalBalance = currentBalance.add(released[token]);
  85. if (now < cliff) {
  86. return 0;
  87. } else if (now >= start + duration || revoked[token]) {
  88. return totalBalance;
  89. } else {
  90. return totalBalance.mul(now - start).div(duration);
  91. }
  92. }
  93. }