TokenVesting.sol 3.3 KB

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