Last active
October 10, 2017 00:21
-
-
Save eordano/5e1fb1858008d5a344819669cfcd3b42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.15; | |
import 'zeppelin-solidity/contracts/tokens/ERC20Basic.sol'; | |
import 'zeppelin-solidity/contracts/ownership/Ownable.sol'; | |
import 'zeppelin-solidity/contracts/math/Math.sol'; | |
import 'zeppelin-solidity/contracts/math/SafeMath.sol'; | |
/** | |
* @title TokenVesting | |
* @dev A token holder contract that can release its token balance gradually like a | |
* typical vesting scheme, with a cliff and vesting period. Optionally revocable by the | |
* owner. | |
*/ | |
contract TokenVesting is Ownable { | |
using SafeMath for uint256; | |
event Released(uint256 amount); | |
event Revoked(); | |
// beneficiary of tokens after they are released | |
address public beneficiary; | |
uint256 public cliff; | |
uint256 public start; | |
uint256 public duration; | |
bool public revocable; | |
uint256 public released; | |
ERC20Basic public token; | |
/** | |
* @dev Creates a vesting contract that vests its balance of any ERC20 token to the | |
* _beneficiary, gradually in a linear fashion until _start + _duration. By then all | |
* of the balance will have vested. | |
* @param _beneficiary address of the beneficiary to whom vested tokens are transferred | |
* @param _cliff duration in seconds of the cliff in which tokens will begin to vest | |
* @param _duration duration in seconds of the period in which the tokens will vest | |
* @param _revocable whether the vesting is revocable or not | |
* @param _token address of the ERC20 token contract | |
*/ | |
function TokenVesting( | |
address _beneficiary, | |
uint256 _start, | |
uint256 _cliff, | |
uint256 _duration, | |
bool _revocable | |
address _token | |
) { | |
require(_beneficiary != 0x0); | |
require(_cliff <= _duration); | |
beneficiary = _beneficiary; | |
start = _start; | |
cliff = _start + _cliff; | |
duration = _duration; | |
revocable = _revocable; | |
token = ERC20Basic(_token); | |
} | |
/** | |
* @notice Only allow calls from the beneficiary of the vesting contract | |
*/ | |
modifier onlyBeneficiary() { | |
require(msg.sender == beneficiary) | |
_ | |
} | |
/** | |
* @notice Transfers vested tokens to beneficiary. | |
* @param token ERC20 token which is being vested | |
*/ | |
function release() onlyBeneficiary public { | |
uint256 vested = vestedAmount(); | |
require(vested > 0); | |
released = released.add(vested); | |
token.transfer(beneficiary, vested); | |
Released(vested); | |
} | |
/** | |
* @notice Allow the beneficiary to change its address | |
* @param target the address to transfer the right to | |
*/ | |
function changeBeneficiary(address target) onlyBeneficiary public { | |
require(target != 0); | |
beneficiary = target; | |
} | |
/** | |
* @notice Allows the owner to revoke the vesting. Tokens already vested remain in the contract. | |
* @param token ERC20 token which is being vested | |
*/ | |
function revoke() onlyOwner public { | |
require(revocable); | |
uint256 balance = token.balanceOf(this); | |
token.transfer(owner, balance); | |
Revoked(); | |
} | |
/** | |
* @dev Calculates the amount that has already vested but hasn't been released yet. | |
* @param token ERC20 token which is being vested | |
*/ | |
function vestedAmount() public constant returns (uint256) { | |
if (now < cliff) { | |
return 0; | |
} else if (now >= start + duration) { | |
return token.balanceOf(this); | |
} else { | |
uint256 currentBalance = token.balanceOf(this); | |
uint256 totalBalance = currentBalance.add(released); | |
uint256 vested = totalBalance.mul(now - start).div(duration); | |
uint256 unreleased = vested.sub(released); | |
// currentBalance can be 0 in case of vesting being revoked earlier. | |
return Math.min256(currentBalance, unreleased); | |
} | |
} | |
function () payable public { | |
reject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment