From 80e591f4878033fbd548e848e32e4c805f212912 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 28 Aug 2017 17:02:27 -0300 Subject: [PATCH 01/17] add TokenVesting contract --- contracts/token/TokenVesting.sol | 82 ++++++++++++++++++++++++++++++++ test/TokenVesting.js | 58 ++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 contracts/token/TokenVesting.sol create mode 100644 test/TokenVesting.js diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol new file mode 100644 index 000000000..a445c644a --- /dev/null +++ b/contracts/token/TokenVesting.sol @@ -0,0 +1,82 @@ +pragma solidity ^0.4.11; + +import './ERC20Basic.sol'; +import '../ownership/Ownable.sol'; +import '../math/Math.sol'; +import '../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. Revokable by the owner. + */ +contract TokenVesting is Ownable { + using SafeMath for uint256; + + // beneficiary of tokens after they are released + address beneficiary; + + uint256 cliff; + uint256 start; + uint256 end; + + mapping (address => uint256) released; + + /** + * @dev Creates a vesting contract that vests its balance of any ERC20 token to the + * _beneficiary, gradually in a linear fashion until _end. By then all of the balance + * will have vested. + * @param _beneficiary address of the beneficiary to whom vested tokens are transferred + * @param _cliff timestamp of the moment when tokens will begin to vest + * @param _end timestamp of the moment when all balance will have been vested + */ + function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end) { + beneficiary = _beneficiary; + cliff = _cliff; + end = _end; + start = now; + } + + /** + * @notice Transfers vested tokens to beneficiary. + * @param token ERC20 token which is being vested + */ + function release(ERC20Basic token) { + uint256 vested = vestedAmount(token); + + require(vested > 0); + + token.transfer(beneficiary, vested); + + released[token] = released[token].add(vested); + } + + /** + * @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(ERC20Basic token) onlyOwner { + uint256 balance = token.balanceOf(this); + + uint256 vested = vestedAmount(token); + + token.transfer(owner, balance - vested); + } + + /** + * @dev Calculates the amount that has already vested. + * @param token ERC20 token which is being vested + */ + function vestedAmount(ERC20Basic token) constant returns (uint256) { + if (now < cliff) { + return 0; + } else { + uint256 currentBalance = token.balanceOf(this); + uint256 totalBalance = currentBalance.add(released[token]); + + uint256 vested = totalBalance.mul(now - start).div(end - start); + + return Math.min256(currentBalance, vested); + } + } +} diff --git a/test/TokenVesting.js b/test/TokenVesting.js new file mode 100644 index 000000000..3435e53ef --- /dev/null +++ b/test/TokenVesting.js @@ -0,0 +1,58 @@ +const BigNumber = web3.BigNumber + +require('chai') + .use(require('chai-as-promised')) + .use(require('chai-bignumber')(BigNumber)) + .should(); + +import EVMThrow from './helpers/EVMThrow' +import latestTime from './helpers/latestTime'; +import {increaseTimeTo, duration} from './helpers/increaseTime'; + +const MintableToken = artifacts.require('MintableToken'); +const TokenVesting = artifacts.require('TokenVesting'); + +contract('TokenVesting', function ([_, owner, beneficiary]) { + + const amount = new BigNumber(1000); + + beforeEach(async function () { + this.token = await MintableToken.new({ from: owner }); + + this.cliff = latestTime() + duration.years(1); + this.end = latestTime() + duration.years(2); + + this.vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, { from: owner }); + + this.start = latestTime(); // gets the timestamp at construction + + await this.token.mint(this.vesting.address, amount, { from: owner }); + }); + + it('cannot be released before cliff', async function () { + await this.vesting.release(this.token.address).should.be.rejectedWith(EVMThrow); + }); + + it('can be released after cliff', async function () { + await increaseTimeTo(this.cliff + duration.weeks(1)); + await this.vesting.release(this.token.address).should.be.fulfilled; + }); + + it('should release proper amount after cliff', async function () { + await increaseTimeTo(this.cliff); + + const { receipt } = await this.vesting.release(this.token.address); + const releaseTime = web3.eth.getBlock(receipt.blockNumber).timestamp; + + const balance = await this.token.balanceOf(beneficiary); + balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.end - this.start).floor()); + }); + + it('should have released all after end', async function () { + await increaseTimeTo(this.end); + await this.vesting.release(this.token.address); + const balance = await this.token.balanceOf(beneficiary); + balance.should.bignumber.equal(amount); + }); + +}); From 00f323d132c08c4eb60f32c9e50e37ccf2118539 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 28 Aug 2017 17:05:15 -0300 Subject: [PATCH 02/17] add shortcut for vestedAmount after vesting end --- contracts/token/TokenVesting.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index a445c644a..6eb2e17d2 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -70,6 +70,8 @@ contract TokenVesting is Ownable { function vestedAmount(ERC20Basic token) constant returns (uint256) { if (now < cliff) { return 0; + } else if (now >= end) { + return token.balanceOf(this); } else { uint256 currentBalance = token.balanceOf(this); uint256 totalBalance = currentBalance.add(released[token]); From 998c72ab5bb7de260e0866d945496afb05dc3d85 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 28 Aug 2017 17:13:59 -0300 Subject: [PATCH 03/17] add preconditions to constructor --- contracts/token/TokenVesting.sol | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index 6eb2e17d2..c05c0cc19 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -31,6 +31,10 @@ contract TokenVesting is Ownable { * @param _end timestamp of the moment when all balance will have been vested */ function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end) { + require(_beneficiary != 0x0); + require(_cliff > now); + require(_end > _cliff); + beneficiary = _beneficiary; cliff = _cliff; end = _end; From 3da7c31484120eb31db1caeb61323e7a3cb733f2 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Tue, 29 Aug 2017 12:40:31 -0300 Subject: [PATCH 04/17] add revocable flag --- contracts/token/TokenVesting.sol | 12 ++++++++++-- test/TokenVesting.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index c05c0cc19..d6bde9f5d 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -8,7 +8,8 @@ import '../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. Revokable by the owner. + * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the + * owner. */ contract TokenVesting is Ownable { using SafeMath for uint256; @@ -20,6 +21,8 @@ contract TokenVesting is Ownable { uint256 start; uint256 end; + bool revocable; + mapping (address => uint256) released; /** @@ -29,8 +32,9 @@ contract TokenVesting is Ownable { * @param _beneficiary address of the beneficiary to whom vested tokens are transferred * @param _cliff timestamp of the moment when tokens will begin to vest * @param _end timestamp of the moment when all balance will have been vested + * @param _revocable whether the vesting is revocable or not */ - function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end) { + function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end, bool _revocable) { require(_beneficiary != 0x0); require(_cliff > now); require(_end > _cliff); @@ -38,6 +42,8 @@ contract TokenVesting is Ownable { beneficiary = _beneficiary; cliff = _cliff; end = _end; + revocable = _revocable; + start = now; } @@ -60,6 +66,8 @@ contract TokenVesting is Ownable { * @param token ERC20 token which is being vested */ function revoke(ERC20Basic token) onlyOwner { + require(revocable); + uint256 balance = token.balanceOf(this); uint256 vested = vestedAmount(token); diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 3435e53ef..d4c233bce 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -22,7 +22,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { this.cliff = latestTime() + duration.years(1); this.end = latestTime() + duration.years(2); - this.vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, { from: owner }); + this.vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner }); this.start = latestTime(); // gets the timestamp at construction From 6344a76f834997150d06cb3eb86bacd01aa6d776 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Tue, 29 Aug 2017 12:40:47 -0300 Subject: [PATCH 05/17] add pending tests --- test/TokenVesting.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index d4c233bce..974f19b71 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -48,6 +48,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.end - this.start).floor()); }); + it('should linearly release tokens during vesting period'); + it('should have released all after end', async function () { await increaseTimeTo(this.end); await this.vesting.release(this.token.address); @@ -55,4 +57,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { balance.should.bignumber.equal(amount); }); + it('should fail to be revoked by owner if revocable not set'); + + it('should be emptied when revoked by owner'); + }); From c11265e6945b2c96676c4ebea508d596fbce5389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Wed, 27 Sep 2017 19:17:08 -0300 Subject: [PATCH 06/17] [TokenVesting] Add events --- contracts/token/TokenVesting.sol | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index d6bde9f5d..b982299c5 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -14,6 +14,9 @@ import '../math/SafeMath.sol'; contract TokenVesting is Ownable { using SafeMath for uint256; + event Release(uint256 amount); + event Revoke(); + // beneficiary of tokens after they are released address beneficiary; @@ -59,6 +62,8 @@ contract TokenVesting is Ownable { token.transfer(beneficiary, vested); released[token] = released[token].add(vested); + + Release(vested); } /** @@ -73,6 +78,8 @@ contract TokenVesting is Ownable { uint256 vested = vestedAmount(token); token.transfer(owner, balance - vested); + + Revoke(); } /** From a227b212f582f6fbed98017ac8913817a9fd12ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Thu, 28 Sep 2017 02:35:53 -0300 Subject: [PATCH 07/17] [TokenVesting] Add tests --- test/TokenVesting.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 974f19b71..74f6a7f39 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -57,8 +57,39 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { balance.should.bignumber.equal(amount); }); - it('should fail to be revoked by owner if revocable not set'); + it('should not fail to be revoked by owner if revocable is set', async function () { + const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner } ); + await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow); + }); - it('should be emptied when revoked by owner'); + it('should fail to be revoked by owner if revocable not set', async function () { + const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, false, { from: owner } ); + await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow); + }); + + it('should return the non-vested tokens when revoked by owner', async function () { + await increaseTimeTo(this.cliff + duration.weeks(1)); + await this.vesting.release(this.token.address); + + const vested = await this.vesting.vestedAmount(this.token.address); + const balance = await this.token.balanceOf(this.vesting.address); + + await this.vesting.revoke(this.token.address, { from: owner }); + + const ownerBalance = await this.token.balanceOf(owner); + ownerBalance.should.bignumber.equal(balance.sub(vested)); + }); + + it('should keep the vested tokens when revoked by owner', async function () { + await increaseTimeTo(this.cliff + duration.weeks(1)); + await this.vesting.release(this.token.address); + + const vested = await this.vesting.vestedAmount(this.token.address); + + await this.vesting.revoke(this.token.address, { from: owner }); + + const balance = await this.token.balanceOf(this.vesting.address); + balance.should.bignumber.equal(vested); + }); }); From 645edfc936ce7a894954dca7bcea32107085bed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Thu, 28 Sep 2017 02:40:38 -0300 Subject: [PATCH 08/17] [TokenVesting] Fix should-revoke test --- test/TokenVesting.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 74f6a7f39..001e0cfe4 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -57,9 +57,9 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { balance.should.bignumber.equal(amount); }); - it('should not fail to be revoked by owner if revocable is set', async function () { + it('should be revoked by owner if revocable is set', async function () { const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner } ); - await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow); + await vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled; }); it('should fail to be revoked by owner if revocable not set', async function () { From 696615d392fefdb16d60af5310d7675c9d910ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Thu, 28 Sep 2017 15:43:18 -0300 Subject: [PATCH 09/17] [TokenVesting] Add linear release test --- test/TokenVesting.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 001e0cfe4..9f7913bee 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -48,7 +48,20 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.end - this.start).floor()); }); - it('should linearly release tokens during vesting period'); + it('should linearly release tokens during vesting period', async function () { + const duration = this.end - this.cliff; + const checkpoints = 4; + + for (let i = 1; i <= checkpoints; i++) { + const now = this.cliff + i * (duration / checkpoints); + await increaseTimeTo(now); + + const vested = await this.vesting.vestedAmount(this.token.address); + const expectedVesting = amount.mul(now - this.start).div(this.end - this.start).floor(); + + vested.should.bignumber.equal(expectedVesting); + } + }); it('should have released all after end', async function () { await increaseTimeTo(this.end); From bd5616390043af0335b3c681dd61e5d196f2b3cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Fri, 29 Sep 2017 02:59:11 -0300 Subject: [PATCH 10/17] [TokenVesting] Fix vestedAmount calculation. Linearity test watches beneficiary balance over vestedAmount --- contracts/token/TokenVesting.sol | 6 ++++-- test/TokenVesting.js | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index b982299c5..cbb56f656 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -83,7 +83,7 @@ contract TokenVesting is Ownable { } /** - * @dev Calculates the amount that has already vested. + * @dev Calculates the amount that has already vested but not released. * @param token ERC20 token which is being vested */ function vestedAmount(ERC20Basic token) constant returns (uint256) { @@ -96,8 +96,10 @@ contract TokenVesting is Ownable { uint256 totalBalance = currentBalance.add(released[token]); uint256 vested = totalBalance.mul(now - start).div(end - start); + uint256 unreleased = vested.sub(released[token]); - return Math.min256(currentBalance, vested); + // currentBalance can be 0 in case of a revoke + return Math.min256(currentBalance, unreleased); } } } diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 9f7913bee..31647ed6c 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -56,10 +56,11 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const now = this.cliff + i * (duration / checkpoints); await increaseTimeTo(now); - const vested = await this.vesting.vestedAmount(this.token.address); + await this.vesting.release(this.token.address); + const balance = await this.token.balanceOf(beneficiary); const expectedVesting = amount.mul(now - this.start).div(this.end - this.start).floor(); - vested.should.bignumber.equal(expectedVesting); + balance.should.bignumber.equal(expectedVesting); } }); From 822de45bfc4c75b255f82aa8f39a229090c4101b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Fri, 29 Sep 2017 15:10:08 -0300 Subject: [PATCH 11/17] [TokenVesting] Add a start parameter to constructor to defer vesting --- contracts/token/TokenVesting.sol | 27 +++++++++++++-------------- test/TokenVesting.js | 31 +++++++++++++++---------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index cbb56f656..35cae73ea 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -22,7 +22,7 @@ contract TokenVesting is Ownable { uint256 cliff; uint256 start; - uint256 end; + uint256 duration; bool revocable; @@ -30,24 +30,23 @@ contract TokenVesting is Ownable { /** * @dev Creates a vesting contract that vests its balance of any ERC20 token to the - * _beneficiary, gradually in a linear fashion until _end. By then all of the balance - * will have vested. + * _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 timestamp of the moment when tokens will begin to vest - * @param _end timestamp of the moment when all balance will have been vested + * @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 */ - function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end, bool _revocable) { + function TokenVesting(address _beneficiary, uint256 _start, uint256 _cliff, uint256 _duration, bool _revocable) { require(_beneficiary != 0x0); - require(_cliff > now); - require(_end > _cliff); + require(_cliff < _duration); + require(_start >= now); beneficiary = _beneficiary; - cliff = _cliff; - end = _end; revocable = _revocable; - - start = now; + duration = _duration; + cliff = _start + _cliff; + start = _start; } /** @@ -89,13 +88,13 @@ contract TokenVesting is Ownable { function vestedAmount(ERC20Basic token) constant returns (uint256) { if (now < cliff) { return 0; - } else if (now >= end) { + } else if (now >= start + duration) { return token.balanceOf(this); } else { uint256 currentBalance = token.balanceOf(this); uint256 totalBalance = currentBalance.add(released[token]); - uint256 vested = totalBalance.mul(now - start).div(end - start); + uint256 vested = totalBalance.mul(now - start).div(duration); uint256 unreleased = vested.sub(released[token]); // currentBalance can be 0 in case of a revoke diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 31647ed6c..78c9496e6 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -19,12 +19,11 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { beforeEach(async function () { this.token = await MintableToken.new({ from: owner }); - this.cliff = latestTime() + duration.years(1); - this.end = latestTime() + duration.years(2); + this.start = latestTime() + duration.minutes(1); // +1 minute so it starts after contract instantiation + this.cliff = duration.years(1); + this.duration = duration.years(2); - this.vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner }); - - this.start = latestTime(); // gets the timestamp at construction + this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner }); await this.token.mint(this.vesting.address, amount, { from: owner }); }); @@ -34,55 +33,55 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('can be released after cliff', async function () { - await increaseTimeTo(this.cliff + duration.weeks(1)); + await increaseTimeTo(this.start + this.cliff + duration.weeks(1)); await this.vesting.release(this.token.address).should.be.fulfilled; }); it('should release proper amount after cliff', async function () { - await increaseTimeTo(this.cliff); + await increaseTimeTo(this.start + this.cliff); const { receipt } = await this.vesting.release(this.token.address); const releaseTime = web3.eth.getBlock(receipt.blockNumber).timestamp; const balance = await this.token.balanceOf(beneficiary); - balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.end - this.start).floor()); + balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor()); }); it('should linearly release tokens during vesting period', async function () { - const duration = this.end - this.cliff; + const vestingPeriod = this.duration - this.cliff; const checkpoints = 4; for (let i = 1; i <= checkpoints; i++) { - const now = this.cliff + i * (duration / checkpoints); + const now = this.start + this.cliff + i * (vestingPeriod / checkpoints); await increaseTimeTo(now); await this.vesting.release(this.token.address); const balance = await this.token.balanceOf(beneficiary); - const expectedVesting = amount.mul(now - this.start).div(this.end - this.start).floor(); + const expectedVesting = amount.mul(now - this.start).div(this.duration).floor(); balance.should.bignumber.equal(expectedVesting); } }); it('should have released all after end', async function () { - await increaseTimeTo(this.end); + await increaseTimeTo(this.start + this.duration); await this.vesting.release(this.token.address); const balance = await this.token.balanceOf(beneficiary); balance.should.bignumber.equal(amount); }); it('should be revoked by owner if revocable is set', async function () { - const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner } ); + const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner } ); await vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled; }); it('should fail to be revoked by owner if revocable not set', async function () { - const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, false, { from: owner } ); + const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, false, { from: owner } ); await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow); }); it('should return the non-vested tokens when revoked by owner', async function () { - await increaseTimeTo(this.cliff + duration.weeks(1)); + await increaseTimeTo(this.start + this.cliff + duration.weeks(1)); await this.vesting.release(this.token.address); const vested = await this.vesting.vestedAmount(this.token.address); @@ -95,7 +94,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should keep the vested tokens when revoked by owner', async function () { - await increaseTimeTo(this.cliff + duration.weeks(1)); + await increaseTimeTo(this.start + this.cliff + duration.weeks(1)); await this.vesting.release(this.token.address); const vested = await this.vesting.vestedAmount(this.token.address); From 562fb6945f345c14371e69dee4d000561a7d4082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Mon, 2 Oct 2017 16:56:47 -0300 Subject: [PATCH 12/17] [TokenVesting] Rename events according to convention --- contracts/token/TokenVesting.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index 35cae73ea..096f84807 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -14,8 +14,8 @@ import '../math/SafeMath.sol'; contract TokenVesting is Ownable { using SafeMath for uint256; - event Release(uint256 amount); - event Revoke(); + event Released(uint256 amount); + event Revoked(); // beneficiary of tokens after they are released address beneficiary; @@ -62,7 +62,7 @@ contract TokenVesting is Ownable { released[token] = released[token].add(vested); - Release(vested); + Released(vested); } /** @@ -78,7 +78,7 @@ contract TokenVesting is Ownable { token.transfer(owner, balance - vested); - Revoke(); + Revoked(); } /** From 9e0e80e820e49b26e8e5c79951202c1be722419a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Mon, 2 Oct 2017 17:30:35 -0300 Subject: [PATCH 13/17] [TokenVesting] Allow instantiation of already started vesting contracts. Improve comments' wording. --- contracts/token/TokenVesting.sol | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/contracts/token/TokenVesting.sol b/contracts/token/TokenVesting.sol index 096f84807..3cf576f5b 100644 --- a/contracts/token/TokenVesting.sol +++ b/contracts/token/TokenVesting.sol @@ -39,8 +39,7 @@ contract TokenVesting is Ownable { */ function TokenVesting(address _beneficiary, uint256 _start, uint256 _cliff, uint256 _duration, bool _revocable) { require(_beneficiary != 0x0); - require(_cliff < _duration); - require(_start >= now); + require(_cliff <= _duration); beneficiary = _beneficiary; revocable = _revocable; @@ -82,7 +81,7 @@ contract TokenVesting is Ownable { } /** - * @dev Calculates the amount that has already vested but not released. + * @dev Calculates the amount that has already vested but hasn't been released yet. * @param token ERC20 token which is being vested */ function vestedAmount(ERC20Basic token) constant returns (uint256) { @@ -97,7 +96,7 @@ contract TokenVesting is Ownable { uint256 vested = totalBalance.mul(now - start).div(duration); uint256 unreleased = vested.sub(released[token]); - // currentBalance can be 0 in case of a revoke + // currentBalance can be 0 in case of vesting being revoked earlier. return Math.min256(currentBalance, unreleased); } } From 4e39f50aeca68aa3e602f68f511897f5bd9dab1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Mon, 2 Oct 2017 17:31:42 -0300 Subject: [PATCH 14/17] [TokenVesting] Remove unnecessary instatiation on test. --- test/TokenVesting.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index 78c9496e6..ffe118b66 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -71,7 +71,6 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should be revoked by owner if revocable is set', async function () { - const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner } ); await vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled; }); From d21d35ca6f5ce82423a72ffa8f6d36db8fbb4fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Mon, 2 Oct 2017 17:34:37 -0300 Subject: [PATCH 15/17] [TokenVesting] Fix test. --- test/TokenVesting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/TokenVesting.js b/test/TokenVesting.js index ffe118b66..dee1001a7 100644 --- a/test/TokenVesting.js +++ b/test/TokenVesting.js @@ -71,7 +71,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should be revoked by owner if revocable is set', async function () { - await vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled; + await this.vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled; }); it('should fail to be revoked by owner if revocable not set', async function () { From 22b9263674d838544d8e23076e96578a3c9ad759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Wed, 4 Oct 2017 14:50:31 -0300 Subject: [PATCH 16/17] [TokenVesting] Remove VestedToken and LimitedTranferToken --- contracts/token/LimitedTransferToken.sol | 57 ------ contracts/token/VestedToken.sol | 241 ----------------------- test/VestedToken.js | 175 ---------------- 3 files changed, 473 deletions(-) delete mode 100644 contracts/token/LimitedTransferToken.sol delete mode 100644 contracts/token/VestedToken.sol delete mode 100644 test/VestedToken.js diff --git a/contracts/token/LimitedTransferToken.sol b/contracts/token/LimitedTransferToken.sol deleted file mode 100644 index 408e0797d..000000000 --- a/contracts/token/LimitedTransferToken.sol +++ /dev/null @@ -1,57 +0,0 @@ -pragma solidity ^0.4.11; - -import "./ERC20.sol"; - -/** - * @title LimitedTransferToken - * @dev LimitedTransferToken defines the generic interface and the implementation to limit token - * transferability for different events. It is intended to be used as a base class for other token - * contracts. - * LimitedTransferToken has been designed to allow for different limiting factors, - * this can be achieved by recursively calling super.transferableTokens() until the base class is - * hit. For example: - * function transferableTokens(address holder, uint64 time) constant public returns (uint256) { - * return min256(unlockedTokens, super.transferableTokens(holder, time)); - * } - * A working example is VestedToken.sol: - * https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/VestedToken.sol - */ - -contract LimitedTransferToken is ERC20 { - - /** - * @dev Checks whether it can transfer or otherwise throws. - */ - modifier canTransfer(address _sender, uint256 _value) { - require(_value <= transferableTokens(_sender, uint64(now))); - _; - } - - /** - * @dev Checks modifier and allows transfer if tokens are not locked. - * @param _to The address that will receive the tokens. - * @param _value The amount of tokens to be transferred. - */ - function transfer(address _to, uint256 _value) canTransfer(msg.sender, _value) public returns (bool) { - return super.transfer(_to, _value); - } - - /** - * @dev Checks modifier and allows transfer if tokens are not locked. - * @param _from The address that will send the tokens. - * @param _to The address that will receive the tokens. - * @param _value The amount of tokens to be transferred. - */ - function transferFrom(address _from, address _to, uint256 _value) canTransfer(_from, _value) public returns (bool) { - return super.transferFrom(_from, _to, _value); - } - - /** - * @dev Default transferable tokens function returns all tokens for a holder (no limit). - * @dev Overwriting transferableTokens(address holder, uint64 time) is the way to provide the - * specific logic for limiting token transferability for a holder over time. - */ - function transferableTokens(address holder, uint64 time) public constant returns (uint256) { - return balanceOf(holder); - } -} diff --git a/contracts/token/VestedToken.sol b/contracts/token/VestedToken.sol deleted file mode 100644 index 8b9dd94bc..000000000 --- a/contracts/token/VestedToken.sol +++ /dev/null @@ -1,241 +0,0 @@ -pragma solidity ^0.4.11; - -import "../math/Math.sol"; -import "./StandardToken.sol"; -import "./LimitedTransferToken.sol"; - -/** - * @title Vested token - * @dev Tokens that can be vested for a group of addresses. - */ -contract VestedToken is StandardToken, LimitedTransferToken { - - uint256 MAX_GRANTS_PER_ADDRESS = 20; - - struct TokenGrant { - address granter; // 20 bytes - uint256 value; // 32 bytes - uint64 cliff; - uint64 vesting; - uint64 start; // 3 * 8 = 24 bytes - bool revokable; - bool burnsOnRevoke; // 2 * 1 = 2 bits? or 2 bytes? - } // total 78 bytes = 3 sstore per operation (32 per sstore) - - mapping (address => TokenGrant[]) public grants; - - event NewTokenGrant(address indexed from, address indexed to, uint256 value, uint256 grantId); - - /** - * @dev Grant tokens to a specified address - * @param _to address The address which the tokens will be granted to. - * @param _value uint256 The amount of tokens to be granted. - * @param _start uint64 Time of the beginning of the grant. - * @param _cliff uint64 Time of the cliff period. - * @param _vesting uint64 The vesting period. - */ - function grantVestedTokens( - address _to, - uint256 _value, - uint64 _start, - uint64 _cliff, - uint64 _vesting, - bool _revokable, - bool _burnsOnRevoke - ) public { - - // Check for date inconsistencies that may cause unexpected behavior - require(_cliff >= _start && _vesting >= _cliff); - - require(tokenGrantsCount(_to) < MAX_GRANTS_PER_ADDRESS); // To prevent a user being spammed and have his balance locked (out of gas attack when calculating vesting). - - uint256 count = grants[_to].push( - TokenGrant( - _revokable ? msg.sender : 0, // avoid storing an extra 20 bytes when it is non-revokable - _value, - _cliff, - _vesting, - _start, - _revokable, - _burnsOnRevoke - ) - ); - - transfer(_to, _value); - - NewTokenGrant(msg.sender, _to, _value, count - 1); - } - - /** - * @dev Revoke the grant of tokens of a specifed address. - * @param _holder The address which will have its tokens revoked. - * @param _grantId The id of the token grant. - */ - function revokeTokenGrant(address _holder, uint256 _grantId) public { - TokenGrant storage grant = grants[_holder][_grantId]; - - require(grant.revokable); - require(grant.granter == msg.sender); // Only granter can revoke it - - address receiver = grant.burnsOnRevoke ? 0xdead : msg.sender; - - uint256 nonVested = nonVestedTokens(grant, uint64(now)); - - // remove grant from array - delete grants[_holder][_grantId]; - grants[_holder][_grantId] = grants[_holder][grants[_holder].length.sub(1)]; - grants[_holder].length -= 1; - - balances[receiver] = balances[receiver].add(nonVested); - balances[_holder] = balances[_holder].sub(nonVested); - - Transfer(_holder, receiver, nonVested); - } - - - /** - * @dev Calculate the total amount of transferable tokens of a holder at a given time - * @param holder address The address of the holder - * @param time uint64 The specific time. - * @return An uint256 representing a holder's total amount of transferable tokens. - */ - function transferableTokens(address holder, uint64 time) public constant returns (uint256) { - uint256 grantIndex = tokenGrantsCount(holder); - - if (grantIndex == 0) return super.transferableTokens(holder, time); // shortcut for holder without grants - - // Iterate through all the grants the holder has, and add all non-vested tokens - uint256 nonVested = 0; - for (uint256 i = 0; i < grantIndex; i++) { - nonVested = SafeMath.add(nonVested, nonVestedTokens(grants[holder][i], time)); - } - - // Balance - totalNonVested is the amount of tokens a holder can transfer at any given time - uint256 vestedTransferable = SafeMath.sub(balanceOf(holder), nonVested); - - // Return the minimum of how many vested can transfer and other value - // in case there are other limiting transferability factors (default is balanceOf) - return Math.min256(vestedTransferable, super.transferableTokens(holder, time)); - } - - /** - * @dev Check the amount of grants that an address has. - * @param _holder The holder of the grants. - * @return A uint256 representing the total amount of grants. - */ - function tokenGrantsCount(address _holder) public constant returns (uint256 index) { - return grants[_holder].length; - } - - /** - * @dev Calculate amount of vested tokens at a specific time - * @param tokens uint256 The amount of tokens granted - * @param time uint64 The time to be checked - * @param start uint64 The time representing the beginning of the grant - * @param cliff uint64 The cliff period, the period before nothing can be paid out - * @param vesting uint64 The vesting period - * @return An uint256 representing the amount of vested tokens of a specific grant - * transferableTokens - * | _/-------- vestedTokens rect - * | _/ - * | _/ - * | _/ - * | _/ - * | / - * | .| - * | . | - * | . | - * | . | - * | . | - * | . | - * +===+===========+---------+----------> time - * Start Cliff Vesting - */ - function calculateVestedTokens( - uint256 tokens, - uint256 time, - uint256 start, - uint256 cliff, - uint256 vesting) public constant returns (uint256) - { - // Shortcuts for before cliff and after vesting cases. - if (time < cliff) return 0; - if (time >= vesting) return tokens; - - // Interpolate all vested tokens. - // As before cliff the shortcut returns 0, we can use just calculate a value - // in the vesting rect (as shown in above's figure) - - // vestedTokens = (tokens * (time - start)) / (vesting - start) - uint256 vestedTokens = SafeMath.div( - SafeMath.mul( - tokens, - SafeMath.sub(time, start) - ), - SafeMath.sub(vesting, start) - ); - - return vestedTokens; - } - - /** - * @dev Get all information about a specific grant. - * @param _holder The address which will have its tokens revoked. - * @param _grantId The id of the token grant. - * @return Returns all the values that represent a TokenGrant(address, value, start, cliff, - * revokability, burnsOnRevoke, and vesting) plus the vested value at the current time. - */ - function tokenGrant(address _holder, uint256 _grantId) public constant returns (address granter, uint256 value, uint256 vested, uint64 start, uint64 cliff, uint64 vesting, bool revokable, bool burnsOnRevoke) { - TokenGrant storage grant = grants[_holder][_grantId]; - - granter = grant.granter; - value = grant.value; - start = grant.start; - cliff = grant.cliff; - vesting = grant.vesting; - revokable = grant.revokable; - burnsOnRevoke = grant.burnsOnRevoke; - - vested = vestedTokens(grant, uint64(now)); - } - - /** - * @dev Get the amount of vested tokens at a specific time. - * @param grant TokenGrant The grant to be checked. - * @param time The time to be checked - * @return An uint256 representing the amount of vested tokens of a specific grant at a specific time. - */ - 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) - ); - } - - /** - * @dev Calculate the amount of non vested tokens at a specific time. - * @param grant TokenGrant The grant to be checked. - * @param time uint64 The time to be checked - * @return An uint256 representing the amount of non vested tokens of a specific grant on the - * passed time frame. - */ - function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) { - return grant.value.sub(vestedTokens(grant, time)); - } - - /** - * @dev Calculate the date when the holder can transfer all its tokens - * @param holder address The address of the holder - * @return An uint256 representing the date of the last transferable tokens. - */ - function lastTokenIsTransferableDate(address holder) public constant returns (uint64 date) { - date = uint64(now); - uint256 grantIndex = grants[holder].length; - for (uint256 i = 0; i < grantIndex; i++) { - date = Math.max64(grants[holder][i].vesting, date); - } - } -} diff --git a/test/VestedToken.js b/test/VestedToken.js deleted file mode 100644 index c861eef97..000000000 --- a/test/VestedToken.js +++ /dev/null @@ -1,175 +0,0 @@ -const assertJump = require('./helpers/assertJump'); -const timer = require('./helpers/timer'); -var VestedTokenMock = artifacts.require("./helpers/VestedTokenMock.sol"); - -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 revokable/non-burnable token grant', async () => { - const cliff = 10000 - const vesting = 20000 // seconds - - beforeEach(async () => { - await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, true, false, { 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), tokenAmount); - }) - - it('throws when trying to transfer non vested tokens', async () => { - try { - await token.transfer(accounts[7], 1, { from: receiver }) - assert.fail('should have thrown before'); - } catch(error) { - assertJump(error); - } - }) - - it('throws when trying to transfer from non vested tokens', async () => { - try { - await token.approve(accounts[7], 1, { from: receiver }) - await token.transferFrom(receiver, accounts[7], tokenAmount, { from: accounts[7] }) - assert.fail('should have thrown before'); - } catch(error) { - assertJump(error); - } - }) - - 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] }); - assert.fail('should have thrown before'); - } catch(error) { - assertJump(error); - } - }) - - 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); - await token.transfer(accounts[7], tokenAmount, { from: receiver }) - assert.equal(await token.balanceOf(accounts[7]), tokenAmount); - }) - - it('can approve and transferFrom all tokens after vesting ends', async () => { - await timer(vesting); - await token.approve(accounts[7], tokenAmount, { from: receiver }) - await token.transferFrom(receiver, accounts[7], tokenAmount, { from: accounts[7] }) - assert.equal(await token.balanceOf(accounts[7]), tokenAmount); - }) - - it('can handle composed vesting schedules', async () => { - await timer(cliff); - await token.transfer(accounts[7], 12, { from: receiver }) - assert.equal(await token.balanceOf(accounts[7]), 12); - - let newNow = web3.eth.getBlock(web3.eth.blockNumber).timestamp - - await token.grantVestedTokens(receiver, tokenAmount, newNow, newNow + cliff, newNow + vesting, false, false, { from: granter }) - - await token.transfer(accounts[7], 13, { from: receiver }) - assert.equal(await token.balanceOf(accounts[7]), tokenAmount / 2); - - assert.equal(await token.balanceOf(receiver), 3 * tokenAmount / 2) - assert.equal(await token.transferableTokens(receiver, newNow), 0) - await timer(vesting); - await token.transfer(accounts[7], 3 * tokenAmount / 2, { from: receiver }) - assert.equal(await token.balanceOf(accounts[7]), tokenAmount * 2) - }) - }) - - describe('getting a non-revokable token grant', async () => { - const cliff = 10000 - const vesting = 20000 // seconds - - beforeEach(async () => { - await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, false, false, { from: granter }) - }) - - it('tokens are received', async () => { - assert.equal(await token.balanceOf(receiver), tokenAmount); - }) - - it('throws when granter attempts to revoke', async () => { - try { - await token.revokeTokenGrant(receiver, 0, { from: granter }); - assert.fail('should have thrown before'); - } catch(error) { - assertJump(error); - } - }) - }) - - describe('getting a revokable/burnable token grant', async () => { - const cliff = 100000 - const vesting = 200000 // seconds - const burnAddress = '0x000000000000000000000000000000000000dead' - - beforeEach(async () => { - await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, true, true, { from: granter }) - }) - - it('tokens are received', async () => { - assert.equal(await token.balanceOf(receiver), tokenAmount); - }) - - it('can be revoked by granter and tokens are burned', async () => { - await token.revokeTokenGrant(receiver, 0, { from: granter }); - assert.equal(await token.balanceOf(receiver), 0); - assert.equal(await token.balanceOf(burnAddress), tokenAmount); - }) - - it('cannot be revoked by non granter', async () => { - try { - await token.revokeTokenGrant(receiver, 0, { from: accounts[3] }); - assert.fail('should have thrown before'); - } catch(error) { - assertJump(error); - } - }) - - 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(burnAddress), tokenAmount * cliff / vesting); - }) - }) -}); From ffd1090718b9382af782349efa2e9f46b33e17dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Wed, 4 Oct 2017 18:08:36 -0300 Subject: [PATCH 17/17] [TokenVesting] Remove VestedTokenMock --- test/helpers/VestedTokenMock.sol | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 test/helpers/VestedTokenMock.sol diff --git a/test/helpers/VestedTokenMock.sol b/test/helpers/VestedTokenMock.sol deleted file mode 100644 index c607cae62..000000000 --- a/test/helpers/VestedTokenMock.sol +++ /dev/null @@ -1,11 +0,0 @@ -pragma solidity ^0.4.11; - -import '../../contracts/token/VestedToken.sol'; - -// mock class using StandardToken -contract VestedTokenMock is VestedToken { - function VestedTokenMock(address initialAccount, uint256 initialBalance) { - balances[initialAccount] = initialBalance; - totalSupply = initialBalance; - } -}