From 0a5af4b8ac929070aaff4efc10ca218c25d93eaa Mon Sep 17 00:00:00 2001 From: Jorge Izquierdo Date: Mon, 13 Feb 2017 11:35:32 +0100 Subject: [PATCH] Change operations order for rounding. Make tests use blocktime as reference time rather than date. --- contracts/token/VestedToken.sol | 7 +++--- test/VestedToken.js | 43 ++++++++++++++------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/contracts/token/VestedToken.sol b/contracts/token/VestedToken.sol index e42b5ff9c..9aec2d282 100644 --- a/contracts/token/VestedToken.sol +++ b/contracts/token/VestedToken.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.4; +pragma solidity ^0.4.8; import "./StandardToken.sol"; @@ -37,6 +37,7 @@ contract VestedToken is StandardToken { 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) { @@ -63,12 +64,12 @@ contract VestedToken is StandardToken { if (time < cliff) return 0; if (time > vesting) return tokens; - uint256 cliffTokens = safeMul(tokens, safeDiv(safeSub(cliff, start), safeSub(vesting, start))); + uint256 cliffTokens = safeDiv(safeMul(tokens, safeSub(cliff, start)), safeSub(vesting, start)); vestedTokens = cliffTokens; uint256 vestingTokens = safeSub(tokens, cliffTokens); - vestedTokens = safeAdd(vestedTokens, safeMul(vestingTokens, safeDiv(safeSub(time, cliff), safeSub(vesting, start)))); + vestedTokens = safeAdd(vestedTokens, safeDiv(safeMul(vestingTokens, safeSub(time, cliff)), safeSub(vesting, start))); } function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) { diff --git a/test/VestedToken.js b/test/VestedToken.js index c895f2cb2..ebd6a587d 100644 --- a/test/VestedToken.js +++ b/test/VestedToken.js @@ -2,32 +2,29 @@ const assertJump = require('./helpers/assertJump'); const timer = require('./helpers/timer'); contract('VestedToken', function(accounts) { - - let token = null; - let now = 0; + let token = null + let now = 0 - const tokenAmount = 50; + const tokenAmount = 50 - const granter = accounts[3]; - const receiver = accounts[4]; + const granter = accounts[0] + const receiver = accounts[1] beforeEach(async () => { - token = await VestedTokenMock.new(granter, tokenAmount*2); - now = +new Date()/1000; - var block = await web3.eth.getBlock('latest'); - now = block.timestamp; - }); + 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 }); + await token.transfer(receiver, tokenAmount, { from: granter }) assert.equal(await token.balanceOf(receiver), tokenAmount); - assert.equal(await token.transferableTokens(receiver, +new Date()/1000), tokenAmount); + assert.equal(await token.transferableTokens(receiver, now), tokenAmount); }) describe('getting a token grant', async () => { - const cliff = 10; - const vesting = 20; // seconds + const cliff = 10000 + const vesting = 20000 // seconds beforeEach(async () => { await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, { from: granter }) @@ -47,7 +44,7 @@ contract('VestedToken', function(accounts) { it('throws when trying to transfer non vested tokens', async () => { try { - await token.transfer(accounts[9], 1, { from: receiver }) + await token.transfer(accounts[7], 1, { from: receiver }) } catch(error) { return assertJump(error); } @@ -62,27 +59,23 @@ contract('VestedToken', function(accounts) { it('cannot be revoked by non granter', async () => { try { - await token.revokeTokenGrant(receiver, 0, { from: accounts[9] }); + await token.revokeTokenGrant(receiver, 0, { from: accounts[3] }); } catch(error) { return assertJump(error); } assert.fail('should have thrown before'); }) - it.only('can be revoked by granter and non vested tokens are returned', async () => { + it('can be revoked by granter and non vested tokens are returned', async () => { await timer(cliff); await token.revokeTokenGrant(receiver, 0, { from: granter }); - var balance = await token.balanceOf(receiver); - var expectedBalance = tokenAmount * cliff / vesting; - console.log('real balance', balance.toString()); - console.log('expected ', expectedBalance); - assert.equal(balance, expectedBalance); + 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[9], tokenAmount, { from: receiver }) - assert.equal(await token.balanceOf(accounts[9]), tokenAmount); + await token.transfer(accounts[7], tokenAmount, { from: receiver }) + assert.equal(await token.balanceOf(accounts[7]), tokenAmount); }) }) });