You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.4 KiB
102 lines
3.4 KiB
8 years ago
|
pragma solidity ^0.4.8;
|
||
|
|
||
|
import "./StandardToken.sol";
|
||
|
|
||
8 years ago
|
contract VestedToken is StandardToken {
|
||
8 years ago
|
struct TokenGrant {
|
||
8 years ago
|
address granter;
|
||
|
uint256 value;
|
||
|
uint64 cliff;
|
||
|
uint64 vesting;
|
||
|
uint64 start;
|
||
|
}
|
||
|
|
||
8 years ago
|
mapping (address => TokenGrant[]) public grants;
|
||
8 years ago
|
|
||
8 years ago
|
function grantVestedTokens(address _to, uint256 _value, uint64 _start, uint64 _cliff, uint64 _vesting) {
|
||
8 years ago
|
if (_cliff < _start) throw;
|
||
|
if (_vesting < _start) throw;
|
||
|
if (_vesting < _cliff) throw;
|
||
|
|
||
8 years ago
|
TokenGrant memory grant = TokenGrant({start: _start, value: _value, cliff: _cliff, vesting: _vesting, granter: msg.sender});
|
||
8 years ago
|
grants[_to].push(grant);
|
||
|
|
||
8 years ago
|
transfer(_to, _value);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
function revokeTokenGrant(address _holder, uint _grantId) {
|
||
|
TokenGrant grant = grants[_holder][_grantId];
|
||
8 years ago
|
|
||
|
if (grant.granter != msg.sender) throw;
|
||
8 years ago
|
uint256 nonVested = nonVestedTokens(grant, uint64(now));
|
||
8 years ago
|
|
||
|
// remove grant from array
|
||
|
delete grants[_holder][_grantId];
|
||
|
grants[_holder][_grantId] = grants[_holder][grants[_holder].length - 1];
|
||
|
grants[_holder].length -= 1;
|
||
|
|
||
|
balances[msg.sender] = safeAdd(balances[msg.sender], nonVested);
|
||
|
balances[_holder] = safeSub(balances[_holder], nonVested);
|
||
|
}
|
||
|
|
||
8 years ago
|
function tokenGrantsCount(address _holder) constant returns (uint index) {
|
||
8 years ago
|
return grants[_holder].length;
|
||
|
}
|
||
|
|
||
8 years ago
|
function tokenGrant(address _holder, uint _grantId) constant returns (address granter, uint256 value, uint256 vested, uint64 start, uint64 cliff, uint64 vesting) {
|
||
|
TokenGrant grant = grants[_holder][_grantId];
|
||
8 years ago
|
|
||
|
granter = grant.granter;
|
||
|
value = grant.value;
|
||
|
start = grant.start;
|
||
|
cliff = grant.cliff;
|
||
|
vesting = grant.vesting;
|
||
|
|
||
8 years ago
|
vested = vestedTokens(grant, uint64(now));
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
function vestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
|
||
|
return calculateVestedTokens(grant.value, uint256(time), uint256(grant.start), uint256(grant.cliff), uint256(grant.vesting));
|
||
|
}
|
||
|
|
||
|
function calculateVestedTokens(uint256 tokens, uint256 time, uint256 start, uint256 cliff, uint256 vesting) constant returns (uint256 vestedTokens) {
|
||
|
if (time < cliff) return 0;
|
||
|
if (time > vesting) return tokens;
|
||
8 years ago
|
|
||
8 years ago
|
uint256 cliffTokens = safeMul(tokens, safeDiv(safeSub(cliff, start), safeSub(vesting, start)));
|
||
8 years ago
|
vestedTokens = cliffTokens;
|
||
8 years ago
|
|
||
8 years ago
|
uint256 vestingTokens = safeSub(tokens, cliffTokens);
|
||
8 years ago
|
|
||
8 years ago
|
vestedTokens = safeAdd(vestedTokens, safeMul(vestingTokens, safeDiv(safeSub(time, cliff), safeSub(vesting, start))));
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
|
||
|
return safeSub(grant.value, vestedTokens(grant, time));
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
|
||
8 years ago
|
date = uint64(now);
|
||
|
uint256 grantIndex = grants[holder].length;
|
||
|
for (uint256 i = 0; i < grantIndex; i++) {
|
||
|
date = max64(grants[holder][i].vesting, date);
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
|
||
8 years ago
|
uint256 grantIndex = grants[holder].length;
|
||
|
|
||
|
for (uint256 i = 0; i < grantIndex; i++) {
|
||
8 years ago
|
nonVested = safeAdd(nonVested, nonVestedTokens(grants[holder][i], time));
|
||
8 years ago
|
}
|
||
|
|
||
|
return safeSub(balances[holder], nonVested);
|
||
|
}
|
||
|
|
||
|
function transfer(address _to, uint _value) returns (bool success){
|
||
8 years ago
|
if (_value > transferableTokens(msg.sender, uint64(now))) throw;
|
||
8 years ago
|
|
||
|
return super.transfer(_to, _value);
|
||
|
}
|
||
|
}
|