From 2b5192b9ce1f56a21ddde643af0f9661ee7ef3d3 Mon Sep 17 00:00:00 2001 From: Jakub Wojciechowski Date: Sun, 6 Aug 2017 16:41:43 +0200 Subject: [PATCH] Change crowdsales to use timestamps instead of block numbers #350 update derived crowdsales --- test/CappedCrowdsale.js | 34 ++++++++++++-------- test/FinalizableCrowdsale.js | 24 +++++++++----- test/RefundableCrowdsale.js | 39 +++++++++++++---------- test/helpers/CappedCrowdsaleImpl.sol | 6 ++-- test/helpers/FinalizableCrowdsaleImpl.sol | 6 ++-- test/helpers/RefundableCrowdsaleImpl.sol | 6 ++-- 6 files changed, 69 insertions(+), 46 deletions(-) diff --git a/test/CappedCrowdsale.js b/test/CappedCrowdsale.js index 753da5c18..96fcd0c89 100644 --- a/test/CappedCrowdsale.js +++ b/test/CappedCrowdsale.js @@ -1,5 +1,8 @@ +import moment from 'moment' import ether from './helpers/ether' import advanceToBlock from './helpers/advanceToBlock' +import increaseTime from './helpers/increaseTime' +import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' const BigNumber = web3.BigNumber @@ -19,28 +22,33 @@ contract('CappedCrowdsale', function ([_, wallet]) { const cap = ether(300) const lessThanCap = ether(60) - describe('creating a valid crowdsale', function () { - - it('should fail with zero cap', async function () { - await CappedCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, 0).should.be.rejectedWith(EVMThrow); - }) - - }); - + before(async function() { + //Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc + await advanceToBlock(web3.eth.getBlock('latest').number + 1) + }) beforeEach(async function () { - this.startBlock = web3.eth.blockNumber + 10 - this.endBlock = web3.eth.blockNumber + 20 + this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); + this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); - this.crowdsale = await CappedCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, cap) + + this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap) this.token = MintableToken.at(await this.crowdsale.token()) }) + describe('creating a valid crowdsale', function () { + + it('should fail with zero cap', async function () { + await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0).should.be.rejectedWith(EVMThrow); + }) + + }); + describe('accepting payments', function () { beforeEach(async function () { - await advanceToBlock(this.startBlock - 1) + await increaseTime(moment.duration(1, 'week')) }) it('should accept payments within cap', async function () { @@ -62,7 +70,7 @@ contract('CappedCrowdsale', function ([_, wallet]) { describe('ending', function () { beforeEach(async function () { - await advanceToBlock(this.startBlock - 1) + await increaseTime(moment.duration(1, 'week')) }) it('should not be ended if under cap', async function () { diff --git a/test/FinalizableCrowdsale.js b/test/FinalizableCrowdsale.js index 53224bb76..5675c7557 100644 --- a/test/FinalizableCrowdsale.js +++ b/test/FinalizableCrowdsale.js @@ -1,4 +1,7 @@ +import moment from 'moment' import advanceToBlock from './helpers/advanceToBlock' +import increaseTime from './helpers/increaseTime' +import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' const BigNumber = web3.BigNumber @@ -15,11 +18,16 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { const rate = new BigNumber(1000) + before(async function() { + //Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc + await advanceToBlock(web3.eth.getBlock('latest').number + 1) + }) + beforeEach(async function () { - this.startBlock = web3.eth.blockNumber + 10 - this.endBlock = web3.eth.blockNumber + 20 + this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); + this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); - this.crowdsale = await FinalizableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, {from: owner}) + this.crowdsale = await FinalizableCrowdsale.new(this.startTime, this.endTime, rate, wallet, {from: owner}) this.token = MintableToken.at(await this.crowdsale.token()) }) @@ -29,30 +37,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }) it('cannot be finalized by third party after ending', async function () { - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(2.1, 'week')) await this.crowdsale.finalize({from: thirdparty}).should.be.rejectedWith(EVMThrow) }) it('can be finalized by owner after ending', async function () { - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(2.1, 'week')) await this.crowdsale.finalize({from: owner}).should.be.fulfilled }) it('cannot be finalized twice', async function () { - await advanceToBlock(this.endBlock + 1) + await increaseTime(moment.duration(2.1, 'week')) await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow) }) it('logs finalized', async function () { - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(2.1, 'week')) 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 advanceToBlock(this.endBlock) + await increaseTime(moment.duration(2.1, 'week')) 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 f1d6fda68..518f92047 100644 --- a/test/RefundableCrowdsale.js +++ b/test/RefundableCrowdsale.js @@ -1,5 +1,8 @@ +import moment from 'moment' import ether from './helpers/ether' import advanceToBlock from './helpers/advanceToBlock' +import increaseTime from './helpers/increaseTime' +import latestTime from './helpers/latestTime' import EVMThrow from './helpers/EVMThrow' const BigNumber = web3.BigNumber @@ -17,39 +20,43 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { const goal = ether(800) const lessThanGoal = ether(750) + before(async function() { + //Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc + await advanceToBlock(web3.eth.getBlock('latest').number + 1) + }) + + beforeEach(async function () { + this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds(); + this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds(); + + this.crowdsale = await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, goal, {from: owner}) + }) + describe('creating a valid crowdsale', function () { it('should fail with zero goal', async function () { - await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, 0, {from: owner}).should.be.rejectedWith(EVMThrow); + await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0, {from: owner}).should.be.rejectedWith(EVMThrow); }) }); - - beforeEach(async function () { - this.startBlock = web3.eth.blockNumber + 10 - this.endBlock = web3.eth.blockNumber + 20 - - this.crowdsale = await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, goal, {from: owner}) - }) - it('should deny refunds before end', async function () { await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) - await advanceToBlock(this.endBlock - 1) + await increaseTime(moment.duration(2, 'week')) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) }) it('should deny refunds after end if goal was reached', async function () { - await advanceToBlock(this.startBlock - 1) + await increaseTime(moment.duration(1, 'week')) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(1.1, 'week')) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) }) it('should allow refunds after end if goal was not reached', async function () { - await advanceToBlock(this.startBlock - 1) + await increaseTime(moment.duration(1, 'week')) await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor}) - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(1.1, 'week')) await this.crowdsale.finalize({from: owner}) @@ -62,9 +69,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) { }) it('should forward funds to wallet after end if goal was reached', async function () { - await advanceToBlock(this.startBlock - 1) + await increaseTime(moment.duration(1, 'week')) await this.crowdsale.sendTransaction({value: goal, from: investor}) - await advanceToBlock(this.endBlock) + await increaseTime(moment.duration(1.1, 'week')) const pre = web3.eth.getBalance(wallet) await this.crowdsale.finalize({from: owner}) diff --git a/test/helpers/CappedCrowdsaleImpl.sol b/test/helpers/CappedCrowdsaleImpl.sol index 6a255dca3..1a9497498 100644 --- a/test/helpers/CappedCrowdsaleImpl.sol +++ b/test/helpers/CappedCrowdsaleImpl.sol @@ -7,13 +7,13 @@ import '../../contracts/crowdsale/CappedCrowdsale.sol'; contract CappedCrowdsaleImpl is CappedCrowdsale { function CappedCrowdsaleImpl ( - uint256 _startBlock, - uint256 _endBlock, + uint256 _startTime, + uint256 _endTime, uint256 _rate, address _wallet, uint256 _cap ) - Crowdsale(_startBlock, _endBlock, _rate, _wallet) + Crowdsale(_startTime, _endTime, _rate, _wallet) CappedCrowdsale(_cap) { } diff --git a/test/helpers/FinalizableCrowdsaleImpl.sol b/test/helpers/FinalizableCrowdsaleImpl.sol index b35b633a9..cc6d31c09 100644 --- a/test/helpers/FinalizableCrowdsaleImpl.sol +++ b/test/helpers/FinalizableCrowdsaleImpl.sol @@ -7,12 +7,12 @@ import '../../contracts/crowdsale/FinalizableCrowdsale.sol'; contract FinalizableCrowdsaleImpl is FinalizableCrowdsale { function FinalizableCrowdsaleImpl ( - uint256 _startBlock, - uint256 _endBlock, + uint256 _startTime, + uint256 _endTime, uint256 _rate, address _wallet ) - Crowdsale(_startBlock, _endBlock, _rate, _wallet) + Crowdsale(_startTime, _endTime, _rate, _wallet) FinalizableCrowdsale() { } diff --git a/test/helpers/RefundableCrowdsaleImpl.sol b/test/helpers/RefundableCrowdsaleImpl.sol index 5250f56f8..2db8257fc 100644 --- a/test/helpers/RefundableCrowdsaleImpl.sol +++ b/test/helpers/RefundableCrowdsaleImpl.sol @@ -7,13 +7,13 @@ import '../../contracts/crowdsale/RefundableCrowdsale.sol'; contract RefundableCrowdsaleImpl is RefundableCrowdsale { function RefundableCrowdsaleImpl ( - uint256 _startBlock, - uint256 _endBlock, + uint256 _startTime, + uint256 _endTime, uint256 _rate, address _wallet, uint256 _goal ) - Crowdsale(_startBlock, _endBlock, _rate, _wallet) + Crowdsale(_startTime, _endTime, _rate, _wallet) RefundableCrowdsale(_goal) { }