From d1e68d94cf6396b110c0e3d9348ee948db89ec49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Mon, 7 Jan 2019 16:52:38 -0300 Subject: [PATCH] Now using BN time values. --- package-lock.json | 6 +++--- package.json | 2 +- test/drafts/TokenVesting.test.js | 30 +++++++++++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0efa8781e..a28d3f836 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index e884cb9e6..d2c2d5aec 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/drafts/TokenVesting.test.js b/test/drafts/TokenVesting.test.js index 9950f780d..84a956c73 100644 --- a/test/drafts/TokenVesting.test.js +++ b/test/drafts/TokenVesting.test.js @@ -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); } }); });