Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify IndividuallyCappedCrowdsale interface #1296

Merged
merged 3 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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