From c8c23090ee8858c035d77006b6b5fe6bf2407c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Wed, 22 Aug 2018 18:47:41 -0300 Subject: [PATCH 01/14] Role tests (#1228) * Moved RBAC tests to access. * Added Roles.addMany and tests. * Fixed linter error. * Now using uint256 indexes. --- contracts/access/rbac/Roles.sol | 11 +++ contracts/mocks/RolesMock.sol | 30 ++++++++ test/{ownership => access}/rbac/RBAC.test.js | 0 test/access/rbac/Roles.test.js | 72 ++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 contracts/mocks/RolesMock.sol rename test/{ownership => access}/rbac/RBAC.test.js (100%) create mode 100644 test/access/rbac/Roles.test.js diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/rbac/Roles.sol index de46894095f..d478f6afa65 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/rbac/Roles.sol @@ -21,6 +21,17 @@ library Roles { _role.bearer[_account] = true; } + /** + * @dev give multiple accounts access to this role + */ + function addMany(Role storage _role, address[] _accounts) + internal + { + for (uint256 i = 0; i < _accounts.length; ++i) { + add(_role, _accounts[i]); + } + } + /** * @dev remove an account's access to this role */ diff --git a/contracts/mocks/RolesMock.sol b/contracts/mocks/RolesMock.sol new file mode 100644 index 00000000000..cf20dfa45e1 --- /dev/null +++ b/contracts/mocks/RolesMock.sol @@ -0,0 +1,30 @@ +pragma solidity ^0.4.24; + +import "../access/rbac/Roles.sol"; + + +contract RolesMock { + using Roles for Roles.Role; + + Roles.Role private dummyRole; + + function add(address _account) public { + dummyRole.add(_account); + } + + function addMany(address[] _accounts) public { + dummyRole.addMany(_accounts); + } + + function remove(address _account) public { + dummyRole.remove(_account); + } + + function check(address _account) public view { + dummyRole.check(_account); + } + + function has(address _account) public view returns (bool) { + return dummyRole.has(_account); + } +} diff --git a/test/ownership/rbac/RBAC.test.js b/test/access/rbac/RBAC.test.js similarity index 100% rename from test/ownership/rbac/RBAC.test.js rename to test/access/rbac/RBAC.test.js diff --git a/test/access/rbac/Roles.test.js b/test/access/rbac/Roles.test.js new file mode 100644 index 00000000000..d7269714d5f --- /dev/null +++ b/test/access/rbac/Roles.test.js @@ -0,0 +1,72 @@ +const { assertRevert } = require('../../helpers/assertRevert'); + +const RolesMock = artifacts.require('RolesMock'); + +require('chai') + .should(); + +contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { + beforeEach(async function () { + this.roles = await RolesMock.new(); + this.testRole = async (account, expected) => { + if (expected) { + (await this.roles.has(account)).should.equal(true); + await this.roles.check(account); // this call shouldn't revert, but is otherwise a no-op + } else { + (await this.roles.has(account)).should.equal(false); + await assertRevert(this.roles.check(account)); + } + }; + }); + + context('initially', function () { + it('doesn\'t pre-assign roles', async function () { + await this.testRole(authorized, false); + await this.testRole(otherAuthorized, false); + await this.testRole(anyone, false); + }); + + describe('adding roles', function () { + it('adds roles to a single account', async function () { + await this.roles.add(authorized); + await this.testRole(authorized, true); + await this.testRole(anyone, false); + }); + + it('adds roles to an already-assigned account', async function () { + await this.roles.add(authorized); + await this.roles.add(authorized); + await this.testRole(authorized, true); + }); + + it('adds roles to multiple accounts', async function () { + await this.roles.addMany([authorized, otherAuthorized]); + await this.testRole(authorized, true); + await this.testRole(otherAuthorized, true); + }); + + it('adds roles to multiple identical accounts', async function () { + await this.roles.addMany([authorized, authorized]); + await this.testRole(authorized, true); + }); + }); + }); + + context('with added roles', function () { + beforeEach(async function () { + await this.roles.addMany([authorized, otherAuthorized]); + }); + + describe('removing roles', function () { + it('removes a single role', async function () { + await this.roles.remove(authorized); + await this.testRole(authorized, false); + await this.testRole(otherAuthorized, true); + }); + + it('removes unassigned roles', async function () { + await this.roles.remove(anyone); + }); + }); + }); +}); From ed0419588570b6ec9dc72760a07d1a2207606396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Wed, 22 Aug 2018 20:51:52 -0300 Subject: [PATCH 02/14] Removed RBAC tokens (#1229) * Deleted RBACCappedTokenMock. * Removed RBACMintableToken. * Removed RBACMintableToken from the MintedCrowdsale tests. --- contracts/mocks/RBACCappedTokenMock.sol | 12 ------ contracts/token/ERC20/RBACMintableToken.sol | 41 ------------------- test/crowdsale/MintedCrowdsale.test.js | 17 -------- test/token/ERC20/RBACCappedToken.test.js | 19 --------- .../token/ERC20/RBACMintableToken.behavior.js | 30 -------------- test/token/ERC20/RBACMintableToken.test.js | 14 ------- 6 files changed, 133 deletions(-) delete mode 100644 contracts/mocks/RBACCappedTokenMock.sol delete mode 100644 contracts/token/ERC20/RBACMintableToken.sol delete mode 100644 test/token/ERC20/RBACCappedToken.test.js delete mode 100644 test/token/ERC20/RBACMintableToken.behavior.js delete mode 100644 test/token/ERC20/RBACMintableToken.test.js diff --git a/contracts/mocks/RBACCappedTokenMock.sol b/contracts/mocks/RBACCappedTokenMock.sol deleted file mode 100644 index bf4ff8f2d6a..00000000000 --- a/contracts/mocks/RBACCappedTokenMock.sol +++ /dev/null @@ -1,12 +0,0 @@ -pragma solidity ^0.4.24; - -import "../token/ERC20/RBACMintableToken.sol"; -import "../token/ERC20/CappedToken.sol"; - - -contract RBACCappedTokenMock is CappedToken, RBACMintableToken { - constructor(uint256 _cap) - CappedToken(_cap) - public - {} -} diff --git a/contracts/token/ERC20/RBACMintableToken.sol b/contracts/token/ERC20/RBACMintableToken.sol deleted file mode 100644 index ed7e8d210c3..00000000000 --- a/contracts/token/ERC20/RBACMintableToken.sol +++ /dev/null @@ -1,41 +0,0 @@ -pragma solidity ^0.4.24; - -import "./MintableToken.sol"; -import "../../access/rbac/RBAC.sol"; - - -/** - * @title RBACMintableToken - * @author Vittorio Minacori (@vittominacori) - * @dev Mintable Token, with RBAC minter permissions - */ -contract RBACMintableToken is MintableToken, RBAC { - /** - * A constant role name for indicating minters. - */ - string public constant ROLE_MINTER = "minter"; - - /** - * @dev override the Mintable token modifier to add role based logic - */ - modifier hasMintPermission() { - checkRole(msg.sender, ROLE_MINTER); - _; - } - - /** - * @dev add a minter role to an address - * @param _minter address - */ - function addMinter(address _minter) public onlyOwner { - addRole(_minter, ROLE_MINTER); - } - - /** - * @dev remove a minter role from an address - * @param _minter address - */ - function removeMinter(address _minter) public onlyOwner { - removeRole(_minter, ROLE_MINTER); - } -} diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index bf7267b50ca..ec36f9340bd 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -5,7 +5,6 @@ const BigNumber = web3.BigNumber; const MintedCrowdsale = artifacts.require('MintedCrowdsaleImpl'); const MintableToken = artifacts.require('MintableToken'); -const RBACMintableToken = artifacts.require('RBACMintableToken'); contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { const rate = new BigNumber(1000); @@ -24,20 +23,4 @@ contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); }); - - describe('using RBACMintableToken', function () { - const ROLE_MINTER = 'minter'; - - beforeEach(async function () { - this.token = await RBACMintableToken.new(); - this.crowdsale = await MintedCrowdsale.new(rate, wallet, this.token.address); - await this.token.addMinter(this.crowdsale.address); - }); - - it('should have minter role on token', async function () { - (await this.token.hasRole(this.crowdsale.address, ROLE_MINTER)).should.be.true; - }); - - shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); - }); }); diff --git a/test/token/ERC20/RBACCappedToken.test.js b/test/token/ERC20/RBACCappedToken.test.js deleted file mode 100644 index 3f3ca71a78e..00000000000 --- a/test/token/ERC20/RBACCappedToken.test.js +++ /dev/null @@ -1,19 +0,0 @@ -const { ether } = require('../../helpers/ether'); -const { shouldBehaveLikeRBACMintableToken } = require('./RBACMintableToken.behavior'); -const { shouldBehaveLikeMintableToken } = require('./MintableToken.behavior'); -const { shouldBehaveLikeCappedToken } = require('./CappedToken.behavior'); - -const RBACCappedTokenMock = artifacts.require('RBACCappedTokenMock'); - -contract('RBACCappedToken', function ([_, owner, minter, ...otherAccounts]) { - const cap = ether(1000); - - beforeEach(async function () { - this.token = await RBACCappedTokenMock.new(cap, { from: owner }); - await this.token.addMinter(minter, { from: owner }); - }); - - shouldBehaveLikeMintableToken(owner, minter, otherAccounts); - shouldBehaveLikeRBACMintableToken(owner, otherAccounts); - shouldBehaveLikeCappedToken(minter, otherAccounts, cap); -}); diff --git a/test/token/ERC20/RBACMintableToken.behavior.js b/test/token/ERC20/RBACMintableToken.behavior.js deleted file mode 100644 index 7ed720b47a2..00000000000 --- a/test/token/ERC20/RBACMintableToken.behavior.js +++ /dev/null @@ -1,30 +0,0 @@ -const { expectThrow } = require('../../helpers/expectThrow'); - -const ROLE_MINTER = 'minter'; - -function shouldBehaveLikeRBACMintableToken (owner, [anyone]) { - describe('handle roles', function () { - it('owner can add and remove a minter role', async function () { - await this.token.addMinter(anyone, { from: owner }); - (await this.token.hasRole(anyone, ROLE_MINTER)).should.be.true; - - await this.token.removeMinter(anyone, { from: owner }); - (await this.token.hasRole(anyone, ROLE_MINTER)).should.be.false; - }); - - it('anyone can\'t add or remove a minter role', async function () { - await expectThrow( - this.token.addMinter(anyone, { from: anyone }) - ); - - await this.token.addMinter(anyone, { from: owner }); - await expectThrow( - this.token.removeMinter(anyone, { from: anyone }) - ); - }); - }); -} - -module.exports = { - shouldBehaveLikeRBACMintableToken, -}; diff --git a/test/token/ERC20/RBACMintableToken.test.js b/test/token/ERC20/RBACMintableToken.test.js deleted file mode 100644 index 839b73db8b6..00000000000 --- a/test/token/ERC20/RBACMintableToken.test.js +++ /dev/null @@ -1,14 +0,0 @@ -const { shouldBehaveLikeRBACMintableToken } = require('./RBACMintableToken.behavior'); -const { shouldBehaveLikeMintableToken } = require('./MintableToken.behavior'); - -const RBACMintableToken = artifacts.require('RBACMintableToken'); - -contract('RBACMintableToken', function ([_, owner, minter, ...otherAccounts]) { - beforeEach(async function () { - this.token = await RBACMintableToken.new({ from: owner }); - await this.token.addMinter(minter, { from: owner }); - }); - - shouldBehaveLikeRBACMintableToken(owner, otherAccounts); - shouldBehaveLikeMintableToken(owner, minter, otherAccounts); -}); From 5b7ec56f283f12ca26ab4166e9096018aceaae6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Tue, 28 Aug 2018 15:10:25 -0300 Subject: [PATCH 03/14] Roles can now be transfered. (#1235) * Roles can now be transfered. * Now explicitly checking support for the null address. * Now rejecting transfer to a role-haver. * Added renounce, roles can no longer be transfered to 0. * Fixed linter errors. * Fixed a Roles test. --- contracts/access/rbac/Roles.sol | 30 ++++++++++--------- contracts/mocks/RolesMock.sol | 8 ++++++ test/access/rbac/Roles.test.js | 51 ++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/rbac/Roles.sol index d478f6afa65..f5c95b89baf 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/rbac/Roles.sol @@ -15,18 +15,14 @@ library Roles { /** * @dev give an account access to this role */ - function add(Role storage _role, address _account) - internal - { + function add(Role storage _role, address _account) internal { _role.bearer[_account] = true; } /** * @dev give multiple accounts access to this role */ - function addMany(Role storage _role, address[] _accounts) - internal - { + function addMany(Role storage _role, address[] _accounts) internal { for (uint256 i = 0; i < _accounts.length; ++i) { add(_role, _accounts[i]); } @@ -35,20 +31,28 @@ library Roles { /** * @dev remove an account's access to this role */ - function remove(Role storage _role, address _account) - internal - { + function remove(Role storage _role, address _account) internal { _role.bearer[_account] = false; } + function transfer(Role storage _role, address _account) internal { + require(_account != address(0)); + require(!has(_role, _account)); + require(has(_role, msg.sender)); + + remove(_role, msg.sender); + add(_role, _account); + } + + function renounce(Role storage _role) internal { + remove(_role, msg.sender); + } + /** * @dev check if an account has this role * // reverts */ - function check(Role storage _role, address _account) - internal - view - { + function check(Role storage _role, address _account) internal view { require(has(_role, _account)); } diff --git a/contracts/mocks/RolesMock.sol b/contracts/mocks/RolesMock.sol index cf20dfa45e1..96e0b64aaef 100644 --- a/contracts/mocks/RolesMock.sol +++ b/contracts/mocks/RolesMock.sol @@ -20,6 +20,14 @@ contract RolesMock { dummyRole.remove(_account); } + function renounce() public { + dummyRole.renounce(); + } + + function transfer(address _account) public { + dummyRole.transfer(_account); + } + function check(address _account) public view { dummyRole.check(_account); } diff --git a/test/access/rbac/Roles.test.js b/test/access/rbac/Roles.test.js index d7269714d5f..743d6fe082c 100644 --- a/test/access/rbac/Roles.test.js +++ b/test/access/rbac/Roles.test.js @@ -6,6 +6,8 @@ require('chai') .should(); contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + beforeEach(async function () { this.roles = await RolesMock.new(); this.testRole = async (account, expected) => { @@ -49,6 +51,10 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { await this.roles.addMany([authorized, authorized]); await this.testRole(authorized, true); }); + + it('doesn\'t revert when adding roles to the null account', async function () { + await this.roles.add(ZERO_ADDRESS); + }); }); }); @@ -64,9 +70,52 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { await this.testRole(otherAuthorized, true); }); - it('removes unassigned roles', async function () { + it('doesn\'t revert when removing unassigned roles', async function () { await this.roles.remove(anyone); }); + + it('doesn\'t revert when removing roles from the null account', async function () { + await this.roles.remove(ZERO_ADDRESS); + }); + }); + + describe('transfering roles', function () { + context('from account with role', function () { + const from = authorized; + + it('transfers to other account with no role', async function () { + await this.roles.transfer(anyone, { from }); + await this.testRole(anyone, true); + await this.testRole(authorized, false); + }); + + it('reverts when transfering to an account with role', async function () { + await assertRevert(this.roles.transfer(otherAuthorized, { from })); + }); + + it('reverts when transfering to the null account', async function () { + await assertRevert(this.roles.transfer(ZERO_ADDRESS, { from })); + }); + }); + + context('from account without role', function () { + const from = anyone; + + it('reverts', async function () { + await assertRevert(this.roles.transfer(anyone, { from })); + }); + }); + }); + + describe('renouncing roles', function () { + it('renounces an assigned role', async function () { + await this.roles.renounce({ from: authorized }); + await this.testRole(authorized, false); + }); + + it('doesn\'t revert when renouncing unassigned role', async function () { + await this.roles.renounce({ from: anyone }); + }); }); }); }); From 0530e1e555a69e4d0fccbf0053dafdb77931db2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 30 Aug 2018 11:31:42 -0300 Subject: [PATCH 04/14] True Ownership (#1247) * Added barebones Secondary. * Added transferPrimary * Escrow is now Secondary instead of Ownable. * Now reverting on transfers to 0. * The Secondary's primary is now private. --- contracts/mocks/SecondaryMock.sol | 9 +++++ contracts/ownership/Secondary.sol | 35 +++++++++++++++++++ contracts/payment/Escrow.sol | 10 +++--- test/ownership/Secondary.test.js | 57 +++++++++++++++++++++++++++++++ test/payment/Escrow.behavior.js | 30 ++++++++-------- test/payment/Escrow.test.js | 6 ++-- 6 files changed, 124 insertions(+), 23 deletions(-) create mode 100644 contracts/mocks/SecondaryMock.sol create mode 100644 contracts/ownership/Secondary.sol create mode 100644 test/ownership/Secondary.test.js diff --git a/contracts/mocks/SecondaryMock.sol b/contracts/mocks/SecondaryMock.sol new file mode 100644 index 00000000000..6863112fa8c --- /dev/null +++ b/contracts/mocks/SecondaryMock.sol @@ -0,0 +1,9 @@ +pragma solidity ^0.4.24; + +import "../ownership/Secondary.sol"; + + +contract SecondaryMock is Secondary { + function onlyPrimaryMock() public view onlyPrimary { + } +} diff --git a/contracts/ownership/Secondary.sol b/contracts/ownership/Secondary.sol new file mode 100644 index 00000000000..e0397216cd1 --- /dev/null +++ b/contracts/ownership/Secondary.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.4.24; + + +/** + * @title Secondary + * @dev A Secondary contract can only be used by its primary account (the one that created it) + */ +contract Secondary { + address private primary_; + + /** + * @dev Sets the primary account to the one that is creating the Secondary contract. + */ + constructor() public { + primary_ = msg.sender; + } + + /** + * @dev Reverts if called from any account other than the primary. + */ + modifier onlyPrimary() { + require(msg.sender == primary_); + _; + } + + function primary() public view returns (address) { + return primary_; + } + + function transferPrimary(address _recipient) public onlyPrimary { + require(_recipient != address(0)); + + primary_ = _recipient; + } +} diff --git a/contracts/payment/Escrow.sol b/contracts/payment/Escrow.sol index dfa762ebb9f..fab31d95c3b 100644 --- a/contracts/payment/Escrow.sol +++ b/contracts/payment/Escrow.sol @@ -1,17 +1,17 @@ pragma solidity ^0.4.23; import "../math/SafeMath.sol"; -import "../ownership/Ownable.sol"; +import "../ownership/Secondary.sol"; /** * @title Escrow * @dev Base escrow contract, holds funds destinated to a payee until they * withdraw them. The contract that uses the escrow as its payment method - * should be its owner, and provide public methods redirecting to the escrow's + * should be its primary, and provide public methods redirecting to the escrow's * deposit and withdraw. */ -contract Escrow is Ownable { +contract Escrow is Secondary { using SafeMath for uint256; event Deposited(address indexed payee, uint256 weiAmount); @@ -27,7 +27,7 @@ contract Escrow is Ownable { * @dev Stores the sent amount as credit to be withdrawn. * @param _payee The destination address of the funds. */ - function deposit(address _payee) public onlyOwner payable { + function deposit(address _payee) public onlyPrimary payable { uint256 amount = msg.value; deposits[_payee] = deposits[_payee].add(amount); @@ -38,7 +38,7 @@ contract Escrow is Ownable { * @dev Withdraw accumulated balance for a payee. * @param _payee The address whose funds will be withdrawn and transferred to. */ - function withdraw(address _payee) public onlyOwner { + function withdraw(address _payee) public onlyPrimary { uint256 payment = deposits[_payee]; assert(address(this).balance >= payment); diff --git a/test/ownership/Secondary.test.js b/test/ownership/Secondary.test.js new file mode 100644 index 00000000000..000d0af11ba --- /dev/null +++ b/test/ownership/Secondary.test.js @@ -0,0 +1,57 @@ +const { assertRevert } = require('../helpers/assertRevert'); + +const SecondaryMock = artifacts.require('SecondaryMock'); + +require('chai') + .should(); + +contract('Secondary', function ([_, primary, newPrimary, anyone]) { + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + + beforeEach(async function () { + this.secondary = await SecondaryMock.new({ from: primary }); + }); + + it('stores the primary\'s address', async function () { + (await this.secondary.primary()).should.equal(primary); + }); + + describe('onlyPrimary', function () { + it('allows the primary account to call onlyPrimary functions', async function () { + await this.secondary.onlyPrimaryMock({ from: primary }); + }); + + it('reverts when anyone calls onlyPrimary functions', async function () { + await assertRevert(this.secondary.onlyPrimaryMock({ from: anyone })); + }); + }); + + describe('transferPrimary', function () { + it('makes the recipient the new primary', async function () { + await this.secondary.transferPrimary(newPrimary, { from: primary }); + (await this.secondary.primary()).should.equal(newPrimary); + }); + + it('reverts when transfering to the null address', async function () { + await assertRevert(this.secondary.transferPrimary(ZERO_ADDRESS, { from: primary })); + }); + + it('reverts when called by anyone', async function () { + await assertRevert(this.secondary.transferPrimary(newPrimary, { from: anyone })); + }); + + context('with new primary', function () { + beforeEach(async function () { + await this.secondary.transferPrimary(newPrimary, { from: primary }); + }); + + it('allows the new primary account to call onlyPrimary functions', async function () { + await this.secondary.onlyPrimaryMock({ from: newPrimary }); + }); + + it('reverts when the old primary account calls onlyPrimary functions', async function () { + await assertRevert(this.secondary.onlyPrimaryMock({ from: primary })); + }); + }); + }); +}); diff --git a/test/payment/Escrow.behavior.js b/test/payment/Escrow.behavior.js index 64e857fa401..761aecd890d 100644 --- a/test/payment/Escrow.behavior.js +++ b/test/payment/Escrow.behavior.js @@ -9,13 +9,13 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeEscrow (owner, [payee1, payee2]) { +function shouldBehaveLikeEscrow (primary, [payee1, payee2]) { const amount = web3.toWei(42.0, 'ether'); describe('as an escrow', function () { describe('deposits', function () { it('can accept a single deposit', async function () { - await this.escrow.deposit(payee1, { from: owner, value: amount }); + await this.escrow.deposit(payee1, { from: primary, value: amount }); (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount); @@ -23,23 +23,23 @@ function shouldBehaveLikeEscrow (owner, [payee1, payee2]) { }); it('can accept an empty deposit', async function () { - await this.escrow.deposit(payee1, { from: owner, value: 0 }); + await this.escrow.deposit(payee1, { from: primary, value: 0 }); }); - it('only the owner can deposit', async function () { + it('only the primary account can deposit', async function () { await expectThrow(this.escrow.deposit(payee1, { from: payee2 }), EVMRevert); }); it('emits a deposited event', async function () { - const receipt = await this.escrow.deposit(payee1, { from: owner, value: amount }); + const receipt = await this.escrow.deposit(payee1, { from: primary, value: amount }); const event = expectEvent.inLogs(receipt.logs, 'Deposited', { payee: payee1 }); event.args.weiAmount.should.be.bignumber.equal(amount); }); it('can add multiple deposits on a single account', async function () { - await this.escrow.deposit(payee1, { from: owner, value: amount }); - await this.escrow.deposit(payee1, { from: owner, value: amount * 2 }); + await this.escrow.deposit(payee1, { from: primary, value: amount }); + await this.escrow.deposit(payee1, { from: primary, value: amount * 2 }); (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3); @@ -47,8 +47,8 @@ function shouldBehaveLikeEscrow (owner, [payee1, payee2]) { }); it('can track deposits to multiple accounts', async function () { - await this.escrow.deposit(payee1, { from: owner, value: amount }); - await this.escrow.deposit(payee2, { from: owner, value: amount * 2 }); + await this.escrow.deposit(payee1, { from: primary, value: amount }); + await this.escrow.deposit(payee2, { from: primary, value: amount * 2 }); (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3); @@ -62,8 +62,8 @@ function shouldBehaveLikeEscrow (owner, [payee1, payee2]) { it('can withdraw payments', async function () { const payeeInitialBalance = await ethGetBalance(payee1); - await this.escrow.deposit(payee1, { from: owner, value: amount }); - await this.escrow.withdraw(payee1, { from: owner }); + await this.escrow.deposit(payee1, { from: primary, value: amount }); + await this.escrow.withdraw(payee1, { from: primary }); (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0); @@ -74,16 +74,16 @@ function shouldBehaveLikeEscrow (owner, [payee1, payee2]) { }); it('can do an empty withdrawal', async function () { - await this.escrow.withdraw(payee1, { from: owner }); + await this.escrow.withdraw(payee1, { from: primary }); }); - it('only the owner can withdraw', async function () { + it('only the primary account can withdraw', async function () { await expectThrow(this.escrow.withdraw(payee1, { from: payee1 }), EVMRevert); }); it('emits a withdrawn event', async function () { - await this.escrow.deposit(payee1, { from: owner, value: amount }); - const receipt = await this.escrow.withdraw(payee1, { from: owner }); + await this.escrow.deposit(payee1, { from: primary, value: amount }); + const receipt = await this.escrow.withdraw(payee1, { from: primary }); const event = expectEvent.inLogs(receipt.logs, 'Withdrawn', { payee: payee1 }); event.args.weiAmount.should.be.bignumber.equal(amount); diff --git a/test/payment/Escrow.test.js b/test/payment/Escrow.test.js index a67b26417d0..6af510d659f 100644 --- a/test/payment/Escrow.test.js +++ b/test/payment/Escrow.test.js @@ -2,10 +2,10 @@ const { shouldBehaveLikeEscrow } = require('./Escrow.behavior'); const Escrow = artifacts.require('Escrow'); -contract('Escrow', function ([_, owner, ...otherAccounts]) { +contract('Escrow', function ([_, primary, ...otherAccounts]) { beforeEach(async function () { - this.escrow = await Escrow.new({ from: owner }); + this.escrow = await Escrow.new({ from: primary }); }); - shouldBehaveLikeEscrow(owner, otherAccounts); + shouldBehaveLikeEscrow(primary, otherAccounts); }); From d8e7c25a74c571d511daa74210e5196bb5bbf01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Tue, 4 Sep 2018 14:23:06 -0300 Subject: [PATCH 05/14] MintableToken using Roles (#1236) * Minor test style improvements (#1219) * Changed .eq to .equal * Changed equal(bool) to .to.be.bool * Changed be.bool to equal(bool), disallowed unused expressions. * Add ERC165Query library (#1086) * Add ERC165Query library * Address PR Comments * Add tests and mocks from #1024 and refactor code slightly * Fix javascript and solidity linting errors * Split supportsInterface into three methods as discussed in #1086 * Change InterfaceId_ERC165 comment to match style in the rest of the repo * Fix max-len lint issue on ERC165Checker.sol * Conditionally ignore the asserts during solidity-coverage test * Switch to abi.encodeWithSelector and add test for account addresses * Switch to supportsInterfaces API as suggested by @frangio * Adding ERC165InterfacesSupported.sol * Fix style issues * Add test for supportsInterfaces returning false * Add ERC165Checker.sol newline * feat: fix coverage implementation * fix: solidity linting error * fix: revert to using boolean tests instead of require statements * fix: make supportsERC165Interface private again * rename SupportsInterfaceWithLookupMock to avoid name clashing * Added mint and burn tests for zero amounts. (#1230) * Changed .eq to .equal. (#1231) * ERC721 pausable token (#1154) * ERC721 pausable token * Reuse of ERC721 Basic behavior for Pausable, split view checks in paused state & style fixes * [~] paused token behavior * Add some detail to releasing steps (#1190) * add note about pulling upstream changes to release branch * add comment about upstream changes in merging section * Increase test coverage (#1237) * Fixed a SplitPayment test * Deleted unnecessary function. * Improved PostDeliveryCrowdsale tests. * Improved RefundableCrowdsale tests. * Improved MintedCrowdsale tests. * Improved IncreasingPriceCrowdsale tests. * Fixed a CappedCrowdsale test. * Improved TimedCrowdsale tests. * Improved descriptions of added tests. * ci: trigger docs update on tag (#1186) * MintableToken now uses Roles. * Fixed FinalizableCrowdsale test. * Roles can now be transfered. * Fixed tests related to MintableToken. * Removed Roles.check. * Renamed transferMintPermission. * Moved MinterRole * Fixed RBAC. * Adressed review comments. * Addressed review comments * Fixed linter errors. * Added Events tests of Pausable contract (#1207) * Fixed roles tests. * Rename events to past-tense (#1181) * fix: refactor sign.js and related tests (#1243) * fix: refactor sign.js and related tests * fix: remove unused dep * fix: update package.json correctly * Added "_" sufix to internal variables (#1171) * Added PublicRole test. * Fixed crowdsale tests. * Rename ERC interfaces to I prefix (#1252) * rename ERC20 to IERC20 * move ERC20.sol to IERC20.sol * rename StandardToken to ERC20 * rename StandardTokenMock to ERC20Mock * move StandardToken.sol to ERC20.sol, likewise test and mock files * rename MintableToken to ERC20Mintable * move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files * rename BurnableToken to ERC20Burnable * move BurnableToken.sol to ERC20Burnable.sol, likewise for related files * rename CappedToken to ERC20Capped * move CappedToken.sol to ERC20Capped.sol, likewise for related files * rename PausableToken to ERC20Pausable * move PausableToken.sol to ERC20Pausable.sol, likewise for related files * rename DetailedERC20 to ERC20Detailed * move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files * rename ERC721 to IERC721, and likewise for other related interfaces * move ERC721.sol to IERC721.sol, likewise for other 721 interfaces * rename ERC721Token to ERC721 * move ERC721Token.sol to ERC721.sol, likewise for related files * rename ERC721BasicToken to ERC721Basic * move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files * rename ERC721PausableToken to ERC721Pausable * move ERC721PausableToken.sol to ERC721Pausable.sol * rename ERC165 to IERC165 * move ERC165.sol to IERC165.sol * amend comment that ERC20 is based on FirstBlood * fix comments mentioning IERC721Receiver * added explicit visibility (#1261) * Remove underscores from event parameters. (#1258) * Remove underscores from event parameters. Fixes #1175 * Add comment about ERC * Move contracts to subdirectories (#1253) * Move contracts to subdirectories Fixes #1177. This Change also removes the LimitBalance contract. * fix import * move MerkleProof to cryptography * Fix import * Remove HasNoEther, HasNoTokens, HasNoContracts, and NoOwner (#1254) * remove HasNoEther, HasNoTokens, HasNoContracts, and NoOwner * remove unused ERC223TokenMock * remove Contactable * remove TokenDestructible * remove DeprecatedERC721 * inline Destructible#destroy in Bounty * remove Destructible * Functions in interfaces changed to "external" (#1263) * Add a leading underscore to internal and private functions. (#1257) * Add a leading underscore to internal and private functions. Fixes #1176 * Remove super * update the ERC721 changes * add missing underscore after merge * Fix mock * Improve encapsulation on SignatureBouncer, Whitelist and RBAC example (#1265) * Improve encapsulation on Whitelist * remove only * update whitelisted crowdsale test * Improve encapsulation on SignatureBouncer * fix missing test * Improve encapsulation on RBAC example * Improve encapsulation on RBAC example * Remove extra visibility * Improve encapsulation on ERC20 Mintable * Improve encapsulation on Superuser * fix lint * add missing constant * Addressed review comments. * Fixed build error. --- .travis.yml | 8 + CODE_STYLE.md | 59 ++- README.md | 3 +- RELEASING.md | 14 +- contracts/LimitBalance.sol | 31 -- contracts/access/SignatureBouncer.sol | 51 ++- contracts/access/Whitelist.sol | 13 +- contracts/access/rbac/MinterRole.sol | 39 ++ contracts/access/rbac/RBAC.sol | 6 +- contracts/access/rbac/Roles.sol | 8 - .../BreakInvariantBounty.sol} | 22 +- contracts/crowdsale/Crowdsale.sol | 16 +- .../distribution/FinalizableCrowdsale.sol | 10 +- .../distribution/PostDeliveryCrowdsale.sol | 2 +- .../distribution/RefundableCrowdsale.sol | 18 +- .../crowdsale/emission/AllowanceCrowdsale.sol | 4 +- .../crowdsale/emission/MintedCrowdsale.sol | 4 +- .../price/IncreasingPriceCrowdsale.sol | 2 +- .../ECDSA.sol} | 2 +- contracts/{ => cryptography}/MerkleProof.sol | 0 contracts/examples/RBACWithAdmin.sol | 15 +- contracts/examples/SampleCrowdsale.sol | 11 +- contracts/examples/SimpleToken.sol | 6 +- contracts/introspection/ERC165Checker.sol | 149 ++++++++ .../introspection/{ERC165.sol => IERC165.sol} | 4 +- .../SupportsInterfaceWithLookup.sol | 10 +- contracts/lifecycle/Destructible.sol | 22 -- contracts/lifecycle/Pausable.sol | 8 +- contracts/lifecycle/TokenDestructible.sol | 36 -- contracts/mocks/AllowanceCrowdsaleImpl.sol | 4 +- contracts/mocks/AutoIncrementingImpl.sol | 2 +- contracts/mocks/BouncerMock.sol | 6 +- contracts/mocks/CappedCrowdsaleImpl.sol | 4 +- contracts/mocks/DestructibleMock.sol | 8 - contracts/mocks/DetailedERC20Mock.sol | 8 +- .../{ECRecoveryMock.sol => ECDSAMock.sol} | 6 +- .../ERC165/ERC165InterfacesSupported.sol | 69 ++++ contracts/mocks/ERC165/ERC165NotSupported.sol | 6 + contracts/mocks/ERC165CheckerMock.sol | 32 ++ ...bleTokenMock.sol => ERC20BurnableMock.sol} | 4 +- contracts/mocks/ERC20MintableMock.sol | 21 ++ .../{StandardTokenMock.sol => ERC20Mock.sol} | 6 +- ...bleTokenMock.sol => ERC20PausableMock.sol} | 6 +- contracts/mocks/ERC20WithMetadataMock.sol | 4 +- contracts/mocks/ERC223TokenMock.sol | 33 -- ...BasicTokenMock.sol => ERC721BasicMock.sol} | 6 +- .../{ERC721TokenMock.sol => ERC721Mock.sol} | 20 +- contracts/mocks/ERC721PausableMock.sol | 22 ++ contracts/mocks/ERC721ReceiverMock.sol | 26 +- contracts/mocks/FinalizableCrowdsaleImpl.sol | 4 +- contracts/mocks/HasNoEtherTest.sol | 12 - .../mocks/IncreasingPriceCrowdsaleImpl.sol | 2 +- .../mocks/IndividuallyCappedCrowdsaleImpl.sol | 4 +- .../mocks/InsecureInvariantTargetBounty.sol | 20 + contracts/mocks/InsecureTargetBounty.sol | 17 - contracts/mocks/LimitBalanceMock.sol | 13 - contracts/mocks/MerkleProofWrapper.sol | 2 +- contracts/mocks/MintedCrowdsaleImpl.sol | 4 +- contracts/mocks/PostDeliveryCrowdsaleImpl.sol | 4 +- contracts/mocks/PullPaymentMock.sol | 2 +- contracts/mocks/RBACMock.sol | 10 +- contracts/mocks/ReentrancyMock.sol | 2 +- contracts/mocks/RefundableCrowdsaleImpl.sol | 4 +- contracts/mocks/RolesMock.sol | 4 - contracts/mocks/SafeERC20Helper.sol | 28 +- .../mocks/SecureInvariantTargetBounty.sol | 20 + contracts/mocks/SecureTargetBounty.sol | 17 - contracts/mocks/TimedCrowdsaleImpl.sol | 4 +- contracts/mocks/WhitelistedCrowdsaleImpl.sol | 4 +- contracts/ownership/CanReclaimToken.sol | 6 +- contracts/ownership/Contactable.sol | 22 -- contracts/ownership/HasNoContracts.sol | 22 -- contracts/ownership/HasNoEther.sol | 41 --- contracts/ownership/HasNoTokens.sol | 35 -- contracts/ownership/Heritable.sol | 15 +- contracts/ownership/NoOwner.sol | 15 - contracts/ownership/Superuser.sol | 8 +- contracts/payment/Escrow.sol | 10 +- contracts/payment/PullPayment.sol | 4 +- contracts/payment/SplitPayment.sol | 4 +- contracts/proposals/ERC1046/TokenMetadata.sol | 6 +- contracts/token/ERC20/ERC20.sol | 213 +++++++++-- .../{BurnableToken.sol => ERC20Burnable.sol} | 10 +- .../{CappedToken.sol => ERC20Capped.sol} | 9 +- .../{DetailedERC20.sol => ERC20Detailed.sol} | 6 +- .../{MintableToken.sol => ERC20Mintable.sol} | 20 +- .../{PausableToken.sol => ERC20Pausable.sol} | 6 +- contracts/token/ERC20/IERC20.sol | 35 ++ contracts/token/ERC20/SafeERC20.sol | 8 +- contracts/token/ERC20/StandardToken.sol | 203 ----------- contracts/token/ERC20/TokenTimelock.sol | 6 +- contracts/token/ERC20/TokenVesting.sol | 10 +- contracts/token/ERC721/DeprecatedERC721.sol | 15 - contracts/token/ERC721/ERC721.sol | 203 +++++++++-- contracts/token/ERC721/ERC721Basic.sol | 344 +++++++++++++++--- contracts/token/ERC721/ERC721BasicToken.sol | 310 ---------------- contracts/token/ERC721/ERC721Holder.sol | 4 +- contracts/token/ERC721/ERC721Pausable.sol | 42 +++ contracts/token/ERC721/ERC721Token.sol | 201 ---------- contracts/token/ERC721/IERC721.sol | 40 ++ contracts/token/ERC721/IERC721Basic.sol | 80 ++++ ...ERC721Receiver.sol => IERC721Receiver.sol} | 4 +- .../{AddressUtils.sol => utils/Address.sol} | 2 +- contracts/{ => utils}/AutoIncrementing.sol | 2 +- contracts/{ => utils}/ReentrancyGuard.sol | 0 package-lock.json | 180 +++++---- package.json | 1 - scripts/ci/trigger_docs_update | 32 ++ test/.eslintrc | 5 - test/AutoIncrementing.test.js | 6 +- ...y.test.js => BreakInvariantBounty.test.js} | 12 +- test/Heritable.test.js | 24 +- test/LimitBalance.test.js | 59 --- test/access/SignatureBouncer.test.js | 33 +- test/{ownership => access}/Whitelist.test.js | 34 +- test/access/rbac/PublicRole.behavior.js | 96 +++++ test/access/rbac/Roles.test.js | 37 +- test/crowdsale/AllowanceCrowdsale.test.js | 6 +- test/crowdsale/CappedCrowdsale.test.js | 79 ++-- test/crowdsale/Crowdsale.test.js | 12 +- test/crowdsale/FinalizableCrowdsale.test.js | 7 +- .../IncreasingPriceCrowdsale.test.js | 90 +++-- test/crowdsale/MintedCrowdsale.behavior.js | 6 +- test/crowdsale/MintedCrowdsale.test.js | 31 +- test/crowdsale/PostDeliveryCrowdsale.test.js | 57 +-- test/crowdsale/RefundableCrowdsale.test.js | 109 ++++-- test/crowdsale/TimedCrowdsale.test.js | 53 ++- test/crowdsale/WhitelistedCrowdsale.test.js | 10 +- test/examples/SampleCrowdsale.test.js | 12 +- test/examples/SimpleToken.test.js | 6 +- test/helpers/expectEvent.js | 2 +- test/helpers/sign.js | 35 +- test/introspection/ERC165Checker.test.js | 137 +++++++ .../SupportsInterface.behavior.js | 2 +- .../{ECRecovery.test.js => ECDSA.test.js} | 52 ++- test/library/MerkleProof.test.js | 6 +- test/lifecycle/Destructible.test.js | 33 -- test/lifecycle/Pausable.test.js | 15 +- test/lifecycle/TokenDestructible.test.js | 39 -- test/ownership/CanReclaimToken.test.js | 4 +- test/ownership/Claimable.test.js | 6 +- test/ownership/Contactable.test.js | 25 -- test/ownership/DelayedClaimable.test.js | 8 +- test/ownership/HasNoContracts.test.js | 29 -- test/ownership/HasNoEther.test.js | 61 ---- test/ownership/HasNoTokens.test.js | 46 --- test/ownership/Ownable.behavior.js | 6 +- test/ownership/Superuser.test.js | 6 +- test/payment/SplitPayment.test.js | 14 +- test/proposals/ERC1046/TokenMetadata.test.js | 2 +- test/token/ERC20/BurnableToken.behavior.js | 99 ----- test/token/ERC20/BurnableToken.test.js | 12 - test/token/ERC20/CappedToken.test.js | 25 -- test/token/ERC20/DetailedERC20.test.js | 6 +- .../{StandardToken.test.js => ERC20.test.js} | 86 ++--- test/token/ERC20/ERC20Burnable.behavior.js | 117 ++++++ test/token/ERC20/ERC20Burnable.test.js | 12 + ...en.behavior.js => ERC20Capped.behavior.js} | 4 +- test/token/ERC20/ERC20Capped.test.js | 25 ++ test/token/ERC20/ERC20Mintable.behavior.js | 158 ++++++++ test/token/ERC20/ERC20Mintable.test.js | 22 ++ ...bleToken.test.js => ERC20Pausable.test.js} | 24 +- test/token/ERC20/MintableToken.behavior.js | 154 -------- test/token/ERC20/MintableToken.test.js | 10 - test/token/ERC20/TokenTimelock.test.js | 8 +- test/token/ERC20/TokenVesting.test.js | 18 +- .../{ERC721Token.test.js => ERC721.test.js} | 20 +- ...en.behavior.js => ERC721Basic.behavior.js} | 74 ++-- test/token/ERC721/ERC721Basic.test.js | 18 + test/token/ERC721/ERC721BasicToken.test.js | 18 - test/token/ERC721/ERC721MintBurn.behavior.js | 18 +- test/token/ERC721/ERC721Pausable.test.js | 36 ++ .../ERC721/ERC721PausedToken.behavior.js | 88 +++++ 173 files changed, 2960 insertions(+), 2621 deletions(-) delete mode 100644 contracts/LimitBalance.sol create mode 100644 contracts/access/rbac/MinterRole.sol rename contracts/{Bounty.sol => bounties/BreakInvariantBounty.sol} (78%) rename contracts/{ECRecovery.sol => cryptography/ECDSA.sol} (98%) rename contracts/{ => cryptography}/MerkleProof.sol (100%) create mode 100644 contracts/introspection/ERC165Checker.sol rename contracts/introspection/{ERC165.sol => IERC165.sol} (92%) delete mode 100644 contracts/lifecycle/Destructible.sol delete mode 100644 contracts/lifecycle/TokenDestructible.sol delete mode 100644 contracts/mocks/DestructibleMock.sol rename contracts/mocks/{ECRecoveryMock.sol => ECDSAMock.sol} (78%) create mode 100644 contracts/mocks/ERC165/ERC165InterfacesSupported.sol create mode 100644 contracts/mocks/ERC165/ERC165NotSupported.sol create mode 100644 contracts/mocks/ERC165CheckerMock.sol rename contracts/mocks/{BurnableTokenMock.sol => ERC20BurnableMock.sol} (63%) create mode 100644 contracts/mocks/ERC20MintableMock.sol rename contracts/mocks/{StandardTokenMock.sol => ERC20Mock.sol} (78%) rename contracts/mocks/{PausableTokenMock.sol => ERC20PausableMock.sol} (55%) delete mode 100644 contracts/mocks/ERC223TokenMock.sol rename contracts/mocks/{ERC721BasicTokenMock.sol => ERC721BasicMock.sol} (70%) rename contracts/mocks/{ERC721TokenMock.sol => ERC721Mock.sol} (53%) create mode 100644 contracts/mocks/ERC721PausableMock.sol delete mode 100644 contracts/mocks/HasNoEtherTest.sol create mode 100644 contracts/mocks/InsecureInvariantTargetBounty.sol delete mode 100644 contracts/mocks/InsecureTargetBounty.sol delete mode 100644 contracts/mocks/LimitBalanceMock.sol create mode 100644 contracts/mocks/SecureInvariantTargetBounty.sol delete mode 100644 contracts/mocks/SecureTargetBounty.sol delete mode 100644 contracts/ownership/Contactable.sol delete mode 100644 contracts/ownership/HasNoContracts.sol delete mode 100644 contracts/ownership/HasNoEther.sol delete mode 100644 contracts/ownership/HasNoTokens.sol delete mode 100644 contracts/ownership/NoOwner.sol rename contracts/token/ERC20/{BurnableToken.sol => ERC20Burnable.sol} (76%) rename contracts/token/ERC20/{CappedToken.sol => ERC20Capped.sol} (78%) rename contracts/token/ERC20/{DetailedERC20.sol => ERC20Detailed.sol} (83%) rename contracts/token/ERC20/{MintableToken.sol => ERC20Mintable.sol} (78%) rename contracts/token/ERC20/{PausableToken.sol => ERC20Pausable.sol} (88%) create mode 100644 contracts/token/ERC20/IERC20.sol delete mode 100644 contracts/token/ERC20/StandardToken.sol delete mode 100644 contracts/token/ERC721/DeprecatedERC721.sol delete mode 100644 contracts/token/ERC721/ERC721BasicToken.sol create mode 100644 contracts/token/ERC721/ERC721Pausable.sol delete mode 100644 contracts/token/ERC721/ERC721Token.sol create mode 100644 contracts/token/ERC721/IERC721.sol create mode 100644 contracts/token/ERC721/IERC721Basic.sol rename contracts/token/ERC721/{ERC721Receiver.sol => IERC721Receiver.sol} (92%) rename contracts/{AddressUtils.sol => utils/Address.sol} (97%) rename contracts/{ => utils}/AutoIncrementing.sol (89%) rename contracts/{ => utils}/ReentrancyGuard.sol (100%) create mode 100755 scripts/ci/trigger_docs_update delete mode 100644 test/.eslintrc rename test/{Bounty.test.js => BreakInvariantBounty.test.js} (87%) delete mode 100644 test/LimitBalance.test.js rename test/{ownership => access}/Whitelist.test.js (62%) create mode 100644 test/access/rbac/PublicRole.behavior.js create mode 100644 test/introspection/ERC165Checker.test.js rename test/library/{ECRecovery.test.js => ECDSA.test.js} (50%) delete mode 100644 test/lifecycle/Destructible.test.js delete mode 100644 test/lifecycle/TokenDestructible.test.js delete mode 100644 test/ownership/Contactable.test.js delete mode 100644 test/ownership/HasNoContracts.test.js delete mode 100644 test/ownership/HasNoEther.test.js delete mode 100644 test/ownership/HasNoTokens.test.js delete mode 100644 test/token/ERC20/BurnableToken.behavior.js delete mode 100644 test/token/ERC20/BurnableToken.test.js delete mode 100644 test/token/ERC20/CappedToken.test.js rename test/token/ERC20/{StandardToken.test.js => ERC20.test.js} (90%) create mode 100644 test/token/ERC20/ERC20Burnable.behavior.js create mode 100644 test/token/ERC20/ERC20Burnable.test.js rename test/token/ERC20/{CappedToken.behavior.js => ERC20Capped.behavior.js} (91%) create mode 100644 test/token/ERC20/ERC20Capped.test.js create mode 100644 test/token/ERC20/ERC20Mintable.behavior.js create mode 100644 test/token/ERC20/ERC20Mintable.test.js rename test/token/ERC20/{PausableToken.test.js => ERC20Pausable.test.js} (91%) delete mode 100644 test/token/ERC20/MintableToken.behavior.js delete mode 100644 test/token/ERC20/MintableToken.test.js rename test/token/ERC721/{ERC721Token.test.js => ERC721.test.js} (91%) rename test/token/ERC721/{ERC721BasicToken.behavior.js => ERC721Basic.behavior.js} (90%) create mode 100644 test/token/ERC721/ERC721Basic.test.js delete mode 100644 test/token/ERC721/ERC721BasicToken.test.js create mode 100644 test/token/ERC721/ERC721Pausable.test.js create mode 100644 test/token/ERC721/ERC721PausedToken.behavior.js diff --git a/.travis.yml b/.travis.yml index 3ed321655a4..19994b1f6ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,14 @@ jobs: - stage: tests name: "static tests" script: npm run lint + - stage: update docs + if: tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ + addons: + apt: + packages: + - curl + script: + - ./scripts/ci/trigger_docs_update "${TRAVIS_TAG}" notifications: slack: diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 11614a246dc..46aaa2e6bd2 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -18,8 +18,59 @@ Any exception or additions specific to our project are documented below. * Parameters must be prefixed with an underscore. -``` -function test(uint256 _testParameter1, uint256 _testParameter2) { + ``` + function test(uint256 _testParameter1, uint256 _testParameter2) { ... -} -``` + } + ``` + + The exception are the parameters of events. There is no chance of ambiguity + with these, so they should not have underscores. Not even if they are + specified on an ERC with underscores; removing them doesn't change the ABI, + so we should be consistent with the rest of the events in this repository + and remove them. + +* Internal and private state variables should have an underscore suffix. + + ``` + contract TestContract { + uint256 internal internalVar_; + uint256 private privateVar_; + } + ``` + + Variables declared in a function should not follow this rule. + + ``` + function test() { + uint256 functionVar; + ... + } + ``` + +* Internal and private functions should have an underscore prefix. + + ``` + function _testInternal() internal { + ... + } + ``` + + ``` + function _testPrivate() private { + ... + } + ``` + +* Events should be emitted immediately after the state change that they + represent, and consequently they should be named in past tense. + + ``` + function _burn(address _who, uint256 _value) internal { + super._burn(_who, _value); + emit TokensBurned(_who, _value); + } + ``` + + Some standards (e.g. ERC20) use present tense, and in those cases the + standard specification prevails. diff --git a/README.md b/README.md index 5e38652ce9b..f866b881e0c 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ The following provides visibility into how OpenZeppelin's contracts are organize - **ERC20** - A standard interface for fungible tokens: - *Interfaces* - Includes the ERC-20 token standard basic interface. I.e., what the contract’s ABI can represent. - *Implementations* - Includes ERC-20 token implementations that include all required and some optional ERC-20 functionality. - - **ERC721** - A standard interface for non-fungible tokens + - **ERC721** - A standard interface for non-fungible tokens - *Interfaces* - Includes the ERC-721 token standard basic interface. I.e., what the contract’s ABI can represent. - *Implementations* - Includes ERC-721 token implementations that include all required and some optional ERC-721 functionality. @@ -125,6 +125,7 @@ Interested in contributing to OpenZeppelin? - Framework proposal and roadmap: https://medium.com/zeppelin-blog/zeppelin-framework-proposal-and-development-roadmap-fdfa9a3a32ab#.iain47pak - Issue tracker: https://github.com/OpenZeppelin/openzeppelin-solidity/issues - Contribution guidelines: https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/CONTRIBUTING.md +- Code-style guide: https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/CODE_STYLE.md - Wiki: https://github.com/OpenZeppelin/openzeppelin-solidity/wiki ## License diff --git a/RELEASING.md b/RELEASING.md index 5199fb97dc5..6514b4cf54c 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -7,7 +7,7 @@ We release a new version of OpenZeppelin monthly. Release cycles are tracked in Each release has at least one release candidate published first, intended for community review and any critical fixes that may come out of it. At the moment we leave 1 week between the first release candidate and the final release. Before starting make sure to verify the following items. -* Your local `master` branch is in sync with your upstream remote. +* Your local `master` branch is in sync with your `upstream` remote (it may have another name depending on your setup). * Your repo is clean, particularly with no untracked files in the contracts and tests directories. Verify with `git clean -n`. @@ -44,8 +44,11 @@ Publish the release notes on GitHub and ask our community manager to announce th ## Creating the final release +Make sure to have the latest changes from `upstream` in your local release branch. + ``` git checkout release-vX.Y.Z +git pull upstream ``` Change the version string in `package.json`, `package-lock.json` and `ethpm.json` removing the "-rc.R" suffix. Commit these changes and tag the commit as `vX.Y.Z`. @@ -75,7 +78,14 @@ npm dist-tag rm --otp $2FA_CODE openzeppelin-solidity next ## Merging the release branch -After the final release, the release branch should be merged back into `master`. This merge must not be squashed, because it would lose the tagged release commit, so it should be merged locally and pushed. +After the final release, the release branch should be merged back into `master`. This merge must not be squashed because it would lose the tagged release commit. Since the GitHub repo is set up to only allow squashed merges, the merge should be done locally and pushed. + +Make sure to have the latest changes from `upstream` in your local release branch. + +``` +git checkout release-vX.Y.Z +git pull upstream +``` ``` git checkout master diff --git a/contracts/LimitBalance.sol b/contracts/LimitBalance.sol deleted file mode 100644 index c3d495a3c72..00000000000 --- a/contracts/LimitBalance.sol +++ /dev/null @@ -1,31 +0,0 @@ -pragma solidity ^0.4.24; - - -/** - * @title LimitBalance - * @dev Simple contract to limit the balance of child contract. - * Note this doesn't prevent other contracts to send funds by using selfdestruct(address); - * See: https://github.com/ConsenSys/smart-contract-best-practices#remember-that-ether-can-be-forcibly-sent-to-an-account - */ -contract LimitBalance { - - uint256 public limit; - - /** - * @dev Constructor that sets the passed value as a limit. - * @param _limit uint256 to represent the limit. - */ - constructor(uint256 _limit) public { - limit = _limit; - } - - /** - * @dev Checks if limit was reached. Case true, it throws. - */ - modifier limitedPayable() { - require(address(this).balance <= limit); - _; - - } - -} diff --git a/contracts/access/SignatureBouncer.sol b/contracts/access/SignatureBouncer.sol index 644b3437419..0b91dcdb3fb 100644 --- a/contracts/access/SignatureBouncer.sol +++ b/contracts/access/SignatureBouncer.sol @@ -2,7 +2,7 @@ pragma solidity ^0.4.24; import "../ownership/Ownable.sol"; import "../access/rbac/RBAC.sol"; -import "../ECRecovery.sol"; +import "../cryptography/ECDSA.sol"; /** @@ -17,7 +17,7 @@ import "../ECRecovery.sol"; * valid addresses on-chain, simply sign a grant of the form * keccak256(abi.encodePacked(`:contractAddress` + `:granteeAddress`)) using a valid bouncer address. * Then restrict access to your crowdsale/whitelist/airdrop using the - * `onlyValidSignature` modifier (or implement your own using isValidSignature). + * `onlyValidSignature` modifier (or implement your own using _isValidSignature). * In addition to `onlyValidSignature`, `onlyValidSignatureAndMethod` and * `onlyValidSignatureAndData` can be used to restrict access to only a given method * or a given method with given parameters respectively. @@ -30,19 +30,22 @@ import "../ECRecovery.sol"; * much more complex. See https://ethereum.stackexchange.com/a/50616 for more details. */ contract SignatureBouncer is Ownable, RBAC { - using ECRecovery for bytes32; + using ECDSA for bytes32; - string public constant ROLE_BOUNCER = "bouncer"; - uint constant METHOD_ID_SIZE = 4; - // signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes - uint constant SIGNATURE_SIZE = 96; + // Name of the bouncer role. + string private constant ROLE_BOUNCER = "bouncer"; + // Function selectors are 4 bytes long, as documented in + // https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector + uint256 private constant METHOD_ID_SIZE = 4; + // Signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes + uint256 private constant SIGNATURE_SIZE = 96; /** * @dev requires that a valid signature of a bouncer was provided */ modifier onlyValidSignature(bytes _signature) { - require(isValidSignature(msg.sender, _signature)); + require(_isValidSignature(msg.sender, _signature)); _; } @@ -51,7 +54,7 @@ contract SignatureBouncer is Ownable, RBAC { */ modifier onlyValidSignatureAndMethod(bytes _signature) { - require(isValidSignatureAndMethod(msg.sender, _signature)); + require(_isValidSignatureAndMethod(msg.sender, _signature)); _; } @@ -60,10 +63,18 @@ contract SignatureBouncer is Ownable, RBAC { */ modifier onlyValidSignatureAndData(bytes _signature) { - require(isValidSignatureAndData(msg.sender, _signature)); + require(_isValidSignatureAndData(msg.sender, _signature)); _; } + /** + * @dev Determine if an account has the bouncer role. + * @return true if the account is a bouncer, false otherwise. + */ + function isBouncer(address _account) public view returns(bool) { + return hasRole(_account, ROLE_BOUNCER); + } + /** * @dev allows the owner to add additional bouncer addresses */ @@ -72,7 +83,7 @@ contract SignatureBouncer is Ownable, RBAC { onlyOwner { require(_bouncer != address(0)); - addRole(_bouncer, ROLE_BOUNCER); + _addRole(_bouncer, ROLE_BOUNCER); } /** @@ -82,19 +93,19 @@ contract SignatureBouncer is Ownable, RBAC { public onlyOwner { - removeRole(_bouncer, ROLE_BOUNCER); + _removeRole(_bouncer, ROLE_BOUNCER); } /** * @dev is the signature of `this + sender` from a bouncer? * @return bool */ - function isValidSignature(address _address, bytes _signature) + function _isValidSignature(address _address, bytes _signature) internal view returns (bool) { - return isValidDataHash( + return _isValidDataHash( keccak256(abi.encodePacked(address(this), _address)), _signature ); @@ -104,7 +115,7 @@ contract SignatureBouncer is Ownable, RBAC { * @dev is the signature of `this + sender + methodId` from a bouncer? * @return bool */ - function isValidSignatureAndMethod(address _address, bytes _signature) + function _isValidSignatureAndMethod(address _address, bytes _signature) internal view returns (bool) @@ -113,7 +124,7 @@ contract SignatureBouncer is Ownable, RBAC { for (uint i = 0; i < data.length; i++) { data[i] = msg.data[i]; } - return isValidDataHash( + return _isValidDataHash( keccak256(abi.encodePacked(address(this), _address, data)), _signature ); @@ -124,7 +135,7 @@ contract SignatureBouncer is Ownable, RBAC { * @notice the _signature parameter of the method being validated must be the "last" parameter * @return bool */ - function isValidSignatureAndData(address _address, bytes _signature) + function _isValidSignatureAndData(address _address, bytes _signature) internal view returns (bool) @@ -134,7 +145,7 @@ contract SignatureBouncer is Ownable, RBAC { for (uint i = 0; i < data.length; i++) { data[i] = msg.data[i]; } - return isValidDataHash( + return _isValidDataHash( keccak256(abi.encodePacked(address(this), _address, data)), _signature ); @@ -145,7 +156,7 @@ contract SignatureBouncer is Ownable, RBAC { * and then recover the signature and check it against the bouncer role * @return bool */ - function isValidDataHash(bytes32 _hash, bytes _signature) + function _isValidDataHash(bytes32 _hash, bytes _signature) internal view returns (bool) @@ -153,6 +164,6 @@ contract SignatureBouncer is Ownable, RBAC { address signer = _hash .toEthSignedMessageHash() .recover(_signature); - return hasRole(signer, ROLE_BOUNCER); + return isBouncer(signer); } } diff --git a/contracts/access/Whitelist.sol b/contracts/access/Whitelist.sol index 50414c6848c..3d9b3da5c6f 100644 --- a/contracts/access/Whitelist.sol +++ b/contracts/access/Whitelist.sol @@ -11,7 +11,9 @@ import "../access/rbac/RBAC.sol"; * This simplifies the implementation of "user permissions". */ contract Whitelist is Ownable, RBAC { - string public constant ROLE_WHITELISTED = "whitelist"; + + // Name of the whitelisted role. + string private constant ROLE_WHITELISTED = "whitelist"; /** * @dev Throws if operator is not whitelisted. @@ -31,13 +33,14 @@ contract Whitelist is Ownable, RBAC { public onlyOwner { - addRole(_operator, ROLE_WHITELISTED); + _addRole(_operator, ROLE_WHITELISTED); } /** - * @dev getter to determine if address is in whitelist + * @dev Determine if an account is whitelisted. + * @return true if the account is whitelisted, false otherwise. */ - function whitelist(address _operator) + function isWhitelisted(address _operator) public view returns (bool) @@ -70,7 +73,7 @@ contract Whitelist is Ownable, RBAC { public onlyOwner { - removeRole(_operator, ROLE_WHITELISTED); + _removeRole(_operator, ROLE_WHITELISTED); } /** diff --git a/contracts/access/rbac/MinterRole.sol b/contracts/access/rbac/MinterRole.sol new file mode 100644 index 00000000000..b01a41de6c5 --- /dev/null +++ b/contracts/access/rbac/MinterRole.sol @@ -0,0 +1,39 @@ +pragma solidity ^0.4.24; + +import "./Roles.sol"; + + +contract MinterRole { + using Roles for Roles.Role; + + Roles.Role private minters; + + constructor(address[] _minters) public { + minters.addMany(_minters); + } + + function transferMinter(address _account) public { + minters.transfer(_account); + } + + function renounceMinter() public { + minters.renounce(); + } + + function isMinter(address _account) public view returns (bool) { + return minters.has(_account); + } + + modifier onlyMinter() { + require(isMinter(msg.sender)); + _; + } + + function _addMinter(address _account) internal { + minters.add(_account); + } + + function _removeMinter(address _account) internal { + minters.remove(_account); + } +} diff --git a/contracts/access/rbac/RBAC.sol b/contracts/access/rbac/RBAC.sol index 7f8196c0683..70e2f9bca28 100644 --- a/contracts/access/rbac/RBAC.sol +++ b/contracts/access/rbac/RBAC.sol @@ -30,7 +30,7 @@ contract RBAC { public view { - roles[_role].check(_operator); + require(roles[_role].has(_operator)); } /** @@ -52,7 +52,7 @@ contract RBAC { * @param _operator address * @param _role the name of the role */ - function addRole(address _operator, string _role) + function _addRole(address _operator, string _role) internal { roles[_role].add(_operator); @@ -64,7 +64,7 @@ contract RBAC { * @param _operator address * @param _role the name of the role */ - function removeRole(address _operator, string _role) + function _removeRole(address _operator, string _role) internal { roles[_role].remove(_operator); diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/rbac/Roles.sol index f5c95b89baf..f93187e6bd3 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/rbac/Roles.sol @@ -48,14 +48,6 @@ library Roles { remove(_role, msg.sender); } - /** - * @dev check if an account has this role - * // reverts - */ - function check(Role storage _role, address _account) internal view { - require(has(_role, _account)); - } - /** * @dev check if an account has this role * @return bool diff --git a/contracts/Bounty.sol b/contracts/bounties/BreakInvariantBounty.sol similarity index 78% rename from contracts/Bounty.sol rename to contracts/bounties/BreakInvariantBounty.sol index e34d6d118d8..622e61ae5af 100644 --- a/contracts/Bounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -1,15 +1,14 @@ pragma solidity ^0.4.24; -import "./payment/PullPayment.sol"; -import "./lifecycle/Destructible.sol"; - +import "../payment/PullPayment.sol"; +import "../ownership/Ownable.sol"; /** - * @title Bounty + * @title BreakInvariantBounty * @dev This bounty will pay out to a researcher if they break invariant logic of the contract. */ -contract Bounty is PullPayment, Destructible { +contract BreakInvariantBounty is PullPayment, Ownable { bool public claimed; mapping(address => address) public researchers; @@ -28,7 +27,7 @@ contract Bounty is PullPayment, Destructible { * @return A target contract */ function createTarget() public returns(Target) { - Target target = Target(deployContract()); + Target target = Target(_deployContract()); researchers[target] = msg.sender; emit TargetCreated(target); return target; @@ -43,15 +42,22 @@ contract Bounty is PullPayment, Destructible { require(researcher != address(0)); // Check Target contract invariants require(!_target.checkInvariant()); - asyncTransfer(researcher, address(this).balance); + _asyncTransfer(researcher, address(this).balance); claimed = true; } + /** + * @dev Transfers the current balance to the owner and terminates the contract. + */ + function destroy() public onlyOwner { + selfdestruct(owner); + } + /** * @dev Internal function to deploy the target contract. * @return A target contract address */ - function deployContract() internal returns(address); + function _deployContract() internal returns(address); } diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index 5d89e85143a..8bf055ada87 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../math/SafeMath.sol"; import "../token/ERC20/SafeERC20.sol"; @@ -19,17 +19,17 @@ import "../token/ERC20/SafeERC20.sol"; */ contract Crowdsale { using SafeMath for uint256; - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; // The token being sold - ERC20 public token; + IERC20 public token; // Address where funds are collected address public wallet; // How many token units a buyer gets per wei. // The rate is the conversion between wei and the smallest and indivisible token unit. - // So, if you are using a rate of 1 with a DetailedERC20 token with 3 decimals called TOK + // So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK // 1 wei will give you 1 unit, or 0.001 TOK. uint256 public rate; @@ -43,7 +43,7 @@ contract Crowdsale { * @param value weis paid for purchase * @param amount amount of tokens purchased */ - event TokenPurchase( + event TokensPurchased( address indexed purchaser, address indexed beneficiary, uint256 value, @@ -55,7 +55,7 @@ contract Crowdsale { * @param _wallet Address where collected funds will be forwarded to * @param _token Address of the token being sold */ - constructor(uint256 _rate, address _wallet, ERC20 _token) public { + constructor(uint256 _rate, address _wallet, IERC20 _token) public { require(_rate > 0); require(_wallet != address(0)); require(_token != address(0)); @@ -92,7 +92,7 @@ contract Crowdsale { weiRaised = weiRaised.add(weiAmount); _processPurchase(_beneficiary, tokens); - emit TokenPurchase( + emit TokensPurchased( msg.sender, _beneficiary, weiAmount, @@ -111,7 +111,7 @@ contract Crowdsale { /** * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use `super` in contracts that inherit from Crowdsale to extend their validations. - * Example from CappedCrowdsale.sol's _preValidatePurchase method: + * Example from CappedCrowdsale.sol's _preValidatePurchase method: * super._preValidatePurchase(_beneficiary, _weiAmount); * require(weiRaised.add(_weiAmount) <= cap); * @param _beneficiary Address performing the token purchase diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index b02b1da74fe..651069d41af 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -15,7 +15,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { bool public isFinalized = false; - event Finalized(); + event CrowdsaleFinalized(); /** * @dev Must be called after crowdsale ends, to do some extra finalization @@ -25,18 +25,18 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { require(!isFinalized); require(hasClosed()); - finalization(); - emit Finalized(); + _finalization(); + emit CrowdsaleFinalized(); isFinalized = true; } /** * @dev Can be overridden to add finalization logic. The overriding function - * should call super.finalization() to ensure the chain of finalization is + * should call super._finalization() to ensure the chain of finalization is * executed entirely. */ - function finalization() internal { + function _finalization() internal { } } diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index 652f21e5d74..b09d09709a5 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; import "../validation/TimedCrowdsale.sol"; -import "../../token/ERC20/ERC20.sol"; +import "../../token/ERC20/IERC20.sol"; import "../../math/SafeMath.sol"; diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 0342173dce0..05f95ca8584 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -18,7 +18,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { uint256 public goal; // refund escrow used to hold funds while crowdsale is running - RefundEscrow private escrow; + RefundEscrow private escrow_; /** * @dev Constructor, creates RefundEscrow. @@ -26,7 +26,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ constructor(uint256 _goal) public { require(_goal > 0); - escrow = new RefundEscrow(wallet); + escrow_ = new RefundEscrow(wallet); goal = _goal; } @@ -37,7 +37,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { require(isFinalized); require(!goalReached()); - escrow.withdraw(msg.sender); + escrow_.withdraw(msg.sender); } /** @@ -51,22 +51,22 @@ contract RefundableCrowdsale is FinalizableCrowdsale { /** * @dev escrow finalization task, called when owner calls finalize() */ - function finalization() internal { + function _finalization() internal { if (goalReached()) { - escrow.close(); - escrow.beneficiaryWithdraw(); + escrow_.close(); + escrow_.beneficiaryWithdraw(); } else { - escrow.enableRefunds(); + escrow_.enableRefunds(); } - super.finalization(); + super._finalization(); } /** * @dev Overrides Crowdsale fund forwarding, sending funds to escrow. */ function _forwardFunds() internal { - escrow.deposit.value(msg.value)(msg.sender); + escrow_.deposit.value(msg.value)(msg.sender); } } diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index 4b0665cba20..4e48638ab74 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; import "../Crowdsale.sol"; -import "../../token/ERC20/ERC20.sol"; +import "../../token/ERC20/IERC20.sol"; import "../../token/ERC20/SafeERC20.sol"; import "../../math/SafeMath.sol"; @@ -12,7 +12,7 @@ import "../../math/SafeMath.sol"; */ contract AllowanceCrowdsale is Crowdsale { using SafeMath for uint256; - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; address public tokenWallet; diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 0db3a7436cd..9f3a8b4b115 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; import "../Crowdsale.sol"; -import "../../token/ERC20/MintableToken.sol"; +import "../../token/ERC20/ERC20Mintable.sol"; /** @@ -23,6 +23,6 @@ contract MintedCrowdsale is Crowdsale { internal { // Potentially dangerous assumption about the type of the token. - require(MintableToken(address(token)).mint(_beneficiary, _tokenAmount)); + require(ERC20Mintable(address(token)).mint(_beneficiary, _tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index dd5fa0942d2..841f09bc36b 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -22,8 +22,8 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { * @param _finalRate Number of tokens a buyer gets per wei at the end of the crowdsale */ constructor(uint256 _initialRate, uint256 _finalRate) public { - require(_initialRate >= _finalRate); require(_finalRate > 0); + require(_initialRate >= _finalRate); initialRate = _initialRate; finalRate = _finalRate; } diff --git a/contracts/ECRecovery.sol b/contracts/cryptography/ECDSA.sol similarity index 98% rename from contracts/ECRecovery.sol rename to contracts/cryptography/ECDSA.sol index 66c49450746..fc14498bc4c 100644 --- a/contracts/ECRecovery.sol +++ b/contracts/cryptography/ECDSA.sol @@ -8,7 +8,7 @@ pragma solidity ^0.4.24; * See https://github.com/ethereum/solidity/issues/864 */ -library ECRecovery { +library ECDSA { /** * @dev Recover signer address from a message by using their signature diff --git a/contracts/MerkleProof.sol b/contracts/cryptography/MerkleProof.sol similarity index 100% rename from contracts/MerkleProof.sol rename to contracts/cryptography/MerkleProof.sol diff --git a/contracts/examples/RBACWithAdmin.sol b/contracts/examples/RBACWithAdmin.sol index 479bb95ac4f..83c2dcef7a8 100644 --- a/contracts/examples/RBACWithAdmin.sol +++ b/contracts/examples/RBACWithAdmin.sol @@ -19,7 +19,7 @@ contract RBACWithAdmin is RBAC { /** * A constant role name for indicating admins. */ - string public constant ROLE_ADMIN = "admin"; + string private constant ROLE_ADMIN = "admin"; /** * @dev modifier to scope access to admins @@ -37,7 +37,14 @@ contract RBACWithAdmin is RBAC { constructor() public { - addRole(msg.sender, ROLE_ADMIN); + _addRole(msg.sender, ROLE_ADMIN); + } + + /** + * @return true if the account is admin, false otherwise. + */ + function isAdmin(address _account) public view returns(bool) { + return hasRole(_account, ROLE_ADMIN); } /** @@ -49,7 +56,7 @@ contract RBACWithAdmin is RBAC { public onlyAdmin { - addRole(_account, _roleName); + _addRole(_account, _roleName); } /** @@ -61,6 +68,6 @@ contract RBACWithAdmin is RBAC { public onlyAdmin { - removeRole(_account, _roleName); + _removeRole(_account, _roleName); } } diff --git a/contracts/examples/SampleCrowdsale.sol b/contracts/examples/SampleCrowdsale.sol index 0a3f1042edf..61b82dc27f2 100644 --- a/contracts/examples/SampleCrowdsale.sol +++ b/contracts/examples/SampleCrowdsale.sol @@ -3,7 +3,7 @@ pragma solidity ^0.4.24; import "../crowdsale/validation/CappedCrowdsale.sol"; import "../crowdsale/distribution/RefundableCrowdsale.sol"; import "../crowdsale/emission/MintedCrowdsale.sol"; -import "../token/ERC20/MintableToken.sol"; +import "../token/ERC20/ERC20Mintable.sol"; /** @@ -11,12 +11,17 @@ import "../token/ERC20/MintableToken.sol"; * @dev Very simple ERC20 Token that can be minted. * It is meant to be used in a crowdsale contract. */ -contract SampleCrowdsaleToken is MintableToken { +contract SampleCrowdsaleToken is ERC20Mintable { string public constant name = "Sample Crowdsale Token"; string public constant symbol = "SCT"; uint8 public constant decimals = 18; + constructor(address[] _minters) + ERC20Mintable(_minters) + public + { + } } @@ -44,7 +49,7 @@ contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale, MintedCrowdsal uint256 _rate, address _wallet, uint256 _cap, - MintableToken _token, + ERC20Mintable _token, uint256 _goal ) public diff --git a/contracts/examples/SimpleToken.sol b/contracts/examples/SimpleToken.sol index 73df121dd8f..67de00ca420 100644 --- a/contracts/examples/SimpleToken.sol +++ b/contracts/examples/SimpleToken.sol @@ -1,16 +1,16 @@ pragma solidity ^0.4.24; -import "../token/ERC20/StandardToken.sol"; +import "../token/ERC20/ERC20.sol"; /** * @title SimpleToken * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator. * Note they can later distribute these tokens as they wish using `transfer` and other - * `StandardToken` functions. + * `ERC20` functions. */ -contract SimpleToken is StandardToken { +contract SimpleToken is ERC20 { string public constant name = "SimpleToken"; string public constant symbol = "SIM"; diff --git a/contracts/introspection/ERC165Checker.sol b/contracts/introspection/ERC165Checker.sol new file mode 100644 index 00000000000..9bb88130936 --- /dev/null +++ b/contracts/introspection/ERC165Checker.sol @@ -0,0 +1,149 @@ +pragma solidity ^0.4.24; + + +/** + * @title ERC165Checker + * @dev Use `using ERC165Checker for address`; to include this library + * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md + */ +library ERC165Checker { + // As per the EIP-165 spec, no interface should ever match 0xffffffff + bytes4 private constant InterfaceId_Invalid = 0xffffffff; + + bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7; + /** + * 0x01ffc9a7 === + * bytes4(keccak256('supportsInterface(bytes4)')) + */ + + + /** + * @notice Query if a contract supports ERC165 + * @param _address The address of the contract to query for support of ERC165 + * @return true if the contract at _address implements ERC165 + */ + function supportsERC165(address _address) + internal + view + returns (bool) + { + // Any contract that implements ERC165 must explicitly indicate support of + // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid + return supportsERC165Interface(_address, InterfaceId_ERC165) && + !supportsERC165Interface(_address, InterfaceId_Invalid); + } + + /** + * @notice Query if a contract implements an interface, also checks support of ERC165 + * @param _address The address of the contract to query for support of an interface + * @param _interfaceId The interface identifier, as specified in ERC-165 + * @return true if the contract at _address indicates support of the interface with + * identifier _interfaceId, false otherwise + * @dev Interface identification is specified in ERC-165. + */ + function supportsInterface(address _address, bytes4 _interfaceId) + internal + view + returns (bool) + { + // query support of both ERC165 as per the spec and support of _interfaceId + return supportsERC165(_address) && + supportsERC165Interface(_address, _interfaceId); + } + + /** + * @notice Query if a contract implements interfaces, also checks support of ERC165 + * @param _address The address of the contract to query for support of an interface + * @param _interfaceIds A list of interface identifiers, as specified in ERC-165 + * @return true if the contract at _address indicates support all interfaces in the + * _interfaceIds list, false otherwise + * @dev Interface identification is specified in ERC-165. + */ + function supportsInterfaces(address _address, bytes4[] _interfaceIds) + internal + view + returns (bool) + { + // query support of ERC165 itself + if (!supportsERC165(_address)) { + return false; + } + + // query support of each interface in _interfaceIds + for (uint256 i = 0; i < _interfaceIds.length; i++) { + if (!supportsERC165Interface(_address, _interfaceIds[i])) { + return false; + } + } + + // all interfaces supported + return true; + } + + /** + * @notice Query if a contract implements an interface, does not check ERC165 support + * @param _address The address of the contract to query for support of an interface + * @param _interfaceId The interface identifier, as specified in ERC-165 + * @return true if the contract at _address indicates support of the interface with + * identifier _interfaceId, false otherwise + * @dev Assumes that _address contains a contract that supports ERC165, otherwise + * the behavior of this method is undefined. This precondition can be checked + * with the `supportsERC165` method in this library. + * Interface identification is specified in ERC-165. + */ + function supportsERC165Interface(address _address, bytes4 _interfaceId) + private + view + returns (bool) + { + // success determines whether the staticcall succeeded and result determines + // whether the contract at _address indicates support of _interfaceId + (bool success, bool result) = callERC165SupportsInterface( + _address, _interfaceId); + + return (success && result); + } + + /** + * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw + * @param _address The address of the contract to query for support of an interface + * @param _interfaceId The interface identifier, as specified in ERC-165 + * @return success true if the STATICCALL succeeded, false otherwise + * @return result true if the STATICCALL succeeded and the contract at _address + * indicates support of the interface with identifier _interfaceId, false otherwise + */ + function callERC165SupportsInterface( + address _address, + bytes4 _interfaceId + ) + private + view + returns (bool success, bool result) + { + bytes memory encodedParams = abi.encodeWithSelector( + InterfaceId_ERC165, + _interfaceId + ); + + // solium-disable-next-line security/no-inline-assembly + assembly { + let encodedParams_data := add(0x20, encodedParams) + let encodedParams_size := mload(encodedParams) + + let output := mload(0x40) // Find empty storage location using "free memory pointer" + mstore(output, 0x0) + + success := staticcall( + 30000, // 30k gas + _address, // To addr + encodedParams_data, + encodedParams_size, + output, + 0x20 // Outputs are 32 bytes long + ) + + result := mload(output) // Load the result + } + } +} + diff --git a/contracts/introspection/ERC165.sol b/contracts/introspection/IERC165.sol similarity index 92% rename from contracts/introspection/ERC165.sol rename to contracts/introspection/IERC165.sol index 06f20a074b4..f3361f0a46d 100644 --- a/contracts/introspection/ERC165.sol +++ b/contracts/introspection/IERC165.sol @@ -2,10 +2,10 @@ pragma solidity ^0.4.24; /** - * @title ERC165 + * @title IERC165 * @dev https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md */ -interface ERC165 { +interface IERC165 { /** * @notice Query if a contract implements an interface diff --git a/contracts/introspection/SupportsInterfaceWithLookup.sol b/contracts/introspection/SupportsInterfaceWithLookup.sol index 6e6d2d5dde3..c2b009aea0f 100644 --- a/contracts/introspection/SupportsInterfaceWithLookup.sol +++ b/contracts/introspection/SupportsInterfaceWithLookup.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "./ERC165.sol"; +import "./IERC165.sol"; /** @@ -8,7 +8,7 @@ import "./ERC165.sol"; * @author Matt Condon (@shrugs) * @dev Implements ERC165 using a lookup table. */ -contract SupportsInterfaceWithLookup is ERC165 { +contract SupportsInterfaceWithLookup is IERC165 { bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7; /** @@ -19,7 +19,7 @@ contract SupportsInterfaceWithLookup is ERC165 { /** * @dev a mapping of interface id to whether or not it's supported */ - mapping(bytes4 => bool) internal supportedInterfaces; + mapping(bytes4 => bool) internal supportedInterfaces_; /** * @dev A contract implementing SupportsInterfaceWithLookup @@ -39,7 +39,7 @@ contract SupportsInterfaceWithLookup is ERC165 { view returns (bool) { - return supportedInterfaces[_interfaceId]; + return supportedInterfaces_[_interfaceId]; } /** @@ -49,6 +49,6 @@ contract SupportsInterfaceWithLookup is ERC165 { internal { require(_interfaceId != 0xffffffff); - supportedInterfaces[_interfaceId] = true; + supportedInterfaces_[_interfaceId] = true; } } diff --git a/contracts/lifecycle/Destructible.sol b/contracts/lifecycle/Destructible.sol deleted file mode 100644 index c10630d88dc..00000000000 --- a/contracts/lifecycle/Destructible.sol +++ /dev/null @@ -1,22 +0,0 @@ -pragma solidity ^0.4.24; - - -import "../ownership/Ownable.sol"; - - -/** - * @title Destructible - * @dev Base contract that can be destroyed by owner. All funds in contract will be sent to the owner. - */ -contract Destructible is Ownable { - /** - * @dev Transfers the current balance to the owner and terminates the contract. - */ - function destroy() public onlyOwner { - selfdestruct(owner); - } - - function destroyAndSend(address _recipient) public onlyOwner { - selfdestruct(_recipient); - } -} diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index d39bbff080f..0a2767aa61f 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -9,8 +9,8 @@ import "../ownership/Ownable.sol"; * @dev Base contract which allows children to implement an emergency stop mechanism. */ contract Pausable is Ownable { - event Pause(); - event Unpause(); + event Paused(); + event Unpaused(); bool public paused = false; @@ -36,7 +36,7 @@ contract Pausable is Ownable { */ function pause() public onlyOwner whenNotPaused { paused = true; - emit Pause(); + emit Paused(); } /** @@ -44,6 +44,6 @@ contract Pausable is Ownable { */ function unpause() public onlyOwner whenPaused { paused = false; - emit Unpause(); + emit Unpaused(); } } diff --git a/contracts/lifecycle/TokenDestructible.sol b/contracts/lifecycle/TokenDestructible.sol deleted file mode 100644 index 38aabcbb18a..00000000000 --- a/contracts/lifecycle/TokenDestructible.sol +++ /dev/null @@ -1,36 +0,0 @@ -pragma solidity ^0.4.24; - -import "../ownership/Ownable.sol"; -import "../token/ERC20/ERC20.sol"; - - -/** - * @title TokenDestructible: - * @author Remco Bloemen - * @dev Base contract that can be destroyed by owner. All funds in contract including - * listed tokens will be sent to the owner. - */ -contract TokenDestructible is Ownable { - - constructor() public payable { } - - /** - * @notice Terminate contract and refund to owner - * @param _tokens List of addresses of ERC20 token contracts to - refund. - * @notice The called token contracts could try to re-enter this contract. Only - supply token contracts you trust. - */ - function destroy(address[] _tokens) public onlyOwner { - - // Transfer tokens to owner - for (uint256 i = 0; i < _tokens.length; i++) { - ERC20 token = ERC20(_tokens[i]); - uint256 balance = token.balanceOf(this); - token.transfer(owner, balance); - } - - // Transfer Eth to owner and terminate contract - selfdestruct(owner); - } -} diff --git a/contracts/mocks/AllowanceCrowdsaleImpl.sol b/contracts/mocks/AllowanceCrowdsaleImpl.sol index 5c179d2deb6..872dd66e193 100644 --- a/contracts/mocks/AllowanceCrowdsaleImpl.sol +++ b/contracts/mocks/AllowanceCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/emission/AllowanceCrowdsale.sol"; @@ -9,7 +9,7 @@ contract AllowanceCrowdsaleImpl is AllowanceCrowdsale { constructor ( uint256 _rate, address _wallet, - ERC20 _token, + IERC20 _token, address _tokenWallet ) public diff --git a/contracts/mocks/AutoIncrementingImpl.sol b/contracts/mocks/AutoIncrementingImpl.sol index 9aae333f0c5..86c498f6954 100644 --- a/contracts/mocks/AutoIncrementingImpl.sol +++ b/contracts/mocks/AutoIncrementingImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../AutoIncrementing.sol"; +import "../utils/AutoIncrementing.sol"; contract AutoIncrementingImpl { diff --git a/contracts/mocks/BouncerMock.sol b/contracts/mocks/BouncerMock.sol index e5bd31a1049..7f8f02d0538 100644 --- a/contracts/mocks/BouncerMock.sol +++ b/contracts/mocks/BouncerMock.sol @@ -9,7 +9,7 @@ contract SignatureBouncerMock is SignatureBouncer { view returns (bool) { - return isValidSignature(_address, _signature); + return _isValidSignature(_address, _signature); } function onlyWithValidSignature(bytes _signature) @@ -25,7 +25,7 @@ contract SignatureBouncerMock is SignatureBouncer { view returns (bool) { - return isValidSignatureAndMethod(_address, _signature); + return _isValidSignatureAndMethod(_address, _signature); } function onlyWithValidSignatureAndMethod(bytes _signature) @@ -46,7 +46,7 @@ contract SignatureBouncerMock is SignatureBouncer { view returns (bool) { - return isValidSignatureAndData(_address, _signature); + return _isValidSignatureAndData(_address, _signature); } function onlyWithValidSignatureAndData(uint, bytes _signature) diff --git a/contracts/mocks/CappedCrowdsaleImpl.sol b/contracts/mocks/CappedCrowdsaleImpl.sol index a43c8b4176c..a05fbd7d25f 100644 --- a/contracts/mocks/CappedCrowdsaleImpl.sol +++ b/contracts/mocks/CappedCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/validation/CappedCrowdsale.sol"; @@ -9,7 +9,7 @@ contract CappedCrowdsaleImpl is CappedCrowdsale { constructor ( uint256 _rate, address _wallet, - ERC20 _token, + IERC20 _token, uint256 _cap ) public diff --git a/contracts/mocks/DestructibleMock.sol b/contracts/mocks/DestructibleMock.sol deleted file mode 100644 index 5fb1bbbc662..00000000000 --- a/contracts/mocks/DestructibleMock.sol +++ /dev/null @@ -1,8 +0,0 @@ -pragma solidity ^0.4.24; - -import "../lifecycle/Destructible.sol"; - - -contract DestructibleMock is Destructible { - function() public payable {} -} diff --git a/contracts/mocks/DetailedERC20Mock.sol b/contracts/mocks/DetailedERC20Mock.sol index a4f17529aab..e9aab88e95d 100644 --- a/contracts/mocks/DetailedERC20Mock.sol +++ b/contracts/mocks/DetailedERC20Mock.sol @@ -1,16 +1,16 @@ pragma solidity ^0.4.24; -import "../token/ERC20/StandardToken.sol"; -import "../token/ERC20/DetailedERC20.sol"; +import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/ERC20Detailed.sol"; -contract DetailedERC20Mock is StandardToken, DetailedERC20 { +contract ERC20DetailedMock is ERC20, ERC20Detailed { constructor( string _name, string _symbol, uint8 _decimals ) - DetailedERC20(_name, _symbol, _decimals) + ERC20Detailed(_name, _symbol, _decimals) public {} } diff --git a/contracts/mocks/ECRecoveryMock.sol b/contracts/mocks/ECDSAMock.sol similarity index 78% rename from contracts/mocks/ECRecoveryMock.sol rename to contracts/mocks/ECDSAMock.sol index a2c5ebcd868..42570569e0e 100644 --- a/contracts/mocks/ECRecoveryMock.sol +++ b/contracts/mocks/ECDSAMock.sol @@ -1,11 +1,11 @@ pragma solidity ^0.4.24; -import "../ECRecovery.sol"; +import "../cryptography/ECDSA.sol"; -contract ECRecoveryMock { - using ECRecovery for bytes32; +contract ECDSAMock { + using ECDSA for bytes32; function recover(bytes32 _hash, bytes _signature) public diff --git a/contracts/mocks/ERC165/ERC165InterfacesSupported.sol b/contracts/mocks/ERC165/ERC165InterfacesSupported.sol new file mode 100644 index 00000000000..9f472b64825 --- /dev/null +++ b/contracts/mocks/ERC165/ERC165InterfacesSupported.sol @@ -0,0 +1,69 @@ +pragma solidity ^0.4.24; + +import "../../introspection/IERC165.sol"; + + +/** + * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-214.md#specification + * > Any attempts to make state-changing operations inside an execution instance with STATIC set to true will instead throw an exception. + * > These operations include [...], LOG0, LOG1, LOG2, [...] + * + * therefore, because this contract is staticcall'd we need to not emit events (which is how solidity-coverage works) + * solidity-coverage ignores the /mocks folder, so we duplicate its implementation here to avoid instrumenting it + */ +contract SupportsInterfaceWithLookupMock is IERC165 { + + bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7; + /** + * 0x01ffc9a7 === + * bytes4(keccak256('supportsInterface(bytes4)')) + */ + + /** + * @dev a mapping of interface id to whether or not it's supported + */ + mapping(bytes4 => bool) internal supportedInterfaces; + + /** + * @dev A contract implementing SupportsInterfaceWithLookup + * implement ERC165 itself + */ + constructor() + public + { + _registerInterface(InterfaceId_ERC165); + } + + /** + * @dev implement supportsInterface(bytes4) using a lookup table + */ + function supportsInterface(bytes4 _interfaceId) + external + view + returns (bool) + { + return supportedInterfaces[_interfaceId]; + } + + /** + * @dev private method for registering an interface + */ + function _registerInterface(bytes4 _interfaceId) + internal + { + require(_interfaceId != 0xffffffff); + supportedInterfaces[_interfaceId] = true; + } +} + + + +contract ERC165InterfacesSupported is SupportsInterfaceWithLookupMock { + constructor (bytes4[] _interfaceIds) + public + { + for (uint256 i = 0; i < _interfaceIds.length; i++) { + _registerInterface(_interfaceIds[i]); + } + } +} diff --git a/contracts/mocks/ERC165/ERC165NotSupported.sol b/contracts/mocks/ERC165/ERC165NotSupported.sol new file mode 100644 index 00000000000..763f8fabadf --- /dev/null +++ b/contracts/mocks/ERC165/ERC165NotSupported.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.4.24; + + +contract ERC165NotSupported { + +} diff --git a/contracts/mocks/ERC165CheckerMock.sol b/contracts/mocks/ERC165CheckerMock.sol new file mode 100644 index 00000000000..13ab1ccd60a --- /dev/null +++ b/contracts/mocks/ERC165CheckerMock.sol @@ -0,0 +1,32 @@ +pragma solidity ^0.4.24; + +import "../introspection/ERC165Checker.sol"; + + +contract ERC165CheckerMock { + using ERC165Checker for address; + + function supportsERC165(address _address) + public + view + returns (bool) + { + return _address.supportsERC165(); + } + + function supportsInterface(address _address, bytes4 _interfaceId) + public + view + returns (bool) + { + return _address.supportsInterface(_interfaceId); + } + + function supportsInterfaces(address _address, bytes4[] _interfaceIds) + public + view + returns (bool) + { + return _address.supportsInterfaces(_interfaceIds); + } +} diff --git a/contracts/mocks/BurnableTokenMock.sol b/contracts/mocks/ERC20BurnableMock.sol similarity index 63% rename from contracts/mocks/BurnableTokenMock.sol rename to contracts/mocks/ERC20BurnableMock.sol index a148f17f47e..79819b748c7 100644 --- a/contracts/mocks/BurnableTokenMock.sol +++ b/contracts/mocks/ERC20BurnableMock.sol @@ -1,9 +1,9 @@ pragma solidity ^0.4.24; -import "../token/ERC20/BurnableToken.sol"; +import "../token/ERC20/ERC20Burnable.sol"; -contract BurnableTokenMock is BurnableToken { +contract ERC20BurnableMock is ERC20Burnable { constructor(address _initialAccount, uint256 _initialBalance) public { _mint(_initialAccount, _initialBalance); diff --git a/contracts/mocks/ERC20MintableMock.sol b/contracts/mocks/ERC20MintableMock.sol new file mode 100644 index 00000000000..6ec365bc660 --- /dev/null +++ b/contracts/mocks/ERC20MintableMock.sol @@ -0,0 +1,21 @@ +pragma solidity ^0.4.24; + +import "../token/ERC20/ERC20Mintable.sol"; + + +// Mock contract exposing internal methods +contract ERC20MintableMock is ERC20Mintable { + constructor(address[] minters) ERC20Mintable(minters) public { + } + + function addMinter(address _account) public { + _addMinter(_account); + } + + function removeMinter(address _account) public { + _removeMinter(_account); + } + + function onlyMinterMock() public view onlyMinter { + } +} diff --git a/contracts/mocks/StandardTokenMock.sol b/contracts/mocks/ERC20Mock.sol similarity index 78% rename from contracts/mocks/StandardTokenMock.sol rename to contracts/mocks/ERC20Mock.sol index e4420c470bc..600169743b3 100644 --- a/contracts/mocks/StandardTokenMock.sol +++ b/contracts/mocks/ERC20Mock.sol @@ -1,10 +1,10 @@ pragma solidity ^0.4.24; -import "../token/ERC20/StandardToken.sol"; +import "../token/ERC20/ERC20.sol"; -// mock class using StandardToken -contract StandardTokenMock is StandardToken { +// mock class using ERC20 +contract ERC20Mock is ERC20 { constructor(address _initialAccount, uint256 _initialBalance) public { _mint(_initialAccount, _initialBalance); diff --git a/contracts/mocks/PausableTokenMock.sol b/contracts/mocks/ERC20PausableMock.sol similarity index 55% rename from contracts/mocks/PausableTokenMock.sol rename to contracts/mocks/ERC20PausableMock.sol index 24ef281bade..a60f8a12da6 100644 --- a/contracts/mocks/PausableTokenMock.sol +++ b/contracts/mocks/ERC20PausableMock.sol @@ -1,10 +1,10 @@ pragma solidity ^0.4.24; -import "../token/ERC20/PausableToken.sol"; +import "../token/ERC20/ERC20Pausable.sol"; -// mock class using PausableToken -contract PausableTokenMock is PausableToken { +// mock class using ERC20Pausable +contract ERC20PausableMock is ERC20Pausable { constructor(address _initialAccount, uint _initialBalance) public { _mint(_initialAccount, _initialBalance); diff --git a/contracts/mocks/ERC20WithMetadataMock.sol b/contracts/mocks/ERC20WithMetadataMock.sol index 6e102bbacac..025f154ab64 100644 --- a/contracts/mocks/ERC20WithMetadataMock.sol +++ b/contracts/mocks/ERC20WithMetadataMock.sol @@ -1,10 +1,10 @@ pragma solidity ^0.4.21; -import "../token/ERC20/StandardToken.sol"; +import "../token/ERC20/ERC20.sol"; import "../proposals/ERC1046/TokenMetadata.sol"; -contract ERC20WithMetadataMock is StandardToken, ERC20WithMetadata { +contract ERC20WithMetadataMock is ERC20, ERC20WithMetadata { constructor(string _tokenURI) public ERC20WithMetadata(_tokenURI) { diff --git a/contracts/mocks/ERC223TokenMock.sol b/contracts/mocks/ERC223TokenMock.sol deleted file mode 100644 index 2f92bc5f1f4..00000000000 --- a/contracts/mocks/ERC223TokenMock.sol +++ /dev/null @@ -1,33 +0,0 @@ -pragma solidity ^0.4.24; - -import "../token/ERC20/StandardToken.sol"; - - -contract ERC223ContractInterface { - function tokenFallback(address _from, uint256 _value, bytes _data) external; -} - - -contract ERC223TokenMock is StandardToken { - - constructor(address _initialAccount, uint256 _initialBalance) public { - _mint(_initialAccount, _initialBalance); - } - - // ERC223 compatible transfer function (except the name) - function transferERC223(address _to, uint256 _value, bytes _data) public - returns (bool success) - { - transfer(_to, _value); - bool isContract = false; - // solium-disable-next-line security/no-inline-assembly - assembly { - isContract := not(iszero(extcodesize(_to))) - } - if (isContract) { - ERC223ContractInterface receiver = ERC223ContractInterface(_to); - receiver.tokenFallback(msg.sender, _value, _data); - } - return true; - } -} diff --git a/contracts/mocks/ERC721BasicTokenMock.sol b/contracts/mocks/ERC721BasicMock.sol similarity index 70% rename from contracts/mocks/ERC721BasicTokenMock.sol rename to contracts/mocks/ERC721BasicMock.sol index 704728198d6..87add3adb7e 100644 --- a/contracts/mocks/ERC721BasicTokenMock.sol +++ b/contracts/mocks/ERC721BasicMock.sol @@ -1,13 +1,13 @@ pragma solidity ^0.4.24; -import "../token/ERC721/ERC721BasicToken.sol"; +import "../token/ERC721/ERC721Basic.sol"; /** - * @title ERC721BasicTokenMock + * @title ERC721BasicMock * This mock just provides a public mint and burn functions for testing purposes */ -contract ERC721BasicTokenMock is ERC721BasicToken { +contract ERC721BasicMock is ERC721Basic { function mint(address _to, uint256 _tokenId) public { super._mint(_to, _tokenId); } diff --git a/contracts/mocks/ERC721TokenMock.sol b/contracts/mocks/ERC721Mock.sol similarity index 53% rename from contracts/mocks/ERC721TokenMock.sol rename to contracts/mocks/ERC721Mock.sol index a16f16f3b96..b6c138fd2cd 100644 --- a/contracts/mocks/ERC721TokenMock.sol +++ b/contracts/mocks/ERC721Mock.sol @@ -1,35 +1,35 @@ pragma solidity ^0.4.24; -import "../token/ERC721/ERC721Token.sol"; +import "../token/ERC721/ERC721.sol"; /** - * @title ERC721TokenMock + * @title ERC721Mock * This mock just provides a public mint and burn functions for testing purposes, * and a public setter for metadata URI */ -contract ERC721TokenMock is ERC721Token { +contract ERC721Mock is ERC721 { constructor(string name, string symbol) public - ERC721Token(name, symbol) + ERC721(name, symbol) { } function mint(address _to, uint256 _tokenId) public { - super._mint(_to, _tokenId); + _mint(_to, _tokenId); } function burn(uint256 _tokenId) public { - super._burn(ownerOf(_tokenId), _tokenId); + _burn(ownerOf(_tokenId), _tokenId); } function exists(uint256 _tokenId) public view returns (bool) { - return super._exists(_tokenId); + return _exists(_tokenId); } function setTokenURI(uint256 _tokenId, string _uri) public { - super._setTokenURI(_tokenId, _uri); + _setTokenURI(_tokenId, _uri); } - function _removeTokenFrom(address _from, uint256 _tokenId) public { - super.removeTokenFrom(_from, _tokenId); + function removeTokenFrom(address _from, uint256 _tokenId) public { + _removeTokenFrom(_from, _tokenId); } } diff --git a/contracts/mocks/ERC721PausableMock.sol b/contracts/mocks/ERC721PausableMock.sol new file mode 100644 index 00000000000..ef2722f4ace --- /dev/null +++ b/contracts/mocks/ERC721PausableMock.sol @@ -0,0 +1,22 @@ +pragma solidity ^0.4.24; + +import "../token/ERC721/ERC721Pausable.sol"; + + +/** + * @title ERC721PausableMock + * This mock just provides a public mint, burn and exists functions for testing purposes + */ +contract ERC721PausableMock is ERC721Pausable { + function mint(address _to, uint256 _tokenId) public { + super._mint(_to, _tokenId); + } + + function burn(uint256 _tokenId) public { + super._burn(ownerOf(_tokenId), _tokenId); + } + + function exists(uint256 _tokenId) public view returns (bool) { + return super._exists(_tokenId); + } +} diff --git a/contracts/mocks/ERC721ReceiverMock.sol b/contracts/mocks/ERC721ReceiverMock.sol index 6fd00b6d40f..3d747f32b11 100644 --- a/contracts/mocks/ERC721ReceiverMock.sol +++ b/contracts/mocks/ERC721ReceiverMock.sol @@ -1,23 +1,23 @@ pragma solidity ^0.4.24; -import "../token/ERC721/ERC721Receiver.sol"; +import "../token/ERC721/IERC721Receiver.sol"; -contract ERC721ReceiverMock is ERC721Receiver { - bytes4 retval; - bool reverts; +contract ERC721ReceiverMock is IERC721Receiver { + bytes4 internal retval_; + bool internal reverts_; event Received( - address _operator, - address _from, - uint256 _tokenId, - bytes _data, - uint256 _gas + address operator, + address from, + uint256 tokenId, + bytes data, + uint256 gas ); constructor(bytes4 _retval, bool _reverts) public { - retval = _retval; - reverts = _reverts; + retval_ = _retval; + reverts_ = _reverts; } function onERC721Received( @@ -29,7 +29,7 @@ contract ERC721ReceiverMock is ERC721Receiver { public returns(bytes4) { - require(!reverts); + require(!reverts_); emit Received( _operator, _from, @@ -37,6 +37,6 @@ contract ERC721ReceiverMock is ERC721Receiver { _data, gasleft() // msg.gas was deprecated in solidityv0.4.21 ); - return retval; + return retval_; } } diff --git a/contracts/mocks/FinalizableCrowdsaleImpl.sol b/contracts/mocks/FinalizableCrowdsaleImpl.sol index 8321bdb9926..def19e6bf24 100644 --- a/contracts/mocks/FinalizableCrowdsaleImpl.sol +++ b/contracts/mocks/FinalizableCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/MintableToken.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/distribution/FinalizableCrowdsale.sol"; @@ -11,7 +11,7 @@ contract FinalizableCrowdsaleImpl is FinalizableCrowdsale { uint256 _closingTime, uint256 _rate, address _wallet, - MintableToken _token + IERC20 _token ) public Crowdsale(_rate, _wallet, _token) diff --git a/contracts/mocks/HasNoEtherTest.sol b/contracts/mocks/HasNoEtherTest.sol deleted file mode 100644 index 46798845887..00000000000 --- a/contracts/mocks/HasNoEtherTest.sol +++ /dev/null @@ -1,12 +0,0 @@ -pragma solidity ^0.4.24; - -import "../../contracts/ownership/HasNoEther.sol"; - - -contract HasNoEtherTest is HasNoEther { - - // Constructor with explicit payable — should still fail - constructor() public payable { - } - -} diff --git a/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol b/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol index 286eb616c89..95e4e367c54 100644 --- a/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol +++ b/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol @@ -10,7 +10,7 @@ contract IncreasingPriceCrowdsaleImpl is IncreasingPriceCrowdsale { uint256 _openingTime, uint256 _closingTime, address _wallet, - ERC20 _token, + IERC20 _token, uint256 _initialRate, uint256 _finalRate ) diff --git a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol index b796060ed07..b4b470f7ca6 100644 --- a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol +++ b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/validation/IndividuallyCappedCrowdsale.sol"; @@ -9,7 +9,7 @@ contract IndividuallyCappedCrowdsaleImpl is IndividuallyCappedCrowdsale { constructor ( uint256 _rate, address _wallet, - ERC20 _token + IERC20 _token ) public Crowdsale(_rate, _wallet, _token) diff --git a/contracts/mocks/InsecureInvariantTargetBounty.sol b/contracts/mocks/InsecureInvariantTargetBounty.sol new file mode 100644 index 00000000000..32d305cac0a --- /dev/null +++ b/contracts/mocks/InsecureInvariantTargetBounty.sol @@ -0,0 +1,20 @@ +pragma solidity ^0.4.24; + +// When this line is split, truffle parsing fails. +// See: https://github.com/ethereum/solidity/issues/4871 +// solium-disable-next-line max-len +import {BreakInvariantBounty, Target} from "../../contracts/bounties/BreakInvariantBounty.sol"; + + +contract InsecureInvariantTargetMock is Target { + function checkInvariant() public returns(bool) { + return false; + } +} + + +contract InsecureInvariantTargetBounty is BreakInvariantBounty { + function _deployContract() internal returns (address) { + return new InsecureInvariantTargetMock(); + } +} diff --git a/contracts/mocks/InsecureTargetBounty.sol b/contracts/mocks/InsecureTargetBounty.sol deleted file mode 100644 index 447fba58402..00000000000 --- a/contracts/mocks/InsecureTargetBounty.sol +++ /dev/null @@ -1,17 +0,0 @@ -pragma solidity ^0.4.24; - -import {Bounty, Target} from "../../contracts/Bounty.sol"; - - -contract InsecureTargetMock is Target { - function checkInvariant() public returns(bool) { - return false; - } -} - - -contract InsecureTargetBounty is Bounty { - function deployContract() internal returns (address) { - return new InsecureTargetMock(); - } -} diff --git a/contracts/mocks/LimitBalanceMock.sol b/contracts/mocks/LimitBalanceMock.sol deleted file mode 100644 index bc28d1ab6fb..00000000000 --- a/contracts/mocks/LimitBalanceMock.sol +++ /dev/null @@ -1,13 +0,0 @@ -pragma solidity ^0.4.24; - - -import "../LimitBalance.sol"; - - -// mock class using LimitBalance -contract LimitBalanceMock is LimitBalance(1000) { - - function limitedDeposit() public payable limitedPayable { - } - -} diff --git a/contracts/mocks/MerkleProofWrapper.sol b/contracts/mocks/MerkleProofWrapper.sol index bf963dea3c9..fe72d75f87f 100644 --- a/contracts/mocks/MerkleProofWrapper.sol +++ b/contracts/mocks/MerkleProofWrapper.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import { MerkleProof } from "../MerkleProof.sol"; +import { MerkleProof } from "../cryptography/MerkleProof.sol"; contract MerkleProofWrapper { diff --git a/contracts/mocks/MintedCrowdsaleImpl.sol b/contracts/mocks/MintedCrowdsaleImpl.sol index b776db3e044..77e3430b52e 100644 --- a/contracts/mocks/MintedCrowdsaleImpl.sol +++ b/contracts/mocks/MintedCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/MintableToken.sol"; +import "../token/ERC20/ERC20Mintable.sol"; import "../crowdsale/emission/MintedCrowdsale.sol"; @@ -9,7 +9,7 @@ contract MintedCrowdsaleImpl is MintedCrowdsale { constructor ( uint256 _rate, address _wallet, - MintableToken _token + ERC20Mintable _token ) public Crowdsale(_rate, _wallet, _token) diff --git a/contracts/mocks/PostDeliveryCrowdsaleImpl.sol b/contracts/mocks/PostDeliveryCrowdsaleImpl.sol index add2d866b01..b4dea2700e5 100644 --- a/contracts/mocks/PostDeliveryCrowdsaleImpl.sol +++ b/contracts/mocks/PostDeliveryCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/distribution/PostDeliveryCrowdsale.sol"; @@ -11,7 +11,7 @@ contract PostDeliveryCrowdsaleImpl is PostDeliveryCrowdsale { uint256 _closingTime, uint256 _rate, address _wallet, - ERC20 _token + IERC20 _token ) public TimedCrowdsale(_openingTime, _closingTime) diff --git a/contracts/mocks/PullPaymentMock.sol b/contracts/mocks/PullPaymentMock.sol index 5aa2b767fbb..639095dcbb8 100644 --- a/contracts/mocks/PullPaymentMock.sol +++ b/contracts/mocks/PullPaymentMock.sol @@ -11,7 +11,7 @@ contract PullPaymentMock is PullPayment { // test helper function to call asyncTransfer function callTransfer(address _dest, uint256 _amount) public { - asyncTransfer(_dest, _amount); + _asyncTransfer(_dest, _amount); } } diff --git a/contracts/mocks/RBACMock.sol b/contracts/mocks/RBACMock.sol index 7c2dbf79f14..f4acc927915 100644 --- a/contracts/mocks/RBACMock.sol +++ b/contracts/mocks/RBACMock.sol @@ -5,12 +5,12 @@ import "../examples/RBACWithAdmin.sol"; contract RBACMock is RBACWithAdmin { - string constant ROLE_ADVISOR = "advisor"; + string internal constant ROLE_ADVISOR = "advisor"; modifier onlyAdminOrAdvisor() { require( - hasRole(msg.sender, ROLE_ADMIN) || + isAdmin(msg.sender) || hasRole(msg.sender, ROLE_ADVISOR) ); _; @@ -19,10 +19,10 @@ contract RBACMock is RBACWithAdmin { constructor(address[] _advisors) public { - addRole(msg.sender, ROLE_ADVISOR); + _addRole(msg.sender, ROLE_ADVISOR); for (uint256 i = 0; i < _advisors.length; i++) { - addRole(_advisors[i], ROLE_ADVISOR); + _addRole(_advisors[i], ROLE_ADVISOR); } } @@ -64,6 +64,6 @@ contract RBACMock is RBACWithAdmin { checkRole(_account, ROLE_ADVISOR); // remove the advisor's role - removeRole(_account, ROLE_ADVISOR); + _removeRole(_account, ROLE_ADVISOR); } } diff --git a/contracts/mocks/ReentrancyMock.sol b/contracts/mocks/ReentrancyMock.sol index 670190558b3..93afdd6dc28 100644 --- a/contracts/mocks/ReentrancyMock.sol +++ b/contracts/mocks/ReentrancyMock.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../ReentrancyGuard.sol"; +import "../utils/ReentrancyGuard.sol"; import "./ReentrancyAttack.sol"; diff --git a/contracts/mocks/RefundableCrowdsaleImpl.sol b/contracts/mocks/RefundableCrowdsaleImpl.sol index b4ff6040a12..b581031bfd5 100644 --- a/contracts/mocks/RefundableCrowdsaleImpl.sol +++ b/contracts/mocks/RefundableCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/MintableToken.sol"; +import "../token/ERC20/ERC20Mintable.sol"; import "../crowdsale/distribution/RefundableCrowdsale.sol"; @@ -11,7 +11,7 @@ contract RefundableCrowdsaleImpl is RefundableCrowdsale { uint256 _closingTime, uint256 _rate, address _wallet, - MintableToken _token, + ERC20Mintable _token, uint256 _goal ) public diff --git a/contracts/mocks/RolesMock.sol b/contracts/mocks/RolesMock.sol index 96e0b64aaef..05cbbeaf2f5 100644 --- a/contracts/mocks/RolesMock.sol +++ b/contracts/mocks/RolesMock.sol @@ -28,10 +28,6 @@ contract RolesMock { dummyRole.transfer(_account); } - function check(address _account) public view { - dummyRole.check(_account); - } - function has(address _account) public view returns (bool) { return dummyRole.has(_account); } diff --git a/contracts/mocks/SafeERC20Helper.sol b/contracts/mocks/SafeERC20Helper.sol index 60254af7c7d..dfc2fb2c0b5 100644 --- a/contracts/mocks/SafeERC20Helper.sol +++ b/contracts/mocks/SafeERC20Helper.sol @@ -1,10 +1,10 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../token/ERC20/SafeERC20.sol"; -contract ERC20FailingMock is ERC20 { +contract ERC20FailingMock is IERC20 { function totalSupply() public view returns (uint256) { return 0; } @@ -31,7 +31,7 @@ contract ERC20FailingMock is ERC20 { } -contract ERC20SucceedingMock is ERC20 { +contract ERC20SucceedingMock is IERC20 { function totalSupply() public view returns (uint256) { return 0; } @@ -59,37 +59,37 @@ contract ERC20SucceedingMock is ERC20 { contract SafeERC20Helper { - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; - ERC20 failing; - ERC20 succeeding; + IERC20 internal failing_; + IERC20 internal succeeding_; constructor() public { - failing = new ERC20FailingMock(); - succeeding = new ERC20SucceedingMock(); + failing_ = new ERC20FailingMock(); + succeeding_ = new ERC20SucceedingMock(); } function doFailingTransfer() public { - failing.safeTransfer(address(0), 0); + failing_.safeTransfer(address(0), 0); } function doFailingTransferFrom() public { - failing.safeTransferFrom(address(0), address(0), 0); + failing_.safeTransferFrom(address(0), address(0), 0); } function doFailingApprove() public { - failing.safeApprove(address(0), 0); + failing_.safeApprove(address(0), 0); } function doSucceedingTransfer() public { - succeeding.safeTransfer(address(0), 0); + succeeding_.safeTransfer(address(0), 0); } function doSucceedingTransferFrom() public { - succeeding.safeTransferFrom(address(0), address(0), 0); + succeeding_.safeTransferFrom(address(0), address(0), 0); } function doSucceedingApprove() public { - succeeding.safeApprove(address(0), 0); + succeeding_.safeApprove(address(0), 0); } } diff --git a/contracts/mocks/SecureInvariantTargetBounty.sol b/contracts/mocks/SecureInvariantTargetBounty.sol new file mode 100644 index 00000000000..f08fbd9b81b --- /dev/null +++ b/contracts/mocks/SecureInvariantTargetBounty.sol @@ -0,0 +1,20 @@ +pragma solidity ^0.4.24; + +// When this line is split, truffle parsing fails. +// See: https://github.com/ethereum/solidity/issues/4871 +// solium-disable-next-line max-len +import {BreakInvariantBounty, Target} from "../../contracts/bounties/BreakInvariantBounty.sol"; + + +contract SecureInvariantTargetMock is Target { + function checkInvariant() public returns(bool) { + return true; + } +} + + +contract SecureInvariantTargetBounty is BreakInvariantBounty { + function _deployContract() internal returns (address) { + return new SecureInvariantTargetMock(); + } +} diff --git a/contracts/mocks/SecureTargetBounty.sol b/contracts/mocks/SecureTargetBounty.sol deleted file mode 100644 index 450e48532ac..00000000000 --- a/contracts/mocks/SecureTargetBounty.sol +++ /dev/null @@ -1,17 +0,0 @@ -pragma solidity ^0.4.24; - -import {Bounty, Target} from "../../contracts/Bounty.sol"; - - -contract SecureTargetMock is Target { - function checkInvariant() public returns(bool) { - return true; - } -} - - -contract SecureTargetBounty is Bounty { - function deployContract() internal returns (address) { - return new SecureTargetMock(); - } -} diff --git a/contracts/mocks/TimedCrowdsaleImpl.sol b/contracts/mocks/TimedCrowdsaleImpl.sol index 9e9c17486d8..b99178aee7c 100644 --- a/contracts/mocks/TimedCrowdsaleImpl.sol +++ b/contracts/mocks/TimedCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/validation/TimedCrowdsale.sol"; @@ -11,7 +11,7 @@ contract TimedCrowdsaleImpl is TimedCrowdsale { uint256 _closingTime, uint256 _rate, address _wallet, - ERC20 _token + IERC20 _token ) public Crowdsale(_rate, _wallet, _token) diff --git a/contracts/mocks/WhitelistedCrowdsaleImpl.sol b/contracts/mocks/WhitelistedCrowdsaleImpl.sol index 4cfd739ec40..25cd5118aea 100644 --- a/contracts/mocks/WhitelistedCrowdsaleImpl.sol +++ b/contracts/mocks/WhitelistedCrowdsaleImpl.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../crowdsale/validation/WhitelistedCrowdsale.sol"; import "../crowdsale/Crowdsale.sol"; @@ -10,7 +10,7 @@ contract WhitelistedCrowdsaleImpl is Crowdsale, WhitelistedCrowdsale { constructor ( uint256 _rate, address _wallet, - ERC20 _token + IERC20 _token ) Crowdsale(_rate, _wallet, _token) public diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index 37a78918cfb..858a1db84e8 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; import "./Ownable.sol"; -import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/IERC20.sol"; import "../token/ERC20/SafeERC20.sol"; @@ -12,13 +12,13 @@ import "../token/ERC20/SafeERC20.sol"; * This will prevent any accidental loss of tokens. */ contract CanReclaimToken is Ownable { - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; /** * @dev Reclaim all ERC20 compatible tokens * @param _token ERC20 The address of the token contract */ - function reclaimToken(ERC20 _token) external onlyOwner { + function reclaimToken(IERC20 _token) external onlyOwner { uint256 balance = _token.balanceOf(this); _token.safeTransfer(owner, balance); } diff --git a/contracts/ownership/Contactable.sol b/contracts/ownership/Contactable.sol deleted file mode 100644 index 9ed32cbbb7a..00000000000 --- a/contracts/ownership/Contactable.sol +++ /dev/null @@ -1,22 +0,0 @@ -pragma solidity ^0.4.24; - -import "./Ownable.sol"; - - -/** - * @title Contactable token - * @dev Basic version of a contactable contract, allowing the owner to provide a string with their - * contact information. - */ -contract Contactable is Ownable { - - string public contactInformation; - - /** - * @dev Allows the owner to set a string with their contact information. - * @param _info The contact information to attach to the contract. - */ - function setContactInformation(string _info) public onlyOwner { - contactInformation = _info; - } -} diff --git a/contracts/ownership/HasNoContracts.sol b/contracts/ownership/HasNoContracts.sol deleted file mode 100644 index f73cc6e5852..00000000000 --- a/contracts/ownership/HasNoContracts.sol +++ /dev/null @@ -1,22 +0,0 @@ -pragma solidity ^0.4.24; - -import "./Ownable.sol"; - - -/** - * @title Contracts that should not own Contracts - * @author Remco Bloemen - * @dev Should contracts (anything Ownable) end up being owned by this contract, it allows the owner - * of this contract to reclaim ownership of the contracts. - */ -contract HasNoContracts is Ownable { - - /** - * @dev Reclaim ownership of Ownable contracts - * @param _contractAddr The address of the Ownable to be reclaimed. - */ - function reclaimContract(address _contractAddr) external onlyOwner { - Ownable contractInst = Ownable(_contractAddr); - contractInst.transferOwnership(owner); - } -} diff --git a/contracts/ownership/HasNoEther.sol b/contracts/ownership/HasNoEther.sol deleted file mode 100644 index cdccd4f5ae3..00000000000 --- a/contracts/ownership/HasNoEther.sol +++ /dev/null @@ -1,41 +0,0 @@ -pragma solidity ^0.4.24; - -import "./Ownable.sol"; - - -/** - * @title Contracts that should not own Ether - * @author Remco Bloemen - * @dev This tries to block incoming ether to prevent accidental loss of Ether. Should Ether end up - * in the contract, it will allow the owner to reclaim this Ether. - * @notice Ether can still be sent to this contract by: - * calling functions labeled `payable` - * `selfdestruct(contract_address)` - * mining directly to the contract address - */ -contract HasNoEther is Ownable { - - /** - * @dev Constructor that rejects incoming Ether - * The `payable` flag is added so we can access `msg.value` without compiler warning. If we - * leave out payable, then Solidity will allow inheriting contracts to implement a payable - * constructor. By doing it this way we prevent a payable constructor from working. Alternatively - * we could use assembly to access msg.value. - */ - constructor() public payable { - require(msg.value == 0); - } - - /** - * @dev Disallows direct send by setting a default function without the `payable` flag. - */ - function() external { - } - - /** - * @dev Transfer all Ether held by the contract to the owner. - */ - function reclaimEther() external onlyOwner { - owner.transfer(address(this).balance); - } -} diff --git a/contracts/ownership/HasNoTokens.sol b/contracts/ownership/HasNoTokens.sol deleted file mode 100644 index 563a6404701..00000000000 --- a/contracts/ownership/HasNoTokens.sol +++ /dev/null @@ -1,35 +0,0 @@ -pragma solidity ^0.4.24; - -import "./CanReclaimToken.sol"; - - -/** - * @title Contracts that should not own Tokens - * @author Remco Bloemen - * @dev This blocks incoming ERC223 tokens to prevent accidental loss of tokens. - * Should tokens (any ERC20 compatible) end up in the contract, it allows the - * owner to reclaim the tokens. - */ -contract HasNoTokens is CanReclaimToken { - - /** - * @dev Reject all ERC223 compatible tokens - * @param _from address The address that is transferring the tokens - * @param _value uint256 the amount of the specified token - * @param _data Bytes The data passed from the caller. - */ - function tokenFallback( - address _from, - uint256 _value, - bytes _data - ) - external - pure - { - _from; - _value; - _data; - revert(); - } - -} diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index 4f36e3d3ee0..88e6fee62f4 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -47,7 +47,7 @@ contract Heritable is Ownable { * before the heir can take ownership. */ constructor(uint256 _heartbeatTimeout) public { - setHeartbeatTimeout(_heartbeatTimeout); + heartbeatTimeout_ = _heartbeatTimeout; } function setHeir(address _newHeir) public onlyOwner { @@ -86,7 +86,7 @@ contract Heritable is Ownable { * have to wait for `heartbeatTimeout` seconds. */ function proclaimDeath() public onlyHeir { - require(ownerLives()); + require(_ownerLives()); emit OwnerProclaimedDead(owner, heir_, timeOfDeath_); // solium-disable-next-line security/no-block-members timeOfDeath_ = block.timestamp; @@ -104,7 +104,7 @@ contract Heritable is Ownable { * @dev Allows heir to transfer ownership only if heartbeat has timed out. */ function claimHeirOwnership() public onlyHeir { - require(!ownerLives()); + require(!_ownerLives()); // solium-disable-next-line security/no-block-members require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); emit OwnershipTransferred(owner, heir_); @@ -113,14 +113,7 @@ contract Heritable is Ownable { timeOfDeath_ = 0; } - function setHeartbeatTimeout(uint256 _newHeartbeatTimeout) - internal onlyOwner - { - require(ownerLives()); - heartbeatTimeout_ = _newHeartbeatTimeout; - } - - function ownerLives() internal view returns (bool) { + function _ownerLives() internal view returns (bool) { return timeOfDeath_ == 0; } } diff --git a/contracts/ownership/NoOwner.sol b/contracts/ownership/NoOwner.sol deleted file mode 100644 index 42c3131dc04..00000000000 --- a/contracts/ownership/NoOwner.sol +++ /dev/null @@ -1,15 +0,0 @@ -pragma solidity ^0.4.24; - -import "./HasNoEther.sol"; -import "./HasNoTokens.sol"; -import "./HasNoContracts.sol"; - - -/** - * @title Base contract for contracts that should not own things. - * @author Remco Bloemen - * @dev Solves a class of errors where a contract accidentally becomes owner of Ether, Tokens or - * Owned contracts. See respective base contracts for details. - */ -contract NoOwner is HasNoEther, HasNoTokens, HasNoContracts { -} diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index ec8120d676e..d8d081d03c3 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -12,10 +12,10 @@ import "../access/rbac/RBAC.sol"; * A superuser can transfer his role to a new address. */ contract Superuser is Ownable, RBAC { - string public constant ROLE_SUPERUSER = "superuser"; + string private constant ROLE_SUPERUSER = "superuser"; constructor () public { - addRole(msg.sender, ROLE_SUPERUSER); + _addRole(msg.sender, ROLE_SUPERUSER); } /** @@ -48,8 +48,8 @@ contract Superuser is Ownable, RBAC { */ function transferSuperuser(address _newSuperuser) public onlySuperuser { require(_newSuperuser != address(0)); - removeRole(msg.sender, ROLE_SUPERUSER); - addRole(_newSuperuser, ROLE_SUPERUSER); + _removeRole(msg.sender, ROLE_SUPERUSER); + _addRole(_newSuperuser, ROLE_SUPERUSER); } /** diff --git a/contracts/payment/Escrow.sol b/contracts/payment/Escrow.sol index fab31d95c3b..2501bd833d9 100644 --- a/contracts/payment/Escrow.sol +++ b/contracts/payment/Escrow.sol @@ -17,10 +17,10 @@ contract Escrow is Secondary { event Deposited(address indexed payee, uint256 weiAmount); event Withdrawn(address indexed payee, uint256 weiAmount); - mapping(address => uint256) private deposits; + mapping(address => uint256) private deposits_; function depositsOf(address _payee) public view returns (uint256) { - return deposits[_payee]; + return deposits_[_payee]; } /** @@ -29,7 +29,7 @@ contract Escrow is Secondary { */ function deposit(address _payee) public onlyPrimary payable { uint256 amount = msg.value; - deposits[_payee] = deposits[_payee].add(amount); + deposits_[_payee] = deposits_[_payee].add(amount); emit Deposited(_payee, amount); } @@ -39,10 +39,10 @@ contract Escrow is Secondary { * @param _payee The address whose funds will be withdrawn and transferred to. */ function withdraw(address _payee) public onlyPrimary { - uint256 payment = deposits[_payee]; + uint256 payment = deposits_[_payee]; assert(address(this).balance >= payment); - deposits[_payee] = 0; + deposits_[_payee] = 0; _payee.transfer(payment); diff --git a/contracts/payment/PullPayment.sol b/contracts/payment/PullPayment.sol index e117fc49eb6..cd10424f315 100644 --- a/contracts/payment/PullPayment.sol +++ b/contracts/payment/PullPayment.sol @@ -6,7 +6,7 @@ import "./Escrow.sol"; /** * @title PullPayment * @dev Base contract supporting async send for pull payments. Inherit from this - * contract and use asyncTransfer instead of send or transfer. + * contract and use _asyncTransfer instead of send or transfer. */ contract PullPayment { Escrow private escrow; @@ -36,7 +36,7 @@ contract PullPayment { * @param _dest The destination address of the funds. * @param _amount The amount to transfer. */ - function asyncTransfer(address _dest, uint256 _amount) internal { + function _asyncTransfer(address _dest, uint256 _amount) internal { escrow.deposit.value(_amount)(_dest); } } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 19202323fcf..04b01425683 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -26,7 +26,7 @@ contract SplitPayment { require(_payees.length > 0); for (uint256 i = 0; i < _payees.length; i++) { - addPayee(_payees[i], _shares[i]); + _addPayee(_payees[i], _shares[i]); } } @@ -64,7 +64,7 @@ contract SplitPayment { * @param _payee The address of the payee to add. * @param _shares The number of shares owned by the payee. */ - function addPayee(address _payee, uint256 _shares) internal { + function _addPayee(address _payee, uint256 _shares) internal { require(_payee != address(0)); require(_shares > 0); require(shares[_payee] == 0); diff --git a/contracts/proposals/ERC1046/TokenMetadata.sol b/contracts/proposals/ERC1046/TokenMetadata.sol index 38edbd06419..6efb4ed72db 100644 --- a/contracts/proposals/ERC1046/TokenMetadata.sol +++ b/contracts/proposals/ERC1046/TokenMetadata.sol @@ -1,15 +1,15 @@ pragma solidity ^0.4.21; -import "../../token/ERC20/ERC20.sol"; +import "../../token/ERC20/IERC20.sol"; /** * @title ERC-1047 Token Metadata * @dev See https://eips.ethereum.org/EIPS/eip-1046 * @dev tokenURI must respond with a URI that implements https://eips.ethereum.org/EIPS/eip-1047 - * @dev TODO - update https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC721/ERC721.sol#L17 when 1046 is finalized + * @dev TODO - update https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC721/IERC721.sol#L17 when 1046 is finalized */ -contract ERC20TokenMetadata is ERC20 { +contract ERC20TokenMetadata is IERC20 { function tokenURI() external view returns (string); } diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 7b152953f0f..0201cc0b849 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -1,35 +1,204 @@ pragma solidity ^0.4.24; +import "./IERC20.sol"; +import "../../math/SafeMath.sol"; + /** - * @title ERC20 interface - * @dev see https://github.com/ethereum/EIPs/issues/20 + * @title Standard ERC20 token + * + * @dev Implementation of the basic standard token. + * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md + * Originally based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol */ -contract ERC20 { - function totalSupply() public view returns (uint256); +contract ERC20 is IERC20 { + using SafeMath for uint256; + + mapping (address => uint256) private balances_; + + mapping (address => mapping (address => uint256)) private allowed_; + + uint256 private totalSupply_; + + /** + * @dev Total number of tokens in existence + */ + function totalSupply() public view returns (uint256) { + return totalSupply_; + } + + /** + * @dev Gets the balance of the specified address. + * @param _owner The address to query the the balance of. + * @return An uint256 representing the amount owned by the passed address. + */ + function balanceOf(address _owner) public view returns (uint256) { + return balances_[_owner]; + } + + /** + * @dev Function to check the amount of tokens that an owner allowed to a spender. + * @param _owner address The address which owns the funds. + * @param _spender address The address which will spend the funds. + * @return A uint256 specifying the amount of tokens still available for the spender. + */ + function allowance( + address _owner, + address _spender + ) + public + view + returns (uint256) + { + return allowed_[_owner][_spender]; + } + + /** + * @dev Transfer token for a specified address + * @param _to The address to transfer to. + * @param _value The amount to be transferred. + */ + function transfer(address _to, uint256 _value) public returns (bool) { + require(_value <= balances_[msg.sender]); + require(_to != address(0)); + + balances_[msg.sender] = balances_[msg.sender].sub(_value); + balances_[_to] = balances_[_to].add(_value); + emit Transfer(msg.sender, _to, _value); + return true; + } + + /** + * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. + * Beware that changing an allowance with this method brings the risk that someone may use both the old + * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this + * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + * @param _spender The address which will spend the funds. + * @param _value The amount of tokens to be spent. + */ + function approve(address _spender, uint256 _value) public returns (bool) { + allowed_[msg.sender][_spender] = _value; + emit Approval(msg.sender, _spender, _value); + return true; + } + + /** + * @dev Transfer tokens from one address to another + * @param _from address The address which you want to send tokens from + * @param _to address The address which you want to transfer to + * @param _value uint256 the amount of tokens to be transferred + */ + function transferFrom( + address _from, + address _to, + uint256 _value + ) + public + returns (bool) + { + require(_value <= balances_[_from]); + require(_value <= allowed_[_from][msg.sender]); + require(_to != address(0)); + + balances_[_from] = balances_[_from].sub(_value); + balances_[_to] = balances_[_to].add(_value); + allowed_[_from][msg.sender] = allowed_[_from][msg.sender].sub(_value); + emit Transfer(_from, _to, _value); + return true; + } - function balanceOf(address _who) public view returns (uint256); + /** + * @dev Increase the amount of tokens that an owner allowed to a spender. + * approve should be called when allowed_[_spender] == 0. To increment + * allowed value is better to use this function to avoid 2 calls (and wait until + * the first transaction is mined) + * From MonolithDAO Token.sol + * @param _spender The address which will spend the funds. + * @param _addedValue The amount of tokens to increase the allowance by. + */ + function increaseApproval( + address _spender, + uint256 _addedValue + ) + public + returns (bool) + { + allowed_[msg.sender][_spender] = ( + allowed_[msg.sender][_spender].add(_addedValue)); + emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]); + return true; + } - function allowance(address _owner, address _spender) - public view returns (uint256); + /** + * @dev Decrease the amount of tokens that an owner allowed to a spender. + * approve should be called when allowed_[_spender] == 0. To decrement + * allowed value is better to use this function to avoid 2 calls (and wait until + * the first transaction is mined) + * From MonolithDAO Token.sol + * @param _spender The address which will spend the funds. + * @param _subtractedValue The amount of tokens to decrease the allowance by. + */ + function decreaseApproval( + address _spender, + uint256 _subtractedValue + ) + public + returns (bool) + { + uint256 oldValue = allowed_[msg.sender][_spender]; + if (_subtractedValue >= oldValue) { + allowed_[msg.sender][_spender] = 0; + } else { + allowed_[msg.sender][_spender] = oldValue.sub(_subtractedValue); + } + emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]); + return true; + } - function transfer(address _to, uint256 _value) public returns (bool); + /** + * @dev Internal function that mints an amount of the token and assigns it to + * an account. This encapsulates the modification of balances such that the + * proper events are emitted. + * @param _account The account that will receive the created tokens. + * @param _amount The amount that will be created. + */ + function _mint(address _account, uint256 _amount) internal { + require(_account != 0); + totalSupply_ = totalSupply_.add(_amount); + balances_[_account] = balances_[_account].add(_amount); + emit Transfer(address(0), _account, _amount); + } - function approve(address _spender, uint256 _value) - public returns (bool); + /** + * @dev Internal function that burns an amount of the token of a given + * account. + * @param _account The account whose tokens will be burnt. + * @param _amount The amount that will be burnt. + */ + function _burn(address _account, uint256 _amount) internal { + require(_account != 0); + require(_amount <= balances_[_account]); - function transferFrom(address _from, address _to, uint256 _value) - public returns (bool); + totalSupply_ = totalSupply_.sub(_amount); + balances_[_account] = balances_[_account].sub(_amount); + emit Transfer(_account, address(0), _amount); + } - event Transfer( - address indexed from, - address indexed to, - uint256 value - ); + /** + * @dev Internal function that burns an amount of the token of a given + * account, deducting from the sender's allowance for said account. Uses the + * internal _burn function. + * @param _account The account whose tokens will be burnt. + * @param _amount The amount that will be burnt. + */ + function _burnFrom(address _account, uint256 _amount) internal { + require(_amount <= allowed_[_account][msg.sender]); - event Approval( - address indexed owner, - address indexed spender, - uint256 value - ); + // Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted, + // this function needs to emit an event with the updated approval. + allowed_[_account][msg.sender] = allowed_[_account][msg.sender].sub( + _amount); + _burn(_account, _amount); + } } diff --git a/contracts/token/ERC20/BurnableToken.sol b/contracts/token/ERC20/ERC20Burnable.sol similarity index 76% rename from contracts/token/ERC20/BurnableToken.sol rename to contracts/token/ERC20/ERC20Burnable.sol index 7b1c7c9be95..1fc83b77a00 100644 --- a/contracts/token/ERC20/BurnableToken.sol +++ b/contracts/token/ERC20/ERC20Burnable.sol @@ -1,15 +1,15 @@ pragma solidity ^0.4.24; -import "./StandardToken.sol"; +import "./ERC20.sol"; /** * @title Burnable Token * @dev Token that can be irreversibly burned (destroyed). */ -contract BurnableToken is StandardToken { +contract ERC20Burnable is ERC20 { - event Burn(address indexed burner, uint256 value); + event TokensBurned(address indexed burner, uint256 value); /** * @dev Burns a specific amount of tokens. @@ -29,11 +29,11 @@ contract BurnableToken is StandardToken { } /** - * @dev Overrides StandardToken._burn in order for burn and burnFrom to emit + * @dev Overrides ERC20._burn in order for burn and burnFrom to emit * an additional Burn event. */ function _burn(address _who, uint256 _value) internal { super._burn(_who, _value); - emit Burn(_who, _value); + emit TokensBurned(_who, _value); } } diff --git a/contracts/token/ERC20/CappedToken.sol b/contracts/token/ERC20/ERC20Capped.sol similarity index 78% rename from contracts/token/ERC20/CappedToken.sol rename to contracts/token/ERC20/ERC20Capped.sol index 1af8bdcb647..a21a9107a62 100644 --- a/contracts/token/ERC20/CappedToken.sol +++ b/contracts/token/ERC20/ERC20Capped.sol @@ -1,17 +1,20 @@ pragma solidity ^0.4.24; -import "./MintableToken.sol"; +import "./ERC20Mintable.sol"; /** * @title Capped token * @dev Mintable token with a token cap. */ -contract CappedToken is MintableToken { +contract ERC20Capped is ERC20Mintable { uint256 public cap; - constructor(uint256 _cap) public { + constructor(uint256 _cap, address[] _minters) + public + ERC20Mintable(_minters) + { require(_cap > 0); cap = _cap; } diff --git a/contracts/token/ERC20/DetailedERC20.sol b/contracts/token/ERC20/ERC20Detailed.sol similarity index 83% rename from contracts/token/ERC20/DetailedERC20.sol rename to contracts/token/ERC20/ERC20Detailed.sol index 20ba0f60c91..ad6f1dd059a 100644 --- a/contracts/token/ERC20/DetailedERC20.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -1,15 +1,15 @@ pragma solidity ^0.4.24; -import "./ERC20.sol"; +import "./IERC20.sol"; /** - * @title DetailedERC20 token + * @title ERC20Detailed token * @dev The decimals are only for visualization purposes. * All the operations are done using the smallest and indivisible token unit, * just as on Ethereum all the operations are done in wei. */ -contract DetailedERC20 is ERC20 { +contract ERC20Detailed is IERC20 { string public name; string public symbol; uint8 public decimals; diff --git a/contracts/token/ERC20/MintableToken.sol b/contracts/token/ERC20/ERC20Mintable.sol similarity index 78% rename from contracts/token/ERC20/MintableToken.sol rename to contracts/token/ERC20/ERC20Mintable.sol index ba1ca68336f..b746aa91783 100644 --- a/contracts/token/ERC20/MintableToken.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; -import "./StandardToken.sol"; -import "../../ownership/Ownable.sol"; +import "./ERC20.sol"; +import "../../access/rbac/MinterRole.sol"; /** @@ -9,23 +9,23 @@ import "../../ownership/Ownable.sol"; * @dev Simple ERC20 Token example, with mintable token creation * Based on code by TokenMarketNet: https://github.com/TokenMarketNet/ico/blob/master/contracts/MintableToken.sol */ -contract MintableToken is StandardToken, Ownable { +contract ERC20Mintable is ERC20, MinterRole { event Mint(address indexed to, uint256 amount); event MintFinished(); bool public mintingFinished = false; + constructor(address[] _minters) + MinterRole(_minters) + public + { + } modifier canMint() { require(!mintingFinished); _; } - modifier hasMintPermission() { - require(msg.sender == owner); - _; - } - /** * @dev Function to mint tokens * @param _to The address that will receive the minted tokens. @@ -37,7 +37,7 @@ contract MintableToken is StandardToken, Ownable { uint256 _amount ) public - hasMintPermission + onlyMinter canMint returns (bool) { @@ -50,7 +50,7 @@ contract MintableToken is StandardToken, Ownable { * @dev Function to stop minting new tokens. * @return True if the operation was successful. */ - function finishMinting() public onlyOwner canMint returns (bool) { + function finishMinting() public onlyMinter canMint returns (bool) { mintingFinished = true; emit MintFinished(); return true; diff --git a/contracts/token/ERC20/PausableToken.sol b/contracts/token/ERC20/ERC20Pausable.sol similarity index 88% rename from contracts/token/ERC20/PausableToken.sol rename to contracts/token/ERC20/ERC20Pausable.sol index 75b4b938c69..d631fe1d960 100644 --- a/contracts/token/ERC20/PausableToken.sol +++ b/contracts/token/ERC20/ERC20Pausable.sol @@ -1,14 +1,14 @@ pragma solidity ^0.4.24; -import "./StandardToken.sol"; +import "./ERC20.sol"; import "../../lifecycle/Pausable.sol"; /** * @title Pausable token - * @dev StandardToken modified with pausable transfers. + * @dev ERC20 modified with pausable transfers. **/ -contract PausableToken is StandardToken, Pausable { +contract ERC20Pausable is ERC20, Pausable { function transfer( address _to, diff --git a/contracts/token/ERC20/IERC20.sol b/contracts/token/ERC20/IERC20.sol new file mode 100644 index 00000000000..155c1cd9dbc --- /dev/null +++ b/contracts/token/ERC20/IERC20.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.4.24; + + +/** + * @title ERC20 interface + * @dev see https://github.com/ethereum/EIPs/issues/20 + */ +interface IERC20 { + function totalSupply() external view returns (uint256); + + function balanceOf(address _who) external view returns (uint256); + + function allowance(address _owner, address _spender) + external view returns (uint256); + + function transfer(address _to, uint256 _value) external returns (bool); + + function approve(address _spender, uint256 _value) + external returns (bool); + + function transferFrom(address _from, address _to, uint256 _value) + external returns (bool); + + event Transfer( + address indexed from, + address indexed to, + uint256 value + ); + + event Approval( + address indexed owner, + address indexed spender, + uint256 value + ); +} diff --git a/contracts/token/ERC20/SafeERC20.sol b/contracts/token/ERC20/SafeERC20.sol index 92aaa0fd2e2..451cf136c81 100644 --- a/contracts/token/ERC20/SafeERC20.sol +++ b/contracts/token/ERC20/SafeERC20.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; -import "./StandardToken.sol"; import "./ERC20.sol"; +import "./IERC20.sol"; /** @@ -12,7 +12,7 @@ import "./ERC20.sol"; */ library SafeERC20 { function safeTransfer( - ERC20 _token, + IERC20 _token, address _to, uint256 _value ) @@ -22,7 +22,7 @@ library SafeERC20 { } function safeTransferFrom( - ERC20 _token, + IERC20 _token, address _from, address _to, uint256 _value @@ -33,7 +33,7 @@ library SafeERC20 { } function safeApprove( - ERC20 _token, + IERC20 _token, address _spender, uint256 _value ) diff --git a/contracts/token/ERC20/StandardToken.sol b/contracts/token/ERC20/StandardToken.sol deleted file mode 100644 index 4eada707a8b..00000000000 --- a/contracts/token/ERC20/StandardToken.sol +++ /dev/null @@ -1,203 +0,0 @@ -pragma solidity ^0.4.24; - -import "./ERC20.sol"; -import "../../math/SafeMath.sol"; - - -/** - * @title Standard ERC20 token - * - * @dev Implementation of the basic standard token. - * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md - * Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol - */ -contract StandardToken is ERC20 { - using SafeMath for uint256; - - mapping (address => uint256) private balances; - - mapping (address => mapping (address => uint256)) private allowed; - - uint256 private totalSupply_; - - /** - * @dev Total number of tokens in existence - */ - function totalSupply() public view returns (uint256) { - return totalSupply_; - } - - /** - * @dev Gets the balance of the specified address. - * @param _owner The address to query the the balance of. - * @return An uint256 representing the amount owned by the passed address. - */ - function balanceOf(address _owner) public view returns (uint256) { - return balances[_owner]; - } - - /** - * @dev Function to check the amount of tokens that an owner allowed to a spender. - * @param _owner address The address which owns the funds. - * @param _spender address The address which will spend the funds. - * @return A uint256 specifying the amount of tokens still available for the spender. - */ - function allowance( - address _owner, - address _spender - ) - public - view - returns (uint256) - { - return allowed[_owner][_spender]; - } - - /** - * @dev Transfer token for a specified address - * @param _to The address to transfer to. - * @param _value The amount to be transferred. - */ - function transfer(address _to, uint256 _value) public returns (bool) { - require(_value <= balances[msg.sender]); - require(_to != address(0)); - - balances[msg.sender] = balances[msg.sender].sub(_value); - balances[_to] = balances[_to].add(_value); - emit Transfer(msg.sender, _to, _value); - return true; - } - - /** - * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. - * Beware that changing an allowance with this method brings the risk that someone may use both the old - * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this - * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: - * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 - * @param _spender The address which will spend the funds. - * @param _value The amount of tokens to be spent. - */ - function approve(address _spender, uint256 _value) public returns (bool) { - allowed[msg.sender][_spender] = _value; - emit Approval(msg.sender, _spender, _value); - return true; - } - - /** - * @dev Transfer tokens from one address to another - * @param _from address The address which you want to send tokens from - * @param _to address The address which you want to transfer to - * @param _value uint256 the amount of tokens to be transferred - */ - function transferFrom( - address _from, - address _to, - uint256 _value - ) - public - returns (bool) - { - require(_value <= balances[_from]); - require(_value <= allowed[_from][msg.sender]); - require(_to != address(0)); - - balances[_from] = balances[_from].sub(_value); - balances[_to] = balances[_to].add(_value); - allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); - emit Transfer(_from, _to, _value); - return true; - } - - /** - * @dev Increase the amount of tokens that an owner allowed to a spender. - * approve should be called when allowed[_spender] == 0. To increment - * allowed value is better to use this function to avoid 2 calls (and wait until - * the first transaction is mined) - * From MonolithDAO Token.sol - * @param _spender The address which will spend the funds. - * @param _addedValue The amount of tokens to increase the allowance by. - */ - function increaseApproval( - address _spender, - uint256 _addedValue - ) - public - returns (bool) - { - allowed[msg.sender][_spender] = ( - allowed[msg.sender][_spender].add(_addedValue)); - emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); - return true; - } - - /** - * @dev Decrease the amount of tokens that an owner allowed to a spender. - * approve should be called when allowed[_spender] == 0. To decrement - * allowed value is better to use this function to avoid 2 calls (and wait until - * the first transaction is mined) - * From MonolithDAO Token.sol - * @param _spender The address which will spend the funds. - * @param _subtractedValue The amount of tokens to decrease the allowance by. - */ - function decreaseApproval( - address _spender, - uint256 _subtractedValue - ) - public - returns (bool) - { - uint256 oldValue = allowed[msg.sender][_spender]; - if (_subtractedValue >= oldValue) { - allowed[msg.sender][_spender] = 0; - } else { - allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); - } - emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); - return true; - } - - /** - * @dev Internal function that mints an amount of the token and assigns it to - * an account. This encapsulates the modification of balances such that the - * proper events are emitted. - * @param _account The account that will receive the created tokens. - * @param _amount The amount that will be created. - */ - function _mint(address _account, uint256 _amount) internal { - require(_account != 0); - totalSupply_ = totalSupply_.add(_amount); - balances[_account] = balances[_account].add(_amount); - emit Transfer(address(0), _account, _amount); - } - - /** - * @dev Internal function that burns an amount of the token of a given - * account. - * @param _account The account whose tokens will be burnt. - * @param _amount The amount that will be burnt. - */ - function _burn(address _account, uint256 _amount) internal { - require(_account != 0); - require(_amount <= balances[_account]); - - totalSupply_ = totalSupply_.sub(_amount); - balances[_account] = balances[_account].sub(_amount); - emit Transfer(_account, address(0), _amount); - } - - /** - * @dev Internal function that burns an amount of the token of a given - * account, deducting from the sender's allowance for said account. Uses the - * internal _burn function. - * @param _account The account whose tokens will be burnt. - * @param _amount The amount that will be burnt. - */ - function _burnFrom(address _account, uint256 _amount) internal { - require(_amount <= allowed[_account][msg.sender]); - - // Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted, - // this function needs to emit an event with the updated approval. - allowed[_account][msg.sender] = allowed[_account][msg.sender].sub(_amount); - _burn(_account, _amount); - } -} diff --git a/contracts/token/ERC20/TokenTimelock.sol b/contracts/token/ERC20/TokenTimelock.sol index 2e3cd6dd55e..a24642f6de5 100644 --- a/contracts/token/ERC20/TokenTimelock.sol +++ b/contracts/token/ERC20/TokenTimelock.sol @@ -9,10 +9,10 @@ import "./SafeERC20.sol"; * beneficiary to extract the tokens after a given release time */ contract TokenTimelock { - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; // ERC20 basic token contract being held - ERC20 public token; + IERC20 public token; // beneficiary of tokens after they are released address public beneficiary; @@ -21,7 +21,7 @@ contract TokenTimelock { uint256 public releaseTime; constructor( - ERC20 _token, + IERC20 _token, address _beneficiary, uint256 _releaseTime ) diff --git a/contracts/token/ERC20/TokenVesting.sol b/contracts/token/ERC20/TokenVesting.sol index 2ceaa31352e..31fac8882ce 100644 --- a/contracts/token/ERC20/TokenVesting.sol +++ b/contracts/token/ERC20/TokenVesting.sol @@ -15,7 +15,7 @@ import "../../math/SafeMath.sol"; */ contract TokenVesting is Ownable { using SafeMath for uint256; - using SafeERC20 for ERC20; + using SafeERC20 for IERC20; event Released(uint256 amount); event Revoked(); @@ -65,7 +65,7 @@ contract TokenVesting is Ownable { * @notice Transfers vested tokens to beneficiary. * @param _token ERC20 token which is being vested */ - function release(ERC20 _token) public { + function release(IERC20 _token) public { uint256 unreleased = releasableAmount(_token); require(unreleased > 0); @@ -82,7 +82,7 @@ contract TokenVesting is Ownable { * remain in the contract, the rest are returned to the owner. * @param _token ERC20 token which is being vested */ - function revoke(ERC20 _token) public onlyOwner { + function revoke(IERC20 _token) public onlyOwner { require(revocable); require(!revoked[_token]); @@ -102,7 +102,7 @@ contract TokenVesting is Ownable { * @dev Calculates the amount that has already vested but hasn't been released yet. * @param _token ERC20 token which is being vested */ - function releasableAmount(ERC20 _token) public view returns (uint256) { + function releasableAmount(IERC20 _token) public view returns (uint256) { return vestedAmount(_token).sub(released[_token]); } @@ -110,7 +110,7 @@ contract TokenVesting is Ownable { * @dev Calculates the amount that has already vested. * @param _token ERC20 token which is being vested */ - function vestedAmount(ERC20 _token) public view returns (uint256) { + function vestedAmount(IERC20 _token) public view returns (uint256) { uint256 currentBalance = _token.balanceOf(this); uint256 totalBalance = currentBalance.add(released[_token]); diff --git a/contracts/token/ERC721/DeprecatedERC721.sol b/contracts/token/ERC721/DeprecatedERC721.sol deleted file mode 100644 index 3cf1f3b2d3b..00000000000 --- a/contracts/token/ERC721/DeprecatedERC721.sol +++ /dev/null @@ -1,15 +0,0 @@ -pragma solidity ^0.4.24; - -import "./ERC721.sol"; - - -/** - * @title ERC-721 methods shipped in OpenZeppelin v1.7.0, removed in the latest version of the standard - * @dev Only use this interface for compatibility with previously deployed contracts - * Use ERC721 for interacting with new contracts which are standard-compliant - */ -contract DeprecatedERC721 is ERC721 { - function takeOwnership(uint256 _tokenId) public; - function transfer(address _to, uint256 _tokenId) public; - function tokensOf(address _owner) public view returns (uint256[]); -} diff --git a/contracts/token/ERC721/ERC721.sol b/contracts/token/ERC721/ERC721.sol index a988d8f4059..287555899f7 100644 --- a/contracts/token/ERC721/ERC721.sol +++ b/contracts/token/ERC721/ERC721.sol @@ -1,40 +1,201 @@ pragma solidity ^0.4.24; +import "./IERC721.sol"; import "./ERC721Basic.sol"; +import "../../introspection/SupportsInterfaceWithLookup.sol"; /** - * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension - * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md + * @title Full ERC721 Token + * This implementation includes all the required and some optional functionality of the ERC721 standard + * Moreover, it includes approve all functionality using operator terminology + * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md */ -contract ERC721Enumerable is ERC721Basic { - function totalSupply() public view returns (uint256); +contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { + + // Token name + string internal name_; + + // Token symbol + string internal symbol_; + + // Mapping from owner to list of owned token IDs + mapping(address => uint256[]) internal ownedTokens; + + // Mapping from token ID to index of the owner tokens list + mapping(uint256 => uint256) internal ownedTokensIndex; + + // Array with all token ids, used for enumeration + uint256[] internal allTokens; + + // Mapping from token id to position in the allTokens array + mapping(uint256 => uint256) internal allTokensIndex; + + // Optional mapping for token URIs + mapping(uint256 => string) internal tokenURIs; + + /** + * @dev Constructor function + */ + constructor(string _name, string _symbol) public { + name_ = _name; + symbol_ = _symbol; + + // register the supported interfaces to conform to ERC721 via ERC165 + _registerInterface(InterfaceId_ERC721Enumerable); + _registerInterface(InterfaceId_ERC721Metadata); + } + + /** + * @dev Gets the token name + * @return string representing the token name + */ + function name() external view returns (string) { + return name_; + } + + /** + * @dev Gets the token symbol + * @return string representing the token symbol + */ + function symbol() external view returns (string) { + return symbol_; + } + + /** + * @dev Returns an URI for a given token ID + * Throws if the token ID does not exist. May return an empty string. + * @param _tokenId uint256 ID of the token to query + */ + function tokenURI(uint256 _tokenId) public view returns (string) { + require(_exists(_tokenId)); + return tokenURIs[_tokenId]; + } + + /** + * @dev Gets the token ID at a given index of the tokens list of the requested owner + * @param _owner address owning the tokens list to be accessed + * @param _index uint256 representing the index to be accessed of the requested tokens list + * @return uint256 token ID at the given index of the tokens list owned by the requested address + */ function tokenOfOwnerByIndex( address _owner, uint256 _index ) public view - returns (uint256 _tokenId); + returns (uint256) + { + require(_index < balanceOf(_owner)); + return ownedTokens[_owner][_index]; + } - function tokenByIndex(uint256 _index) public view returns (uint256); -} + /** + * @dev Gets the total amount of tokens stored by the contract + * @return uint256 representing the total amount of tokens + */ + function totalSupply() public view returns (uint256) { + return allTokens.length; + } + /** + * @dev Gets the token ID at a given index of all the tokens in this contract + * Reverts if the index is greater or equal to the total number of tokens + * @param _index uint256 representing the index to be accessed of the tokens list + * @return uint256 token ID at the given index of the tokens list + */ + function tokenByIndex(uint256 _index) public view returns (uint256) { + require(_index < totalSupply()); + return allTokens[_index]; + } -/** - * @title ERC-721 Non-Fungible Token Standard, optional metadata extension - * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md - */ -contract ERC721Metadata is ERC721Basic { - function name() external view returns (string _name); - function symbol() external view returns (string _symbol); - function tokenURI(uint256 _tokenId) public view returns (string); -} + /** + * @dev Internal function to set the token URI for a given token + * Reverts if the token ID does not exist + * @param _tokenId uint256 ID of the token to set its URI + * @param _uri string URI to assign + */ + function _setTokenURI(uint256 _tokenId, string _uri) internal { + require(_exists(_tokenId)); + tokenURIs[_tokenId] = _uri; + } + /** + * @dev Internal function to add a token ID to the list of a given address + * @param _to address representing the new owner of the given token ID + * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address + */ + function _addTokenTo(address _to, uint256 _tokenId) internal { + super._addTokenTo(_to, _tokenId); + uint256 length = ownedTokens[_to].length; + ownedTokens[_to].push(_tokenId); + ownedTokensIndex[_tokenId] = length; + } + + /** + * @dev Internal function to remove a token ID from the list of a given address + * @param _from address representing the previous owner of the given token ID + * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address + */ + function _removeTokenFrom(address _from, uint256 _tokenId) internal { + super._removeTokenFrom(_from, _tokenId); + + // To prevent a gap in the array, we store the last token in the index of the token to delete, and + // then delete the last slot. + uint256 tokenIndex = ownedTokensIndex[_tokenId]; + uint256 lastTokenIndex = ownedTokens[_from].length.sub(1); + uint256 lastToken = ownedTokens[_from][lastTokenIndex]; + + ownedTokens[_from][tokenIndex] = lastToken; + // This also deletes the contents at the last position of the array + ownedTokens[_from].length--; + + // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to + // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping + // the lastToken to the first position, and then dropping the element placed in the last position of the list + + ownedTokensIndex[_tokenId] = 0; + ownedTokensIndex[lastToken] = tokenIndex; + } + + /** + * @dev Internal function to mint a new token + * Reverts if the given token ID already exists + * @param _to address the beneficiary that will own the minted token + * @param _tokenId uint256 ID of the token to be minted by the msg.sender + */ + function _mint(address _to, uint256 _tokenId) internal { + super._mint(_to, _tokenId); + + allTokensIndex[_tokenId] = allTokens.length; + allTokens.push(_tokenId); + } + + /** + * @dev Internal function to burn a specific token + * Reverts if the token does not exist + * @param _owner owner of the token to burn + * @param _tokenId uint256 ID of the token being burned by the msg.sender + */ + function _burn(address _owner, uint256 _tokenId) internal { + super._burn(_owner, _tokenId); + + // Clear metadata (if any) + if (bytes(tokenURIs[_tokenId]).length != 0) { + delete tokenURIs[_tokenId]; + } + + // Reorg all tokens array + uint256 tokenIndex = allTokensIndex[_tokenId]; + uint256 lastTokenIndex = allTokens.length.sub(1); + uint256 lastToken = allTokens[lastTokenIndex]; + + allTokens[tokenIndex] = lastToken; + allTokens[lastTokenIndex] = 0; + + allTokens.length--; + allTokensIndex[_tokenId] = 0; + allTokensIndex[lastToken] = tokenIndex; + } -/** - * @title ERC-721 Non-Fungible Token Standard, full implementation interface - * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md - */ -contract ERC721 is ERC721Basic, ERC721Enumerable, ERC721Metadata { } diff --git a/contracts/token/ERC721/ERC721Basic.sol b/contracts/token/ERC721/ERC721Basic.sol index f9075f06004..763629d4838 100644 --- a/contracts/token/ERC721/ERC721Basic.sol +++ b/contracts/token/ERC721/ERC721Basic.sol @@ -1,80 +1,310 @@ pragma solidity ^0.4.24; -import "../../introspection/ERC165.sol"; +import "./IERC721Basic.sol"; +import "./IERC721Receiver.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; +import "../../introspection/SupportsInterfaceWithLookup.sol"; /** - * @title ERC721 Non-Fungible Token Standard basic interface + * @title ERC721 Non-Fungible Token Standard basic implementation * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md */ -contract ERC721Basic is ERC165 { - - bytes4 internal constant InterfaceId_ERC721 = 0x80ac58cd; - /* - * 0x80ac58cd === - * bytes4(keccak256('balanceOf(address)')) ^ - * bytes4(keccak256('ownerOf(uint256)')) ^ - * bytes4(keccak256('approve(address,uint256)')) ^ - * bytes4(keccak256('getApproved(uint256)')) ^ - * bytes4(keccak256('setApprovalForAll(address,bool)')) ^ - * bytes4(keccak256('isApprovedForAll(address,address)')) ^ - * bytes4(keccak256('transferFrom(address,address,uint256)')) ^ - * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^ - * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) +contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { + + using SafeMath for uint256; + using Address for address; + + // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` + // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` + bytes4 private constant ERC721_RECEIVED = 0x150b7a02; + + // Mapping from token ID to owner + mapping (uint256 => address) internal tokenOwner; + + // Mapping from token ID to approved address + mapping (uint256 => address) internal tokenApprovals; + + // Mapping from owner to number of owned token + mapping (address => uint256) internal ownedTokensCount; + + // Mapping from owner to operator approvals + mapping (address => mapping (address => bool)) internal operatorApprovals; + + constructor() + public + { + // register the supported interfaces to conform to ERC721 via ERC165 + _registerInterface(InterfaceId_ERC721); + } + + /** + * @dev Gets the balance of the specified address + * @param _owner address to query the balance of + * @return uint256 representing the amount owned by the passed address + */ + function balanceOf(address _owner) public view returns (uint256) { + require(_owner != address(0)); + return ownedTokensCount[_owner]; + } + + /** + * @dev Gets the owner of the specified token ID + * @param _tokenId uint256 ID of the token to query the owner of + * @return owner address currently marked as the owner of the given token ID + */ + function ownerOf(uint256 _tokenId) public view returns (address) { + address owner = tokenOwner[_tokenId]; + require(owner != address(0)); + return owner; + } + + /** + * @dev Approves another address to transfer the given token ID + * The zero address indicates there is no approved address. + * There can only be one approved address per token at a given time. + * Can only be called by the token owner or an approved operator. + * @param _to address to be approved for the given token ID + * @param _tokenId uint256 ID of the token to be approved + */ + function approve(address _to, uint256 _tokenId) public { + address owner = ownerOf(_tokenId); + require(_to != owner); + require(msg.sender == owner || isApprovedForAll(owner, msg.sender)); + + tokenApprovals[_tokenId] = _to; + emit Approval(owner, _to, _tokenId); + } + + /** + * @dev Gets the approved address for a token ID, or zero if no address set + * @param _tokenId uint256 ID of the token to query the approval of + * @return address currently approved for the given token ID */ + function getApproved(uint256 _tokenId) public view returns (address) { + return tokenApprovals[_tokenId]; + } - bytes4 internal constant InterfaceId_ERC721Enumerable = 0x780e9d63; /** - * 0x780e9d63 === - * bytes4(keccak256('totalSupply()')) ^ - * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^ - * bytes4(keccak256('tokenByIndex(uint256)')) + * @dev Sets or unsets the approval of a given operator + * An operator is allowed to transfer all tokens of the sender on their behalf + * @param _to operator address to set the approval + * @param _approved representing the status of the approval to be set */ + function setApprovalForAll(address _to, bool _approved) public { + require(_to != msg.sender); + operatorApprovals[msg.sender][_to] = _approved; + emit ApprovalForAll(msg.sender, _to, _approved); + } - bytes4 internal constant InterfaceId_ERC721Metadata = 0x5b5e139f; /** - * 0x5b5e139f === - * bytes4(keccak256('name()')) ^ - * bytes4(keccak256('symbol()')) ^ - * bytes4(keccak256('tokenURI(uint256)')) + * @dev Tells whether an operator is approved by a given owner + * @param _owner owner address which you want to query the approval of + * @param _operator operator address which you want to query the approval of + * @return bool whether the given operator is approved by the given owner */ + function isApprovedForAll( + address _owner, + address _operator + ) + public + view + returns (bool) + { + return operatorApprovals[_owner][_operator]; + } + + /** + * @dev Transfers the ownership of a given token ID to another address + * Usage of this method is discouraged, use `safeTransferFrom` whenever possible + * Requires the msg sender to be the owner, approved, or operator + * @param _from current owner of the token + * @param _to address to receive the ownership of the given token ID + * @param _tokenId uint256 ID of the token to be transferred + */ + function transferFrom( + address _from, + address _to, + uint256 _tokenId + ) + public + { + require(_isApprovedOrOwner(msg.sender, _tokenId)); + require(_to != address(0)); + + _clearApproval(_from, _tokenId); + _removeTokenFrom(_from, _tokenId); + _addTokenTo(_to, _tokenId); - event Transfer( - address indexed _from, - address indexed _to, - uint256 indexed _tokenId - ); - event Approval( - address indexed _owner, - address indexed _approved, - uint256 indexed _tokenId - ); - event ApprovalForAll( - address indexed _owner, - address indexed _operator, - bool _approved - ); - - function balanceOf(address _owner) public view returns (uint256 _balance); - function ownerOf(uint256 _tokenId) public view returns (address _owner); - - function approve(address _to, uint256 _tokenId) public; - function getApproved(uint256 _tokenId) - public view returns (address _operator); - - function setApprovalForAll(address _operator, bool _approved) public; - function isApprovedForAll(address _owner, address _operator) - public view returns (bool); - - function transferFrom(address _from, address _to, uint256 _tokenId) public; - function safeTransferFrom(address _from, address _to, uint256 _tokenId) - public; + emit Transfer(_from, _to, _tokenId); + } + + /** + * @dev Safely transfers the ownership of a given token ID to another address + * If the target address is a contract, it must implement `onERC721Received`, + * which is called upon a safe transfer, and return the magic value + * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, + * the transfer is reverted. + * + * Requires the msg sender to be the owner, approved, or operator + * @param _from current owner of the token + * @param _to address to receive the ownership of the given token ID + * @param _tokenId uint256 ID of the token to be transferred + */ + function safeTransferFrom( + address _from, + address _to, + uint256 _tokenId + ) + public + { + // solium-disable-next-line arg-overflow + safeTransferFrom(_from, _to, _tokenId, ""); + } + /** + * @dev Safely transfers the ownership of a given token ID to another address + * If the target address is a contract, it must implement `onERC721Received`, + * which is called upon a safe transfer, and return the magic value + * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, + * the transfer is reverted. + * Requires the msg sender to be the owner, approved, or operator + * @param _from current owner of the token + * @param _to address to receive the ownership of the given token ID + * @param _tokenId uint256 ID of the token to be transferred + * @param _data bytes data to send along with a safe transfer check + */ function safeTransferFrom( address _from, address _to, uint256 _tokenId, bytes _data ) - public; + public + { + transferFrom(_from, _to, _tokenId); + // solium-disable-next-line arg-overflow + require(_checkAndCallSafeTransfer(_from, _to, _tokenId, _data)); + } + + /** + * @dev Returns whether the specified token exists + * @param _tokenId uint256 ID of the token to query the existence of + * @return whether the token exists + */ + function _exists(uint256 _tokenId) internal view returns (bool) { + address owner = tokenOwner[_tokenId]; + return owner != address(0); + } + + /** + * @dev Returns whether the given spender can transfer a given token ID + * @param _spender address of the spender to query + * @param _tokenId uint256 ID of the token to be transferred + * @return bool whether the msg.sender is approved for the given token ID, + * is an operator of the owner, or is the owner of the token + */ + function _isApprovedOrOwner( + address _spender, + uint256 _tokenId + ) + internal + view + returns (bool) + { + address owner = ownerOf(_tokenId); + // Disable solium check because of + // https://github.com/duaraghav8/Solium/issues/175 + // solium-disable-next-line operator-whitespace + return ( + _spender == owner || + getApproved(_tokenId) == _spender || + isApprovedForAll(owner, _spender) + ); + } + + /** + * @dev Internal function to mint a new token + * Reverts if the given token ID already exists + * @param _to The address that will own the minted token + * @param _tokenId uint256 ID of the token to be minted by the msg.sender + */ + function _mint(address _to, uint256 _tokenId) internal { + require(_to != address(0)); + _addTokenTo(_to, _tokenId); + emit Transfer(address(0), _to, _tokenId); + } + + /** + * @dev Internal function to burn a specific token + * Reverts if the token does not exist + * @param _tokenId uint256 ID of the token being burned by the msg.sender + */ + function _burn(address _owner, uint256 _tokenId) internal { + _clearApproval(_owner, _tokenId); + _removeTokenFrom(_owner, _tokenId); + emit Transfer(_owner, address(0), _tokenId); + } + + /** + * @dev Internal function to clear current approval of a given token ID + * Reverts if the given address is not indeed the owner of the token + * @param _owner owner of the token + * @param _tokenId uint256 ID of the token to be transferred + */ + function _clearApproval(address _owner, uint256 _tokenId) internal { + require(ownerOf(_tokenId) == _owner); + if (tokenApprovals[_tokenId] != address(0)) { + tokenApprovals[_tokenId] = address(0); + } + } + + /** + * @dev Internal function to add a token ID to the list of a given address + * @param _to address representing the new owner of the given token ID + * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address + */ + function _addTokenTo(address _to, uint256 _tokenId) internal { + require(tokenOwner[_tokenId] == address(0)); + tokenOwner[_tokenId] = _to; + ownedTokensCount[_to] = ownedTokensCount[_to].add(1); + } + + /** + * @dev Internal function to remove a token ID from the list of a given address + * @param _from address representing the previous owner of the given token ID + * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address + */ + function _removeTokenFrom(address _from, uint256 _tokenId) internal { + require(ownerOf(_tokenId) == _from); + ownedTokensCount[_from] = ownedTokensCount[_from].sub(1); + tokenOwner[_tokenId] = address(0); + } + + /** + * @dev Internal function to invoke `onERC721Received` on a target address + * The call is not executed if the target address is not a contract + * @param _from address representing the previous owner of the given token ID + * @param _to target address that will receive the tokens + * @param _tokenId uint256 ID of the token to be transferred + * @param _data bytes optional data to send along with the call + * @return whether the call correctly returned the expected magic value + */ + function _checkAndCallSafeTransfer( + address _from, + address _to, + uint256 _tokenId, + bytes _data + ) + internal + returns (bool) + { + if (!_to.isContract()) { + return true; + } + bytes4 retval = IERC721Receiver(_to).onERC721Received( + msg.sender, _from, _tokenId, _data); + return (retval == ERC721_RECEIVED); + } } diff --git a/contracts/token/ERC721/ERC721BasicToken.sol b/contracts/token/ERC721/ERC721BasicToken.sol deleted file mode 100644 index 568be300cbc..00000000000 --- a/contracts/token/ERC721/ERC721BasicToken.sol +++ /dev/null @@ -1,310 +0,0 @@ -pragma solidity ^0.4.24; - -import "./ERC721Basic.sol"; -import "./ERC721Receiver.sol"; -import "../../math/SafeMath.sol"; -import "../../AddressUtils.sol"; -import "../../introspection/SupportsInterfaceWithLookup.sol"; - - -/** - * @title ERC721 Non-Fungible Token Standard basic implementation - * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md - */ -contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic { - - using SafeMath for uint256; - using AddressUtils for address; - - // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` - // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector` - bytes4 private constant ERC721_RECEIVED = 0x150b7a02; - - // Mapping from token ID to owner - mapping (uint256 => address) internal tokenOwner; - - // Mapping from token ID to approved address - mapping (uint256 => address) internal tokenApprovals; - - // Mapping from owner to number of owned token - mapping (address => uint256) internal ownedTokensCount; - - // Mapping from owner to operator approvals - mapping (address => mapping (address => bool)) internal operatorApprovals; - - constructor() - public - { - // register the supported interfaces to conform to ERC721 via ERC165 - _registerInterface(InterfaceId_ERC721); - } - - /** - * @dev Gets the balance of the specified address - * @param _owner address to query the balance of - * @return uint256 representing the amount owned by the passed address - */ - function balanceOf(address _owner) public view returns (uint256) { - require(_owner != address(0)); - return ownedTokensCount[_owner]; - } - - /** - * @dev Gets the owner of the specified token ID - * @param _tokenId uint256 ID of the token to query the owner of - * @return owner address currently marked as the owner of the given token ID - */ - function ownerOf(uint256 _tokenId) public view returns (address) { - address owner = tokenOwner[_tokenId]; - require(owner != address(0)); - return owner; - } - - /** - * @dev Approves another address to transfer the given token ID - * The zero address indicates there is no approved address. - * There can only be one approved address per token at a given time. - * Can only be called by the token owner or an approved operator. - * @param _to address to be approved for the given token ID - * @param _tokenId uint256 ID of the token to be approved - */ - function approve(address _to, uint256 _tokenId) public { - address owner = ownerOf(_tokenId); - require(_to != owner); - require(msg.sender == owner || isApprovedForAll(owner, msg.sender)); - - tokenApprovals[_tokenId] = _to; - emit Approval(owner, _to, _tokenId); - } - - /** - * @dev Gets the approved address for a token ID, or zero if no address set - * @param _tokenId uint256 ID of the token to query the approval of - * @return address currently approved for the given token ID - */ - function getApproved(uint256 _tokenId) public view returns (address) { - return tokenApprovals[_tokenId]; - } - - /** - * @dev Sets or unsets the approval of a given operator - * An operator is allowed to transfer all tokens of the sender on their behalf - * @param _to operator address to set the approval - * @param _approved representing the status of the approval to be set - */ - function setApprovalForAll(address _to, bool _approved) public { - require(_to != msg.sender); - operatorApprovals[msg.sender][_to] = _approved; - emit ApprovalForAll(msg.sender, _to, _approved); - } - - /** - * @dev Tells whether an operator is approved by a given owner - * @param _owner owner address which you want to query the approval of - * @param _operator operator address which you want to query the approval of - * @return bool whether the given operator is approved by the given owner - */ - function isApprovedForAll( - address _owner, - address _operator - ) - public - view - returns (bool) - { - return operatorApprovals[_owner][_operator]; - } - - /** - * @dev Transfers the ownership of a given token ID to another address - * Usage of this method is discouraged, use `safeTransferFrom` whenever possible - * Requires the msg sender to be the owner, approved, or operator - * @param _from current owner of the token - * @param _to address to receive the ownership of the given token ID - * @param _tokenId uint256 ID of the token to be transferred - */ - function transferFrom( - address _from, - address _to, - uint256 _tokenId - ) - public - { - require(isApprovedOrOwner(msg.sender, _tokenId)); - require(_to != address(0)); - - clearApproval(_from, _tokenId); - removeTokenFrom(_from, _tokenId); - addTokenTo(_to, _tokenId); - - emit Transfer(_from, _to, _tokenId); - } - - /** - * @dev Safely transfers the ownership of a given token ID to another address - * If the target address is a contract, it must implement `onERC721Received`, - * which is called upon a safe transfer, and return the magic value - * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, - * the transfer is reverted. - * - * Requires the msg sender to be the owner, approved, or operator - * @param _from current owner of the token - * @param _to address to receive the ownership of the given token ID - * @param _tokenId uint256 ID of the token to be transferred - */ - function safeTransferFrom( - address _from, - address _to, - uint256 _tokenId - ) - public - { - // solium-disable-next-line arg-overflow - safeTransferFrom(_from, _to, _tokenId, ""); - } - - /** - * @dev Safely transfers the ownership of a given token ID to another address - * If the target address is a contract, it must implement `onERC721Received`, - * which is called upon a safe transfer, and return the magic value - * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, - * the transfer is reverted. - * Requires the msg sender to be the owner, approved, or operator - * @param _from current owner of the token - * @param _to address to receive the ownership of the given token ID - * @param _tokenId uint256 ID of the token to be transferred - * @param _data bytes data to send along with a safe transfer check - */ - function safeTransferFrom( - address _from, - address _to, - uint256 _tokenId, - bytes _data - ) - public - { - transferFrom(_from, _to, _tokenId); - // solium-disable-next-line arg-overflow - require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data)); - } - - /** - * @dev Returns whether the specified token exists - * @param _tokenId uint256 ID of the token to query the existence of - * @return whether the token exists - */ - function _exists(uint256 _tokenId) internal view returns (bool) { - address owner = tokenOwner[_tokenId]; - return owner != address(0); - } - - /** - * @dev Returns whether the given spender can transfer a given token ID - * @param _spender address of the spender to query - * @param _tokenId uint256 ID of the token to be transferred - * @return bool whether the msg.sender is approved for the given token ID, - * is an operator of the owner, or is the owner of the token - */ - function isApprovedOrOwner( - address _spender, - uint256 _tokenId - ) - internal - view - returns (bool) - { - address owner = ownerOf(_tokenId); - // Disable solium check because of - // https://github.com/duaraghav8/Solium/issues/175 - // solium-disable-next-line operator-whitespace - return ( - _spender == owner || - getApproved(_tokenId) == _spender || - isApprovedForAll(owner, _spender) - ); - } - - /** - * @dev Internal function to mint a new token - * Reverts if the given token ID already exists - * @param _to The address that will own the minted token - * @param _tokenId uint256 ID of the token to be minted by the msg.sender - */ - function _mint(address _to, uint256 _tokenId) internal { - require(_to != address(0)); - addTokenTo(_to, _tokenId); - emit Transfer(address(0), _to, _tokenId); - } - - /** - * @dev Internal function to burn a specific token - * Reverts if the token does not exist - * @param _tokenId uint256 ID of the token being burned by the msg.sender - */ - function _burn(address _owner, uint256 _tokenId) internal { - clearApproval(_owner, _tokenId); - removeTokenFrom(_owner, _tokenId); - emit Transfer(_owner, address(0), _tokenId); - } - - /** - * @dev Internal function to clear current approval of a given token ID - * Reverts if the given address is not indeed the owner of the token - * @param _owner owner of the token - * @param _tokenId uint256 ID of the token to be transferred - */ - function clearApproval(address _owner, uint256 _tokenId) internal { - require(ownerOf(_tokenId) == _owner); - if (tokenApprovals[_tokenId] != address(0)) { - tokenApprovals[_tokenId] = address(0); - } - } - - /** - * @dev Internal function to add a token ID to the list of a given address - * @param _to address representing the new owner of the given token ID - * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address - */ - function addTokenTo(address _to, uint256 _tokenId) internal { - require(tokenOwner[_tokenId] == address(0)); - tokenOwner[_tokenId] = _to; - ownedTokensCount[_to] = ownedTokensCount[_to].add(1); - } - - /** - * @dev Internal function to remove a token ID from the list of a given address - * @param _from address representing the previous owner of the given token ID - * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address - */ - function removeTokenFrom(address _from, uint256 _tokenId) internal { - require(ownerOf(_tokenId) == _from); - ownedTokensCount[_from] = ownedTokensCount[_from].sub(1); - tokenOwner[_tokenId] = address(0); - } - - /** - * @dev Internal function to invoke `onERC721Received` on a target address - * The call is not executed if the target address is not a contract - * @param _from address representing the previous owner of the given token ID - * @param _to target address that will receive the tokens - * @param _tokenId uint256 ID of the token to be transferred - * @param _data bytes optional data to send along with the call - * @return whether the call correctly returned the expected magic value - */ - function checkAndCallSafeTransfer( - address _from, - address _to, - uint256 _tokenId, - bytes _data - ) - internal - returns (bool) - { - if (!_to.isContract()) { - return true; - } - bytes4 retval = ERC721Receiver(_to).onERC721Received( - msg.sender, _from, _tokenId, _data); - return (retval == ERC721_RECEIVED); - } -} diff --git a/contracts/token/ERC721/ERC721Holder.sol b/contracts/token/ERC721/ERC721Holder.sol index 0f9299c6cc7..332061339e7 100644 --- a/contracts/token/ERC721/ERC721Holder.sol +++ b/contracts/token/ERC721/ERC721Holder.sol @@ -1,9 +1,9 @@ pragma solidity ^0.4.24; -import "./ERC721Receiver.sol"; +import "./IERC721Receiver.sol"; -contract ERC721Holder is ERC721Receiver { +contract ERC721Holder is IERC721Receiver { function onERC721Received( address, address, diff --git a/contracts/token/ERC721/ERC721Pausable.sol b/contracts/token/ERC721/ERC721Pausable.sol new file mode 100644 index 00000000000..4604597d923 --- /dev/null +++ b/contracts/token/ERC721/ERC721Pausable.sol @@ -0,0 +1,42 @@ +pragma solidity ^0.4.24; + +import "./ERC721Basic.sol"; +import "../../lifecycle/Pausable.sol"; + + +/** + * @title ERC721 Non-Fungible Pausable token + * @dev ERC721Basic modified with pausable transfers. + **/ +contract ERC721Pausable is ERC721Basic, Pausable { + function approve( + address _to, + uint256 _tokenId + ) + public + whenNotPaused + { + super.approve(_to, _tokenId); + } + + function setApprovalForAll( + address _to, + bool _approved + ) + public + whenNotPaused + { + super.setApprovalForAll(_to, _approved); + } + + function transferFrom( + address _from, + address _to, + uint256 _tokenId + ) + public + whenNotPaused + { + super.transferFrom(_from, _to, _tokenId); + } +} diff --git a/contracts/token/ERC721/ERC721Token.sol b/contracts/token/ERC721/ERC721Token.sol deleted file mode 100644 index af610fa245a..00000000000 --- a/contracts/token/ERC721/ERC721Token.sol +++ /dev/null @@ -1,201 +0,0 @@ -pragma solidity ^0.4.24; - -import "./ERC721.sol"; -import "./ERC721BasicToken.sol"; -import "../../introspection/SupportsInterfaceWithLookup.sol"; - - -/** - * @title Full ERC721 Token - * This implementation includes all the required and some optional functionality of the ERC721 standard - * Moreover, it includes approve all functionality using operator terminology - * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md - */ -contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 { - - // Token name - string internal name_; - - // Token symbol - string internal symbol_; - - // Mapping from owner to list of owned token IDs - mapping(address => uint256[]) internal ownedTokens; - - // Mapping from token ID to index of the owner tokens list - mapping(uint256 => uint256) internal ownedTokensIndex; - - // Array with all token ids, used for enumeration - uint256[] internal allTokens; - - // Mapping from token id to position in the allTokens array - mapping(uint256 => uint256) internal allTokensIndex; - - // Optional mapping for token URIs - mapping(uint256 => string) internal tokenURIs; - - /** - * @dev Constructor function - */ - constructor(string _name, string _symbol) public { - name_ = _name; - symbol_ = _symbol; - - // register the supported interfaces to conform to ERC721 via ERC165 - _registerInterface(InterfaceId_ERC721Enumerable); - _registerInterface(InterfaceId_ERC721Metadata); - } - - /** - * @dev Gets the token name - * @return string representing the token name - */ - function name() external view returns (string) { - return name_; - } - - /** - * @dev Gets the token symbol - * @return string representing the token symbol - */ - function symbol() external view returns (string) { - return symbol_; - } - - /** - * @dev Returns an URI for a given token ID - * Throws if the token ID does not exist. May return an empty string. - * @param _tokenId uint256 ID of the token to query - */ - function tokenURI(uint256 _tokenId) public view returns (string) { - require(_exists(_tokenId)); - return tokenURIs[_tokenId]; - } - - /** - * @dev Gets the token ID at a given index of the tokens list of the requested owner - * @param _owner address owning the tokens list to be accessed - * @param _index uint256 representing the index to be accessed of the requested tokens list - * @return uint256 token ID at the given index of the tokens list owned by the requested address - */ - function tokenOfOwnerByIndex( - address _owner, - uint256 _index - ) - public - view - returns (uint256) - { - require(_index < balanceOf(_owner)); - return ownedTokens[_owner][_index]; - } - - /** - * @dev Gets the total amount of tokens stored by the contract - * @return uint256 representing the total amount of tokens - */ - function totalSupply() public view returns (uint256) { - return allTokens.length; - } - - /** - * @dev Gets the token ID at a given index of all the tokens in this contract - * Reverts if the index is greater or equal to the total number of tokens - * @param _index uint256 representing the index to be accessed of the tokens list - * @return uint256 token ID at the given index of the tokens list - */ - function tokenByIndex(uint256 _index) public view returns (uint256) { - require(_index < totalSupply()); - return allTokens[_index]; - } - - /** - * @dev Internal function to set the token URI for a given token - * Reverts if the token ID does not exist - * @param _tokenId uint256 ID of the token to set its URI - * @param _uri string URI to assign - */ - function _setTokenURI(uint256 _tokenId, string _uri) internal { - require(_exists(_tokenId)); - tokenURIs[_tokenId] = _uri; - } - - /** - * @dev Internal function to add a token ID to the list of a given address - * @param _to address representing the new owner of the given token ID - * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address - */ - function addTokenTo(address _to, uint256 _tokenId) internal { - super.addTokenTo(_to, _tokenId); - uint256 length = ownedTokens[_to].length; - ownedTokens[_to].push(_tokenId); - ownedTokensIndex[_tokenId] = length; - } - - /** - * @dev Internal function to remove a token ID from the list of a given address - * @param _from address representing the previous owner of the given token ID - * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address - */ - function removeTokenFrom(address _from, uint256 _tokenId) internal { - super.removeTokenFrom(_from, _tokenId); - - // To prevent a gap in the array, we store the last token in the index of the token to delete, and - // then delete the last slot. - uint256 tokenIndex = ownedTokensIndex[_tokenId]; - uint256 lastTokenIndex = ownedTokens[_from].length.sub(1); - uint256 lastToken = ownedTokens[_from][lastTokenIndex]; - - ownedTokens[_from][tokenIndex] = lastToken; - // This also deletes the contents at the last position of the array - ownedTokens[_from].length--; - - // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to - // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping - // the lastToken to the first position, and then dropping the element placed in the last position of the list - - ownedTokensIndex[_tokenId] = 0; - ownedTokensIndex[lastToken] = tokenIndex; - } - - /** - * @dev Internal function to mint a new token - * Reverts if the given token ID already exists - * @param _to address the beneficiary that will own the minted token - * @param _tokenId uint256 ID of the token to be minted by the msg.sender - */ - function _mint(address _to, uint256 _tokenId) internal { - super._mint(_to, _tokenId); - - allTokensIndex[_tokenId] = allTokens.length; - allTokens.push(_tokenId); - } - - /** - * @dev Internal function to burn a specific token - * Reverts if the token does not exist - * @param _owner owner of the token to burn - * @param _tokenId uint256 ID of the token being burned by the msg.sender - */ - function _burn(address _owner, uint256 _tokenId) internal { - super._burn(_owner, _tokenId); - - // Clear metadata (if any) - if (bytes(tokenURIs[_tokenId]).length != 0) { - delete tokenURIs[_tokenId]; - } - - // Reorg all tokens array - uint256 tokenIndex = allTokensIndex[_tokenId]; - uint256 lastTokenIndex = allTokens.length.sub(1); - uint256 lastToken = allTokens[lastTokenIndex]; - - allTokens[tokenIndex] = lastToken; - allTokens[lastTokenIndex] = 0; - - allTokens.length--; - allTokensIndex[_tokenId] = 0; - allTokensIndex[lastToken] = tokenIndex; - } - -} diff --git a/contracts/token/ERC721/IERC721.sol b/contracts/token/ERC721/IERC721.sol new file mode 100644 index 00000000000..24b7e01ceeb --- /dev/null +++ b/contracts/token/ERC721/IERC721.sol @@ -0,0 +1,40 @@ +pragma solidity ^0.4.24; + +import "./IERC721Basic.sol"; + + +/** + * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension + * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md + */ +contract IERC721Enumerable is IERC721Basic { + function totalSupply() public view returns (uint256); + function tokenOfOwnerByIndex( + address _owner, + uint256 _index + ) + public + view + returns (uint256 _tokenId); + + function tokenByIndex(uint256 _index) public view returns (uint256); +} + + +/** + * @title ERC-721 Non-Fungible Token Standard, optional metadata extension + * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md + */ +contract IERC721Metadata is IERC721Basic { + function name() external view returns (string _name); + function symbol() external view returns (string _symbol); + function tokenURI(uint256 _tokenId) public view returns (string); +} + + +/** + * @title ERC-721 Non-Fungible Token Standard, full implementation interface + * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md + */ +contract IERC721 is IERC721Basic, IERC721Enumerable, IERC721Metadata { +} diff --git a/contracts/token/ERC721/IERC721Basic.sol b/contracts/token/ERC721/IERC721Basic.sol new file mode 100644 index 00000000000..50a5c7f5359 --- /dev/null +++ b/contracts/token/ERC721/IERC721Basic.sol @@ -0,0 +1,80 @@ +pragma solidity ^0.4.24; + +import "../../introspection/IERC165.sol"; + + +/** + * @title ERC721 Non-Fungible Token Standard basic interface + * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md + */ +contract IERC721Basic is IERC165 { + + bytes4 internal constant InterfaceId_ERC721 = 0x80ac58cd; + /* + * 0x80ac58cd === + * bytes4(keccak256('balanceOf(address)')) ^ + * bytes4(keccak256('ownerOf(uint256)')) ^ + * bytes4(keccak256('approve(address,uint256)')) ^ + * bytes4(keccak256('getApproved(uint256)')) ^ + * bytes4(keccak256('setApprovalForAll(address,bool)')) ^ + * bytes4(keccak256('isApprovedForAll(address,address)')) ^ + * bytes4(keccak256('transferFrom(address,address,uint256)')) ^ + * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^ + * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) + */ + + bytes4 internal constant InterfaceId_ERC721Enumerable = 0x780e9d63; + /** + * 0x780e9d63 === + * bytes4(keccak256('totalSupply()')) ^ + * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^ + * bytes4(keccak256('tokenByIndex(uint256)')) + */ + + bytes4 internal constant InterfaceId_ERC721Metadata = 0x5b5e139f; + /** + * 0x5b5e139f === + * bytes4(keccak256('name()')) ^ + * bytes4(keccak256('symbol()')) ^ + * bytes4(keccak256('tokenURI(uint256)')) + */ + + event Transfer( + address indexed from, + address indexed to, + uint256 indexed tokenId + ); + event Approval( + address indexed owner, + address indexed approved, + uint256 indexed tokenId + ); + event ApprovalForAll( + address indexed owner, + address indexed operator, + bool approved + ); + + function balanceOf(address _owner) public view returns (uint256 _balance); + function ownerOf(uint256 _tokenId) public view returns (address _owner); + + function approve(address _to, uint256 _tokenId) public; + function getApproved(uint256 _tokenId) + public view returns (address _operator); + + function setApprovalForAll(address _operator, bool _approved) public; + function isApprovedForAll(address _owner, address _operator) + public view returns (bool); + + function transferFrom(address _from, address _to, uint256 _tokenId) public; + function safeTransferFrom(address _from, address _to, uint256 _tokenId) + public; + + function safeTransferFrom( + address _from, + address _to, + uint256 _tokenId, + bytes _data + ) + public; +} diff --git a/contracts/token/ERC721/ERC721Receiver.sol b/contracts/token/ERC721/IERC721Receiver.sol similarity index 92% rename from contracts/token/ERC721/ERC721Receiver.sol rename to contracts/token/ERC721/IERC721Receiver.sol index 45440149a66..b4c228d88f1 100644 --- a/contracts/token/ERC721/ERC721Receiver.sol +++ b/contracts/token/ERC721/IERC721Receiver.sol @@ -6,11 +6,11 @@ pragma solidity ^0.4.24; * @dev Interface for any contract that wants to support safeTransfers * from ERC721 asset contracts. */ -contract ERC721Receiver { +contract IERC721Receiver { /** * @dev Magic value to be returned upon successful reception of an NFT * Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`, - * which can be also obtained as `ERC721Receiver(0).onERC721Received.selector` + * which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` */ bytes4 internal constant ERC721_RECEIVED = 0x150b7a02; diff --git a/contracts/AddressUtils.sol b/contracts/utils/Address.sol similarity index 97% rename from contracts/AddressUtils.sol rename to contracts/utils/Address.sol index b07447d389f..7c972200518 100644 --- a/contracts/AddressUtils.sol +++ b/contracts/utils/Address.sol @@ -4,7 +4,7 @@ pragma solidity ^0.4.24; /** * Utility library of inline functions on addresses */ -library AddressUtils { +library Address { /** * Returns whether the target address is a contract diff --git a/contracts/AutoIncrementing.sol b/contracts/utils/AutoIncrementing.sol similarity index 89% rename from contracts/AutoIncrementing.sol rename to contracts/utils/AutoIncrementing.sol index cf46e2f57fc..8c01861fb68 100644 --- a/contracts/AutoIncrementing.sol +++ b/contracts/utils/AutoIncrementing.sol @@ -5,7 +5,7 @@ pragma solidity ^0.4.24; * @title AutoIncrementing * @author Matt Condon (@shrugs) * @dev Provides an auto-incrementing uint256 id acquired by the `Counter#nextId` getter. - * Use this for issuing ERC721Token ids or keeping track of request ids, anything you want, really. + * Use this for issuing ERC721 ids or keeping track of request ids, anything you want, really. * * Include with `using AutoIncrementing for AutoIncrementing.Counter;` * @notice Does not allow an Id of 0, which is popularly used to signify a null state in solidity. diff --git a/contracts/ReentrancyGuard.sol b/contracts/utils/ReentrancyGuard.sol similarity index 100% rename from contracts/ReentrancyGuard.sol rename to contracts/utils/ReentrancyGuard.sol diff --git a/package-lock.json b/package-lock.json index fa88ffc4ce0..3fa9ab04e48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4655,23 +4655,21 @@ "dev": true, "optional": true, "requires": { - "delegates": "1.0.0", - "readable-stream": "2.3.6" + "delegates": "^1.0.0", + "readable-stream": "^2.0.6" } }, "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { - "balanced-match": "1.0.0", + "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, @@ -4684,20 +4682,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -4738,7 +4733,7 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "fs.realpath": { @@ -4753,14 +4748,14 @@ "dev": true, "optional": true, "requires": { - "aproba": "1.2.0", - "console-control-strings": "1.1.0", - "has-unicode": "2.0.1", - "object-assign": "4.1.1", - "signal-exit": "3.0.2", - "string-width": "1.0.2", - "strip-ansi": "3.0.1", - "wide-align": "1.1.2" + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" } }, "glob": { @@ -4769,12 +4764,12 @@ "dev": true, "optional": true, "requires": { - "fs.realpath": "1.0.0", - "inflight": "1.0.6", - "inherits": "2.0.3", - "minimatch": "3.0.4", - "once": "1.4.0", - "path-is-absolute": "1.0.1" + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" } }, "has-unicode": { @@ -4789,7 +4784,7 @@ "dev": true, "optional": true, "requires": { - "safer-buffer": "2.1.2" + "safer-buffer": "^2.1.0" } }, "ignore-walk": { @@ -4798,7 +4793,7 @@ "dev": true, "optional": true, "requires": { - "minimatch": "3.0.4" + "minimatch": "^3.0.4" } }, "inflight": { @@ -4807,15 +4802,14 @@ "dev": true, "optional": true, "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" + "once": "^1.3.0", + "wrappy": "1" } }, "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -4827,9 +4821,8 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { - "number-is-nan": "1.0.1" + "number-is-nan": "^1.0.0" } }, "isarray": { @@ -4842,25 +4835,22 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { - "brace-expansion": "1.1.11" + "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "safe-buffer": "^5.1.1", + "yallist": "^3.0.0" } }, "minizlib": { @@ -4869,14 +4859,13 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "mkdirp": { "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4893,9 +4882,9 @@ "dev": true, "optional": true, "requires": { - "debug": "2.6.9", - "iconv-lite": "0.4.21", - "sax": "1.2.4" + "debug": "^2.1.2", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" } }, "node-pre-gyp": { @@ -4904,16 +4893,16 @@ "dev": true, "optional": true, "requires": { - "detect-libc": "1.0.3", - "mkdirp": "0.5.1", - "needle": "2.2.0", - "nopt": "4.0.1", - "npm-packlist": "1.1.10", - "npmlog": "4.1.2", - "rc": "1.2.7", - "rimraf": "2.6.2", - "semver": "5.5.0", - "tar": "4.4.1" + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.0", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.1.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4" } }, "nopt": { @@ -4922,8 +4911,8 @@ "dev": true, "optional": true, "requires": { - "abbrev": "1.1.1", - "osenv": "0.1.5" + "abbrev": "1", + "osenv": "^0.1.4" } }, "npm-bundled": { @@ -4938,8 +4927,8 @@ "dev": true, "optional": true, "requires": { - "ignore-walk": "3.0.1", - "npm-bundled": "1.0.3" + "ignore-walk": "^3.0.1", + "npm-bundled": "^1.0.1" } }, "npmlog": { @@ -4948,17 +4937,16 @@ "dev": true, "optional": true, "requires": { - "are-we-there-yet": "1.1.4", - "console-control-strings": "1.1.0", - "gauge": "2.7.4", - "set-blocking": "2.0.0" + "are-we-there-yet": "~1.1.2", + "console-control-strings": "~1.1.0", + "gauge": "~2.7.3", + "set-blocking": "~2.0.0" } }, "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -4970,9 +4958,8 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { - "wrappy": "1.0.2" + "wrappy": "1" } }, "os-homedir": { @@ -4993,8 +4980,8 @@ "dev": true, "optional": true, "requires": { - "os-homedir": "1.0.2", - "os-tmpdir": "1.0.2" + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" } }, "path-is-absolute": { @@ -5015,10 +5002,10 @@ "dev": true, "optional": true, "requires": { - "deep-extend": "0.5.1", - "ini": "1.3.5", - "minimist": "1.2.0", - "strip-json-comments": "2.0.1" + "deep-extend": "^0.5.1", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" }, "dependencies": { "minimist": { @@ -5035,13 +5022,13 @@ "dev": true, "optional": true, "requires": { - "core-util-is": "1.0.2", - "inherits": "2.0.3", - "isarray": "1.0.0", - "process-nextick-args": "2.0.0", - "safe-buffer": "5.1.1", - "string_decoder": "1.1.1", - "util-deprecate": "1.0.2" + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" } }, "rimraf": { @@ -5050,7 +5037,7 @@ "dev": true, "optional": true, "requires": { - "glob": "7.1.2" + "glob": "^7.0.5" } }, "safe-buffer": { @@ -5092,11 +5079,10 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { - "code-point-at": "1.1.0", - "is-fullwidth-code-point": "1.0.0", - "strip-ansi": "3.0.1" + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" } }, "string_decoder": { @@ -5105,7 +5091,7 @@ "dev": true, "optional": true, "requires": { - "safe-buffer": "5.1.1" + "safe-buffer": "~5.1.0" } }, "strip-ansi": { @@ -5113,7 +5099,7 @@ "bundled": true, "dev": true, "requires": { - "ansi-regex": "2.1.1" + "ansi-regex": "^2.0.0" } }, "strip-json-comments": { @@ -5128,13 +5114,13 @@ "dev": true, "optional": true, "requires": { - "chownr": "1.0.1", - "fs-minipass": "1.2.5", - "minipass": "2.2.4", - "minizlib": "1.1.0", - "mkdirp": "0.5.1", - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "chownr": "^1.0.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.2.4", + "minizlib": "^1.1.0", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.1", + "yallist": "^3.0.2" } }, "util-deprecate": { @@ -5149,7 +5135,7 @@ "dev": true, "optional": true, "requires": { - "string-width": "1.0.2" + "string-width": "^1.0.2" } }, "wrappy": { diff --git a/package.json b/package.json index 791a0a9107a..b09a1537b0e 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "eslint-plugin-node": "^5.2.1", "eslint-plugin-promise": "^3.8.0", "eslint-plugin-standard": "^3.1.0", - "ethereumjs-util": "^5.2.0", "ethjs-abi": "^0.2.1", "ganache-cli": "6.1.0", "solidity-coverage": "^0.5.4", diff --git a/scripts/ci/trigger_docs_update b/scripts/ci/trigger_docs_update new file mode 100755 index 00000000000..47a6db64280 --- /dev/null +++ b/scripts/ci/trigger_docs_update @@ -0,0 +1,32 @@ +#!/bin/bash +# +# Trigger the job that will update the documentation website. +# Argument: +# version: the version of the new release. This should be a tag in the +# https://github.com/OpenZeppelin/openzeppelin-solidity repository. + +set -ev + +if [ "$#" -lt 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +readonly VERSION="$1" + +readonly BODY="{ + \"request\": { + \"branch\": \"master\", + \"config\": { + \"env\": [\"VERSION=${VERSION}\"] + } + } +}" + +curl -s -X POST \ + -H "Content-Type: application/json" \ + -H "Accept: application/json" \ + -H "Travis-API-Version: 3" \ + -H "Authorization: token ${DOCS_TRAVIS_API_TOKEN}" \ + -d "${BODY}" \ + https://api.travis-ci.com/repo/OpenZeppelin%2Fopenzeppelin-docs/requests diff --git a/test/.eslintrc b/test/.eslintrc deleted file mode 100644 index 9609ab5d01c..00000000000 --- a/test/.eslintrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "rules": { - "no-unused-expressions": 0 - } -} diff --git a/test/AutoIncrementing.test.js b/test/AutoIncrementing.test.js index 5d36094eec9..6b48158300d 100644 --- a/test/AutoIncrementing.test.js +++ b/test/AutoIncrementing.test.js @@ -1,5 +1,3 @@ -const { hashMessage } = require('./helpers/sign'); - const AutoIncrementing = artifacts.require('AutoIncrementingImpl'); require('chai') @@ -7,8 +5,8 @@ require('chai') .should(); const EXPECTED = [1, 2, 3, 4]; -const KEY1 = hashMessage('key1'); -const KEY2 = hashMessage('key2'); +const KEY1 = web3.sha3('key1'); +const KEY2 = web3.sha3('key2'); contract('AutoIncrementing', function ([_, owner]) { beforeEach(async function () { diff --git a/test/Bounty.test.js b/test/BreakInvariantBounty.test.js similarity index 87% rename from test/Bounty.test.js rename to test/BreakInvariantBounty.test.js index 7438ab02266..1f0e36aa9e8 100644 --- a/test/Bounty.test.js +++ b/test/BreakInvariantBounty.test.js @@ -2,8 +2,8 @@ const { ethGetBalance, ethSendTransaction } = require('./helpers/web3'); const expectEvent = require('./helpers/expectEvent'); const { assertRevert } = require('./helpers/assertRevert'); -const SecureTargetBounty = artifacts.require('SecureTargetBounty'); -const InsecureTargetBounty = artifacts.require('InsecureTargetBounty'); +const SecureInvariantTargetBounty = artifacts.require('SecureInvariantTargetBounty'); +const InsecureInvariantTargetBounty = artifacts.require('InsecureInvariantTargetBounty'); require('chai') .use(require('chai-bignumber')(web3.BigNumber)) @@ -17,10 +17,10 @@ const sendReward = async (from, to, value) => ethSendTransaction({ const reward = new web3.BigNumber(web3.toWei(1, 'ether')); -contract('Bounty', function ([_, owner, researcher, nonTarget]) { +contract('BreakInvariantBounty', function ([_, owner, researcher, nonTarget]) { context('against secure contract', function () { beforeEach(async function () { - this.bounty = await SecureTargetBounty.new({ from: owner }); + this.bounty = await SecureInvariantTargetBounty.new({ from: owner }); }); it('can set reward', async function () { @@ -53,7 +53,7 @@ contract('Bounty', function ([_, owner, researcher, nonTarget]) { context('against broken contract', function () { beforeEach(async function () { - this.bounty = await InsecureTargetBounty.new(); + this.bounty = await InsecureInvariantTargetBounty.new(); const result = await this.bounty.createTarget({ from: researcher }); const event = expectEvent.inLogs(result.logs, 'TargetCreated'); @@ -66,7 +66,7 @@ contract('Bounty', function ([_, owner, researcher, nonTarget]) { await this.bounty.claim(this.targetAddress, { from: researcher }); const claim = await this.bounty.claimed(); - claim.should.eq(true); + claim.should.equal(true); const researcherPrevBalance = await ethGetBalance(researcher); diff --git a/test/Heritable.test.js b/test/Heritable.test.js index 631975309fa..77036417905 100644 --- a/test/Heritable.test.js +++ b/test/Heritable.test.js @@ -38,10 +38,10 @@ contract('Heritable', function ([_, owner, heir, anyone]) { it('owner can remove heir', async function () { await heritable.setHeir(heir, { from: owner }); - (await heritable.heir()).should.eq(heir); + (await heritable.heir()).should.equal(heir); await heritable.removeHeir({ from: owner }); - (await heritable.heir()).should.eq(NULL_ADDRESS); + (await heritable.heir()).should.equal(NULL_ADDRESS); }); it('heir can claim ownership only if owner is dead and timeout was reached', async function () { @@ -54,7 +54,7 @@ contract('Heritable', function ([_, owner, heir, anyone]) { await increaseTime(heartbeatTimeout); await heritable.claimHeirOwnership({ from: heir }); - (await heritable.heir()).should.eq(heir); + (await heritable.heir()).should.equal(heir); }); it('only heir can proclaim death', async function () { @@ -85,29 +85,29 @@ contract('Heritable', function ([_, owner, heir, anyone]) { const setHeirLogs = (await heritable.setHeir(heir, { from: owner })).logs; const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged'); - setHeirEvent.args.owner.should.eq(owner); - setHeirEvent.args.newHeir.should.eq(heir); + setHeirEvent.args.owner.should.equal(owner); + setHeirEvent.args.newHeir.should.equal(heir); const heartbeatLogs = (await heritable.heartbeat({ from: owner })).logs; const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated'); - heartbeatEvent.args.owner.should.eq(owner); + heartbeatEvent.args.owner.should.equal(owner); const proclaimDeathLogs = (await heritable.proclaimDeath({ from: heir })).logs; const ownerDeadEvent = proclaimDeathLogs.find(e => e.event === 'OwnerProclaimedDead'); - ownerDeadEvent.args.owner.should.eq(owner); - ownerDeadEvent.args.heir.should.eq(heir); + ownerDeadEvent.args.owner.should.equal(owner); + ownerDeadEvent.args.heir.should.equal(heir); await increaseTime(heartbeatTimeout); const claimHeirOwnershipLogs = (await heritable.claimHeirOwnership({ from: heir })).logs; const ownershipTransferredEvent = claimHeirOwnershipLogs.find(e => e.event === 'OwnershipTransferred'); const heirOwnershipClaimedEvent = claimHeirOwnershipLogs.find(e => e.event === 'HeirOwnershipClaimed'); - ownershipTransferredEvent.args.previousOwner.should.eq(owner); - ownershipTransferredEvent.args.newOwner.should.eq(heir); - heirOwnershipClaimedEvent.args.previousOwner.should.eq(owner); - heirOwnershipClaimedEvent.args.newOwner.should.eq(heir); + ownershipTransferredEvent.args.previousOwner.should.equal(owner); + ownershipTransferredEvent.args.newOwner.should.equal(heir); + heirOwnershipClaimedEvent.args.previousOwner.should.equal(owner); + heirOwnershipClaimedEvent.args.newOwner.should.equal(heir); }); it('timeOfDeath can be queried', async function () { diff --git a/test/LimitBalance.test.js b/test/LimitBalance.test.js deleted file mode 100644 index f096916a9f8..00000000000 --- a/test/LimitBalance.test.js +++ /dev/null @@ -1,59 +0,0 @@ -const { assertRevert } = require('./helpers/assertRevert'); -const { ethGetBalance } = require('./helpers/web3'); - -const LimitBalanceMock = artifacts.require('LimitBalanceMock'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('LimitBalance', function () { - let limitBalance; - - beforeEach(async function () { - limitBalance = await LimitBalanceMock.new(); - }); - - const LIMIT = 1000; - - it('should expose limit', async function () { - const limit = await limitBalance.limit(); - limit.should.be.bignumber.equal(LIMIT); - }); - - it('should allow sending below limit', async function () { - const amount = 1; - await limitBalance.limitedDeposit({ value: amount }); - - const balance = await ethGetBalance(limitBalance.address); - balance.should.be.bignumber.equal(amount); - }); - - it('shouldnt allow sending above limit', async function () { - const amount = 1110; - await assertRevert(limitBalance.limitedDeposit({ value: amount })); - }); - - it('should allow multiple sends below limit', async function () { - const amount = 500; - await limitBalance.limitedDeposit({ value: amount }); - - const balance = await ethGetBalance(limitBalance.address); - balance.should.be.bignumber.equal(amount); - - await limitBalance.limitedDeposit({ value: amount }); - const updatedBalance = await ethGetBalance(limitBalance.address); - updatedBalance.should.be.bignumber.equal(amount * 2); - }); - - it('shouldnt allow multiple sends above limit', async function () { - const amount = 500; - await limitBalance.limitedDeposit({ value: amount }); - - const balance = await ethGetBalance(limitBalance.address); - balance.should.be.bignumber.equal(amount); - await assertRevert(limitBalance.limitedDeposit({ value: amount + 1 })); - }); -}); diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js index 47d734ddf1a..798ab0ac9fa 100644 --- a/test/access/SignatureBouncer.test.js +++ b/test/access/SignatureBouncer.test.js @@ -16,17 +16,16 @@ const INVALID_SIGNATURE = '0xabcd'; contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser]) { beforeEach(async function () { this.bouncer = await Bouncer.new({ from: owner }); - this.roleBouncer = await this.bouncer.ROLE_BOUNCER(); }); context('management', function () { it('has a default owner of self', async function () { - (await this.bouncer.owner()).should.eq(owner); + (await this.bouncer.owner()).should.equal(owner); }); it('allows the owner to add a bouncer', async function () { await this.bouncer.addBouncer(bouncerAddress, { from: owner }); - (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(true); + (await this.bouncer.isBouncer(bouncerAddress)).should.equal(true); }); it('does not allow adding an invalid address', async function () { @@ -39,7 +38,7 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] await this.bouncer.addBouncer(bouncerAddress, { from: owner }); await this.bouncer.removeBouncer(bouncerAddress, { from: owner }); - (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(false); + (await this.bouncer.isBouncer(bouncerAddress)).should.equal(false); }); it('does not allow anyone to add a bouncer', async function () { @@ -168,20 +167,20 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] context('signature validation', function () { context('plain signature', function () { it('validates valid signature for valid user', async function () { - (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.eq(true); + (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true); }); it('does not validate invalid signature for valid user', async function () { - (await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.eq(false); + (await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.equal(false); }); it('does not validate valid signature for anyone', async function () { - (await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.eq(false); + (await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false); }); it('does not validate valid signature for method for valid user', async function () { (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature')) - ).should.eq(false); + ).should.equal(false); }); }); @@ -189,22 +188,22 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] it('validates valid signature with correct method for valid user', async function () { (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) - ).should.eq(true); + ).should.equal(true); }); it('does not validate invalid signature with correct method for valid user', async function () { - (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.eq(false); + (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.equal(false); }); it('does not validate valid signature with correct method for anyone', async function () { (await this.bouncer.checkValidSignatureAndMethod(anyone, this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) - ).should.eq(false); + ).should.equal(false); }); it('does not validate valid non-method signature with correct method for valid user', async function () { (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser)) - ).should.eq(false); + ).should.equal(false); }); }); @@ -212,33 +211,33 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] it('validates valid signature with correct method and data for valid user', async function () { (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.eq(true); + ).should.equal(true); }); it('does not validate invalid signature with correct method and data for valid user', async function () { (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE) - ).should.eq(false); + ).should.equal(false); }); it('does not validate valid signature with correct method and incorrect data for valid user', async function () { (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10, this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.eq(false); + ).should.equal(false); } ); it('does not validate valid signature with correct method and data for anyone', async function () { (await this.bouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE, this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.eq(false); + ).should.equal(false); }); it('does not validate valid non-method-data signature with correct method and data for valid user', async function () { (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, this.signFor(authorizedUser, 'checkValidSignatureAndData')) - ).should.eq(false); + ).should.equal(false); } ); }); diff --git a/test/ownership/Whitelist.test.js b/test/access/Whitelist.test.js similarity index 62% rename from test/ownership/Whitelist.test.js rename to test/access/Whitelist.test.js index 49391f17916..f1113ffc3c9 100644 --- a/test/ownership/Whitelist.test.js +++ b/test/access/Whitelist.test.js @@ -1,5 +1,4 @@ const { expectThrow } = require('../helpers/expectThrow'); -const expectEvent = require('../helpers/expectEvent'); const WhitelistMock = artifacts.require('WhitelistMock'); @@ -11,47 +10,30 @@ contract('Whitelist', function ([_, owner, whitelistedAddress1, whitelistedAddre beforeEach(async function () { this.mock = await WhitelistMock.new({ from: owner }); - this.role = await this.mock.ROLE_WHITELISTED(); }); context('in normal conditions', function () { it('should add address to the whitelist', async function () { - await expectEvent.inTransaction( - this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }), - 'RoleAdded', - { role: this.role }, - ); - (await this.mock.whitelist(whitelistedAddress1)).should.be.be.true; + await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }); + (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(true); }); it('should add addresses to the whitelist', async function () { - await expectEvent.inTransaction( - this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }), - 'RoleAdded', - { role: this.role }, - ); + await this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }); for (const addr of whitelistedAddresses) { - (await this.mock.whitelist(addr)).should.be.be.true; + (await this.mock.isWhitelisted(addr)).should.equal(true); } }); it('should remove address from the whitelist', async function () { - await expectEvent.inTransaction( - this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }), - 'RoleRemoved', - { role: this.role }, - ); - (await this.mock.whitelist(whitelistedAddress1)).should.be.be.false; + await this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }); + (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(false); }); it('should remove addresses from the the whitelist', async function () { - await expectEvent.inTransaction( - this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }), - 'RoleRemoved', - { role: this.role }, - ); + await this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }); for (const addr of whitelistedAddresses) { - (await this.mock.whitelist(addr)).should.be.be.false; + (await this.mock.isWhitelisted(addr)).should.equal(false); } }); diff --git a/test/access/rbac/PublicRole.behavior.js b/test/access/rbac/PublicRole.behavior.js new file mode 100644 index 00000000000..74012fe41b1 --- /dev/null +++ b/test/access/rbac/PublicRole.behavior.js @@ -0,0 +1,96 @@ +const { assertRevert } = require('../../helpers/assertRevert'); + +require('chai') + .should(); + +function capitalize (str) { + return str.replace(/\b\w/g, l => l.toUpperCase()); +} + +function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], rolename) { + rolename = capitalize(rolename); + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + + describe(`role ${rolename}`, function () { + beforeEach('check preconditions', async function () { + (await this.contract[`is${rolename}`](authorized)).should.equal(true); + (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true); + (await this.contract[`is${rolename}`](anyone)).should.equal(false); + }); + + describe('add', function () { + it('adds role to a new account', async function () { + await this.contract[`add${rolename}`](anyone); + (await this.contract[`is${rolename}`](anyone)).should.equal(true); + }); + + it('adds role to an already-assigned account', async function () { + await this.contract[`add${rolename}`](authorized); + (await this.contract[`is${rolename}`](authorized)).should.equal(true); + }); + + it('doesn\'t revert when adding role to the null account', async function () { + await this.contract[`add${rolename}`](ZERO_ADDRESS); + }); + }); + + describe('remove', function () { + it('removes role from an already assigned account', async function () { + await this.contract[`remove${rolename}`](authorized); + (await this.contract[`is${rolename}`](authorized)).should.equal(false); + (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true); + }); + + it('doesn\'t revert when removing from an unassigned account', async function () { + await this.contract[`remove${rolename}`](anyone); + }); + + it('doesn\'t revert when removing role from the null account', async function () { + await this.contract[`remove${rolename}`](ZERO_ADDRESS); + }); + }); + + describe('transfering', function () { + context('from account with role', function () { + const from = authorized; + + it('transfers to other account without the role', async function () { + await this.contract[`transfer${rolename}`](anyone, { from }); + (await this.contract[`is${rolename}`](anyone)).should.equal(true); + (await this.contract[`is${rolename}`](authorized)).should.equal(false); + }); + + it('reverts when transfering to an account with role', async function () { + await assertRevert(this.contract[`transfer${rolename}`](otherAuthorized, { from })); + }); + + it('reverts when transfering to the null account', async function () { + await assertRevert(this.contract[`transfer${rolename}`](ZERO_ADDRESS, { from })); + }); + }); + + context('from account without role', function () { + const from = anyone; + + it('reverts', async function () { + await assertRevert(this.contract[`transfer${rolename}`](anyone, { from })); + }); + }); + }); + + describe('renouncing roles', function () { + it('renounces an assigned role', async function () { + await this.contract[`renounce${rolename}`]({ from: authorized }); + (await this.contract[`is${rolename}`](authorized)).should.equal(false); + }); + + it('doesn\'t revert when renouncing unassigned role', async function () { + await this.contract[`renounce${rolename}`]({ from: anyone }); + }); + }); + }); +} + +module.exports = { + shouldBehaveLikePublicRole, +}; diff --git a/test/access/rbac/Roles.test.js b/test/access/rbac/Roles.test.js index 743d6fe082c..57e932692e7 100644 --- a/test/access/rbac/Roles.test.js +++ b/test/access/rbac/Roles.test.js @@ -10,46 +10,37 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { beforeEach(async function () { this.roles = await RolesMock.new(); - this.testRole = async (account, expected) => { - if (expected) { - (await this.roles.has(account)).should.equal(true); - await this.roles.check(account); // this call shouldn't revert, but is otherwise a no-op - } else { - (await this.roles.has(account)).should.equal(false); - await assertRevert(this.roles.check(account)); - } - }; }); context('initially', function () { it('doesn\'t pre-assign roles', async function () { - await this.testRole(authorized, false); - await this.testRole(otherAuthorized, false); - await this.testRole(anyone, false); + (await this.roles.has(authorized)).should.equal(false); + (await this.roles.has(otherAuthorized)).should.equal(false); + (await this.roles.has(anyone)).should.equal(false); }); describe('adding roles', function () { it('adds roles to a single account', async function () { await this.roles.add(authorized); - await this.testRole(authorized, true); - await this.testRole(anyone, false); + (await this.roles.has(authorized)).should.equal(true); + (await this.roles.has(anyone)).should.equal(false); }); it('adds roles to an already-assigned account', async function () { await this.roles.add(authorized); await this.roles.add(authorized); - await this.testRole(authorized, true); + (await this.roles.has(authorized)).should.equal(true); }); it('adds roles to multiple accounts', async function () { await this.roles.addMany([authorized, otherAuthorized]); - await this.testRole(authorized, true); - await this.testRole(otherAuthorized, true); + (await this.roles.has(authorized)).should.equal(true); + (await this.roles.has(otherAuthorized)).should.equal(true); }); it('adds roles to multiple identical accounts', async function () { await this.roles.addMany([authorized, authorized]); - await this.testRole(authorized, true); + (await this.roles.has(authorized)).should.equal(true); }); it('doesn\'t revert when adding roles to the null account', async function () { @@ -66,8 +57,8 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { describe('removing roles', function () { it('removes a single role', async function () { await this.roles.remove(authorized); - await this.testRole(authorized, false); - await this.testRole(otherAuthorized, true); + (await this.roles.has(authorized)).should.equal(false); + (await this.roles.has(otherAuthorized)).should.equal(true); }); it('doesn\'t revert when removing unassigned roles', async function () { @@ -85,8 +76,8 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { it('transfers to other account with no role', async function () { await this.roles.transfer(anyone, { from }); - await this.testRole(anyone, true); - await this.testRole(authorized, false); + (await this.roles.has(anyone)).should.equal(true); + (await this.roles.has(authorized)).should.equal(false); }); it('reverts when transfering to an account with role', async function () { @@ -110,7 +101,7 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { describe('renouncing roles', function () { it('renounces an assigned role', async function () { await this.roles.renounce({ from: authorized }); - await this.testRole(authorized, false); + (await this.roles.has(authorized)).should.equal(false); }); it('doesn\'t revert when renouncing unassigned role', async function () { diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index f3f907f3566..b0d68a9b15c 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -37,10 +37,10 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW describe('high-level purchase', function () { it('should log purchase', async function () { const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor }); - const event = logs.find(e => e.event === 'TokenPurchase'); + const event = logs.find(e => e.event === 'TokensPurchased'); should.exist(event); - event.args.purchaser.should.eq(investor); - event.args.beneficiary.should.eq(investor); + event.args.purchaser.should.equal(investor); + event.args.beneficiary.should.equal(investor); event.args.value.should.be.bignumber.equal(value); event.args.amount.should.be.bignumber.equal(expectedTokenAmount); }); diff --git a/test/crowdsale/CappedCrowdsale.test.js b/test/crowdsale/CappedCrowdsale.test.js index df00d3c976e..94ed0aedda4 100644 --- a/test/crowdsale/CappedCrowdsale.test.js +++ b/test/crowdsale/CappedCrowdsale.test.js @@ -19,55 +19,58 @@ contract('CappedCrowdsale', function ([_, wallet]) { beforeEach(async function () { this.token = await SimpleToken.new(); - this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address, cap); - await this.token.transfer(this.crowdsale.address, tokenSupply); }); - describe('creating a valid crowdsale', function () { - it('should fail with zero cap', async function () { - await expectThrow( - CappedCrowdsale.new(rate, wallet, 0, this.token.address), - EVMRevert, - ); - }); + it('rejects a cap of zero', async function () { + await expectThrow( + CappedCrowdsale.new(rate, wallet, this.token.address, 0), + EVMRevert, + ); }); - describe('accepting payments', function () { - it('should accept payments within cap', async function () { - await this.crowdsale.send(cap.minus(lessThanCap)); - await this.crowdsale.send(lessThanCap); + context('with crowdsale', function () { + beforeEach(async function () { + this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address, cap); + await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('should reject payments outside cap', async function () { - await this.crowdsale.send(cap); - await expectThrow( - this.crowdsale.send(1), - EVMRevert, - ); - }); + describe('accepting payments', function () { + it('should accept payments within cap', async function () { + await this.crowdsale.send(cap.minus(lessThanCap)); + await this.crowdsale.send(lessThanCap); + }); - it('should reject payments that exceed cap', async function () { - await expectThrow( - this.crowdsale.send(cap.plus(1)), - EVMRevert, - ); - }); - }); + it('should reject payments outside cap', async function () { + await this.crowdsale.send(cap); + await expectThrow( + this.crowdsale.send(1), + EVMRevert, + ); + }); - describe('ending', function () { - it('should not reach cap if sent under cap', async function () { - await this.crowdsale.send(lessThanCap); - (await this.crowdsale.capReached()).should.be.false; + it('should reject payments that exceed cap', async function () { + await expectThrow( + this.crowdsale.send(cap.plus(1)), + EVMRevert, + ); + }); }); - it('should not reach cap if sent just under cap', async function () { - await this.crowdsale.send(cap.minus(1)); - (await this.crowdsale.capReached()).should.be.false; - }); + describe('ending', function () { + it('should not reach cap if sent under cap', async function () { + await this.crowdsale.send(lessThanCap); + (await this.crowdsale.capReached()).should.equal(false); + }); + + it('should not reach cap if sent just under cap', async function () { + await this.crowdsale.send(cap.minus(1)); + (await this.crowdsale.capReached()).should.equal(false); + }); - it('should reach cap if cap sent', async function () { - await this.crowdsale.send(cap); - (await this.crowdsale.capReached()).should.be.true; + it('should reach cap if cap sent', async function () { + await this.crowdsale.send(cap); + (await this.crowdsale.capReached()).should.equal(true); + }); }); }); }); diff --git a/test/crowdsale/Crowdsale.test.js b/test/crowdsale/Crowdsale.test.js index bf32f94ca06..556855796d4 100644 --- a/test/crowdsale/Crowdsale.test.js +++ b/test/crowdsale/Crowdsale.test.js @@ -82,10 +82,10 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('high-level purchase', function () { it('should log purchase', async function () { const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor }); - const event = logs.find(e => e.event === 'TokenPurchase'); + const event = logs.find(e => e.event === 'TokensPurchased'); should.exist(event); - event.args.purchaser.should.eq(investor); - event.args.beneficiary.should.eq(investor); + event.args.purchaser.should.equal(investor); + event.args.beneficiary.should.equal(investor); event.args.value.should.be.bignumber.equal(value); event.args.amount.should.be.bignumber.equal(expectedTokenAmount); }); @@ -106,10 +106,10 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { describe('low-level purchase', function () { 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'); + const event = logs.find(e => e.event === 'TokensPurchased'); should.exist(event); - event.args.purchaser.should.eq(purchaser); - event.args.beneficiary.should.eq(investor); + 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); }); diff --git a/test/crowdsale/FinalizableCrowdsale.test.js b/test/crowdsale/FinalizableCrowdsale.test.js index ef4c1d0d78c..e6f2914ea58 100644 --- a/test/crowdsale/FinalizableCrowdsale.test.js +++ b/test/crowdsale/FinalizableCrowdsale.test.js @@ -11,7 +11,7 @@ const should = require('chai') .should(); const FinalizableCrowdsale = artifacts.require('FinalizableCrowdsaleImpl'); -const MintableToken = artifacts.require('MintableToken'); +const ERC20 = artifacts.require('ERC20'); contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { const rate = new BigNumber(1000); @@ -26,11 +26,10 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); - this.token = await MintableToken.new(); + this.token = await ERC20.new(); this.crowdsale = await FinalizableCrowdsale.new( this.openingTime, this.closingTime, rate, wallet, this.token.address, { from: owner } ); - await this.token.transferOwnership(this.crowdsale.address); }); it('cannot be finalized before ending', async function () { @@ -56,7 +55,7 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { it('logs finalized', async function () { await increaseTimeTo(this.afterClosingTime); const { logs } = await this.crowdsale.finalize({ from: owner }); - const event = logs.find(e => e.event === 'Finalized'); + const event = logs.find(e => e.event === 'CrowdsaleFinalized'); should.exist(event); }); }); diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index 9453d12bba4..e68d451bd46 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -2,6 +2,7 @@ const { ether } = require('../helpers/ether'); const { advanceBlock } = require('../helpers/advanceToBlock'); const { increaseTimeTo, duration } = require('../helpers/increaseTime'); const { latestTime } = require('../helpers/latestTime'); +const { assertRevert } = require('../helpers/assertRevert'); const BigNumber = web3.BigNumber; @@ -32,52 +33,69 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) this.closingTime = this.startTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); this.token = await SimpleToken.new(); - this.crowdsale = await IncreasingPriceCrowdsale.new( - this.startTime, this.closingTime, wallet, this.token.address, initialRate, finalRate - ); - await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('at start', async function () { - await increaseTimeTo(this.startTime); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate)); + it('rejects a final rate larger than the initial rate', async function () { + await assertRevert(IncreasingPriceCrowdsale.new( + this.startTime, this.closingTime, wallet, this.token.address, initialRate, initialRate.plus(1) + )); }); - it('at time 150', async function () { - await increaseTimeTo(this.startTime + 150); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150)); + it('rejects a final rate of zero', async function () { + await assertRevert(IncreasingPriceCrowdsale.new( + this.startTime, this.closingTime, wallet, this.token.address, initialRate, 0 + )); }); - it('at time 300', async function () { - await increaseTimeTo(this.startTime + 300); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300)); - }); + context('with crowdsale', function () { + beforeEach(async function () { + this.crowdsale = await IncreasingPriceCrowdsale.new( + this.startTime, this.closingTime, wallet, this.token.address, initialRate, finalRate + ); + await this.token.transfer(this.crowdsale.address, tokenSupply); + }); - it('at time 1500', async function () { - await increaseTimeTo(this.startTime + 1500); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500)); - }); + it('at start', async function () { + await increaseTimeTo(this.startTime); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate)); + }); - it('at time 30', async function () { - await increaseTimeTo(this.startTime + 30); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30)); - }); + it('at time 150', async function () { + await increaseTimeTo(this.startTime + 150); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150)); + }); - it('at time 150000', async function () { - await increaseTimeTo(this.startTime + 150000); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000)); - }); + it('at time 300', async function () { + await increaseTimeTo(this.startTime + 300); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300)); + }); + + it('at time 1500', async function () { + await increaseTimeTo(this.startTime + 1500); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500)); + }); + + it('at time 30', async function () { + await increaseTimeTo(this.startTime + 30); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30)); + }); + + it('at time 150000', async function () { + await increaseTimeTo(this.startTime + 150000); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000)); + }); - it('at time 450000', async function () { - await increaseTimeTo(this.startTime + 450000); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000)); + it('at time 450000', async function () { + await increaseTimeTo(this.startTime + 450000); + await this.crowdsale.buyTokens(investor, { value, from: purchaser }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000)); + }); }); }); }); diff --git a/test/crowdsale/MintedCrowdsale.behavior.js b/test/crowdsale/MintedCrowdsale.behavior.js index e77328d6a3a..f5d61328c3a 100644 --- a/test/crowdsale/MintedCrowdsale.behavior.js +++ b/test/crowdsale/MintedCrowdsale.behavior.js @@ -20,10 +20,10 @@ function shouldBehaveLikeMintedCrowdsale ([_, investor, wallet, purchaser], rate describe('high-level purchase', function () { it('should log purchase', async function () { const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor }); - const event = logs.find(e => e.event === 'TokenPurchase'); + const event = logs.find(e => e.event === 'TokensPurchased'); should.exist(event); - event.args.purchaser.should.eq(investor); - event.args.beneficiary.should.eq(investor); + event.args.purchaser.should.equal(investor); + event.args.beneficiary.should.equal(investor); event.args.value.should.be.bignumber.equal(value); event.args.amount.should.be.bignumber.equal(expectedTokenAmount); }); diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index ec36f9340bd..408300e3426 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -1,26 +1,43 @@ const { shouldBehaveLikeMintedCrowdsale } = require('./MintedCrowdsale.behavior'); const { ether } = require('../helpers/ether'); +const { assertRevert } = require('../helpers/assertRevert'); const BigNumber = web3.BigNumber; const MintedCrowdsale = artifacts.require('MintedCrowdsaleImpl'); -const MintableToken = artifacts.require('MintableToken'); +const ERC20Mintable = artifacts.require('ERC20Mintable'); +const ERC20 = artifacts.require('ERC20'); -contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { +contract('MintedCrowdsale', function ([_, initialMinter, investor, wallet, purchaser]) { const rate = new BigNumber(1000); const value = ether(5); - describe('using MintableToken', function () { + describe('using ERC20Mintable', function () { beforeEach(async function () { - this.token = await MintableToken.new(); + this.token = await ERC20Mintable.new([initialMinter]); this.crowdsale = await MintedCrowdsale.new(rate, wallet, this.token.address); - await this.token.transferOwnership(this.crowdsale.address); + await this.token.transferMinter(this.crowdsale.address, { from: initialMinter }); }); - it('should be token owner', async function () { - (await this.token.owner()).should.eq(this.crowdsale.address); + it('crowdsale should be minter', async function () { + (await this.token.isMinter(this.crowdsale.address)).should.equal(true); }); shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); }); + + describe('using non-mintable token', function () { + beforeEach(async function () { + this.token = await ERC20.new(); + this.crowdsale = await MintedCrowdsale.new(rate, wallet, this.token.address); + }); + + it('rejects bare payments', async function () { + await assertRevert(this.crowdsale.send(value)); + }); + + it('rejects token purchases', async function () { + await assertRevert(this.crowdsale.buyTokens(investor, { value: value, from: purchaser })); + }); + }); }); diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index 5511d07573f..ebc41111c3d 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -16,7 +16,6 @@ const SimpleToken = artifacts.require('SimpleToken'); contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { const rate = new BigNumber(1); - const value = ether(42); const tokenSupply = new BigNumber('1e22'); before(async function () { @@ -27,7 +26,6 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { beforeEach(async function () { this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); - this.beforeEndTime = this.closingTime - duration.hours(1); this.afterClosingTime = this.closingTime + duration.seconds(1); this.token = await SimpleToken.new(); this.crowdsale = await PostDeliveryCrowdsale.new( @@ -36,30 +34,41 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('should not immediately assign tokens to beneficiary', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); - }); + context('after opening time', function () { + beforeEach(async function () { + await increaseTimeTo(this.openingTime); + }); - it('should not allow beneficiaries to withdraw tokens before crowdsale ends', async function () { - await increaseTimeTo(this.beforeEndTime); - await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); - await expectThrow(this.crowdsale.withdrawTokens({ from: investor }), EVMRevert); - }); + context('with bought tokens', function () { + const value = ether(42); - it('should allow beneficiaries to withdraw tokens after crowdsale ends', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); - await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.withdrawTokens({ from: investor }); - }); + beforeEach(async function () { + await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); + }); + + it('does not immediately assign tokens to beneficiaries', async function () { + (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); + }); + + it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () { + await expectThrow(this.crowdsale.withdrawTokens({ from: investor }), EVMRevert); + }); + + context('after closing time', function () { + beforeEach(async function () { + await increaseTimeTo(this.afterClosingTime); + }); + + it('allows beneficiaries to withdraw tokens', async function () { + await this.crowdsale.withdrawTokens({ from: investor }); + (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); + }); - it('should return the amount of tokens bought', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); - await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.withdrawTokens({ from: investor }); - (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); + it('rejects multiple withdrawals', async function () { + await this.crowdsale.withdrawTokens({ from: investor }); + await expectThrow(this.crowdsale.withdrawTokens({ from: investor }), EVMRevert); + }); + }); + }); }); }); diff --git a/test/crowdsale/RefundableCrowdsale.test.js b/test/crowdsale/RefundableCrowdsale.test.js index 743342cb9e4..320697267aa 100644 --- a/test/crowdsale/RefundableCrowdsale.test.js +++ b/test/crowdsale/RefundableCrowdsale.test.js @@ -30,56 +30,85 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); + this.preWalletBalance = await ethGetBalance(wallet); this.token = await SimpleToken.new(); - this.crowdsale = await RefundableCrowdsale.new( - this.openingTime, this.closingTime, rate, wallet, this.token.address, goal, { from: owner } + }); + + it('rejects a goal of zero', async function () { + await expectThrow( + RefundableCrowdsale.new( + this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner } + ), + EVMRevert, ); - await this.token.transfer(this.crowdsale.address, tokenSupply); }); - describe('creating a valid crowdsale', function () { - it('should fail with zero goal', async function () { - await expectThrow( - RefundableCrowdsale.new( - this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner } - ), - EVMRevert, + context('with crowdsale', function () { + beforeEach(async function () { + this.crowdsale = await RefundableCrowdsale.new( + this.openingTime, this.closingTime, rate, wallet, this.token.address, goal, { from: owner } ); + + await this.token.transfer(this.crowdsale.address, tokenSupply); }); - }); - it('should deny refunds before end', async function () { - await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); - await increaseTimeTo(this.openingTime); - await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); - }); + context('before opening time', function () { + it('denies refunds', async function () { + await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); + }); + }); - it('should deny refunds after end if goal was reached', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.sendTransaction({ value: goal, from: investor }); - await increaseTimeTo(this.afterClosingTime); - await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); - }); + context('after opening time', function () { + beforeEach(async function () { + await increaseTimeTo(this.openingTime); + }); - it('should allow refunds after end if goal was not reached', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor }); - await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); - const pre = await ethGetBalance(investor); - await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }); - const post = await ethGetBalance(investor); - post.minus(pre).should.be.bignumber.equal(lessThanGoal); - }); + it('denies refunds', async function () { + await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); + }); - it('should forward funds to wallet after end if goal was reached', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.sendTransaction({ value: goal, from: investor }); - await increaseTimeTo(this.afterClosingTime); - const pre = await ethGetBalance(wallet); - await this.crowdsale.finalize({ from: owner }); - const post = await ethGetBalance(wallet); - post.minus(pre).should.be.bignumber.equal(goal); + context('with unreached goal', function () { + beforeEach(async function () { + await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor }); + }); + + context('after closing time and finalization', function () { + beforeEach(async function () { + await increaseTimeTo(this.afterClosingTime); + await this.crowdsale.finalize({ from: owner }); + }); + + it('refunds', async function () { + const pre = await ethGetBalance(investor); + await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }); + const post = await ethGetBalance(investor); + post.minus(pre).should.be.bignumber.equal(lessThanGoal); + }); + }); + }); + + context('with reached goal', function () { + beforeEach(async function () { + await this.crowdsale.sendTransaction({ value: goal, from: investor }); + }); + + context('after closing time and finalization', function () { + beforeEach(async function () { + await increaseTimeTo(this.afterClosingTime); + await this.crowdsale.finalize({ from: owner }); + }); + + it('denies refunds', async function () { + await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert); + }); + + it('forwards funds to wallet', async function () { + const postWalletBalance = await ethGetBalance(wallet); + postWalletBalance.minus(this.preWalletBalance).should.be.bignumber.equal(goal); + }); + }); + }); + }); }); }); diff --git a/test/crowdsale/TimedCrowdsale.test.js b/test/crowdsale/TimedCrowdsale.test.js index 3183b264005..ad9ca29f556 100644 --- a/test/crowdsale/TimedCrowdsale.test.js +++ b/test/crowdsale/TimedCrowdsale.test.js @@ -29,32 +29,49 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); this.token = await SimpleToken.new(); - this.crowdsale = await TimedCrowdsale.new(this.openingTime, this.closingTime, rate, wallet, this.token.address); - await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('should be ended only after end', async function () { - (await this.crowdsale.hasClosed()).should.be.false; - await increaseTimeTo(this.afterClosingTime); - (await this.crowdsale.hasClosed()).should.be.true; + it('rejects an opening time in the past', async function () { + await expectThrow(TimedCrowdsale.new( + (await latestTime()) - duration.days(1), this.closingTime, rate, wallet, this.token.address + ), EVMRevert); }); - describe('accepting payments', function () { - it('should reject payments before start', async function () { - await expectThrow(this.crowdsale.send(value), EVMRevert); - await expectThrow(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }), EVMRevert); - }); + it('rejects a closing time before the opening time', async function () { + await expectThrow(TimedCrowdsale.new( + this.openingTime, this.openingTime - duration.seconds(1), rate, wallet, this.token.address + ), EVMRevert); + }); - it('should accept payments after start', async function () { - await increaseTimeTo(this.openingTime); - await this.crowdsale.send(value); - await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); + context('with crowdsale', function () { + beforeEach(async function () { + this.crowdsale = await TimedCrowdsale.new(this.openingTime, this.closingTime, rate, wallet, this.token.address); + await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('should reject payments after end', async function () { + it('should be ended only after end', async function () { + (await this.crowdsale.hasClosed()).should.equal(false); await increaseTimeTo(this.afterClosingTime); - await expectThrow(this.crowdsale.send(value), EVMRevert); - await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert); + (await this.crowdsale.hasClosed()).should.equal(true); + }); + + describe('accepting payments', function () { + it('should reject payments before start', async function () { + await expectThrow(this.crowdsale.send(value), EVMRevert); + await expectThrow(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }), EVMRevert); + }); + + it('should accept payments after start', async function () { + await increaseTimeTo(this.openingTime); + await this.crowdsale.send(value); + await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); + }); + + it('should reject payments after end', async function () { + await increaseTimeTo(this.afterClosingTime); + await expectThrow(this.crowdsale.send(value), EVMRevert); + await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert); + }); }); }); }); diff --git a/test/crowdsale/WhitelistedCrowdsale.test.js b/test/crowdsale/WhitelistedCrowdsale.test.js index 2d67bfd5f98..3cf9b3206b9 100644 --- a/test/crowdsale/WhitelistedCrowdsale.test.js +++ b/test/crowdsale/WhitelistedCrowdsale.test.js @@ -43,8 +43,8 @@ contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized, describe('reporting whitelisted', function () { it('should correctly report whitelisted addresses', async function () { - (await this.crowdsale.whitelist(authorized)).should.be.true; - (await this.crowdsale.whitelist(unauthorized)).should.be.false; + (await this.crowdsale.isWhitelisted(authorized)).should.equal(true); + (await this.crowdsale.isWhitelisted(unauthorized)).should.equal(false); }); }); }); @@ -80,9 +80,9 @@ contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized, describe('reporting whitelisted', function () { it('should correctly report whitelisted addresses', async function () { - (await this.crowdsale.whitelist(authorized)).should.be.true; - (await this.crowdsale.whitelist(anotherAuthorized)).should.be.true; - (await this.crowdsale.whitelist(unauthorized)).should.be.false; + (await this.crowdsale.isWhitelisted(authorized)).should.equal(true); + (await this.crowdsale.isWhitelisted(anotherAuthorized)).should.equal(true); + (await this.crowdsale.isWhitelisted(unauthorized)).should.equal(false); }); }); }); diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index 6d30b2b8126..6e9706d49d4 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -9,14 +9,14 @@ const { ethGetBalance } = require('../helpers/web3'); const BigNumber = web3.BigNumber; -require('chai') +const should = require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); const SampleCrowdsale = artifacts.require('SampleCrowdsale'); const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken'); -contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { +contract('SampleCrowdsale', function ([_, initialMinter, owner, wallet, investor]) { const RATE = new BigNumber(10); const GOAL = ether(10); const CAP = ether(20); @@ -31,17 +31,17 @@ contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); - this.token = await SampleCrowdsaleToken.new({ from: owner }); + this.token = await SampleCrowdsaleToken.new([initialMinter]); this.crowdsale = await SampleCrowdsale.new( this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL, { from: owner } ); - await this.token.transferOwnership(this.crowdsale.address, { from: owner }); + await this.token.transferMinter(this.crowdsale.address, { from: initialMinter }); }); it('should create crowdsale with correct parameters', async function () { - this.crowdsale.should.exist; - this.token.should.exist; + should.exist(this.crowdsale); + should.exist(this.token); (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime); (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime); diff --git a/test/examples/SimpleToken.test.js b/test/examples/SimpleToken.test.js index 89b54c37f73..a28cf4641ab 100644 --- a/test/examples/SimpleToken.test.js +++ b/test/examples/SimpleToken.test.js @@ -17,11 +17,11 @@ contract('SimpleToken', function ([_, creator]) { }); it('has a name', async function () { - (await token.name()).should.eq('SimpleToken'); + (await token.name()).should.equal('SimpleToken'); }); it('has a symbol', async function () { - (await token.symbol()).should.eq('SIM'); + (await token.symbol()).should.equal('SIM'); }); it('has 18 decimals', async function () { @@ -36,7 +36,7 @@ contract('SimpleToken', function ([_, creator]) { const receipt = await web3.eth.getTransactionReceipt(token.transactionHash); const logs = decodeLogs(receipt.logs, SimpleToken, token.address); - logs.length.should.eq(1); + logs.length.should.equal(1); logs[0].event.should.equal('Transfer'); logs[0].args.from.valueOf().should.equal(ZERO_ADDRESS); logs[0].args.to.valueOf().should.equal(creator); diff --git a/test/helpers/expectEvent.js b/test/helpers/expectEvent.js index 1bfacd6f2b2..565d61fe55c 100644 --- a/test/helpers/expectEvent.js +++ b/test/helpers/expectEvent.js @@ -5,7 +5,7 @@ function inLogs (logs, eventName, eventArgs = {}) { should.exist(event); for (const [k, v] of Object.entries(eventArgs)) { should.exist(event.args[k]); - event.args[k].should.eq(v); + event.args[k].should.equal(v); } return event; } diff --git a/test/helpers/sign.js b/test/helpers/sign.js index 4e14d71be6c..a05238ad4fa 100644 --- a/test/helpers/sign.js +++ b/test/helpers/sign.js @@ -1,26 +1,21 @@ -const utils = require('ethereumjs-util'); -const { soliditySha3 } = require('web3-utils'); +const { sha3, soliditySha3 } = require('web3-utils'); const REAL_SIGNATURE_SIZE = 2 * 65; // 65 bytes in hexadecimal string legnth const PADDED_SIGNATURE_SIZE = 2 * 96; // 96 bytes in hexadecimal string length const DUMMY_SIGNATURE = `0x${web3.padLeft('', REAL_SIGNATURE_SIZE)}`; -/** - * Hash and add same prefix to the hash that ganache use. - * @param {string} message the plaintext/ascii/original message - * @return {string} the hash of the message, prefixed, and then hashed again - */ -function hashMessage (message) { - const messageHex = Buffer.from(utils.sha3(message).toString('hex'), 'hex'); - const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + messageHex.length.toString()); - return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex]))); +// messageHex = '0xdeadbeef' +function toEthSignedMessageHash (messageHex) { + const messageBuffer = Buffer.from(messageHex.substring(2), 'hex'); + const prefix = Buffer.from(`\u0019Ethereum Signed Message:\n${messageBuffer.length}`); + return sha3(Buffer.concat([prefix, messageBuffer])); } -// signs message in node (auto-applies prefix) -// message must be in hex already! will not be autoconverted! -const signMessage = (signer, message = '') => { - return web3.eth.sign(signer, message); +// signs message in node (ganache auto-applies "Ethereum Signed Message" prefix) +// messageHex = '0xdeadbeef' +const signMessage = (signer, messageHex = '0x') => { + return web3.eth.sign(signer, messageHex); // actually personal_sign }; // @TODO - remove this when we migrate to web3-1.0.0 @@ -62,18 +57,18 @@ const getBouncerSigner = (contract, signer) => (redeemer, methodName, methodArgs } else { const abi = contract.abi.find(abi => abi.name === methodName); const name = transformToFullName(abi); - const signature = web3.sha3(name).slice(0, 10); + const signature = sha3(name).slice(0, 10); parts.push(signature); } } - // ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd` - const hashOfMessage = soliditySha3(...parts); - return signMessage(signer, hashOfMessage); + // return the signature of the "Ethereum Signed Message" hash of the hash of `parts` + const messageHex = soliditySha3(...parts); + return signMessage(signer, messageHex); }; module.exports = { - hashMessage, signMessage, + toEthSignedMessageHash, getBouncerSigner, }; diff --git a/test/introspection/ERC165Checker.test.js b/test/introspection/ERC165Checker.test.js new file mode 100644 index 00000000000..a6bfc518d46 --- /dev/null +++ b/test/introspection/ERC165Checker.test.js @@ -0,0 +1,137 @@ +const ERC165CheckerMock = artifacts.require('ERC165CheckerMock'); +const ERC165NotSupported = artifacts.require('ERC165NotSupported'); +const ERC165InterfacesSupported = artifacts.require('ERC165InterfacesSupported'); + +const DUMMY_ID = '0xdeadbeef'; +const DUMMY_ID_2 = '0xcafebabe'; +const DUMMY_ID_3 = '0xdecafbad'; +const DUMMY_UNSUPPORTED_ID = '0xbaddcafe'; +const DUMMY_UNSUPPORTED_ID_2 = '0xbaadcafe'; +const DUMMY_ACCOUNT = '0x1111111111111111111111111111111111111111'; + +require('chai') + .should(); + +contract('ERC165Checker', function () { + beforeEach(async function () { + this.mock = await ERC165CheckerMock.new(); + }); + + context('ERC165 not supported', function () { + beforeEach(async function () { + this.target = await ERC165NotSupported.new(); + }); + + it('does not support ERC165', async function () { + const supported = await this.mock.supportsERC165(this.target.address); + supported.should.equal(false); + }); + + it('does not support mock interface via supportsInterface', async function () { + const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID); + supported.should.equal(false); + }); + + it('does not support mock interface via supportsInterfaces', async function () { + const supported = await this.mock.supportsInterfaces(this.target.address, [DUMMY_ID]); + supported.should.equal(false); + }); + }); + + context('ERC165 supported', function () { + beforeEach(async function () { + this.target = await ERC165InterfacesSupported.new([]); + }); + + it('supports ERC165', async function () { + const supported = await this.mock.supportsERC165(this.target.address); + supported.should.equal(true); + }); + + it('does not support mock interface via supportsInterface', async function () { + const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID); + supported.should.equal(false); + }); + + it('does not support mock interface via supportsInterfaces', async function () { + const supported = await this.mock.supportsInterfaces(this.target.address, [DUMMY_ID]); + supported.should.equal(false); + }); + }); + + context('ERC165 and single interface supported', function () { + beforeEach(async function () { + this.target = await ERC165InterfacesSupported.new([DUMMY_ID]); + }); + + it('supports ERC165', async function () { + const supported = await this.mock.supportsERC165(this.target.address); + supported.should.equal(true); + }); + + it('supports mock interface via supportsInterface', async function () { + const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID); + supported.should.equal(true); + }); + + it('supports mock interface via supportsInterfaces', async function () { + const supported = await this.mock.supportsInterfaces(this.target.address, [DUMMY_ID]); + supported.should.equal(true); + }); + }); + + context('ERC165 and many interfaces supported', function () { + beforeEach(async function () { + this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3]; + this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces); + }); + + it('supports ERC165', async function () { + const supported = await this.mock.supportsERC165(this.target.address); + supported.should.equal(true); + }); + + it('supports each interfaceId via supportsInterface', async function () { + for (const interfaceId of this.supportedInterfaces) { + const supported = await this.mock.supportsInterface(this.target.address, interfaceId); + supported.should.equal(true); + }; + }); + + it('supports all interfaceIds via supportsInterfaces', async function () { + const supported = await this.mock.supportsInterfaces(this.target.address, this.supportedInterfaces); + supported.should.equal(true); + }); + + it('supports none of the interfaces queried via supportsInterfaces', async function () { + const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2]; + + const supported = await this.mock.supportsInterfaces(this.target.address, interfaceIdsToTest); + supported.should.equal(false); + }); + + it('supports not all of the interfaces queried via supportsInterfaces', async function () { + const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID]; + + const supported = await this.mock.supportsInterfaces(this.target.address, interfaceIdsToTest); + supported.should.equal(false); + }); + }); + + context('account address does not support ERC165', function () { + it('does not support ERC165', async function () { + const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT); + supported.should.equal(false); + }); + + it('does not support mock interface via supportsInterface', async function () { + const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID); + supported.should.equal(false); + }); + + it('does not support mock interface via supportsInterfaces', async function () { + const supported = await this.mock.supportsInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]); + supported.should.equal(false); + }); + }); +}); diff --git a/test/introspection/SupportsInterface.behavior.js b/test/introspection/SupportsInterface.behavior.js index 395e868a7bf..838787f726d 100644 --- a/test/introspection/SupportsInterface.behavior.js +++ b/test/introspection/SupportsInterface.behavior.js @@ -44,7 +44,7 @@ function shouldSupportInterfaces (interfaces = []) { }); it('is supported', async function () { - (await this.thing.supportsInterface(interfaceId)).should.be.true; + (await this.thing.supportsInterface(interfaceId)).should.equal(true); }); }); } diff --git a/test/library/ECRecovery.test.js b/test/library/ECDSA.test.js similarity index 50% rename from test/library/ECRecovery.test.js rename to test/library/ECDSA.test.js index 8e7fd6cc3f1..7fe7056538a 100644 --- a/test/library/ECRecovery.test.js +++ b/test/library/ECDSA.test.js @@ -1,72 +1,66 @@ -const { hashMessage, signMessage } = require('../helpers/sign'); +const { signMessage, toEthSignedMessageHash } = require('../helpers/sign'); const { expectThrow } = require('../helpers/expectThrow'); -const ECRecoveryMock = artifacts.require('ECRecoveryMock'); +const ECDSAMock = artifacts.require('ECDSAMock'); require('chai') .should(); -contract('ECRecovery', function ([_, anyone]) { - let ecrecovery; - const TEST_MESSAGE = 'OpenZeppelin'; +const TEST_MESSAGE = web3.sha3('OpenZeppelin'); +const WRONG_MESSAGE = web3.sha3('Nope'); +contract('ECDSA', function ([_, anyone]) { beforeEach(async function () { - ecrecovery = await ECRecoveryMock.new(); + this.mock = await ECDSAMock.new(); }); it('recover v0', async function () { // Signature generated outside ganache with method web3.eth.sign(signer, message) const signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c'; - const message = web3.sha3(TEST_MESSAGE); // eslint-disable-next-line max-len const signature = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be89200'; - (await ecrecovery.recover(message, signature)).should.eq(signer); + (await this.mock.recover(TEST_MESSAGE, signature)).should.equal(signer); }); it('recover v1', async function () { // Signature generated outside ganache with method web3.eth.sign(signer, message) const signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e'; - const message = web3.sha3(TEST_MESSAGE); // eslint-disable-next-line max-len const signature = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e001'; - (await ecrecovery.recover(message, signature)).should.eq(signer); + (await this.mock.recover(TEST_MESSAGE, signature)).should.equal(signer); }); it('recover using web3.eth.sign()', async function () { - // Create the signature using account[0] - const signature = signMessage(anyone, web3.sha3(TEST_MESSAGE)); + // Create the signature + const signature = signMessage(anyone, TEST_MESSAGE); // Recover the signer address from the generated message and signature. - (await ecrecovery.recover( - hashMessage(TEST_MESSAGE), + (await this.mock.recover( + toEthSignedMessageHash(TEST_MESSAGE), signature - )).should.eq(anyone); + )).should.equal(anyone); }); it('recover using web3.eth.sign() should return wrong signer', async function () { - // Create the signature using account[0] - const signature = signMessage(anyone, web3.sha3(TEST_MESSAGE)); + // Create the signature + const signature = signMessage(anyone, TEST_MESSAGE); // Recover the signer address from the generated message and wrong signature. - (await ecrecovery.recover(hashMessage('Nope'), signature)).should.not.eq(anyone); + (await this.mock.recover(WRONG_MESSAGE, signature)).should.not.equal(anyone); }); - it('recover should revert when a small hash is sent', async function () { - // Create the signature using account[0] + // @TODO - remove `skip` once we upgrade to solc^0.5 + it.skip('recover should revert when a small hash is sent', async function () { + // Create the signature const signature = signMessage(anyone, TEST_MESSAGE); - try { - await expectThrow( - ecrecovery.recover(hashMessage(TEST_MESSAGE).substring(2), signature) - ); - } catch (error) { - // @TODO(shrugs) - remove this once we upgrade to solc^0.5 - } + await expectThrow( + this.mock.recover(TEST_MESSAGE.substring(2), signature) + ); }); context('toEthSignedMessage', function () { it('should prefix hashes correctly', async function () { - const hashedMessage = web3.sha3(TEST_MESSAGE); - (await ecrecovery.toEthSignedMessageHash(hashedMessage)).should.eq(hashMessage(TEST_MESSAGE)); + (await this.mock.toEthSignedMessageHash(TEST_MESSAGE)).should.equal(toEthSignedMessageHash(TEST_MESSAGE)); }); }); }); diff --git a/test/library/MerkleProof.test.js b/test/library/MerkleProof.test.js index 3c221208e78..cba5d69f5f1 100644 --- a/test/library/MerkleProof.test.js +++ b/test/library/MerkleProof.test.js @@ -24,7 +24,7 @@ contract('MerkleProof', function () { const leaf = bufferToHex(sha3(elements[0])); - (await merkleProof.verifyProof(proof, root, leaf)).should.be.true; + (await merkleProof.verifyProof(proof, root, leaf)).should.equal(true); }); it('should return false for an invalid Merkle proof', async function () { @@ -40,7 +40,7 @@ contract('MerkleProof', function () { const badProof = badMerkleTree.getHexProof(badElements[0]); - (await merkleProof.verifyProof(badProof, correctRoot, correctLeaf)).should.be.false; + (await merkleProof.verifyProof(badProof, correctRoot, correctLeaf)).should.equal(false); }); it('should return false for a Merkle proof of invalid length', async function () { @@ -54,7 +54,7 @@ contract('MerkleProof', function () { const leaf = bufferToHex(sha3(elements[0])); - (await merkleProof.verifyProof(badProof, root, leaf)).should.be.false; + (await merkleProof.verifyProof(badProof, root, leaf)).should.equal(false); }); }); }); diff --git a/test/lifecycle/Destructible.test.js b/test/lifecycle/Destructible.test.js deleted file mode 100644 index a2ba054c175..00000000000 --- a/test/lifecycle/Destructible.test.js +++ /dev/null @@ -1,33 +0,0 @@ -const DestructibleMock = artifacts.require('DestructibleMock'); -const { ethGetBalance } = require('../helpers/web3'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('Destructible', function ([_, owner, recipient]) { - beforeEach(async function () { - this.destructible = await DestructibleMock.new({ from: owner }); - await web3.eth.sendTransaction({ - from: owner, - to: this.destructible.address, - value: web3.toWei('10', 'ether'), - }); - }); - - it('should send balance to owner after destruction', async function () { - const initBalance = await ethGetBalance(owner); - await this.destructible.destroy({ from: owner }); - const newBalance = await ethGetBalance(owner); - newBalance.should.be.bignumber.gt(initBalance); - }); - - it('should send balance to recepient after destruction', async function () { - const initBalance = await ethGetBalance(recipient); - await this.destructible.destroyAndSend(recipient, { from: owner }); - const newBalance = await ethGetBalance(recipient); - newBalance.should.be.bignumber.gt(initBalance); - }); -}); diff --git a/test/lifecycle/Pausable.test.js b/test/lifecycle/Pausable.test.js index 9a6e25b6a67..da37b6e6a01 100644 --- a/test/lifecycle/Pausable.test.js +++ b/test/lifecycle/Pausable.test.js @@ -1,4 +1,5 @@ const { assertRevert } = require('../helpers/assertRevert'); +const expectEvent = require('../helpers/expectEvent'); const PausableMock = artifacts.require('PausableMock'); const BigNumber = web3.BigNumber; @@ -29,13 +30,13 @@ contract('Pausable', function () { it('can not take drastic measure in non-pause', async function () { await assertRevert(this.Pausable.drasticMeasure()); - (await this.Pausable.drasticMeasureTaken()).should.be.false; + (await this.Pausable.drasticMeasureTaken()).should.equal(false); }); it('can take a drastic measure in a pause', async function () { await this.Pausable.pause(); await this.Pausable.drasticMeasure(); - (await this.Pausable.drasticMeasureTaken()).should.be.true; + (await this.Pausable.drasticMeasureTaken()).should.equal(true); }); it('should resume allowing normal process after pause is over', async function () { @@ -51,6 +52,14 @@ contract('Pausable', function () { await assertRevert(this.Pausable.drasticMeasure()); - (await this.Pausable.drasticMeasureTaken()).should.be.false; + (await this.Pausable.drasticMeasureTaken()).should.equal(false); + }); + + it('should log Pause and Unpause events appropriately', async function () { + const setPauseLogs = (await this.Pausable.pause()).logs; + expectEvent.inLogs(setPauseLogs, 'Paused'); + + const setUnPauseLogs = (await this.Pausable.unpause()).logs; + expectEvent.inLogs(setUnPauseLogs, 'Unpaused'); }); }); diff --git a/test/lifecycle/TokenDestructible.test.js b/test/lifecycle/TokenDestructible.test.js deleted file mode 100644 index 5aa631da51e..00000000000 --- a/test/lifecycle/TokenDestructible.test.js +++ /dev/null @@ -1,39 +0,0 @@ -const { ethGetBalance } = require('../helpers/web3'); - -const TokenDestructible = artifacts.require('TokenDestructible'); -const StandardTokenMock = artifacts.require('StandardTokenMock'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('TokenDestructible', function ([_, owner]) { - let tokenDestructible; - - beforeEach(async function () { - tokenDestructible = await TokenDestructible.new({ - from: owner, - value: web3.toWei('10', 'ether'), - }); - }); - - it('should send balance to owner after destruction', async function () { - const initBalance = await ethGetBalance(owner); - await tokenDestructible.destroy([], { from: owner }); - - const newBalance = await ethGetBalance(owner); - newBalance.should.be.bignumber.gt(initBalance); - }); - - it('should send tokens to owner after destruction', async function () { - const token = await StandardTokenMock.new(tokenDestructible.address, 100); - (await token.balanceOf(tokenDestructible.address)).should.be.bignumber.equal(100); - (await token.balanceOf(owner)).should.be.bignumber.equal(0); - - await tokenDestructible.destroy([token.address], { from: owner }); - (await token.balanceOf(tokenDestructible.address)).should.be.bignumber.equal(0); - (await token.balanceOf(owner)).should.be.bignumber.equal(100); - }); -}); diff --git a/test/ownership/CanReclaimToken.test.js b/test/ownership/CanReclaimToken.test.js index 1710ad2d60d..dd3fef62201 100644 --- a/test/ownership/CanReclaimToken.test.js +++ b/test/ownership/CanReclaimToken.test.js @@ -1,7 +1,7 @@ const { expectThrow } = require('../helpers/expectThrow'); const CanReclaimToken = artifacts.require('CanReclaimToken'); -const StandardTokenMock = artifacts.require('StandardTokenMock'); +const ERC20Mock = artifacts.require('ERC20Mock'); const BigNumber = web3.BigNumber; @@ -15,7 +15,7 @@ contract('CanReclaimToken', function ([_, owner, anyone]) { beforeEach(async function () { // Create contract and token - token = await StandardTokenMock.new(owner, 100, { from: owner }); + token = await ERC20Mock.new(owner, 100, { from: owner }); canReclaimToken = await CanReclaimToken.new({ from: owner }); // Force token into contract diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js index 97c041712e7..bfc1355d5f7 100644 --- a/test/ownership/Claimable.test.js +++ b/test/ownership/Claimable.test.js @@ -16,12 +16,12 @@ contract('Claimable', function ([_, owner, newOwner, anyone]) { }); it('should have an owner', async function () { - (await claimable.owner()).should.not.eq(0); + (await claimable.owner()).should.not.equal(0); }); it('changes pendingOwner after transfer', async function () { await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.pendingOwner()).should.eq(newOwner); + (await claimable.pendingOwner()).should.equal(newOwner); }); it('should prevent to claimOwnership from anyone', async function () { @@ -40,7 +40,7 @@ contract('Claimable', function ([_, owner, newOwner, anyone]) { it('changes allow pending owner to claim ownership', async function () { await claimable.claimOwnership({ from: newOwner }); - (await claimable.owner()).should.eq(newOwner); + (await claimable.owner()).should.equal(newOwner); }); }); }); diff --git a/test/ownership/Contactable.test.js b/test/ownership/Contactable.test.js deleted file mode 100644 index de0ca4d1da4..00000000000 --- a/test/ownership/Contactable.test.js +++ /dev/null @@ -1,25 +0,0 @@ -const Contactable = artifacts.require('Contactable'); - -contract('Contactable', function () { - let contactable; - - beforeEach(async function () { - contactable = await Contactable.new(); - }); - - it('should have an empty contact info', async function () { - (await contactable.contactInformation()).should.eq(''); - }); - - describe('after setting the contact information', function () { - const contactInfo = 'contact information'; - - beforeEach(async function () { - await contactable.setContactInformation(contactInfo); - }); - - it('should return the setted contact information', async function () { - (await contactable.contactInformation()).should.eq(contactInfo); - }); - }); -}); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js index 0765f17c3fc..e65ac86c41d 100644 --- a/test/ownership/DelayedClaimable.test.js +++ b/test/ownership/DelayedClaimable.test.js @@ -30,9 +30,9 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { (await this.delayedClaimable.start()).should.be.bignumber.equal(0); - (await this.delayedClaimable.pendingOwner()).should.eq(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.owner()).should.eq(newOwner); + (await this.delayedClaimable.owner()).should.equal(newOwner); }); it('changes pendingOwner after transfer fails', async function () { @@ -43,9 +43,9 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { (await this.delayedClaimable.start()).should.be.bignumber.equal(100); - (await this.delayedClaimable.pendingOwner()).should.eq(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.owner()).should.not.eq(newOwner); + (await this.delayedClaimable.owner()).should.not.equal(newOwner); }); it('set end and start invalid values fail', async function () { diff --git a/test/ownership/HasNoContracts.test.js b/test/ownership/HasNoContracts.test.js deleted file mode 100644 index d891e3bd653..00000000000 --- a/test/ownership/HasNoContracts.test.js +++ /dev/null @@ -1,29 +0,0 @@ -const { expectThrow } = require('../helpers/expectThrow'); - -const Ownable = artifacts.require('Ownable'); -const HasNoContracts = artifacts.require('HasNoContracts'); - -contract('HasNoContracts', function ([_, owner, anyone]) { - let hasNoContracts = null; - let ownable = null; - - beforeEach(async function () { - // Create contract and token - hasNoContracts = await HasNoContracts.new({ from: owner }); - ownable = await Ownable.new({ from: owner }); - - // Force ownership into contract - await ownable.transferOwnership(hasNoContracts.address, { from: owner }); - }); - - it('should allow owner to reclaim contracts', async function () { - await hasNoContracts.reclaimContract(ownable.address, { from: owner }); - (await ownable.owner()).should.eq(owner); - }); - - it('should allow only owner to reclaim contracts', async function () { - await expectThrow( - hasNoContracts.reclaimContract(ownable.address, { from: anyone }) - ); - }); -}); diff --git a/test/ownership/HasNoEther.test.js b/test/ownership/HasNoEther.test.js deleted file mode 100644 index 7f03f1a62a9..00000000000 --- a/test/ownership/HasNoEther.test.js +++ /dev/null @@ -1,61 +0,0 @@ -const { expectThrow } = require('../helpers/expectThrow'); -const { ethSendTransaction, ethGetBalance } = require('../helpers/web3'); - -const HasNoEtherTest = artifacts.require('HasNoEtherTest'); -const ForceEther = artifacts.require('ForceEther'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('HasNoEther', function ([_, owner, anyone]) { - const amount = web3.toWei('1', 'ether'); - - beforeEach(async function () { - this.hasNoEther = await HasNoEtherTest.new({ from: owner }); - }); - - it('should not accept ether in constructor', async function () { - await expectThrow(HasNoEtherTest.new({ value: amount })); - }); - - it('should not accept ether', async function () { - await expectThrow( - ethSendTransaction({ - from: owner, - to: this.hasNoEther.address, - value: amount, - }), - ); - }); - - it('should allow owner to reclaim ether', async function () { - const startBalance = await ethGetBalance(this.hasNoEther.address); - startBalance.should.be.bignumber.equal(0); - - // Force ether into it - const forceEther = await ForceEther.new({ value: amount }); - await forceEther.destroyAndSend(this.hasNoEther.address); - (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(amount); - - // Reclaim - const ownerStartBalance = await ethGetBalance(owner); - await this.hasNoEther.reclaimEther({ from: owner }); - const ownerFinalBalance = await ethGetBalance(owner); - ownerFinalBalance.should.be.bignumber.gt(ownerStartBalance); - - (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(0); - }); - - it('should allow only owner to reclaim ether', async function () { - // Force ether into it - const forceEther = await ForceEther.new({ value: amount }); - await forceEther.destroyAndSend(this.hasNoEther.address); - (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(amount); - - // Reclaim - await expectThrow(this.hasNoEther.reclaimEther({ from: anyone })); - }); -}); diff --git a/test/ownership/HasNoTokens.test.js b/test/ownership/HasNoTokens.test.js deleted file mode 100644 index 2a62dafb76a..00000000000 --- a/test/ownership/HasNoTokens.test.js +++ /dev/null @@ -1,46 +0,0 @@ -const { expectThrow } = require('../helpers/expectThrow'); - -const HasNoTokens = artifacts.require('HasNoTokens'); -const ERC223TokenMock = artifacts.require('ERC223TokenMock'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('HasNoTokens', function ([_, owner, initialAccount, anyone]) { - let hasNoTokens = null; - let token = null; - - beforeEach(async function () { - // Create contract and token - hasNoTokens = await HasNoTokens.new({ from: owner }); - token = await ERC223TokenMock.new(initialAccount, 100); - - // Force token into contract - await token.transfer(hasNoTokens.address, 10, { from: initialAccount }); - - (await token.balanceOf(hasNoTokens.address)).should.be.bignumber.equal(10); - }); - - it('should not accept ERC223 tokens', async function () { - await expectThrow(token.transferERC223(hasNoTokens.address, 10, '', { from: initialAccount })); - }); - - it('should allow owner to reclaim tokens', async function () { - const ownerStartBalance = await token.balanceOf(owner); - await hasNoTokens.reclaimToken(token.address, { from: owner }); - - const ownerFinalBalance = await token.balanceOf(owner); - ownerFinalBalance.sub(ownerStartBalance).should.be.bignumber.equal(10); - - (await token.balanceOf(hasNoTokens.address)).should.be.bignumber.equal(0); - }); - - it('should allow only owner to reclaim tokens', async function () { - await expectThrow( - hasNoTokens.reclaimToken(token.address, { from: anyone }) - ); - }); -}); diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index aa95e04e2ce..6257c01df5a 100644 --- a/test/ownership/Ownable.behavior.js +++ b/test/ownership/Ownable.behavior.js @@ -9,12 +9,12 @@ require('chai') function shouldBehaveLikeOwnable (owner, [anyone]) { describe('as an ownable', function () { it('should have an owner', async function () { - (await this.ownable.owner()).should.eq(owner); + (await this.ownable.owner()).should.equal(owner); }); it('changes owner after transfer', async function () { await this.ownable.transferOwnership(anyone, { from: owner }); - (await this.ownable.owner()).should.eq(anyone); + (await this.ownable.owner()).should.equal(anyone); }); it('should prevent non-owners from transfering', async function () { @@ -27,7 +27,7 @@ function shouldBehaveLikeOwnable (owner, [anyone]) { it('loses owner after renouncement', async function () { await this.ownable.renounceOwnership({ from: owner }); - (await this.ownable.owner()).should.eq(ZERO_ADDRESS); + (await this.ownable.owner()).should.equal(ZERO_ADDRESS); }); it('should prevent non-owners from renouncement', async function () { diff --git a/test/ownership/Superuser.test.js b/test/ownership/Superuser.test.js index 3eac42a7318..5e4d2c32456 100644 --- a/test/ownership/Superuser.test.js +++ b/test/ownership/Superuser.test.js @@ -15,15 +15,15 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) context('in normal conditions', function () { it('should set the owner as the default superuser', async function () { - (await this.superuser.isSuperuser(firstOwner)).should.be.be.true; + (await this.superuser.isSuperuser(firstOwner)).should.equal(true); }); it('should change superuser after transferring', async function () { await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner }); - (await this.superuser.isSuperuser(firstOwner)).should.be.be.false; + (await this.superuser.isSuperuser(firstOwner)).should.equal(false); - (await this.superuser.isSuperuser(newSuperuser)).should.be.be.true; + (await this.superuser.isSuperuser(newSuperuser)).should.equal(true); }); it('should prevent changing to a null superuser', async function () { diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index bc950e5b252..bb09ea73cac 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -14,28 +14,28 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, const amount = web3.toWei(1.0, 'ether'); const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - it('cannot be created with no payees', async function () { + it('rejects an empty set of payees', async function () { await expectThrow(SplitPayment.new([], []), EVMRevert); }); - it('requires shares for each payee', async function () { + it('rejects more payees than shares', async function () { await expectThrow(SplitPayment.new([payee1, payee2, payee3], [20, 30]), EVMRevert); }); - it('requires a payee for each share', async function () { + it('rejects more shares than payees', async function () { await expectThrow(SplitPayment.new([payee1, payee2], [20, 30, 40]), EVMRevert); }); - it('requires non-null payees', async function () { + it('rejects null payees', async function () { await expectThrow(SplitPayment.new([payee1, ZERO_ADDRESS], [20, 30]), EVMRevert); }); - it('requires non-zero shares', async function () { + it('rejects zero-valued shares', async function () { await expectThrow(SplitPayment.new([payee1, payee2], [20, 0]), EVMRevert); }); it('rejects repeated payees', async function () { - await expectThrow(SplitPayment.new([payee1, payee1], [20, 0]), EVMRevert); + await expectThrow(SplitPayment.new([payee1, payee1], [20, 30]), EVMRevert); }); context('once deployed', function () { @@ -53,7 +53,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, }); it('should store shares if address is payee', async function () { - (await this.contract.shares.call(payee1)).should.be.bignumber.not.eq(0); + (await this.contract.shares.call(payee1)).should.be.bignumber.not.equal(0); }); it('should not store shares if address is not payee', async function () { diff --git a/test/proposals/ERC1046/TokenMetadata.test.js b/test/proposals/ERC1046/TokenMetadata.test.js index 9a34991151d..ef0a2ca3dee 100644 --- a/test/proposals/ERC1046/TokenMetadata.test.js +++ b/test/proposals/ERC1046/TokenMetadata.test.js @@ -11,6 +11,6 @@ describe('ERC20WithMetadata', function () { }); it('responds with the metadata', async function () { - (await this.token.tokenURI()).should.eq(metadataURI); + (await this.token.tokenURI()).should.equal(metadataURI); }); }); diff --git a/test/token/ERC20/BurnableToken.behavior.js b/test/token/ERC20/BurnableToken.behavior.js deleted file mode 100644 index b26170e0deb..00000000000 --- a/test/token/ERC20/BurnableToken.behavior.js +++ /dev/null @@ -1,99 +0,0 @@ -const { assertRevert } = require('../../helpers/assertRevert'); -const expectEvent = require('../../helpers/expectEvent'); - -const BigNumber = web3.BigNumber; -const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -function shouldBehaveLikeBurnableToken (owner, initialBalance, [burner]) { - describe('burn', function () { - describe('when the given amount is not greater than balance of the sender', function () { - const amount = 100; - - beforeEach(async function () { - ({ logs: this.logs } = await this.token.burn(amount, { from: owner })); - }); - - it('burns the requested amount', async function () { - (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount); - }); - - it('emits a burn event', async function () { - const event = expectEvent.inLogs(this.logs, 'Burn'); - event.args.burner.should.eq(owner); - event.args.value.should.be.bignumber.equal(amount); - }); - - it('emits a transfer event', async function () { - const event = expectEvent.inLogs(this.logs, 'Transfer'); - event.args.from.should.eq(owner); - event.args.to.should.eq(ZERO_ADDRESS); - event.args.value.should.be.bignumber.equal(amount); - }); - }); - - describe('when the given amount is greater than the balance of the sender', function () { - const amount = initialBalance + 1; - - it('reverts', async function () { - await assertRevert(this.token.burn(amount, { from: owner })); - }); - }); - }); - - describe('burnFrom', function () { - describe('on success', function () { - const amount = 100; - - beforeEach(async function () { - await this.token.approve(burner, 300, { from: owner }); - const { logs } = await this.token.burnFrom(owner, amount, { from: burner }); - this.logs = logs; - }); - - it('burns the requested amount', async function () { - (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount); - }); - - it('decrements allowance', async function () { - (await this.token.allowance(owner, burner)).should.be.bignumber.equal(200); - }); - - it('emits a burn event', async function () { - const event = expectEvent.inLogs(this.logs, 'Burn'); - event.args.burner.should.eq(owner); - event.args.value.should.be.bignumber.equal(amount); - }); - - it('emits a transfer event', async function () { - const event = expectEvent.inLogs(this.logs, 'Transfer'); - event.args.from.should.eq(owner); - event.args.to.should.eq(ZERO_ADDRESS); - event.args.value.should.be.bignumber.equal(amount); - }); - }); - - describe('when the given amount is greater than the balance of the sender', function () { - const amount = initialBalance + 1; - it('reverts', async function () { - await this.token.approve(burner, amount, { from: owner }); - await assertRevert(this.token.burnFrom(owner, amount, { from: burner })); - }); - }); - - describe('when the given amount is greater than the allowance', function () { - const amount = 100; - it('reverts', async function () { - await this.token.approve(burner, amount - 1, { from: owner }); - await assertRevert(this.token.burnFrom(owner, amount, { from: burner })); - }); - }); - }); -} - -module.exports = { - shouldBehaveLikeBurnableToken, -}; diff --git a/test/token/ERC20/BurnableToken.test.js b/test/token/ERC20/BurnableToken.test.js deleted file mode 100644 index 0622a48b607..00000000000 --- a/test/token/ERC20/BurnableToken.test.js +++ /dev/null @@ -1,12 +0,0 @@ -const { shouldBehaveLikeBurnableToken } = require('./BurnableToken.behavior'); -const BurnableTokenMock = artifacts.require('BurnableTokenMock'); - -contract('BurnableToken', function ([_, owner, ...otherAccounts]) { - const initialBalance = 1000; - - beforeEach(async function () { - this.token = await BurnableTokenMock.new(owner, initialBalance, { from: owner }); - }); - - shouldBehaveLikeBurnableToken(owner, initialBalance, otherAccounts); -}); diff --git a/test/token/ERC20/CappedToken.test.js b/test/token/ERC20/CappedToken.test.js deleted file mode 100644 index 1098de2942c..00000000000 --- a/test/token/ERC20/CappedToken.test.js +++ /dev/null @@ -1,25 +0,0 @@ -const { assertRevert } = require('../../helpers/assertRevert'); -const { ether } = require('../../helpers/ether'); -const { shouldBehaveLikeMintableToken } = require('./MintableToken.behavior'); -const { shouldBehaveLikeCappedToken } = require('./CappedToken.behavior'); - -const CappedToken = artifacts.require('CappedToken'); - -contract('Capped', function ([_, owner, ...otherAccounts]) { - const cap = ether(1000); - - it('requires a non-zero cap', async function () { - await assertRevert( - CappedToken.new(0, { from: owner }) - ); - }); - - context('once deployed', async function () { - beforeEach(async function () { - this.token = await CappedToken.new(cap, { from: owner }); - }); - - shouldBehaveLikeCappedToken(owner, otherAccounts, cap); - shouldBehaveLikeMintableToken(owner, owner, otherAccounts); - }); -}); diff --git a/test/token/ERC20/DetailedERC20.test.js b/test/token/ERC20/DetailedERC20.test.js index 4d42f04e6aa..71b22a7b9a8 100644 --- a/test/token/ERC20/DetailedERC20.test.js +++ b/test/token/ERC20/DetailedERC20.test.js @@ -4,9 +4,9 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -const DetailedERC20Mock = artifacts.require('DetailedERC20Mock'); +const ERC20DetailedMock = artifacts.require('ERC20DetailedMock'); -contract('DetailedERC20', function () { +contract('ERC20Detailed', function () { let detailedERC20 = null; const _name = 'My Detailed ERC20'; @@ -14,7 +14,7 @@ contract('DetailedERC20', function () { const _decimals = 18; beforeEach(async function () { - detailedERC20 = await DetailedERC20Mock.new(_name, _symbol, _decimals); + detailedERC20 = await ERC20DetailedMock.new(_name, _symbol, _decimals); }); it('has a name', async function () { diff --git a/test/token/ERC20/StandardToken.test.js b/test/token/ERC20/ERC20.test.js similarity index 90% rename from test/token/ERC20/StandardToken.test.js rename to test/token/ERC20/ERC20.test.js index 590b8111367..a5ca274c79f 100644 --- a/test/token/ERC20/StandardToken.test.js +++ b/test/token/ERC20/ERC20.test.js @@ -1,7 +1,7 @@ const { assertRevert } = require('../../helpers/assertRevert'); const expectEvent = require('../../helpers/expectEvent'); -const StandardToken = artifacts.require('StandardTokenMock'); +const ERC20 = artifacts.require('ERC20Mock'); const BigNumber = web3.BigNumber; @@ -9,11 +9,11 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { +contract('ERC20', function ([_, owner, recipient, anotherAccount]) { const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; beforeEach(async function () { - this.token = await StandardToken.new(owner, 100); + this.token = await ERC20.new(owner, 100); }); describe('total supply', function () { @@ -91,10 +91,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.approve(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); @@ -125,10 +125,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.approve(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); @@ -167,10 +167,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.approve(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); }); @@ -207,10 +207,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits a transfer event', async function () { const { logs } = await this.token.transferFrom(owner, to, amount, { from: spender }); - logs.length.should.eq(1); - logs[0].event.should.eq('Transfer'); - logs[0].args.from.should.eq(owner); - logs[0].args.to.should.eq(to); + logs.length.should.equal(1); + logs[0].event.should.equal('Transfer'); + logs[0].args.from.should.equal(owner); + logs[0].args.to.should.equal(to); logs[0].args.value.should.be.bignumber.equal(amount); }); }); @@ -271,10 +271,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(0); }); @@ -317,10 +317,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(0); }); @@ -359,10 +359,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(0); }); }); @@ -378,10 +378,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.increaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); @@ -412,10 +412,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.increaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); @@ -453,10 +453,10 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { it('emits an approval event', async function () { const { logs } = await this.token.increaseApproval(spender, amount, { from: owner }); - logs.length.should.eq(1); - logs[0].event.should.eq('Approval'); - logs[0].args.owner.should.eq(owner); - logs[0].args.spender.should.eq(spender); + logs.length.should.equal(1); + logs[0].event.should.equal('Approval'); + logs[0].args.owner.should.equal(owner); + logs[0].args.spender.should.equal(spender); logs[0].args.value.should.be.bignumber.equal(amount); }); }); diff --git a/test/token/ERC20/ERC20Burnable.behavior.js b/test/token/ERC20/ERC20Burnable.behavior.js new file mode 100644 index 00000000000..69c5bf51541 --- /dev/null +++ b/test/token/ERC20/ERC20Burnable.behavior.js @@ -0,0 +1,117 @@ +const { assertRevert } = require('../../helpers/assertRevert'); +const expectEvent = require('../../helpers/expectEvent'); + +const BigNumber = web3.BigNumber; +const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) { + describe('burn', function () { + describe('when the given amount is not greater than balance of the sender', function () { + context('for a zero amount', function () { + shouldBurn(0); + }); + + context('for a non-zero amount', function () { + shouldBurn(100); + }); + + function shouldBurn (amount) { + beforeEach(async function () { + ({ logs: this.logs } = await this.token.burn(amount, { from: owner })); + }); + + it('burns the requested amount', async function () { + (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount); + }); + + it('emits a burn event', async function () { + const event = expectEvent.inLogs(this.logs, 'TokensBurned'); + event.args.burner.should.equal(owner); + event.args.value.should.be.bignumber.equal(amount); + }); + + it('emits a transfer event', async function () { + const event = expectEvent.inLogs(this.logs, 'Transfer'); + event.args.from.should.equal(owner); + event.args.to.should.equal(ZERO_ADDRESS); + event.args.value.should.be.bignumber.equal(amount); + }); + } + }); + + describe('when the given amount is greater than the balance of the sender', function () { + const amount = initialBalance + 1; + + it('reverts', async function () { + await assertRevert(this.token.burn(amount, { from: owner })); + }); + }); + }); + + describe('burnFrom', function () { + describe('on success', function () { + context('for a zero amount', function () { + shouldBurnFrom(0); + }); + + context('for a non-zero amount', function () { + shouldBurnFrom(100); + }); + + function shouldBurnFrom (amount) { + const originalAllowance = amount * 3; + + beforeEach(async function () { + await this.token.approve(burner, originalAllowance, { from: owner }); + const { logs } = await this.token.burnFrom(owner, amount, { from: burner }); + this.logs = logs; + }); + + it('burns the requested amount', async function () { + (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount); + }); + + it('decrements allowance', async function () { + (await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance - amount); + }); + + it('emits a burn event', async function () { + const event = expectEvent.inLogs(this.logs, 'TokensBurned'); + event.args.burner.should.equal(owner); + event.args.value.should.be.bignumber.equal(amount); + }); + + it('emits a transfer event', async function () { + const event = expectEvent.inLogs(this.logs, 'Transfer'); + event.args.from.should.equal(owner); + event.args.to.should.equal(ZERO_ADDRESS); + event.args.value.should.be.bignumber.equal(amount); + }); + } + }); + + describe('when the given amount is greater than the balance of the sender', function () { + const amount = initialBalance + 1; + it('reverts', async function () { + await this.token.approve(burner, amount, { from: owner }); + await assertRevert(this.token.burnFrom(owner, amount, { from: burner })); + }); + }); + + describe('when the given amount is greater than the allowance', function () { + const amount = 100; + it('reverts', async function () { + await this.token.approve(burner, amount - 1, { from: owner }); + await assertRevert(this.token.burnFrom(owner, amount, { from: burner })); + }); + }); + }); +} + +module.exports = { + shouldBehaveLikeERC20Burnable, +}; diff --git a/test/token/ERC20/ERC20Burnable.test.js b/test/token/ERC20/ERC20Burnable.test.js new file mode 100644 index 00000000000..0e18e786766 --- /dev/null +++ b/test/token/ERC20/ERC20Burnable.test.js @@ -0,0 +1,12 @@ +const { shouldBehaveLikeERC20Burnable } = require('./ERC20Burnable.behavior'); +const ERC20BurnableMock = artifacts.require('ERC20BurnableMock'); + +contract('ERC20Burnable', function ([_, owner, ...otherAccounts]) { + const initialBalance = 1000; + + beforeEach(async function () { + this.token = await ERC20BurnableMock.new(owner, initialBalance, { from: owner }); + }); + + shouldBehaveLikeERC20Burnable(owner, initialBalance, otherAccounts); +}); diff --git a/test/token/ERC20/CappedToken.behavior.js b/test/token/ERC20/ERC20Capped.behavior.js similarity index 91% rename from test/token/ERC20/CappedToken.behavior.js rename to test/token/ERC20/ERC20Capped.behavior.js index 46c541c597c..004d512c042 100644 --- a/test/token/ERC20/CappedToken.behavior.js +++ b/test/token/ERC20/ERC20Capped.behavior.js @@ -7,7 +7,7 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeCappedToken (minter, [anyone], cap) { +function shouldBehaveLikeERC20Capped (minter, [anyone], cap) { describe('capped token', function () { const from = minter; @@ -33,5 +33,5 @@ function shouldBehaveLikeCappedToken (minter, [anyone], cap) { } module.exports = { - shouldBehaveLikeCappedToken, + shouldBehaveLikeERC20Capped, }; diff --git a/test/token/ERC20/ERC20Capped.test.js b/test/token/ERC20/ERC20Capped.test.js new file mode 100644 index 00000000000..ccbd920a2e6 --- /dev/null +++ b/test/token/ERC20/ERC20Capped.test.js @@ -0,0 +1,25 @@ +const { assertRevert } = require('../../helpers/assertRevert'); +const { ether } = require('../../helpers/ether'); +const { shouldBehaveLikeERC20Mintable } = require('./ERC20Mintable.behavior'); +const { shouldBehaveLikeERC20Capped } = require('./ERC20Capped.behavior'); + +const ERC20Capped = artifacts.require('ERC20Capped'); + +contract('ERC20Capped', function ([_, minter, ...otherAccounts]) { + const cap = ether(1000); + + it('requires a non-zero cap', async function () { + await assertRevert( + ERC20Capped.new(0, [minter]) + ); + }); + + context('once deployed', async function () { + beforeEach(async function () { + this.token = await ERC20Capped.new(cap, [minter]); + }); + + shouldBehaveLikeERC20Capped(minter, otherAccounts, cap); + shouldBehaveLikeERC20Mintable(minter, otherAccounts); + }); +}); diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js new file mode 100644 index 00000000000..0fb68e41a23 --- /dev/null +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -0,0 +1,158 @@ +const { assertRevert } = require('../../helpers/assertRevert'); +const expectEvent = require('../../helpers/expectEvent'); + +const BigNumber = web3.BigNumber; + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +function shouldBehaveLikeERC20Mintable (minter, [anyone]) { + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + + describe('as a mintable token', function () { + describe('mintingFinished', function () { + context('when token minting is not finished', function () { + it('returns false', async function () { + (await this.token.mintingFinished()).should.equal(false); + }); + }); + + context('when token minting is finished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from: minter }); + }); + + it('returns true', async function () { + (await this.token.mintingFinished()).should.equal(true); + }); + }); + }); + + describe('finishMinting', function () { + context('when the sender has minting permission', function () { + const from = minter; + + context('when token minting was not finished', function () { + it('finishes token minting', async function () { + await this.token.finishMinting({ from }); + + (await this.token.mintingFinished()).should.equal(true); + }); + + it('emits a mint finished event', async function () { + const { logs } = await this.token.finishMinting({ from }); + + logs.length.should.be.equal(1); + logs[0].event.should.equal('MintFinished'); + }); + }); + + context('when token minting was already finished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from }); + }); + + it('reverts', async function () { + await assertRevert(this.token.finishMinting({ from })); + }); + }); + }); + + context('when the sender doesn\'t have minting permission', function () { + const from = anyone; + + context('when token minting was not finished', function () { + it('reverts', async function () { + await assertRevert(this.token.finishMinting({ from })); + }); + }); + + context('when token minting was already finished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from: minter }); + }); + + it('reverts', async function () { + await assertRevert(this.token.finishMinting({ from })); + }); + }); + }); + }); + + describe('mint', function () { + const amount = 100; + + context('when the sender has minting permission', function () { + const from = minter; + + context('when token minting is not finished', function () { + context('for a zero amount', function () { + shouldMint(0); + }); + + context('for a non-zero amount', function () { + shouldMint(amount); + }); + + function shouldMint (amount) { + beforeEach(async function () { + ({ logs: this.logs } = await this.token.mint(anyone, amount, { from })); + }); + + it('mints the requested amount', async function () { + (await this.token.balanceOf(anyone)).should.be.bignumber.equal(amount); + }); + + it('emits a mint and a transfer event', async function () { + const mintEvent = expectEvent.inLogs(this.logs, 'Mint', { + to: anyone, + }); + mintEvent.args.amount.should.be.bignumber.equal(amount); + + const transferEvent = expectEvent.inLogs(this.logs, 'Transfer', { + from: ZERO_ADDRESS, + to: anyone, + }); + transferEvent.args.value.should.be.bignumber.equal(amount); + }); + } + }); + + context('when token minting is finished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from: minter }); + }); + + it('reverts', async function () { + await assertRevert(this.token.mint(anyone, amount, { from })); + }); + }); + }); + + context('when the sender doesn\'t have minting permission', function () { + const from = anyone; + + context('when token minting is not finished', function () { + it('reverts', async function () { + await assertRevert(this.token.mint(anyone, amount, { from })); + }); + }); + + context('when token minting is already finished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from: minter }); + }); + + it('reverts', async function () { + await assertRevert(this.token.mint(anyone, amount, { from })); + }); + }); + }); + }); + }); +} + +module.exports = { + shouldBehaveLikeERC20Mintable, +}; diff --git a/test/token/ERC20/ERC20Mintable.test.js b/test/token/ERC20/ERC20Mintable.test.js new file mode 100644 index 00000000000..c838f0ccd02 --- /dev/null +++ b/test/token/ERC20/ERC20Mintable.test.js @@ -0,0 +1,22 @@ +const { shouldBehaveLikeERC20Mintable } = require('./ERC20Mintable.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const ERC20MintableMock = artifacts.require('ERC20MintableMock'); + +contract('ERC20Mintable', function ([_, originalMinter, otherMinter, ...otherAccounts]) { + beforeEach(async function () { + this.token = await ERC20MintableMock.new([originalMinter, otherMinter]); + }); + + context('with original minter', function () { + shouldBehaveLikeERC20Mintable(originalMinter, otherAccounts); + }); + + describe('minter role', function () { + beforeEach(async function () { + await this.token.addMinter(otherMinter); + this.contract = this.token; + }); + + shouldBehaveLikePublicRole(originalMinter, otherMinter, otherAccounts, 'minter'); + }); +}); diff --git a/test/token/ERC20/PausableToken.test.js b/test/token/ERC20/ERC20Pausable.test.js similarity index 91% rename from test/token/ERC20/PausableToken.test.js rename to test/token/ERC20/ERC20Pausable.test.js index ef3cded326b..8fbe4f70876 100644 --- a/test/token/ERC20/PausableToken.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -1,9 +1,9 @@ const { assertRevert } = require('../../helpers/assertRevert'); -const PausableToken = artifacts.require('PausableTokenMock'); +const ERC20Pausable = artifacts.require('ERC20PausableMock'); -contract('PausableToken', function ([_, owner, recipient, anotherAccount]) { +contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { beforeEach(async function () { - this.token = await PausableToken.new(owner, 100, { from: owner }); + this.token = await ERC20Pausable.new(owner, 100, { from: owner }); }); describe('pause', function () { @@ -13,14 +13,14 @@ contract('PausableToken', function ([_, owner, recipient, anotherAccount]) { describe('when the token is unpaused', function () { it('pauses the token', async function () { await this.token.pause({ from }); - (await this.token.paused()).should.be.true; + (await this.token.paused()).should.equal(true); }); it('emits a Pause event', async function () { const { logs } = await this.token.pause({ from }); - logs.length.should.eq(1); - logs[0].event.should.eq('Pause'); + logs.length.should.equal(1); + logs[0].event.should.equal('Paused'); }); }); @@ -55,14 +55,14 @@ contract('PausableToken', function ([_, owner, recipient, anotherAccount]) { it('unpauses the token', async function () { await this.token.unpause({ from }); - (await this.token.paused()).should.be.false; + (await this.token.paused()).should.equal(false); }); it('emits an Unpause event', async function () { const { logs } = await this.token.unpause({ from }); - logs.length.should.eq(1); - logs[0].event.should.eq('Unpause'); + logs.length.should.equal(1); + logs[0].event.should.equal('Unpaused'); }); }); @@ -87,18 +87,18 @@ contract('PausableToken', function ([_, owner, recipient, anotherAccount]) { describe('paused', function () { it('is not paused by default', async function () { - (await this.token.paused({ from })).should.be.false; + (await this.token.paused({ from })).should.equal(false); }); it('is paused after being paused', async function () { await this.token.pause({ from }); - (await this.token.paused({ from })).should.be.true; + (await this.token.paused({ from })).should.equal(true); }); it('is not paused after being paused and then unpaused', async function () { await this.token.pause({ from }); await this.token.unpause({ from }); - (await this.token.paused()).should.be.false; + (await this.token.paused()).should.equal(false); }); }); diff --git a/test/token/ERC20/MintableToken.behavior.js b/test/token/ERC20/MintableToken.behavior.js deleted file mode 100644 index ccc8b464a64..00000000000 --- a/test/token/ERC20/MintableToken.behavior.js +++ /dev/null @@ -1,154 +0,0 @@ -const { assertRevert } = require('../../helpers/assertRevert'); -const expectEvent = require('../../helpers/expectEvent'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -function shouldBehaveLikeMintableToken (owner, minter, [anyone]) { - const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - - describe('as a basic mintable token', function () { - describe('after token creation', function () { - it('sender should be token owner', async function () { - (await this.token.owner({ from: owner })).should.equal(owner); - }); - }); - - describe('minting finished', function () { - describe('when the token minting is not finished', function () { - it('returns false', async function () { - (await this.token.mintingFinished()).should.be.false; - }); - }); - - describe('when the token is minting finished', function () { - beforeEach(async function () { - await this.token.finishMinting({ from: owner }); - }); - - it('returns true', async function () { - (await this.token.mintingFinished()).should.be.true; - }); - }); - }); - - describe('finish minting', function () { - describe('when the sender is the token owner', function () { - const from = owner; - - describe('when the token minting was not finished', function () { - it('finishes token minting', async function () { - await this.token.finishMinting({ from }); - - (await this.token.mintingFinished()).should.be.true; - }); - - it('emits a mint finished event', async function () { - const { logs } = await this.token.finishMinting({ from }); - - logs.length.should.be.equal(1); - logs[0].event.should.eq('MintFinished'); - }); - }); - - describe('when the token minting was already finished', function () { - beforeEach(async function () { - await this.token.finishMinting({ from }); - }); - - it('reverts', async function () { - await assertRevert(this.token.finishMinting({ from })); - }); - }); - }); - - describe('when the sender is not the token owner', function () { - const from = anyone; - - describe('when the token minting was not finished', function () { - it('reverts', async function () { - await assertRevert(this.token.finishMinting({ from })); - }); - }); - - describe('when the token minting was already finished', function () { - beforeEach(async function () { - await this.token.finishMinting({ from: owner }); - }); - - it('reverts', async function () { - await assertRevert(this.token.finishMinting({ from })); - }); - }); - }); - }); - - describe('mint', function () { - const amount = 100; - - describe('when the sender has the minting permission', function () { - const from = minter; - - describe('when the token minting is not finished', function () { - it('mints the requested amount', async function () { - await this.token.mint(owner, amount, { from }); - - (await this.token.balanceOf(owner)).should.be.bignumber.equal(amount); - }); - - it('emits a mint and a transfer event', async function () { - const { logs } = await this.token.mint(owner, amount, { from }); - - const mintEvent = expectEvent.inLogs(logs, 'Mint', { - to: owner, - }); - mintEvent.args.amount.should.be.bignumber.equal(amount); - - const transferEvent = expectEvent.inLogs(logs, 'Transfer', { - from: ZERO_ADDRESS, - to: owner, - }); - transferEvent.args.value.should.be.bignumber.equal(amount); - }); - }); - - describe('when the token minting is finished', function () { - beforeEach(async function () { - await this.token.finishMinting({ from: owner }); - }); - - it('reverts', async function () { - await assertRevert(this.token.mint(owner, amount, { from })); - }); - }); - }); - - describe('when the sender has not the minting permission', function () { - const from = anyone; - - describe('when the token minting is not finished', function () { - it('reverts', async function () { - await assertRevert(this.token.mint(owner, amount, { from })); - }); - }); - - describe('when the token minting is already finished', function () { - beforeEach(async function () { - await this.token.finishMinting({ from: owner }); - }); - - it('reverts', async function () { - await assertRevert(this.token.mint(owner, amount, { from })); - }); - }); - }); - }); - }); -} - -module.exports = { - shouldBehaveLikeMintableToken, -}; diff --git a/test/token/ERC20/MintableToken.test.js b/test/token/ERC20/MintableToken.test.js deleted file mode 100644 index c06380a80ce..00000000000 --- a/test/token/ERC20/MintableToken.test.js +++ /dev/null @@ -1,10 +0,0 @@ -const { shouldBehaveLikeMintableToken } = require('./MintableToken.behavior'); -const MintableToken = artifacts.require('MintableToken'); - -contract('MintableToken', function ([_, owner, ...otherAccounts]) { - beforeEach(async function () { - this.token = await MintableToken.new({ from: owner }); - }); - - shouldBehaveLikeMintableToken(owner, owner, otherAccounts); -}); diff --git a/test/token/ERC20/TokenTimelock.test.js b/test/token/ERC20/TokenTimelock.test.js index 4f3535efca5..d7c80f0ab16 100644 --- a/test/token/ERC20/TokenTimelock.test.js +++ b/test/token/ERC20/TokenTimelock.test.js @@ -8,15 +8,15 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -const MintableToken = artifacts.require('MintableToken'); +const ERC20Mintable = artifacts.require('ERC20Mintable'); const TokenTimelock = artifacts.require('TokenTimelock'); -contract('TokenTimelock', function ([_, owner, beneficiary]) { +contract('TokenTimelock', function ([_, minter, beneficiary]) { const amount = new BigNumber(100); context('with token', function () { beforeEach(async function () { - this.token = await MintableToken.new({ from: owner }); + this.token = await ERC20Mintable.new([minter]); }); it('rejects a release time in the past', async function () { @@ -30,7 +30,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { beforeEach(async function () { this.releaseTime = (await latestTime()) + duration.years(1); this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime); - await this.token.mint(this.timelock.address, amount, { from: owner }); + await this.token.mint(this.timelock.address, amount, { from: minter }); }); it('cannot be released before time limit', async function () { diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index 5c1a8503dd8..5138289a66b 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -10,10 +10,10 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -const MintableToken = artifacts.require('MintableToken'); +const ERC20Mintable = artifacts.require('ERC20Mintable'); const TokenVesting = artifacts.require('TokenVesting'); -contract('TokenVesting', function ([_, owner, beneficiary]) { +contract('TokenVesting', function ([_, owner, beneficiary, minter]) { const amount = new BigNumber(1000); const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; @@ -44,8 +44,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { beforeEach(async function () { this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner }); - this.token = await MintableToken.new({ from: owner }); - await this.token.mint(this.vesting.address, amount, { from: owner }); + this.token = await ERC20Mintable.new([minter]); + await this.token.mint(this.vesting.address, amount, { from: minter }); }); it('cannot be released before cliff', async function () { @@ -67,7 +67,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const block = await ethGetBlock(receipt.blockNumber); const releaseTime = block.timestamp; - (await this.token.balanceOf(beneficiary)).should.bignumber.eq( + (await this.token.balanceOf(beneficiary)).should.bignumber.equal( amount.mul(releaseTime - this.start).div(this.duration).floor() ); }); @@ -82,14 +82,14 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await this.vesting.release(this.token.address); const expectedVesting = amount.mul(now - this.start).div(this.duration).floor(); - (await this.token.balanceOf(beneficiary)).should.bignumber.eq(expectedVesting); + (await this.token.balanceOf(beneficiary)).should.bignumber.equal(expectedVesting); } }); it('should have released all after end', async function () { await increaseTimeTo(this.start + this.duration); await this.vesting.release(this.token.address); - (await this.token.balanceOf(beneficiary)).should.bignumber.eq(amount); + (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount); }); it('should be revoked by owner if revocable is set', async function () { @@ -114,7 +114,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await this.vesting.revoke(this.token.address, { from: owner }); - (await this.token.balanceOf(owner)).should.bignumber.eq(amount.sub(vested)); + (await this.token.balanceOf(owner)).should.bignumber.equal(amount.sub(vested)); }); it('should keep the vested tokens when revoked by owner', async function () { @@ -126,7 +126,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const vestedPost = await this.vesting.vestedAmount(this.token.address); - vestedPre.should.bignumber.eq(vestedPost); + vestedPre.should.bignumber.equal(vestedPost); }); it('should fail to be revoked a second time', async function () { diff --git a/test/token/ERC721/ERC721Token.test.js b/test/token/ERC721/ERC721.test.js similarity index 91% rename from test/token/ERC721/ERC721Token.test.js rename to test/token/ERC721/ERC721.test.js index 0752d9ddc15..5469c87cf28 100644 --- a/test/token/ERC721/ERC721Token.test.js +++ b/test/token/ERC721/ERC721.test.js @@ -1,17 +1,17 @@ const { assertRevert } = require('../../helpers/assertRevert'); -const { shouldBehaveLikeERC721BasicToken } = require('./ERC721BasicToken.behavior'); -const { shouldBehaveLikeMintAndBurnERC721Token } = require('./ERC721MintBurn.behavior'); +const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); +const { shouldBehaveLikeMintAndBurnERC721 } = require('./ERC721MintBurn.behavior'); const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior'); const _ = require('lodash'); const BigNumber = web3.BigNumber; -const ERC721Token = artifacts.require('ERC721TokenMock.sol'); +const ERC721 = artifacts.require('ERC721Mock.sol'); require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721Token', function (accounts) { +contract('ERC721', function (accounts) { const name = 'Non Fungible Token'; const symbol = 'NFT'; const firstTokenId = 100; @@ -21,11 +21,11 @@ contract('ERC721Token', function (accounts) { const anyone = accounts[9]; beforeEach(async function () { - this.token = await ERC721Token.new(name, symbol, { from: creator }); + this.token = await ERC721.new(name, symbol, { from: creator }); }); - shouldBehaveLikeERC721BasicToken(accounts); - shouldBehaveLikeMintAndBurnERC721Token(accounts); + shouldBehaveLikeERC721Basic(accounts); + shouldBehaveLikeMintAndBurnERC721(accounts); describe('like a full ERC721', function () { beforeEach(async function () { @@ -76,13 +76,13 @@ contract('ERC721Token', function (accounts) { describe('removeTokenFrom', function () { it('reverts if the correct owner is not passed', async function () { await assertRevert( - this.token._removeTokenFrom(anyone, firstTokenId, { from: creator }) + this.token.removeTokenFrom(anyone, firstTokenId, { from: creator }) ); }); context('once removed', function () { beforeEach(async function () { - await this.token._removeTokenFrom(creator, firstTokenId, { from: creator }); + await this.token.removeTokenFrom(creator, firstTokenId, { from: creator }); }); it('has been removed', async function () { @@ -126,7 +126,7 @@ contract('ERC721Token', function (accounts) { it('can burn token with metadata', async function () { await this.token.setTokenURI(firstTokenId, sampleUri); await this.token.burn(firstTokenId); - (await this.token.exists(firstTokenId)).should.be.false; + (await this.token.exists(firstTokenId)).should.equal(false); }); it('returns empty metadata for token', async function () { diff --git a/test/token/ERC721/ERC721BasicToken.behavior.js b/test/token/ERC721/ERC721Basic.behavior.js similarity index 90% rename from test/token/ERC721/ERC721BasicToken.behavior.js rename to test/token/ERC721/ERC721Basic.behavior.js index 27c48dc22fb..765614afce7 100644 --- a/test/token/ERC721/ERC721BasicToken.behavior.js +++ b/test/token/ERC721/ERC721Basic.behavior.js @@ -11,7 +11,7 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeERC721BasicToken (accounts) { +function shouldBehaveLikeERC721Basic (accounts) { const firstTokenId = 1; const secondTokenId = 2; const unknownTokenId = 3; @@ -19,7 +19,7 @@ function shouldBehaveLikeERC721BasicToken (accounts) { const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const RECEIVER_MAGIC_VALUE = '0x150b7a02'; - describe('like an ERC721BasicToken', function () { + describe('like an ERC721Basic', function () { beforeEach(async function () { await this.token.mint(creator, firstTokenId, { from: creator }); await this.token.mint(creator, secondTokenId, { from: creator }); @@ -92,17 +92,17 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('emit only a transfer event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args._from.should.be.equal(owner); - logs[0].args._to.should.be.equal(this.to); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.from.should.be.equal(owner); + logs[0].args.to.should.be.equal(this.to); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); } else { it('emits only a transfer event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args._from.should.be.equal(owner); - logs[0].args._to.should.be.equal(this.to); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.from.should.be.equal(owner); + logs[0].args.to.should.be.equal(this.to); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); } @@ -117,7 +117,7 @@ function shouldBehaveLikeERC721BasicToken (accounts) { (await this.token.tokenOfOwnerByIndex(this.to, 0)).toNumber().should.be.equal(tokenId); - (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.not.be.eq(tokenId); + (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.not.be.equal(tokenId); }); }; @@ -167,9 +167,9 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('emits only a transfer event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args._from.should.be.equal(owner); - logs[0].args._to.should.be.equal(owner); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.from.should.be.equal(owner); + logs[0].args.to.should.be.equal(owner); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); it('keeps the owner balance', async function () { @@ -247,10 +247,10 @@ function shouldBehaveLikeERC721BasicToken (accounts) { result.receipt.logs.length.should.be.equal(2); const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address); log.event.should.be.equal('Received'); - log.args._operator.should.be.equal(owner); - log.args._from.should.be.equal(owner); - log.args._tokenId.toNumber().should.be.equal(tokenId); - log.args._data.should.be.equal(data); + log.args.operator.should.be.equal(owner); + log.args.from.should.be.equal(owner); + log.args.tokenId.toNumber().should.be.equal(tokenId); + log.args.data.should.be.equal(data); }); it('should call onERC721Received from approved', async function () { @@ -258,10 +258,10 @@ function shouldBehaveLikeERC721BasicToken (accounts) { result.receipt.logs.length.should.be.equal(2); const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address); log.event.should.be.equal('Received'); - log.args._operator.should.be.equal(approved); - log.args._from.should.be.equal(owner); - log.args._tokenId.toNumber().should.be.equal(tokenId); - log.args._data.should.be.equal(data); + log.args.operator.should.be.equal(approved); + log.args.from.should.be.equal(owner); + log.args.tokenId.toNumber().should.be.equal(tokenId); + log.args.data.should.be.equal(data); }); describe('with an invalid token id', function () { @@ -334,9 +334,9 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('emits an approval event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Approval'); - logs[0].args._owner.should.be.equal(sender); - logs[0].args._approved.should.be.equal(address); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.owner.should.be.equal(sender); + logs[0].args.approved.should.be.equal(address); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); }; @@ -439,7 +439,7 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('approves the operator', async function () { await this.token.setApprovalForAll(operator, true, { from: sender }); - (await this.token.isApprovedForAll(sender, operator)).should.be.true; + (await this.token.isApprovedForAll(sender, operator)).should.equal(true); }); it('emits an approval event', async function () { @@ -447,9 +447,9 @@ function shouldBehaveLikeERC721BasicToken (accounts) { logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args._owner.should.be.equal(sender); - logs[0].args._operator.should.be.equal(operator); - logs[0].args._approved.should.be.true; + logs[0].args.owner.should.be.equal(sender); + logs[0].args.operator.should.be.equal(operator); + logs[0].args.approved.should.equal(true); }); }); @@ -461,7 +461,7 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('approves the operator', async function () { await this.token.setApprovalForAll(operator, true, { from: sender }); - (await this.token.isApprovedForAll(sender, operator)).should.be.true; + (await this.token.isApprovedForAll(sender, operator)).should.equal(true); }); it('emits an approval event', async function () { @@ -469,15 +469,15 @@ function shouldBehaveLikeERC721BasicToken (accounts) { logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args._owner.should.be.equal(sender); - logs[0].args._operator.should.be.equal(operator); - logs[0].args._approved.should.be.true; + logs[0].args.owner.should.be.equal(sender); + logs[0].args.operator.should.be.equal(operator); + logs[0].args.approved.should.equal(true); }); it('can unset the operator approval', async function () { await this.token.setApprovalForAll(operator, false, { from: sender }); - (await this.token.isApprovedForAll(sender, operator)).should.be.false; + (await this.token.isApprovedForAll(sender, operator)).should.equal(false); }); }); @@ -489,7 +489,7 @@ function shouldBehaveLikeERC721BasicToken (accounts) { it('keeps the approval to the given address', async function () { await this.token.setApprovalForAll(operator, true, { from: sender }); - (await this.token.isApprovedForAll(sender, operator)).should.be.true; + (await this.token.isApprovedForAll(sender, operator)).should.equal(true); }); it('emits an approval event', async function () { @@ -497,9 +497,9 @@ function shouldBehaveLikeERC721BasicToken (accounts) { logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args._owner.should.be.equal(sender); - logs[0].args._operator.should.be.equal(operator); - logs[0].args._approved.should.be.true; + logs[0].args.owner.should.be.equal(sender); + logs[0].args.operator.should.be.equal(operator); + logs[0].args.approved.should.equal(true); }); }); }); @@ -521,5 +521,5 @@ function shouldBehaveLikeERC721BasicToken (accounts) { } module.exports = { - shouldBehaveLikeERC721BasicToken, + shouldBehaveLikeERC721Basic, }; diff --git a/test/token/ERC721/ERC721Basic.test.js b/test/token/ERC721/ERC721Basic.test.js new file mode 100644 index 00000000000..f86a97f5b68 --- /dev/null +++ b/test/token/ERC721/ERC721Basic.test.js @@ -0,0 +1,18 @@ +const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); +const { shouldBehaveLikeMintAndBurnERC721 } = require('./ERC721MintBurn.behavior'); + +const BigNumber = web3.BigNumber; +const ERC721Basic = artifacts.require('ERC721BasicMock.sol'); + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('ERC721Basic', function (accounts) { + beforeEach(async function () { + this.token = await ERC721Basic.new({ from: accounts[0] }); + }); + + shouldBehaveLikeERC721Basic(accounts); + shouldBehaveLikeMintAndBurnERC721(accounts); +}); diff --git a/test/token/ERC721/ERC721BasicToken.test.js b/test/token/ERC721/ERC721BasicToken.test.js deleted file mode 100644 index 4525c7dc57d..00000000000 --- a/test/token/ERC721/ERC721BasicToken.test.js +++ /dev/null @@ -1,18 +0,0 @@ -const { shouldBehaveLikeERC721BasicToken } = require('./ERC721BasicToken.behavior'); -const { shouldBehaveLikeMintAndBurnERC721Token } = require('./ERC721MintBurn.behavior'); - -const BigNumber = web3.BigNumber; -const ERC721BasicToken = artifacts.require('ERC721BasicTokenMock.sol'); - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('ERC721BasicToken', function (accounts) { - beforeEach(async function () { - this.token = await ERC721BasicToken.new({ from: accounts[0] }); - }); - - shouldBehaveLikeERC721BasicToken(accounts); - shouldBehaveLikeMintAndBurnERC721Token(accounts); -}); diff --git a/test/token/ERC721/ERC721MintBurn.behavior.js b/test/token/ERC721/ERC721MintBurn.behavior.js index c09480a38b7..c165f170356 100644 --- a/test/token/ERC721/ERC721MintBurn.behavior.js +++ b/test/token/ERC721/ERC721MintBurn.behavior.js @@ -5,14 +5,14 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeMintAndBurnERC721Token (accounts) { +function shouldBehaveLikeMintAndBurnERC721 (accounts) { const firstTokenId = 1; const secondTokenId = 2; const unknownTokenId = 3; const creator = accounts[0]; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - describe('like a mintable and burnable ERC721Token', function () { + describe('like a mintable and burnable ERC721', function () { beforeEach(async function () { await this.token.mint(creator, firstTokenId, { from: creator }); await this.token.mint(creator, secondTokenId, { from: creator }); @@ -40,9 +40,9 @@ function shouldBehaveLikeMintAndBurnERC721Token (accounts) { it('emits a transfer event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args._from.should.be.equal(ZERO_ADDRESS); - logs[0].args._to.should.be.equal(to); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.from.should.be.equal(ZERO_ADDRESS); + logs[0].args.to.should.be.equal(to); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); }); @@ -78,9 +78,9 @@ function shouldBehaveLikeMintAndBurnERC721Token (accounts) { it('emits a burn event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args._from.should.be.equal(sender); - logs[0].args._to.should.be.equal(ZERO_ADDRESS); - logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args.from.should.be.equal(sender); + logs[0].args.to.should.be.equal(ZERO_ADDRESS); + logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); }); @@ -106,5 +106,5 @@ function shouldBehaveLikeMintAndBurnERC721Token (accounts) { } module.exports = { - shouldBehaveLikeMintAndBurnERC721Token, + shouldBehaveLikeMintAndBurnERC721, }; diff --git a/test/token/ERC721/ERC721Pausable.test.js b/test/token/ERC721/ERC721Pausable.test.js new file mode 100644 index 00000000000..7c03c888ab5 --- /dev/null +++ b/test/token/ERC721/ERC721Pausable.test.js @@ -0,0 +1,36 @@ +const { shouldBehaveLikeERC721PausedToken } = require('./ERC721PausedToken.behavior'); +const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); + +const BigNumber = web3.BigNumber; +const ERC721Pausable = artifacts.require('ERC721PausableMock.sol'); + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('ERC721Pausable', function ([_, owner, recipient, operator, ...otherAccounts]) { + beforeEach(async function () { + this.token = await ERC721Pausable.new({ from: owner }); + }); + + context('when token is paused', function () { + beforeEach(async function () { + await this.token.pause({ from: owner }); + }); + + shouldBehaveLikeERC721PausedToken(owner, [...otherAccounts]); + }); + + context('when token is not paused yet', function () { + shouldBehaveLikeERC721Basic([owner, ...otherAccounts]); + }); + + context('when token is paused and then unpaused', function () { + beforeEach(async function () { + await this.token.pause({ from: owner }); + await this.token.unpause({ from: owner }); + }); + + shouldBehaveLikeERC721Basic([owner, ...otherAccounts]); + }); +}); diff --git a/test/token/ERC721/ERC721PausedToken.behavior.js b/test/token/ERC721/ERC721PausedToken.behavior.js new file mode 100644 index 00000000000..d14e6a39533 --- /dev/null +++ b/test/token/ERC721/ERC721PausedToken.behavior.js @@ -0,0 +1,88 @@ +const { assertRevert } = require('../../helpers/assertRevert'); +const { sendTransaction } = require('../../helpers/sendTransaction'); + +const BigNumber = web3.BigNumber; + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +function shouldBehaveLikeERC721PausedToken (owner, [recipient, operator]) { + const firstTokenId = 1; + const mintedTokens = 1; + const mockData = '0x42'; + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + + describe('like a paused ERC721', function () { + beforeEach(async function () { + await this.token.mint(owner, firstTokenId, { from: owner }); + }); + + it('reverts when trying to approve', async function () { + await assertRevert(this.token.approve(recipient, firstTokenId, { from: owner })); + }); + + it('reverts when trying to setApprovalForAll', async function () { + await assertRevert(this.token.setApprovalForAll(operator, true, { from: owner })); + }); + + it('reverts when trying to transferFrom', async function () { + await assertRevert(this.token.transferFrom(owner, recipient, firstTokenId, { from: owner })); + }); + + it('reverts when trying to safeTransferFrom', async function () { + await assertRevert(this.token.safeTransferFrom(owner, recipient, firstTokenId, { from: owner })); + }); + + it('reverts when trying to safeTransferFrom with data', async function () { + await assertRevert( + sendTransaction( + this.token, + 'safeTransferFrom', + 'address,address,uint256,bytes', + [owner, recipient, firstTokenId, mockData], + { from: owner } + ) + ); + }); + + describe('getApproved', function () { + it('returns approved address', async function () { + const approvedAccount = await this.token.getApproved(firstTokenId); + approvedAccount.should.be.equal(ZERO_ADDRESS); + }); + }); + + describe('balanceOf', function () { + it('returns the amount of tokens owned by the given address', async function () { + const balance = await this.token.balanceOf(owner); + balance.should.be.bignumber.equal(mintedTokens); + }); + }); + + describe('ownerOf', function () { + it('returns the amount of tokens owned by the given address', async function () { + const ownerOfToken = await this.token.ownerOf(firstTokenId); + ownerOfToken.should.be.equal(owner); + }); + }); + + describe('exists', function () { + it('should return token existance', async function () { + const result = await this.token.exists(firstTokenId); + result.should.eq(true); + }); + }); + + describe('isApprovedForAll', function () { + it('returns the approval of the operator', async function () { + const isApproved = await this.token.isApprovedForAll(owner, operator); + isApproved.should.eq(false); + }); + }); + }); +} + +module.exports = { + shouldBehaveLikeERC721PausedToken, +}; From a14963bd2929a26ccd0d710b5885c756c0283d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 6 Sep 2018 10:17:22 -0300 Subject: [PATCH 06/14] Improved Roles API. (#1280) * Improved Roles API. * fix linter error --- contracts/access/rbac/MinterRole.sol | 22 ++++------ contracts/access/rbac/Roles.sol | 22 ---------- contracts/examples/SampleCrowdsale.sol | 6 --- contracts/mocks/ERC20MintableMock.sol | 7 ---- contracts/mocks/RolesMock.sol | 12 ------ contracts/token/ERC20/ERC20Capped.sol | 3 +- contracts/token/ERC20/ERC20Mintable.sol | 6 --- test/access/rbac/PublicRole.behavior.js | 36 ++-------------- test/access/rbac/Roles.test.js | 55 +------------------------ test/crowdsale/MintedCrowdsale.test.js | 8 ++-- test/examples/SampleCrowdsale.test.js | 8 ++-- test/token/ERC20/ERC20Capped.test.js | 4 +- test/token/ERC20/ERC20Mintable.test.js | 10 ++--- test/token/ERC20/TokenTimelock.test.js | 2 +- test/token/ERC20/TokenVesting.test.js | 2 +- 15 files changed, 34 insertions(+), 169 deletions(-) diff --git a/contracts/access/rbac/MinterRole.sol b/contracts/access/rbac/MinterRole.sol index b01a41de6c5..7fd7d8decaf 100644 --- a/contracts/access/rbac/MinterRole.sol +++ b/contracts/access/rbac/MinterRole.sol @@ -8,29 +8,25 @@ contract MinterRole { Roles.Role private minters; - constructor(address[] _minters) public { - minters.addMany(_minters); + constructor() public { + minters.add(msg.sender); } - function transferMinter(address _account) public { - minters.transfer(_account); - } - - function renounceMinter() public { - minters.renounce(); + modifier onlyMinter() { + require(isMinter(msg.sender)); + _; } function isMinter(address _account) public view returns (bool) { return minters.has(_account); } - modifier onlyMinter() { - require(isMinter(msg.sender)); - _; + function addMinter(address _account) public onlyMinter { + minters.add(_account); } - function _addMinter(address _account) internal { - minters.add(_account); + function renounceMinter() public { + minters.remove(msg.sender); } function _removeMinter(address _account) internal { diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/rbac/Roles.sol index f93187e6bd3..e3d9f867bf1 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/rbac/Roles.sol @@ -19,15 +19,6 @@ library Roles { _role.bearer[_account] = true; } - /** - * @dev give multiple accounts access to this role - */ - function addMany(Role storage _role, address[] _accounts) internal { - for (uint256 i = 0; i < _accounts.length; ++i) { - add(_role, _accounts[i]); - } - } - /** * @dev remove an account's access to this role */ @@ -35,19 +26,6 @@ library Roles { _role.bearer[_account] = false; } - function transfer(Role storage _role, address _account) internal { - require(_account != address(0)); - require(!has(_role, _account)); - require(has(_role, msg.sender)); - - remove(_role, msg.sender); - add(_role, _account); - } - - function renounce(Role storage _role) internal { - remove(_role, msg.sender); - } - /** * @dev check if an account has this role * @return bool diff --git a/contracts/examples/SampleCrowdsale.sol b/contracts/examples/SampleCrowdsale.sol index 61b82dc27f2..5773f5d8646 100644 --- a/contracts/examples/SampleCrowdsale.sol +++ b/contracts/examples/SampleCrowdsale.sol @@ -16,12 +16,6 @@ contract SampleCrowdsaleToken is ERC20Mintable { string public constant name = "Sample Crowdsale Token"; string public constant symbol = "SCT"; uint8 public constant decimals = 18; - - constructor(address[] _minters) - ERC20Mintable(_minters) - public - { - } } diff --git a/contracts/mocks/ERC20MintableMock.sol b/contracts/mocks/ERC20MintableMock.sol index 6ec365bc660..39345081f4d 100644 --- a/contracts/mocks/ERC20MintableMock.sol +++ b/contracts/mocks/ERC20MintableMock.sol @@ -5,13 +5,6 @@ import "../token/ERC20/ERC20Mintable.sol"; // Mock contract exposing internal methods contract ERC20MintableMock is ERC20Mintable { - constructor(address[] minters) ERC20Mintable(minters) public { - } - - function addMinter(address _account) public { - _addMinter(_account); - } - function removeMinter(address _account) public { _removeMinter(_account); } diff --git a/contracts/mocks/RolesMock.sol b/contracts/mocks/RolesMock.sol index 05cbbeaf2f5..22e14312652 100644 --- a/contracts/mocks/RolesMock.sol +++ b/contracts/mocks/RolesMock.sol @@ -12,22 +12,10 @@ contract RolesMock { dummyRole.add(_account); } - function addMany(address[] _accounts) public { - dummyRole.addMany(_accounts); - } - function remove(address _account) public { dummyRole.remove(_account); } - function renounce() public { - dummyRole.renounce(); - } - - function transfer(address _account) public { - dummyRole.transfer(_account); - } - function has(address _account) public view returns (bool) { return dummyRole.has(_account); } diff --git a/contracts/token/ERC20/ERC20Capped.sol b/contracts/token/ERC20/ERC20Capped.sol index a21a9107a62..289b6585ca9 100644 --- a/contracts/token/ERC20/ERC20Capped.sol +++ b/contracts/token/ERC20/ERC20Capped.sol @@ -11,9 +11,8 @@ contract ERC20Capped is ERC20Mintable { uint256 public cap; - constructor(uint256 _cap, address[] _minters) + constructor(uint256 _cap) public - ERC20Mintable(_minters) { require(_cap > 0); cap = _cap; diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index b746aa91783..ae9019b36dc 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -15,12 +15,6 @@ contract ERC20Mintable is ERC20, MinterRole { bool public mintingFinished = false; - constructor(address[] _minters) - MinterRole(_minters) - public - { - } - modifier canMint() { require(!mintingFinished); _; diff --git a/test/access/rbac/PublicRole.behavior.js b/test/access/rbac/PublicRole.behavior.js index 74012fe41b1..80d78e917b5 100644 --- a/test/access/rbac/PublicRole.behavior.js +++ b/test/access/rbac/PublicRole.behavior.js @@ -1,5 +1,3 @@ -const { assertRevert } = require('../../helpers/assertRevert'); - require('chai') .should(); @@ -20,17 +18,17 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role describe('add', function () { it('adds role to a new account', async function () { - await this.contract[`add${rolename}`](anyone); + await this.contract[`add${rolename}`](anyone, { from: authorized }); (await this.contract[`is${rolename}`](anyone)).should.equal(true); }); it('adds role to an already-assigned account', async function () { - await this.contract[`add${rolename}`](authorized); + await this.contract[`add${rolename}`](authorized, { from: authorized }); (await this.contract[`is${rolename}`](authorized)).should.equal(true); }); it('doesn\'t revert when adding role to the null account', async function () { - await this.contract[`add${rolename}`](ZERO_ADDRESS); + await this.contract[`add${rolename}`](ZERO_ADDRESS, { from: authorized }); }); }); @@ -50,34 +48,6 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role }); }); - describe('transfering', function () { - context('from account with role', function () { - const from = authorized; - - it('transfers to other account without the role', async function () { - await this.contract[`transfer${rolename}`](anyone, { from }); - (await this.contract[`is${rolename}`](anyone)).should.equal(true); - (await this.contract[`is${rolename}`](authorized)).should.equal(false); - }); - - it('reverts when transfering to an account with role', async function () { - await assertRevert(this.contract[`transfer${rolename}`](otherAuthorized, { from })); - }); - - it('reverts when transfering to the null account', async function () { - await assertRevert(this.contract[`transfer${rolename}`](ZERO_ADDRESS, { from })); - }); - }); - - context('from account without role', function () { - const from = anyone; - - it('reverts', async function () { - await assertRevert(this.contract[`transfer${rolename}`](anyone, { from })); - }); - }); - }); - describe('renouncing roles', function () { it('renounces an assigned role', async function () { await this.contract[`renounce${rolename}`]({ from: authorized }); diff --git a/test/access/rbac/Roles.test.js b/test/access/rbac/Roles.test.js index 57e932692e7..be13be072d1 100644 --- a/test/access/rbac/Roles.test.js +++ b/test/access/rbac/Roles.test.js @@ -1,5 +1,3 @@ -const { assertRevert } = require('../../helpers/assertRevert'); - const RolesMock = artifacts.require('RolesMock'); require('chai') @@ -32,17 +30,6 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { (await this.roles.has(authorized)).should.equal(true); }); - it('adds roles to multiple accounts', async function () { - await this.roles.addMany([authorized, otherAuthorized]); - (await this.roles.has(authorized)).should.equal(true); - (await this.roles.has(otherAuthorized)).should.equal(true); - }); - - it('adds roles to multiple identical accounts', async function () { - await this.roles.addMany([authorized, authorized]); - (await this.roles.has(authorized)).should.equal(true); - }); - it('doesn\'t revert when adding roles to the null account', async function () { await this.roles.add(ZERO_ADDRESS); }); @@ -51,7 +38,8 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { context('with added roles', function () { beforeEach(async function () { - await this.roles.addMany([authorized, otherAuthorized]); + await this.roles.add(authorized); + await this.roles.add(otherAuthorized); }); describe('removing roles', function () { @@ -69,44 +57,5 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) { await this.roles.remove(ZERO_ADDRESS); }); }); - - describe('transfering roles', function () { - context('from account with role', function () { - const from = authorized; - - it('transfers to other account with no role', async function () { - await this.roles.transfer(anyone, { from }); - (await this.roles.has(anyone)).should.equal(true); - (await this.roles.has(authorized)).should.equal(false); - }); - - it('reverts when transfering to an account with role', async function () { - await assertRevert(this.roles.transfer(otherAuthorized, { from })); - }); - - it('reverts when transfering to the null account', async function () { - await assertRevert(this.roles.transfer(ZERO_ADDRESS, { from })); - }); - }); - - context('from account without role', function () { - const from = anyone; - - it('reverts', async function () { - await assertRevert(this.roles.transfer(anyone, { from })); - }); - }); - }); - - describe('renouncing roles', function () { - it('renounces an assigned role', async function () { - await this.roles.renounce({ from: authorized }); - (await this.roles.has(authorized)).should.equal(false); - }); - - it('doesn\'t revert when renouncing unassigned role', async function () { - await this.roles.renounce({ from: anyone }); - }); - }); }); }); diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index 408300e3426..e254ddf1b93 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -8,15 +8,17 @@ const MintedCrowdsale = artifacts.require('MintedCrowdsaleImpl'); const ERC20Mintable = artifacts.require('ERC20Mintable'); const ERC20 = artifacts.require('ERC20'); -contract('MintedCrowdsale', function ([_, initialMinter, investor, wallet, purchaser]) { +contract('MintedCrowdsale', function ([_, deployer, investor, wallet, purchaser]) { const rate = new BigNumber(1000); const value = ether(5); describe('using ERC20Mintable', function () { beforeEach(async function () { - this.token = await ERC20Mintable.new([initialMinter]); + this.token = await ERC20Mintable.new({ from: deployer }); this.crowdsale = await MintedCrowdsale.new(rate, wallet, this.token.address); - await this.token.transferMinter(this.crowdsale.address, { from: initialMinter }); + + await this.token.addMinter(this.crowdsale.address, { from: deployer }); + await this.token.renounceMinter({ from: deployer }); }); it('crowdsale should be minter', async function () { diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index 6e9706d49d4..9dd48ba9553 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -16,7 +16,7 @@ const should = require('chai') const SampleCrowdsale = artifacts.require('SampleCrowdsale'); const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken'); -contract('SampleCrowdsale', function ([_, initialMinter, owner, wallet, investor]) { +contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) { const RATE = new BigNumber(10); const GOAL = ether(10); const CAP = ether(20); @@ -31,12 +31,14 @@ contract('SampleCrowdsale', function ([_, initialMinter, owner, wallet, investor this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); - this.token = await SampleCrowdsaleToken.new([initialMinter]); + this.token = await SampleCrowdsaleToken.new({ from: deployer }); this.crowdsale = await SampleCrowdsale.new( this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL, { from: owner } ); - await this.token.transferMinter(this.crowdsale.address, { from: initialMinter }); + + await this.token.addMinter(this.crowdsale.address, { from: deployer }); + await this.token.renounceMinter({ from: deployer }); }); it('should create crowdsale with correct parameters', async function () { diff --git a/test/token/ERC20/ERC20Capped.test.js b/test/token/ERC20/ERC20Capped.test.js index ccbd920a2e6..7e0c35c13c6 100644 --- a/test/token/ERC20/ERC20Capped.test.js +++ b/test/token/ERC20/ERC20Capped.test.js @@ -10,13 +10,13 @@ contract('ERC20Capped', function ([_, minter, ...otherAccounts]) { it('requires a non-zero cap', async function () { await assertRevert( - ERC20Capped.new(0, [minter]) + ERC20Capped.new(0, { from: minter }) ); }); context('once deployed', async function () { beforeEach(async function () { - this.token = await ERC20Capped.new(cap, [minter]); + this.token = await ERC20Capped.new(cap, { from: minter }); }); shouldBehaveLikeERC20Capped(minter, otherAccounts, cap); diff --git a/test/token/ERC20/ERC20Mintable.test.js b/test/token/ERC20/ERC20Mintable.test.js index c838f0ccd02..4cd1c33253d 100644 --- a/test/token/ERC20/ERC20Mintable.test.js +++ b/test/token/ERC20/ERC20Mintable.test.js @@ -2,21 +2,21 @@ const { shouldBehaveLikeERC20Mintable } = require('./ERC20Mintable.behavior'); const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); const ERC20MintableMock = artifacts.require('ERC20MintableMock'); -contract('ERC20Mintable', function ([_, originalMinter, otherMinter, ...otherAccounts]) { +contract('ERC20Mintable', function ([_, minter, otherMinter, ...otherAccounts]) { beforeEach(async function () { - this.token = await ERC20MintableMock.new([originalMinter, otherMinter]); + this.token = await ERC20MintableMock.new({ from: minter }); }); context('with original minter', function () { - shouldBehaveLikeERC20Mintable(originalMinter, otherAccounts); + shouldBehaveLikeERC20Mintable(minter, otherAccounts); }); describe('minter role', function () { beforeEach(async function () { - await this.token.addMinter(otherMinter); + await this.token.addMinter(otherMinter, { from: minter }); this.contract = this.token; }); - shouldBehaveLikePublicRole(originalMinter, otherMinter, otherAccounts, 'minter'); + shouldBehaveLikePublicRole(minter, otherMinter, otherAccounts, 'minter'); }); }); diff --git a/test/token/ERC20/TokenTimelock.test.js b/test/token/ERC20/TokenTimelock.test.js index d7c80f0ab16..5b5609a5311 100644 --- a/test/token/ERC20/TokenTimelock.test.js +++ b/test/token/ERC20/TokenTimelock.test.js @@ -16,7 +16,7 @@ contract('TokenTimelock', function ([_, minter, beneficiary]) { context('with token', function () { beforeEach(async function () { - this.token = await ERC20Mintable.new([minter]); + this.token = await ERC20Mintable.new({ from: minter }); }); it('rejects a release time in the past', async function () { diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index 5138289a66b..2af7a1d3767 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -44,7 +44,7 @@ contract('TokenVesting', function ([_, owner, beneficiary, minter]) { beforeEach(async function () { this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner }); - this.token = await ERC20Mintable.new([minter]); + this.token = await ERC20Mintable.new({ from: minter }); await this.token.mint(this.vesting.address, amount, { from: minter }); }); From 4d4a0044b7d5dc09f8c07a97e3cd88a482aefeb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 6 Sep 2018 11:36:38 -0300 Subject: [PATCH 07/14] Added PauserRole. (#1283) --- contracts/access/rbac/PauserRole.sol | 35 ++++++ contracts/bounties/BreakInvariantBounty.sol | 1 + contracts/lifecycle/Pausable.sol | 9 +- test/lifecycle/Pausable.test.js | 115 +++++++++++++------- test/token/ERC20/ERC20Pausable.test.js | 104 +++++++++--------- test/token/ERC721/ERC721Pausable.test.js | 16 +-- 6 files changed, 176 insertions(+), 104 deletions(-) create mode 100644 contracts/access/rbac/PauserRole.sol diff --git a/contracts/access/rbac/PauserRole.sol b/contracts/access/rbac/PauserRole.sol new file mode 100644 index 00000000000..ec0802d9c68 --- /dev/null +++ b/contracts/access/rbac/PauserRole.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.4.24; + +import "./Roles.sol"; + + +contract PauserRole { + using Roles for Roles.Role; + + Roles.Role private pausers; + + constructor() public { + pausers.add(msg.sender); + } + + modifier onlyPauser() { + require(isPauser(msg.sender)); + _; + } + + function isPauser(address _account) public view returns (bool) { + return pausers.has(_account); + } + + function addPauser(address _account) public onlyPauser { + pausers.add(_account); + } + + function renouncePauser() public { + pausers.remove(msg.sender); + } + + function _removePauser(address _account) internal { + pausers.remove(_account); + } +} diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 622e61ae5af..ed8884fc222 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -4,6 +4,7 @@ pragma solidity ^0.4.24; import "../payment/PullPayment.sol"; import "../ownership/Ownable.sol"; + /** * @title BreakInvariantBounty * @dev This bounty will pay out to a researcher if they break invariant logic of the contract. diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 0a2767aa61f..c3943f6e4bd 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -1,14 +1,13 @@ pragma solidity ^0.4.24; - -import "../ownership/Ownable.sol"; +import "../access/rbac/PauserRole.sol"; /** * @title Pausable * @dev Base contract which allows children to implement an emergency stop mechanism. */ -contract Pausable is Ownable { +contract Pausable is PauserRole { event Paused(); event Unpaused(); @@ -34,7 +33,7 @@ contract Pausable is Ownable { /** * @dev called by the owner to pause, triggers stopped state */ - function pause() public onlyOwner whenNotPaused { + function pause() public onlyPauser whenNotPaused { paused = true; emit Paused(); } @@ -42,7 +41,7 @@ contract Pausable is Ownable { /** * @dev called by the owner to unpause, returns to normal state */ - function unpause() public onlyOwner whenPaused { + function unpause() public onlyPauser whenPaused { paused = false; emit Unpaused(); } diff --git a/test/lifecycle/Pausable.test.js b/test/lifecycle/Pausable.test.js index da37b6e6a01..48a80064096 100644 --- a/test/lifecycle/Pausable.test.js +++ b/test/lifecycle/Pausable.test.js @@ -8,58 +8,95 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('Pausable', function () { +contract('Pausable', function ([_, pauser, anyone]) { beforeEach(async function () { - this.Pausable = await PausableMock.new(); + this.Pausable = await PausableMock.new({ from: pauser }); }); - it('can perform normal process in non-pause', async function () { - (await this.Pausable.count()).should.be.bignumber.equal(0); + context('when unapused', function () { + beforeEach(async function () { + (await this.Pausable.paused()).should.equal(false); + }); - await this.Pausable.normalProcess(); - (await this.Pausable.count()).should.be.bignumber.equal(1); - }); + it('can perform normal process in non-pause', async function () { + (await this.Pausable.count()).should.be.bignumber.equal(0); - it('can not perform normal process in pause', async function () { - await this.Pausable.pause(); - (await this.Pausable.count()).should.be.bignumber.equal(0); + await this.Pausable.normalProcess({ from: anyone }); + (await this.Pausable.count()).should.be.bignumber.equal(1); + }); - await assertRevert(this.Pausable.normalProcess()); - (await this.Pausable.count()).should.be.bignumber.equal(0); - }); + it('cannot take drastic measure in non-pause', async function () { + await assertRevert(this.Pausable.drasticMeasure({ from: anyone })); + (await this.Pausable.drasticMeasureTaken()).should.equal(false); + }); - it('can not take drastic measure in non-pause', async function () { - await assertRevert(this.Pausable.drasticMeasure()); - (await this.Pausable.drasticMeasureTaken()).should.equal(false); - }); + describe('pausing', function () { + it('is pausable by the pauser', async function () { + await this.Pausable.pause({ from: pauser }); + (await this.Pausable.paused()).should.equal(true); + }); - it('can take a drastic measure in a pause', async function () { - await this.Pausable.pause(); - await this.Pausable.drasticMeasure(); - (await this.Pausable.drasticMeasureTaken()).should.equal(true); - }); + it('reverts when pausing from non-pauser', async function () { + await assertRevert(this.Pausable.pause({ from: anyone })); + }); - it('should resume allowing normal process after pause is over', async function () { - await this.Pausable.pause(); - await this.Pausable.unpause(); - await this.Pausable.normalProcess(); - (await this.Pausable.count()).should.be.bignumber.equal(1); - }); + context('when paused', function () { + beforeEach(async function () { + ({ logs: this.logs } = await this.Pausable.pause({ from: pauser })); + }); - it('should prevent drastic measure after pause is over', async function () { - await this.Pausable.pause(); - await this.Pausable.unpause(); + it('emits a Paused event', function () { + expectEvent.inLogs(this.logs, 'Paused'); + }); - await assertRevert(this.Pausable.drasticMeasure()); + it('cannot perform normal process in pause', async function () { + await assertRevert(this.Pausable.normalProcess({ from: anyone })); + }); - (await this.Pausable.drasticMeasureTaken()).should.equal(false); - }); + it('can take a drastic measure in a pause', async function () { + await this.Pausable.drasticMeasure({ from: anyone }); + (await this.Pausable.drasticMeasureTaken()).should.equal(true); + }); + + it('reverts when re-pausing', async function () { + await assertRevert(this.Pausable.pause({ from: pauser })); + }); + + describe('unpausing', function () { + it('is unpausable by the pauser', async function () { + await this.Pausable.unpause({ from: pauser }); + (await this.Pausable.paused()).should.equal(false); + }); + + it('reverts when unpausing from non-pauser', async function () { + await assertRevert(this.Pausable.unpause({ from: anyone })); + }); + + context('when paused', function () { + beforeEach(async function () { + ({ logs: this.logs } = await this.Pausable.unpause({ from: pauser })); + }); + + it('emits an Unpaused event', function () { + expectEvent.inLogs(this.logs, 'Unpaused'); + }); + + it('should resume allowing normal process', async function () { + (await this.Pausable.count()).should.be.bignumber.equal(0); + await this.Pausable.normalProcess({ from: anyone }); + (await this.Pausable.count()).should.be.bignumber.equal(1); + }); - it('should log Pause and Unpause events appropriately', async function () { - const setPauseLogs = (await this.Pausable.pause()).logs; - expectEvent.inLogs(setPauseLogs, 'Paused'); + it('should prevent drastic measure', async function () { + await assertRevert(this.Pausable.drasticMeasure({ from: anyone })); + }); - const setUnPauseLogs = (await this.Pausable.unpause()).logs; - expectEvent.inLogs(setUnPauseLogs, 'Unpaused'); + it('reverts when re-unpausing', async function () { + await assertRevert(this.Pausable.unpause({ from: pauser })); + }); + }); + }); + }); + }); }); }); diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index 8fbe4f70876..7bf4f0e8871 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -1,14 +1,14 @@ const { assertRevert } = require('../../helpers/assertRevert'); const ERC20Pausable = artifacts.require('ERC20PausableMock'); -contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { +contract('ERC20Pausable', function ([_, pauser, recipient, anotherAccount]) { beforeEach(async function () { - this.token = await ERC20Pausable.new(owner, 100, { from: owner }); + this.token = await ERC20Pausable.new(pauser, 100, { from: pauser }); }); describe('pause', function () { - describe('when the sender is the token owner', function () { - const from = owner; + describe('when the sender is the token pauser', function () { + const from = pauser; describe('when the token is unpaused', function () { it('pauses the token', async function () { @@ -35,7 +35,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { }); }); - describe('when the sender is not the token owner', function () { + describe('when the sender is not the token pauser', function () { const from = anotherAccount; it('reverts', async function () { @@ -45,8 +45,8 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { }); describe('unpause', function () { - describe('when the sender is the token owner', function () { - const from = owner; + describe('when the sender is the token pauser', function () { + const from = pauser; describe('when the token is paused', function () { beforeEach(async function () { @@ -73,7 +73,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { }); }); - describe('when the sender is not the token owner', function () { + describe('when the sender is not the token pauser', function () { const from = anotherAccount; it('reverts', async function () { @@ -83,7 +83,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { }); describe('pausable token', function () { - const from = owner; + const from = pauser; describe('paused', function () { it('is not paused by default', async function () { @@ -104,132 +104,132 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { describe('transfer', function () { it('allows to transfer when unpaused', async function () { - await this.token.transfer(recipient, 100, { from: owner }); + await this.token.transfer(recipient, 100, { from: pauser }); - (await this.token.balanceOf(owner)).should.be.bignumber.equal(0); + (await this.token.balanceOf(pauser)).should.be.bignumber.equal(0); (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100); }); it('allows to transfer when paused and then unpaused', async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); - await this.token.transfer(recipient, 100, { from: owner }); + await this.token.transfer(recipient, 100, { from: pauser }); - (await this.token.balanceOf(owner)).should.be.bignumber.equal(0); + (await this.token.balanceOf(pauser)).should.be.bignumber.equal(0); (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100); }); it('reverts when trying to transfer when paused', async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); - await assertRevert(this.token.transfer(recipient, 100, { from: owner })); + await assertRevert(this.token.transfer(recipient, 100, { from: pauser })); }); }); describe('approve', function () { it('allows to approve when unpaused', async function () { - await this.token.approve(anotherAccount, 40, { from: owner }); + await this.token.approve(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(40); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(40); }); it('allows to transfer when paused and then unpaused', async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); - await this.token.approve(anotherAccount, 40, { from: owner }); + await this.token.approve(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(40); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(40); }); it('reverts when trying to transfer when paused', async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); - await assertRevert(this.token.approve(anotherAccount, 40, { from: owner })); + await assertRevert(this.token.approve(anotherAccount, 40, { from: pauser })); }); }); describe('transfer from', function () { beforeEach(async function () { - await this.token.approve(anotherAccount, 50, { from: owner }); + await this.token.approve(anotherAccount, 50, { from: pauser }); }); it('allows to transfer from when unpaused', async function () { - await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount }); + await this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount }); - (await this.token.balanceOf(owner)).should.be.bignumber.equal(60); + (await this.token.balanceOf(pauser)).should.be.bignumber.equal(60); (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40); }); it('allows to transfer when paused and then unpaused', async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); - await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount }); + await this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount }); - (await this.token.balanceOf(owner)).should.be.bignumber.equal(60); + (await this.token.balanceOf(pauser)).should.be.bignumber.equal(60); (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40); }); it('reverts when trying to transfer from when paused', async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); - await assertRevert(this.token.transferFrom(owner, recipient, 40, { from: anotherAccount })); + await assertRevert(this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount })); }); }); describe('decrease approval', function () { beforeEach(async function () { - await this.token.approve(anotherAccount, 100, { from: owner }); + await this.token.approve(anotherAccount, 100, { from: pauser }); }); it('allows to decrease approval when unpaused', async function () { - await this.token.decreaseApproval(anotherAccount, 40, { from: owner }); + await this.token.decreaseApproval(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(60); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60); }); it('allows to decrease approval when paused and then unpaused', async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); - await this.token.decreaseApproval(anotherAccount, 40, { from: owner }); + await this.token.decreaseApproval(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(60); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60); }); it('reverts when trying to transfer when paused', async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); - await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: owner })); + await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: pauser })); }); }); describe('increase approval', function () { beforeEach(async function () { - await this.token.approve(anotherAccount, 100, { from: owner }); + await this.token.approve(anotherAccount, 100, { from: pauser }); }); it('allows to increase approval when unpaused', async function () { - await this.token.increaseApproval(anotherAccount, 40, { from: owner }); + await this.token.increaseApproval(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(140); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140); }); it('allows to increase approval when paused and then unpaused', async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); - await this.token.increaseApproval(anotherAccount, 40, { from: owner }); + await this.token.increaseApproval(anotherAccount, 40, { from: pauser }); - (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(140); + (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140); }); it('reverts when trying to increase approval when paused', async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); - await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: owner })); + await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: pauser })); }); }); }); diff --git a/test/token/ERC721/ERC721Pausable.test.js b/test/token/ERC721/ERC721Pausable.test.js index 7c03c888ab5..e40b02e2910 100644 --- a/test/token/ERC721/ERC721Pausable.test.js +++ b/test/token/ERC721/ERC721Pausable.test.js @@ -8,29 +8,29 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721Pausable', function ([_, owner, recipient, operator, ...otherAccounts]) { +contract('ERC721Pausable', function ([_, pauser, recipient, operator, ...otherAccounts]) { beforeEach(async function () { - this.token = await ERC721Pausable.new({ from: owner }); + this.token = await ERC721Pausable.new({ from: pauser }); }); context('when token is paused', function () { beforeEach(async function () { - await this.token.pause({ from: owner }); + await this.token.pause({ from: pauser }); }); - shouldBehaveLikeERC721PausedToken(owner, [...otherAccounts]); + shouldBehaveLikeERC721PausedToken(pauser, [...otherAccounts]); }); context('when token is not paused yet', function () { - shouldBehaveLikeERC721Basic([owner, ...otherAccounts]); + shouldBehaveLikeERC721Basic([pauser, ...otherAccounts]); }); context('when token is paused and then unpaused', function () { beforeEach(async function () { - await this.token.pause({ from: owner }); - await this.token.unpause({ from: owner }); + await this.token.pause({ from: pauser }); + await this.token.unpause({ from: pauser }); }); - shouldBehaveLikeERC721Basic([owner, ...otherAccounts]); + shouldBehaveLikeERC721Basic([pauser, ...otherAccounts]); }); }); From b2f350dca6afeb26393388bcc0cdad2a36ed407c Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Wed, 5 Sep 2018 11:17:18 -0300 Subject: [PATCH 08/14] Remove Claimable, DelayedClaimable, Heritable (#1274) * remove Claimable, DelayedClaimable, Heritable * remove SimpleSavingsWallet example which used Heritable (cherry picked from commit 0dc711732a297e70af63f23a9b52e4b3712eac40) --- contracts/examples/SimpleSavingsWallet.sol | 40 ------- contracts/ownership/Claimable.sol | 39 ------- contracts/ownership/DelayedClaimable.sol | 40 ------- contracts/ownership/Heritable.sol | 119 -------------------- test/Heritable.test.js | 120 --------------------- test/SimpleSavingsWallet.test.js | 40 ------- test/ownership/Claimable.test.js | 46 -------- test/ownership/DelayedClaimable.test.js | 55 ---------- 8 files changed, 499 deletions(-) delete mode 100644 contracts/examples/SimpleSavingsWallet.sol delete mode 100644 contracts/ownership/Claimable.sol delete mode 100644 contracts/ownership/DelayedClaimable.sol delete mode 100644 contracts/ownership/Heritable.sol delete mode 100644 test/Heritable.test.js delete mode 100644 test/SimpleSavingsWallet.test.js delete mode 100644 test/ownership/Claimable.test.js delete mode 100644 test/ownership/DelayedClaimable.test.js diff --git a/contracts/examples/SimpleSavingsWallet.sol b/contracts/examples/SimpleSavingsWallet.sol deleted file mode 100644 index e6bfabba7e3..00000000000 --- a/contracts/examples/SimpleSavingsWallet.sol +++ /dev/null @@ -1,40 +0,0 @@ -pragma solidity ^0.4.24; - -import "../ownership/Heritable.sol"; - - -/** - * @title SimpleSavingsWallet - * @dev Simplest form of savings wallet whose ownership can be claimed by a heir - * if owner dies. - * In this example, we take a very simple savings wallet providing two operations - * (to send and receive funds) and extend its capabilities by making it Heritable. - * The account that creates the contract is set as owner, who has the authority to - * choose an heir account. Heir account can reclaim the contract ownership in the - * case that the owner dies. - */ -contract SimpleSavingsWallet is Heritable { - - event Sent(address indexed payee, uint256 amount, uint256 balance); - event Received(address indexed payer, uint256 amount, uint256 balance); - - - constructor(uint256 _heartbeatTimeout) Heritable(_heartbeatTimeout) public {} - - /** - * @dev wallet can receive funds. - */ - function () external payable { - emit Received(msg.sender, msg.value, address(this).balance); - } - - /** - * @dev wallet can send funds - */ - function sendTo(address _payee, uint256 _amount) public onlyOwner { - require(_payee != address(0) && _payee != address(this)); - require(_amount > 0); - _payee.transfer(_amount); - emit Sent(_payee, _amount, address(this).balance); - } -} diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol deleted file mode 100644 index 9027c11f52d..00000000000 --- a/contracts/ownership/Claimable.sol +++ /dev/null @@ -1,39 +0,0 @@ -pragma solidity ^0.4.24; - - -import "./Ownable.sol"; - - -/** - * @title Claimable - * @dev Extension for the Ownable contract, where the ownership needs to be claimed. - * This allows the new owner to accept the transfer. - */ -contract Claimable is Ownable { - address public pendingOwner; - - /** - * @dev Modifier throws if called by any account other than the pendingOwner. - */ - modifier onlyPendingOwner() { - require(msg.sender == pendingOwner); - _; - } - - /** - * @dev Allows the current owner to set the pendingOwner address. - * @param newOwner The address to transfer ownership to. - */ - function transferOwnership(address newOwner) public onlyOwner { - pendingOwner = newOwner; - } - - /** - * @dev Allows the pendingOwner address to finalize the transfer. - */ - function claimOwnership() public onlyPendingOwner { - emit OwnershipTransferred(owner, pendingOwner); - owner = pendingOwner; - pendingOwner = address(0); - } -} diff --git a/contracts/ownership/DelayedClaimable.sol b/contracts/ownership/DelayedClaimable.sol deleted file mode 100644 index a9c0857f8e9..00000000000 --- a/contracts/ownership/DelayedClaimable.sol +++ /dev/null @@ -1,40 +0,0 @@ -pragma solidity ^0.4.24; - -import "./Claimable.sol"; - - -/** - * @title DelayedClaimable - * @dev Extension for the Claimable contract, where the ownership needs to be claimed before/after - * a certain block number. - */ -contract DelayedClaimable is Claimable { - - uint256 public end; - uint256 public start; - - /** - * @dev Used to specify the time period during which a pending - * owner can claim ownership. - * @param _start The earliest time ownership can be claimed. - * @param _end The latest time ownership can be claimed. - */ - function setLimits(uint256 _start, uint256 _end) public onlyOwner { - require(_start <= _end); - end = _end; - start = _start; - } - - /** - * @dev Allows the pendingOwner address to finalize the transfer, as long as it is called within - * the specified start and end time. - */ - function claimOwnership() public onlyPendingOwner { - require((block.number <= end) && (block.number >= start)); - emit OwnershipTransferred(owner, pendingOwner); - owner = pendingOwner; - pendingOwner = address(0); - end = 0; - } - -} diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol deleted file mode 100644 index 88e6fee62f4..00000000000 --- a/contracts/ownership/Heritable.sol +++ /dev/null @@ -1,119 +0,0 @@ -pragma solidity ^0.4.24; - - -import "./Ownable.sol"; - - -/** - * @title Heritable - * @dev The Heritable contract provides ownership transfer capabilities, in the - * case that the current owner stops "heartbeating". Only the heir can pronounce the - * owner's death. - */ -contract Heritable is Ownable { - address private heir_; - - // Time window the owner has to notify they are alive. - uint256 private heartbeatTimeout_; - - // Timestamp of the owner's death, as pronounced by the heir. - uint256 private timeOfDeath_; - - event HeirChanged(address indexed owner, address indexed newHeir); - event OwnerHeartbeated(address indexed owner); - event OwnerProclaimedDead( - address indexed owner, - address indexed heir, - uint256 timeOfDeath - ); - event HeirOwnershipClaimed( - address indexed previousOwner, - address indexed newOwner - ); - - - /** - * @dev Throw an exception if called by any account other than the heir's. - */ - modifier onlyHeir() { - require(msg.sender == heir_); - _; - } - - - /** - * @notice Create a new Heritable Contract with heir address 0x0. - * @param _heartbeatTimeout time available for the owner to notify they are alive, - * before the heir can take ownership. - */ - constructor(uint256 _heartbeatTimeout) public { - heartbeatTimeout_ = _heartbeatTimeout; - } - - function setHeir(address _newHeir) public onlyOwner { - require(_newHeir != owner); - heartbeat(); - emit HeirChanged(owner, _newHeir); - heir_ = _newHeir; - } - - /** - * @dev Use these getter functions to access the internal variables in - * an inherited contract. - */ - function heir() public view returns(address) { - return heir_; - } - - function heartbeatTimeout() public view returns(uint256) { - return heartbeatTimeout_; - } - - function timeOfDeath() public view returns(uint256) { - return timeOfDeath_; - } - - /** - * @dev set heir = 0x0 - */ - function removeHeir() public onlyOwner { - heartbeat(); - heir_ = address(0); - } - - /** - * @dev Heir can pronounce the owners death. To claim the ownership, they will - * have to wait for `heartbeatTimeout` seconds. - */ - function proclaimDeath() public onlyHeir { - require(_ownerLives()); - emit OwnerProclaimedDead(owner, heir_, timeOfDeath_); - // solium-disable-next-line security/no-block-members - timeOfDeath_ = block.timestamp; - } - - /** - * @dev Owner can send a heartbeat if they were mistakenly pronounced dead. - */ - function heartbeat() public onlyOwner { - emit OwnerHeartbeated(owner); - timeOfDeath_ = 0; - } - - /** - * @dev Allows heir to transfer ownership only if heartbeat has timed out. - */ - function claimHeirOwnership() public onlyHeir { - require(!_ownerLives()); - // solium-disable-next-line security/no-block-members - require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); - emit OwnershipTransferred(owner, heir_); - emit HeirOwnershipClaimed(owner, heir_); - owner = heir_; - timeOfDeath_ = 0; - } - - function _ownerLives() internal view returns (bool) { - return timeOfDeath_ == 0; - } -} diff --git a/test/Heritable.test.js b/test/Heritable.test.js deleted file mode 100644 index 77036417905..00000000000 --- a/test/Heritable.test.js +++ /dev/null @@ -1,120 +0,0 @@ -const { increaseTime } = require('./helpers/increaseTime'); -const { expectThrow } = require('./helpers/expectThrow'); -const { assertRevert } = require('./helpers/assertRevert'); - -const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'; - -const Heritable = artifacts.require('Heritable'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('Heritable', function ([_, owner, heir, anyone]) { - const heartbeatTimeout = 4141; - let heritable; - - beforeEach(async function () { - heritable = await Heritable.new(heartbeatTimeout, { from: owner }); - }); - - it('should start off with an owner, but without heir', async function () { - const heir = await heritable.heir(); - - owner.should.be.a('string').that.is.not.equal(NULL_ADDRESS); - heir.should.be.a('string').that.is.equal(NULL_ADDRESS); - }); - - it('only owner should set heir', async function () { - await heritable.setHeir(heir, { from: owner }); - await expectThrow(heritable.setHeir(heir, { from: anyone })); - }); - - it('owner can\'t be heir', async function () { - await assertRevert(heritable.setHeir(owner, { from: owner })); - }); - - it('owner can remove heir', async function () { - await heritable.setHeir(heir, { from: owner }); - (await heritable.heir()).should.equal(heir); - - await heritable.removeHeir({ from: owner }); - (await heritable.heir()).should.equal(NULL_ADDRESS); - }); - - it('heir can claim ownership only if owner is dead and timeout was reached', async function () { - await heritable.setHeir(heir, { from: owner }); - await expectThrow(heritable.claimHeirOwnership({ from: heir })); - - await heritable.proclaimDeath({ from: heir }); - await increaseTime(1); - await expectThrow(heritable.claimHeirOwnership({ from: heir })); - - await increaseTime(heartbeatTimeout); - await heritable.claimHeirOwnership({ from: heir }); - (await heritable.heir()).should.equal(heir); - }); - - it('only heir can proclaim death', async function () { - await assertRevert(heritable.proclaimDeath({ from: owner })); - await assertRevert(heritable.proclaimDeath({ from: anyone })); - }); - - it('heir can\'t proclaim death if owner is death', async function () { - await heritable.setHeir(heir, { from: owner }); - await heritable.proclaimDeath({ from: heir }); - await assertRevert(heritable.proclaimDeath({ from: heir })); - }); - - it('heir can\'t claim ownership if owner heartbeats', async function () { - await heritable.setHeir(heir, { from: owner }); - - await heritable.proclaimDeath({ from: heir }); - await heritable.heartbeat({ from: owner }); - await expectThrow(heritable.claimHeirOwnership({ from: heir })); - - await heritable.proclaimDeath({ from: heir }); - await increaseTime(heartbeatTimeout); - await heritable.heartbeat({ from: owner }); - await expectThrow(heritable.claimHeirOwnership({ from: heir })); - }); - - it('should log events appropriately', async function () { - const setHeirLogs = (await heritable.setHeir(heir, { from: owner })).logs; - const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged'); - - setHeirEvent.args.owner.should.equal(owner); - setHeirEvent.args.newHeir.should.equal(heir); - - const heartbeatLogs = (await heritable.heartbeat({ from: owner })).logs; - const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated'); - - heartbeatEvent.args.owner.should.equal(owner); - - const proclaimDeathLogs = (await heritable.proclaimDeath({ from: heir })).logs; - const ownerDeadEvent = proclaimDeathLogs.find(e => e.event === 'OwnerProclaimedDead'); - - ownerDeadEvent.args.owner.should.equal(owner); - ownerDeadEvent.args.heir.should.equal(heir); - - await increaseTime(heartbeatTimeout); - const claimHeirOwnershipLogs = (await heritable.claimHeirOwnership({ from: heir })).logs; - const ownershipTransferredEvent = claimHeirOwnershipLogs.find(e => e.event === 'OwnershipTransferred'); - const heirOwnershipClaimedEvent = claimHeirOwnershipLogs.find(e => e.event === 'HeirOwnershipClaimed'); - - ownershipTransferredEvent.args.previousOwner.should.equal(owner); - ownershipTransferredEvent.args.newOwner.should.equal(heir); - heirOwnershipClaimedEvent.args.previousOwner.should.equal(owner); - heirOwnershipClaimedEvent.args.newOwner.should.equal(heir); - }); - - it('timeOfDeath can be queried', async function () { - (await heritable.timeOfDeath()).should.be.bignumber.equal(0); - }); - - it('heartbeatTimeout can be queried', async function () { - (await heritable.heartbeatTimeout()).should.be.bignumber.equal(heartbeatTimeout); - }); -}); diff --git a/test/SimpleSavingsWallet.test.js b/test/SimpleSavingsWallet.test.js deleted file mode 100644 index 3582816451a..00000000000 --- a/test/SimpleSavingsWallet.test.js +++ /dev/null @@ -1,40 +0,0 @@ -const { expectThrow } = require('./helpers/expectThrow'); -const { ethGetBalance, ethSendTransaction } = require('./helpers/web3'); - -const SimpleSavingsWallet = artifacts.require('SimpleSavingsWallet'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('SimpleSavingsWallet', function ([_, owner, anyone]) { - let savingsWallet; - - const paymentAmount = 4242; - - beforeEach(async function () { - savingsWallet = await SimpleSavingsWallet.new(4141, { from: owner }); - }); - - it('should receive funds', async function () { - await ethSendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount }); - const balance = await ethGetBalance(savingsWallet.address); - balance.should.be.bignumber.equal(paymentAmount); - }); - - it('owner can send funds', async function () { - // Receive payment so we have some money to spend. - await ethSendTransaction({ from: anyone, to: savingsWallet.address, value: 1000000 }); - - await expectThrow(savingsWallet.sendTo(0, paymentAmount, { from: owner })); - await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner })); - await expectThrow(savingsWallet.sendTo(anyone, 0, { from: owner })); - - const balance = await ethGetBalance(anyone); - await savingsWallet.sendTo(anyone, paymentAmount, { from: owner }); - const updatedBalance = await ethGetBalance(anyone); - balance.plus(paymentAmount).should.be.bignumber.equal(updatedBalance); - }); -}); diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js deleted file mode 100644 index bfc1355d5f7..00000000000 --- a/test/ownership/Claimable.test.js +++ /dev/null @@ -1,46 +0,0 @@ -const { assertRevert } = require('../helpers/assertRevert'); - -const Claimable = artifacts.require('Claimable'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -contract('Claimable', function ([_, owner, newOwner, anyone]) { - let claimable; - - beforeEach(async function () { - claimable = await Claimable.new({ from: owner }); - }); - - it('should have an owner', async function () { - (await claimable.owner()).should.not.equal(0); - }); - - it('changes pendingOwner after transfer', async function () { - await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.pendingOwner()).should.equal(newOwner); - }); - - it('should prevent to claimOwnership from anyone', async function () { - await assertRevert(claimable.claimOwnership({ from: anyone })); - }); - - it('should prevent non-owners from transfering', async function () { - await assertRevert(claimable.transferOwnership(anyone, { from: anyone })); - }); - - describe('after initiating a transfer', function () { - beforeEach(async function () { - await claimable.transferOwnership(newOwner, { from: owner }); - }); - - it('changes allow pending owner to claim ownership', async function () { - await claimable.claimOwnership({ from: newOwner }); - - (await claimable.owner()).should.equal(newOwner); - }); - }); -}); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js deleted file mode 100644 index e65ac86c41d..00000000000 --- a/test/ownership/DelayedClaimable.test.js +++ /dev/null @@ -1,55 +0,0 @@ -const { assertRevert } = require('../helpers/assertRevert'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -const DelayedClaimable = artifacts.require('DelayedClaimable'); - -contract('DelayedClaimable', function ([_, owner, newOwner]) { - beforeEach(async function () { - this.delayedClaimable = await DelayedClaimable.new({ from: owner }); - }); - - it('can set claim blocks', async function () { - await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); - await this.delayedClaimable.setLimits(0, 1000, { from: owner }); - - (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); - }); - - it('changes pendingOwner after transfer successful', async function () { - await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); - await this.delayedClaimable.setLimits(0, 1000, { from: owner }); - - (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); - - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); - await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.owner()).should.equal(newOwner); - }); - - it('changes pendingOwner after transfer fails', async function () { - await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); - await this.delayedClaimable.setLimits(100, 110, { from: owner }); - - (await this.delayedClaimable.end()).should.be.bignumber.equal(110); - - (await this.delayedClaimable.start()).should.be.bignumber.equal(100); - - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); - await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.owner()).should.not.equal(newOwner); - }); - - it('set end and start invalid values fail', async function () { - await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); - await assertRevert(this.delayedClaimable.setLimits(1001, 1000, { from: owner })); - }); -}); From bf4a211e2473f12450cbcaf1bcc6d28ec51721b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 6 Sep 2018 15:52:47 -0300 Subject: [PATCH 09/14] Role behavior tests (#1285) * Added role tests. * Added PauserRole tests to contracts that have that role. * Added MinterRole tests to contracts that have that role. * Fixed linter errors. --- contracts/mocks/ERC20MintableMock.sol | 10 +--- contracts/mocks/ERC20PausableMock.sol | 3 +- contracts/mocks/ERC721PausableMock.sol | 3 +- contracts/mocks/MinterRoleMock.sol | 18 +++++++ contracts/mocks/PausableMock.sol | 3 +- contracts/mocks/PauserRoleMock.sol | 18 +++++++ test/access/rbac/MinterRole.test.js | 11 +++++ test/access/rbac/PauserRole.test.js | 11 +++++ test/lifecycle/Pausable.test.js | 63 ++++++++++++++---------- test/token/ERC20/ERC20Mintable.test.js | 10 ++-- test/token/ERC20/ERC20Pausable.test.js | 13 ++++- test/token/ERC721/ERC721Pausable.test.js | 12 ++++- 12 files changed, 130 insertions(+), 45 deletions(-) create mode 100644 contracts/mocks/MinterRoleMock.sol create mode 100644 contracts/mocks/PauserRoleMock.sol create mode 100644 test/access/rbac/MinterRole.test.js create mode 100644 test/access/rbac/PauserRole.test.js diff --git a/contracts/mocks/ERC20MintableMock.sol b/contracts/mocks/ERC20MintableMock.sol index 39345081f4d..a2056548f74 100644 --- a/contracts/mocks/ERC20MintableMock.sol +++ b/contracts/mocks/ERC20MintableMock.sol @@ -1,14 +1,8 @@ pragma solidity ^0.4.24; import "../token/ERC20/ERC20Mintable.sol"; +import "./MinterRoleMock.sol"; -// Mock contract exposing internal methods -contract ERC20MintableMock is ERC20Mintable { - function removeMinter(address _account) public { - _removeMinter(_account); - } - - function onlyMinterMock() public view onlyMinter { - } +contract ERC20MintableMock is ERC20Mintable, MinterRoleMock { } diff --git a/contracts/mocks/ERC20PausableMock.sol b/contracts/mocks/ERC20PausableMock.sol index a60f8a12da6..8dad33cd40f 100644 --- a/contracts/mocks/ERC20PausableMock.sol +++ b/contracts/mocks/ERC20PausableMock.sol @@ -1,10 +1,11 @@ pragma solidity ^0.4.24; import "../token/ERC20/ERC20Pausable.sol"; +import "./PauserRoleMock.sol"; // mock class using ERC20Pausable -contract ERC20PausableMock is ERC20Pausable { +contract ERC20PausableMock is ERC20Pausable, PauserRoleMock { constructor(address _initialAccount, uint _initialBalance) public { _mint(_initialAccount, _initialBalance); diff --git a/contracts/mocks/ERC721PausableMock.sol b/contracts/mocks/ERC721PausableMock.sol index ef2722f4ace..8382518468c 100644 --- a/contracts/mocks/ERC721PausableMock.sol +++ b/contracts/mocks/ERC721PausableMock.sol @@ -1,13 +1,14 @@ pragma solidity ^0.4.24; import "../token/ERC721/ERC721Pausable.sol"; +import "./PauserRoleMock.sol"; /** * @title ERC721PausableMock * This mock just provides a public mint, burn and exists functions for testing purposes */ -contract ERC721PausableMock is ERC721Pausable { +contract ERC721PausableMock is ERC721Pausable, PauserRoleMock { function mint(address _to, uint256 _tokenId) public { super._mint(_to, _tokenId); } diff --git a/contracts/mocks/MinterRoleMock.sol b/contracts/mocks/MinterRoleMock.sol new file mode 100644 index 00000000000..20c21e11356 --- /dev/null +++ b/contracts/mocks/MinterRoleMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.24; + +import "../access/rbac/MinterRole.sol"; + + +contract MinterRoleMock is MinterRole { + function removeMinter(address _account) public { + _removeMinter(_account); + } + + function onlyMinterMock() public view onlyMinter { + } + + // Causes a compilation error if super._removeMinter is not internal + function _removeMinter(address _account) internal { + super._removeMinter(_account); + } +} diff --git a/contracts/mocks/PausableMock.sol b/contracts/mocks/PausableMock.sol index 11802687aa5..a1c24c0877e 100644 --- a/contracts/mocks/PausableMock.sol +++ b/contracts/mocks/PausableMock.sol @@ -2,10 +2,11 @@ pragma solidity ^0.4.24; import "../lifecycle/Pausable.sol"; +import "./PauserRoleMock.sol"; // mock class using Pausable -contract PausableMock is Pausable { +contract PausableMock is Pausable, PauserRoleMock { bool public drasticMeasureTaken; uint256 public count; diff --git a/contracts/mocks/PauserRoleMock.sol b/contracts/mocks/PauserRoleMock.sol new file mode 100644 index 00000000000..ea51335d241 --- /dev/null +++ b/contracts/mocks/PauserRoleMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.24; + +import "../access/rbac/PauserRole.sol"; + + +contract PauserRoleMock is PauserRole { + function removePauser(address _account) public { + _removePauser(_account); + } + + function onlyPauserMock() public view onlyPauser { + } + + // Causes a compilation error if super._removePauser is not internal + function _removePauser(address _account) internal { + super._removePauser(_account); + } +} diff --git a/test/access/rbac/MinterRole.test.js b/test/access/rbac/MinterRole.test.js new file mode 100644 index 00000000000..ed222d20f60 --- /dev/null +++ b/test/access/rbac/MinterRole.test.js @@ -0,0 +1,11 @@ +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const MinterRoleMock = artifacts.require('MinterRoleMock'); + +contract('MinterRole', function ([_, minter, otherMinter, ...otherAccounts]) { + beforeEach(async function () { + this.contract = await MinterRoleMock.new({ from: minter }); + await this.contract.addMinter(otherMinter, { from: minter }); + }); + + shouldBehaveLikePublicRole(minter, otherMinter, otherAccounts, 'minter'); +}); diff --git a/test/access/rbac/PauserRole.test.js b/test/access/rbac/PauserRole.test.js new file mode 100644 index 00000000000..a0767af22de --- /dev/null +++ b/test/access/rbac/PauserRole.test.js @@ -0,0 +1,11 @@ +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const PauserRoleMock = artifacts.require('PauserRoleMock'); + +contract('PauserRole', function ([_, pauser, otherPauser, ...otherAccounts]) { + beforeEach(async function () { + this.contract = await PauserRoleMock.new({ from: pauser }); + await this.contract.addPauser(otherPauser, { from: pauser }); + }); + + shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser'); +}); diff --git a/test/lifecycle/Pausable.test.js b/test/lifecycle/Pausable.test.js index 48a80064096..2bbe392963e 100644 --- a/test/lifecycle/Pausable.test.js +++ b/test/lifecycle/Pausable.test.js @@ -1,6 +1,8 @@ const { assertRevert } = require('../helpers/assertRevert'); const expectEvent = require('../helpers/expectEvent'); + const PausableMock = artifacts.require('PausableMock'); +const { shouldBehaveLikePublicRole } = require('../access/rbac/PublicRole.behavior'); const BigNumber = web3.BigNumber; @@ -8,41 +10,50 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('Pausable', function ([_, pauser, anyone]) { +contract('Pausable', function ([_, pauser, otherPauser, anyone, ...otherAccounts]) { beforeEach(async function () { - this.Pausable = await PausableMock.new({ from: pauser }); + this.pausable = await PausableMock.new({ from: pauser }); + }); + + describe('pauser role', function () { + beforeEach(async function () { + this.contract = this.pausable; + await this.contract.addPauser(otherPauser, { from: pauser }); + }); + + shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser'); }); context('when unapused', function () { beforeEach(async function () { - (await this.Pausable.paused()).should.equal(false); + (await this.pausable.paused()).should.equal(false); }); it('can perform normal process in non-pause', async function () { - (await this.Pausable.count()).should.be.bignumber.equal(0); + (await this.pausable.count()).should.be.bignumber.equal(0); - await this.Pausable.normalProcess({ from: anyone }); - (await this.Pausable.count()).should.be.bignumber.equal(1); + await this.pausable.normalProcess({ from: anyone }); + (await this.pausable.count()).should.be.bignumber.equal(1); }); it('cannot take drastic measure in non-pause', async function () { - await assertRevert(this.Pausable.drasticMeasure({ from: anyone })); - (await this.Pausable.drasticMeasureTaken()).should.equal(false); + await assertRevert(this.pausable.drasticMeasure({ from: anyone })); + (await this.pausable.drasticMeasureTaken()).should.equal(false); }); describe('pausing', function () { it('is pausable by the pauser', async function () { - await this.Pausable.pause({ from: pauser }); - (await this.Pausable.paused()).should.equal(true); + await this.pausable.pause({ from: pauser }); + (await this.pausable.paused()).should.equal(true); }); it('reverts when pausing from non-pauser', async function () { - await assertRevert(this.Pausable.pause({ from: anyone })); + await assertRevert(this.pausable.pause({ from: anyone })); }); context('when paused', function () { beforeEach(async function () { - ({ logs: this.logs } = await this.Pausable.pause({ from: pauser })); + ({ logs: this.logs } = await this.pausable.pause({ from: pauser })); }); it('emits a Paused event', function () { @@ -50,31 +61,31 @@ contract('Pausable', function ([_, pauser, anyone]) { }); it('cannot perform normal process in pause', async function () { - await assertRevert(this.Pausable.normalProcess({ from: anyone })); + await assertRevert(this.pausable.normalProcess({ from: anyone })); }); it('can take a drastic measure in a pause', async function () { - await this.Pausable.drasticMeasure({ from: anyone }); - (await this.Pausable.drasticMeasureTaken()).should.equal(true); + await this.pausable.drasticMeasure({ from: anyone }); + (await this.pausable.drasticMeasureTaken()).should.equal(true); }); it('reverts when re-pausing', async function () { - await assertRevert(this.Pausable.pause({ from: pauser })); + await assertRevert(this.pausable.pause({ from: pauser })); }); describe('unpausing', function () { it('is unpausable by the pauser', async function () { - await this.Pausable.unpause({ from: pauser }); - (await this.Pausable.paused()).should.equal(false); + await this.pausable.unpause({ from: pauser }); + (await this.pausable.paused()).should.equal(false); }); it('reverts when unpausing from non-pauser', async function () { - await assertRevert(this.Pausable.unpause({ from: anyone })); + await assertRevert(this.pausable.unpause({ from: anyone })); }); - context('when paused', function () { + context('when unpaused', function () { beforeEach(async function () { - ({ logs: this.logs } = await this.Pausable.unpause({ from: pauser })); + ({ logs: this.logs } = await this.pausable.unpause({ from: pauser })); }); it('emits an Unpaused event', function () { @@ -82,17 +93,17 @@ contract('Pausable', function ([_, pauser, anyone]) { }); it('should resume allowing normal process', async function () { - (await this.Pausable.count()).should.be.bignumber.equal(0); - await this.Pausable.normalProcess({ from: anyone }); - (await this.Pausable.count()).should.be.bignumber.equal(1); + (await this.pausable.count()).should.be.bignumber.equal(0); + await this.pausable.normalProcess({ from: anyone }); + (await this.pausable.count()).should.be.bignumber.equal(1); }); it('should prevent drastic measure', async function () { - await assertRevert(this.Pausable.drasticMeasure({ from: anyone })); + await assertRevert(this.pausable.drasticMeasure({ from: anyone })); }); it('reverts when re-unpausing', async function () { - await assertRevert(this.Pausable.unpause({ from: pauser })); + await assertRevert(this.pausable.unpause({ from: pauser })); }); }); }); diff --git a/test/token/ERC20/ERC20Mintable.test.js b/test/token/ERC20/ERC20Mintable.test.js index 4cd1c33253d..460164d3800 100644 --- a/test/token/ERC20/ERC20Mintable.test.js +++ b/test/token/ERC20/ERC20Mintable.test.js @@ -1,22 +1,20 @@ const { shouldBehaveLikeERC20Mintable } = require('./ERC20Mintable.behavior'); -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); const ERC20MintableMock = artifacts.require('ERC20MintableMock'); +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); contract('ERC20Mintable', function ([_, minter, otherMinter, ...otherAccounts]) { beforeEach(async function () { this.token = await ERC20MintableMock.new({ from: minter }); }); - context('with original minter', function () { - shouldBehaveLikeERC20Mintable(minter, otherAccounts); - }); - describe('minter role', function () { beforeEach(async function () { - await this.token.addMinter(otherMinter, { from: minter }); this.contract = this.token; + await this.contract.addMinter(otherMinter, { from: minter }); }); shouldBehaveLikePublicRole(minter, otherMinter, otherAccounts, 'minter'); }); + + shouldBehaveLikeERC20Mintable(minter, otherAccounts); }); diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index 7bf4f0e8871..41013c6bded 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -1,11 +1,22 @@ const { assertRevert } = require('../../helpers/assertRevert'); + const ERC20Pausable = artifacts.require('ERC20PausableMock'); +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); -contract('ERC20Pausable', function ([_, pauser, recipient, anotherAccount]) { +contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherAccount, ...otherAccounts]) { beforeEach(async function () { this.token = await ERC20Pausable.new(pauser, 100, { from: pauser }); }); + describe('pauser role', function () { + beforeEach(async function () { + this.contract = this.token; + await this.contract.addPauser(otherPauser, { from: pauser }); + }); + + shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser'); + }); + describe('pause', function () { describe('when the sender is the token pauser', function () { const from = pauser; diff --git a/test/token/ERC721/ERC721Pausable.test.js b/test/token/ERC721/ERC721Pausable.test.js index e40b02e2910..8940400c29e 100644 --- a/test/token/ERC721/ERC721Pausable.test.js +++ b/test/token/ERC721/ERC721Pausable.test.js @@ -1,5 +1,6 @@ const { shouldBehaveLikeERC721PausedToken } = require('./ERC721PausedToken.behavior'); const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); const BigNumber = web3.BigNumber; const ERC721Pausable = artifacts.require('ERC721PausableMock.sol'); @@ -8,11 +9,20 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721Pausable', function ([_, pauser, recipient, operator, ...otherAccounts]) { +contract('ERC721Pausable', function ([_, pauser, otherPauser, recipient, operator, ...otherAccounts]) { beforeEach(async function () { this.token = await ERC721Pausable.new({ from: pauser }); }); + describe('pauser role', function () { + beforeEach(async function () { + this.contract = this.token; + await this.contract.addPauser(otherPauser, { from: pauser }); + }); + + shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser'); + }); + context('when token is paused', function () { beforeEach(async function () { await this.token.pause({ from: pauser }); From 199e156eb274c8cbb99838ae83c6f9d7b23c66bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 6 Sep 2018 19:02:58 -0300 Subject: [PATCH 10/14] Migrate Ownable to Roles (#1287) * Added CapperRole. * RefundEscrow is now Secondary. * FinalizableCrowdsale is no longer Ownable. * Removed Whitelist and WhitelistedCrowdsale, redesign needed. * Fixed linter errors, disabled lbrace due to it being buggy. --- .soliumrc.json | 1 + contracts/access/Whitelist.sol | 94 ----------- contracts/access/rbac/CapperRole.sol | 35 ++++ .../distribution/FinalizableCrowdsale.sol | 11 +- .../distribution/RefundableCrowdsale.sol | 2 +- .../IndividuallyCappedCrowdsale.sol | 8 +- .../validation/WhitelistedCrowdsale.sol | 27 ---- contracts/mocks/CapperRoleMock.sol | 18 +++ .../mocks/IndividuallyCappedCrowdsaleImpl.sol | 7 +- contracts/mocks/WhitelistMock.sol | 14 -- contracts/mocks/WhitelistedCrowdsaleImpl.sol | 19 --- contracts/payment/RefundEscrow.sol | 14 +- test/access/Whitelist.test.js | 65 -------- test/access/rbac/CapperRole.test.js | 11 ++ test/crowdsale/FinalizableCrowdsale.test.js | 21 +-- .../IndividuallyCappedCrowdsale.test.js | 149 +++++++++++------- test/crowdsale/RefundableCrowdsale.test.js | 10 +- test/crowdsale/WhitelistedCrowdsale.test.js | 89 ----------- test/payment/RefundEscrow.test.js | 42 ++--- 19 files changed, 212 insertions(+), 425 deletions(-) delete mode 100644 contracts/access/Whitelist.sol create mode 100644 contracts/access/rbac/CapperRole.sol delete mode 100644 contracts/crowdsale/validation/WhitelistedCrowdsale.sol create mode 100644 contracts/mocks/CapperRoleMock.sol delete mode 100644 contracts/mocks/WhitelistMock.sol delete mode 100644 contracts/mocks/WhitelistedCrowdsaleImpl.sol delete mode 100644 test/access/Whitelist.test.js create mode 100644 test/access/rbac/CapperRole.test.js delete mode 100644 test/crowdsale/WhitelistedCrowdsale.test.js diff --git a/.soliumrc.json b/.soliumrc.json index 6a08c646678..be4afce1095 100644 --- a/.soliumrc.json +++ b/.soliumrc.json @@ -4,6 +4,7 @@ "rules": { "error-reason": "off", "indentation": ["error", 2], + "lbrace": "off", "linebreak-style": ["error", "unix"], "max-len": ["error", 79], "no-constant": ["error"], diff --git a/contracts/access/Whitelist.sol b/contracts/access/Whitelist.sol deleted file mode 100644 index 3d9b3da5c6f..00000000000 --- a/contracts/access/Whitelist.sol +++ /dev/null @@ -1,94 +0,0 @@ -pragma solidity ^0.4.24; - - -import "../ownership/Ownable.sol"; -import "../access/rbac/RBAC.sol"; - - -/** - * @title Whitelist - * @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions. - * This simplifies the implementation of "user permissions". - */ -contract Whitelist is Ownable, RBAC { - - // Name of the whitelisted role. - string private constant ROLE_WHITELISTED = "whitelist"; - - /** - * @dev Throws if operator is not whitelisted. - * @param _operator address - */ - modifier onlyIfWhitelisted(address _operator) { - checkRole(_operator, ROLE_WHITELISTED); - _; - } - - /** - * @dev add an address to the whitelist - * @param _operator address - * @return true if the address was added to the whitelist, false if the address was already in the whitelist - */ - function addAddressToWhitelist(address _operator) - public - onlyOwner - { - _addRole(_operator, ROLE_WHITELISTED); - } - - /** - * @dev Determine if an account is whitelisted. - * @return true if the account is whitelisted, false otherwise. - */ - function isWhitelisted(address _operator) - public - view - returns (bool) - { - return hasRole(_operator, ROLE_WHITELISTED); - } - - /** - * @dev add addresses to the whitelist - * @param _operators addresses - * @return true if at least one address was added to the whitelist, - * false if all addresses were already in the whitelist - */ - function addAddressesToWhitelist(address[] _operators) - public - onlyOwner - { - for (uint256 i = 0; i < _operators.length; i++) { - addAddressToWhitelist(_operators[i]); - } - } - - /** - * @dev remove an address from the whitelist - * @param _operator address - * @return true if the address was removed from the whitelist, - * false if the address wasn't in the whitelist in the first place - */ - function removeAddressFromWhitelist(address _operator) - public - onlyOwner - { - _removeRole(_operator, ROLE_WHITELISTED); - } - - /** - * @dev remove addresses from the whitelist - * @param _operators addresses - * @return true if at least one address was removed from the whitelist, - * false if all addresses weren't in the whitelist in the first place - */ - function removeAddressesFromWhitelist(address[] _operators) - public - onlyOwner - { - for (uint256 i = 0; i < _operators.length; i++) { - removeAddressFromWhitelist(_operators[i]); - } - } - -} diff --git a/contracts/access/rbac/CapperRole.sol b/contracts/access/rbac/CapperRole.sol new file mode 100644 index 00000000000..d73ebc97a4a --- /dev/null +++ b/contracts/access/rbac/CapperRole.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.4.24; + +import "./Roles.sol"; + + +contract CapperRole { + using Roles for Roles.Role; + + Roles.Role private cappers; + + constructor() public { + cappers.add(msg.sender); + } + + modifier onlyCapper() { + require(isCapper(msg.sender)); + _; + } + + function isCapper(address _account) public view returns (bool) { + return cappers.has(_account); + } + + function addCapper(address _account) public onlyCapper { + cappers.add(_account); + } + + function renounceCapper() public { + cappers.remove(msg.sender); + } + + function _removeCapper(address _account) internal { + cappers.remove(_account); + } +} diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 693e0b498e1..d8aa1eeabae 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -1,16 +1,15 @@ pragma solidity ^0.4.24; import "../../math/SafeMath.sol"; -import "../../ownership/Ownable.sol"; import "../validation/TimedCrowdsale.sol"; /** * @title FinalizableCrowdsale - * @dev Extension of Crowdsale where an owner can do extra work - * after finishing. + * @dev Extension of Crowdsale with a one-off finalization action, where one + * can do extra work after finishing. */ -contract FinalizableCrowdsale is Ownable, TimedCrowdsale { +contract FinalizableCrowdsale is TimedCrowdsale { using SafeMath for uint256; bool private finalized_ = false; @@ -20,7 +19,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { /** * @return true if the crowdsale is finalized, false otherwise. */ - function finalized() public view returns(bool) { + function finalized() public view returns (bool) { return finalized_; } @@ -28,7 +27,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { * @dev Must be called after crowdsale ends, to do some extra finalization * work. Calls the contract's finalization function. */ - function finalize() public onlyOwner { + function finalize() public { require(!finalized_); require(hasClosed()); diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 6279fbbdeb9..6e8f368238f 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -57,7 +57,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { } /** - * @dev escrow finalization task, called when owner calls finalize() + * @dev escrow finalization task, called when finalize() is called */ function _finalization() internal { if (goalReached()) { diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index f0c3fdd4c43..e3375d565da 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -2,14 +2,14 @@ pragma solidity ^0.4.24; import "../../math/SafeMath.sol"; import "../Crowdsale.sol"; -import "../../ownership/Ownable.sol"; +import "../../access/rbac/CapperRole.sol"; /** * @title IndividuallyCappedCrowdsale * @dev Crowdsale with per-user caps. */ -contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { +contract IndividuallyCappedCrowdsale is Crowdsale, CapperRole { using SafeMath for uint256; mapping(address => uint256) private contributions_; @@ -20,7 +20,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { * @param _beneficiary Address to be capped * @param _cap Wei limit for individual contribution */ - function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner { + function setUserCap(address _beneficiary, uint256 _cap) external onlyCapper { caps_[_beneficiary] = _cap; } @@ -34,7 +34,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { uint256 _cap ) external - onlyOwner + onlyCapper { for (uint256 i = 0; i < _beneficiaries.length; i++) { caps_[_beneficiaries[i]] = _cap; diff --git a/contracts/crowdsale/validation/WhitelistedCrowdsale.sol b/contracts/crowdsale/validation/WhitelistedCrowdsale.sol deleted file mode 100644 index e3d1de08e7f..00000000000 --- a/contracts/crowdsale/validation/WhitelistedCrowdsale.sol +++ /dev/null @@ -1,27 +0,0 @@ -pragma solidity ^0.4.24; - -import "../Crowdsale.sol"; -import "../../access/Whitelist.sol"; - - -/** - * @title WhitelistedCrowdsale - * @dev Crowdsale in which only whitelisted users can contribute. - */ -contract WhitelistedCrowdsale is Whitelist, Crowdsale { - /** - * @dev Extend parent behavior requiring beneficiary to be in whitelist. - * @param _beneficiary Token beneficiary - * @param _weiAmount Amount of wei contributed - */ - function _preValidatePurchase( - address _beneficiary, - uint256 _weiAmount - ) - internal - onlyIfWhitelisted(_beneficiary) - { - super._preValidatePurchase(_beneficiary, _weiAmount); - } - -} diff --git a/contracts/mocks/CapperRoleMock.sol b/contracts/mocks/CapperRoleMock.sol new file mode 100644 index 00000000000..08d2aff7b0a --- /dev/null +++ b/contracts/mocks/CapperRoleMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.24; + +import "../access/rbac/CapperRole.sol"; + + +contract CapperRoleMock is CapperRole { + function removeCapper(address _account) public { + _removeCapper(_account); + } + + function onlyCapperMock() public view onlyCapper { + } + + // Causes a compilation error if super._removeCapper is not internal + function _removeCapper(address _account) internal { + super._removeCapper(_account); + } +} diff --git a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol index b4b470f7ca6..552e9f8c5ca 100644 --- a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol +++ b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol @@ -2,11 +2,13 @@ pragma solidity ^0.4.24; import "../token/ERC20/IERC20.sol"; import "../crowdsale/validation/IndividuallyCappedCrowdsale.sol"; +import "./CapperRoleMock.sol"; -contract IndividuallyCappedCrowdsaleImpl is IndividuallyCappedCrowdsale { +contract IndividuallyCappedCrowdsaleImpl + is IndividuallyCappedCrowdsale, CapperRoleMock { - constructor ( + constructor( uint256 _rate, address _wallet, IERC20 _token @@ -15,5 +17,4 @@ contract IndividuallyCappedCrowdsaleImpl is IndividuallyCappedCrowdsale { Crowdsale(_rate, _wallet, _token) { } - } diff --git a/contracts/mocks/WhitelistMock.sol b/contracts/mocks/WhitelistMock.sol deleted file mode 100644 index 081b6631bb2..00000000000 --- a/contracts/mocks/WhitelistMock.sol +++ /dev/null @@ -1,14 +0,0 @@ -pragma solidity ^0.4.24; - -import "../access/Whitelist.sol"; - - -contract WhitelistMock is Whitelist { - - function onlyWhitelistedCanDoThis() - external - onlyIfWhitelisted(msg.sender) - view - { - } -} diff --git a/contracts/mocks/WhitelistedCrowdsaleImpl.sol b/contracts/mocks/WhitelistedCrowdsaleImpl.sol deleted file mode 100644 index 25cd5118aea..00000000000 --- a/contracts/mocks/WhitelistedCrowdsaleImpl.sol +++ /dev/null @@ -1,19 +0,0 @@ -pragma solidity ^0.4.24; - -import "../token/ERC20/IERC20.sol"; -import "../crowdsale/validation/WhitelistedCrowdsale.sol"; -import "../crowdsale/Crowdsale.sol"; - - -contract WhitelistedCrowdsaleImpl is Crowdsale, WhitelistedCrowdsale { - - constructor ( - uint256 _rate, - address _wallet, - IERC20 _token - ) - Crowdsale(_rate, _wallet, _token) - public - { - } -} diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index 802dd471128..fc46ee03a19 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -1,16 +1,16 @@ pragma solidity ^0.4.23; import "./ConditionalEscrow.sol"; -import "../ownership/Ownable.sol"; +import "../ownership/Secondary.sol"; /** * @title RefundEscrow * @dev Escrow that holds funds for a beneficiary, deposited from multiple parties. - * The contract owner may close the deposit period, and allow for either withdrawal + * The primary account may close the deposit period, and allow for either withdrawal * by the beneficiary, or refunds to the depositors. */ -contract RefundEscrow is Ownable, ConditionalEscrow { +contract RefundEscrow is Secondary, ConditionalEscrow { enum State { Active, Refunding, Closed } event Closed(); @@ -32,14 +32,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { /** * @return the current state of the escrow. */ - function state() public view returns(State) { + function state() public view returns (State) { return state_; } /** * @return the beneficiary of the escrow. */ - function beneficiary() public view returns(address) { + function beneficiary() public view returns (address) { return beneficiary_; } @@ -56,7 +56,7 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Allows for the beneficiary to withdraw their funds, rejecting * further deposits. */ - function close() public onlyOwner { + function close() public onlyPrimary { require(state_ == State.Active); state_ = State.Closed; emit Closed(); @@ -65,7 +65,7 @@ contract RefundEscrow is Ownable, ConditionalEscrow { /** * @dev Allows for refunds to take place, rejecting further deposits. */ - function enableRefunds() public onlyOwner { + function enableRefunds() public onlyPrimary { require(state_ == State.Active); state_ = State.Refunding; emit RefundsEnabled(); diff --git a/test/access/Whitelist.test.js b/test/access/Whitelist.test.js deleted file mode 100644 index f1113ffc3c9..00000000000 --- a/test/access/Whitelist.test.js +++ /dev/null @@ -1,65 +0,0 @@ -const { expectThrow } = require('../helpers/expectThrow'); - -const WhitelistMock = artifacts.require('WhitelistMock'); - -require('chai') - .should(); - -contract('Whitelist', function ([_, owner, whitelistedAddress1, whitelistedAddress2, anyone]) { - const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2]; - - beforeEach(async function () { - this.mock = await WhitelistMock.new({ from: owner }); - }); - - context('in normal conditions', function () { - it('should add address to the whitelist', async function () { - await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }); - (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(true); - }); - - it('should add addresses to the whitelist', async function () { - await this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }); - for (const addr of whitelistedAddresses) { - (await this.mock.isWhitelisted(addr)).should.equal(true); - } - }); - - it('should remove address from the whitelist', async function () { - await this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }); - (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(false); - }); - - it('should remove addresses from the the whitelist', async function () { - await this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }); - for (const addr of whitelistedAddresses) { - (await this.mock.isWhitelisted(addr)).should.equal(false); - } - }); - - it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async function () { - await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }); - await this.mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 }); - }); - }); - - context('in adversarial conditions', function () { - it('should not allow "anyone" to add to the whitelist', async function () { - await expectThrow( - this.mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone }) - ); - }); - - it('should not allow "anyone" to remove from the whitelist', async function () { - await expectThrow( - this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone }) - ); - }); - - it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async function () { - await expectThrow( - this.mock.onlyWhitelistedCanDoThis({ from: anyone }) - ); - }); - }); -}); diff --git a/test/access/rbac/CapperRole.test.js b/test/access/rbac/CapperRole.test.js new file mode 100644 index 00000000000..117b25b03d9 --- /dev/null +++ b/test/access/rbac/CapperRole.test.js @@ -0,0 +1,11 @@ +const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const CapperRoleMock = artifacts.require('CapperRoleMock'); + +contract('CapperRole', function ([_, capper, otherCapper, ...otherAccounts]) { + beforeEach(async function () { + this.contract = await CapperRoleMock.new({ from: capper }); + await this.contract.addCapper(otherCapper, { from: capper }); + }); + + shouldBehaveLikePublicRole(capper, otherCapper, otherAccounts, 'capper'); +}); diff --git a/test/crowdsale/FinalizableCrowdsale.test.js b/test/crowdsale/FinalizableCrowdsale.test.js index e6f2914ea58..df7a743dd20 100644 --- a/test/crowdsale/FinalizableCrowdsale.test.js +++ b/test/crowdsale/FinalizableCrowdsale.test.js @@ -13,7 +13,7 @@ const should = require('chai') const FinalizableCrowdsale = artifacts.require('FinalizableCrowdsaleImpl'); const ERC20 = artifacts.require('ERC20'); -contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { +contract('FinalizableCrowdsale', function ([_, wallet, anyone]) { const rate = new BigNumber(1000); before(async function () { @@ -28,33 +28,28 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { this.token = await ERC20.new(); this.crowdsale = await FinalizableCrowdsale.new( - this.openingTime, this.closingTime, rate, wallet, this.token.address, { from: owner } + this.openingTime, this.closingTime, rate, wallet, this.token.address ); }); it('cannot be finalized before ending', async function () { - await expectThrow(this.crowdsale.finalize({ from: owner }), EVMRevert); + await expectThrow(this.crowdsale.finalize({ from: anyone }), EVMRevert); }); - it('cannot be finalized by third party after ending', async function () { + it('can be finalized by anyone after ending', async function () { await increaseTimeTo(this.afterClosingTime); - await expectThrow(this.crowdsale.finalize({ from: thirdparty }), EVMRevert); - }); - - it('can be finalized by owner after ending', async function () { - await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); + await this.crowdsale.finalize({ from: anyone }); }); it('cannot be finalized twice', async function () { await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); - await expectThrow(this.crowdsale.finalize({ from: owner }), EVMRevert); + await this.crowdsale.finalize({ from: anyone }); + await expectThrow(this.crowdsale.finalize({ from: anyone }), EVMRevert); }); it('logs finalized', async function () { await increaseTimeTo(this.afterClosingTime); - const { logs } = await this.crowdsale.finalize({ from: owner }); + const { logs } = await this.crowdsale.finalize({ from: anyone }); const event = logs.find(e => e.event === 'CrowdsaleFinalized'); should.exist(event); }); diff --git a/test/crowdsale/IndividuallyCappedCrowdsale.test.js b/test/crowdsale/IndividuallyCappedCrowdsale.test.js index 8deeca37700..64eddec9748 100644 --- a/test/crowdsale/IndividuallyCappedCrowdsale.test.js +++ b/test/crowdsale/IndividuallyCappedCrowdsale.test.js @@ -8,10 +8,12 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -const CappedCrowdsale = artifacts.require('IndividuallyCappedCrowdsaleImpl'); +const IndividuallyCappedCrowdsaleImpl = artifacts.require('IndividuallyCappedCrowdsaleImpl'); const SimpleToken = artifacts.require('SimpleToken'); +const { shouldBehaveLikePublicRole } = require('../access/rbac/PublicRole.behavior'); -contract('IndividuallyCappedCrowdsale', function ([_, wallet, alice, bob, charlie]) { +contract('IndividuallyCappedCrowdsale', function ( + [_, capper, otherCapper, wallet, alice, bob, charlie, anyone, ...otherAccounts]) { const rate = new BigNumber(1); const capAlice = ether(10); const capBob = ether(2); @@ -19,84 +21,117 @@ contract('IndividuallyCappedCrowdsale', function ([_, wallet, alice, bob, charli const lessThanCapBoth = ether(1); const tokenSupply = new BigNumber('1e22'); - describe('individual capping', function () { + beforeEach(async function () { + this.token = await SimpleToken.new(); + this.crowdsale = await IndividuallyCappedCrowdsaleImpl.new(rate, wallet, this.token.address, { from: capper }); + }); + + describe('capper role', function () { 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.token.transfer(this.crowdsale.address, tokenSupply); + this.contract = this.crowdsale; + await this.contract.addCapper(otherCapper, { from: capper }); }); - describe('accepting payments', function () { - it('should accept payments within cap', async function () { - await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice }); - await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth }); - }); + shouldBehaveLikePublicRole(capper, otherCapper, otherAccounts, 'capper'); + }); - it('should reject payments outside cap', async function () { - await this.crowdsale.buyTokens(alice, { value: capAlice }); - await expectThrow(this.crowdsale.buyTokens(alice, { value: 1 }), EVMRevert); - }); + describe('individual caps', function () { + it('sets a cap when the sender is a capper', async function () { + await this.crowdsale.setUserCap(alice, capAlice, { from: capper }); + (await this.crowdsale.getUserCap(alice)).should.be.bignumber.equal(capAlice); + }); - it('should reject payments that exceed cap', async function () { - await expectThrow(this.crowdsale.buyTokens(alice, { value: capAlice.plus(1) }), EVMRevert); - await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert); - }); + it('reverts when a non-capper sets a cap', async function () { + await expectThrow(this.crowdsale.setUserCap(alice, capAlice, { from: anyone }), EVMRevert); + }); - it('should manage independent caps', async function () { - await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice }); - await expectThrow(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }), EVMRevert); + context('with individual caps', function () { + beforeEach(async function () { + await this.crowdsale.setUserCap(alice, capAlice, { from: capper }); + await this.crowdsale.setUserCap(bob, capBob, { from: capper }); + await this.token.transfer(this.crowdsale.address, tokenSupply); }); - it('should default to a cap of zero', async function () { - await expectThrow(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }), EVMRevert); + describe('accepting payments', function () { + it('should accept payments within cap', async function () { + await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice }); + await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth }); + }); + + it('should reject payments outside cap', async function () { + await this.crowdsale.buyTokens(alice, { value: capAlice }); + await expectThrow(this.crowdsale.buyTokens(alice, { value: 1 }), EVMRevert); + }); + + it('should reject payments that exceed cap', async function () { + await expectThrow(this.crowdsale.buyTokens(alice, { value: capAlice.plus(1) }), EVMRevert); + await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert); + }); + + it('should manage independent caps', async function () { + await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice }); + await expectThrow(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }), EVMRevert); + }); + + it('should default to a cap of zero', async function () { + await expectThrow(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }), EVMRevert); + }); }); - }); - describe('reporting state', function () { - it('should report correct cap', async function () { - (await this.crowdsale.getUserCap(alice)).should.be.bignumber.equal(capAlice); - }); + describe('reporting state', function () { + it('should report correct cap', async function () { + (await this.crowdsale.getUserCap(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); + 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); + it('sets caps when the sender is a capper', async function () { + await this.crowdsale.setGroupCap([bob, charlie], capBob, { from: capper }); + (await this.crowdsale.getUserCap(bob)).should.be.bignumber.equal(capBob); + (await this.crowdsale.getUserCap(charlie)).should.be.bignumber.equal(capBob); }); - 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('reverts when a non-capper set caps', async function () { + await expectThrow(this.crowdsale.setGroupCap([bob, charlie], capBob, { from: anyone }), EVMRevert); + }); - 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); + context('with group caps', function () { + beforeEach(async function () { + await this.crowdsale.setGroupCap([bob, charlie], capBob, { from: capper }); + await this.token.transfer(this.crowdsale.address, tokenSupply); }); - 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('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); + 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); + }); }); }); }); diff --git a/test/crowdsale/RefundableCrowdsale.test.js b/test/crowdsale/RefundableCrowdsale.test.js index c031073353f..3a5305ceb5d 100644 --- a/test/crowdsale/RefundableCrowdsale.test.js +++ b/test/crowdsale/RefundableCrowdsale.test.js @@ -15,7 +15,7 @@ require('chai') const RefundableCrowdsale = artifacts.require('RefundableCrowdsaleImpl'); const SimpleToken = artifacts.require('SimpleToken'); -contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser]) { +contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyone]) { const rate = new BigNumber(1); const goal = ether(50); const lessThanGoal = ether(45); @@ -38,7 +38,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser it('rejects a goal of zero', async function () { await expectThrow( RefundableCrowdsale.new( - this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner } + this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, ), EVMRevert, ); @@ -47,7 +47,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser context('with crowdsale', function () { beforeEach(async function () { this.crowdsale = await RefundableCrowdsale.new( - this.openingTime, this.closingTime, rate, wallet, this.token.address, goal, { from: owner } + this.openingTime, this.closingTime, rate, wallet, this.token.address, goal ); await this.token.transfer(this.crowdsale.address, tokenSupply); @@ -76,7 +76,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser context('after closing time and finalization', function () { beforeEach(async function () { await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); + await this.crowdsale.finalize({ from: anyone }); }); it('refunds', async function () { @@ -96,7 +96,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser context('after closing time and finalization', function () { beforeEach(async function () { await increaseTimeTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); + await this.crowdsale.finalize({ from: anyone }); }); it('denies refunds', async function () { diff --git a/test/crowdsale/WhitelistedCrowdsale.test.js b/test/crowdsale/WhitelistedCrowdsale.test.js deleted file mode 100644 index 3cf9b3206b9..00000000000 --- a/test/crowdsale/WhitelistedCrowdsale.test.js +++ /dev/null @@ -1,89 +0,0 @@ -const { ether } = require('../helpers/ether'); -const { expectThrow } = require('../helpers/expectThrow'); - -const BigNumber = web3.BigNumber; - -require('chai') - .should(); - -const WhitelistedCrowdsale = artifacts.require('WhitelistedCrowdsaleImpl'); -const SimpleToken = artifacts.require('SimpleToken'); - -contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized, anotherAuthorized]) { - const rate = 1; - const value = ether(42); - const tokenSupply = new BigNumber('1e22'); - - describe('single user whitelisting', function () { - beforeEach(async function () { - 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.addAddressToWhitelist(authorized); - }); - - describe('accepting payments', function () { - it('should accept payments to whitelisted (from whichever buyers)', async function () { - await this.crowdsale.sendTransaction({ value, from: authorized }); - await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }); - await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized }); - }); - - it('should reject payments to not whitelisted (from whichever buyers)', async function () { - await expectThrow(this.crowdsale.sendTransaction({ value, from: unauthorized })); - await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized })); - await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized })); - }); - - it('should reject payments to addresses removed from whitelist', async function () { - await this.crowdsale.removeAddressFromWhitelist(authorized); - await expectThrow(this.crowdsale.buyTokens(authorized, { value: value, from: authorized })); - }); - }); - - describe('reporting whitelisted', function () { - it('should correctly report whitelisted addresses', async function () { - (await this.crowdsale.isWhitelisted(authorized)).should.equal(true); - (await this.crowdsale.isWhitelisted(unauthorized)).should.equal(false); - }); - }); - }); - - describe('many user whitelisting', function () { - beforeEach(async function () { - 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.addAddressesToWhitelist([authorized, anotherAuthorized]); - }); - - describe('accepting payments', function () { - it('should accept payments to whitelisted (from whichever buyers)', async function () { - await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }); - await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized }); - await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }); - await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: unauthorized }); - }); - - it('should reject payments to not whitelisted (with whichever buyers)', async function () { - await expectThrow(this.crowdsale.send(value)); - await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized })); - await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized })); - }); - - it('should reject payments to addresses removed from whitelist', async function () { - await this.crowdsale.removeAddressFromWhitelist(anotherAuthorized); - await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }); - await expectThrow(this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized })); - }); - }); - - describe('reporting whitelisted', function () { - it('should correctly report whitelisted addresses', async function () { - (await this.crowdsale.isWhitelisted(authorized)).should.equal(true); - (await this.crowdsale.isWhitelisted(anotherAuthorized)).should.equal(true); - (await this.crowdsale.isWhitelisted(unauthorized)).should.equal(false); - }); - }); - }); -}); diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index 7730d8242a0..e412816c405 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -11,20 +11,20 @@ require('chai') const RefundEscrow = artifacts.require('RefundEscrow'); -contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2]) { +contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) { const amount = web3.toWei(54.0, 'ether'); const refundees = [refundee1, refundee2]; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; it('requires a non-null beneficiary', async function () { await expectThrow( - RefundEscrow.new(ZERO_ADDRESS, { from: owner }) + RefundEscrow.new(ZERO_ADDRESS, { from: primary }) ); }); context('once deployed', function () { beforeEach(async function () { - this.escrow = await RefundEscrow.new(beneficiary, { from: owner }); + this.escrow = await RefundEscrow.new(beneficiary, { from: primary }); }); context('active state', function () { @@ -34,39 +34,39 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); it('accepts deposits', async function () { - await this.escrow.deposit(refundee1, { from: owner, value: amount }); + await this.escrow.deposit(refundee1, { from: primary, value: amount }); (await this.escrow.depositsOf(refundee1)).should.be.bignumber.equal(amount); }); it('does not refund refundees', async function () { - await this.escrow.deposit(refundee1, { from: owner, value: amount }); + await this.escrow.deposit(refundee1, { from: primary, value: amount }); await expectThrow(this.escrow.withdraw(refundee1), EVMRevert); }); it('does not allow beneficiary withdrawal', async function () { - await this.escrow.deposit(refundee1, { from: owner, value: amount }); + await this.escrow.deposit(refundee1, { from: primary, value: amount }); await expectThrow(this.escrow.beneficiaryWithdraw(), EVMRevert); }); }); - it('only owner can enter closed state', async function () { + it('only the primary account can enter closed state', async function () { await expectThrow(this.escrow.close({ from: beneficiary }), EVMRevert); - const receipt = await this.escrow.close({ from: owner }); + const receipt = await this.escrow.close({ from: primary }); expectEvent.inLogs(receipt.logs, 'Closed'); }); context('closed state', function () { beforeEach(async function () { - await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount }))); + await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount }))); - await this.escrow.close({ from: owner }); + await this.escrow.close({ from: primary }); }); it('rejects deposits', async function () { - await expectThrow(this.escrow.deposit(refundee1, { from: owner, value: amount }), EVMRevert); + await expectThrow(this.escrow.deposit(refundee1, { from: primary, value: amount }), EVMRevert); }); it('does not refund refundees', async function () { @@ -82,37 +82,37 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); it('prevents entering the refund state', async function () { - await expectThrow(this.escrow.enableRefunds({ from: owner }), EVMRevert); + await expectThrow(this.escrow.enableRefunds({ from: primary }), EVMRevert); }); it('prevents re-entering the closed state', async function () { - await expectThrow(this.escrow.close({ from: owner }), EVMRevert); + await expectThrow(this.escrow.close({ from: primary }), EVMRevert); }); }); - it('only owner can enter refund state', async function () { + it('only the primary account can enter refund state', async function () { await expectThrow(this.escrow.enableRefunds({ from: beneficiary }), EVMRevert); - const receipt = await this.escrow.enableRefunds({ from: owner }); + const receipt = await this.escrow.enableRefunds({ from: primary }); expectEvent.inLogs(receipt.logs, 'RefundsEnabled'); }); context('refund state', function () { beforeEach(async function () { - await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount }))); + await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount }))); - await this.escrow.enableRefunds({ from: owner }); + await this.escrow.enableRefunds({ from: primary }); }); it('rejects deposits', async function () { - await expectThrow(this.escrow.deposit(refundee1, { from: owner, value: amount }), EVMRevert); + await expectThrow(this.escrow.deposit(refundee1, { from: primary, value: amount }), EVMRevert); }); it('refunds refundees', async function () { for (const refundee of [refundee1, refundee2]) { const refundeeInitialBalance = await ethGetBalance(refundee); - await this.escrow.withdraw(refundee, { from: owner }); + await this.escrow.withdraw(refundee, { from: primary }); const refundeeFinalBalance = await ethGetBalance(refundee); refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount); @@ -124,11 +124,11 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); it('prevents entering the closed state', async function () { - await expectThrow(this.escrow.close({ from: owner }), EVMRevert); + await expectThrow(this.escrow.close({ from: primary }), EVMRevert); }); it('prevents re-entering the refund state', async function () { - await expectThrow(this.escrow.enableRefunds({ from: owner }), EVMRevert); + await expectThrow(this.escrow.enableRefunds({ from: primary }), EVMRevert); }); }); }); From d4dea3c8977f780bac13aba298f62554fdb26fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 6 Sep 2018 19:36:44 -0300 Subject: [PATCH 11/14] Remove RBAC, SignatureBouncer refactor (#1289) * Added CapperRole. * RefundEscrow is now Secondary. * FinalizableCrowdsale is no longer Ownable. * Removed Whitelist and WhitelistedCrowdsale, redesign needed. * Fixed linter errors, disabled lbrace due to it being buggy. * Moved SignatureBouncer tests. * Deleted RBAC and Superuser. * Deleted rbac directory. * Updated readme. * SignatureBouncer now uses SignerRole, renamed bouncer to signer. --- README.md | 3 +- contracts/access/{rbac => }/Roles.sol | 2 - contracts/access/rbac/RBAC.sol | 106 -------- .../access/{rbac => roles}/CapperRole.sol | 2 +- .../access/{rbac => roles}/MinterRole.sol | 2 +- .../access/{rbac => roles}/PauserRole.sol | 2 +- contracts/access/roles/SignerRole.sol | 35 +++ .../IndividuallyCappedCrowdsale.sol | 2 +- contracts/drafts/SignatureBouncer.sol | 64 ++--- contracts/examples/RBACWithAdmin.sol | 73 ------ contracts/lifecycle/Pausable.sol | 2 +- contracts/mocks/CapperRoleMock.sol | 2 +- contracts/mocks/MinterRoleMock.sol | 2 +- contracts/mocks/PauserRoleMock.sol | 2 +- contracts/mocks/RBACMock.sol | 69 ----- contracts/mocks/RolesMock.sol | 2 +- contracts/mocks/SignatureBouncerMock.sol | 3 +- contracts/mocks/SignerRoleMock.sol | 18 ++ contracts/ownership/Superuser.sol | 62 ----- contracts/token/ERC20/ERC20Mintable.sol | 2 +- test/access/{rbac => }/Roles.test.js | 0 test/access/SignatureBouncer.test.js | 246 ------------------ test/access/rbac/CapperRole.test.js | 2 +- test/access/rbac/RBAC.test.js | 73 ------ test/access/roles/CapperRole.test.js | 11 + .../access/{rbac => roles}/MinterRole.test.js | 2 +- .../access/{rbac => roles}/PauserRole.test.js | 2 +- .../{rbac => roles}/PublicRole.behavior.js | 2 +- test/access/roles/SignerRole.test.js | 11 + .../IndividuallyCappedCrowdsale.test.js | 2 +- test/drafts/SignatureBouncer.test.js | 212 +++++++++++++++ test/helpers/sign.js | 4 +- test/lifecycle/Pausable.test.js | 2 +- test/ownership/Superuser.test.js | 69 ----- test/token/ERC20/ERC20Mintable.test.js | 2 +- test/token/ERC20/ERC20Pausable.test.js | 2 +- test/token/ERC721/ERC721Pausable.test.js | 2 +- 37 files changed, 327 insertions(+), 772 deletions(-) rename contracts/access/{rbac => }/Roles.sol (90%) delete mode 100644 contracts/access/rbac/RBAC.sol rename contracts/access/{rbac => roles}/CapperRole.sol (96%) rename contracts/access/{rbac => roles}/MinterRole.sol (96%) rename contracts/access/{rbac => roles}/PauserRole.sol (96%) create mode 100644 contracts/access/roles/SignerRole.sol delete mode 100644 contracts/examples/RBACWithAdmin.sol delete mode 100644 contracts/mocks/RBACMock.sol create mode 100644 contracts/mocks/SignerRoleMock.sol delete mode 100644 contracts/ownership/Superuser.sol rename test/access/{rbac => }/Roles.test.js (100%) delete mode 100644 test/access/SignatureBouncer.test.js delete mode 100644 test/access/rbac/RBAC.test.js create mode 100644 test/access/roles/CapperRole.test.js rename test/access/{rbac => roles}/MinterRole.test.js (81%) rename test/access/{rbac => roles}/PauserRole.test.js (81%) rename test/access/{rbac => roles}/PublicRole.behavior.js (97%) create mode 100644 test/access/roles/SignerRole.test.js create mode 100644 test/drafts/SignatureBouncer.test.js delete mode 100644 test/ownership/Superuser.test.js diff --git a/README.md b/README.md index f866b881e0c..887083c2e92 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,7 @@ contract MyContract is Ownable { ## Architecture The following provides visibility into how OpenZeppelin's contracts are organized: -- **access** - Smart contracts that enable functionality that can be used for selective restrictions and basic authorization control functions. Includes address whitelisting and signature-based permissions management. - - **rbac** - A library used to manage addresses assigned to different user roles and an example Role-Based Access Control (RBAC) interface that demonstrates how to handle setters and getters for roles and addresses. +- **access** - Smart contracts that enable functionality that can be used for selective restrictions and basic authorization control functions. - **crowdsale** - A collection of smart contracts used to manage token crowdsales that allow investors to purchase tokens with ETH. Includes a base contract which implements fundamental crowdsale functionality in its simplest form. The base contract can be extended in order to satisfy your crowdsale’s specific requirements. - **distribution** - Includes extensions of the base crowdsale contract which can be used to customize the completion of a crowdsale. - **emission** - Includes extensions of the base crowdsale contract which can be used to mint and manage how tokens are issued to purchasers. diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/Roles.sol similarity index 90% rename from contracts/access/rbac/Roles.sol rename to contracts/access/Roles.sol index e3d9f867bf1..f5523a3e1b1 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/Roles.sol @@ -3,9 +3,7 @@ pragma solidity ^0.4.24; /** * @title Roles - * @author Francisco Giordano (@frangio) * @dev Library for managing addresses assigned to a Role. - * See RBAC.sol for example usage. */ library Roles { struct Role { diff --git a/contracts/access/rbac/RBAC.sol b/contracts/access/rbac/RBAC.sol deleted file mode 100644 index 70e2f9bca28..00000000000 --- a/contracts/access/rbac/RBAC.sol +++ /dev/null @@ -1,106 +0,0 @@ -pragma solidity ^0.4.24; - -import "./Roles.sol"; - - -/** - * @title RBAC (Role-Based Access Control) - * @author Matt Condon (@Shrugs) - * @dev Stores and provides setters and getters for roles and addresses. - * Supports unlimited numbers of roles and addresses. - * See //contracts/mocks/RBACMock.sol for an example of usage. - * This RBAC method uses strings to key roles. It may be beneficial - * for you to write your own implementation of this interface using Enums or similar. - */ -contract RBAC { - using Roles for Roles.Role; - - mapping (string => Roles.Role) private roles; - - event RoleAdded(address indexed operator, string role); - event RoleRemoved(address indexed operator, string role); - - /** - * @dev reverts if addr does not have role - * @param _operator address - * @param _role the name of the role - * // reverts - */ - function checkRole(address _operator, string _role) - public - view - { - require(roles[_role].has(_operator)); - } - - /** - * @dev determine if addr has role - * @param _operator address - * @param _role the name of the role - * @return bool - */ - function hasRole(address _operator, string _role) - public - view - returns (bool) - { - return roles[_role].has(_operator); - } - - /** - * @dev add a role to an address - * @param _operator address - * @param _role the name of the role - */ - function _addRole(address _operator, string _role) - internal - { - roles[_role].add(_operator); - emit RoleAdded(_operator, _role); - } - - /** - * @dev remove a role from an address - * @param _operator address - * @param _role the name of the role - */ - function _removeRole(address _operator, string _role) - internal - { - roles[_role].remove(_operator); - emit RoleRemoved(_operator, _role); - } - - /** - * @dev modifier to scope access to a single role (uses msg.sender as addr) - * @param _role the name of the role - * // reverts - */ - modifier onlyRole(string _role) - { - checkRole(msg.sender, _role); - _; - } - - /** - * @dev modifier to scope access to a set of roles (uses msg.sender as addr) - * @param _roles the names of the roles to scope access to - * // reverts - * - * @TODO - when solidity supports dynamic arrays as arguments to modifiers, provide this - * see: https://github.com/ethereum/solidity/issues/2467 - */ - // modifier onlyRoles(string[] _roles) { - // bool hasAnyRole = false; - // for (uint8 i = 0; i < _roles.length; i++) { - // if (hasRole(msg.sender, _roles[i])) { - // hasAnyRole = true; - // break; - // } - // } - - // require(hasAnyRole); - - // _; - // } -} diff --git a/contracts/access/rbac/CapperRole.sol b/contracts/access/roles/CapperRole.sol similarity index 96% rename from contracts/access/rbac/CapperRole.sol rename to contracts/access/roles/CapperRole.sol index d73ebc97a4a..bfa28712c8e 100644 --- a/contracts/access/rbac/CapperRole.sol +++ b/contracts/access/roles/CapperRole.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "./Roles.sol"; +import "../Roles.sol"; contract CapperRole { diff --git a/contracts/access/rbac/MinterRole.sol b/contracts/access/roles/MinterRole.sol similarity index 96% rename from contracts/access/rbac/MinterRole.sol rename to contracts/access/roles/MinterRole.sol index 7fd7d8decaf..4e21b40472e 100644 --- a/contracts/access/rbac/MinterRole.sol +++ b/contracts/access/roles/MinterRole.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "./Roles.sol"; +import "../Roles.sol"; contract MinterRole { diff --git a/contracts/access/rbac/PauserRole.sol b/contracts/access/roles/PauserRole.sol similarity index 96% rename from contracts/access/rbac/PauserRole.sol rename to contracts/access/roles/PauserRole.sol index ec0802d9c68..ed7fb2727a2 100644 --- a/contracts/access/rbac/PauserRole.sol +++ b/contracts/access/roles/PauserRole.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "./Roles.sol"; +import "../Roles.sol"; contract PauserRole { diff --git a/contracts/access/roles/SignerRole.sol b/contracts/access/roles/SignerRole.sol new file mode 100644 index 00000000000..5c01a611eec --- /dev/null +++ b/contracts/access/roles/SignerRole.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.4.24; + +import "../Roles.sol"; + + +contract SignerRole { + using Roles for Roles.Role; + + Roles.Role private signers; + + constructor() public { + signers.add(msg.sender); + } + + modifier onlySigner() { + require(isSigner(msg.sender)); + _; + } + + function isSigner(address _account) public view returns (bool) { + return signers.has(_account); + } + + function addSigner(address _account) public onlySigner { + signers.add(_account); + } + + function renounceSigner() public { + signers.remove(msg.sender); + } + + function _removeSigner(address _account) internal { + signers.remove(_account); + } +} diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index e3375d565da..8a65f66bfd2 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -2,7 +2,7 @@ pragma solidity ^0.4.24; import "../../math/SafeMath.sol"; import "../Crowdsale.sol"; -import "../../access/rbac/CapperRole.sol"; +import "../../access/roles/CapperRole.sol"; /** diff --git a/contracts/drafts/SignatureBouncer.sol b/contracts/drafts/SignatureBouncer.sol index 0b91dcdb3fb..024cfd75c46 100644 --- a/contracts/drafts/SignatureBouncer.sol +++ b/contracts/drafts/SignatureBouncer.sol @@ -1,27 +1,26 @@ pragma solidity ^0.4.24; -import "../ownership/Ownable.sol"; -import "../access/rbac/RBAC.sol"; +import "../access/roles/SignerRole.sol"; import "../cryptography/ECDSA.sol"; /** * @title SignatureBouncer * @author PhABC, Shrugs and aflesher - * @dev Bouncer allows users to submit a signature as a permission to do an action. - * If the signature is from one of the authorized bouncer addresses, the signature - * is valid. The owner of the contract adds/removes bouncers. - * Bouncer addresses can be individual servers signing grants or different + * @dev SignatureBouncer allows users to submit a signature as a permission to do an action. + * If the signature is from one of the authorized signer addresses, the signature + * is valid. + * Signer addresses can be individual servers signing grants or different * users within a decentralized club that have permission to invite other members. * This technique is useful for whitelists and airdrops; instead of putting all * valid addresses on-chain, simply sign a grant of the form - * keccak256(abi.encodePacked(`:contractAddress` + `:granteeAddress`)) using a valid bouncer address. + * keccak256(abi.encodePacked(`:contractAddress` + `:granteeAddress`)) using a valid signer address. * Then restrict access to your crowdsale/whitelist/airdrop using the * `onlyValidSignature` modifier (or implement your own using _isValidSignature). * In addition to `onlyValidSignature`, `onlyValidSignatureAndMethod` and * `onlyValidSignatureAndData` can be used to restrict access to only a given method * or a given method with given parameters respectively. - * See the tests Bouncer.test.js for specific usage examples. + * See the tests in SignatureBouncer.test.js for specific usage examples. * @notice A method that uses the `onlyValidSignatureAndData` modifier must make the _signature * parameter the "last" parameter. You cannot sign a message that has its own * signature in it so the last 128 bytes of msg.data (which represents the @@ -29,11 +28,9 @@ import "../cryptography/ECDSA.sol"; * Also non fixed sized parameters make constructing the data in the signature * much more complex. See https://ethereum.stackexchange.com/a/50616 for more details. */ -contract SignatureBouncer is Ownable, RBAC { +contract SignatureBouncer is SignerRole { using ECDSA for bytes32; - // Name of the bouncer role. - string private constant ROLE_BOUNCER = "bouncer"; // Function selectors are 4 bytes long, as documented in // https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector uint256 private constant METHOD_ID_SIZE = 4; @@ -41,7 +38,7 @@ contract SignatureBouncer is Ownable, RBAC { uint256 private constant SIGNATURE_SIZE = 96; /** - * @dev requires that a valid signature of a bouncer was provided + * @dev requires that a valid signature of a signer was provided */ modifier onlyValidSignature(bytes _signature) { @@ -50,7 +47,7 @@ contract SignatureBouncer is Ownable, RBAC { } /** - * @dev requires that a valid signature with a specifed method of a bouncer was provided + * @dev requires that a valid signature with a specifed method of a signer was provided */ modifier onlyValidSignatureAndMethod(bytes _signature) { @@ -59,7 +56,7 @@ contract SignatureBouncer is Ownable, RBAC { } /** - * @dev requires that a valid signature with a specifed method and params of a bouncer was provided + * @dev requires that a valid signature with a specifed method and params of a signer was provided */ modifier onlyValidSignatureAndData(bytes _signature) { @@ -68,36 +65,7 @@ contract SignatureBouncer is Ownable, RBAC { } /** - * @dev Determine if an account has the bouncer role. - * @return true if the account is a bouncer, false otherwise. - */ - function isBouncer(address _account) public view returns(bool) { - return hasRole(_account, ROLE_BOUNCER); - } - - /** - * @dev allows the owner to add additional bouncer addresses - */ - function addBouncer(address _bouncer) - public - onlyOwner - { - require(_bouncer != address(0)); - _addRole(_bouncer, ROLE_BOUNCER); - } - - /** - * @dev allows the owner to remove bouncer addresses - */ - function removeBouncer(address _bouncer) - public - onlyOwner - { - _removeRole(_bouncer, ROLE_BOUNCER); - } - - /** - * @dev is the signature of `this + sender` from a bouncer? + * @dev is the signature of `this + sender` from a signer? * @return bool */ function _isValidSignature(address _address, bytes _signature) @@ -112,7 +80,7 @@ contract SignatureBouncer is Ownable, RBAC { } /** - * @dev is the signature of `this + sender + methodId` from a bouncer? + * @dev is the signature of `this + sender + methodId` from a signer? * @return bool */ function _isValidSignatureAndMethod(address _address, bytes _signature) @@ -131,7 +99,7 @@ contract SignatureBouncer is Ownable, RBAC { } /** - * @dev is the signature of `this + sender + methodId + params(s)` from a bouncer? + * @dev is the signature of `this + sender + methodId + params(s)` from a signer? * @notice the _signature parameter of the method being validated must be the "last" parameter * @return bool */ @@ -153,7 +121,7 @@ contract SignatureBouncer is Ownable, RBAC { /** * @dev internal function to convert a hash to an eth signed message - * and then recover the signature and check it against the bouncer role + * and then recover the signature and check it against the signer role * @return bool */ function _isValidDataHash(bytes32 _hash, bytes _signature) @@ -164,6 +132,6 @@ contract SignatureBouncer is Ownable, RBAC { address signer = _hash .toEthSignedMessageHash() .recover(_signature); - return isBouncer(signer); + return isSigner(signer); } } diff --git a/contracts/examples/RBACWithAdmin.sol b/contracts/examples/RBACWithAdmin.sol deleted file mode 100644 index 83c2dcef7a8..00000000000 --- a/contracts/examples/RBACWithAdmin.sol +++ /dev/null @@ -1,73 +0,0 @@ -pragma solidity ^0.4.24; - -import "../access/rbac/RBAC.sol"; - - -/** - * @title RBACWithAdmin - * @author Matt Condon (@Shrugs) - * @dev It's recommended that you define constants in the contract, - * like ROLE_ADMIN below, to avoid typos. - * @notice RBACWithAdmin is probably too expansive and powerful for your - * application; an admin is actually able to change any address to any role - * which is a very large API surface. It's recommended that you follow a strategy - * of strictly defining the abilities of your roles - * and the API-surface of your contract. - * This is just an example for example's sake. - */ -contract RBACWithAdmin is RBAC { - /** - * A constant role name for indicating admins. - */ - string private constant ROLE_ADMIN = "admin"; - - /** - * @dev modifier to scope access to admins - * // reverts - */ - modifier onlyAdmin() - { - checkRole(msg.sender, ROLE_ADMIN); - _; - } - - /** - * @dev constructor. Sets msg.sender as admin by default - */ - constructor() - public - { - _addRole(msg.sender, ROLE_ADMIN); - } - - /** - * @return true if the account is admin, false otherwise. - */ - function isAdmin(address _account) public view returns(bool) { - return hasRole(_account, ROLE_ADMIN); - } - - /** - * @dev add a role to an account - * @param _account the account that will have the role - * @param _roleName the name of the role - */ - function adminAddRole(address _account, string _roleName) - public - onlyAdmin - { - _addRole(_account, _roleName); - } - - /** - * @dev remove a role from an account - * @param _account the account that will no longer have the role - * @param _roleName the name of the role - */ - function adminRemoveRole(address _account, string _roleName) - public - onlyAdmin - { - _removeRole(_account, _roleName); - } -} diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index a681aa21b93..28bcac96682 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../access/rbac/PauserRole.sol"; +import "../access/roles/PauserRole.sol"; /** diff --git a/contracts/mocks/CapperRoleMock.sol b/contracts/mocks/CapperRoleMock.sol index 08d2aff7b0a..f4f58a05fdb 100644 --- a/contracts/mocks/CapperRoleMock.sol +++ b/contracts/mocks/CapperRoleMock.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../access/rbac/CapperRole.sol"; +import "../access/roles/CapperRole.sol"; contract CapperRoleMock is CapperRole { diff --git a/contracts/mocks/MinterRoleMock.sol b/contracts/mocks/MinterRoleMock.sol index 20c21e11356..232cbc19b20 100644 --- a/contracts/mocks/MinterRoleMock.sol +++ b/contracts/mocks/MinterRoleMock.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../access/rbac/MinterRole.sol"; +import "../access/roles/MinterRole.sol"; contract MinterRoleMock is MinterRole { diff --git a/contracts/mocks/PauserRoleMock.sol b/contracts/mocks/PauserRoleMock.sol index ea51335d241..21b0b0c0346 100644 --- a/contracts/mocks/PauserRoleMock.sol +++ b/contracts/mocks/PauserRoleMock.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../access/rbac/PauserRole.sol"; +import "../access/roles/PauserRole.sol"; contract PauserRoleMock is PauserRole { diff --git a/contracts/mocks/RBACMock.sol b/contracts/mocks/RBACMock.sol deleted file mode 100644 index f4acc927915..00000000000 --- a/contracts/mocks/RBACMock.sol +++ /dev/null @@ -1,69 +0,0 @@ -pragma solidity ^0.4.24; - -import "../examples/RBACWithAdmin.sol"; - - -contract RBACMock is RBACWithAdmin { - - string internal constant ROLE_ADVISOR = "advisor"; - - modifier onlyAdminOrAdvisor() - { - require( - isAdmin(msg.sender) || - hasRole(msg.sender, ROLE_ADVISOR) - ); - _; - } - - constructor(address[] _advisors) - public - { - _addRole(msg.sender, ROLE_ADVISOR); - - for (uint256 i = 0; i < _advisors.length; i++) { - _addRole(_advisors[i], ROLE_ADVISOR); - } - } - - function onlyAdminsCanDoThis() - external - onlyAdmin - view - { - } - - function onlyAdvisorsCanDoThis() - external - onlyRole(ROLE_ADVISOR) - view - { - } - - function eitherAdminOrAdvisorCanDoThis() - external - onlyAdminOrAdvisor - view - { - } - - function nobodyCanDoThis() - external - onlyRole("unknown") - view - { - } - - // admins can remove advisor's role - function removeAdvisor(address _account) - public - onlyAdmin - { - // revert if the user isn't an advisor - // (perhaps you want to soft-fail here instead?) - checkRole(_account, ROLE_ADVISOR); - - // remove the advisor's role - _removeRole(_account, ROLE_ADVISOR); - } -} diff --git a/contracts/mocks/RolesMock.sol b/contracts/mocks/RolesMock.sol index 22e14312652..6baf873988e 100644 --- a/contracts/mocks/RolesMock.sol +++ b/contracts/mocks/RolesMock.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "../access/rbac/Roles.sol"; +import "../access/Roles.sol"; contract RolesMock { diff --git a/contracts/mocks/SignatureBouncerMock.sol b/contracts/mocks/SignatureBouncerMock.sol index d9fea81f402..d2663d2c527 100644 --- a/contracts/mocks/SignatureBouncerMock.sol +++ b/contracts/mocks/SignatureBouncerMock.sol @@ -1,9 +1,10 @@ pragma solidity ^0.4.24; import "../drafts/SignatureBouncer.sol"; +import "./SignerRoleMock.sol"; -contract SignatureBouncerMock is SignatureBouncer { +contract SignatureBouncerMock is SignatureBouncer, SignerRoleMock { function checkValidSignature(address _address, bytes _signature) public view diff --git a/contracts/mocks/SignerRoleMock.sol b/contracts/mocks/SignerRoleMock.sol new file mode 100644 index 00000000000..0157884e41c --- /dev/null +++ b/contracts/mocks/SignerRoleMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.24; + +import "../access/roles/SignerRole.sol"; + + +contract SignerRoleMock is SignerRole { + function removeSigner(address _account) public { + _removeSigner(_account); + } + + function onlySignerMock() public view onlySigner { + } + + // Causes a compilation error if super._removeSigner is not internal + function _removeSigner(address _account) internal { + super._removeSigner(_account); + } +} diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol deleted file mode 100644 index b1f1fa4b52d..00000000000 --- a/contracts/ownership/Superuser.sol +++ /dev/null @@ -1,62 +0,0 @@ -pragma solidity ^0.4.24; - - -import "./Ownable.sol"; -import "../access/rbac/RBAC.sol"; - - -/** - * @title Superuser - * @dev The Superuser contract defines a single superuser who can transfer the ownership - * of a contract to a new address, even if he is not the owner. - * A superuser can transfer his role to a new address. - */ -contract Superuser is Ownable, RBAC { - string private constant ROLE_SUPERUSER = "superuser"; - - constructor () public { - _addRole(msg.sender, ROLE_SUPERUSER); - } - - /** - * @dev Throws if called by any account that's not a superuser. - */ - modifier onlySuperuser() { - checkRole(msg.sender, ROLE_SUPERUSER); - _; - } - - modifier onlyOwnerOrSuperuser() { - require(msg.sender == owner() || isSuperuser(msg.sender)); - _; - } - - /** - * @dev getter to determine if an account has superuser role - */ - function isSuperuser(address _account) - public - view - returns (bool) - { - return hasRole(_account, ROLE_SUPERUSER); - } - - /** - * @dev Allows the current superuser to transfer his role to a newSuperuser. - * @param _newSuperuser The address to transfer ownership to. - */ - function transferSuperuser(address _newSuperuser) public onlySuperuser { - require(_newSuperuser != address(0)); - _removeRole(msg.sender, ROLE_SUPERUSER); - _addRole(_newSuperuser, ROLE_SUPERUSER); - } - - /** - * @dev Allows the current superuser or owner to transfer control of the contract to a newOwner. - * @param _newOwner The address to transfer ownership to. - */ - function transferOwnership(address _newOwner) public onlyOwnerOrSuperuser { - _transferOwnership(_newOwner); - } -} diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index c42319b99dc..6de17feb8c6 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.24; import "./ERC20.sol"; -import "../../access/rbac/MinterRole.sol"; +import "../../access/roles/MinterRole.sol"; /** diff --git a/test/access/rbac/Roles.test.js b/test/access/Roles.test.js similarity index 100% rename from test/access/rbac/Roles.test.js rename to test/access/Roles.test.js diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js deleted file mode 100644 index 798ab0ac9fa..00000000000 --- a/test/access/SignatureBouncer.test.js +++ /dev/null @@ -1,246 +0,0 @@ -const { assertRevert } = require('../helpers/assertRevert'); -const { getBouncerSigner } = require('../helpers/sign'); - -const Bouncer = artifacts.require('SignatureBouncerMock'); - -const BigNumber = web3.BigNumber; - -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -const UINT_VALUE = 23; -const BYTES_VALUE = web3.toHex('test'); -const INVALID_SIGNATURE = '0xabcd'; - -contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser]) { - beforeEach(async function () { - this.bouncer = await Bouncer.new({ from: owner }); - }); - - context('management', function () { - it('has a default owner of self', async function () { - (await this.bouncer.owner()).should.equal(owner); - }); - - it('allows the owner to add a bouncer', async function () { - await this.bouncer.addBouncer(bouncerAddress, { from: owner }); - (await this.bouncer.isBouncer(bouncerAddress)).should.equal(true); - }); - - it('does not allow adding an invalid address', async function () { - await assertRevert( - this.bouncer.addBouncer('0x0', { from: owner }) - ); - }); - - it('allows the owner to remove a bouncer', async function () { - await this.bouncer.addBouncer(bouncerAddress, { from: owner }); - - await this.bouncer.removeBouncer(bouncerAddress, { from: owner }); - (await this.bouncer.isBouncer(bouncerAddress)).should.equal(false); - }); - - it('does not allow anyone to add a bouncer', async function () { - await assertRevert( - this.bouncer.addBouncer(bouncerAddress, { from: anyone }) - ); - }); - - it('does not allow anyone to remove a bouncer', async function () { - await this.bouncer.addBouncer(bouncerAddress, { from: owner }); - - await assertRevert( - this.bouncer.removeBouncer(bouncerAddress, { from: anyone }) - ); - }); - }); - - context('with bouncer address', function () { - beforeEach(async function () { - await this.bouncer.addBouncer(bouncerAddress, { from: owner }); - this.signFor = getBouncerSigner(this.bouncer, bouncerAddress); - }); - - describe('modifiers', function () { - context('plain signature', function () { - it('allows valid signature for sender', async function () { - await this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser }); - }); - - it('does not allow invalid signature for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser }) - ); - }); - - it('does not allow valid signature for other sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone }) - ); - }); - - it('does not allow valid signature for method for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'), - { from: authorizedUser }) - ); - }); - }); - - context('method signature', function () { - it('allows valid signature with correct method for sender', async function () { - await this.bouncer.onlyWithValidSignatureAndMethod( - this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser } - ); - }); - - it('does not allow invalid signature with correct method for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser }) - ); - }); - - it('does not allow valid signature with correct method for other sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndMethod( - this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone } - ) - ); - }); - - it('does not allow valid method signature with incorrect method for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'), - { from: authorizedUser }) - ); - }); - - it('does not allow valid non-method signature method for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser }) - ); - }); - }); - - context('method and data signature', function () { - it('allows valid signature with correct method and data for sender', async function () { - await this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, - this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser } - ); - }); - - it('does not allow invalid signature with correct method and data for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser }) - ); - }); - - it('does not allow valid signature with correct method and incorrect data for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10, - this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), - { from: authorizedUser } - ) - ); - }); - - it('does not allow valid signature with correct method and data for other sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, - this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), - { from: anyone } - ) - ); - }); - - it('does not allow valid non-method signature for sender', async function () { - await assertRevert( - this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, - this.signFor(authorizedUser), { from: authorizedUser } - ) - ); - }); - }); - }); - - context('signature validation', function () { - context('plain signature', function () { - it('validates valid signature for valid user', async function () { - (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true); - }); - - it('does not validate invalid signature for valid user', async function () { - (await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.equal(false); - }); - - it('does not validate valid signature for anyone', async function () { - (await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false); - }); - - it('does not validate valid signature for method for valid user', async function () { - (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature')) - ).should.equal(false); - }); - }); - - context('method signature', function () { - it('validates valid signature with correct method for valid user', async function () { - (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, - this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) - ).should.equal(true); - }); - - it('does not validate invalid signature with correct method for valid user', async function () { - (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.equal(false); - }); - - it('does not validate valid signature with correct method for anyone', async function () { - (await this.bouncer.checkValidSignatureAndMethod(anyone, - this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) - ).should.equal(false); - }); - - it('does not validate valid non-method signature with correct method for valid user', async function () { - (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser)) - ).should.equal(false); - }); - }); - - context('method and data signature', function () { - it('validates valid signature with correct method and data for valid user', async function () { - (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, - this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.equal(true); - }); - - it('does not validate invalid signature with correct method and data for valid user', async function () { - (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE) - ).should.equal(false); - }); - - it('does not validate valid signature with correct method and incorrect data for valid user', - async function () { - (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10, - this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.equal(false); - } - ); - - it('does not validate valid signature with correct method and data for anyone', async function () { - (await this.bouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE, - this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) - ).should.equal(false); - }); - - it('does not validate valid non-method-data signature with correct method and data for valid user', - async function () { - (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, - this.signFor(authorizedUser, 'checkValidSignatureAndData')) - ).should.equal(false); - } - ); - }); - }); - }); -}); diff --git a/test/access/rbac/CapperRole.test.js b/test/access/rbac/CapperRole.test.js index 117b25b03d9..a79944a2778 100644 --- a/test/access/rbac/CapperRole.test.js +++ b/test/access/rbac/CapperRole.test.js @@ -1,4 +1,4 @@ -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); const CapperRoleMock = artifacts.require('CapperRoleMock'); contract('CapperRole', function ([_, capper, otherCapper, ...otherAccounts]) { diff --git a/test/access/rbac/RBAC.test.js b/test/access/rbac/RBAC.test.js deleted file mode 100644 index 32859993d1d..00000000000 --- a/test/access/rbac/RBAC.test.js +++ /dev/null @@ -1,73 +0,0 @@ -const { expectThrow } = require('../../helpers/expectThrow'); -const expectEvent = require('../../helpers/expectEvent'); - -const RBACMock = artifacts.require('RBACMock'); - -require('chai') - .should(); - -const ROLE_ADVISOR = 'advisor'; - -contract('RBAC', function ([_, admin, anyone, advisor, otherAdvisor, futureAdvisor]) { - let mock; - - beforeEach(async function () { - mock = await RBACMock.new([advisor, otherAdvisor], { from: admin }); - }); - - context('in normal conditions', function () { - it('allows admin to call #onlyAdminsCanDoThis', async function () { - await mock.onlyAdminsCanDoThis({ from: admin }); - }); - it('allows admin to call #onlyAdvisorsCanDoThis', async function () { - await mock.onlyAdvisorsCanDoThis({ from: admin }); - }); - it('allows advisors to call #onlyAdvisorsCanDoThis', async function () { - await mock.onlyAdvisorsCanDoThis({ from: advisor }); - }); - it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async function () { - await mock.eitherAdminOrAdvisorCanDoThis({ from: admin }); - }); - it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async function () { - await mock.eitherAdminOrAdvisorCanDoThis({ from: advisor }); - }); - it('does not allow admins to call #nobodyCanDoThis', async function () { - await expectThrow(mock.nobodyCanDoThis({ from: admin })); - }); - it('does not allow advisors to call #nobodyCanDoThis', async function () { - await expectThrow(mock.nobodyCanDoThis({ from: advisor })); - }); - it('does not allow anyone to call #nobodyCanDoThis', async function () { - await expectThrow(mock.nobodyCanDoThis({ from: anyone })); - }); - it('allows an admin to remove an advisor\'s role', async function () { - await mock.removeAdvisor(advisor, { from: admin }); - }); - it('allows admins to #adminRemoveRole', async function () { - await mock.adminRemoveRole(advisor, ROLE_ADVISOR, { from: admin }); - }); - - it('announces a RoleAdded event on addRole', async function () { - await expectEvent.inTransaction( - mock.adminAddRole(futureAdvisor, ROLE_ADVISOR, { from: admin }), - 'RoleAdded' - ); - }); - - it('announces a RoleRemoved event on removeRole', async function () { - await expectEvent.inTransaction( - mock.adminRemoveRole(futureAdvisor, ROLE_ADVISOR, { from: admin }), - 'RoleRemoved' - ); - }); - }); - - context('in adversarial conditions', function () { - it('does not allow an advisor to remove another advisor', async function () { - await expectThrow(mock.removeAdvisor(otherAdvisor, { from: advisor })); - }); - it('does not allow "anyone" to remove an advisor', async function () { - await expectThrow(mock.removeAdvisor(advisor, { from: anyone })); - }); - }); -}); diff --git a/test/access/roles/CapperRole.test.js b/test/access/roles/CapperRole.test.js new file mode 100644 index 00000000000..a79944a2778 --- /dev/null +++ b/test/access/roles/CapperRole.test.js @@ -0,0 +1,11 @@ +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); +const CapperRoleMock = artifacts.require('CapperRoleMock'); + +contract('CapperRole', function ([_, capper, otherCapper, ...otherAccounts]) { + beforeEach(async function () { + this.contract = await CapperRoleMock.new({ from: capper }); + await this.contract.addCapper(otherCapper, { from: capper }); + }); + + shouldBehaveLikePublicRole(capper, otherCapper, otherAccounts, 'capper'); +}); diff --git a/test/access/rbac/MinterRole.test.js b/test/access/roles/MinterRole.test.js similarity index 81% rename from test/access/rbac/MinterRole.test.js rename to test/access/roles/MinterRole.test.js index ed222d20f60..b2e70b53a45 100644 --- a/test/access/rbac/MinterRole.test.js +++ b/test/access/roles/MinterRole.test.js @@ -1,4 +1,4 @@ -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); const MinterRoleMock = artifacts.require('MinterRoleMock'); contract('MinterRole', function ([_, minter, otherMinter, ...otherAccounts]) { diff --git a/test/access/rbac/PauserRole.test.js b/test/access/roles/PauserRole.test.js similarity index 81% rename from test/access/rbac/PauserRole.test.js rename to test/access/roles/PauserRole.test.js index a0767af22de..927e46c0ba8 100644 --- a/test/access/rbac/PauserRole.test.js +++ b/test/access/roles/PauserRole.test.js @@ -1,4 +1,4 @@ -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); const PauserRoleMock = artifacts.require('PauserRoleMock'); contract('PauserRole', function ([_, pauser, otherPauser, ...otherAccounts]) { diff --git a/test/access/rbac/PublicRole.behavior.js b/test/access/roles/PublicRole.behavior.js similarity index 97% rename from test/access/rbac/PublicRole.behavior.js rename to test/access/roles/PublicRole.behavior.js index 80d78e917b5..3181b705165 100644 --- a/test/access/rbac/PublicRole.behavior.js +++ b/test/access/roles/PublicRole.behavior.js @@ -9,7 +9,7 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role rolename = capitalize(rolename); const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - describe(`role ${rolename}`, function () { + describe('should behave like public role', function () { beforeEach('check preconditions', async function () { (await this.contract[`is${rolename}`](authorized)).should.equal(true); (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true); diff --git a/test/access/roles/SignerRole.test.js b/test/access/roles/SignerRole.test.js new file mode 100644 index 00000000000..317c100d7ed --- /dev/null +++ b/test/access/roles/SignerRole.test.js @@ -0,0 +1,11 @@ +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); +const SignerRoleMock = artifacts.require('SignerRoleMock'); + +contract('SignerRole', function ([_, signer, otherSigner, ...otherAccounts]) { + beforeEach(async function () { + this.contract = await SignerRoleMock.new({ from: signer }); + await this.contract.addSigner(otherSigner, { from: signer }); + }); + + shouldBehaveLikePublicRole(signer, otherSigner, otherAccounts, 'signer'); +}); diff --git a/test/crowdsale/IndividuallyCappedCrowdsale.test.js b/test/crowdsale/IndividuallyCappedCrowdsale.test.js index 64eddec9748..49a4fe2700d 100644 --- a/test/crowdsale/IndividuallyCappedCrowdsale.test.js +++ b/test/crowdsale/IndividuallyCappedCrowdsale.test.js @@ -10,7 +10,7 @@ require('chai') const IndividuallyCappedCrowdsaleImpl = artifacts.require('IndividuallyCappedCrowdsaleImpl'); const SimpleToken = artifacts.require('SimpleToken'); -const { shouldBehaveLikePublicRole } = require('../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior'); contract('IndividuallyCappedCrowdsale', function ( [_, capper, otherCapper, wallet, alice, bob, charlie, anyone, ...otherAccounts]) { diff --git a/test/drafts/SignatureBouncer.test.js b/test/drafts/SignatureBouncer.test.js new file mode 100644 index 00000000000..b320ca98c32 --- /dev/null +++ b/test/drafts/SignatureBouncer.test.js @@ -0,0 +1,212 @@ +const { assertRevert } = require('../helpers/assertRevert'); +const { getSignFor } = require('../helpers/sign'); +const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior'); + +const SignatureBouncerMock = artifacts.require('SignatureBouncerMock'); + +const BigNumber = web3.BigNumber; + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +const UINT_VALUE = 23; +const BYTES_VALUE = web3.toHex('test'); +const INVALID_SIGNATURE = '0xabcd'; + +contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authorizedUser, ...otherAccounts]) { + beforeEach(async function () { + this.sigBouncer = await SignatureBouncerMock.new({ from: signer }); + this.signFor = getSignFor(this.sigBouncer, signer); + }); + + describe('signer role', function () { + beforeEach(async function () { + this.contract = this.sigBouncer; + await this.contract.addSigner(otherSigner, { from: signer }); + }); + + shouldBehaveLikePublicRole(signer, otherSigner, otherAccounts, 'signer'); + }); + + describe('modifiers', function () { + context('plain signature', function () { + it('allows valid signature for sender', async function () { + await this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser }); + }); + + it('does not allow invalid signature for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser }) + ); + }); + + it('does not allow valid signature for other sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone }) + ); + }); + + it('does not allow valid signature for method for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'), + { from: authorizedUser }) + ); + }); + }); + + context('method signature', function () { + it('allows valid signature with correct method for sender', async function () { + await this.sigBouncer.onlyWithValidSignatureAndMethod( + this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser } + ); + }); + + it('does not allow invalid signature with correct method for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser }) + ); + }); + + it('does not allow valid signature with correct method for other sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndMethod( + this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone } + ) + ); + }); + + it('does not allow valid method signature with incorrect method for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'), + { from: authorizedUser }) + ); + }); + + it('does not allow valid non-method signature method for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser }) + ); + }); + }); + + context('method and data signature', function () { + it('allows valid signature with correct method and data for sender', async function () { + await this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, + this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser } + ); + }); + + it('does not allow invalid signature with correct method and data for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser }) + ); + }); + + it('does not allow valid signature with correct method and incorrect data for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10, + this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), + { from: authorizedUser } + ) + ); + }); + + it('does not allow valid signature with correct method and data for other sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, + this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), + { from: anyone } + ) + ); + }); + + it('does not allow valid non-method signature for sender', async function () { + await assertRevert( + this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, + this.signFor(authorizedUser), { from: authorizedUser } + ) + ); + }); + }); + }); + + context('signature validation', function () { + context('plain signature', function () { + it('validates valid signature for valid user', async function () { + (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true); + }); + + it('does not validate invalid signature for valid user', async function () { + (await this.sigBouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.equal(false); + }); + + it('does not validate valid signature for anyone', async function () { + (await this.sigBouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false); + }); + + it('does not validate valid signature for method for valid user', async function () { + (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature')) + ).should.equal(false); + }); + }); + + context('method signature', function () { + it('validates valid signature with correct method for valid user', async function () { + (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, + this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) + ).should.equal(true); + }); + + it('does not validate invalid signature with correct method for valid user', async function () { + (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.equal(false); + }); + + it('does not validate valid signature with correct method for anyone', async function () { + (await this.sigBouncer.checkValidSignatureAndMethod(anyone, + this.signFor(authorizedUser, 'checkValidSignatureAndMethod')) + ).should.equal(false); + }); + + it('does not validate valid non-method signature with correct method for valid user', async function () { + (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser)) + ).should.equal(false); + }); + }); + + context('method and data signature', function () { + it('validates valid signature with correct method and data for valid user', async function () { + (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, + this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) + ).should.equal(true); + }); + + it('does not validate invalid signature with correct method and data for valid user', async function () { + (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE) + ).should.equal(false); + }); + + it('does not validate valid signature with correct method and incorrect data for valid user', + async function () { + (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10, + this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) + ).should.equal(false); + } + ); + + it('does not validate valid signature with correct method and data for anyone', async function () { + (await this.sigBouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE, + this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE])) + ).should.equal(false); + }); + + it('does not validate valid non-method-data signature with correct method and data for valid user', + async function () { + (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, + this.signFor(authorizedUser, 'checkValidSignatureAndData')) + ).should.equal(false); + } + ); + }); + }); +}); diff --git a/test/helpers/sign.js b/test/helpers/sign.js index a05238ad4fa..9164a9cd1a2 100644 --- a/test/helpers/sign.js +++ b/test/helpers/sign.js @@ -39,7 +39,7 @@ const transformToFullName = function (json) { * @param methodName string * @param methodArgs any[] */ -const getBouncerSigner = (contract, signer) => (redeemer, methodName, methodArgs = []) => { +const getSignFor = (contract, signer) => (redeemer, methodName, methodArgs = []) => { const parts = [ contract.address, redeemer, @@ -70,5 +70,5 @@ const getBouncerSigner = (contract, signer) => (redeemer, methodName, methodArgs module.exports = { signMessage, toEthSignedMessageHash, - getBouncerSigner, + getSignFor, }; diff --git a/test/lifecycle/Pausable.test.js b/test/lifecycle/Pausable.test.js index 2bbe392963e..2ae4c90d7da 100644 --- a/test/lifecycle/Pausable.test.js +++ b/test/lifecycle/Pausable.test.js @@ -2,7 +2,7 @@ const { assertRevert } = require('../helpers/assertRevert'); const expectEvent = require('../helpers/expectEvent'); const PausableMock = artifacts.require('PausableMock'); -const { shouldBehaveLikePublicRole } = require('../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior'); const BigNumber = web3.BigNumber; diff --git a/test/ownership/Superuser.test.js b/test/ownership/Superuser.test.js deleted file mode 100644 index 5e4d2c32456..00000000000 --- a/test/ownership/Superuser.test.js +++ /dev/null @@ -1,69 +0,0 @@ -const { expectThrow } = require('../helpers/expectThrow'); -const expectEvent = require('../helpers/expectEvent'); - -const Superuser = artifacts.require('Superuser'); - -require('chai') - .should(); - -contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) { - const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; - - beforeEach(async function () { - this.superuser = await Superuser.new({ from: firstOwner }); - }); - - context('in normal conditions', function () { - it('should set the owner as the default superuser', async function () { - (await this.superuser.isSuperuser(firstOwner)).should.equal(true); - }); - - it('should change superuser after transferring', async function () { - await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner }); - - (await this.superuser.isSuperuser(firstOwner)).should.equal(false); - - (await this.superuser.isSuperuser(newSuperuser)).should.equal(true); - }); - - it('should prevent changing to a null superuser', async function () { - await expectThrow( - this.superuser.transferSuperuser(ZERO_ADDRESS, { from: firstOwner }) - ); - }); - - it('should change owner after the superuser transfers the ownership', async function () { - await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner }); - - await expectEvent.inTransaction( - this.superuser.transferOwnership(newOwner, { from: newSuperuser }), - 'OwnershipTransferred' - ); - - (await this.superuser.owner()).should.equal(newOwner); - }); - - it('should change owner after the owner transfers the ownership', async function () { - await expectEvent.inTransaction( - this.superuser.transferOwnership(newOwner, { from: firstOwner }), - 'OwnershipTransferred' - ); - - (await this.superuser.owner()).should.equal(newOwner); - }); - }); - - context('in adversarial conditions', function () { - it('should prevent non-superusers from transfering the superuser role', async function () { - await expectThrow( - this.superuser.transferSuperuser(newOwner, { from: anyone }) - ); - }); - - it('should prevent users that are not superuser nor owner from setting a new owner', async function () { - await expectThrow( - this.superuser.transferOwnership(newOwner, { from: anyone }) - ); - }); - }); -}); diff --git a/test/token/ERC20/ERC20Mintable.test.js b/test/token/ERC20/ERC20Mintable.test.js index 460164d3800..9dca0e55890 100644 --- a/test/token/ERC20/ERC20Mintable.test.js +++ b/test/token/ERC20/ERC20Mintable.test.js @@ -1,6 +1,6 @@ const { shouldBehaveLikeERC20Mintable } = require('./ERC20Mintable.behavior'); const ERC20MintableMock = artifacts.require('ERC20MintableMock'); -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); contract('ERC20Mintable', function ([_, minter, otherMinter, ...otherAccounts]) { beforeEach(async function () { diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index 41013c6bded..59eb5ce3877 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -1,7 +1,7 @@ const { assertRevert } = require('../../helpers/assertRevert'); const ERC20Pausable = artifacts.require('ERC20PausableMock'); -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherAccount, ...otherAccounts]) { beforeEach(async function () { diff --git a/test/token/ERC721/ERC721Pausable.test.js b/test/token/ERC721/ERC721Pausable.test.js index 8940400c29e..0cf0299cea9 100644 --- a/test/token/ERC721/ERC721Pausable.test.js +++ b/test/token/ERC721/ERC721Pausable.test.js @@ -1,6 +1,6 @@ const { shouldBehaveLikeERC721PausedToken } = require('./ERC721PausedToken.behavior'); const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); -const { shouldBehaveLikePublicRole } = require('../../access/rbac/PublicRole.behavior'); +const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); const BigNumber = web3.BigNumber; const ERC721Pausable = artifacts.require('ERC721PausableMock.sol'); From e9cc437c15fa61f11ba386a7af516d58f9af13f6 Mon Sep 17 00:00:00 2001 From: Matt Condon Date: Thu, 6 Sep 2018 16:17:33 -0700 Subject: [PATCH 12/14] feat: implement ERC721Mintable and ERC721Burnable (#1276) * feat: implement ERC721Mintable and ERC721Burnable * fix: linting errors * fix: remove unused mintable mock for ERC721BasicMock * fix: add finishMinting tests * fix: catch MintFinished typo * inline ERC721Full behavior * undo pretty formatting * fix lint errors * rename canMint to onlyBeforeMintingFinished for consistency with ERC20Mintable --- contracts/mocks/ERC721BasicMock.sol | 4 +- .../mocks/ERC721MintableBurnableImpl.sol | 18 ++ contracts/mocks/ERC721Mock.sol | 19 +-- contracts/token/ERC20/ERC20Mintable.sol | 13 +- contracts/token/ERC721/ERC721Burnable.sol | 13 ++ contracts/token/ERC721/ERC721Mintable.sol | 71 ++++++++ test/token/ERC20/ERC20Capped.behavior.js | 2 +- test/token/ERC20/ERC20Mintable.behavior.js | 5 +- test/token/ERC721/ERC721.test.js | 65 +++---- test/token/ERC721/ERC721Basic.behavior.js | 159 +++++++++--------- test/token/ERC721/ERC721Basic.test.js | 8 +- test/token/ERC721/ERC721Burnable.test.js | 22 +++ test/token/ERC721/ERC721MintBurn.behavior.js | 97 ++++++++--- test/token/ERC721/ERC721Mintable.test.js | 24 +++ test/token/ERC721/ERC721Pausable.test.js | 27 +-- 15 files changed, 370 insertions(+), 177 deletions(-) create mode 100644 contracts/mocks/ERC721MintableBurnableImpl.sol create mode 100644 contracts/token/ERC721/ERC721Burnable.sol create mode 100644 contracts/token/ERC721/ERC721Mintable.sol create mode 100644 test/token/ERC721/ERC721Burnable.test.js create mode 100644 test/token/ERC721/ERC721Mintable.test.js diff --git a/contracts/mocks/ERC721BasicMock.sol b/contracts/mocks/ERC721BasicMock.sol index 87add3adb7e..5b8417fd5fb 100644 --- a/contracts/mocks/ERC721BasicMock.sol +++ b/contracts/mocks/ERC721BasicMock.sol @@ -9,10 +9,10 @@ import "../token/ERC721/ERC721Basic.sol"; */ contract ERC721BasicMock is ERC721Basic { function mint(address _to, uint256 _tokenId) public { - super._mint(_to, _tokenId); + _mint(_to, _tokenId); } function burn(uint256 _tokenId) public { - super._burn(ownerOf(_tokenId), _tokenId); + _burn(ownerOf(_tokenId), _tokenId); } } diff --git a/contracts/mocks/ERC721MintableBurnableImpl.sol b/contracts/mocks/ERC721MintableBurnableImpl.sol new file mode 100644 index 00000000000..d0eae1fb353 --- /dev/null +++ b/contracts/mocks/ERC721MintableBurnableImpl.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.24; + +import "../token/ERC721/ERC721.sol"; +import "../token/ERC721/ERC721Mintable.sol"; +import "../token/ERC721/ERC721Burnable.sol"; + + +/** + * @title ERC721MintableBurnableImpl + */ +contract ERC721MintableBurnableImpl is ERC721, ERC721Mintable, ERC721Burnable { + constructor() + ERC721Mintable() + ERC721("Test", "TEST") + public + { + } +} diff --git a/contracts/mocks/ERC721Mock.sol b/contracts/mocks/ERC721Mock.sol index b6c138fd2cd..88ac56a5682 100644 --- a/contracts/mocks/ERC721Mock.sol +++ b/contracts/mocks/ERC721Mock.sol @@ -1,6 +1,8 @@ pragma solidity ^0.4.24; import "../token/ERC721/ERC721.sol"; +import "../token/ERC721/ERC721Mintable.sol"; +import "../token/ERC721/ERC721Burnable.sol"; /** @@ -8,18 +10,11 @@ import "../token/ERC721/ERC721.sol"; * This mock just provides a public mint and burn functions for testing purposes, * and a public setter for metadata URI */ -contract ERC721Mock is ERC721 { - constructor(string name, string symbol) public - ERC721(name, symbol) - { } - - function mint(address _to, uint256 _tokenId) public { - _mint(_to, _tokenId); - } - - function burn(uint256 _tokenId) public { - _burn(ownerOf(_tokenId), _tokenId); - } +contract ERC721Mock is ERC721, ERC721Mintable, ERC721Burnable { + constructor(string _name, string _symbol) public + ERC721Mintable() + ERC721(_name, _symbol) + {} function exists(uint256 _tokenId) public view returns (bool) { return _exists(_tokenId); diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 6de17feb8c6..430a3cdf795 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -5,13 +5,12 @@ import "../../access/roles/MinterRole.sol"; /** - * @title Mintable token - * @dev Simple ERC20 Token example, with mintable token creation - * Based on code by TokenMarketNet: https://github.com/TokenMarketNet/ico/blob/master/contracts/MintableToken.sol + * @title ERC20Mintable + * @dev ERC20 minting logic */ contract ERC20Mintable is ERC20, MinterRole { - event Mint(address indexed to, uint256 amount); - event MintFinished(); + event Minted(address indexed to, uint256 amount); + event MintingFinished(); bool private mintingFinished_ = false; @@ -43,7 +42,7 @@ contract ERC20Mintable is ERC20, MinterRole { returns (bool) { _mint(_to, _amount); - emit Mint(_to, _amount); + emit Minted(_to, _amount); return true; } @@ -58,7 +57,7 @@ contract ERC20Mintable is ERC20, MinterRole { returns (bool) { mintingFinished_ = true; - emit MintFinished(); + emit MintingFinished(); return true; } } diff --git a/contracts/token/ERC721/ERC721Burnable.sol b/contracts/token/ERC721/ERC721Burnable.sol new file mode 100644 index 00000000000..ea42dab0528 --- /dev/null +++ b/contracts/token/ERC721/ERC721Burnable.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.4.24; + +import "./ERC721.sol"; + + +contract ERC721Burnable is ERC721 { + function burn(uint256 _tokenId) + public + { + require(_isApprovedOrOwner(msg.sender, _tokenId)); + _burn(ownerOf(_tokenId), _tokenId); + } +} diff --git a/contracts/token/ERC721/ERC721Mintable.sol b/contracts/token/ERC721/ERC721Mintable.sol new file mode 100644 index 00000000000..0329a92e3f3 --- /dev/null +++ b/contracts/token/ERC721/ERC721Mintable.sol @@ -0,0 +1,71 @@ +pragma solidity ^0.4.24; + +import "./ERC721.sol"; +import "../../access/roles/MinterRole.sol"; + + +/** + * @title ERC721Mintable + * @dev ERC721 minting logic + */ +contract ERC721Mintable is ERC721, MinterRole { + event Minted(address indexed to, uint256 tokenId); + event MintingFinished(); + + bool public mintingFinished = false; + + modifier onlyBeforeMintingFinished() { + require(!mintingFinished); + _; + } + + /** + * @dev Function to mint tokens + * @param _to The address that will receive the minted tokens. + * @param _tokenId The token id to mint. + * @return A boolean that indicates if the operation was successful. + */ + function mint( + address _to, + uint256 _tokenId + ) + public + onlyMinter + onlyBeforeMintingFinished + returns (bool) + { + _mint(_to, _tokenId); + emit Minted(_to, _tokenId); + return true; + } + + function mintWithTokenURI( + address _to, + uint256 _tokenId, + string _tokenURI + ) + public + onlyMinter + onlyBeforeMintingFinished + returns (bool) + { + mint(_to, _tokenId); + _setTokenURI(_tokenId, _tokenURI); + return true; + } + + /** + * @dev Function to stop minting new tokens. + * @return True if the operation was successful. + */ + function finishMinting() + public + onlyMinter + onlyBeforeMintingFinished + returns (bool) + { + mintingFinished = true; + emit MintingFinished(); + return true; + } +} diff --git a/test/token/ERC20/ERC20Capped.behavior.js b/test/token/ERC20/ERC20Capped.behavior.js index 004d512c042..3bec1930256 100644 --- a/test/token/ERC20/ERC20Capped.behavior.js +++ b/test/token/ERC20/ERC20Capped.behavior.js @@ -17,7 +17,7 @@ function shouldBehaveLikeERC20Capped (minter, [anyone], cap) { it('should mint when amount is less than cap', async function () { const { logs } = await this.token.mint(anyone, cap.sub(1), { from }); - expectEvent.inLogs(logs, 'Mint'); + expectEvent.inLogs(logs, 'Minted'); }); it('should fail to mint if the ammount exceeds the cap', async function () { diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index 0fb68e41a23..debca80975a 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -43,8 +43,7 @@ function shouldBehaveLikeERC20Mintable (minter, [anyone]) { it('emits a mint finished event', async function () { const { logs } = await this.token.finishMinting({ from }); - logs.length.should.be.equal(1); - logs[0].event.should.equal('MintFinished'); + await expectEvent.inLogs(logs, 'MintingFinished'); }); }); @@ -105,7 +104,7 @@ function shouldBehaveLikeERC20Mintable (minter, [anyone]) { }); it('emits a mint and a transfer event', async function () { - const mintEvent = expectEvent.inLogs(this.logs, 'Mint', { + const mintEvent = expectEvent.inLogs(this.logs, 'Minted', { to: anyone, }); mintEvent.args.amount.should.be.bignumber.equal(amount); diff --git a/test/token/ERC721/ERC721.test.js b/test/token/ERC721/ERC721.test.js index 5469c87cf28..081d8e86f9e 100644 --- a/test/token/ERC721/ERC721.test.js +++ b/test/token/ERC721/ERC721.test.js @@ -11,55 +11,57 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721', function (accounts) { +contract('ERC721', function ([ + creator, + ...accounts +]) { const name = 'Non Fungible Token'; const symbol = 'NFT'; const firstTokenId = 100; const secondTokenId = 200; + const thirdTokenId = 300; const nonExistentTokenId = 999; - const creator = accounts[0]; - const anyone = accounts[9]; + + const minter = creator; + + const [ + owner, + newOwner, + another, + anyone, + ] = accounts; beforeEach(async function () { this.token = await ERC721.new(name, symbol, { from: creator }); }); - shouldBehaveLikeERC721Basic(accounts); - shouldBehaveLikeMintAndBurnERC721(accounts); - describe('like a full ERC721', function () { beforeEach(async function () { - await this.token.mint(creator, firstTokenId, { from: creator }); - await this.token.mint(creator, secondTokenId, { from: creator }); + await this.token.mint(owner, firstTokenId, { from: minter }); + await this.token.mint(owner, secondTokenId, { from: minter }); }); describe('mint', function () { - const to = accounts[1]; - const tokenId = 3; - beforeEach(async function () { - await this.token.mint(to, tokenId); + await this.token.mint(newOwner, thirdTokenId, { from: minter }); }); it('adjusts owner tokens by index', async function () { - (await this.token.tokenOfOwnerByIndex(to, 0)).toNumber().should.be.equal(tokenId); + (await this.token.tokenOfOwnerByIndex(newOwner, 0)).toNumber().should.be.equal(thirdTokenId); }); it('adjusts all tokens list', async function () { - (await this.token.tokenByIndex(2)).toNumber().should.be.equal(tokenId); + (await this.token.tokenByIndex(2)).toNumber().should.be.equal(thirdTokenId); }); }); describe('burn', function () { - const tokenId = firstTokenId; - const sender = creator; - beforeEach(async function () { - await this.token.burn(tokenId, { from: sender }); + await this.token.burn(firstTokenId, { from: owner }); }); it('removes that token from the token list of the owner', async function () { - (await this.token.tokenOfOwnerByIndex(sender, 0)).toNumber().should.be.equal(secondTokenId); + (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.be.equal(secondTokenId); }); it('adjusts all tokens list', async function () { @@ -67,7 +69,7 @@ contract('ERC721', function (accounts) { }); it('burns all tokens', async function () { - await this.token.burn(secondTokenId, { from: sender }); + await this.token.burn(secondTokenId, { from: owner }); (await this.token.totalSupply()).toNumber().should.be.equal(0); await assertRevert(this.token.tokenByIndex(0)); }); @@ -76,25 +78,25 @@ contract('ERC721', function (accounts) { describe('removeTokenFrom', function () { it('reverts if the correct owner is not passed', async function () { await assertRevert( - this.token.removeTokenFrom(anyone, firstTokenId, { from: creator }) + this.token.removeTokenFrom(anyone, firstTokenId, { from: owner }) ); }); context('once removed', function () { beforeEach(async function () { - await this.token.removeTokenFrom(creator, firstTokenId, { from: creator }); + await this.token.removeTokenFrom(owner, firstTokenId, { from: owner }); }); it('has been removed', async function () { - await assertRevert(this.token.tokenOfOwnerByIndex(creator, 1)); + await assertRevert(this.token.tokenOfOwnerByIndex(owner, 1)); }); it('adjusts token list', async function () { - (await this.token.tokenOfOwnerByIndex(creator, 0)).toNumber().should.be.equal(secondTokenId); + (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.be.equal(secondTokenId); }); it('adjusts owner count', async function () { - (await this.token.balanceOf(creator)).toNumber().should.be.equal(1); + (await this.token.balanceOf(owner)).toNumber().should.be.equal(1); }); it('does not adjust supply', async function () { @@ -125,7 +127,7 @@ contract('ERC721', function (accounts) { it('can burn token with metadata', async function () { await this.token.setTokenURI(firstTokenId, sampleUri); - await this.token.burn(firstTokenId); + await this.token.burn(firstTokenId, { from: owner }); (await this.token.exists(firstTokenId)).should.equal(false); }); @@ -145,9 +147,6 @@ contract('ERC721', function (accounts) { }); describe('tokenOfOwnerByIndex', function () { - const owner = creator; - const another = accounts[1]; - describe('when the given index is lower than the amount of tokens owned by the given address', function () { it('returns the token ID placed at the given index', async function () { (await this.token.tokenOfOwnerByIndex(owner, 0)).should.be.bignumber.equal(firstTokenId); @@ -197,13 +196,12 @@ contract('ERC721', function (accounts) { [firstTokenId, secondTokenId].forEach(function (tokenId) { it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () { - const owner = accounts[0]; const newTokenId = 300; const anotherNewTokenId = 400; await this.token.burn(tokenId, { from: owner }); - await this.token.mint(owner, newTokenId, { from: owner }); - await this.token.mint(owner, anotherNewTokenId, { from: owner }); + await this.token.mint(newOwner, newTokenId, { from: minter }); + await this.token.mint(newOwner, anotherNewTokenId, { from: minter }); (await this.token.totalSupply()).toNumber().should.be.equal(3); @@ -218,6 +216,9 @@ contract('ERC721', function (accounts) { }); }); + shouldBehaveLikeERC721Basic(creator, minter, accounts); + shouldBehaveLikeMintAndBurnERC721(creator, minter, accounts); + shouldSupportInterfaces([ 'ERC165', 'ERC721', diff --git a/test/token/ERC721/ERC721Basic.behavior.js b/test/token/ERC721/ERC721Basic.behavior.js index 765614afce7..b3be95ed020 100644 --- a/test/token/ERC721/ERC721Basic.behavior.js +++ b/test/token/ERC721/ERC721Basic.behavior.js @@ -11,30 +11,34 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeERC721Basic (accounts) { +function shouldBehaveLikeERC721Basic ( + creator, + minter, + [owner, approved, anotherApproved, operator, anyone] +) { const firstTokenId = 1; const secondTokenId = 2; const unknownTokenId = 3; - const creator = accounts[0]; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const RECEIVER_MAGIC_VALUE = '0x150b7a02'; describe('like an ERC721Basic', function () { beforeEach(async function () { - await this.token.mint(creator, firstTokenId, { from: creator }); - await this.token.mint(creator, secondTokenId, { from: creator }); + await this.token.mint(owner, firstTokenId, { from: minter }); + await this.token.mint(owner, secondTokenId, { from: minter }); + this.toWhom = anyone; // default to anyone for toWhom in context-dependent tests }); describe('balanceOf', function () { context('when the given address owns some tokens', function () { it('returns the amount of tokens owned by the given address', async function () { - (await this.token.balanceOf(creator)).should.be.bignumber.equal(2); + (await this.token.balanceOf(owner)).should.be.bignumber.equal(2); }); }); context('when the given address does not own any tokens', function () { it('returns 0', async function () { - (await this.token.balanceOf(accounts[1])).should.be.bignumber.equal(0); + (await this.token.balanceOf(anyone)).should.be.bignumber.equal(0); }); }); @@ -50,7 +54,7 @@ function shouldBehaveLikeERC721Basic (accounts) { const tokenId = firstTokenId; it('returns the owner of the given token ID', async function () { - (await this.token.ownerOf(tokenId)).should.be.equal(creator); + (await this.token.ownerOf(tokenId)).should.be.equal(owner); }); }); @@ -64,24 +68,19 @@ function shouldBehaveLikeERC721Basic (accounts) { }); describe('transfers', function () { - const owner = accounts[0]; - const approved = accounts[2]; - const operator = accounts[3]; - const unauthorized = accounts[4]; const tokenId = firstTokenId; const data = '0x42'; let logs = null; beforeEach(async function () { - this.to = accounts[1]; await this.token.approve(approved, tokenId, { from: owner }); await this.token.setApprovalForAll(operator, true, { from: owner }); }); const transferWasSuccessful = function ({ owner, tokenId, approved }) { it('transfers the ownership of the given token ID to the given address', async function () { - (await this.token.ownerOf(tokenId)).should.be.equal(this.to); + (await this.token.ownerOf(tokenId)).should.be.equal(this.toWhom); }); it('clears the approval for the token ID', async function () { @@ -93,7 +92,7 @@ function shouldBehaveLikeERC721Basic (accounts) { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); logs[0].args.from.should.be.equal(owner); - logs[0].args.to.should.be.equal(this.to); + logs[0].args.to.should.be.equal(this.toWhom); logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); } else { @@ -101,21 +100,19 @@ function shouldBehaveLikeERC721Basic (accounts) { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); logs[0].args.from.should.be.equal(owner); - logs[0].args.to.should.be.equal(this.to); + logs[0].args.to.should.be.equal(this.toWhom); logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); } it('adjusts owners balances', async function () { - (await this.token.balanceOf(this.to)).should.be.bignumber.equal(1); - (await this.token.balanceOf(owner)).should.be.bignumber.equal(1); }); it('adjusts owners tokens by index', async function () { if (!this.token.tokenOfOwnerByIndex) return; - (await this.token.tokenOfOwnerByIndex(this.to, 0)).toNumber().should.be.equal(tokenId); + (await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).toNumber().should.be.equal(tokenId); (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.not.be.equal(tokenId); }); @@ -124,21 +121,21 @@ function shouldBehaveLikeERC721Basic (accounts) { const shouldTransferTokensByUsers = function (transferFunction) { context('when called by the owner', function () { beforeEach(async function () { - ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: owner })); + ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner })); }); transferWasSuccessful({ owner, tokenId, approved }); }); context('when called by the approved individual', function () { beforeEach(async function () { - ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: approved })); + ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved })); }); transferWasSuccessful({ owner, tokenId, approved }); }); context('when called by the operator', function () { beforeEach(async function () { - ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator })); + ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator })); }); transferWasSuccessful({ owner, tokenId, approved }); }); @@ -146,7 +143,7 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when called by the owner without an approved user', function () { beforeEach(async function () { await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }); - ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator })); + ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator })); }); transferWasSuccessful({ owner, tokenId, approved: null }); }); @@ -185,19 +182,22 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when the address of the previous owner is incorrect', function () { it('reverts', async function () { - await assertRevert(transferFunction.call(this, unauthorized, this.to, tokenId, { from: owner })); + await assertRevert(transferFunction.call(this, anyone, anyone, tokenId, { from: owner }) + ); }); }); context('when the sender is not authorized for the token id', function () { it('reverts', async function () { - await assertRevert(transferFunction.call(this, owner, this.to, tokenId, { from: unauthorized })); + await assertRevert(transferFunction.call(this, owner, anyone, tokenId, { from: anyone }) + ); }); }); context('when the given token ID does not exist', function () { it('reverts', async function () { - await assertRevert(transferFunction.call(this, owner, this.to, unknownTokenId, { from: owner })); + await assertRevert(transferFunction.call(this, owner, anyone, unknownTokenId, { from: owner }) + ); }); }); @@ -237,13 +237,13 @@ function shouldBehaveLikeERC721Basic (accounts) { describe('to a valid receiver contract', function () { beforeEach(async function () { this.receiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false); - this.to = this.receiver.address; + this.toWhom = this.receiver.address; }); shouldTransferTokensByUsers(transferFun); it('should call onERC721Received', async function () { - const result = await transferFun.call(this, owner, this.to, tokenId, { from: owner }); + const result = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner }); result.receipt.logs.length.should.be.equal(2); const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address); log.event.should.be.equal('Received'); @@ -254,9 +254,15 @@ function shouldBehaveLikeERC721Basic (accounts) { }); it('should call onERC721Received from approved', async function () { - const result = await transferFun.call(this, owner, this.to, tokenId, { from: approved }); + const result = await transferFun.call(this, owner, this.receiver.address, tokenId, { + from: approved, + }); result.receipt.logs.length.should.be.equal(2); - const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address); + const [log] = decodeLogs( + [result.receipt.logs[1]], + ERC721Receiver, + this.receiver.address + ); log.event.should.be.equal('Received'); log.args.operator.should.be.equal(approved); log.args.from.should.be.equal(owner); @@ -270,7 +276,7 @@ function shouldBehaveLikeERC721Basic (accounts) { transferFun.call( this, owner, - this.to, + this.receiver.address, unknownTokenId, { from: owner }, ) @@ -313,8 +319,6 @@ function shouldBehaveLikeERC721Basic (accounts) { describe('approve', function () { const tokenId = firstTokenId; - const sender = creator; - const to = accounts[1]; let logs = null; @@ -334,7 +338,7 @@ function shouldBehaveLikeERC721Basic (accounts) { it('emits an approval event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Approval'); - logs[0].args.owner.should.be.equal(sender); + logs[0].args.owner.should.be.equal(owner); logs[0].args.approved.should.be.equal(address); logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); @@ -343,7 +347,7 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when clearing approval', function () { context('when there was no prior approval', function () { beforeEach(async function () { - ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender })); + ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner })); }); itClearsApproval(); @@ -352,8 +356,8 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when there was a prior approval', function () { beforeEach(async function () { - await this.token.approve(to, tokenId, { from: sender }); - ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender })); + await this.token.approve(approved, tokenId, { from: owner }); + ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner })); }); itClearsApproval(); @@ -364,90 +368,87 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when approving a non-zero address', function () { context('when there was no prior approval', function () { beforeEach(async function () { - ({ logs } = await this.token.approve(to, tokenId, { from: sender })); + ({ logs } = await this.token.approve(approved, tokenId, { from: owner })); }); - itApproves(to); - itEmitsApprovalEvent(to); + itApproves(approved); + itEmitsApprovalEvent(approved); }); context('when there was a prior approval to the same address', function () { beforeEach(async function () { - await this.token.approve(to, tokenId, { from: sender }); - ({ logs } = await this.token.approve(to, tokenId, { from: sender })); + await this.token.approve(approved, tokenId, { from: owner }); + ({ logs } = await this.token.approve(approved, tokenId, { from: owner })); }); - itApproves(to); - itEmitsApprovalEvent(to); + itApproves(approved); + itEmitsApprovalEvent(approved); }); context('when there was a prior approval to a different address', function () { beforeEach(async function () { - await this.token.approve(accounts[2], tokenId, { from: sender }); - ({ logs } = await this.token.approve(to, tokenId, { from: sender })); + await this.token.approve(anotherApproved, tokenId, { from: owner }); + ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner })); }); - itApproves(to); - itEmitsApprovalEvent(to); + itApproves(anotherApproved); + itEmitsApprovalEvent(anotherApproved); }); }); context('when the address that receives the approval is the owner', function () { it('reverts', async function () { - await assertRevert(this.token.approve(sender, tokenId, { from: sender })); + await assertRevert( + this.token.approve(owner, tokenId, { from: owner }) + ); }); }); context('when the sender does not own the given token ID', function () { it('reverts', async function () { - await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] })); + await assertRevert(this.token.approve(approved, tokenId, { from: anyone })); }); }); context('when the sender is approved for the given token ID', function () { it('reverts', async function () { - await this.token.approve(accounts[2], tokenId, { from: sender }); - await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] })); + await this.token.approve(approved, tokenId, { from: owner }); + await assertRevert(this.token.approve(anotherApproved, tokenId, { from: approved })); }); }); context('when the sender is an operator', function () { - const operator = accounts[2]; beforeEach(async function () { - await this.token.setApprovalForAll(operator, true, { from: sender }); - ({ logs } = await this.token.approve(to, tokenId, { from: operator })); + await this.token.setApprovalForAll(operator, true, { from: owner }); + ({ logs } = await this.token.approve(approved, tokenId, { from: operator })); }); - itApproves(to); - itEmitsApprovalEvent(to); + itApproves(approved); + itEmitsApprovalEvent(approved); }); context('when the given token ID does not exist', function () { it('reverts', async function () { - await assertRevert(this.token.approve(to, unknownTokenId, { from: sender })); + await assertRevert(this.token.approve(approved, unknownTokenId, { from: operator })); }); }); }); describe('setApprovalForAll', function () { - const sender = creator; - context('when the operator willing to approve is not the owner', function () { - const operator = accounts[1]; - context('when there is no operator approval set by the sender', function () { it('approves the operator', async function () { - await this.token.setApprovalForAll(operator, true, { from: sender }); + await this.token.setApprovalForAll(operator, true, { from: owner }); - (await this.token.isApprovedForAll(sender, operator)).should.equal(true); + (await this.token.isApprovedForAll(owner, operator)).should.equal(true); }); it('emits an approval event', async function () { - const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender }); + const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner }); logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args.owner.should.be.equal(sender); + logs[0].args.owner.should.be.equal(owner); logs[0].args.operator.should.be.equal(operator); logs[0].args.approved.should.equal(true); }); @@ -455,49 +456,49 @@ function shouldBehaveLikeERC721Basic (accounts) { context('when the operator was set as not approved', function () { beforeEach(async function () { - await this.token.setApprovalForAll(operator, false, { from: sender }); + await this.token.setApprovalForAll(operator, false, { from: owner }); }); it('approves the operator', async function () { - await this.token.setApprovalForAll(operator, true, { from: sender }); + await this.token.setApprovalForAll(operator, true, { from: owner }); - (await this.token.isApprovedForAll(sender, operator)).should.equal(true); + (await this.token.isApprovedForAll(owner, operator)).should.equal(true); }); it('emits an approval event', async function () { - const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender }); + const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner }); logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args.owner.should.be.equal(sender); + logs[0].args.owner.should.be.equal(owner); logs[0].args.operator.should.be.equal(operator); logs[0].args.approved.should.equal(true); }); it('can unset the operator approval', async function () { - await this.token.setApprovalForAll(operator, false, { from: sender }); + await this.token.setApprovalForAll(operator, false, { from: owner }); - (await this.token.isApprovedForAll(sender, operator)).should.equal(false); + (await this.token.isApprovedForAll(owner, operator)).should.equal(false); }); }); context('when the operator was already approved', function () { beforeEach(async function () { - await this.token.setApprovalForAll(operator, true, { from: sender }); + await this.token.setApprovalForAll(operator, true, { from: owner }); }); it('keeps the approval to the given address', async function () { - await this.token.setApprovalForAll(operator, true, { from: sender }); + await this.token.setApprovalForAll(operator, true, { from: owner }); - (await this.token.isApprovedForAll(sender, operator)).should.equal(true); + (await this.token.isApprovedForAll(owner, operator)).should.equal(true); }); it('emits an approval event', async function () { - const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender }); + const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner }); logs.length.should.be.equal(1); logs[0].event.should.be.equal('ApprovalForAll'); - logs[0].args.owner.should.be.equal(sender); + logs[0].args.owner.should.be.equal(owner); logs[0].args.operator.should.be.equal(operator); logs[0].args.approved.should.equal(true); }); @@ -505,10 +506,8 @@ function shouldBehaveLikeERC721Basic (accounts) { }); context('when the operator is the owner', function () { - const operator = creator; - it('reverts', async function () { - await assertRevert(this.token.setApprovalForAll(operator, true, { from: sender })); + await assertRevert(this.token.setApprovalForAll(owner, true, { from: owner })); }); }); }); diff --git a/test/token/ERC721/ERC721Basic.test.js b/test/token/ERC721/ERC721Basic.test.js index f86a97f5b68..708523ed228 100644 --- a/test/token/ERC721/ERC721Basic.test.js +++ b/test/token/ERC721/ERC721Basic.test.js @@ -1,5 +1,4 @@ const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); -const { shouldBehaveLikeMintAndBurnERC721 } = require('./ERC721MintBurn.behavior'); const BigNumber = web3.BigNumber; const ERC721Basic = artifacts.require('ERC721BasicMock.sol'); @@ -8,11 +7,10 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721Basic', function (accounts) { +contract('ERC721Basic', function ([_, creator, ...accounts]) { beforeEach(async function () { - this.token = await ERC721Basic.new({ from: accounts[0] }); + this.token = await ERC721Basic.new({ from: creator }); }); - shouldBehaveLikeERC721Basic(accounts); - shouldBehaveLikeMintAndBurnERC721(accounts); + shouldBehaveLikeERC721Basic(creator, creator, accounts); }); diff --git a/test/token/ERC721/ERC721Burnable.test.js b/test/token/ERC721/ERC721Burnable.test.js new file mode 100644 index 00000000000..34605028ddb --- /dev/null +++ b/test/token/ERC721/ERC721Burnable.test.js @@ -0,0 +1,22 @@ +const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); +const { + shouldBehaveLikeMintAndBurnERC721, +} = require('./ERC721MintBurn.behavior'); + +const BigNumber = web3.BigNumber; +const ERC721Burnable = artifacts.require('ERC721MintableBurnableImpl.sol'); + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('ERC721Burnable', function ([_, creator, ...accounts]) { + const minter = creator; + + beforeEach(async function () { + this.token = await ERC721Burnable.new({ from: creator }); + }); + + shouldBehaveLikeERC721Basic(creator, minter, accounts); + shouldBehaveLikeMintAndBurnERC721(creator, minter, accounts); +}); diff --git a/test/token/ERC721/ERC721MintBurn.behavior.js b/test/token/ERC721/ERC721MintBurn.behavior.js index c165f170356..2b22937d7e4 100644 --- a/test/token/ERC721/ERC721MintBurn.behavior.js +++ b/test/token/ERC721/ERC721MintBurn.behavior.js @@ -1,84 +1,100 @@ const { assertRevert } = require('../../helpers/assertRevert'); +const expectEvent = require('../../helpers/expectEvent'); const BigNumber = web3.BigNumber; require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -function shouldBehaveLikeMintAndBurnERC721 (accounts) { +function shouldBehaveLikeMintAndBurnERC721 ( + creator, + minter, + [owner, newOwner, approved, anyone] +) { const firstTokenId = 1; const secondTokenId = 2; - const unknownTokenId = 3; - const creator = accounts[0]; + const thirdTokenId = 3; + const unknownTokenId = 4; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + const MOCK_URI = 'https://example.com'; describe('like a mintable and burnable ERC721', function () { beforeEach(async function () { - await this.token.mint(creator, firstTokenId, { from: creator }); - await this.token.mint(creator, secondTokenId, { from: creator }); + await this.token.mint(owner, firstTokenId, { from: minter }); + await this.token.mint(owner, secondTokenId, { from: minter }); }); describe('mint', function () { - const to = accounts[1]; - const tokenId = unknownTokenId; let logs = null; describe('when successful', function () { beforeEach(async function () { - const result = await this.token.mint(to, tokenId); + const result = await this.token.mint(newOwner, thirdTokenId, { from: minter }); logs = result.logs; }); it('assigns the token to the new owner', async function () { - (await this.token.ownerOf(tokenId)).should.be.equal(to); + (await this.token.ownerOf(thirdTokenId)).should.be.equal(newOwner); }); it('increases the balance of its owner', async function () { - (await this.token.balanceOf(to)).should.be.bignumber.equal(1); + (await this.token.balanceOf(newOwner)).should.be.bignumber.equal(1); }); - it('emits a transfer event', async function () { - logs.length.should.be.equal(1); - logs[0].event.should.be.equal('Transfer'); - logs[0].args.from.should.be.equal(ZERO_ADDRESS); - logs[0].args.to.should.be.equal(to); - logs[0].args.tokenId.should.be.bignumber.equal(tokenId); + it('emits a transfer and minted event', async function () { + await expectEvent.inLogs(logs, 'Transfer', { + from: ZERO_ADDRESS, + to: newOwner, + }); + logs[0].args.tokenId.should.be.bignumber.equal(thirdTokenId); + + await expectEvent.inLogs(logs, 'Minted', { + to: newOwner, + }); + logs[1].args.tokenId.should.be.bignumber.equal(thirdTokenId); }); }); describe('when the given owner address is the zero address', function () { it('reverts', async function () { - await assertRevert(this.token.mint(ZERO_ADDRESS, tokenId)); + await assertRevert(this.token.mint(ZERO_ADDRESS, thirdTokenId)); }); }); describe('when the given token ID was already tracked by this contract', function () { it('reverts', async function () { - await assertRevert(this.token.mint(accounts[1], firstTokenId)); + await assertRevert(this.token.mint(owner, firstTokenId)); + }); + }); + }); + + describe('mintWithTokenURI', function () { + it('can mint with a tokenUri', async function () { + await this.token.mintWithTokenURI(newOwner, thirdTokenId, MOCK_URI, { + from: minter, }); }); }); describe('burn', function () { const tokenId = firstTokenId; - const sender = creator; let logs = null; describe('when successful', function () { beforeEach(async function () { - const result = await this.token.burn(tokenId, { from: sender }); + const result = await this.token.burn(tokenId, { from: owner }); logs = result.logs; }); it('burns the given token ID and adjusts the balance of the owner', async function () { await assertRevert(this.token.ownerOf(tokenId)); - (await this.token.balanceOf(sender)).should.be.bignumber.equal(1); + (await this.token.balanceOf(owner)).should.be.bignumber.equal(1); }); it('emits a burn event', async function () { logs.length.should.be.equal(1); logs[0].event.should.be.equal('Transfer'); - logs[0].args.from.should.be.equal(sender); + logs[0].args.from.should.be.equal(owner); logs[0].args.to.should.be.equal(ZERO_ADDRESS); logs[0].args.tokenId.should.be.bignumber.equal(tokenId); }); @@ -86,8 +102,8 @@ function shouldBehaveLikeMintAndBurnERC721 (accounts) { describe('when there is a previous approval', function () { beforeEach(async function () { - await this.token.approve(accounts[1], tokenId, { from: sender }); - const result = await this.token.burn(tokenId, { from: sender }); + await this.token.approve(approved, tokenId, { from: owner }); + const result = await this.token.burn(tokenId, { from: owner }); logs = result.logs; }); @@ -98,7 +114,38 @@ function shouldBehaveLikeMintAndBurnERC721 (accounts) { describe('when the given token ID was not tracked by this contract', function () { it('reverts', async function () { - await assertRevert(this.token.burn(unknownTokenId, { from: creator })); + await assertRevert( + this.token.burn(unknownTokenId, { from: creator }) + ); + }); + }); + }); + + describe('finishMinting', function () { + it('allows the minter to finish minting', async function () { + const { logs } = await this.token.finishMinting({ from: minter }); + expectEvent.inLogs(logs, 'MintingFinished'); + }); + }); + + context('mintingFinished', function () { + beforeEach(async function () { + await this.token.finishMinting({ from: minter }); + }); + + describe('mint', function () { + it('reverts', async function () { + await assertRevert( + this.token.mint(owner, thirdTokenId, { from: minter }) + ); + }); + }); + + describe('mintWithTokenURI', function () { + it('reverts', async function () { + await assertRevert( + this.token.mintWithTokenURI(owner, thirdTokenId, MOCK_URI, { from: minter }) + ); }); }); }); diff --git a/test/token/ERC721/ERC721Mintable.test.js b/test/token/ERC721/ERC721Mintable.test.js new file mode 100644 index 00000000000..b53ddb59301 --- /dev/null +++ b/test/token/ERC721/ERC721Mintable.test.js @@ -0,0 +1,24 @@ +const { shouldBehaveLikeERC721Basic } = require('./ERC721Basic.behavior'); +const { + shouldBehaveLikeMintAndBurnERC721, +} = require('./ERC721MintBurn.behavior'); + +const BigNumber = web3.BigNumber; +const ERC721Mintable = artifacts.require('ERC721MintableBurnableImpl.sol'); + +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('ERC721Mintable', function ([_, creator, ...accounts]) { + const minter = creator; + + beforeEach(async function () { + this.token = await ERC721Mintable.new({ + from: creator, + }); + }); + + shouldBehaveLikeERC721Basic(creator, minter, accounts); + shouldBehaveLikeMintAndBurnERC721(creator, minter, accounts); +}); diff --git a/test/token/ERC721/ERC721Pausable.test.js b/test/token/ERC721/ERC721Pausable.test.js index 0cf0299cea9..240e478e8a3 100644 --- a/test/token/ERC721/ERC721Pausable.test.js +++ b/test/token/ERC721/ERC721Pausable.test.js @@ -9,38 +9,45 @@ require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); -contract('ERC721Pausable', function ([_, pauser, otherPauser, recipient, operator, ...otherAccounts]) { +contract('ERC721Pausable', function ([ + _, + creator, + owner, + operator, + otherPauser, + ...accounts +]) { beforeEach(async function () { - this.token = await ERC721Pausable.new({ from: pauser }); + this.token = await ERC721Pausable.new({ from: creator }); }); describe('pauser role', function () { beforeEach(async function () { this.contract = this.token; - await this.contract.addPauser(otherPauser, { from: pauser }); + await this.contract.addPauser(otherPauser, { from: creator }); }); - shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser'); + shouldBehaveLikePublicRole(creator, otherPauser, accounts, 'pauser'); }); context('when token is paused', function () { beforeEach(async function () { - await this.token.pause({ from: pauser }); + await this.token.pause({ from: creator }); }); - shouldBehaveLikeERC721PausedToken(pauser, [...otherAccounts]); + shouldBehaveLikeERC721PausedToken(creator, accounts); }); context('when token is not paused yet', function () { - shouldBehaveLikeERC721Basic([pauser, ...otherAccounts]); + shouldBehaveLikeERC721Basic(creator, creator, accounts); }); context('when token is paused and then unpaused', function () { beforeEach(async function () { - await this.token.pause({ from: pauser }); - await this.token.unpause({ from: pauser }); + await this.token.pause({ from: creator }); + await this.token.unpause({ from: creator }); }); - shouldBehaveLikeERC721Basic([pauser, ...otherAccounts]); + shouldBehaveLikeERC721Basic(creator, creator, accounts); }); }); From c81e75de9fef7437b4780f41d06cbd16fb6de1b2 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Fri, 7 Sep 2018 05:27:18 +0000 Subject: [PATCH 13/14] Fix the merge with the privatization branch --- contracts/token/ERC20/ERC20Detailed.sol | 31 ++++++++++++++++++----- contracts/token/ERC721/ERC721Mintable.sol | 13 +++++++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/contracts/token/ERC20/ERC20Detailed.sol b/contracts/token/ERC20/ERC20Detailed.sol index ad6f1dd059a..b6d50fca2f8 100644 --- a/contracts/token/ERC20/ERC20Detailed.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -10,13 +10,32 @@ import "./IERC20.sol"; * just as on Ethereum all the operations are done in wei. */ contract ERC20Detailed is IERC20 { - string public name; - string public symbol; - uint8 public decimals; + string private name_; + string private symbol_; + uint8 private decimals_; constructor(string _name, string _symbol, uint8 _decimals) public { - name = _name; - symbol = _symbol; - decimals = _decimals; + name_ = _name; + symbol_ = _symbol; + decimals_ = _decimals; + } + + /** + * @return the name of the token. + */ + function name() public view returns(string) { + return name_; + } + /** + * @return the symbol of the token. + */ + function symbol() public view returns(string) { + return symbol_; + } + /** + * @return the number of decimals of the token. + */ + function decimals() public view returns(uint8) { + return decimals_; } } diff --git a/contracts/token/ERC721/ERC721Mintable.sol b/contracts/token/ERC721/ERC721Mintable.sol index 0329a92e3f3..adee3595200 100644 --- a/contracts/token/ERC721/ERC721Mintable.sol +++ b/contracts/token/ERC721/ERC721Mintable.sol @@ -12,13 +12,20 @@ contract ERC721Mintable is ERC721, MinterRole { event Minted(address indexed to, uint256 tokenId); event MintingFinished(); - bool public mintingFinished = false; + bool private mintingFinished_ = false; modifier onlyBeforeMintingFinished() { - require(!mintingFinished); + require(!mintingFinished_); _; } + /** + * @return true if the minting is finished. + */ + function mintingFinished() public view returns(bool) { + return mintingFinished_; + } + /** * @dev Function to mint tokens * @param _to The address that will receive the minted tokens. @@ -64,7 +71,7 @@ contract ERC721Mintable is ERC721, MinterRole { onlyBeforeMintingFinished returns (bool) { - mintingFinished = true; + mintingFinished_ = true; emit MintingFinished(); return true; } From f6306b8db739dd13c6520b84d1404a62279b882d Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 7 Sep 2018 02:39:25 -0300 Subject: [PATCH 14/14] remove duplicate CapperRole test --- test/access/rbac/CapperRole.test.js | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 test/access/rbac/CapperRole.test.js diff --git a/test/access/rbac/CapperRole.test.js b/test/access/rbac/CapperRole.test.js deleted file mode 100644 index a79944a2778..00000000000 --- a/test/access/rbac/CapperRole.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior'); -const CapperRoleMock = artifacts.require('CapperRoleMock'); - -contract('CapperRole', function ([_, capper, otherCapper, ...otherAccounts]) { - beforeEach(async function () { - this.contract = await CapperRoleMock.new({ from: capper }); - await this.contract.addCapper(otherCapper, { from: capper }); - }); - - shouldBehaveLikePublicRole(capper, otherCapper, otherAccounts, 'capper'); -});