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] [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); - }) - }) -});