Skip to content

Commit

Permalink
Merge pull request #353 from jakub-wojciechowski/master
Browse files Browse the repository at this point in the history
Change crowdsales to use timestamps instead of block numbers #350
  • Loading branch information
frangio authored Aug 10, 2017
2 parents 69daed7 + 46c5759 commit 2b07913
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 96 deletions.
23 changes: 11 additions & 12 deletions contracts/crowdsale/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../math/SafeMath.sol';
/**
* @title Crowdsale
* @dev Crowdsale is a base contract for managing a token crowdsale.
* Crowdsales have a start and end block, where investors can make
* Crowdsales have a start and end timestamps, where investors can make
* token purchases and the crowdsale will assign them tokens based
* on a token per ETH rate. Funds collected are forwarded to a wallet
* as they arrive.
Expand All @@ -17,9 +17,9 @@ contract Crowdsale {
// The token being sold
MintableToken public token;

// start and end block where investments are allowed (both inclusive)
uint256 public startBlock;
uint256 public endBlock;
// start and end timestamps where investments are allowed (both inclusive)
uint256 public startTime;
uint256 public endTime;

// address where funds are collected
address public wallet;
Expand All @@ -40,15 +40,15 @@ contract Crowdsale {
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);


function Crowdsale(uint256 _startBlock, uint256 _endBlock, uint256 _rate, address _wallet) {
require(_startBlock >= block.number);
require(_endBlock >= _startBlock);
function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet) {
require(_startTime >= now);
require(_endTime >= _startTime);
require(_rate > 0);
require(_wallet != 0x0);

token = createTokenContract();
startBlock = _startBlock;
endBlock = _endBlock;
startTime = _startTime;
endTime = _endTime;
rate = _rate;
wallet = _wallet;
}
Expand Down Expand Up @@ -92,15 +92,14 @@ contract Crowdsale {

// @return true if the transaction can buy tokens
function validPurchase() internal constant returns (bool) {
uint256 current = block.number;
bool withinPeriod = current >= startBlock && current <= endBlock;
bool withinPeriod = now >= startTime && now <= endTime;
bool nonZeroPurchase = msg.value != 0;
return withinPeriod && nonZeroPurchase;
}

// @return true if crowdsale event has ended
function hasEnded() public constant returns (bool) {
return block.number > endBlock;
return now > endTime;
}


Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/SampleCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 20 additions & 14 deletions test/CappedCrowdsale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ether from './helpers/ether'
import advanceToBlock from './helpers/advanceToBlock'
import {advanceBlock} from './helpers/advanceToBlock'
import {increaseTimeTo, duration} from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow'

const BigNumber = web3.BigNumber
Expand All @@ -19,28 +21,32 @@ 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 advanceBlock()
})

beforeEach(async function () {
this.startBlock = web3.eth.blockNumber + 10
this.endBlock = web3.eth.blockNumber + 20
this.startTime = latestTime().unix() + duration.weeks(1);
this.endTime = this.startTime + duration.weeks(1);

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 increaseTimeTo(this.startTime)
})

it('should accept payments within cap', async function () {
Expand All @@ -62,7 +68,7 @@ contract('CappedCrowdsale', function ([_, wallet]) {
describe('ending', function () {

beforeEach(async function () {
await advanceToBlock(this.startBlock - 1)
await increaseTimeTo(this.startTime)
})

it('should not be ended if under cap', async function () {
Expand Down
39 changes: 24 additions & 15 deletions test/Crowdsale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ether from './helpers/ether'
import advanceToBlock from './helpers/advanceToBlock'
import {advanceBlock} from './helpers/advanceToBlock'
import {increaseTimeTo, duration} from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow'

const BigNumber = web3.BigNumber
Expand All @@ -19,11 +21,18 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {

const expectedTokenAmount = rate.mul(value)

before(async function() {
//Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
await advanceBlock()
})

beforeEach(async function () {
this.startBlock = web3.eth.blockNumber + 10
this.endBlock = web3.eth.blockNumber + 20
this.startTime = latestTime().unix() + duration.weeks(1);
this.endTime = this.startTime + duration.weeks(1);
this.afterEndTime = this.endTime + duration.seconds(1)


this.crowdsale = await Crowdsale.new(this.startBlock, this.endBlock, rate, wallet)
this.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet)

this.token = MintableToken.at(await this.crowdsale.token())
})
Expand All @@ -36,7 +45,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 advanceToBlock(this.endBlock + 1)
await increaseTimeTo(this.afterEndTime)
ended = await this.crowdsale.hasEnded()
ended.should.equal(true)
})
Expand All @@ -49,13 +58,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
})

it('should accept payments after start', async function () {
await advanceToBlock(this.startBlock - 1)
await increaseTimeTo(this.startTime)
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 advanceToBlock(this.endBlock)
await increaseTimeTo(this.afterEndTime)
await this.crowdsale.send(value).should.be.rejectedWith(EVMThrow)
await this.crowdsale.buyTokens(investor, {value: value, from: purchaser}).should.be.rejectedWith(EVMThrow)
})
Expand All @@ -65,7 +74,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe('high-level purchase', function () {

beforeEach(async function() {
await advanceToBlock(this.startBlock)
await increaseTimeTo(this.startTime)
})

it('should log purchase', async function () {
Expand Down Expand Up @@ -104,33 +113,33 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe('low-level purchase', function () {

beforeEach(async function() {
await advanceToBlock(this.startBlock)
await increaseTimeTo(this.startTime)
})

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})
Expand Down
27 changes: 18 additions & 9 deletions test/FinalizableCrowdsale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import advanceToBlock from './helpers/advanceToBlock'
import {advanceBlock} from './helpers/advanceToBlock'
import {increaseTimeTo, duration} from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow'

const BigNumber = web3.BigNumber
Expand All @@ -15,11 +17,18 @@ 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 advanceBlock()
})

beforeEach(async function () {
this.startBlock = web3.eth.blockNumber + 10
this.endBlock = web3.eth.blockNumber + 20
this.startTime = latestTime().unix() + duration.weeks(1)
this.endTime = this.startTime + duration.weeks(1)
this.afterEndTime = this.endTime + duration.seconds(1)


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())
})
Expand All @@ -29,30 +38,30 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
})

it('cannot be finalized by third party after ending', async function () {
await advanceToBlock(this.endBlock)
await increaseTimeTo(this.afterEndTime)
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 increaseTimeTo(this.afterEndTime)
await this.crowdsale.finalize({from: owner}).should.be.fulfilled
})

it('cannot be finalized twice', async function () {
await advanceToBlock(this.endBlock + 1)
await increaseTimeTo(this.afterEndTime)
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 increaseTimeTo(this.afterEndTime)
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 increaseTimeTo(this.afterEndTime)
await this.crowdsale.finalize({from: owner})
const finished = await this.token.mintingFinished()
finished.should.equal(true)
Expand Down
41 changes: 24 additions & 17 deletions test/RefundableCrowdsale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ether from './helpers/ether'
import advanceToBlock from './helpers/advanceToBlock'
import {advanceBlock} from './helpers/advanceToBlock'
import {increaseTimeTo, duration} from './helpers/increaseTime'
import latestTime from './helpers/latestTime'
import EVMThrow from './helpers/EVMThrow'

const BigNumber = web3.BigNumber
Expand All @@ -17,39 +19,44 @@ 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 advanceBlock()
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1)
this.endTime = this.startTime + duration.weeks(1)
this.afterEndTime = this.endTime + duration.seconds(1)

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 increaseTimeTo(this.startTime)
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 increaseTimeTo(this.startTime)
await this.crowdsale.sendTransaction({value: goal, from: investor})
await advanceToBlock(this.endBlock)
await increaseTimeTo(this.afterEndTime)
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 increaseTimeTo(this.startTime)
await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
await advanceToBlock(this.endBlock)
await increaseTimeTo(this.afterEndTime)

await this.crowdsale.finalize({from: owner})

Expand All @@ -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 increaseTimeTo(this.startTime)
await this.crowdsale.sendTransaction({value: goal, from: investor})
await advanceToBlock(this.endBlock)
await increaseTimeTo(this.afterEndTime)

const pre = web3.eth.getBalance(wallet)
await this.crowdsale.finalize({from: owner})
Expand Down
Loading

0 comments on commit 2b07913

Please # to comment.