Explorar o código

Change crowdsales to use timestamps instead of block numbers #350 update derived crowdsales

Jakub Wojciechowski %!s(int64=8) %!d(string=hai) anos
pai
achega
2b5192b9ce

+ 21 - 13
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 () {

+ 16 - 8
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)

+ 23 - 16
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})

+ 3 - 3
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) 
   {
   }

+ 3 - 3
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() 
   {
   }

+ 3 - 3
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) 
   {
   }