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

6
package-lock.json generated
View File

@ -5796,9 +5796,9 @@
}
},
"openzeppelin-test-helpers": {
"version": "0.1.0-beta.1",
"resolved": "https://registry.npmjs.org/openzeppelin-test-helpers/-/openzeppelin-test-helpers-0.1.0-beta.1.tgz",
"integrity": "sha512-Vsvo78bsfkA5ju06spPpMKNV9e3KHUJjORSMWYnHLp33Kl96mrWfWSGfKsTH/1x0AbNI0LgRqFHMupV4ohTflg==",
"version": "0.1.0-beta.2",
"resolved": "https://registry.npmjs.org/openzeppelin-test-helpers/-/openzeppelin-test-helpers-0.1.0-beta.2.tgz",
"integrity": "sha512-xX1s8GZvQvC5QyuG8C0UpWBY4nLdIE5lg348wFCV8d4p/dUO9dGhB8RS+rxyjJ7jHDMzpRLKgCmVuQgetQhYWw==",
"dev": true,
"requires": {
"chai": "^4.2.0",

View File

@ -53,7 +53,7 @@
"ethereumjs-util": "^6.0.0",
"ethjs-abi": "^0.2.1",
"ganache-cli": "6.1.8",
"openzeppelin-test-helpers": "0.1.0-beta.1",
"openzeppelin-test-helpers": "0.1.0-beta.2",
"pify": "^4.0.1",
"solhint": "^1.5.0",
"solidity-coverage": "^0.5.4",

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