Skip to content

Commit 34bc709

Browse files
authored
Merged latestTime, increaseTime and duration into a time helper. (#1364)
1 parent 6ae041b commit 34bc709

10 files changed

+84
-95
lines changed

test/crowdsale/FinalizableCrowdsale.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const expectEvent = require('../helpers/expectEvent');
22
const { advanceBlock } = require('../helpers/advanceToBlock');
3-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
4-
const { latestTime } = require('../helpers/latestTime');
3+
const time = require('../helpers/time');
54
const { expectThrow } = require('../helpers/expectThrow');
65
const { EVMRevert } = require('../helpers/EVMRevert');
76

@@ -23,9 +22,9 @@ contract('FinalizableCrowdsale', function ([_, wallet, anyone]) {
2322
});
2423

2524
beforeEach(async function () {
26-
this.openingTime = (await latestTime()) + duration.weeks(1);
27-
this.closingTime = this.openingTime + duration.weeks(1);
28-
this.afterClosingTime = this.closingTime + duration.seconds(1);
25+
this.openingTime = (await time.latest()) + time.duration.weeks(1);
26+
this.closingTime = this.openingTime + time.duration.weeks(1);
27+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
2928

3029
this.token = await ERC20.new();
3130
this.crowdsale = await FinalizableCrowdsaleImpl.new(
@@ -38,18 +37,18 @@ contract('FinalizableCrowdsale', function ([_, wallet, anyone]) {
3837
});
3938

4039
it('can be finalized by anyone after ending', async function () {
41-
await increaseTimeTo(this.afterClosingTime);
40+
await time.increaseTo(this.afterClosingTime);
4241
await this.crowdsale.finalize({ from: anyone });
4342
});
4443

4544
it('cannot be finalized twice', async function () {
46-
await increaseTimeTo(this.afterClosingTime);
45+
await time.increaseTo(this.afterClosingTime);
4746
await this.crowdsale.finalize({ from: anyone });
4847
await expectThrow(this.crowdsale.finalize({ from: anyone }), EVMRevert);
4948
});
5049

5150
it('logs finalized', async function () {
52-
await increaseTimeTo(this.afterClosingTime);
51+
await time.increaseTo(this.afterClosingTime);
5352
const { logs } = await this.crowdsale.finalize({ from: anyone });
5453
expectEvent.inLogs(logs, 'CrowdsaleFinalized');
5554
});

test/crowdsale/IncreasingPriceCrowdsale.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { ether } = require('../helpers/ether');
22
const { advanceBlock } = require('../helpers/advanceToBlock');
3-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
4-
const { latestTime } = require('../helpers/latestTime');
3+
const time = require('../helpers/time');
54
const { assertRevert } = require('../helpers/assertRevert');
65

76
const BigNumber = web3.BigNumber;
@@ -29,9 +28,9 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser])
2928

3029
beforeEach(async function () {
3130
await advanceBlock();
32-
this.startTime = (await latestTime()) + duration.weeks(1);
33-
this.closingTime = this.startTime + duration.weeks(1);
34-
this.afterClosingTime = this.closingTime + duration.seconds(1);
31+
this.startTime = (await time.latest()) + time.duration.weeks(1);
32+
this.closingTime = this.startTime + time.duration.weeks(1);
33+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
3534
this.token = await SimpleToken.new();
3635
});
3736

@@ -61,43 +60,43 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser])
6160
});
6261

6362
it('at start', async function () {
64-
await increaseTimeTo(this.startTime);
63+
await time.increaseTo(this.startTime);
6564
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
6665
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate));
6766
});
6867

6968
it('at time 150', async function () {
70-
await increaseTimeTo(this.startTime + 150);
69+
await time.increaseTo(this.startTime + 150);
7170
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
7271
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150));
7372
});
7473

7574
it('at time 300', async function () {
76-
await increaseTimeTo(this.startTime + 300);
75+
await time.increaseTo(this.startTime + 300);
7776
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
7877
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300));
7978
});
8079

8180
it('at time 1500', async function () {
82-
await increaseTimeTo(this.startTime + 1500);
81+
await time.increaseTo(this.startTime + 1500);
8382
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
8483
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500));
8584
});
8685

8786
it('at time 30', async function () {
88-
await increaseTimeTo(this.startTime + 30);
87+
await time.increaseTo(this.startTime + 30);
8988
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
9089
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30));
9190
});
9291

9392
it('at time 150000', async function () {
94-
await increaseTimeTo(this.startTime + 150000);
93+
await time.increaseTo(this.startTime + 150000);
9594
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
9695
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000));
9796
});
9897

9998
it('at time 450000', async function () {
100-
await increaseTimeTo(this.startTime + 450000);
99+
await time.increaseTo(this.startTime + 450000);
101100
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
102101
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000));
103102
});

test/crowdsale/PostDeliveryCrowdsale.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { advanceBlock } = require('../helpers/advanceToBlock');
2-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
3-
const { latestTime } = require('../helpers/latestTime');
2+
const time = require('../helpers/time');
43
const { expectThrow } = require('../helpers/expectThrow');
54
const { EVMRevert } = require('../helpers/EVMRevert');
65
const { ether } = require('../helpers/ether');
@@ -24,9 +23,9 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
2423
});
2524

2625
beforeEach(async function () {
27-
this.openingTime = (await latestTime()) + duration.weeks(1);
28-
this.closingTime = this.openingTime + duration.weeks(1);
29-
this.afterClosingTime = this.closingTime + duration.seconds(1);
26+
this.openingTime = (await time.latest()) + time.duration.weeks(1);
27+
this.closingTime = this.openingTime + time.duration.weeks(1);
28+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
3029
this.token = await SimpleToken.new();
3130
this.crowdsale = await PostDeliveryCrowdsaleImpl.new(
3231
this.openingTime, this.closingTime, rate, wallet, this.token.address
@@ -36,7 +35,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
3635

3736
context('after opening time', function () {
3837
beforeEach(async function () {
39-
await increaseTimeTo(this.openingTime);
38+
await time.increaseTo(this.openingTime);
4039
});
4140

4241
context('with bought tokens', function () {
@@ -57,7 +56,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
5756

5857
context('after closing time', function () {
5958
beforeEach(async function () {
60-
await increaseTimeTo(this.afterClosingTime);
59+
await time.increaseTo(this.afterClosingTime);
6160
});
6261

6362
it('allows beneficiaries to withdraw tokens', async function () {

test/crowdsale/RefundableCrowdsale.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { ether } = require('../helpers/ether');
22
const { advanceBlock } = require('../helpers/advanceToBlock');
3-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
4-
const { latestTime } = require('../helpers/latestTime');
3+
const time = require('../helpers/time');
54
const { expectThrow } = require('../helpers/expectThrow');
65
const { EVMRevert } = require('../helpers/EVMRevert');
76
const { ethGetBalance } = require('../helpers/web3');
@@ -27,9 +26,9 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
2726
});
2827

2928
beforeEach(async function () {
30-
this.openingTime = (await latestTime()) + duration.weeks(1);
31-
this.closingTime = this.openingTime + duration.weeks(1);
32-
this.afterClosingTime = this.closingTime + duration.seconds(1);
29+
this.openingTime = (await time.latest()) + time.duration.weeks(1);
30+
this.closingTime = this.openingTime + time.duration.weeks(1);
31+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
3332
this.preWalletBalance = await ethGetBalance(wallet);
3433

3534
this.token = await SimpleToken.new();
@@ -61,7 +60,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
6160

6261
context('after opening time', function () {
6362
beforeEach(async function () {
64-
await increaseTimeTo(this.openingTime);
63+
await time.increaseTo(this.openingTime);
6564
});
6665

6766
it('denies refunds', async function () {
@@ -75,7 +74,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
7574

7675
context('after closing time and finalization', function () {
7776
beforeEach(async function () {
78-
await increaseTimeTo(this.afterClosingTime);
77+
await time.increaseTo(this.afterClosingTime);
7978
await this.crowdsale.finalize({ from: anyone });
8079
});
8180

@@ -95,7 +94,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
9594

9695
context('after closing time and finalization', function () {
9796
beforeEach(async function () {
98-
await increaseTimeTo(this.afterClosingTime);
97+
await time.increaseTo(this.afterClosingTime);
9998
await this.crowdsale.finalize({ from: anyone });
10099
});
101100

test/crowdsale/TimedCrowdsale.test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { ether } = require('../helpers/ether');
22
const { advanceBlock } = require('../helpers/advanceToBlock');
3-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
4-
const { latestTime } = require('../helpers/latestTime');
3+
const time = require('../helpers/time');
54
const { expectThrow } = require('../helpers/expectThrow');
65
const { EVMRevert } = require('../helpers/EVMRevert');
76

@@ -25,21 +24,21 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
2524
});
2625

2726
beforeEach(async function () {
28-
this.openingTime = (await latestTime()) + duration.weeks(1);
29-
this.closingTime = this.openingTime + duration.weeks(1);
30-
this.afterClosingTime = this.closingTime + duration.seconds(1);
27+
this.openingTime = (await time.latest()) + time.duration.weeks(1);
28+
this.closingTime = this.openingTime + time.duration.weeks(1);
29+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
3130
this.token = await SimpleToken.new();
3231
});
3332

3433
it('rejects an opening time in the past', async function () {
3534
await expectThrow(TimedCrowdsaleImpl.new(
36-
(await latestTime()) - duration.days(1), this.closingTime, rate, wallet, this.token.address
35+
(await time.latest()) - time.duration.days(1), this.closingTime, rate, wallet, this.token.address
3736
), EVMRevert);
3837
});
3938

4039
it('rejects a closing time before the opening time', async function () {
4140
await expectThrow(TimedCrowdsaleImpl.new(
42-
this.openingTime, this.openingTime - duration.seconds(1), rate, wallet, this.token.address
41+
this.openingTime, this.openingTime - time.duration.seconds(1), rate, wallet, this.token.address
4342
), EVMRevert);
4443
});
4544

@@ -53,7 +52,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
5352

5453
it('should be ended only after end', async function () {
5554
(await this.crowdsale.hasClosed()).should.equal(false);
56-
await increaseTimeTo(this.afterClosingTime);
55+
await time.increaseTo(this.afterClosingTime);
5756
(await this.crowdsale.isOpen()).should.equal(false);
5857
(await this.crowdsale.hasClosed()).should.equal(true);
5958
});
@@ -66,14 +65,14 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
6665
});
6766

6867
it('should accept payments after start', async function () {
69-
await increaseTimeTo(this.openingTime);
68+
await time.increaseTo(this.openingTime);
7069
(await this.crowdsale.isOpen()).should.equal(true);
7170
await this.crowdsale.send(value);
7271
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
7372
});
7473

7574
it('should reject payments after end', async function () {
76-
await increaseTimeTo(this.afterClosingTime);
75+
await time.increaseTo(this.afterClosingTime);
7776
await expectThrow(this.crowdsale.send(value), EVMRevert);
7877
await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert);
7978
});

test/examples/SampleCrowdsale.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { ether } = require('../helpers/ether');
22
const { advanceBlock } = require('../helpers/advanceToBlock');
3-
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
4-
const { latestTime } = require('../helpers/latestTime');
3+
const time = require('../helpers/time');
54
const { expectThrow } = require('../helpers/expectThrow');
65
const { EVMRevert } = require('../helpers/EVMRevert');
76
const { assertRevert } = require('../helpers/assertRevert');
@@ -27,9 +26,9 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
2726
});
2827

2928
beforeEach(async function () {
30-
this.openingTime = (await latestTime()) + duration.weeks(1);
31-
this.closingTime = this.openingTime + duration.weeks(1);
32-
this.afterClosingTime = this.closingTime + duration.seconds(1);
29+
this.openingTime = (await time.latest()) + time.duration.weeks(1);
30+
this.closingTime = this.openingTime + time.duration.weeks(1);
31+
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
3332

3433
this.token = await SampleCrowdsaleToken.new({ from: deployer });
3534
this.crowdsale = await SampleCrowdsale.new(
@@ -68,31 +67,31 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
6867
const investmentAmount = ether(1);
6968
const expectedTokenAmount = RATE.mul(investmentAmount);
7069

71-
await increaseTimeTo(this.openingTime);
70+
await time.increaseTo(this.openingTime);
7271
await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor });
7372

7473
(await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
7574
(await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
7675
});
7776

7877
it('should reject payments after end', async function () {
79-
await increaseTimeTo(this.afterClosingTime);
78+
await time.increaseTo(this.afterClosingTime);
8079
await expectThrow(this.crowdsale.send(ether(1)), EVMRevert);
8180
await expectThrow(this.crowdsale.buyTokens(investor, { value: ether(1), from: investor }), EVMRevert);
8281
});
8382

8483
it('should reject payments over cap', async function () {
85-
await increaseTimeTo(this.openingTime);
84+
await time.increaseTo(this.openingTime);
8685
await this.crowdsale.send(CAP);
8786
await expectThrow(this.crowdsale.send(1), EVMRevert);
8887
});
8988

9089
it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
91-
await increaseTimeTo(this.openingTime);
90+
await time.increaseTo(this.openingTime);
9291
await this.crowdsale.send(GOAL);
9392

9493
const beforeFinalization = await ethGetBalance(wallet);
95-
await increaseTimeTo(this.afterClosingTime);
94+
await time.increaseTo(this.afterClosingTime);
9695
await this.crowdsale.finalize({ from: owner });
9796
const afterFinalization = await ethGetBalance(wallet);
9897

@@ -102,9 +101,9 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
102101
it('should allow refunds if the goal is not reached', async function () {
103102
const balanceBeforeInvestment = await ethGetBalance(investor);
104103

105-
await increaseTimeTo(this.openingTime);
104+
await time.increaseTo(this.openingTime);
106105
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
107-
await increaseTimeTo(this.afterClosingTime);
106+
await time.increaseTo(this.afterClosingTime);
108107

109108
await this.crowdsale.finalize({ from: owner });
110109
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });

test/helpers/latestTime.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/helpers/increaseTime.js renamed to test/helpers/time.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
const { latestTime } = require('./latestTime');
1+
const { ethGetBlock } = require('./web3');
2+
3+
// Returns the time of the last mined block in seconds
4+
async function latest () {
5+
const block = await ethGetBlock('latest');
6+
return block.timestamp;
7+
}
28

39
// Increases ganache time by the passed duration in seconds
4-
function increaseTime (duration) {
10+
function increase (duration) {
511
const id = Date.now();
612

713
return new Promise((resolve, reject) => {
@@ -31,12 +37,12 @@ function increaseTime (duration) {
3137
*
3238
* @param target time in seconds
3339
*/
34-
async function increaseTimeTo (target) {
35-
const now = (await latestTime());
40+
async function increaseTo (target) {
41+
const now = (await latest());
3642

3743
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
3844
const diff = target - now;
39-
return increaseTime(diff);
45+
return increase(diff);
4046
}
4147

4248
const duration = {
@@ -49,7 +55,8 @@ const duration = {
4955
};
5056

5157
module.exports = {
52-
increaseTime,
53-
increaseTimeTo,
58+
latest,
59+
increase,
60+
increaseTo,
5461
duration,
5562
};

0 commit comments

Comments
 (0)