Commit 46c5759b by Jakub Wojciechowski

Introduce increaseTimeTo helper method.

parent 7c883b63
import ether from './helpers/ether' import ether from './helpers/ether'
import {advanceBlock} from './helpers/advanceToBlock' import {advanceBlock} from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime' import {increaseTimeTo, duration} from './helpers/increaseTime'
import {duration, increaseTimeHandicap} from './helpers/increaseTime'
import latestTime from './helpers/latestTime' import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
...@@ -28,12 +27,8 @@ contract('CappedCrowdsale', function ([_, wallet]) { ...@@ -28,12 +27,8 @@ contract('CappedCrowdsale', function ([_, wallet]) {
}) })
beforeEach(async function () { beforeEach(async function () {
this.timeToStart = duration.weeks(1); this.startTime = latestTime().unix() + duration.weeks(1);
this.crowdsalePeriod = duration.weeks(1); this.endTime = this.startTime + duration.weeks(1);
this.startTime = latestTime().unix() + this.timeToStart;
this.endTime = this.startTime + this.crowdsalePeriod;
this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap) this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap)
...@@ -51,7 +46,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { ...@@ -51,7 +46,7 @@ contract('CappedCrowdsale', function ([_, wallet]) {
describe('accepting payments', function () { describe('accepting payments', function () {
beforeEach(async function () { beforeEach(async function () {
await increaseTime(this.timeToStart) await increaseTimeTo(this.startTime)
}) })
it('should accept payments within cap', async function () { it('should accept payments within cap', async function () {
...@@ -73,7 +68,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { ...@@ -73,7 +68,7 @@ contract('CappedCrowdsale', function ([_, wallet]) {
describe('ending', function () { describe('ending', function () {
beforeEach(async function () { beforeEach(async function () {
await increaseTime(this.timeToStart) await increaseTimeTo(this.startTime)
}) })
it('should not be ended if under cap', async function () { it('should not be ended if under cap', async function () {
......
import ether from './helpers/ether' import ether from './helpers/ether'
import {advanceBlock} from './helpers/advanceToBlock' import {advanceBlock} from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime' import {increaseTimeTo, duration} from './helpers/increaseTime'
import {duration, increaseTimeHandicap} from './helpers/increaseTime'
import latestTime from './helpers/latestTime' import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
...@@ -28,12 +27,10 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -28,12 +27,10 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
}) })
beforeEach(async function () { beforeEach(async function () {
this.timeToStart = duration.weeks(1); this.startTime = latestTime().unix() + duration.weeks(1);
this.crowdsalePeriod = duration.weeks(1); this.endTime = this.startTime + duration.weeks(1);
this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; 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) this.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet)
...@@ -48,7 +45,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -48,7 +45,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
it('should be ended only after end', async function () { it('should be ended only after end', async function () {
let ended = await this.crowdsale.hasEnded() let ended = await this.crowdsale.hasEnded()
ended.should.equal(false) ended.should.equal(false)
await increaseTime(this.timeToEnd) await increaseTimeTo(this.afterEndTime)
ended = await this.crowdsale.hasEnded() ended = await this.crowdsale.hasEnded()
ended.should.equal(true) ended.should.equal(true)
}) })
...@@ -61,13 +58,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -61,13 +58,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
}) })
it('should accept payments after start', async function () { 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.send(value).should.be.fulfilled
await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.fulfilled await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.fulfilled
}) })
it('should reject payments after end', async function () { 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.send(value).should.be.rejectedWith(EVMThrow)
await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).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]) { ...@@ -77,7 +74,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe('high-level purchase', function () { describe('high-level purchase', function () {
beforeEach(async function() { beforeEach(async function() {
await increaseTime(this.timeToStart) await increaseTimeTo(this.startTime)
}) })
it('should log purchase', async function () { it('should log purchase', async function () {
...@@ -116,7 +113,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -116,7 +113,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe('low-level purchase', function () { describe('low-level purchase', function () {
beforeEach(async function() { beforeEach(async function() {
await increaseTime(this.timeToStart) await increaseTimeTo(this.startTime)
}) })
it('should log purchase', async function () { it('should log purchase', async function () {
......
import {advanceBlock} from './helpers/advanceToBlock' import {advanceBlock} from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime' import {increaseTimeTo, duration} from './helpers/increaseTime'
import {duration, increaseTimeHandicap} from './helpers/increaseTime'
import latestTime from './helpers/latestTime' import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
...@@ -24,12 +23,10 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { ...@@ -24,12 +23,10 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
}) })
beforeEach(async function () { beforeEach(async function () {
this.timeToStart = duration.weeks(1); this.startTime = latestTime().unix() + duration.weeks(1)
this.crowdsalePeriod = duration.weeks(1); this.endTime = this.startTime + duration.weeks(1)
this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; 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}) this.crowdsale = await FinalizableCrowdsale.new(this.startTime, this.endTime, rate, wallet, {from: owner})
...@@ -41,30 +38,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { ...@@ -41,30 +38,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
}) })
it('cannot be finalized by third party after ending', async function () { 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) await this.crowdsale.finalize({from: thirdparty}).should.be.rejectedWith(EVMThrow)
}) })
it('can be finalized by owner after ending', async function () { 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 await this.crowdsale.finalize({from: owner}).should.be.fulfilled
}) })
it('cannot be finalized twice', async function () { 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})
await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow) await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow)
}) })
it('logs finalized', async function () { it('logs finalized', async function () {
await increaseTime(this.timeToEnd) await increaseTimeTo(this.afterEndTime)
const {logs} = await this.crowdsale.finalize({from: owner}) const {logs} = await this.crowdsale.finalize({from: owner})
const event = logs.find(e => e.event === 'Finalized') const event = logs.find(e => e.event === 'Finalized')
should.exist(event) should.exist(event)
}) })
it('finishes minting of token', async function () { it('finishes minting of token', async function () {
await increaseTime(this.timeToEnd) await increaseTimeTo(this.afterEndTime)
await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner})
const finished = await this.token.mintingFinished() const finished = await this.token.mintingFinished()
finished.should.equal(true) finished.should.equal(true)
......
import ether from './helpers/ether' import ether from './helpers/ether'
import {advanceBlock} from './helpers/advanceToBlock' import {advanceBlock} from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime' import {increaseTimeTo, duration} from './helpers/increaseTime'
import {duration, increaseTimeHandicap} from './helpers/increaseTime'
import latestTime from './helpers/latestTime' import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
...@@ -26,12 +25,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { ...@@ -26,12 +25,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
}) })
beforeEach(async function () { beforeEach(async function () {
this.timeToStart = duration.weeks(1); this.startTime = latestTime().unix() + duration.weeks(1)
this.crowdsalePeriod = duration.weeks(1); this.endTime = this.startTime + duration.weeks(1)
this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; this.afterEndTime = this.endTime + duration.seconds(1)
this.startTime = latestTime().unix() + this.timeToStart;
this.endTime = this.startTime + this.crowdsalePeriod;
this.crowdsale = await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, goal, {from: owner}) this.crowdsale = await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, goal, {from: owner})
}) })
...@@ -46,21 +42,21 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { ...@@ -46,21 +42,21 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
it('should deny refunds before end', async function () { it('should deny refunds before end', async function () {
await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) 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) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
}) })
it('should deny refunds after end if goal was reached', async function () { 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 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) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
}) })
it('should allow refunds after end if goal was not reached', async function () { 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 this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) await increaseTimeTo(this.afterEndTime)
await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner})
...@@ -73,9 +69,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { ...@@ -73,9 +69,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
}) })
it('should forward funds to wallet after end if goal was reached', async function () { 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 this.crowdsale.sendTransaction({value: goal, from: investor})
await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) await increaseTimeTo(this.afterEndTime)
const pre = web3.eth.getBalance(wallet) const pre = web3.eth.getBalance(wallet)
await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner})
......
import ether from './helpers/ether' import ether from './helpers/ether'
import {advanceBlock} from './helpers/advanceToBlock' import {advanceBlock} from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime' import {increaseTimeTo, duration} from './helpers/increaseTime'
import {duration, increaseTimeHandicap} from './helpers/increaseTime'
import latestTime from './helpers/latestTime' import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
...@@ -27,13 +26,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) { ...@@ -27,13 +26,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) {
}) })
beforeEach(async function () { beforeEach(async function () {
this.timeToStart = duration.weeks(1); this.startTime = latestTime().unix() + duration.weeks(1);
this.crowdsalePeriod = duration.weeks(1); this.endTime = this.startTime + duration.weeks(1);
this.timeToEnd = this.timeToStart + this.crowdsalePeriod + increaseTimeHandicap; this.afterEndTime = this.endTime + duration.seconds(1);
this.startTime = latestTime().unix() + this.timeToStart;
this.endTime = this.startTime + this.crowdsalePeriod;
this.crowdsale = await SampleCrowdsale.new(this.startTime, this.endTime, RATE, GOAL, CAP, wallet); this.crowdsale = await SampleCrowdsale.new(this.startTime, this.endTime, RATE, GOAL, CAP, wallet);
this.token = SampleCrowdsaleToken.at(await this.crowdsale.token()); this.token = SampleCrowdsaleToken.at(await this.crowdsale.token());
...@@ -61,7 +56,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) { ...@@ -61,7 +56,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) {
const investmentAmount = ether(1); const investmentAmount = ether(1);
const expectedTokenAmount = RATE.mul(investmentAmount); 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.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled;
(await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount); (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
...@@ -69,23 +64,23 @@ contract('Crowdsale', function ([owner, wallet, investor]) { ...@@ -69,23 +64,23 @@ contract('Crowdsale', function ([owner, wallet, investor]) {
}); });
it('should reject payments after end', async function () { 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.send(ether(1)).should.be.rejectedWith(EVMThrow);
await this.crowdsale.buyTokens(investor, {value: ether(1), from: investor}).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 () { 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(CAP);
await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow); 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 () { 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); await this.crowdsale.send(GOAL);
const beforeFinalization = web3.eth.getBalance(wallet); const beforeFinalization = web3.eth.getBalance(wallet);
await increaseTime(this.crowdsalePeriod + increaseTimeHandicap); await increaseTimeTo(this.afterEndTime);
await this.crowdsale.finalize({from: owner}); await this.crowdsale.finalize({from: owner});
const afterFinalization = web3.eth.getBalance(wallet); const afterFinalization = web3.eth.getBalance(wallet);
...@@ -95,9 +90,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) { ...@@ -95,9 +90,9 @@ contract('Crowdsale', function ([owner, wallet, investor]) {
it('should allow refunds if the goal is not reached', async function () { it('should allow refunds if the goal is not reached', async function () {
const balanceBeforeInvestment = web3.eth.getBalance(investor); 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 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.finalize({from: owner});
await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled; await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled;
......
// 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) { export default function increaseTime(duration) {
const id = Date.now() const id = Date.now()
...@@ -22,12 +24,24 @@ export default function increaseTime(duration) { ...@@ -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 = { export const duration = {
seconds: function(val) { return val}, seconds: function(val) { return val},
minutes: function(val) { return val * this.seconds(60) }, minutes: function(val) { return val * this.seconds(60) },
hours: function(val) { return val * this.minutes(60) }, hours: function(val) { return val * this.minutes(60) },
days: function(val) { return val * this.hours(24) }, days: function(val) { return val * this.hours(24) },
weeks: function(val) { return val * this.days(7) } weeks: function(val) { return val * this.days(7) }
}; };
\ No newline at end of file
export const increaseTimeHandicap = duration.seconds(10);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment