From 7c883b6368419c39f316e78dda5043fe46987605 Mon Sep 17 00:00:00 2001 From: Jakub Wojciechowski Date: Thu, 10 Aug 2017 13:13:49 +0200 Subject: [PATCH] Duration helper and eliminated hardcoded periods --- test/CappedCrowdsale.js | 13 ++++++++----- test/Crowdsale.js | 32 ++++++++++++++++++-------------- test/FinalizableCrowdsale.js | 20 ++++++++++++-------- test/RefundableCrowdsale.js | 24 ++++++++++++++---------- test/SampleCrowdsale.js | 24 ++++++++++++++---------- test/TokenTimelock.js | 8 ++++---- test/helpers/increaseTime.js | 12 +++++++++++- 7 files changed, 81 insertions(+), 52 deletions(-) diff --git a/test/CappedCrowdsale.js b/test/CappedCrowdsale.js index 65d4ac9bd..bcc4e4a78 100644 --- a/test/CappedCrowdsale.js +++ b/test/CappedCrowdsale.js @@ -1,7 +1,7 @@ -import moment from 'moment' import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' import increaseTime from './helpers/increaseTime' +import {duration, increaseTimeHandicap} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -28,8 +28,11 @@ contract('CappedCrowdsale', function ([_, wallet]) { }) beforeEach(async function () { - this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); - this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + this.timeToStart = duration.weeks(1); + this.crowdsalePeriod = 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) @@ -48,7 +51,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { describe('accepting payments', function () { beforeEach(async function () { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) }) it('should accept payments within cap', async function () { @@ -70,7 +73,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { describe('ending', function () { beforeEach(async function () { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) }) it('should not be ended if under cap', async function () { diff --git a/test/Crowdsale.js b/test/Crowdsale.js index 4be913404..20c1b365d 100644 --- a/test/Crowdsale.js +++ b/test/Crowdsale.js @@ -1,7 +1,7 @@ -import moment from 'moment' import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' import increaseTime from './helpers/increaseTime' +import {duration, increaseTimeHandicap} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -28,8 +28,12 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }) beforeEach(async function () { - this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); - this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + 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.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet) @@ -44,7 +48,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(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) ended = await this.crowdsale.hasEnded() ended.should.equal(true) }) @@ -57,13 +61,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }) it('should accept payments after start', async function () { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) 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(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) await this.crowdsale.send(value).should.be.rejectedWith(EVMThrow) await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.rejectedWith(EVMThrow) }) @@ -73,7 +77,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('high-level purchase', function () { beforeEach(async function() { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) }) it('should log purchase', async function () { @@ -112,33 +116,33 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('low-level purchase', function () { beforeEach(async function() { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) }) - + it('should log purchase', async function () { const {logs} = await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}) - + const event = logs.find(e => e.event === 'TokenPurchase') - + should.exist(event) event.args.purchaser.should.equal(purchaser) event.args.beneficiary.should.equal(investor) event.args.value.should.be.bignumber.equal(value) event.args.amount.should.be.bignumber.equal(expectedTokenAmount) }) - + it('should increase totalSupply', async function () { await this.crowdsale.buyTokens(investor, {value, from: purchaser}) const totalSupply = await this.token.totalSupply() totalSupply.should.be.bignumber.equal(expectedTokenAmount) }) - + it('should assign tokens to beneficiary', async function () { await this.crowdsale.buyTokens(investor, {value, from: purchaser}) const balance = await this.token.balanceOf(investor) balance.should.be.bignumber.equal(expectedTokenAmount) }) - + it('should forward funds to wallet', async function () { const pre = web3.eth.getBalance(wallet) await this.crowdsale.buyTokens(investor, {value, from: purchaser}) diff --git a/test/FinalizableCrowdsale.js b/test/FinalizableCrowdsale.js index d92fe6608..af14708ea 100644 --- a/test/FinalizableCrowdsale.js +++ b/test/FinalizableCrowdsale.js @@ -1,6 +1,6 @@ -import moment from 'moment' import {advanceBlock} from './helpers/advanceToBlock' import increaseTime from './helpers/increaseTime' +import {duration, increaseTimeHandicap} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -24,8 +24,12 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }) beforeEach(async function () { - this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); - this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + 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.crowdsale = await FinalizableCrowdsale.new(this.startTime, this.endTime, rate, wallet, {from: owner}) @@ -37,30 +41,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }) it('cannot be finalized by third party after ending', async function () { - await increaseTime(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) await this.crowdsale.finalize({from: thirdparty}).should.be.rejectedWith(EVMThrow) }) it('can be finalized by owner after ending', async function () { - await increaseTime(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) await this.crowdsale.finalize({from: owner}).should.be.fulfilled }) it('cannot be finalized twice', async function () { - await increaseTime(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow) }) it('logs finalized', async function () { - await increaseTime(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) 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(moment.duration(2.1, 'week')) + await increaseTime(this.timeToEnd) 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 7423fd37f..6f0fc39a7 100644 --- a/test/RefundableCrowdsale.js +++ b/test/RefundableCrowdsale.js @@ -1,7 +1,7 @@ -import moment from 'moment' import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' import increaseTime from './helpers/increaseTime' +import {duration, increaseTimeHandicap} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -26,8 +26,12 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { }) beforeEach(async function () { - this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); - this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + 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.crowdsale = await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, goal, {from: owner}) }) @@ -42,21 +46,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(moment.duration(2, 'week')) + await increaseTime(this.timeToStart) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) }) it('should deny refunds after end if goal was reached', async function () { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await increaseTime(moment.duration(1.1, 'week')) + await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) 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(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor}) - await increaseTime(moment.duration(1.1, 'week')) + await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) await this.crowdsale.finalize({from: owner}) @@ -69,9 +73,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { }) it('should forward funds to wallet after end if goal was reached', async function () { - await increaseTime(moment.duration(1, 'week')) + await increaseTime(this.timeToStart) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await increaseTime(moment.duration(1.1, 'week')) + await increaseTime(this.crowdsalePeriod + increaseTimeHandicap) const pre = web3.eth.getBalance(wallet) await this.crowdsale.finalize({from: owner}) diff --git a/test/SampleCrowdsale.js b/test/SampleCrowdsale.js index 2e64111c9..dbcf2ba56 100644 --- a/test/SampleCrowdsale.js +++ b/test/SampleCrowdsale.js @@ -1,7 +1,7 @@ -import moment from 'moment' import ether from './helpers/ether' import {advanceBlock} from './helpers/advanceToBlock' import increaseTime from './helpers/increaseTime' +import {duration, increaseTimeHandicap} from './helpers/increaseTime' import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' @@ -27,8 +27,12 @@ contract('Crowdsale', function ([owner, wallet, investor]) { }) beforeEach(async function () { - this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); - this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + 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.crowdsale = await SampleCrowdsale.new(this.startTime, this.endTime, RATE, GOAL, CAP, wallet); @@ -57,7 +61,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) { const investmentAmount = ether(1); const expectedTokenAmount = RATE.mul(investmentAmount); - await increaseTime(moment.duration(1, 'week')); + await increaseTime(this.timeToStart); await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled; (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount); @@ -65,23 +69,23 @@ contract('Crowdsale', function ([owner, wallet, investor]) { }); it('should reject payments after end', async function () { - await increaseTime(moment.duration(2.1, 'week')); + await increaseTime(this.timeToEnd); 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(moment.duration(1, 'week')); + await increaseTime(this.timeToStart); 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(moment.duration(1, 'week')); + await increaseTime(this.timeToStart); await this.crowdsale.send(GOAL); const beforeFinalization = web3.eth.getBalance(wallet); - await increaseTime(moment.duration(1.1, 'week')); + await increaseTime(this.crowdsalePeriod + increaseTimeHandicap); await this.crowdsale.finalize({from: owner}); const afterFinalization = web3.eth.getBalance(wallet); @@ -91,9 +95,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(moment.duration(1, 'week')); + await increaseTime(this.timeToStart); await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0}); - await increaseTime(moment.duration(1.1, 'week')); + await increaseTime(this.crowdsalePeriod + increaseTimeHandicap); await this.crowdsale.finalize({from: owner}); await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled; diff --git a/test/TokenTimelock.js b/test/TokenTimelock.js index 318cec98b..25ffd9e2e 100644 --- a/test/TokenTimelock.js +++ b/test/TokenTimelock.js @@ -29,26 +29,26 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { }) it('cannot be released just before time limit', async function () { - await increaseTime(moment.duration(0.99, 'year')) + await increaseTime(moment.duration(0.99, 'year').asSeconds()) await this.timelock.release().should.be.rejected }) it('can be released just after limit', async function () { - await increaseTime(moment.duration(1.01, 'year')) + await increaseTime(moment.duration(1.01, 'year').asSeconds()) await this.timelock.release().should.be.fulfilled const balance = await this.token.balanceOf(beneficiary) balance.should.be.bignumber.equal(amount) }) it('can be released after time limit', async function () { - await increaseTime(moment.duration(2, 'year')) + await increaseTime(moment.duration(2, 'year').asSeconds()) await this.timelock.release().should.be.fulfilled const balance = await this.token.balanceOf(beneficiary) balance.should.be.bignumber.equal(amount) }) it('cannot be released twice', async function () { - await increaseTime(moment.duration(2, 'year')) + await increaseTime(moment.duration(2, 'year').asSeconds()) await this.timelock.release().should.be.fulfilled await this.timelock.release().should.be.rejected const balance = await this.token.balanceOf(beneficiary) diff --git a/test/helpers/increaseTime.js b/test/helpers/increaseTime.js index cd1a756fd..06f7d9c0b 100644 --- a/test/helpers/increaseTime.js +++ b/test/helpers/increaseTime.js @@ -6,7 +6,7 @@ export default function increaseTime(duration) { web3.currentProvider.sendAsync({ jsonrpc: '2.0', method: 'evm_increaseTime', - params: [duration.asSeconds()], + params: [duration], id: id, }, err1 => { if (err1) return reject(err1) @@ -21,3 +21,13 @@ export default function increaseTime(duration) { }) }) } + +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