diff --git a/test/CappedCrowdsale.js b/test/CappedCrowdsale.js index bcc4e4a78..9dd027b35 100644 --- a/test/CappedCrowdsale.js +++ b/test/CappedCrowdsale.js @@ -1,7 +1,6 @@ import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' -import increaseTime from './helpers/increaseTime' -import {duration, increaseTimeHandicap} from './helpers/increaseTime' +import {increaseTimeTo, duration} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -28,12 +27,8 @@ contract('CappedCrowdsale', function ([_, wallet]) { }) beforeEach(async function () { - this.timeToStart = duration.weeks(1); - this.crowdsalePeriod = duration.weeks(1); - - this.startTime = latestTime().unix() + this.timeToStart; - this.endTime = this.startTime + this.crowdsalePeriod; - + this.startTime = latestTime().unix() + duration.weeks(1); + this.endTime = this.startTime + duration.weeks(1); this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap) @@ -51,7 +46,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { describe('accepting payments', function () { beforeEach(async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) }) it('should accept payments within cap', async function () { @@ -73,7 +68,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { describe('ending', function () { beforeEach(async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) }) it('should not be ended if under cap', async function () { diff --git a/test/Crowdsale.js b/test/Crowdsale.js index 20c1b365d..e3abe1975 100644 --- a/test/Crowdsale.js +++ b/test/Crowdsale.js @@ -1,7 +1,6 @@ import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' -import increaseTime from './helpers/increaseTime' -import {duration, increaseTimeHandicap} from './helpers/increaseTime' +import {increaseTimeTo, duration} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -28,12 +27,10 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }) beforeEach(async function () { - this.timeToStart = duration.weeks(1); - this.crowdsalePeriod = duration.weeks(1); - this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; + this.startTime = latestTime().unix() + duration.weeks(1); + this.endTime = this.startTime + duration.weeks(1); + this.afterEndTime = this.endTime + duration.seconds(1) - this.startTime = latestTime().unix() + this.timeToStart; - this.endTime = this.startTime + this.crowdsalePeriod; this.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet) @@ -48,7 +45,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { it('should be ended only after end', async function () { let ended = await this.crowdsale.hasEnded() ended.should.equal(false) - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) ended = await this.crowdsale.hasEnded() ended.should.equal(true) }) @@ -61,13 +58,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }) it('should accept payments after start', async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) await this.crowdsale.send(value).should.be.fulfilled await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.fulfilled }) it('should reject payments after end', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.send(value).should.be.rejectedWith(EVMThrow) await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.rejectedWith(EVMThrow) }) @@ -77,7 +74,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('high-level purchase', function () { beforeEach(async function() { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) }) it('should log purchase', async function () { @@ -116,7 +113,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('low-level purchase', function () { beforeEach(async function() { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) }) it('should log purchase', async function () { diff --git a/test/FinalizableCrowdsale.js b/test/FinalizableCrowdsale.js index af14708ea..a7e76bfbf 100644 --- a/test/FinalizableCrowdsale.js +++ b/test/FinalizableCrowdsale.js @@ -1,6 +1,5 @@ import {advanceBlock} from './helpers/advanceToBlock' -import increaseTime from './helpers/increaseTime' -import {duration, increaseTimeHandicap} from './helpers/increaseTime' +import {increaseTimeTo, duration} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -24,12 +23,10 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }) beforeEach(async function () { - this.timeToStart = duration.weeks(1); - this.crowdsalePeriod = duration.weeks(1); - this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; + this.startTime = latestTime().unix() + duration.weeks(1) + this.endTime = this.startTime + duration.weeks(1) + this.afterEndTime = this.endTime + duration.seconds(1) - this.startTime = latestTime().unix() + this.timeToStart; - this.endTime = this.startTime + this.crowdsalePeriod; this.crowdsale = await FinalizableCrowdsale.new(this.startTime, this.endTime, rate, wallet, {from: owner}) @@ -41,30 +38,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }) it('cannot be finalized by third party after ending', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.finalize({from: thirdparty}).should.be.rejectedWith(EVMThrow) }) it('can be finalized by owner after ending', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.finalize({from: owner}).should.be.fulfilled }) it('cannot be finalized twice', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow) }) it('logs finalized', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) const {logs} = await this.crowdsale.finalize({from: owner}) const event = logs.find(e => e.event === 'Finalized') should.exist(event) }) it('finishes minting of token', async function () { - await increaseTime(this.timeToEnd) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.finalize({from: owner}) const finished = await this.token.mintingFinished() finished.should.equal(true) diff --git a/test/RefundableCrowdsale.js b/test/RefundableCrowdsale.js index 6f0fc39a7..d3150a94f 100644 --- a/test/RefundableCrowdsale.js +++ b/test/RefundableCrowdsale.js @@ -1,7 +1,6 @@ import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' -import increaseTime from './helpers/increaseTime' -import {duration, increaseTimeHandicap} from './helpers/increaseTime' +import {increaseTimeTo, duration} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -26,12 +25,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { }) beforeEach(async function () { - this.timeToStart = duration.weeks(1); - this.crowdsalePeriod = duration.weeks(1); - this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; - - this.startTime = latestTime().unix() + this.timeToStart; - this.endTime = this.startTime + this.crowdsalePeriod; + this.startTime = latestTime().unix() + duration.weeks(1) + this.endTime = this.startTime + duration.weeks(1) + this.afterEndTime = this.endTime + duration.seconds(1) this.crowdsale = await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, goal, {from: owner}) }) @@ -46,21 +42,21 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { it('should deny refunds before end', async function () { await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) }) it('should deny refunds after end if goal was reached', async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) }) it('should allow refunds after end if goal was not reached', async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor}) - await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) + await increaseTimeTo(this.afterEndTime) await this.crowdsale.finalize({from: owner}) @@ -73,9 +69,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { }) it('should forward funds to wallet after end if goal was reached', async function () { - await increaseTime(this.timeToStart) + await increaseTimeTo(this.startTime) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) + await increaseTimeTo(this.afterEndTime) const pre = web3.eth.getBalance(wallet) await this.crowdsale.finalize({from: owner}) diff --git a/test/SampleCrowdsale.js b/test/SampleCrowdsale.js index dbcf2ba56..b17c15aeb 100644 --- a/test/SampleCrowdsale.js +++ b/test/SampleCrowdsale.js @@ -1,7 +1,6 @@ import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' -import increaseTime from './helpers/increaseTime' -import {duration, increaseTimeHandicap} from './helpers/increaseTime' +import {increaseTimeTo, duration} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -27,13 +26,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) { }) beforeEach(async function () { - this.timeToStart = duration.weeks(1); - this.crowdsalePeriod = duration.weeks(1); - this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; - - this.startTime = latestTime().unix() + this.timeToStart; - this.endTime = this.startTime + this.crowdsalePeriod; - + this.startTime = latestTime().unix() + duration.weeks(1); + this.endTime = this.startTime + duration.weeks(1); + this.afterEndTime = this.endTime + duration.seconds(1); this.crowdsale = await SampleCrowdsale.new(this.startTime, this.endTime, RATE, GOAL, CAP, wallet); this.token = SampleCrowdsaleToken.at(await this.crowdsale.token()); @@ -61,7 +56,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) { const investmentAmount = ether(1); const expectedTokenAmount = RATE.mul(investmentAmount); - await increaseTime(this.timeToStart); + await increaseTimeTo(this.startTime); await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled; (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount); @@ -69,23 +64,23 @@ contract('Crowdsale', function ([owner, wallet, investor]) { }); it('should reject payments after end', async function () { - await increaseTime(this.timeToEnd); + await increaseTimeTo(this.afterEnd); await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow); await this.crowdsale.buyTokens(investor, {value: ether(1), from: investor}).should.be.rejectedWith(EVMThrow); }); it('should reject payments over cap', async function () { - await increaseTime(this.timeToStart); + await increaseTimeTo(this.startTime); await this.crowdsale.send(CAP); await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow); }); it('should allow finalization and transfer funds to wallet if the goal is reached', async function () { - await increaseTime(this.timeToStart); + await increaseTimeTo(this.startTime); await this.crowdsale.send(GOAL); const beforeFinalization = web3.eth.getBalance(wallet); - await increaseTime(this.crowdsalePeriod + increaseTimeHandicap); + await increaseTimeTo(this.afterEndTime); await this.crowdsale.finalize({from: owner}); const afterFinalization = web3.eth.getBalance(wallet); @@ -95,9 +90,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) { it('should allow refunds if the goal is not reached', async function () { const balanceBeforeInvestment = web3.eth.getBalance(investor); - await increaseTime(this.timeToStart); + await increaseTimeTo(this.startTime); await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0}); - await increaseTime(this.crowdsalePeriod + increaseTimeHandicap); + await increaseTimeTo(this.afterEndTime); await this.crowdsale.finalize({from: owner}); await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled; diff --git a/test/helpers/increaseTime.js b/test/helpers/increaseTime.js index 06f7d9c0b..5a6a8e2c2 100644 --- a/test/helpers/increaseTime.js +++ b/test/helpers/increaseTime.js @@ -1,4 +1,6 @@ -// Increases testrpc time by the passed duration (a moment.js instance) +import latestTime from './latestTime' + +// Increases testrpc time by the passed duration in seconds export default function increaseTime(duration) { const id = Date.now() @@ -22,12 +24,24 @@ export default function increaseTime(duration) { }) } +/** + * Beware that due to the need of calling two separate testrpc methods and rpc calls overhead + * it's hard to increase time precisely to a target point so design your test to tolerate + * small fluctuations from time to time. + * + * @param target time in seconds + */ +export function increaseTimeTo(target) { + let now = latestTime().unix(); + if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`); + let diff = target - now; + return increaseTime(diff); +} + export const duration = { seconds: function(val) { return val}, minutes: function(val) { return val * this.seconds(60) }, hours: function(val) { return val * this.minutes(60) }, days: function(val) { return val * this.hours(24) }, weeks: function(val) { return val * this.days(7) } -}; - -export const increaseTimeHandicap = duration.seconds(10); \ No newline at end of file +}; \ No newline at end of file