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

pull/353/head
Jakub Wojciechowski 8 years ago
parent 77dfcb6e23
commit 2b5192b9ce
  1. 32
      test/CappedCrowdsale.js
  2. 24
      test/FinalizableCrowdsale.js
  3. 39
      test/RefundableCrowdsale.js
  4. 6
      test/helpers/CappedCrowdsaleImpl.sol
  5. 6
      test/helpers/FinalizableCrowdsaleImpl.sol
  6. 6
      test/helpers/RefundableCrowdsaleImpl.sol

@ -1,5 +1,8 @@
import moment from 'moment'
import ether from './helpers/ether' import ether from './helpers/ether'
import advanceToBlock from './helpers/advanceToBlock' import advanceToBlock from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
const BigNumber = web3.BigNumber const BigNumber = web3.BigNumber
@ -19,28 +22,33 @@ contract('CappedCrowdsale', function ([_, wallet]) {
const cap = ether(300) const cap = ether(300)
const lessThanCap = ether(60) const lessThanCap = ether(60)
describe('creating a valid crowdsale', function () { before(async function() {
//Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
it('should fail with zero cap', async function () { await advanceToBlock(web3.eth.getBlock('latest').number + 1)
await CappedCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, 0).should.be.rejectedWith(EVMThrow);
}) })
});
beforeEach(async function () { beforeEach(async function () {
this.startBlock = web3.eth.blockNumber + 10 this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds();
this.endBlock = web3.eth.blockNumber + 20 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()) 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 () { describe('accepting payments', function () {
beforeEach(async function () { beforeEach(async function () {
await advanceToBlock(this.startBlock - 1) await increaseTime(moment.duration(1, 'week'))
}) })
it('should accept payments within cap', async function () { it('should accept payments within cap', async function () {
@ -62,7 +70,7 @@ contract('CappedCrowdsale', function ([_, wallet]) {
describe('ending', function () { describe('ending', function () {
beforeEach(async 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 () { it('should not be ended if under cap', async function () {

@ -1,4 +1,7 @@
import moment from 'moment'
import advanceToBlock from './helpers/advanceToBlock' import advanceToBlock from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
const BigNumber = web3.BigNumber const BigNumber = web3.BigNumber
@ -15,11 +18,16 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
const rate = new BigNumber(1000) 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 () { beforeEach(async function () {
this.startBlock = web3.eth.blockNumber + 10 this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds();
this.endBlock = web3.eth.blockNumber + 20 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()) 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 () { 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) await this.crowdsale.finalize({from: thirdparty}).should.be.rejectedWith(EVMThrow)
}) })
it('can be finalized by owner after ending', async function () { 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 await this.crowdsale.finalize({from: owner}).should.be.fulfilled
}) })
it('cannot be finalized twice', async function () { 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})
await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow) await this.crowdsale.finalize({from: owner}).should.be.rejectedWith(EVMThrow)
}) })
it('logs finalized', async function () { 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 {logs} = await this.crowdsale.finalize({from: owner})
const event = logs.find(e => e.event === 'Finalized') const event = logs.find(e => e.event === 'Finalized')
should.exist(event) should.exist(event)
}) })
it('finishes minting of token', async function () { it('finishes minting of token', async function () {
await advanceToBlock(this.endBlock) await increaseTime(moment.duration(2.1, 'week'))
await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner})
const finished = await this.token.mintingFinished() const finished = await this.token.mintingFinished()
finished.should.equal(true) finished.should.equal(true)

@ -1,5 +1,8 @@
import moment from 'moment'
import ether from './helpers/ether' import ether from './helpers/ether'
import advanceToBlock from './helpers/advanceToBlock' import advanceToBlock from './helpers/advanceToBlock'
import increaseTime from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow' import EVMThrow from './helpers/EVMThrow'
const BigNumber = web3.BigNumber const BigNumber = web3.BigNumber
@ -17,39 +20,43 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
const goal = ether(800) const goal = ether(800)
const lessThanGoal = ether(750) const lessThanGoal = ether(750)
describe('creating a valid crowdsale', function () { before(async function() {
//Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
it('should fail with zero goal', async function () { await advanceToBlock(web3.eth.getBlock('latest').number + 1)
await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, 0, {from: owner}).should.be.rejectedWith(EVMThrow);
}) })
}); 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})
})
beforeEach(async function () { describe('creating a valid crowdsale', 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 fail with zero goal', async function () {
await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0, {from: owner}).should.be.rejectedWith(EVMThrow);
}) })
});
it('should deny refunds before end', async function () { it('should deny refunds before end', async function () {
await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow) 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) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
}) })
it('should deny refunds after end if goal was reached', async function () { 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 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) await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
}) })
it('should allow refunds after end if goal was not reached', async function () { 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 this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
await advanceToBlock(this.endBlock) await increaseTime(moment.duration(1.1, 'week'))
await this.crowdsale.finalize({from: owner}) 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 () { 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 this.crowdsale.sendTransaction({value: goal, from: investor})
await advanceToBlock(this.endBlock) await increaseTime(moment.duration(1.1, 'week'))
const pre = web3.eth.getBalance(wallet) const pre = web3.eth.getBalance(wallet)
await this.crowdsale.finalize({from: owner}) await this.crowdsale.finalize({from: owner})

@ -7,13 +7,13 @@ import '../../contracts/crowdsale/CappedCrowdsale.sol';
contract CappedCrowdsaleImpl is CappedCrowdsale { contract CappedCrowdsaleImpl is CappedCrowdsale {
function CappedCrowdsaleImpl ( function CappedCrowdsaleImpl (
uint256 _startBlock, uint256 _startTime,
uint256 _endBlock, uint256 _endTime,
uint256 _rate, uint256 _rate,
address _wallet, address _wallet,
uint256 _cap uint256 _cap
) )
Crowdsale(_startBlock, _endBlock, _rate, _wallet) Crowdsale(_startTime, _endTime, _rate, _wallet)
CappedCrowdsale(_cap) CappedCrowdsale(_cap)
{ {
} }

@ -7,12 +7,12 @@ import '../../contracts/crowdsale/FinalizableCrowdsale.sol';
contract FinalizableCrowdsaleImpl is FinalizableCrowdsale { contract FinalizableCrowdsaleImpl is FinalizableCrowdsale {
function FinalizableCrowdsaleImpl ( function FinalizableCrowdsaleImpl (
uint256 _startBlock, uint256 _startTime,
uint256 _endBlock, uint256 _endTime,
uint256 _rate, uint256 _rate,
address _wallet address _wallet
) )
Crowdsale(_startBlock, _endBlock, _rate, _wallet) Crowdsale(_startTime, _endTime, _rate, _wallet)
FinalizableCrowdsale() FinalizableCrowdsale()
{ {
} }

@ -7,13 +7,13 @@ import '../../contracts/crowdsale/RefundableCrowdsale.sol';
contract RefundableCrowdsaleImpl is RefundableCrowdsale { contract RefundableCrowdsaleImpl is RefundableCrowdsale {
function RefundableCrowdsaleImpl ( function RefundableCrowdsaleImpl (
uint256 _startBlock, uint256 _startTime,
uint256 _endBlock, uint256 _endTime,
uint256 _rate, uint256 _rate,
address _wallet, address _wallet,
uint256 _goal uint256 _goal
) )
Crowdsale(_startBlock, _endBlock, _rate, _wallet) Crowdsale(_startTime, _endTime, _rate, _wallet)
RefundableCrowdsale(_goal) RefundableCrowdsale(_goal)
{ {
} }

Loading…
Cancel
Save