Change operations order for rounding. Make tests use blocktime as reference time rather than date.
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
pragma solidity ^0.4.4;
|
pragma solidity ^0.4.8;
|
||||||
|
|
||||||
import "./StandardToken.sol";
|
import "./StandardToken.sol";
|
||||||
|
|
||||||
@ -37,6 +37,7 @@ contract VestedToken is StandardToken {
|
|||||||
|
|
||||||
balances[msg.sender] = safeAdd(balances[msg.sender], nonVested);
|
balances[msg.sender] = safeAdd(balances[msg.sender], nonVested);
|
||||||
balances[_holder] = safeSub(balances[_holder], nonVested);
|
balances[_holder] = safeSub(balances[_holder], nonVested);
|
||||||
|
Transfer(_holder, msg.sender, nonVested);
|
||||||
}
|
}
|
||||||
|
|
||||||
function tokenGrantsCount(address _holder) constant returns (uint index) {
|
function tokenGrantsCount(address _holder) constant returns (uint index) {
|
||||||
@ -63,12 +64,12 @@ contract VestedToken is StandardToken {
|
|||||||
if (time < cliff) return 0;
|
if (time < cliff) return 0;
|
||||||
if (time > vesting) return tokens;
|
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;
|
vestedTokens = cliffTokens;
|
||||||
|
|
||||||
uint256 vestingTokens = safeSub(tokens, 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) {
|
function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
|
||||||
|
|||||||
@ -2,32 +2,29 @@ const assertJump = require('./helpers/assertJump');
|
|||||||
const timer = require('./helpers/timer');
|
const timer = require('./helpers/timer');
|
||||||
|
|
||||||
contract('VestedToken', function(accounts) {
|
contract('VestedToken', function(accounts) {
|
||||||
|
let token = null
|
||||||
let token = null;
|
let now = 0
|
||||||
let now = 0;
|
|
||||||
|
|
||||||
const tokenAmount = 50;
|
const tokenAmount = 50
|
||||||
|
|
||||||
const granter = accounts[3];
|
const granter = accounts[0]
|
||||||
const receiver = accounts[4];
|
const receiver = accounts[1]
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
token = await VestedTokenMock.new(granter, tokenAmount*2);
|
token = await VestedTokenMock.new(granter, 100);
|
||||||
now = +new Date()/1000;
|
now = web3.eth.getBlock(web3.eth.blockNumber).timestamp;
|
||||||
var block = await web3.eth.getBlock('latest');
|
})
|
||||||
now = block.timestamp;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('granter can grant tokens without vesting', async () => {
|
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.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 () => {
|
describe('getting a token grant', async () => {
|
||||||
const cliff = 10;
|
const cliff = 10000
|
||||||
const vesting = 20; // seconds
|
const vesting = 20000 // seconds
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await token.grantVestedTokens(receiver, tokenAmount, now, now + cliff, now + vesting, { from: granter })
|
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 () => {
|
it('throws when trying to transfer non vested tokens', async () => {
|
||||||
try {
|
try {
|
||||||
await token.transfer(accounts[9], 1, { from: receiver })
|
await token.transfer(accounts[7], 1, { from: receiver })
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return assertJump(error);
|
return assertJump(error);
|
||||||
}
|
}
|
||||||
@ -62,27 +59,23 @@ contract('VestedToken', function(accounts) {
|
|||||||
|
|
||||||
it('cannot be revoked by non granter', async () => {
|
it('cannot be revoked by non granter', async () => {
|
||||||
try {
|
try {
|
||||||
await token.revokeTokenGrant(receiver, 0, { from: accounts[9] });
|
await token.revokeTokenGrant(receiver, 0, { from: accounts[3] });
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return assertJump(error);
|
return assertJump(error);
|
||||||
}
|
}
|
||||||
assert.fail('should have thrown before');
|
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 timer(cliff);
|
||||||
await token.revokeTokenGrant(receiver, 0, { from: granter });
|
await token.revokeTokenGrant(receiver, 0, { from: granter });
|
||||||
var balance = await token.balanceOf(receiver);
|
assert.equal(await token.balanceOf(receiver), tokenAmount * cliff / vesting);
|
||||||
var expectedBalance = tokenAmount * cliff / vesting;
|
|
||||||
console.log('real balance', balance.toString());
|
|
||||||
console.log('expected ', expectedBalance);
|
|
||||||
assert.equal(balance, expectedBalance);
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can transfer all tokens after vesting ends', async () => {
|
it('can transfer all tokens after vesting ends', async () => {
|
||||||
await timer(vesting + 1);
|
await timer(vesting + 1);
|
||||||
await token.transfer(accounts[9], tokenAmount, { from: receiver })
|
await token.transfer(accounts[7], tokenAmount, { from: receiver })
|
||||||
assert.equal(await token.balanceOf(accounts[9]), tokenAmount);
|
assert.equal(await token.balanceOf(accounts[7]), tokenAmount);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user