Now using BN time values.

This commit is contained in:
Nicolás Venturo
2019-01-07 16:52:38 -03:00
parent e517490ba2
commit d1e68d94cf
3 changed files with 19 additions and 19 deletions

View File

@ -8,7 +8,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
beforeEach(async function () {
// +1 minute so it starts after contract instantiation
this.start = (await time.latest()) + time.duration.minutes(1);
this.start = (await time.latest()).add(time.duration.minutes(1));
this.cliffDuration = time.duration.years(1);
this.duration = time.duration.years(2);
});
@ -17,7 +17,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
const cliffDuration = this.duration;
const duration = this.cliffDuration;
cliffDuration.should.be.gt(duration);
cliffDuration.should.be.bignumber.that.is.at.least(duration);
await shouldFail.reverting(
TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner })
@ -40,7 +40,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
it('reverts if the end time is in the past', async function () {
const now = await time.latest();
this.start = now - this.duration - time.duration.minutes(1);
this.start = now.sub(this.duration).sub(time.duration.minutes(1));
await shouldFail.reverting(
TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner })
);
@ -57,7 +57,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
it('can get state', async function () {
(await this.vesting.beneficiary()).should.be.equal(beneficiary);
(await this.vesting.cliff()).should.be.bignumber.equal(this.start + this.cliffDuration);
(await this.vesting.cliff()).should.be.bignumber.equal(this.start.add(this.cliffDuration));
(await this.vesting.start()).should.be.bignumber.equal(this.start);
(await this.vesting.duration()).should.be.bignumber.equal(this.duration);
(await this.vesting.revocable()).should.be.equal(true);
@ -68,7 +68,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('can be released after cliff', async function () {
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(1)));
const { logs } = await this.vesting.release(this.token.address);
expectEvent.inLogs(logs, 'TokensReleased', {
token: this.token.address,
@ -77,34 +77,34 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should release proper amount after cliff', async function () {
await time.increaseTo(this.start + this.cliffDuration);
await time.increaseTo(this.start.add(this.cliffDuration));
const { receipt } = await this.vesting.release(this.token.address);
const block = await web3.eth.getBlock(receipt.blockNumber);
const releaseTime = block.timestamp;
const releaseTime = await time.latest();
const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor();
const releasedAmount = amount.mul(releaseTime.sub(this.start)).div(this.duration);
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount);
(await this.vesting.released(this.token.address)).should.bignumber.equal(releasedAmount);
});
it('should linearly release tokens during vesting period', async function () {
const vestingPeriod = this.duration - this.cliffDuration;
const vestingPeriod = this.duration.sub(this.cliffDuration);
const checkpoints = 4;
for (let i = 1; i <= checkpoints; i++) {
const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints);
const now = this.start.add(this.cliffDuration).add((vestingPeriod.muln(i).divn(checkpoints)));
await time.increaseTo(now);
await this.vesting.release(this.token.address);
const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
const expectedVesting = amount.mul(now.sub(this.start)).div(this.duration);
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(expectedVesting);
(await this.vesting.released(this.token.address)).should.bignumber.equal(expectedVesting);
}
});
it('should have released all after end', async function () {
await time.increaseTo(this.start + this.duration);
await time.increaseTo(this.start.add(this.duration));
await this.vesting.release(this.token.address);
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount);
(await this.vesting.released(this.token.address)).should.bignumber.equal(amount);
@ -125,7 +125,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should return the non-vested tokens when revoked by owner', async function () {
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
const vested = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
@ -135,7 +135,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should keep the vested tokens when revoked by owner', async function () {
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
const vestedPre = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
@ -152,7 +152,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
function vestedAmount (total, now, start, cliffDuration, duration) {
return (now < start + cliffDuration) ? 0 : Math.round(total * (now - start) / duration);
return (now.lt(start.add(cliffDuration))) ? new BN(0) : total.mul((now.sub(start))).div(duration);
}
});
});