Fix/whitelisted crowdsale (#981)

* fix: swithc WhitelistedCrowdsale to use Whitelist.sol

* feat: refactor whitelist.sol, rbac.sol and whitelistedcrowdsale.sol

* feat: add event arg assets and update whitelist

* fix: update modifier comment and also test isWhitelisted

* fix: remove onlyWhitelisted backwards compat attempt, fix explicit inheritance

* fix: remove underscore prefix from event args

* fix: user access/Whitelist
This commit is contained in:
Matt Condon
2018-06-15 14:11:50 -07:00
committed by GitHub
parent ee78f67985
commit 92b695f2fb
8 changed files with 98 additions and 131 deletions

View File

@ -19,23 +19,24 @@ contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized,
this.token = await SimpleToken.new();
this.crowdsale = await WhitelistedCrowdsale.new(rate, wallet, this.token.address);
await this.token.transfer(this.crowdsale.address, tokenSupply);
await this.crowdsale.addToWhitelist(authorized);
await this.crowdsale.addAddressToWhitelist(authorized);
});
describe('accepting payments', function () {
it('should accept payments to whitelisted (from whichever buyers)', async function () {
await this.crowdsale.sendTransaction({ value, from: authorized }).should.be.fulfilled;
await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }).should.be.fulfilled;
await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized }).should.be.fulfilled;
});
it('should reject payments to not whitelisted (from whichever buyers)', async function () {
await this.crowdsale.send(value).should.be.rejected;
await this.crowdsale.sendTransaction({ value, from: unauthorized }).should.be.rejected;
await this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }).should.be.rejected;
await this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }).should.be.rejected;
});
it('should reject payments to addresses removed from whitelist', async function () {
await this.crowdsale.removeFromWhitelist(authorized);
await this.crowdsale.removeAddressFromWhitelist(authorized);
await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }).should.be.rejected;
});
});
@ -55,7 +56,7 @@ contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized,
this.token = await SimpleToken.new();
this.crowdsale = await WhitelistedCrowdsale.new(rate, wallet, this.token.address);
await this.token.transfer(this.crowdsale.address, tokenSupply);
await this.crowdsale.addManyToWhitelist([authorized, anotherAuthorized]);
await this.crowdsale.addAddressesToWhitelist([authorized, anotherAuthorized]);
});
describe('accepting payments', function () {
@ -73,7 +74,7 @@ contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized,
});
it('should reject payments to addresses removed from whitelist', async function () {
await this.crowdsale.removeFromWhitelist(anotherAuthorized);
await this.crowdsale.removeAddressFromWhitelist(anotherAuthorized);
await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }).should.be.fulfilled;
await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }).should.be.rejected;
});

View File

@ -1,14 +1,18 @@
const assert = require('chai').assert;
const should = require('chai').should();
const inLogs = async (logs, eventName) => {
const inLogs = async (logs, eventName, eventArgs = {}) => {
const event = logs.find(e => e.event === eventName);
assert.exists(event);
should.exist(event);
for (const [k, v] of Object.entries(eventArgs)) {
should.exist(event.args[k]);
event.args[k].should.eq(v);
}
return event;
};
const inTransaction = async (tx, eventName) => {
const inTransaction = async (tx, eventName, eventArgs = {}) => {
const { logs } = await tx;
return inLogs(logs, eventName);
return inLogs(logs, eventName, eventArgs);
};
module.exports = {

View File

@ -8,8 +8,6 @@ require('chai')
.should();
contract('Whitelist', function (accounts) {
let mock;
const [
owner,
whitelistedAddress1,
@ -20,73 +18,78 @@ contract('Whitelist', function (accounts) {
const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
before(async function () {
mock = await WhitelistMock.new();
this.mock = await WhitelistMock.new();
this.role = await this.mock.ROLE_WHITELISTED();
});
context('in normal conditions', () => {
context('in normal conditions', function () {
it('should add address to the whitelist', async function () {
await expectEvent.inTransaction(
mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }),
'WhitelistedAddressAdded'
this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }),
'RoleAdded',
{ role: this.role },
);
const isWhitelisted = await mock.whitelist(whitelistedAddress1);
const isWhitelisted = await this.mock.whitelist(whitelistedAddress1);
isWhitelisted.should.be.equal(true);
});
it('should add addresses to the whitelist', async function () {
await expectEvent.inTransaction(
mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
'WhitelistedAddressAdded'
this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
'RoleAdded',
{ role: this.role },
);
for (let addr of whitelistedAddresses) {
const isWhitelisted = await mock.whitelist(addr);
const isWhitelisted = await this.mock.whitelist(addr);
isWhitelisted.should.be.equal(true);
}
});
it('should remove address from the whitelist', async function () {
await expectEvent.inTransaction(
mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
'WhitelistedAddressRemoved'
this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
'RoleRemoved',
{ role: this.role },
);
let isWhitelisted = await mock.whitelist(whitelistedAddress1);
let isWhitelisted = await this.mock.whitelist(whitelistedAddress1);
isWhitelisted.should.be.equal(false);
});
it('should remove addresses from the the whitelist', async function () {
await expectEvent.inTransaction(
mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
'WhitelistedAddressRemoved'
this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
'RoleRemoved',
{ role: this.role },
);
for (let addr of whitelistedAddresses) {
const isWhitelisted = await mock.whitelist(addr);
const isWhitelisted = await this.mock.whitelist(addr);
isWhitelisted.should.be.equal(false);
}
});
it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => {
await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async function () {
await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
await this.mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
.should.be.fulfilled;
});
});
context('in adversarial conditions', () => {
it('should not allow "anyone" to add to the whitelist', async () => {
context('in adversarial conditions', function () {
it('should not allow "anyone" to add to the whitelist', async function () {
await expectThrow(
mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
this.mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
);
});
it('should not allow "anyone" to remove from the whitelist', async () => {
it('should not allow "anyone" to remove from the whitelist', async function () {
await expectThrow(
mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
);
});
it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async () => {
it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async function () {
await expectThrow(
mock.onlyWhitelistedCanDoThis({ from: anyone })
this.mock.onlyWhitelistedCanDoThis({ from: anyone })
);
});
});