Merge pull request #140 from AragonOne/vesting

Vested Token implementation (Standard Token with vesting calendar)
This commit is contained in:
Manuel Aráoz
2017-02-13 12:43:54 -03:00
committed by GitHub
6 changed files with 233 additions and 1 deletions

View File

@ -11,6 +11,13 @@ contract SafeMath {
return c; return c;
} }
function safeDiv(uint a, uint b) internal returns (uint) {
assert(b > 0);
uint c = a / b;
assert(a == b * c + a % b);
return c;
}
function safeSub(uint a, uint b) internal returns (uint) { function safeSub(uint a, uint b) internal returns (uint) {
assert(b <= a); assert(b <= a);
return a - b; return a - b;
@ -22,8 +29,23 @@ contract SafeMath {
return c; return c;
} }
function max64(uint64 a, uint64 b) internal constant returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal constant returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal constant returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal constant returns (uint256) {
return a < b ? a : b;
}
function assert(bool assertion) internal { function assert(bool assertion) internal {
if (!assertion) throw; if (!assertion) throw;
} }
} }

View File

@ -0,0 +1,11 @@
pragma solidity ^0.4.4;
import '../token/VestedToken.sol';
// mock class using StandardToken
contract VestedTokenMock is VestedToken {
function VestedTokenMock(address initialAccount, uint initialBalance) {
balances[initialAccount] = initialBalance;
totalSupply = initialBalance;
}
}

View File

@ -0,0 +1,102 @@
pragma solidity ^0.4.8;
import "./StandardToken.sol";
contract VestedToken is StandardToken {
struct TokenGrant {
address granter;
uint256 value;
uint64 cliff;
uint64 vesting;
uint64 start;
}
mapping (address => TokenGrant[]) public grants;
function grantVestedTokens(address _to, uint256 _value, uint64 _start, uint64 _cliff, uint64 _vesting) {
if (_cliff < _start) throw;
if (_vesting < _start) throw;
if (_vesting < _cliff) throw;
TokenGrant memory grant = TokenGrant({start: _start, value: _value, cliff: _cliff, vesting: _vesting, granter: msg.sender});
grants[_to].push(grant);
transfer(_to, _value);
}
function revokeTokenGrant(address _holder, uint _grantId) {
TokenGrant grant = grants[_holder][_grantId];
if (grant.granter != msg.sender) throw;
uint256 nonVested = nonVestedTokens(grant, uint64(now));
// 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);
Transfer(_holder, msg.sender, nonVested);
}
function tokenGrantsCount(address _holder) constant returns (uint index) {
return grants[_holder].length;
}
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];
granter = grant.granter;
value = grant.value;
start = grant.start;
cliff = grant.cliff;
vesting = grant.vesting;
vested = vestedTokens(grant, uint64(now));
}
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;
uint256 cliffTokens = safeDiv(safeMul(tokens, safeSub(cliff, start)), safeSub(vesting, start));
vestedTokens = cliffTokens;
uint256 vestingTokens = safeSub(tokens, cliffTokens);
vestedTokens = safeAdd(vestedTokens, safeDiv(safeMul(vestingTokens, safeSub(time, cliff)), safeSub(vesting, start)));
}
function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
return safeSub(grant.value, vestedTokens(grant, time));
}
function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
date = uint64(now);
uint256 grantIndex = grants[holder].length;
for (uint256 i = 0; i < grantIndex; i++) {
date = max64(grants[holder][i].vesting, date);
}
}
function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
uint256 grantIndex = grants[holder].length;
for (uint256 i = 0; i < grantIndex; i++) {
nonVested = safeAdd(nonVested, nonVestedTokens(grants[holder][i], time));
}
return safeSub(balances[holder], nonVested);
}
function transfer(address _to, uint _value) returns (bool success){
if (_value > transferableTokens(msg.sender, uint64(now))) throw;
return super.transfer(_to, _value);
}
}

View File

@ -12,6 +12,7 @@
}, },
"scripts": { "scripts": {
"test": "truffle test", "test": "truffle test",
"console": "truffle console",
"install": "scripts/install.sh" "install": "scripts/install.sh"
}, },
"repository": { "repository": {

81
test/VestedToken.js Normal file
View File

@ -0,0 +1,81 @@
const assertJump = require('./helpers/assertJump');
const timer = require('./helpers/timer');
contract('VestedToken', function(accounts) {
let token = null
let now = 0
const tokenAmount = 50
const granter = accounts[0]
const receiver = accounts[1]
beforeEach(async () => {
token = await VestedTokenMock.new(granter, 100);
now = web3.eth.getBlock(web3.eth.blockNumber).timestamp;
})
it('granter can grant tokens without vesting', async () => {
await token.transfer(receiver, tokenAmount, { from: granter })
assert.equal(await token.balanceOf(receiver), tokenAmount);
assert.equal(await token.transferableTokens(receiver, now), tokenAmount);
})
describe('getting a token grant', async () => {
const cliff = 10000
const vesting = 20000 // seconds
beforeEach(async () => {
await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, { from: granter })
})
it('tokens are received', async () => {
assert.equal(await token.balanceOf(receiver), tokenAmount);
})
it('has 0 transferable tokens before cliff', async () => {
assert.equal(await token.transferableTokens(receiver, now), 0);
})
it('all tokens are transferable after vesting', async () => {
assert.equal(await token.transferableTokens(receiver, now + vesting + 1), tokenAmount);
})
it('throws when trying to transfer non vested tokens', async () => {
try {
await token.transfer(accounts[7], 1, { from: receiver })
} catch(error) {
return assertJump(error);
}
assert.fail('should have thrown before');
})
it('can be revoked by granter', async () => {
await token.revokeTokenGrant(receiver, 0, { from: granter });
assert.equal(await token.balanceOf(receiver), 0);
assert.equal(await token.balanceOf(granter), 100);
})
it('cannot be revoked by non granter', async () => {
try {
await token.revokeTokenGrant(receiver, 0, { from: accounts[3] });
} catch(error) {
return assertJump(error);
}
assert.fail('should have thrown before');
})
it('can be revoked by granter and non vested tokens are returned', async () => {
await timer(cliff);
await token.revokeTokenGrant(receiver, 0, { from: granter });
assert.equal(await token.balanceOf(receiver), tokenAmount * cliff / vesting);
})
it('can transfer all tokens after vesting ends', async () => {
await timer(vesting + 1);
await token.transfer(accounts[7], tokenAmount, { from: receiver })
assert.equal(await token.balanceOf(accounts[7]), tokenAmount);
})
})
});

15
test/helpers/timer.js Normal file
View File

@ -0,0 +1,15 @@
// timer for tests specific to testrpc
module.exports = s => {
return new Promise((resolve, reject) => {
web3.currentProvider.sendAsync({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [s], // 60 seaconds, may need to be hex, I forget
id: new Date().getTime() // Id of the request; anything works, really
}, function(err) {
if (err) return reject(err);
resolve();
});
//setTimeout(() => resolve(), s * 1000 + 600) // 600ms breathing room for testrpc to sync
});
};