Skip to content

Commit

Permalink
Simplify IndividuallyCappedCrowdsale interface (#1296)
Browse files Browse the repository at this point in the history
* remove concept of User and remove setGroupCap

* fix linting error

* remove mention of users from comments
  • Loading branch information
frangio authored Sep 7, 2018
1 parent 897ef29 commit bf95602
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 67 deletions.
39 changes: 11 additions & 28 deletions contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "../../ownership/Ownable.sol";

/**
* @title IndividuallyCappedCrowdsale
* @dev Crowdsale with per-user caps.
* @dev Crowdsale with per-beneficiary caps.
*/
contract IndividuallyCappedCrowdsale is Ownable, Crowdsale {
using SafeMath for uint256;
Expand All @@ -16,53 +16,36 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale {
mapping(address => uint256) private caps_;

/**
* @dev Sets a specific user's maximum contribution.
* @dev Sets a specific beneficiary's maximum contribution.
* @param _beneficiary Address to be capped
* @param _cap Wei limit for individual contribution
*/
function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner {
function setCap(address _beneficiary, uint256 _cap) external onlyOwner {
caps_[_beneficiary] = _cap;
}

/**
* @dev Sets a group of users' maximum contribution.
* @param _beneficiaries List of addresses to be capped
* @param _cap Wei limit for individual contribution
*/
function setGroupCap(
address[] _beneficiaries,
uint256 _cap
)
external
onlyOwner
{
for (uint256 i = 0; i < _beneficiaries.length; i++) {
caps_[_beneficiaries[i]] = _cap;
}
}

/**
* @dev Returns the cap of a specific user.
* @dev Returns the cap of a specific beneficiary.
* @param _beneficiary Address whose cap is to be checked
* @return Current cap for individual user
* @return Current cap for individual beneficiary
*/
function getUserCap(address _beneficiary) public view returns (uint256) {
function getCap(address _beneficiary) public view returns (uint256) {
return caps_[_beneficiary];
}

/**
* @dev Returns the amount contributed so far by a sepecific user.
* @dev Returns the amount contributed so far by a specific beneficiary.
* @param _beneficiary Address of contributor
* @return User contribution so far
* @return Beneficiary contribution so far
*/
function getUserContribution(address _beneficiary)
function getContribution(address _beneficiary)
public view returns (uint256)
{
return contributions_[_beneficiary];
}

/**
* @dev Extend parent behavior requiring purchase to respect the user's funding cap.
* @dev Extend parent behavior requiring purchase to respect the beneficiary's funding cap.
* @param _beneficiary Token purchaser
* @param _weiAmount Amount of wei contributed
*/
Expand All @@ -78,7 +61,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale {
}

/**
* @dev Extend parent behavior to update user contributions
* @dev Extend parent behavior to update beneficiary contributions
* @param _beneficiary Token purchaser
* @param _weiAmount Amount of wei contributed
*/
Expand Down
43 changes: 4 additions & 39 deletions test/crowdsale/IndividuallyCappedCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ contract('IndividuallyCappedCrowdsale', function ([_, wallet, alice, bob, charli
beforeEach(async function () {
this.token = await SimpleToken.new();
this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address);
await this.crowdsale.setUserCap(alice, capAlice);
await this.crowdsale.setUserCap(bob, capBob);
await this.crowdsale.setCap(alice, capAlice);
await this.crowdsale.setCap(bob, capBob);
await this.token.transfer(this.crowdsale.address, tokenSupply);
});

Expand Down Expand Up @@ -56,47 +56,12 @@ contract('IndividuallyCappedCrowdsale', function ([_, wallet, alice, bob, charli

describe('reporting state', function () {
it('should report correct cap', async function () {
(await this.crowdsale.getUserCap(alice)).should.be.bignumber.equal(capAlice);
(await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
});

it('should report actual contribution', async function () {
await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
(await this.crowdsale.getUserContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
});
});
});

describe('group capping', function () {
beforeEach(async function () {
this.token = await SimpleToken.new();
this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address);
await this.crowdsale.setGroupCap([bob, charlie], capBob);
await this.token.transfer(this.crowdsale.address, tokenSupply);
});

describe('accepting payments', function () {
it('should accept payments within cap', async function () {
await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
await this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth });
});

it('should reject payments outside cap', async function () {
await this.crowdsale.buyTokens(bob, { value: capBob });
await expectThrow(this.crowdsale.buyTokens(bob, { value: 1 }), EVMRevert);
await this.crowdsale.buyTokens(charlie, { value: capBob });
await expectThrow(this.crowdsale.buyTokens(charlie, { value: 1 }), EVMRevert);
});

it('should reject payments that exceed cap', async function () {
await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert);
await expectThrow(this.crowdsale.buyTokens(charlie, { value: capBob.plus(1) }), EVMRevert);
});
});

describe('reporting state', function () {
it('should report correct cap', async function () {
(await this.crowdsale.getUserCap(bob)).should.be.bignumber.equal(capBob);
(await this.crowdsale.getUserCap(charlie)).should.be.bignumber.equal(capBob);
(await this.crowdsale.getContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
});
});
});
Expand Down

0 comments on commit bf95602

Please sign in to comment.