Skip to content

Commit

Permalink
Added PausableCrowdsale contract (#832)
Browse files Browse the repository at this point in the history
* Added PausableCrowdsale contract

* Changed inheritance order to prevent "Linearization of inheritance graph impossible" error

* Updated mock PausableCrowdsaleImpl to new constructor syntax

* Broke function definition to multiple lines

Comply with new max line length

* Rename events to past-tense in PausableCrowdsale test

* Removed should.be.fullfilled from PausableCrowdsale tests

* Change import assertRevert to require in PausableCrowdsale tests

* Remove dependency on chai-as-promised and added BigNumber support in PausableCrowdsale tests

* reindent solidity with 4 spaces

* add missing view modifier in _preValidatePurchase

* convert assertRevert to new shoulFail helper

* add new setup helper

* use expectEvent

* convert to assert to chai should style

* add description to beforeEach blocks

* extract common step to beforeEach

* improve documentation

* revert inheritance error

* move PausableCrowdsale into crowdsale/validation

* make documentation more specific

* put whitespace in line with convention

* improve test suite account names

* undo beforeEach descriptions

* simplify tests

* fix transaction senders to be the anyone account

* make transaction senders more explicit

* remove mocha only
  • Loading branch information
TalAter authored and frangio committed Dec 14, 2018
1 parent 12533bc commit 8056433
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
22 changes: 22 additions & 0 deletions contracts/crowdsale/validation/PausableCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity ^0.4.18;

import "../Crowdsale.sol";
import "../../lifecycle/Pausable.sol";


/**
* @title PausableCrowdsale
* @dev Extension of Crowdsale contract where purchases can be paused and unpaused by the pauser role.
*/
contract PausableCrowdsale is Crowdsale, Pausable {

/**
* @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
* Adds the validation that the crowdsale must not be paused.
* @param _beneficiary Address performing the token purchase
* @param _weiAmount Value in wei involved in the purchase
*/
function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal view whenNotPaused {
return super._preValidatePurchase(_beneficiary, _weiAmount);
}
}
9 changes: 9 additions & 0 deletions contracts/mocks/PausableCrowdsaleImpl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.4.18;

import "../token/ERC20/ERC20.sol";
import "../crowdsale/validation/PausableCrowdsale.sol";

contract PausableCrowdsaleImpl is PausableCrowdsale {
constructor (uint256 _rate, address _wallet, ERC20 _token) public Crowdsale(_rate, _wallet, _token) {
}
}
46 changes: 46 additions & 0 deletions test/crowdsale/PausableCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const shouldFail = require('../helpers/shouldFail');

const PausableCrowdsale = artifacts.require('PausableCrowdsaleImpl');
const SimpleToken = artifacts.require('SimpleToken');

require('../helpers/setup');

contract('PausableCrowdsale', function ([_, pauser, wallet, anyone]) {
const rate = 1;
const value = 1;

beforeEach(async function () {
const from = pauser;

this.token = await SimpleToken.new({ from });
this.crowdsale = await PausableCrowdsale.new(rate, wallet, this.token.address, { from });
await this.token.transfer(this.crowdsale.address, 2 * value, { from });
});

it('purchases work', async function () {
await this.crowdsale.sendTransaction({ from: anyone, value });
await this.crowdsale.buyTokens(anyone, { from: anyone, value });
});

context('after pause', function () {
beforeEach(async function () {
await this.crowdsale.pause({ from: pauser });
});

it('purchases do not work', async function () {
await shouldFail.reverting(this.crowdsale.sendTransaction({ from: anyone, value }));
await shouldFail.reverting(this.crowdsale.buyTokens(anyone, { from: anyone, value }));
});

context('after unpause', function () {
beforeEach(async function () {
await this.crowdsale.unpause({ from: pauser });
});

it('purchases work', async function () {
await this.crowdsale.sendTransaction({ from: anyone, value });
await this.crowdsale.buyTokens(anyone, { from: anyone, value });
});
});
});
});

0 comments on commit 8056433

Please sign in to comment.