TokenVesting.sol 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* solium-disable security/no-block-members */
  2. pragma solidity ^0.4.23;
  3. import "./ERC20Basic.sol";
  4. import "./SafeERC20.sol";
  5. import "../../ownership/Ownable.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 _start the time (as Unix time) at which point vesting starts
  33. * @param _duration duration in seconds of the period in which the tokens will vest
  34. * @param _revocable whether the vesting is revocable or not
  35. */
  36. constructor(
  37. address _beneficiary,
  38. uint256 _start,
  39. uint256 _cliff,
  40. uint256 _duration,
  41. bool _revocable
  42. )
  43. public
  44. {
  45. require(_beneficiary != address(0));
  46. require(_cliff <= _duration);
  47. beneficiary = _beneficiary;
  48. revocable = _revocable;
  49. duration = _duration;
  50. cliff = _start.add(_cliff);
  51. start = _start;
  52. }
  53. /**
  54. * @notice Transfers vested tokens to beneficiary.
  55. * @param token ERC20 token which is being vested
  56. */
  57. function release(ERC20Basic token) public {
  58. uint256 unreleased = releasableAmount(token);
  59. require(unreleased > 0);
  60. released[token] = released[token].add(unreleased);
  61. token.safeTransfer(beneficiary, unreleased);
  62. emit Released(unreleased);
  63. }
  64. /**
  65. * @notice Allows the owner to revoke the vesting. Tokens already vested
  66. * remain in the contract, the rest are returned to the owner.
  67. * @param token ERC20 token which is being vested
  68. */
  69. function revoke(ERC20Basic token) public onlyOwner {
  70. require(revocable);
  71. require(!revoked[token]);
  72. uint256 balance = token.balanceOf(this);
  73. uint256 unreleased = releasableAmount(token);
  74. uint256 refund = balance.sub(unreleased);
  75. revoked[token] = true;
  76. token.safeTransfer(owner, refund);
  77. emit Revoked();
  78. }
  79. /**
  80. * @dev Calculates the amount that has already vested but hasn't been released yet.
  81. * @param token ERC20 token which is being vested
  82. */
  83. function releasableAmount(ERC20Basic token) public view returns (uint256) {
  84. return vestedAmount(token).sub(released[token]);
  85. }
  86. /**
  87. * @dev Calculates the amount that has already vested.
  88. * @param token ERC20 token which is being vested
  89. */
  90. function vestedAmount(ERC20Basic token) public view returns (uint256) {
  91. uint256 currentBalance = token.balanceOf(this);
  92. uint256 totalBalance = currentBalance.add(released[token]);
  93. if (block.timestamp < cliff) {
  94. return 0;
  95. } else if (block.timestamp >= start.add(duration) || revoked[token]) {
  96. return totalBalance;
  97. } else {
  98. return totalBalance.mul(block.timestamp.sub(start)).div(duration);
  99. }
  100. }
  101. }