diff --git a/contracts/examples/SampleCrowdsale.sol b/contracts/examples/SampleCrowdsale.sol index 2fc95c610..554aa6be8 100644 --- a/contracts/examples/SampleCrowdsale.sol +++ b/contracts/examples/SampleCrowdsale.sol @@ -30,11 +30,11 @@ contract SampleCrowdsaleToken is MintableToken { */ contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale { - function SampleCrowdsale(uint256 _startBlock, uint256 _endBlock, uint256 _rate, uint256 _goal, uint256 _cap, address _wallet) + function SampleCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, uint256 _goal, uint256 _cap, address _wallet) CappedCrowdsale(_cap) FinalizableCrowdsale() RefundableCrowdsale(_goal) - Crowdsale(_startBlock, _endBlock, _rate, _wallet) + Crowdsale(_startTime, _endTime, _rate, _wallet) { //As goal needs to be met for a successful crowdsale //the value needs to less or equal than a cap which is limit for accepted funds diff --git a/test/SampleCrowdsale.js b/test/SampleCrowdsale.js index a589194f7..b2669699a 100644 --- a/test/SampleCrowdsale.js +++ b/test/SampleCrowdsale.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; @@ -18,11 +21,17 @@ contract('Crowdsale', function ([owner, wallet, investor]) { const GOAL = ether(10); const CAP = ether(20); + 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 SampleCrowdsale.new(this.startBlock, this.endBlock, 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()); }); @@ -31,8 +40,8 @@ contract('Crowdsale', function ([owner, wallet, investor]) { this.crowdsale.should.exist; this.token.should.exist; - (await this.crowdsale.startBlock()).should.be.bignumber.equal(this.startBlock); - (await this.crowdsale.endBlock()).should.be.bignumber.equal(this.endBlock); + (await this.crowdsale.startTime()).should.be.bignumber.equal(this.startTime); + (await this.crowdsale.endTime()).should.be.bignumber.equal(this.endTime); (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); (await this.crowdsale.wallet()).should.be.equal(wallet); (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); @@ -48,7 +57,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) { const investmentAmount = ether(1); const expectedTokenAmount = RATE.mul(investmentAmount); - await advanceToBlock(this.startBlock - 1); + await increaseTime(moment.duration(1, 'week')); await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled; (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount); @@ -56,22 +65,23 @@ contract('Crowdsale', function ([owner, wallet, investor]) { }); it('should reject payments after end', async function () { - await advanceToBlock(this.endBlock); + await increaseTime(moment.duration(2.1, 'week')); 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 advanceToBlock(this.startBlock - 1); + await increaseTime(moment.duration(1, 'week')); 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 advanceToBlock(this.endBlock - 1); + await increaseTime(moment.duration(1, 'week')); await this.crowdsale.send(GOAL); const beforeFinalization = web3.eth.getBalance(wallet); + await increaseTime(moment.duration(1.1, 'week')); await this.crowdsale.finalize({from: owner}); const afterFinalization = web3.eth.getBalance(wallet); @@ -81,9 +91,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 advanceToBlock(this.startBlock - 1); + await increaseTime(moment.duration(1, 'week')); await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0}); - await advanceToBlock(this.endBlock); + await increaseTime(moment.duration(1.1, 'week')); await this.crowdsale.finalize({from: owner}); await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled;