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/67] 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/67] 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/67] 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/67] 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 128c4c0dda8d50bfa5ade266f29a0ed00300b280 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 05:41:34 +0000 Subject: [PATCH 05/67] Improve encapsulation on ERC165 --- contracts/introspection/SupportsInterfaceWithLookup.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/introspection/SupportsInterfaceWithLookup.sol b/contracts/introspection/SupportsInterfaceWithLookup.sol index c2b009aea0f..6fca22ac8ca 100644 --- a/contracts/introspection/SupportsInterfaceWithLookup.sol +++ b/contracts/introspection/SupportsInterfaceWithLookup.sol @@ -10,7 +10,7 @@ import "./IERC165.sol"; */ contract SupportsInterfaceWithLookup is IERC165 { - bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7; + bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7; /** * 0x01ffc9a7 === * bytes4(keccak256('supportsInterface(bytes4)')) From 0d8a5e4ab76bc1e7b16e0e026caacb2a6bc43b0c Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 05:44:20 +0000 Subject: [PATCH 06/67] Improve encapsulation on ERC20 --- contracts/token/ERC20/ERC20Capped.sol | 13 ++- contracts/token/ERC20/ERC20Detailed.sol | 33 ++++++-- contracts/token/ERC20/ERC20Mintable.sol | 13 ++- contracts/token/ERC20/TokenTimelock.sol | 39 ++++++--- contracts/token/ERC20/TokenVesting.sol | 93 +++++++++++++++++----- test/token/ERC20/DetailedERC20.test.js | 6 +- test/token/ERC20/ERC20Capped.behavior.js | 2 +- test/token/ERC20/ERC20Mintable.behavior.js | 6 +- 8 files changed, 155 insertions(+), 50 deletions(-) diff --git a/contracts/token/ERC20/ERC20Capped.sol b/contracts/token/ERC20/ERC20Capped.sol index 3af03f167cd..13f5dabdcbf 100644 --- a/contracts/token/ERC20/ERC20Capped.sol +++ b/contracts/token/ERC20/ERC20Capped.sol @@ -9,11 +9,18 @@ import "./ERC20Mintable.sol"; */ contract ERC20Capped is ERC20Mintable { - uint256 public cap; + uint256 private cap_; constructor(uint256 _cap) public { require(_cap > 0); - cap = _cap; + cap_ = _cap; + } + + /** + * @return the cap for the token minting. + */ + function getCap() public view returns(uint256) { + return cap_; } /** @@ -29,7 +36,7 @@ contract ERC20Capped is ERC20Mintable { public returns (bool) { - require(totalSupply().add(_amount) <= cap); + require(totalSupply().add(_amount) <= cap_); return super.mint(_to, _amount); } diff --git a/contracts/token/ERC20/ERC20Detailed.sol b/contracts/token/ERC20/ERC20Detailed.sol index ad6f1dd059a..aefa3947184 100644 --- a/contracts/token/ERC20/ERC20Detailed.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -10,13 +10,34 @@ 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 getName() public view returns(string) { + return name_; + } + + /** + * @return the symbol of the token. + */ + function getSymbol() public view returns(string) { + return symbol_; + } + + /** + * @return the number of decimals of the token. + */ + function getDecimals() public view returns(uint8) { + return decimals_; } } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 0ba200cdfac..6c329317a58 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -13,11 +13,11 @@ contract ERC20Mintable is ERC20, Ownable { event Mint(address indexed to, uint256 amount); event MintFinished(); - bool public mintingFinished = false; + bool private mintingFinished_ = false; modifier canMint() { - require(!mintingFinished); + require(!mintingFinished_); _; } @@ -26,6 +26,13 @@ contract ERC20Mintable is ERC20, Ownable { _; } + /** + * @return true if the minting is finished. + */ + function isMintingFinished() public view returns(bool) { + return mintingFinished_; + } + /** * @dev Function to mint tokens * @param _to The address that will receive the minted tokens. @@ -51,7 +58,7 @@ contract ERC20Mintable is ERC20, Ownable { * @return True if the operation was successful. */ function finishMinting() public onlyOwner canMint returns (bool) { - mintingFinished = true; + mintingFinished_ = true; emit MintFinished(); return true; } diff --git a/contracts/token/ERC20/TokenTimelock.sol b/contracts/token/ERC20/TokenTimelock.sol index a24642f6de5..0ed6341a71c 100644 --- a/contracts/token/ERC20/TokenTimelock.sol +++ b/contracts/token/ERC20/TokenTimelock.sol @@ -12,13 +12,13 @@ contract TokenTimelock { using SafeERC20 for IERC20; // ERC20 basic token contract being held - IERC20 public token; + IERC20 private token_; // beneficiary of tokens after they are released - address public beneficiary; + address private beneficiary_; // timestamp when token release is enabled - uint256 public releaseTime; + uint256 private releaseTime_; constructor( IERC20 _token, @@ -29,9 +29,30 @@ contract TokenTimelock { { // solium-disable-next-line security/no-block-members require(_releaseTime > block.timestamp); - token = _token; - beneficiary = _beneficiary; - releaseTime = _releaseTime; + token_ = _token; + beneficiary_ = _beneficiary; + releaseTime_ = _releaseTime; + } + + /** + * @return the token being held. + */ + function getToken() public view returns(IERC20) { + return token_; + } + + /** + * @return the beneficiary of the tokens. + */ + function getBeneficiary() public view returns(address) { + return beneficiary_; + } + + /** + * @return the time when the tokens are released. + */ + function getReleaseTime() public view returns(uint256) { + return releaseTime_; } /** @@ -39,11 +60,11 @@ contract TokenTimelock { */ function release() public { // solium-disable-next-line security/no-block-members - require(block.timestamp >= releaseTime); + require(block.timestamp >= releaseTime_); - uint256 amount = token.balanceOf(address(this)); + uint256 amount = token_.balanceOf(address(this)); require(amount > 0); - token.safeTransfer(beneficiary, amount); + token_.safeTransfer(beneficiary_, amount); } } diff --git a/contracts/token/ERC20/TokenVesting.sol b/contracts/token/ERC20/TokenVesting.sol index 31fac8882ce..cf3d97a783c 100644 --- a/contracts/token/ERC20/TokenVesting.sol +++ b/contracts/token/ERC20/TokenVesting.sol @@ -21,16 +21,16 @@ contract TokenVesting is Ownable { event Revoked(); // beneficiary of tokens after they are released - address public beneficiary; + address private beneficiary_; - uint256 public cliff; - uint256 public start; - uint256 public duration; + uint256 private cliff_; + uint256 private start_; + uint256 private duration_; - bool public revocable; + bool private revocable_; - mapping (address => uint256) public released; - mapping (address => bool) public revoked; + mapping (address => uint256) private released_; + mapping (address => bool) private revoked_; /** * @dev Creates a vesting contract that vests its balance of any ERC20 token to the @@ -54,11 +54,60 @@ contract TokenVesting is Ownable { require(_beneficiary != address(0)); require(_cliff <= _duration); - beneficiary = _beneficiary; - revocable = _revocable; - duration = _duration; - cliff = _start.add(_cliff); - start = _start; + beneficiary_ = _beneficiary; + revocable_ = _revocable; + duration_ = _duration; + cliff_ = _start.add(_cliff); + start_ = _start; + } + + /** + * @return the beneficiary of the tokens. + */ + function getBeneficiary() public view returns(address) { + return beneficiary_; + } + + /** + * @return the cliff time of the token vesting. + */ + function getCliff() public view returns(uint256) { + return cliff_; + } + + /** + * @return the start time of the token vesting. + */ + function getStart() public view returns(uint256) { + return start_; + } + + /** + * @return the duration of the token vesting. + */ + function getDuration() public view returns(uint256) { + return duration_; + } + + /** + * @return true if the vesting is revocable. + */ + function isRevocable() public view returns(bool) { + return revocable_; + } + + /** + * @return the amount released to an account. + */ + function getReleased(address _account) public view returns(uint256) { + return released_[_account]; + } + + /** + * @return true if the account is revoked. + */ + function isRevoked(address _account) public view returns(bool) { + return revoked_[_account]; } /** @@ -70,9 +119,9 @@ contract TokenVesting is Ownable { require(unreleased > 0); - released[_token] = released[_token].add(unreleased); + released_[_token] = released_[_token].add(unreleased); - _token.safeTransfer(beneficiary, unreleased); + _token.safeTransfer(beneficiary_, unreleased); emit Released(unreleased); } @@ -83,15 +132,15 @@ contract TokenVesting is Ownable { * @param _token ERC20 token which is being vested */ function revoke(IERC20 _token) public onlyOwner { - require(revocable); - require(!revoked[_token]); + require(revocable_); + require(!revoked_[_token]); uint256 balance = _token.balanceOf(address(this)); uint256 unreleased = releasableAmount(_token); uint256 refund = balance.sub(unreleased); - revoked[_token] = true; + revoked_[_token] = true; _token.safeTransfer(owner, refund); @@ -103,7 +152,7 @@ contract TokenVesting is Ownable { * @param _token ERC20 token which is being vested */ function releasableAmount(IERC20 _token) public view returns (uint256) { - return vestedAmount(_token).sub(released[_token]); + return vestedAmount(_token).sub(released_[_token]); } /** @@ -112,14 +161,14 @@ contract TokenVesting is Ownable { */ function vestedAmount(IERC20 _token) public view returns (uint256) { uint256 currentBalance = _token.balanceOf(this); - uint256 totalBalance = currentBalance.add(released[_token]); + uint256 totalBalance = currentBalance.add(released_[_token]); - if (block.timestamp < cliff) { + if (block.timestamp < cliff_) { return 0; - } else if (block.timestamp >= start.add(duration) || revoked[_token]) { + } else if (block.timestamp >= start_.add(duration_) || revoked_[_token]) { return totalBalance; } else { - return totalBalance.mul(block.timestamp.sub(start)).div(duration); + return totalBalance.mul(block.timestamp.sub(start_)).div(duration_); } } } diff --git a/test/token/ERC20/DetailedERC20.test.js b/test/token/ERC20/DetailedERC20.test.js index 71b22a7b9a8..07dce7f642e 100644 --- a/test/token/ERC20/DetailedERC20.test.js +++ b/test/token/ERC20/DetailedERC20.test.js @@ -18,14 +18,14 @@ contract('ERC20Detailed', function () { }); it('has a name', async function () { - (await detailedERC20.name()).should.be.equal(_name); + (await detailedERC20.getName()).should.be.equal(_name); }); it('has a symbol', async function () { - (await detailedERC20.symbol()).should.be.equal(_symbol); + (await detailedERC20.getSymbol()).should.be.equal(_symbol); }); it('has an amount of decimals', async function () { - (await detailedERC20.decimals()).should.be.bignumber.equal(_decimals); + (await detailedERC20.getDecimals()).should.be.bignumber.equal(_decimals); }); }); diff --git a/test/token/ERC20/ERC20Capped.behavior.js b/test/token/ERC20/ERC20Capped.behavior.js index 004d512c042..8aeb58d3279 100644 --- a/test/token/ERC20/ERC20Capped.behavior.js +++ b/test/token/ERC20/ERC20Capped.behavior.js @@ -12,7 +12,7 @@ function shouldBehaveLikeERC20Capped (minter, [anyone], cap) { const from = minter; it('should start with the correct cap', async function () { - (await this.token.cap()).should.be.bignumber.equal(cap); + (await this.token.getCap()).should.be.bignumber.equal(cap); }); it('should mint when amount is less than cap', async function () { diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index 63fb861b551..12a8d376378 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -20,7 +20,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { describe('minting finished', function () { describe('when the token minting is not finished', function () { it('returns false', async function () { - (await this.token.mintingFinished()).should.equal(false); + (await this.token.isMintingFinished()).should.equal(false); }); }); @@ -30,7 +30,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { }); it('returns true', async function () { - (await this.token.mintingFinished()).should.equal(true); + (await this.token.isMintingFinished()).should.equal(true); }); }); }); @@ -43,7 +43,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { it('finishes token minting', async function () { await this.token.finishMinting({ from }); - (await this.token.mintingFinished()).should.equal(true); + (await this.token.isMintingFinished()).should.equal(true); }); it('emits a mint finished event', async function () { From 1c365fe9287800f4ae2c6a965195bbfa9fac4ff8 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 07:02:46 +0000 Subject: [PATCH 07/67] Improve encapsulation on ERC721 --- contracts/token/ERC721/ERC721.sol | 64 +++++++++++++------------- contracts/token/ERC721/ERC721Basic.sol | 36 +++++++-------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/contracts/token/ERC721/ERC721.sol b/contracts/token/ERC721/ERC721.sol index ef6a98453c4..b52b2b5ae63 100644 --- a/contracts/token/ERC721/ERC721.sol +++ b/contracts/token/ERC721/ERC721.sol @@ -20,19 +20,19 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { string internal symbol_; // Mapping from owner to list of owned token IDs - mapping(address => uint256[]) internal ownedTokens; + mapping(address => uint256[]) private ownedTokens_; // Mapping from token ID to index of the owner tokens list - mapping(uint256 => uint256) internal ownedTokensIndex; + mapping(uint256 => uint256) private ownedTokensIndex_; // Array with all token ids, used for enumeration - uint256[] internal allTokens; + uint256[] private allTokens_; // Mapping from token id to position in the allTokens array - mapping(uint256 => uint256) internal allTokensIndex; + mapping(uint256 => uint256) private allTokensIndex_; // Optional mapping for token URIs - mapping(uint256 => string) internal tokenURIs; + mapping(uint256 => string) private tokenURIs_; /** * @dev Constructor function @@ -69,7 +69,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { */ function tokenURI(uint256 _tokenId) public view returns (string) { require(_exists(_tokenId)); - return tokenURIs[_tokenId]; + return tokenURIs_[_tokenId]; } /** @@ -87,7 +87,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { returns (uint256) { require(_index < balanceOf(_owner)); - return ownedTokens[_owner][_index]; + return ownedTokens_[_owner][_index]; } /** @@ -95,7 +95,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { * @return uint256 representing the total amount of tokens */ function totalSupply() public view returns (uint256) { - return allTokens.length; + return allTokens_.length; } /** @@ -106,7 +106,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { */ function tokenByIndex(uint256 _index) public view returns (uint256) { require(_index < totalSupply()); - return allTokens[_index]; + return allTokens_[_index]; } /** @@ -117,7 +117,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { */ function _setTokenURI(uint256 _tokenId, string _uri) internal { require(_exists(_tokenId)); - tokenURIs[_tokenId] = _uri; + tokenURIs_[_tokenId] = _uri; } /** @@ -127,9 +127,9 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { */ function addTokenTo(address _to, uint256 _tokenId) internal { super.addTokenTo(_to, _tokenId); - uint256 length = ownedTokens[_to].length; - ownedTokens[_to].push(_tokenId); - ownedTokensIndex[_tokenId] = length; + uint256 length = ownedTokens_[_to].length; + ownedTokens_[_to].push(_tokenId); + ownedTokensIndex_[_tokenId] = length; } /** @@ -142,20 +142,20 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { // 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]; + uint256 tokenIndex = ownedTokensIndex_[_tokenId]; + uint256 lastTokenIndex = ownedTokens_[_from].length.sub(1); + uint256 lastToken = ownedTokens_[_from][lastTokenIndex]; - ownedTokens[_from][tokenIndex] = lastToken; + ownedTokens_[_from][tokenIndex] = lastToken; // This also deletes the contents at the last position of the array - ownedTokens[_from].length--; + 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; + ownedTokensIndex_[_tokenId] = 0; + ownedTokensIndex_[lastToken] = tokenIndex; } /** @@ -167,8 +167,8 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { function _mint(address _to, uint256 _tokenId) internal { super._mint(_to, _tokenId); - allTokensIndex[_tokenId] = allTokens.length; - allTokens.push(_tokenId); + allTokensIndex_[_tokenId] = allTokens_.length; + allTokens_.push(_tokenId); } /** @@ -181,21 +181,21 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { super._burn(_owner, _tokenId); // Clear metadata (if any) - if (bytes(tokenURIs[_tokenId]).length != 0) { - delete tokenURIs[_tokenId]; + 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]; + uint256 tokenIndex = allTokensIndex_[_tokenId]; + uint256 lastTokenIndex = allTokens_.length.sub(1); + uint256 lastToken = allTokens_[lastTokenIndex]; - allTokens[tokenIndex] = lastToken; - allTokens[lastTokenIndex] = 0; + allTokens_[tokenIndex] = lastToken; + allTokens_[lastTokenIndex] = 0; - allTokens.length--; - allTokensIndex[_tokenId] = 0; - allTokensIndex[lastToken] = tokenIndex; + allTokens_.length--; + allTokensIndex_[_tokenId] = 0; + allTokensIndex_[lastToken] = tokenIndex; } } diff --git a/contracts/token/ERC721/ERC721Basic.sol b/contracts/token/ERC721/ERC721Basic.sol index 58a40cae893..0c4700bf46a 100644 --- a/contracts/token/ERC721/ERC721Basic.sol +++ b/contracts/token/ERC721/ERC721Basic.sol @@ -21,16 +21,16 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { bytes4 private constant ERC721_RECEIVED = 0x150b7a02; // Mapping from token ID to owner - mapping (uint256 => address) internal tokenOwner; + mapping (uint256 => address) private tokenOwner_; // Mapping from token ID to approved address - mapping (uint256 => address) internal tokenApprovals; + mapping (uint256 => address) private tokenApprovals_; // Mapping from owner to number of owned token - mapping (address => uint256) internal ownedTokensCount; + mapping (address => uint256) private ownedTokensCount_; // Mapping from owner to operator approvals - mapping (address => mapping (address => bool)) internal operatorApprovals; + mapping (address => mapping (address => bool)) private operatorApprovals_; constructor() public @@ -46,7 +46,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { */ function balanceOf(address _owner) public view returns (uint256) { require(_owner != address(0)); - return ownedTokensCount[_owner]; + return ownedTokensCount_[_owner]; } /** @@ -55,7 +55,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * @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]; + address owner = tokenOwner_[_tokenId]; require(owner != address(0)); return owner; } @@ -73,7 +73,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { require(_to != owner); require(msg.sender == owner || isApprovedForAll(owner, msg.sender)); - tokenApprovals[_tokenId] = _to; + tokenApprovals_[_tokenId] = _to; emit Approval(owner, _to, _tokenId); } @@ -83,7 +83,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * @return address currently approved for the given token ID */ function getApproved(uint256 _tokenId) public view returns (address) { - return tokenApprovals[_tokenId]; + return tokenApprovals_[_tokenId]; } /** @@ -94,7 +94,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { */ function setApprovalForAll(address _to, bool _approved) public { require(_to != msg.sender); - operatorApprovals[msg.sender][_to] = _approved; + operatorApprovals_[msg.sender][_to] = _approved; emit ApprovalForAll(msg.sender, _to, _approved); } @@ -112,7 +112,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { view returns (bool) { - return operatorApprovals[_owner][_operator]; + return operatorApprovals_[_owner][_operator]; } /** @@ -194,7 +194,7 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * @return whether the token exists */ function _exists(uint256 _tokenId) internal view returns (bool) { - address owner = tokenOwner[_tokenId]; + address owner = tokenOwner_[_tokenId]; return owner != address(0); } @@ -255,8 +255,8 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { */ function clearApproval(address _owner, uint256 _tokenId) internal { require(ownerOf(_tokenId) == _owner); - if (tokenApprovals[_tokenId] != address(0)) { - tokenApprovals[_tokenId] = address(0); + if (tokenApprovals_[_tokenId] != address(0)) { + tokenApprovals_[_tokenId] = address(0); } } @@ -266,9 +266,9 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * @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); + require(tokenOwner_[_tokenId] == address(0)); + tokenOwner_[_tokenId] = _to; + ownedTokensCount_[_to] = ownedTokensCount_[_to].add(1); } /** @@ -278,8 +278,8 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { */ function removeTokenFrom(address _from, uint256 _tokenId) internal { require(ownerOf(_tokenId) == _from); - ownedTokensCount[_from] = ownedTokensCount[_from].sub(1); - tokenOwner[_tokenId] = address(0); + ownedTokensCount_[_from] = ownedTokensCount_[_from].sub(1); + tokenOwner_[_tokenId] = address(0); } /** From 9ebdae64860039eaf9881b86466f2dc0e15f7f47 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 16:27:05 +0000 Subject: [PATCH 08/67] Add tests, use standard getters --- contracts/token/ERC20/ERC20Detailed.sol | 6 +++--- contracts/token/ERC20/TokenVesting.sol | 6 +++--- ...ailedERC20.test.js => ERC20Detailed.test.js} | 6 +++--- test/token/ERC20/TokenTimelock.test.js | 6 ++++++ test/token/ERC20/TokenVesting.test.js | 17 ++++++++++++++--- 5 files changed, 29 insertions(+), 12 deletions(-) rename test/token/ERC20/{DetailedERC20.test.js => ERC20Detailed.test.js} (75%) diff --git a/contracts/token/ERC20/ERC20Detailed.sol b/contracts/token/ERC20/ERC20Detailed.sol index aefa3947184..6becdee1086 100644 --- a/contracts/token/ERC20/ERC20Detailed.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -23,21 +23,21 @@ contract ERC20Detailed is IERC20 { /** * @return the name of the token. */ - function getName() public view returns(string) { + function name() public view returns(string) { return name_; } /** * @return the symbol of the token. */ - function getSymbol() public view returns(string) { + function symbol() public view returns(string) { return symbol_; } /** * @return the number of decimals of the token. */ - function getDecimals() public view returns(uint8) { + function decimals() public view returns(uint8) { return decimals_; } } diff --git a/contracts/token/ERC20/TokenVesting.sol b/contracts/token/ERC20/TokenVesting.sol index cf3d97a783c..422431e7200 100644 --- a/contracts/token/ERC20/TokenVesting.sol +++ b/contracts/token/ERC20/TokenVesting.sol @@ -104,10 +104,10 @@ contract TokenVesting is Ownable { } /** - * @return true if the account is revoked. + * @return true if the token is revoked. */ - function isRevoked(address _account) public view returns(bool) { - return revoked_[_account]; + function isRevoked(address _token) public view returns(bool) { + return revoked_[_token]; } /** diff --git a/test/token/ERC20/DetailedERC20.test.js b/test/token/ERC20/ERC20Detailed.test.js similarity index 75% rename from test/token/ERC20/DetailedERC20.test.js rename to test/token/ERC20/ERC20Detailed.test.js index 07dce7f642e..71b22a7b9a8 100644 --- a/test/token/ERC20/DetailedERC20.test.js +++ b/test/token/ERC20/ERC20Detailed.test.js @@ -18,14 +18,14 @@ contract('ERC20Detailed', function () { }); it('has a name', async function () { - (await detailedERC20.getName()).should.be.equal(_name); + (await detailedERC20.name()).should.be.equal(_name); }); it('has a symbol', async function () { - (await detailedERC20.getSymbol()).should.be.equal(_symbol); + (await detailedERC20.symbol()).should.be.equal(_symbol); }); it('has an amount of decimals', async function () { - (await detailedERC20.getDecimals()).should.be.bignumber.equal(_decimals); + (await detailedERC20.decimals()).should.be.bignumber.equal(_decimals); }); }); diff --git a/test/token/ERC20/TokenTimelock.test.js b/test/token/ERC20/TokenTimelock.test.js index 168992707af..c121247e3e7 100644 --- a/test/token/ERC20/TokenTimelock.test.js +++ b/test/token/ERC20/TokenTimelock.test.js @@ -33,6 +33,12 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { await this.token.mint(this.timelock.address, amount, { from: owner }); }); + it('can get state', async function () { + (await this.timelock.getToken()).should.be.equal(this.token.address); + (await this.timelock.getBeneficiary()).should.be.equal(beneficiary); + (await this.timelock.getReleaseTime()).should.be.bignumber.equal(this.releaseTime); + }); + it('cannot be released before time limit', async function () { await expectThrow(this.timelock.release()); }); diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index 60d773511ec..7bb6d72c69a 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -48,6 +48,14 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await this.token.mint(this.vesting.address, amount, { from: owner }); }); + it('can get state', async function () { + (await this.vesting.getBeneficiary()).should.be.equal(beneficiary); + (await this.vesting.getCliff()).should.be.equal(this.cliff); + (await this.vesting.getStart()).should.be.equal(this.start); + (await this.vesting.getDuration()).should.be.equal(this.duration); + (await this.vesting.isRevocable()).should.be.equal(true); + }); + it('cannot be released before cliff', async function () { await expectThrow( this.vesting.release(this.token.address), @@ -67,9 +75,9 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const block = await ethGetBlock(receipt.blockNumber); const releaseTime = block.timestamp; - (await this.token.balanceOf(beneficiary)).should.bignumber.equal( - amount.mul(releaseTime - this.start).div(this.duration).floor() - ); + const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor(); + (await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount); + (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(releasedAmount); }); it('should linearly release tokens during vesting period', async function () { @@ -83,6 +91,7 @@ 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.equal(expectedVesting); + (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(expectedVesting); } }); @@ -90,10 +99,12 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await increaseTimeTo(this.start + this.duration); await this.vesting.release(this.token.address); (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount); + (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(amount); }); it('should be revoked by owner if revocable is set', async function () { await this.vesting.revoke(this.token.address, { from: owner }); + (await this.vesting.isRevoked(this.token.address)).should.equal(true); }); it('should fail to be revoked by owner if revocable not set', async function () { From 963a00bfa6b587e9be728750bea5689c5b1e8ebf Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 17:06:19 +0000 Subject: [PATCH 09/67] fix tests --- contracts/token/ERC20/TokenVesting.sol | 14 ++++---- test/token/ERC20/TokenVesting.test.js | 44 +++++++++++++------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/contracts/token/ERC20/TokenVesting.sol b/contracts/token/ERC20/TokenVesting.sol index 422431e7200..5c48e18882e 100644 --- a/contracts/token/ERC20/TokenVesting.sol +++ b/contracts/token/ERC20/TokenVesting.sol @@ -37,7 +37,7 @@ contract TokenVesting is Ownable { * _beneficiary, gradually in a linear fashion until _start + _duration. By then all * of the balance will have vested. * @param _beneficiary address of the beneficiary to whom vested tokens are transferred - * @param _cliff duration in seconds of the cliff in which tokens will begin to vest + * @param _cliffDuration duration in seconds of the cliff in which tokens will begin to vest * @param _start the time (as Unix time) at which point vesting starts * @param _duration duration in seconds of the period in which the tokens will vest * @param _revocable whether the vesting is revocable or not @@ -45,19 +45,19 @@ contract TokenVesting is Ownable { constructor( address _beneficiary, uint256 _start, - uint256 _cliff, + uint256 _cliffDuration, uint256 _duration, bool _revocable ) public { require(_beneficiary != address(0)); - require(_cliff <= _duration); + require(_cliffDuration <= _duration); beneficiary_ = _beneficiary; revocable_ = _revocable; duration_ = _duration; - cliff_ = _start.add(_cliff); + cliff_ = _start.add(_cliffDuration); start_ = _start; } @@ -97,10 +97,10 @@ contract TokenVesting is Ownable { } /** - * @return the amount released to an account. + * @return the amount of the token released. */ - function getReleased(address _account) public view returns(uint256) { - return released_[_account]; + function getReleased(address _token) public view returns(uint256) { + return released_[_token]; } /** diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index 7bb6d72c69a..9fc7c347599 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -13,36 +13,36 @@ require('chai') const ERC20Mintable = artifacts.require('ERC20Mintable'); const TokenVesting = artifacts.require('TokenVesting'); -contract('TokenVesting', function ([_, owner, beneficiary]) { +contract.only('TokenVesting', function ([_, owner, beneficiary]) { const amount = new BigNumber(1000); const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; beforeEach(async function () { this.start = (await latestTime()) + duration.minutes(1); // +1 minute so it starts after contract instantiation - this.cliff = duration.years(1); + this.cliffDuration = duration.years(1); this.duration = duration.years(2); }); it('rejects a duration shorter than the cliff', async function () { - const cliff = this.duration; - const duration = this.cliff; + const cliffDuration = this.duration; + const duration = this.cliffDuration; - cliff.should.be.gt(duration); + cliffDuration.should.be.gt(duration); await expectThrow( - TokenVesting.new(beneficiary, this.start, cliff, duration, true, { from: owner }) + TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner }) ); }); it('requires a valid beneficiary', async function () { await expectThrow( - TokenVesting.new(ZERO_ADDRESS, this.start, this.cliff, this.duration, true, { from: owner }) + TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner }) ); }); context('once deployed', function () { beforeEach(async function () { - this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner }); + this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }); this.token = await ERC20Mintable.new({ from: owner }); await this.token.mint(this.vesting.address, amount, { from: owner }); @@ -50,9 +50,9 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { it('can get state', async function () { (await this.vesting.getBeneficiary()).should.be.equal(beneficiary); - (await this.vesting.getCliff()).should.be.equal(this.cliff); - (await this.vesting.getStart()).should.be.equal(this.start); - (await this.vesting.getDuration()).should.be.equal(this.duration); + (await this.vesting.getCliff()).should.be.bignumber.equal(this.start + this.cliffDuration); + (await this.vesting.getStart()).should.be.bignumber.equal(this.start); + (await this.vesting.getDuration()).should.be.bignumber.equal(this.duration); (await this.vesting.isRevocable()).should.be.equal(true); }); @@ -64,12 +64,12 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('can be released after cliff', async function () { - await increaseTimeTo(this.start + this.cliff + duration.weeks(1)); + await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(1)); await this.vesting.release(this.token.address); }); it('should release proper amount after cliff', async function () { - await increaseTimeTo(this.start + this.cliff); + await increaseTimeTo(this.start + this.cliffDuration); const { receipt } = await this.vesting.release(this.token.address); const block = await ethGetBlock(receipt.blockNumber); @@ -77,21 +77,21 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor(); (await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount); - (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(releasedAmount); + (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(releasedAmount); }); it('should linearly release tokens during vesting period', async function () { - const vestingPeriod = this.duration - this.cliff; + const vestingPeriod = this.duration - this.cliffDuration; const checkpoints = 4; for (let i = 1; i <= checkpoints; i++) { - const now = this.start + this.cliff + i * (vestingPeriod / checkpoints); + const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints); await increaseTimeTo(now); 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.equal(expectedVesting); - (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(expectedVesting); + (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(expectedVesting); } }); @@ -99,7 +99,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await increaseTimeTo(this.start + this.duration); await this.vesting.release(this.token.address); (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount); - (await this.vesting.getReleased(beneficiary)).should.bignumber.equal(amount); + (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(amount); }); it('should be revoked by owner if revocable is set', async function () { @@ -109,7 +109,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { it('should fail to be revoked by owner if revocable not set', async function () { const vesting = await TokenVesting.new( - beneficiary, this.start, this.cliff, this.duration, false, { from: owner } + beneficiary, this.start, this.cliffDuration, this.duration, false, { from: owner } ); await expectThrow( @@ -119,7 +119,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should return the non-vested tokens when revoked by owner', async function () { - await increaseTimeTo(this.start + this.cliff + duration.weeks(12)); + await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12)); const vested = await this.vesting.vestedAmount(this.token.address); @@ -129,7 +129,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should keep the vested tokens when revoked by owner', async function () { - await increaseTimeTo(this.start + this.cliff + duration.weeks(12)); + await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12)); const vestedPre = await this.vesting.vestedAmount(this.token.address); @@ -141,7 +141,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('should fail to be revoked a second time', async function () { - await increaseTimeTo(this.start + this.cliff + duration.weeks(12)); + await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12)); await this.vesting.vestedAmount(this.token.address); From 4d8db8d8e5e8a1b3b639ed4ab5d70726ee3c9fe1 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 17:48:16 +0000 Subject: [PATCH 10/67] Fix lint --- test/token/ERC20/TokenVesting.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index 9fc7c347599..dcf080cfe50 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -13,7 +13,7 @@ require('chai') const ERC20Mintable = artifacts.require('ERC20Mintable'); const TokenVesting = artifacts.require('TokenVesting'); -contract.only('TokenVesting', function ([_, owner, beneficiary]) { +contract('TokenVesting', function ([_, owner, beneficiary]) { const amount = new BigNumber(1000); const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; @@ -42,7 +42,8 @@ contract.only('TokenVesting', function ([_, owner, beneficiary]) { context('once deployed', function () { beforeEach(async function () { - this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }); + this.vesting = await TokenVesting.new( + beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }); this.token = await ERC20Mintable.new({ from: owner }); await this.token.mint(this.vesting.address, amount, { from: owner }); 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 11/67] 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 0b0d6c6ebb8f515919b1fee53b67c75c08460a4f Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Tue, 4 Sep 2018 17:35:53 -0300 Subject: [PATCH 12/67] move interface ids to implementation contracts --- contracts/token/ERC721/ERC721.sol | 16 +++++++++++++ contracts/token/ERC721/ERC721Basic.sol | 14 ++++++++++++ contracts/token/ERC721/IERC721Basic.sol | 30 ------------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/contracts/token/ERC721/ERC721.sol b/contracts/token/ERC721/ERC721.sol index 35575bd6a0d..a2c63ef5527 100644 --- a/contracts/token/ERC721/ERC721.sol +++ b/contracts/token/ERC721/ERC721.sol @@ -34,6 +34,22 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { // Optional mapping for token URIs mapping(uint256 => string) private tokenURIs_; + bytes4 private constant InterfaceId_ERC721Enumerable = 0x780e9d63; + /** + * 0x780e9d63 === + * bytes4(keccak256('totalSupply()')) ^ + * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^ + * bytes4(keccak256('tokenByIndex(uint256)')) + */ + + bytes4 private constant InterfaceId_ERC721Metadata = 0x5b5e139f; + /** + * 0x5b5e139f === + * bytes4(keccak256('name()')) ^ + * bytes4(keccak256('symbol()')) ^ + * bytes4(keccak256('tokenURI(uint256)')) + */ + /** * @dev Constructor function */ diff --git a/contracts/token/ERC721/ERC721Basic.sol b/contracts/token/ERC721/ERC721Basic.sol index 93b1f2321d6..fcc8397ea87 100644 --- a/contracts/token/ERC721/ERC721Basic.sol +++ b/contracts/token/ERC721/ERC721Basic.sol @@ -32,6 +32,20 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { // Mapping from owner to operator approvals mapping (address => mapping (address => bool)) private operatorApprovals_; + bytes4 private 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)')) + */ + constructor() public { diff --git a/contracts/token/ERC721/IERC721Basic.sol b/contracts/token/ERC721/IERC721Basic.sol index 50a5c7f5359..6e71d1cf589 100644 --- a/contracts/token/ERC721/IERC721Basic.sol +++ b/contracts/token/ERC721/IERC721Basic.sol @@ -9,36 +9,6 @@ import "../../introspection/IERC165.sol"; */ 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, From 68094d13c8105908ce5301d32e3c538eb3788c2f Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 02:30:19 +0000 Subject: [PATCH 13/67] Do not prefix getters --- contracts/token/ERC20/ERC20Capped.sol | 2 +- contracts/token/ERC20/ERC20Mintable.sol | 2 +- contracts/token/ERC20/TokenTimelock.sol | 6 +++--- contracts/token/ERC20/TokenVesting.sol | 14 +++++++------- test/token/ERC20/ERC20Capped.behavior.js | 2 +- test/token/ERC20/ERC20Mintable.behavior.js | 6 +++--- test/token/ERC20/TokenTimelock.test.js | 6 +++--- test/token/ERC20/TokenVesting.test.js | 18 +++++++++--------- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/contracts/token/ERC20/ERC20Capped.sol b/contracts/token/ERC20/ERC20Capped.sol index 13f5dabdcbf..23631f9049c 100644 --- a/contracts/token/ERC20/ERC20Capped.sol +++ b/contracts/token/ERC20/ERC20Capped.sol @@ -19,7 +19,7 @@ contract ERC20Capped is ERC20Mintable { /** * @return the cap for the token minting. */ - function getCap() public view returns(uint256) { + function cap() public view returns(uint256) { return cap_; } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 6c329317a58..e680e6b6055 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -29,7 +29,7 @@ contract ERC20Mintable is ERC20, Ownable { /** * @return true if the minting is finished. */ - function isMintingFinished() public view returns(bool) { + function mintingFinished() public view returns(bool) { return mintingFinished_; } diff --git a/contracts/token/ERC20/TokenTimelock.sol b/contracts/token/ERC20/TokenTimelock.sol index 0ed6341a71c..d8f96fa6668 100644 --- a/contracts/token/ERC20/TokenTimelock.sol +++ b/contracts/token/ERC20/TokenTimelock.sol @@ -37,21 +37,21 @@ contract TokenTimelock { /** * @return the token being held. */ - function getToken() public view returns(IERC20) { + function token() public view returns(IERC20) { return token_; } /** * @return the beneficiary of the tokens. */ - function getBeneficiary() public view returns(address) { + function beneficiary() public view returns(address) { return beneficiary_; } /** * @return the time when the tokens are released. */ - function getReleaseTime() public view returns(uint256) { + function releaseTime() public view returns(uint256) { return releaseTime_; } diff --git a/contracts/token/ERC20/TokenVesting.sol b/contracts/token/ERC20/TokenVesting.sol index 5c48e18882e..5cadf03c465 100644 --- a/contracts/token/ERC20/TokenVesting.sol +++ b/contracts/token/ERC20/TokenVesting.sol @@ -64,49 +64,49 @@ contract TokenVesting is Ownable { /** * @return the beneficiary of the tokens. */ - function getBeneficiary() public view returns(address) { + function beneficiary() public view returns(address) { return beneficiary_; } /** * @return the cliff time of the token vesting. */ - function getCliff() public view returns(uint256) { + function cliff() public view returns(uint256) { return cliff_; } /** * @return the start time of the token vesting. */ - function getStart() public view returns(uint256) { + function start() public view returns(uint256) { return start_; } /** * @return the duration of the token vesting. */ - function getDuration() public view returns(uint256) { + function duration() public view returns(uint256) { return duration_; } /** * @return true if the vesting is revocable. */ - function isRevocable() public view returns(bool) { + function revocable() public view returns(bool) { return revocable_; } /** * @return the amount of the token released. */ - function getReleased(address _token) public view returns(uint256) { + function released(address _token) public view returns(uint256) { return released_[_token]; } /** * @return true if the token is revoked. */ - function isRevoked(address _token) public view returns(bool) { + function revoked(address _token) public view returns(bool) { return revoked_[_token]; } diff --git a/test/token/ERC20/ERC20Capped.behavior.js b/test/token/ERC20/ERC20Capped.behavior.js index 8aeb58d3279..004d512c042 100644 --- a/test/token/ERC20/ERC20Capped.behavior.js +++ b/test/token/ERC20/ERC20Capped.behavior.js @@ -12,7 +12,7 @@ function shouldBehaveLikeERC20Capped (minter, [anyone], cap) { const from = minter; it('should start with the correct cap', async function () { - (await this.token.getCap()).should.be.bignumber.equal(cap); + (await this.token.cap()).should.be.bignumber.equal(cap); }); it('should mint when amount is less than cap', async function () { diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index 12a8d376378..63fb861b551 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -20,7 +20,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { describe('minting finished', function () { describe('when the token minting is not finished', function () { it('returns false', async function () { - (await this.token.isMintingFinished()).should.equal(false); + (await this.token.mintingFinished()).should.equal(false); }); }); @@ -30,7 +30,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { }); it('returns true', async function () { - (await this.token.isMintingFinished()).should.equal(true); + (await this.token.mintingFinished()).should.equal(true); }); }); }); @@ -43,7 +43,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { it('finishes token minting', async function () { await this.token.finishMinting({ from }); - (await this.token.isMintingFinished()).should.equal(true); + (await this.token.mintingFinished()).should.equal(true); }); it('emits a mint finished event', async function () { diff --git a/test/token/ERC20/TokenTimelock.test.js b/test/token/ERC20/TokenTimelock.test.js index c121247e3e7..70eaae8f54d 100644 --- a/test/token/ERC20/TokenTimelock.test.js +++ b/test/token/ERC20/TokenTimelock.test.js @@ -34,9 +34,9 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { }); it('can get state', async function () { - (await this.timelock.getToken()).should.be.equal(this.token.address); - (await this.timelock.getBeneficiary()).should.be.equal(beneficiary); - (await this.timelock.getReleaseTime()).should.be.bignumber.equal(this.releaseTime); + (await this.timelock.token()).should.be.equal(this.token.address); + (await this.timelock.beneficiary()).should.be.equal(beneficiary); + (await this.timelock.releaseTime()).should.be.bignumber.equal(this.releaseTime); }); 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 dcf080cfe50..1fe9326f5fa 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -50,11 +50,11 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { }); it('can get state', async function () { - (await this.vesting.getBeneficiary()).should.be.equal(beneficiary); - (await this.vesting.getCliff()).should.be.bignumber.equal(this.start + this.cliffDuration); - (await this.vesting.getStart()).should.be.bignumber.equal(this.start); - (await this.vesting.getDuration()).should.be.bignumber.equal(this.duration); - (await this.vesting.isRevocable()).should.be.equal(true); + (await this.vesting.beneficiary()).should.be.equal(beneficiary); + (await this.vesting.cliff()).should.be.bignumber.equal(this.start + this.cliffDuration); + (await this.vesting.start()).should.be.bignumber.equal(this.start); + (await this.vesting.duration()).should.be.bignumber.equal(this.duration); + (await this.vesting.revocable()).should.be.equal(true); }); it('cannot be released before cliff', async function () { @@ -78,7 +78,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor(); (await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount); - (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(releasedAmount); + (await this.vesting.released(this.token.address)).should.bignumber.equal(releasedAmount); }); it('should linearly release tokens during vesting period', async function () { @@ -92,7 +92,7 @@ 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.equal(expectedVesting); - (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(expectedVesting); + (await this.vesting.released(this.token.address)).should.bignumber.equal(expectedVesting); } }); @@ -100,12 +100,12 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await increaseTimeTo(this.start + this.duration); await this.vesting.release(this.token.address); (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount); - (await this.vesting.getReleased(this.token.address)).should.bignumber.equal(amount); + (await this.vesting.released(this.token.address)).should.bignumber.equal(amount); }); it('should be revoked by owner if revocable is set', async function () { await this.vesting.revoke(this.token.address, { from: owner }); - (await this.vesting.isRevoked(this.token.address)).should.equal(true); + (await this.vesting.revoked(this.token.address)).should.equal(true); }); it('should fail to be revoked by owner if revocable not set', async function () { From 5fc3742423913c8e460cac5fc2a947a029daf420 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:11:24 +0000 Subject: [PATCH 14/67] Improve encapsulation on Pausable --- contracts/lifecycle/Pausable.sol | 9 ++++++++- test/token/ERC20/ERC20Pausable.test.js | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 0a2767aa61f..03dff6692ee 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -12,9 +12,16 @@ contract Pausable is Ownable { event Paused(); event Unpaused(); - bool public paused = false; + bool private paused = false; + /** + * @return true if the contract is paused, false otherwise. + */ + function isPaused() public view returns(bool) { + return paused; + } + /** * @dev Modifier to make a function callable only when the contract is not paused. */ diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index 8fbe4f70876..b11ecb3e7b6 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -13,7 +13,7 @@ contract('ERC20Pausable', 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.equal(true); + (await this.token.isPaused()).should.equal(true); }); it('emits a Pause event', async function () { @@ -55,7 +55,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { it('unpauses the token', async function () { await this.token.unpause({ from }); - (await this.token.paused()).should.equal(false); + (await this.token.isPaused()).should.equal(false); }); it('emits an Unpause event', async function () { @@ -87,18 +87,18 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { describe('paused', function () { it('is not paused by default', async function () { - (await this.token.paused({ from })).should.equal(false); + (await this.token.isPaused({ from })).should.equal(false); }); it('is paused after being paused', async function () { await this.token.pause({ from }); - (await this.token.paused({ from })).should.equal(true); + (await this.token.isPaused({ 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.equal(false); + (await this.token.isPaused()).should.equal(false); }); }); From 172b72066699690b24c446acc1ecd34bddb4acba Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:13:48 +0000 Subject: [PATCH 15/67] add the underscore --- contracts/lifecycle/Pausable.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 03dff6692ee..3e5676f7f87 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -12,21 +12,21 @@ contract Pausable is Ownable { event Paused(); event Unpaused(); - bool private paused = false; + bool private paused_ = false; /** * @return true if the contract is paused, false otherwise. */ function isPaused() public view returns(bool) { - return paused; + return paused_; } /** * @dev Modifier to make a function callable only when the contract is not paused. */ modifier whenNotPaused() { - require(!paused); + require(!paused_); _; } @@ -34,7 +34,7 @@ contract Pausable is Ownable { * @dev Modifier to make a function callable only when the contract is paused. */ modifier whenPaused() { - require(paused); + require(paused_); _; } @@ -42,7 +42,7 @@ contract Pausable is Ownable { * @dev called by the owner to pause, triggers stopped state */ function pause() public onlyOwner whenNotPaused { - paused = true; + paused_ = true; emit Paused(); } @@ -50,7 +50,7 @@ contract Pausable is Ownable { * @dev called by the owner to unpause, returns to normal state */ function unpause() public onlyOwner whenPaused { - paused = false; + paused_ = false; emit Unpaused(); } } From 86bbab3bb26ed30baf6dbb1bdb2c69da98290d52 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:44:22 +0000 Subject: [PATCH 16/67] Improve encapsulation on ownership --- contracts/drafts/TokenVesting.sol | 2 +- contracts/ownership/Claimable.sol | 19 +++++++++---- contracts/ownership/DelayedClaimable.sol | 32 +++++++++++++++------- contracts/ownership/Heritable.sol | 13 ++++----- contracts/ownership/Ownable.sol | 19 +++++++++---- contracts/ownership/Superuser.sol | 2 +- contracts/token/ERC20/ERC20Mintable.sol | 2 +- test/access/SignatureBouncer.test.js | 2 +- test/crowdsale/MintedCrowdsale.test.js | 2 +- test/ownership/Claimable.test.js | 6 ++-- test/ownership/DelayedClaimable.test.js | 20 +++++++------- test/ownership/Ownable.behavior.js | 6 ++-- test/ownership/Superuser.test.js | 4 +-- test/token/ERC20/ERC20Mintable.behavior.js | 2 +- 14 files changed, 78 insertions(+), 53 deletions(-) diff --git a/contracts/drafts/TokenVesting.sol b/contracts/drafts/TokenVesting.sol index cc90e24940b..08f2c64d988 100644 --- a/contracts/drafts/TokenVesting.sol +++ b/contracts/drafts/TokenVesting.sol @@ -142,7 +142,7 @@ contract TokenVesting is Ownable { revoked_[_token] = true; - _token.safeTransfer(owner, refund); + _token.safeTransfer(getOwner(), refund); emit Revoked(); } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index 9027c11f52d..6576632af12 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -10,30 +10,37 @@ import "./Ownable.sol"; * This allows the new owner to accept the transfer. */ contract Claimable is Ownable { - address public pendingOwner; + address private pendingOwner_; /** * @dev Modifier throws if called by any account other than the pendingOwner. */ modifier onlyPendingOwner() { - require(msg.sender == pendingOwner); + require(msg.sender == pendingOwner_); _; } + /** + * @return the address of the pending owner. + */ + function getPendingOwner() public view returns(address) { + return 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; + pendingOwner_ = newOwner; } /** * @dev Allows the pendingOwner address to finalize the transfer. */ function claimOwnership() public onlyPendingOwner { - emit OwnershipTransferred(owner, pendingOwner); - owner = pendingOwner; - pendingOwner = address(0); + emit OwnershipTransferred(getOwner(), pendingOwner_); + owner = pendingOwner_; + pendingOwner_ = address(0); } } diff --git a/contracts/ownership/DelayedClaimable.sol b/contracts/ownership/DelayedClaimable.sol index a9c0857f8e9..b965856490a 100644 --- a/contracts/ownership/DelayedClaimable.sol +++ b/contracts/ownership/DelayedClaimable.sol @@ -10,8 +10,22 @@ import "./Claimable.sol"; */ contract DelayedClaimable is Claimable { - uint256 public end; - uint256 public start; + uint256 private start_; + uint256 private end_; + + /** + * @return the start of the claimable period. + */ + function getStart() public view returns(uint256) { + return start_; + } + + /** + * @return the end of the claimable period. + */ + function getEnd() public view returns(uint256) { + return end_; + } /** * @dev Used to specify the time period during which a pending @@ -21,20 +35,18 @@ contract DelayedClaimable is Claimable { */ function setLimits(uint256 _start, uint256 _end) public onlyOwner { require(_start <= _end); - end = _end; - start = _start; + end_ = _end; + start_ = _start; } /** - * @dev Allows the pendingOwner address to finalize the transfer, as long as it is called within + * @dev Allows the pending owner 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; + require((block.number <= end_) && (block.number >= start_)); + super.claimOwnership(); + end_ = 0; } } diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index 88e6fee62f4..9e3214c071f 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -51,9 +51,9 @@ contract Heritable is Ownable { } function setHeir(address _newHeir) public onlyOwner { - require(_newHeir != owner); + require(_newHeir != getOwner()); heartbeat(); - emit HeirChanged(owner, _newHeir); + emit HeirChanged(getOwner(), _newHeir); heir_ = _newHeir; } @@ -87,7 +87,7 @@ contract Heritable is Ownable { */ function proclaimDeath() public onlyHeir { require(_ownerLives()); - emit OwnerProclaimedDead(owner, heir_, timeOfDeath_); + emit OwnerProclaimedDead(getOwner(), heir_, timeOfDeath_); // solium-disable-next-line security/no-block-members timeOfDeath_ = block.timestamp; } @@ -96,7 +96,7 @@ contract Heritable is Ownable { * @dev Owner can send a heartbeat if they were mistakenly pronounced dead. */ function heartbeat() public onlyOwner { - emit OwnerHeartbeated(owner); + emit OwnerHeartbeated(getOwner()); timeOfDeath_ = 0; } @@ -107,9 +107,8 @@ contract Heritable is Ownable { 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_; + emit HeirOwnershipClaimed(getOwner(), heir_); + transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index 1fbf571bf7c..a4d42ab0501 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -7,7 +7,7 @@ pragma solidity ^0.4.24; * functions, this simplifies the implementation of "user permissions". */ contract Ownable { - address public owner; + address private owner_; event OwnershipRenounced(address indexed previousOwner); @@ -22,14 +22,21 @@ contract Ownable { * account. */ constructor() public { - owner = msg.sender; + owner_ = msg.sender; + } + + /** + * @return the address of the owner. + */ + function getOwner() public view returns(address) { + return owner_; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { - require(msg.sender == owner); + require(msg.sender == owner_); _; } @@ -40,7 +47,7 @@ contract Ownable { * modifier anymore. */ function renounceOwnership() public onlyOwner { - emit OwnershipRenounced(owner); + emit OwnershipRenounced(owner_); owner = address(0); } @@ -58,7 +65,7 @@ contract Ownable { */ function _transferOwnership(address _newOwner) internal { require(_newOwner != address(0)); - emit OwnershipTransferred(owner, _newOwner); - owner = _newOwner; + emit OwnershipTransferred(owner_, _newOwner); + owner_ = _newOwner; } } diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index d8d081d03c3..c0fddf3ab5c 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -27,7 +27,7 @@ contract Superuser is Ownable, RBAC { } modifier onlyOwnerOrSuperuser() { - require(msg.sender == owner || isSuperuser(msg.sender)); + require(msg.sender == getOwner() || isSuperuser(msg.sender)); _; } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index e680e6b6055..93f0d28e977 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier hasMintPermission() { - require(msg.sender == owner); + require(msg.sender == getOwner()); _; } diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js index 798ab0ac9fa..19143c850b0 100644 --- a/test/access/SignatureBouncer.test.js +++ b/test/access/SignatureBouncer.test.js @@ -20,7 +20,7 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] context('management', function () { it('has a default owner of self', async function () { - (await this.bouncer.owner()).should.equal(owner); + (await this.bouncer.getOwner()).should.equal(owner); }); it('allows the owner to add a bouncer', async function () { diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index 50a37eaef72..9f3c8204229 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -21,7 +21,7 @@ contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be token owner', async function () { - (await this.token.owner()).should.equal(this.crowdsale.address); + (await this.token.getOwner()).should.equal(this.crowdsale.address); }); shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js index bfc1355d5f7..40659532676 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.equal(0); + (await claimable.getOwner()).should.not.equal(0); }); it('changes pendingOwner after transfer', async function () { await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.pendingOwner()).should.equal(newOwner); + (await claimable.getPendingOwner()).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.equal(newOwner); + (await claimable.getOwner()).should.equal(newOwner); }); }); }); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js index e65ac86c41d..039f1ae672c 100644 --- a/test/ownership/DelayedClaimable.test.js +++ b/test/ownership/DelayedClaimable.test.js @@ -17,35 +17,35 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { 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.getEnd()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); + (await this.delayedClaimable.getStart()).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.getEnd()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); + (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.owner()).should.equal(newOwner); + (await this.delayedClaimable.getOwner()).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.getEnd()).should.be.bignumber.equal(110); - (await this.delayedClaimable.start()).should.be.bignumber.equal(100); + (await this.delayedClaimable.getStart()).should.be.bignumber.equal(100); - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.owner()).should.not.equal(newOwner); + (await this.delayedClaimable.getOwner()).should.not.equal(newOwner); }); it('set end and start invalid values fail', async function () { diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index 6257c01df5a..ebb550b0f25 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.equal(owner); + (await this.ownable.getOwner()).should.equal(owner); }); it('changes owner after transfer', async function () { await this.ownable.transferOwnership(anyone, { from: owner }); - (await this.ownable.owner()).should.equal(anyone); + (await this.ownable.getOwner()).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.equal(ZERO_ADDRESS); + (await this.ownable.getOwner()).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 5e4d2c32456..3b4e6ede62d 100644 --- a/test/ownership/Superuser.test.js +++ b/test/ownership/Superuser.test.js @@ -40,7 +40,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.owner()).should.equal(newOwner); + (await this.superuser.getOwner()).should.equal(newOwner); }); it('should change owner after the owner transfers the ownership', async function () { @@ -49,7 +49,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.owner()).should.equal(newOwner); + (await this.superuser.getOwner()).should.equal(newOwner); }); }); diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index 63fb861b551..cb982ed26f1 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -13,7 +13,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { 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); + (await this.token.getOwner({ from: owner })).should.equal(owner); }); }); From faed52de1fc8c3e152b0b9089495f5ae1674e459 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:50:07 +0000 Subject: [PATCH 17/67] fix rebase --- contracts/bounties/BreakInvariantBounty.sol | 2 +- contracts/ownership/Ownable.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index c889fbe606f..1f330ffd378 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -50,7 +50,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() public onlyOwner { - selfdestruct(owner); + selfdestruct(getOwner()); } /** diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index a4d42ab0501..9a7d60d8978 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -48,7 +48,7 @@ contract Ownable { */ function renounceOwnership() public onlyOwner { emit OwnershipRenounced(owner_); - owner = address(0); + owner_ = address(0); } /** From 21ae1773101d8df91cc7124a0e748624bef5004c Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:55:20 +0000 Subject: [PATCH 18/67] fix ownership --- contracts/ownership/CanReclaimToken.sol | 2 +- contracts/ownership/Claimable.sol | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index 858a1db84e8..11452cdeb9b 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -20,7 +20,7 @@ contract CanReclaimToken is Ownable { */ function reclaimToken(IERC20 _token) external onlyOwner { uint256 balance = _token.balanceOf(this); - _token.safeTransfer(owner, balance); + _token.safeTransfer(getOwner(), balance); } } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index 6576632af12..fd7c2ffbb39 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -39,8 +39,6 @@ contract Claimable is Ownable { * @dev Allows the pendingOwner address to finalize the transfer. */ function claimOwnership() public onlyPendingOwner { - emit OwnershipTransferred(getOwner(), pendingOwner_); - owner = pendingOwner_; - pendingOwner_ = address(0); + _transferOwnership(pendingOwner_); } } From 917a0197586e9bb6d3c0b628b22659a1a261ca20 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 04:23:33 +0000 Subject: [PATCH 19/67] Improve encapsulation on payments --- contracts/ownership/Heritable.sol | 2 +- contracts/payment/RefundEscrow.sol | 38 +++++++++++------ contracts/payment/SplitPayment.sol | 67 +++++++++++++++++++++++------- test/payment/RefundEscrow.test.js | 4 ++ test/payment/SplitPayment.test.js | 6 +-- 5 files changed, 85 insertions(+), 32 deletions(-) diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index 9e3214c071f..fc3d561fcad 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -108,7 +108,7 @@ contract Heritable is Ownable { // solium-disable-next-line security/no-block-members require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); emit HeirOwnershipClaimed(getOwner(), heir_); - transferOwnership(heir_); + _transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index 9ba5fc60032..d9c11db5395 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -16,8 +16,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { event Closed(); event RefundsEnabled(); - State public state; - address public beneficiary; + State private state_; + address private beneficiary_; /** * @dev Constructor. @@ -25,8 +25,22 @@ contract RefundEscrow is Ownable, ConditionalEscrow { */ constructor(address _beneficiary) public { require(_beneficiary != address(0)); - beneficiary = _beneficiary; - state = State.Active; + beneficiary_ = _beneficiary; + state_ = State.Active; + } + + /** + * @return the current state of the escrow. + */ + function getState() public view returns(State) { + return state_; + } + + /** + * @return the beneficiary of the escrow. + */ + function getBeneficiary() public view returns(address) { + return beneficiary_; } /** @@ -34,7 +48,7 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @param _refundee The address funds will be sent to if a refund occurs. */ function deposit(address _refundee) public payable { - require(state == State.Active); + require(state_ == State.Active); super.deposit(_refundee); } @@ -43,8 +57,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * further deposits. */ function close() public onlyOwner { - require(state == State.Active); - state = State.Closed; + require(state_ == State.Active); + state_ = State.Closed; emit Closed(); } @@ -52,8 +66,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Allows for refunds to take place, rejecting further deposits. */ function enableRefunds() public onlyOwner { - require(state == State.Active); - state = State.Refunding; + require(state_ == State.Active); + state_ = State.Refunding; emit RefundsEnabled(); } @@ -61,14 +75,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Withdraws the beneficiary's funds. */ function beneficiaryWithdraw() public { - require(state == State.Closed); - beneficiary.transfer(address(this).balance); + require(state_ == State.Closed); + beneficiary_.transfer(address(this).balance); } /** * @dev Returns whether refundees can withdraw their deposits (be refunded). */ function withdrawalAllowed(address _payee) public view returns (bool) { - return state == State.Refunding; + return state_ == State.Refunding; } } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 04b01425683..8b1c53017de 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -11,12 +11,12 @@ import "../math/SafeMath.sol"; contract SplitPayment { using SafeMath for uint256; - uint256 public totalShares = 0; - uint256 public totalReleased = 0; + uint256 private totalShares_ = 0; + uint256 private totalReleased_ = 0; - mapping(address => uint256) public shares; - mapping(address => uint256) public released; - address[] public payees; + mapping(address => uint256) private shares_; + mapping(address => uint256) private released_; + address[] private payees_; /** * @dev Constructor @@ -35,26 +35,61 @@ contract SplitPayment { */ function () external payable {} + /** + * @return the total shares of the contract. + */ + function getTotalShares() public view returns(uint256) { + return totalShares_; + } + + /** + * @return the total amount already released. + */ + function getTotalReleased() public view returns(uint256) { + return totalReleased_; + } + + /** + * @return the shares of an account. + */ + function getShares(address _account) public view returns(uint256) { + return shares_[_account]; + } + + /** + * @return the amount already released to an account. + */ + function getReleased(address _account) public view returns(uint256) { + return released_[_account]; + } + + /** + * @return the address of a payee. + */ + function getPayee(uint256 index) public view returns(address) { + return payees_[index]; + } + /** * @dev Claim your share of the balance. */ function claim() public { address payee = msg.sender; - require(shares[payee] > 0); + require(shares_[payee] > 0); - uint256 totalReceived = address(this).balance.add(totalReleased); + uint256 totalReceived = address(this).balance.add(totalReleased_); uint256 payment = totalReceived.mul( - shares[payee]).div( - totalShares).sub( - released[payee] + shares_[payee]).div( + totalShares_).sub( + released_[payee] ); require(payment != 0); assert(address(this).balance >= payment); - released[payee] = released[payee].add(payment); - totalReleased = totalReleased.add(payment); + released_[payee] = released_[payee].add(payment); + totalReleased_ = totalReleased_.add(payment); payee.transfer(payment); } @@ -67,10 +102,10 @@ contract SplitPayment { function _addPayee(address _payee, uint256 _shares) internal { require(_payee != address(0)); require(_shares > 0); - require(shares[_payee] == 0); + require(shares_[_payee] == 0); - payees.push(_payee); - shares[_payee] = _shares; - totalShares = totalShares.add(_shares); + payees_.push(_payee); + shares_[_payee] = _shares; + totalShares_ = totalShares_.add(_shares); } } diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index e74d68f674b..d96413e4148 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -28,6 +28,10 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); context('active state', function () { + it('has beneficiary', async function () { + (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); + }); + it('accepts deposits', async function () { await this.escrow.deposit(refundee1, { from: owner, value: amount }); diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index bb09ea73cac..c713ff48cc6 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -53,11 +53,11 @@ 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.equal(0); + (await this.contract.getShares(payee1)).should.be.bignumber.not.equal(0); }); it('should not store shares if address is not payee', async function () { - (await this.contract.shares.call(nonpayee1)).should.be.bignumber.equal(0); + (await this.contract.getShares(nonpayee1)).should.be.bignumber.equal(0); }); it('should throw if no funds to claim', async function () { @@ -96,7 +96,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0); // check correct funds released accounting - (await this.contract.totalReleased.call()).should.be.bignumber.equal(initBalance); + (await this.contract.getTotalReleased()).should.be.bignumber.equal(initBalance); }); }); }); From 478d9741f0be068e102e169e09a52ad2ffb07adc Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Tue, 4 Sep 2018 04:55:07 +0000 Subject: [PATCH 20/67] Add missing tests --- test/payment/SplitPayment.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index c713ff48cc6..fcbf6c58af1 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -46,6 +46,17 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, this.contract = await SplitPayment.new(this.payees, this.shares); }); + it('should have total shares', async function () { + (await this.contract.getTotalShares()).should.be.bignumber.equal(20 + 10 + 70); + }); + + it('should have payees', async function () { + this.payees.forEach(async (payee, index) => { + (await this.getPayee(index)).should.be.equal(payee); + (await this.contract.getReleased(payee)).should.be.bignumber.equal(0); + }); + }); + it('should accept payments', async function () { await ethSendTransaction({ from: owner, to: this.contract.address, value: amount }); From 0fecbacc2fb68513d40884cb7dec96d6941c8934 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Tue, 4 Sep 2018 18:39:30 +0000 Subject: [PATCH 21/67] add missing test --- test/payment/RefundEscrow.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index d96413e4148..392d07603cb 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -28,8 +28,9 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); context('active state', function () { - it('has beneficiary', async function () { + it('has beneficiary and state', async function () { (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); + (await this.escrow.getState()).should.be.bignumber.equal(0); }); it('accepts deposits', async function () { From 9449572c3ec14ec7cbd679b54cf4e1ff3dd2030c Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 02:23:44 +0000 Subject: [PATCH 22/67] Do not prefix getters --- contracts/bounties/BreakInvariantBounty.sol | 2 +- contracts/drafts/TokenVesting.sol | 2 +- contracts/lifecycle/Pausable.sol | 2 +- contracts/ownership/CanReclaimToken.sol | 2 +- contracts/ownership/Claimable.sol | 2 +- contracts/ownership/DelayedClaimable.sol | 4 ++-- contracts/ownership/Heritable.sol | 10 +++++----- contracts/ownership/Ownable.sol | 2 +- contracts/ownership/Superuser.sol | 2 +- contracts/payment/RefundEscrow.sol | 4 ++-- contracts/payment/SplitPayment.sol | 10 +++++----- contracts/token/ERC20/ERC20Mintable.sol | 2 +- test/access/SignatureBouncer.test.js | 2 +- test/crowdsale/MintedCrowdsale.test.js | 2 +- test/ownership/Claimable.test.js | 6 +++--- test/ownership/DelayedClaimable.test.js | 20 ++++++++++---------- test/ownership/Ownable.behavior.js | 6 +++--- test/ownership/Superuser.test.js | 4 ++-- test/payment/RefundEscrow.test.js | 4 ++-- test/payment/SplitPayment.test.js | 10 +++++----- test/token/ERC20/ERC20Pausable.test.js | 10 +++++----- 21 files changed, 54 insertions(+), 54 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 1f330ffd378..5301a2d51a4 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -50,7 +50,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() public onlyOwner { - selfdestruct(getOwner()); + selfdestruct(owner()); } /** diff --git a/contracts/drafts/TokenVesting.sol b/contracts/drafts/TokenVesting.sol index 08f2c64d988..cfe7697d093 100644 --- a/contracts/drafts/TokenVesting.sol +++ b/contracts/drafts/TokenVesting.sol @@ -142,7 +142,7 @@ contract TokenVesting is Ownable { revoked_[_token] = true; - _token.safeTransfer(getOwner(), refund); + _token.safeTransfer(owner(), refund); emit Revoked(); } diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 3e5676f7f87..37caf1e0a52 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -18,7 +18,7 @@ contract Pausable is Ownable { /** * @return true if the contract is paused, false otherwise. */ - function isPaused() public view returns(bool) { + function paused() public view returns(bool) { return paused_; } diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index 11452cdeb9b..e4f7e88a638 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -20,7 +20,7 @@ contract CanReclaimToken is Ownable { */ function reclaimToken(IERC20 _token) external onlyOwner { uint256 balance = _token.balanceOf(this); - _token.safeTransfer(getOwner(), balance); + _token.safeTransfer(owner(), balance); } } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index fd7c2ffbb39..7c19d288076 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -23,7 +23,7 @@ contract Claimable is Ownable { /** * @return the address of the pending owner. */ - function getPendingOwner() public view returns(address) { + function pendingOwner() public view returns(address) { return pendingOwner_; } diff --git a/contracts/ownership/DelayedClaimable.sol b/contracts/ownership/DelayedClaimable.sol index b965856490a..b373e0067ea 100644 --- a/contracts/ownership/DelayedClaimable.sol +++ b/contracts/ownership/DelayedClaimable.sol @@ -16,14 +16,14 @@ contract DelayedClaimable is Claimable { /** * @return the start of the claimable period. */ - function getStart() public view returns(uint256) { + function start() public view returns(uint256) { return start_; } /** * @return the end of the claimable period. */ - function getEnd() public view returns(uint256) { + function end() public view returns(uint256) { return end_; } diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index fc3d561fcad..d015468f5ca 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -51,9 +51,9 @@ contract Heritable is Ownable { } function setHeir(address _newHeir) public onlyOwner { - require(_newHeir != getOwner()); + require(_newHeir != owner()); heartbeat(); - emit HeirChanged(getOwner(), _newHeir); + emit HeirChanged(owner(), _newHeir); heir_ = _newHeir; } @@ -87,7 +87,7 @@ contract Heritable is Ownable { */ function proclaimDeath() public onlyHeir { require(_ownerLives()); - emit OwnerProclaimedDead(getOwner(), heir_, timeOfDeath_); + emit OwnerProclaimedDead(owner(), heir_, timeOfDeath_); // solium-disable-next-line security/no-block-members timeOfDeath_ = block.timestamp; } @@ -96,7 +96,7 @@ contract Heritable is Ownable { * @dev Owner can send a heartbeat if they were mistakenly pronounced dead. */ function heartbeat() public onlyOwner { - emit OwnerHeartbeated(getOwner()); + emit OwnerHeartbeated(owner()); timeOfDeath_ = 0; } @@ -107,7 +107,7 @@ contract Heritable is Ownable { require(!_ownerLives()); // solium-disable-next-line security/no-block-members require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); - emit HeirOwnershipClaimed(getOwner(), heir_); + emit HeirOwnershipClaimed(owner(), heir_); _transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index 9a7d60d8978..f89013a1a53 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -28,7 +28,7 @@ contract Ownable { /** * @return the address of the owner. */ - function getOwner() public view returns(address) { + function owner() public view returns(address) { return owner_; } diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index c0fddf3ab5c..b1f1fa4b52d 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -27,7 +27,7 @@ contract Superuser is Ownable, RBAC { } modifier onlyOwnerOrSuperuser() { - require(msg.sender == getOwner() || isSuperuser(msg.sender)); + require(msg.sender == owner() || isSuperuser(msg.sender)); _; } diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index d9c11db5395..802dd471128 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -32,14 +32,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { /** * @return the current state of the escrow. */ - function getState() public view returns(State) { + function state() public view returns(State) { return state_; } /** * @return the beneficiary of the escrow. */ - function getBeneficiary() public view returns(address) { + function beneficiary() public view returns(address) { return beneficiary_; } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 8b1c53017de..1655b738383 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -38,35 +38,35 @@ contract SplitPayment { /** * @return the total shares of the contract. */ - function getTotalShares() public view returns(uint256) { + function totalShares() public view returns(uint256) { return totalShares_; } /** * @return the total amount already released. */ - function getTotalReleased() public view returns(uint256) { + function totalReleased() public view returns(uint256) { return totalReleased_; } /** * @return the shares of an account. */ - function getShares(address _account) public view returns(uint256) { + function shares(address _account) public view returns(uint256) { return shares_[_account]; } /** * @return the amount already released to an account. */ - function getReleased(address _account) public view returns(uint256) { + function released(address _account) public view returns(uint256) { return released_[_account]; } /** * @return the address of a payee. */ - function getPayee(uint256 index) public view returns(address) { + function payee(uint256 index) public view returns(address) { return payees_[index]; } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 93f0d28e977..518936699e4 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier hasMintPermission() { - require(msg.sender == getOwner()); + require(msg.sender == owner()); _; } diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js index 19143c850b0..798ab0ac9fa 100644 --- a/test/access/SignatureBouncer.test.js +++ b/test/access/SignatureBouncer.test.js @@ -20,7 +20,7 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] context('management', function () { it('has a default owner of self', async function () { - (await this.bouncer.getOwner()).should.equal(owner); + (await this.bouncer.owner()).should.equal(owner); }); it('allows the owner to add a bouncer', async function () { diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index 9f3c8204229..50a37eaef72 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -21,7 +21,7 @@ contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be token owner', async function () { - (await this.token.getOwner()).should.equal(this.crowdsale.address); + (await this.token.owner()).should.equal(this.crowdsale.address); }); shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js index 40659532676..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.getOwner()).should.not.equal(0); + (await claimable.owner()).should.not.equal(0); }); it('changes pendingOwner after transfer', async function () { await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.getPendingOwner()).should.equal(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.getOwner()).should.equal(newOwner); + (await claimable.owner()).should.equal(newOwner); }); }); }); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js index 039f1ae672c..e65ac86c41d 100644 --- a/test/ownership/DelayedClaimable.test.js +++ b/test/ownership/DelayedClaimable.test.js @@ -17,35 +17,35 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); await this.delayedClaimable.setLimits(0, 1000, { from: owner }); - (await this.delayedClaimable.getEnd()).should.be.bignumber.equal(1000); + (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); + (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.getEnd()).should.be.bignumber.equal(1000); + (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); + (await this.delayedClaimable.start()).should.be.bignumber.equal(0); - (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.getOwner()).should.equal(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.getEnd()).should.be.bignumber.equal(110); + (await this.delayedClaimable.end()).should.be.bignumber.equal(110); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(100); + (await this.delayedClaimable.start()).should.be.bignumber.equal(100); - (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.getOwner()).should.not.equal(newOwner); + (await this.delayedClaimable.owner()).should.not.equal(newOwner); }); it('set end and start invalid values fail', async function () { diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index ebb550b0f25..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.getOwner()).should.equal(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.getOwner()).should.equal(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.getOwner()).should.equal(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 3b4e6ede62d..5e4d2c32456 100644 --- a/test/ownership/Superuser.test.js +++ b/test/ownership/Superuser.test.js @@ -40,7 +40,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.getOwner()).should.equal(newOwner); + (await this.superuser.owner()).should.equal(newOwner); }); it('should change owner after the owner transfers the ownership', async function () { @@ -49,7 +49,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.getOwner()).should.equal(newOwner); + (await this.superuser.owner()).should.equal(newOwner); }); }); diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index 392d07603cb..7730d8242a0 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -29,8 +29,8 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] context('active state', function () { it('has beneficiary and state', async function () { - (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); - (await this.escrow.getState()).should.be.bignumber.equal(0); + (await this.escrow.beneficiary()).should.be.equal(beneficiary); + (await this.escrow.state()).should.be.bignumber.equal(0); }); it('accepts deposits', async function () { diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index fcbf6c58af1..f1f08601e3d 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -47,13 +47,13 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, }); it('should have total shares', async function () { - (await this.contract.getTotalShares()).should.be.bignumber.equal(20 + 10 + 70); + (await this.contract.totalShares()).should.be.bignumber.equal(20 + 10 + 70); }); it('should have payees', async function () { this.payees.forEach(async (payee, index) => { - (await this.getPayee(index)).should.be.equal(payee); - (await this.contract.getReleased(payee)).should.be.bignumber.equal(0); + (await this.payee(index)).should.be.equal(payee); + (await this.contract.released(payee)).should.be.bignumber.equal(0); }); }); @@ -64,11 +64,11 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, }); it('should store shares if address is payee', async function () { - (await this.contract.getShares(payee1)).should.be.bignumber.not.equal(0); + (await this.contract.shares(payee1)).should.be.bignumber.not.equal(0); }); it('should not store shares if address is not payee', async function () { - (await this.contract.getShares(nonpayee1)).should.be.bignumber.equal(0); + (await this.contract.shares(nonpayee1)).should.be.bignumber.equal(0); }); it('should throw if no funds to claim', async function () { diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index b11ecb3e7b6..8fbe4f70876 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -13,7 +13,7 @@ contract('ERC20Pausable', 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.isPaused()).should.equal(true); + (await this.token.paused()).should.equal(true); }); it('emits a Pause event', async function () { @@ -55,7 +55,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { it('unpauses the token', async function () { await this.token.unpause({ from }); - (await this.token.isPaused()).should.equal(false); + (await this.token.paused()).should.equal(false); }); it('emits an Unpause event', async function () { @@ -87,18 +87,18 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { describe('paused', function () { it('is not paused by default', async function () { - (await this.token.isPaused({ from })).should.equal(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.isPaused({ from })).should.equal(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.isPaused()).should.equal(false); + (await this.token.paused()).should.equal(false); }); }); From 1856f07ff33e7918271d0b48bd0bca683075c25e Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 03:02:17 +0000 Subject: [PATCH 23/67] Fix tests. --- test/payment/SplitPayment.test.js | 2 +- test/token/ERC20/ERC20Mintable.behavior.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index f1f08601e3d..a284530996a 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -107,7 +107,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0); // check correct funds released accounting - (await this.contract.getTotalReleased()).should.be.bignumber.equal(initBalance); + (await this.contract.totalReleased()).should.be.bignumber.equal(initBalance); }); }); }); diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index cb982ed26f1..63fb861b551 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -13,7 +13,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { describe('as a basic mintable token', function () { describe('after token creation', function () { it('sender should be token owner', async function () { - (await this.token.getOwner({ from: owner })).should.equal(owner); + (await this.token.owner({ from: owner })).should.equal(owner); }); }); From f61acdcd51c554ba067c2455090419ea75720503 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 01:24:02 +0000 Subject: [PATCH 24/67] Improve encapsulation on Crowdsales --- contracts/crowdsale/Crowdsale.sol | 58 ++++++++++++++----- .../distribution/FinalizableCrowdsale.sol | 13 ++++- .../distribution/PostDeliveryCrowdsale.sol | 15 +++-- .../distribution/RefundableCrowdsale.sol | 17 ++++-- .../crowdsale/emission/AllowanceCrowdsale.sol | 15 +++-- .../crowdsale/emission/MintedCrowdsale.sol | 3 +- .../price/IncreasingPriceCrowdsale.sol | 26 +++++++-- .../crowdsale/validation/CappedCrowdsale.sol | 15 +++-- .../IndividuallyCappedCrowdsale.sol | 18 +++--- test/examples/SampleCrowdsale.test.js | 8 +-- 10 files changed, 134 insertions(+), 54 deletions(-) diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index 8bf055ada87..adfb59e95ea 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -22,19 +22,16 @@ contract Crowdsale { using SafeERC20 for IERC20; // The token being sold - IERC20 public token; + IERC20 private token_; // Address where funds are collected - address public wallet; + address private 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 ERC20Detailed token with 3 decimals called TOK - // 1 wei will give you 1 unit, or 0.001 TOK. - uint256 public rate; + uint256 private rate_; // Amount of wei raised - uint256 public weiRaised; + uint256 private weiRaised_; /** * Event for token purchase logging @@ -52,6 +49,9 @@ contract Crowdsale { /** * @param _rate Number of token units a buyer gets per wei + * @dev 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 ERC20Detailed token + * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK. * @param _wallet Address where collected funds will be forwarded to * @param _token Address of the token being sold */ @@ -60,9 +60,9 @@ contract Crowdsale { require(_wallet != address(0)); require(_token != address(0)); - rate = _rate; - wallet = _wallet; - token = _token; + rate_ = _rate; + wallet_ = _wallet; + token_ = _token; } // ----------------------------------------- @@ -76,6 +76,34 @@ contract Crowdsale { buyTokens(msg.sender); } + /** + * @return the token being sold. + */ + function getToken() public view returns(IERC20) { + return token_; + } + + /** + * @return the address where funds are collected. + */ + function getWallet() public view returns(address) { + return wallet_; + } + + /** + * @return the number of token units a buyer gets per wei. + */ + function getRate() public view returns(uint256) { + return rate_; + } + + /** + * @return the mount of wei raised. + */ + function getWeiRaised() public view returns (uint256) { + return weiRaised_; + } + /** * @dev low level token purchase ***DO NOT OVERRIDE*** * @param _beneficiary Address performing the token purchase @@ -89,7 +117,7 @@ contract Crowdsale { uint256 tokens = _getTokenAmount(weiAmount); // update state - weiRaised = weiRaised.add(weiAmount); + weiRaised_ = weiRaised_.add(weiAmount); _processPurchase(_beneficiary, tokens); emit TokensPurchased( @@ -113,7 +141,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: * super._preValidatePurchase(_beneficiary, _weiAmount); - * require(weiRaised.add(_weiAmount) <= cap); + * require(getWeiRaised().add(_weiAmount) <= cap); * @param _beneficiary Address performing the token purchase * @param _weiAmount Value in wei involved in the purchase */ @@ -152,7 +180,7 @@ contract Crowdsale { ) internal { - token.safeTransfer(_beneficiary, _tokenAmount); + token_.safeTransfer(_beneficiary, _tokenAmount); } /** @@ -191,13 +219,13 @@ contract Crowdsale { function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) { - return _weiAmount.mul(rate); + return _weiAmount.mul(rate_); } /** * @dev Determines how ETH is stored/forwarded on purchases. */ function _forwardFunds() internal { - wallet.transfer(msg.value); + wallet_.transfer(msg.value); } } diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 651069d41af..42d37b8ee15 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -13,22 +13,29 @@ import "../validation/TimedCrowdsale.sol"; contract FinalizableCrowdsale is Ownable, TimedCrowdsale { using SafeMath for uint256; - bool public isFinalized = false; + bool private finalized_ = false; event CrowdsaleFinalized(); + /** + * @return true if the crowdsale is finalized, false otherwise. + */ + function isFinalized() public view returns(bool) { + return finalized_; + } + /** * @dev Must be called after crowdsale ends, to do some extra finalization * work. Calls the contract's finalization function. */ function finalize() public onlyOwner { - require(!isFinalized); + require(!finalized_); require(hasClosed()); _finalization(); emit CrowdsaleFinalized(); - isFinalized = true; + finalized_ = true; } /** diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index b09d09709a5..6971669aedb 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -12,19 +12,26 @@ import "../../math/SafeMath.sol"; contract PostDeliveryCrowdsale is TimedCrowdsale { using SafeMath for uint256; - mapping(address => uint256) public balances; + mapping(address => uint256) private balances_; /** * @dev Withdraw tokens only after crowdsale ends. */ function withdrawTokens() public { require(hasClosed()); - uint256 amount = balances[msg.sender]; + uint256 amount = balances_[msg.sender]; require(amount > 0); - balances[msg.sender] = 0; + balances_[msg.sender] = 0; _deliverTokens(msg.sender, amount); } + /** + * @return the balance of an account. + */ + function getBalance(address _account) public view returns(uint256) { + return balances_[_account]; + } + /** * @dev Overrides parent by storing balances instead of issuing tokens right away. * @param _beneficiary Token purchaser @@ -36,7 +43,7 @@ contract PostDeliveryCrowdsale is TimedCrowdsale { ) internal { - balances[_beneficiary] = balances[_beneficiary].add(_tokenAmount); + balances_[_beneficiary] = balances_[_beneficiary].add(_tokenAmount); } } diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 05f95ca8584..690afd2910d 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -15,7 +15,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { using SafeMath for uint256; // minimum amount of funds to be raised in weis - uint256 public goal; + uint256 private goal_; // refund escrow used to hold funds while crowdsale is running RefundEscrow private escrow_; @@ -26,15 +26,22 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ constructor(uint256 _goal) public { require(_goal > 0); - escrow_ = new RefundEscrow(wallet); - goal = _goal; + escrow_ = new RefundEscrow(getWallet()); + goal_ = _goal; + } + + /** + * @return minimum amount of funds to be raised in wei. + */ + function getGoal() public view returns(uint256) { + return goal_; } /** * @dev Investors can claim refunds here if crowdsale is unsuccessful */ function claimRefund() public { - require(isFinalized); + require(isFinalized()); require(!goalReached()); escrow_.withdraw(msg.sender); @@ -45,7 +52,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @return Whether funding goal was reached */ function goalReached() public view returns (bool) { - return weiRaised >= goal; + return getWeiRaised() >= goal_; } /** diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index 4e48638ab74..776933948ac 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -14,7 +14,7 @@ contract AllowanceCrowdsale is Crowdsale { using SafeMath for uint256; using SafeERC20 for IERC20; - address public tokenWallet; + address private tokenWallet_; /** * @dev Constructor, takes token wallet address. @@ -22,7 +22,14 @@ contract AllowanceCrowdsale is Crowdsale { */ constructor(address _tokenWallet) public { require(_tokenWallet != address(0)); - tokenWallet = _tokenWallet; + tokenWallet_ = _tokenWallet; + } + + /** + * @return the address of the wallet that will hold the tokens. + */ + function getTokenWallet() public view returns(address) { + return tokenWallet_; } /** @@ -30,7 +37,7 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return token.allowance(tokenWallet, this); + return getToken().allowance(tokenWallet_, this); } /** @@ -44,6 +51,6 @@ contract AllowanceCrowdsale is Crowdsale { ) internal { - token.safeTransferFrom(tokenWallet, _beneficiary, _tokenAmount); + getToken().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); } } diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 9f3a8b4b115..5718fca1ede 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -23,6 +23,7 @@ contract MintedCrowdsale is Crowdsale { internal { // Potentially dangerous assumption about the type of the token. - require(ERC20Mintable(address(token)).mint(_beneficiary, _tokenAmount)); + require( + ERC20Mintable(address(getToken())).mint(_beneficiary, _tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index 841f09bc36b..dd75917873f 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -13,8 +13,8 @@ import "../../math/SafeMath.sol"; contract IncreasingPriceCrowdsale is TimedCrowdsale { using SafeMath for uint256; - uint256 public initialRate; - uint256 public finalRate; + uint256 private initialRate_; + uint256 private finalRate_; /** * @dev Constructor, takes initial and final rates of tokens received per wei contributed. @@ -24,8 +24,22 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { constructor(uint256 _initialRate, uint256 _finalRate) public { require(_finalRate > 0); require(_initialRate >= _finalRate); - initialRate = _initialRate; - finalRate = _finalRate; + initialRate_ = _initialRate; + finalRate_ = _finalRate; + } + + /** + * @return the initial rate of the crowdsale. + */ + function getInitialRate() public view returns(uint256) { + return initialRate_; + } + + /** + * @return the final rate of the crowdsale. + */ + function getFinalRate() public view returns (uint256) { + return finalRate_; } /** @@ -37,8 +51,8 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { // solium-disable-next-line security/no-block-members uint256 elapsedTime = block.timestamp.sub(openingTime); uint256 timeRange = closingTime.sub(openingTime); - uint256 rateRange = initialRate.sub(finalRate); - return initialRate.sub(elapsedTime.mul(rateRange).div(timeRange)); + uint256 rateRange = initialRate_.sub(finalRate_); + return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange)); } /** diff --git a/contracts/crowdsale/validation/CappedCrowdsale.sol b/contracts/crowdsale/validation/CappedCrowdsale.sol index 1df0e3eb311..9e158143e04 100644 --- a/contracts/crowdsale/validation/CappedCrowdsale.sol +++ b/contracts/crowdsale/validation/CappedCrowdsale.sol @@ -11,7 +11,7 @@ import "../Crowdsale.sol"; contract CappedCrowdsale is Crowdsale { using SafeMath for uint256; - uint256 public cap; + uint256 private cap_; /** * @dev Constructor, takes maximum amount of wei accepted in the crowdsale. @@ -19,7 +19,14 @@ contract CappedCrowdsale is Crowdsale { */ constructor(uint256 _cap) public { require(_cap > 0); - cap = _cap; + cap_ = _cap; + } + + /** + * @return the cap of the crowdsale. + */ + function getCap() public view returns(uint256) { + return cap_; } /** @@ -27,7 +34,7 @@ contract CappedCrowdsale is Crowdsale { * @return Whether the cap was reached */ function capReached() public view returns (bool) { - return weiRaised >= cap; + return getWeiRaised() >= cap_; } /** @@ -42,7 +49,7 @@ contract CappedCrowdsale is Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(weiRaised.add(_weiAmount) <= cap); + require(getWeiRaised().add(_weiAmount) <= cap_); } } diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index 3c7884c658a..f0c3fdd4c43 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -12,8 +12,8 @@ import "../../ownership/Ownable.sol"; contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { using SafeMath for uint256; - mapping(address => uint256) public contributions; - mapping(address => uint256) public caps; + mapping(address => uint256) private contributions_; + mapping(address => uint256) private caps_; /** * @dev Sets a specific user's maximum contribution. @@ -21,7 +21,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { * @param _cap Wei limit for individual contribution */ function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner { - caps[_beneficiary] = _cap; + caps_[_beneficiary] = _cap; } /** @@ -37,7 +37,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { onlyOwner { for (uint256 i = 0; i < _beneficiaries.length; i++) { - caps[_beneficiaries[i]] = _cap; + caps_[_beneficiaries[i]] = _cap; } } @@ -47,7 +47,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { * @return Current cap for individual user */ function getUserCap(address _beneficiary) public view returns (uint256) { - return caps[_beneficiary]; + return caps_[_beneficiary]; } /** @@ -58,7 +58,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { function getUserContribution(address _beneficiary) public view returns (uint256) { - return contributions[_beneficiary]; + return contributions_[_beneficiary]; } /** @@ -73,7 +73,8 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(contributions[_beneficiary].add(_weiAmount) <= caps[_beneficiary]); + require( + contributions_[_beneficiary].add(_weiAmount) <= caps_[_beneficiary]); } /** @@ -88,7 +89,8 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { internal { super._updatePurchasingState(_beneficiary, _weiAmount); - contributions[_beneficiary] = contributions[_beneficiary].add(_weiAmount); + contributions_[_beneficiary] = contributions_[_beneficiary].add( + _weiAmount); } } diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index f7b88080fb3..bfca735a992 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -45,10 +45,10 @@ contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime); (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime); - (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); - (await this.crowdsale.wallet()).should.be.equal(wallet); - (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); - (await this.crowdsale.cap()).should.be.bignumber.equal(CAP); + (await this.crowdsale.getRate()).should.be.bignumber.equal(RATE); + (await this.crowdsale.getWallet()).should.be.equal(wallet); + (await this.crowdsale.getGoal()).should.be.bignumber.equal(GOAL); + (await this.crowdsale.getCap()).should.be.bignumber.equal(CAP); }); it('should not accept payments before start', async function () { From 44d113ab2e3642c6a9015abc11be385a10f4b9f2 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 02:31:01 +0000 Subject: [PATCH 25/67] add missing tests --- test/crowdsale/AllowanceCrowdsale.test.js | 4 ++++ test/crowdsale/IncreasingPriceCrowdsale.test.js | 5 +++++ test/crowdsale/PostDeliveryCrowdsale.test.js | 2 ++ 3 files changed, 11 insertions(+) diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index b0d68a9b15c..7a49f7e827c 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -25,6 +25,10 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); describe('accepting payments', function () { + it.only('should have token wallet', async function () { + (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); + }); + it('should accept sends', async function () { await this.crowdsale.send(value); }); diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index e68d451bd46..ef647c18a28 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -55,6 +55,11 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) await this.token.transfer(this.crowdsale.address, tokenSupply); }); + it('should have initial and final rate', async function () { + (await this.crowdsale.getInitialRate()).should.be.bignumber.equal(initialRate); + (await this.crowdsale.getFinalRate()).should.be.bignumber.equal(finalRate); + }); + it('at start', async function () { await increaseTimeTo(this.startTime); await this.crowdsale.buyTokens(investor, { value, from: purchaser }); diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index ebc41111c3d..843ea0b73c4 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -47,6 +47,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('does not immediately assign tokens to beneficiaries', async function () { + (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(value); (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); }); @@ -61,6 +62,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { it('allows beneficiaries to withdraw tokens', async function () { await this.crowdsale.withdrawTokens({ from: investor }); + (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(0); (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); }); From 02576701372e1c4fe38c1b587f5063a1d31bef1d Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 02:49:14 +0000 Subject: [PATCH 26/67] remove only --- test/crowdsale/AllowanceCrowdsale.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 7a49f7e827c..64988d0f112 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -25,7 +25,7 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); describe('accepting payments', function () { - it.only('should have token wallet', async function () { + it('should have token wallet', async function () { (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); }); From 5a476d14e548595217d9c5a9ddf0e82b51618281 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 02:09:36 +0000 Subject: [PATCH 27/67] Do not prefix getters --- contracts/crowdsale/Crowdsale.sol | 10 +++++----- .../crowdsale/distribution/FinalizableCrowdsale.sol | 2 +- .../crowdsale/distribution/PostDeliveryCrowdsale.sol | 2 +- .../crowdsale/distribution/RefundableCrowdsale.sol | 8 ++++---- contracts/crowdsale/emission/AllowanceCrowdsale.sol | 6 +++--- contracts/crowdsale/emission/MintedCrowdsale.sol | 2 +- contracts/crowdsale/price/IncreasingPriceCrowdsale.sol | 4 ++-- contracts/crowdsale/validation/CappedCrowdsale.sol | 6 +++--- test/crowdsale/AllowanceCrowdsale.test.js | 2 +- test/crowdsale/IncreasingPriceCrowdsale.test.js | 4 ++-- test/crowdsale/PostDeliveryCrowdsale.test.js | 4 ++-- test/examples/SampleCrowdsale.test.js | 8 ++++---- 12 files changed, 29 insertions(+), 29 deletions(-) diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index adfb59e95ea..0b133c0aa28 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -79,28 +79,28 @@ contract Crowdsale { /** * @return the token being sold. */ - function getToken() public view returns(IERC20) { + function token() public view returns(IERC20) { return token_; } /** * @return the address where funds are collected. */ - function getWallet() public view returns(address) { + function wallet() public view returns(address) { return wallet_; } /** * @return the number of token units a buyer gets per wei. */ - function getRate() public view returns(uint256) { + function rate() public view returns(uint256) { return rate_; } /** * @return the mount of wei raised. */ - function getWeiRaised() public view returns (uint256) { + function weiRaised() public view returns (uint256) { return weiRaised_; } @@ -141,7 +141,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: * super._preValidatePurchase(_beneficiary, _weiAmount); - * require(getWeiRaised().add(_weiAmount) <= cap); + * require(weiRaised().add(_weiAmount) <= cap); * @param _beneficiary Address performing the token purchase * @param _weiAmount Value in wei involved in the purchase */ diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 42d37b8ee15..693e0b498e1 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -20,7 +20,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { /** * @return true if the crowdsale is finalized, false otherwise. */ - function isFinalized() public view returns(bool) { + function finalized() public view returns(bool) { return finalized_; } diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index 6971669aedb..382c6210c89 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -28,7 +28,7 @@ contract PostDeliveryCrowdsale is TimedCrowdsale { /** * @return the balance of an account. */ - function getBalance(address _account) public view returns(uint256) { + function balanceOf(address _account) public view returns(uint256) { return balances_[_account]; } diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 690afd2910d..8ffcfb592c6 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -26,14 +26,14 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ constructor(uint256 _goal) public { require(_goal > 0); - escrow_ = new RefundEscrow(getWallet()); + escrow_ = new RefundEscrow(wallet()); goal_ = _goal; } /** * @return minimum amount of funds to be raised in wei. */ - function getGoal() public view returns(uint256) { + function goal() public view returns(uint256) { return goal_; } @@ -41,7 +41,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @dev Investors can claim refunds here if crowdsale is unsuccessful */ function claimRefund() public { - require(isFinalized()); + require(finalized()); require(!goalReached()); escrow_.withdraw(msg.sender); @@ -52,7 +52,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @return Whether funding goal was reached */ function goalReached() public view returns (bool) { - return getWeiRaised() >= goal_; + return weiRaised() >= goal_; } /** diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index 776933948ac..54db50af1c9 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -28,7 +28,7 @@ contract AllowanceCrowdsale is Crowdsale { /** * @return the address of the wallet that will hold the tokens. */ - function getTokenWallet() public view returns(address) { + function tokenWallet() public view returns(address) { return tokenWallet_; } @@ -37,7 +37,7 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return getToken().allowance(tokenWallet_, this); + return token().allowance(tokenWallet_, this); } /** @@ -51,6 +51,6 @@ contract AllowanceCrowdsale is Crowdsale { ) internal { - getToken().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); + token().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); } } diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 5718fca1ede..1b676e07767 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -24,6 +24,6 @@ contract MintedCrowdsale is Crowdsale { { // Potentially dangerous assumption about the type of the token. require( - ERC20Mintable(address(getToken())).mint(_beneficiary, _tokenAmount)); + ERC20Mintable(address(token())).mint(_beneficiary, _tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index dd75917873f..7cc07f083d5 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -31,14 +31,14 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { /** * @return the initial rate of the crowdsale. */ - function getInitialRate() public view returns(uint256) { + function initialRate() public view returns(uint256) { return initialRate_; } /** * @return the final rate of the crowdsale. */ - function getFinalRate() public view returns (uint256) { + function finalRate() public view returns (uint256) { return finalRate_; } diff --git a/contracts/crowdsale/validation/CappedCrowdsale.sol b/contracts/crowdsale/validation/CappedCrowdsale.sol index 9e158143e04..99b188a9883 100644 --- a/contracts/crowdsale/validation/CappedCrowdsale.sol +++ b/contracts/crowdsale/validation/CappedCrowdsale.sol @@ -25,7 +25,7 @@ contract CappedCrowdsale is Crowdsale { /** * @return the cap of the crowdsale. */ - function getCap() public view returns(uint256) { + function cap() public view returns(uint256) { return cap_; } @@ -34,7 +34,7 @@ contract CappedCrowdsale is Crowdsale { * @return Whether the cap was reached */ function capReached() public view returns (bool) { - return getWeiRaised() >= cap_; + return weiRaised() >= cap_; } /** @@ -49,7 +49,7 @@ contract CappedCrowdsale is Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(getWeiRaised().add(_weiAmount) <= cap_); + require(weiRaised().add(_weiAmount) <= cap_); } } diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 64988d0f112..ee6a063f2bd 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -26,7 +26,7 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW describe('accepting payments', function () { it('should have token wallet', async function () { - (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); + (await this.crowdsale.tokenWallet()).should.be.equal(tokenWallet); }); it('should accept sends', async function () { diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index ef647c18a28..c62c17d89bc 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -56,8 +56,8 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) }); it('should have initial and final rate', async function () { - (await this.crowdsale.getInitialRate()).should.be.bignumber.equal(initialRate); - (await this.crowdsale.getFinalRate()).should.be.bignumber.equal(finalRate); + (await this.crowdsale.initialRate()).should.be.bignumber.equal(initialRate); + (await this.crowdsale.finalRate()).should.be.bignumber.equal(finalRate); }); it('at start', async function () { diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index 843ea0b73c4..11058928ade 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -47,7 +47,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('does not immediately assign tokens to beneficiaries', async function () { - (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(value); + (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value); (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); }); @@ -62,7 +62,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { it('allows beneficiaries to withdraw tokens', async function () { await this.crowdsale.withdrawTokens({ from: investor }); - (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(0); + (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(0); (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); }); diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index bfca735a992..f7b88080fb3 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -45,10 +45,10 @@ contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime); (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime); - (await this.crowdsale.getRate()).should.be.bignumber.equal(RATE); - (await this.crowdsale.getWallet()).should.be.equal(wallet); - (await this.crowdsale.getGoal()).should.be.bignumber.equal(GOAL); - (await this.crowdsale.getCap()).should.be.bignumber.equal(CAP); + (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); + (await this.crowdsale.wallet()).should.be.equal(wallet); + (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); + (await this.crowdsale.cap()).should.be.bignumber.equal(CAP); }); it('should not accept payments before start', async function () { From 85a0fc3fdea37d7f9cdd266ce050ddb7d6d9ebb0 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 03:45:14 +0000 Subject: [PATCH 28/67] Update modifiers to call public view functions. Fixes #1179. --- .../crowdsale/validation/TimedCrowdsale.sol | 10 +++++++- contracts/ownership/Ownable.sol | 9 ++++++- contracts/token/ERC20/ERC20Mintable.sol | 24 ++++++++++++++----- contracts/token/ERC20/RBACMintableToken.sol | 2 +- test/crowdsale/TimedCrowdsale.test.js | 2 ++ test/ownership/Ownable.behavior.js | 3 +++ 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/contracts/crowdsale/validation/TimedCrowdsale.sol b/contracts/crowdsale/validation/TimedCrowdsale.sol index 52b7f4ef3e0..be2dd7e81db 100644 --- a/contracts/crowdsale/validation/TimedCrowdsale.sol +++ b/contracts/crowdsale/validation/TimedCrowdsale.sol @@ -19,7 +19,7 @@ contract TimedCrowdsale is Crowdsale { */ modifier onlyWhileOpen { // solium-disable-next-line security/no-block-members - require(block.timestamp >= openingTime && block.timestamp <= closingTime); + require(isOpen()); _; } @@ -37,6 +37,14 @@ contract TimedCrowdsale is Crowdsale { closingTime = _closingTime; } + /** + * @return true if the crowdsale is open, false otherwise. + */ + function isOpen() public view returns (bool) { + // solium-disable-next-line security/no-block-members + return block.timestamp >= openingTime && block.timestamp <= closingTime; + } + /** * @dev Checks whether the period in which the crowdsale is open has already elapsed. * @return Whether crowdsale period has elapsed diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index f89013a1a53..c6ea3e16fa0 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -36,10 +36,17 @@ contract Ownable { * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { - require(msg.sender == owner_); + require(isOwner()); _; } + /** + * @return true if `msg.sender` is the owner of the contract. + */ + function isOwner() public view returns(bool) { + return msg.sender == owner_; + } + /** * @dev Allows the current owner to relinquish control of the contract. * @notice Renouncing to ownership will leave the contract without an owner. diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 518936699e4..0ecabe3f4d1 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -16,13 +16,13 @@ contract ERC20Mintable is ERC20, Ownable { bool private mintingFinished_ = false; - modifier canMint() { + modifier onlyBeforeMintingFinished() { require(!mintingFinished_); _; } - modifier hasMintPermission() { - require(msg.sender == owner()); + modifier onlyMinter() { + require(isMinter()); _; } @@ -33,6 +33,13 @@ contract ERC20Mintable is ERC20, Ownable { return mintingFinished_; } + /** + * @return true if `msg.sender` is the minter. + */ + function isMinter() public view returns(bool) { + return msg.sender == owner(); + } + /** * @dev Function to mint tokens * @param _to The address that will receive the minted tokens. @@ -44,8 +51,8 @@ contract ERC20Mintable is ERC20, Ownable { uint256 _amount ) public - hasMintPermission - canMint + onlyMinter + onlyBeforeMintingFinished returns (bool) { _mint(_to, _amount); @@ -57,7 +64,12 @@ contract ERC20Mintable is ERC20, 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 + onlyOwner + onlyBeforeMintingFinished + returns (bool) + { mintingFinished_ = true; emit MintFinished(); return true; diff --git a/contracts/token/ERC20/RBACMintableToken.sol b/contracts/token/ERC20/RBACMintableToken.sol index ad6ab19ca58..65e16f564f8 100644 --- a/contracts/token/ERC20/RBACMintableToken.sol +++ b/contracts/token/ERC20/RBACMintableToken.sol @@ -18,7 +18,7 @@ contract RBACMintableToken is ERC20Mintable, RBAC { /** * @dev override the Mintable token modifier to add role based logic */ - modifier hasMintPermission() { + modifier onlyMinter() { checkRole(msg.sender, ROLE_MINTER); _; } diff --git a/test/crowdsale/TimedCrowdsale.test.js b/test/crowdsale/TimedCrowdsale.test.js index ad9ca29f556..5c246482f4a 100644 --- a/test/crowdsale/TimedCrowdsale.test.js +++ b/test/crowdsale/TimedCrowdsale.test.js @@ -50,8 +50,10 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be ended only after end', async function () { + (await this.crowdsale.isOpen()).should.equal(true); (await this.crowdsale.hasClosed()).should.equal(false); await increaseTimeTo(this.afterClosingTime); + (await this.crowdsale.isOpen()).should.equal(false); (await this.crowdsale.hasClosed()).should.equal(true); }); diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index 6257c01df5a..eac3c10ce81 100644 --- a/test/ownership/Ownable.behavior.js +++ b/test/ownership/Ownable.behavior.js @@ -13,8 +13,11 @@ function shouldBehaveLikeOwnable (owner, [anyone]) { }); it('changes owner after transfer', async function () { + (await this.ownable.isOwner({ from: anyone })).should.be.equal(false); await this.ownable.transferOwnership(anyone, { from: owner }); + (await this.ownable.owner()).should.equal(anyone); + (await this.ownable.isOwner({ from: anyone })).should.be.equal(true); }); it('should prevent non-owners from transfering', async function () { From c42056380b547b632508dd5061583a6258c49a32 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:11:24 +0000 Subject: [PATCH 29/67] Improve encapsulation on Pausable --- contracts/lifecycle/Pausable.sol | 9 ++++++++- test/token/ERC20/ERC20Pausable.test.js | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 0a2767aa61f..03dff6692ee 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -12,9 +12,16 @@ contract Pausable is Ownable { event Paused(); event Unpaused(); - bool public paused = false; + bool private paused = false; + /** + * @return true if the contract is paused, false otherwise. + */ + function isPaused() public view returns(bool) { + return paused; + } + /** * @dev Modifier to make a function callable only when the contract is not paused. */ diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index 8fbe4f70876..b11ecb3e7b6 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -13,7 +13,7 @@ contract('ERC20Pausable', 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.equal(true); + (await this.token.isPaused()).should.equal(true); }); it('emits a Pause event', async function () { @@ -55,7 +55,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { it('unpauses the token', async function () { await this.token.unpause({ from }); - (await this.token.paused()).should.equal(false); + (await this.token.isPaused()).should.equal(false); }); it('emits an Unpause event', async function () { @@ -87,18 +87,18 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { describe('paused', function () { it('is not paused by default', async function () { - (await this.token.paused({ from })).should.equal(false); + (await this.token.isPaused({ from })).should.equal(false); }); it('is paused after being paused', async function () { await this.token.pause({ from }); - (await this.token.paused({ from })).should.equal(true); + (await this.token.isPaused({ 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.equal(false); + (await this.token.isPaused()).should.equal(false); }); }); From fcd369a13aaba8bc5aa3297ac1c2ed6550081d55 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:13:48 +0000 Subject: [PATCH 30/67] add the underscore --- contracts/lifecycle/Pausable.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 03dff6692ee..3e5676f7f87 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -12,21 +12,21 @@ contract Pausable is Ownable { event Paused(); event Unpaused(); - bool private paused = false; + bool private paused_ = false; /** * @return true if the contract is paused, false otherwise. */ function isPaused() public view returns(bool) { - return paused; + return paused_; } /** * @dev Modifier to make a function callable only when the contract is not paused. */ modifier whenNotPaused() { - require(!paused); + require(!paused_); _; } @@ -34,7 +34,7 @@ contract Pausable is Ownable { * @dev Modifier to make a function callable only when the contract is paused. */ modifier whenPaused() { - require(paused); + require(paused_); _; } @@ -42,7 +42,7 @@ contract Pausable is Ownable { * @dev called by the owner to pause, triggers stopped state */ function pause() public onlyOwner whenNotPaused { - paused = true; + paused_ = true; emit Paused(); } @@ -50,7 +50,7 @@ contract Pausable is Ownable { * @dev called by the owner to unpause, returns to normal state */ function unpause() public onlyOwner whenPaused { - paused = false; + paused_ = false; emit Unpaused(); } } From 1eb13408619133da53e195c8017c429779d89b27 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:44:22 +0000 Subject: [PATCH 31/67] Improve encapsulation on ownership --- contracts/drafts/TokenVesting.sol | 2 +- contracts/ownership/Claimable.sol | 19 +++++++++---- contracts/ownership/DelayedClaimable.sol | 32 +++++++++++++++------- contracts/ownership/Heritable.sol | 13 ++++----- contracts/ownership/Ownable.sol | 19 +++++++++---- contracts/ownership/Superuser.sol | 2 +- contracts/token/ERC20/ERC20Mintable.sol | 2 +- test/access/SignatureBouncer.test.js | 2 +- test/crowdsale/MintedCrowdsale.test.js | 2 +- test/ownership/Claimable.test.js | 6 ++-- test/ownership/DelayedClaimable.test.js | 20 +++++++------- test/ownership/Ownable.behavior.js | 6 ++-- test/ownership/Superuser.test.js | 4 +-- test/token/ERC20/ERC20Mintable.behavior.js | 2 +- 14 files changed, 78 insertions(+), 53 deletions(-) diff --git a/contracts/drafts/TokenVesting.sol b/contracts/drafts/TokenVesting.sol index cc90e24940b..08f2c64d988 100644 --- a/contracts/drafts/TokenVesting.sol +++ b/contracts/drafts/TokenVesting.sol @@ -142,7 +142,7 @@ contract TokenVesting is Ownable { revoked_[_token] = true; - _token.safeTransfer(owner, refund); + _token.safeTransfer(getOwner(), refund); emit Revoked(); } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index 9027c11f52d..6576632af12 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -10,30 +10,37 @@ import "./Ownable.sol"; * This allows the new owner to accept the transfer. */ contract Claimable is Ownable { - address public pendingOwner; + address private pendingOwner_; /** * @dev Modifier throws if called by any account other than the pendingOwner. */ modifier onlyPendingOwner() { - require(msg.sender == pendingOwner); + require(msg.sender == pendingOwner_); _; } + /** + * @return the address of the pending owner. + */ + function getPendingOwner() public view returns(address) { + return 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; + pendingOwner_ = newOwner; } /** * @dev Allows the pendingOwner address to finalize the transfer. */ function claimOwnership() public onlyPendingOwner { - emit OwnershipTransferred(owner, pendingOwner); - owner = pendingOwner; - pendingOwner = address(0); + emit OwnershipTransferred(getOwner(), pendingOwner_); + owner = pendingOwner_; + pendingOwner_ = address(0); } } diff --git a/contracts/ownership/DelayedClaimable.sol b/contracts/ownership/DelayedClaimable.sol index a9c0857f8e9..b965856490a 100644 --- a/contracts/ownership/DelayedClaimable.sol +++ b/contracts/ownership/DelayedClaimable.sol @@ -10,8 +10,22 @@ import "./Claimable.sol"; */ contract DelayedClaimable is Claimable { - uint256 public end; - uint256 public start; + uint256 private start_; + uint256 private end_; + + /** + * @return the start of the claimable period. + */ + function getStart() public view returns(uint256) { + return start_; + } + + /** + * @return the end of the claimable period. + */ + function getEnd() public view returns(uint256) { + return end_; + } /** * @dev Used to specify the time period during which a pending @@ -21,20 +35,18 @@ contract DelayedClaimable is Claimable { */ function setLimits(uint256 _start, uint256 _end) public onlyOwner { require(_start <= _end); - end = _end; - start = _start; + end_ = _end; + start_ = _start; } /** - * @dev Allows the pendingOwner address to finalize the transfer, as long as it is called within + * @dev Allows the pending owner 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; + require((block.number <= end_) && (block.number >= start_)); + super.claimOwnership(); + end_ = 0; } } diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index 88e6fee62f4..9e3214c071f 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -51,9 +51,9 @@ contract Heritable is Ownable { } function setHeir(address _newHeir) public onlyOwner { - require(_newHeir != owner); + require(_newHeir != getOwner()); heartbeat(); - emit HeirChanged(owner, _newHeir); + emit HeirChanged(getOwner(), _newHeir); heir_ = _newHeir; } @@ -87,7 +87,7 @@ contract Heritable is Ownable { */ function proclaimDeath() public onlyHeir { require(_ownerLives()); - emit OwnerProclaimedDead(owner, heir_, timeOfDeath_); + emit OwnerProclaimedDead(getOwner(), heir_, timeOfDeath_); // solium-disable-next-line security/no-block-members timeOfDeath_ = block.timestamp; } @@ -96,7 +96,7 @@ contract Heritable is Ownable { * @dev Owner can send a heartbeat if they were mistakenly pronounced dead. */ function heartbeat() public onlyOwner { - emit OwnerHeartbeated(owner); + emit OwnerHeartbeated(getOwner()); timeOfDeath_ = 0; } @@ -107,9 +107,8 @@ contract Heritable is Ownable { 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_; + emit HeirOwnershipClaimed(getOwner(), heir_); + transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index 1fbf571bf7c..a4d42ab0501 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -7,7 +7,7 @@ pragma solidity ^0.4.24; * functions, this simplifies the implementation of "user permissions". */ contract Ownable { - address public owner; + address private owner_; event OwnershipRenounced(address indexed previousOwner); @@ -22,14 +22,21 @@ contract Ownable { * account. */ constructor() public { - owner = msg.sender; + owner_ = msg.sender; + } + + /** + * @return the address of the owner. + */ + function getOwner() public view returns(address) { + return owner_; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { - require(msg.sender == owner); + require(msg.sender == owner_); _; } @@ -40,7 +47,7 @@ contract Ownable { * modifier anymore. */ function renounceOwnership() public onlyOwner { - emit OwnershipRenounced(owner); + emit OwnershipRenounced(owner_); owner = address(0); } @@ -58,7 +65,7 @@ contract Ownable { */ function _transferOwnership(address _newOwner) internal { require(_newOwner != address(0)); - emit OwnershipTransferred(owner, _newOwner); - owner = _newOwner; + emit OwnershipTransferred(owner_, _newOwner); + owner_ = _newOwner; } } diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index d8d081d03c3..c0fddf3ab5c 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -27,7 +27,7 @@ contract Superuser is Ownable, RBAC { } modifier onlyOwnerOrSuperuser() { - require(msg.sender == owner || isSuperuser(msg.sender)); + require(msg.sender == getOwner() || isSuperuser(msg.sender)); _; } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index e680e6b6055..93f0d28e977 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier hasMintPermission() { - require(msg.sender == owner); + require(msg.sender == getOwner()); _; } diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js index 798ab0ac9fa..19143c850b0 100644 --- a/test/access/SignatureBouncer.test.js +++ b/test/access/SignatureBouncer.test.js @@ -20,7 +20,7 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] context('management', function () { it('has a default owner of self', async function () { - (await this.bouncer.owner()).should.equal(owner); + (await this.bouncer.getOwner()).should.equal(owner); }); it('allows the owner to add a bouncer', async function () { diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index 50a37eaef72..9f3c8204229 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -21,7 +21,7 @@ contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be token owner', async function () { - (await this.token.owner()).should.equal(this.crowdsale.address); + (await this.token.getOwner()).should.equal(this.crowdsale.address); }); shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js index bfc1355d5f7..40659532676 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.equal(0); + (await claimable.getOwner()).should.not.equal(0); }); it('changes pendingOwner after transfer', async function () { await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.pendingOwner()).should.equal(newOwner); + (await claimable.getPendingOwner()).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.equal(newOwner); + (await claimable.getOwner()).should.equal(newOwner); }); }); }); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js index e65ac86c41d..039f1ae672c 100644 --- a/test/ownership/DelayedClaimable.test.js +++ b/test/ownership/DelayedClaimable.test.js @@ -17,35 +17,35 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { 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.getEnd()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); + (await this.delayedClaimable.getStart()).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.getEnd()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.start()).should.be.bignumber.equal(0); + (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.owner()).should.equal(newOwner); + (await this.delayedClaimable.getOwner()).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.getEnd()).should.be.bignumber.equal(110); - (await this.delayedClaimable.start()).should.be.bignumber.equal(100); + (await this.delayedClaimable.getStart()).should.be.bignumber.equal(100); - (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.owner()).should.not.equal(newOwner); + (await this.delayedClaimable.getOwner()).should.not.equal(newOwner); }); it('set end and start invalid values fail', async function () { diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index 6257c01df5a..ebb550b0f25 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.equal(owner); + (await this.ownable.getOwner()).should.equal(owner); }); it('changes owner after transfer', async function () { await this.ownable.transferOwnership(anyone, { from: owner }); - (await this.ownable.owner()).should.equal(anyone); + (await this.ownable.getOwner()).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.equal(ZERO_ADDRESS); + (await this.ownable.getOwner()).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 5e4d2c32456..3b4e6ede62d 100644 --- a/test/ownership/Superuser.test.js +++ b/test/ownership/Superuser.test.js @@ -40,7 +40,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.owner()).should.equal(newOwner); + (await this.superuser.getOwner()).should.equal(newOwner); }); it('should change owner after the owner transfers the ownership', async function () { @@ -49,7 +49,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.owner()).should.equal(newOwner); + (await this.superuser.getOwner()).should.equal(newOwner); }); }); diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index 63fb861b551..cb982ed26f1 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -13,7 +13,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { 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); + (await this.token.getOwner({ from: owner })).should.equal(owner); }); }); From 211e2aa14c4d64dca313bffb603cdca55d702f0a Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:50:07 +0000 Subject: [PATCH 32/67] fix rebase --- contracts/bounties/BreakInvariantBounty.sol | 2 +- contracts/ownership/Ownable.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index c889fbe606f..1f330ffd378 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -50,7 +50,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() public onlyOwner { - selfdestruct(owner); + selfdestruct(getOwner()); } /** diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index a4d42ab0501..9a7d60d8978 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -48,7 +48,7 @@ contract Ownable { */ function renounceOwnership() public onlyOwner { emit OwnershipRenounced(owner_); - owner = address(0); + owner_ = address(0); } /** From 411058f288b1899d8e40e5c48eb41dc1d814ed2f Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 03:55:20 +0000 Subject: [PATCH 33/67] fix ownership --- contracts/ownership/CanReclaimToken.sol | 2 +- contracts/ownership/Claimable.sol | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index 858a1db84e8..11452cdeb9b 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -20,7 +20,7 @@ contract CanReclaimToken is Ownable { */ function reclaimToken(IERC20 _token) external onlyOwner { uint256 balance = _token.balanceOf(this); - _token.safeTransfer(owner, balance); + _token.safeTransfer(getOwner(), balance); } } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index 6576632af12..fd7c2ffbb39 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -39,8 +39,6 @@ contract Claimable is Ownable { * @dev Allows the pendingOwner address to finalize the transfer. */ function claimOwnership() public onlyPendingOwner { - emit OwnershipTransferred(getOwner(), pendingOwner_); - owner = pendingOwner_; - pendingOwner_ = address(0); + _transferOwnership(pendingOwner_); } } From 0be51291b768cc939317a55873e541414f9ee92d Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 04:23:33 +0000 Subject: [PATCH 34/67] Improve encapsulation on payments --- contracts/ownership/Heritable.sol | 2 +- contracts/payment/RefundEscrow.sol | 38 +++++++++++------ contracts/payment/SplitPayment.sol | 67 +++++++++++++++++++++++------- test/payment/RefundEscrow.test.js | 4 ++ test/payment/SplitPayment.test.js | 6 +-- 5 files changed, 85 insertions(+), 32 deletions(-) diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index 9e3214c071f..fc3d561fcad 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -108,7 +108,7 @@ contract Heritable is Ownable { // solium-disable-next-line security/no-block-members require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); emit HeirOwnershipClaimed(getOwner(), heir_); - transferOwnership(heir_); + _transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index 9ba5fc60032..d9c11db5395 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -16,8 +16,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { event Closed(); event RefundsEnabled(); - State public state; - address public beneficiary; + State private state_; + address private beneficiary_; /** * @dev Constructor. @@ -25,8 +25,22 @@ contract RefundEscrow is Ownable, ConditionalEscrow { */ constructor(address _beneficiary) public { require(_beneficiary != address(0)); - beneficiary = _beneficiary; - state = State.Active; + beneficiary_ = _beneficiary; + state_ = State.Active; + } + + /** + * @return the current state of the escrow. + */ + function getState() public view returns(State) { + return state_; + } + + /** + * @return the beneficiary of the escrow. + */ + function getBeneficiary() public view returns(address) { + return beneficiary_; } /** @@ -34,7 +48,7 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @param _refundee The address funds will be sent to if a refund occurs. */ function deposit(address _refundee) public payable { - require(state == State.Active); + require(state_ == State.Active); super.deposit(_refundee); } @@ -43,8 +57,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * further deposits. */ function close() public onlyOwner { - require(state == State.Active); - state = State.Closed; + require(state_ == State.Active); + state_ = State.Closed; emit Closed(); } @@ -52,8 +66,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Allows for refunds to take place, rejecting further deposits. */ function enableRefunds() public onlyOwner { - require(state == State.Active); - state = State.Refunding; + require(state_ == State.Active); + state_ = State.Refunding; emit RefundsEnabled(); } @@ -61,14 +75,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Withdraws the beneficiary's funds. */ function beneficiaryWithdraw() public { - require(state == State.Closed); - beneficiary.transfer(address(this).balance); + require(state_ == State.Closed); + beneficiary_.transfer(address(this).balance); } /** * @dev Returns whether refundees can withdraw their deposits (be refunded). */ function withdrawalAllowed(address _payee) public view returns (bool) { - return state == State.Refunding; + return state_ == State.Refunding; } } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 04b01425683..8b1c53017de 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -11,12 +11,12 @@ import "../math/SafeMath.sol"; contract SplitPayment { using SafeMath for uint256; - uint256 public totalShares = 0; - uint256 public totalReleased = 0; + uint256 private totalShares_ = 0; + uint256 private totalReleased_ = 0; - mapping(address => uint256) public shares; - mapping(address => uint256) public released; - address[] public payees; + mapping(address => uint256) private shares_; + mapping(address => uint256) private released_; + address[] private payees_; /** * @dev Constructor @@ -35,26 +35,61 @@ contract SplitPayment { */ function () external payable {} + /** + * @return the total shares of the contract. + */ + function getTotalShares() public view returns(uint256) { + return totalShares_; + } + + /** + * @return the total amount already released. + */ + function getTotalReleased() public view returns(uint256) { + return totalReleased_; + } + + /** + * @return the shares of an account. + */ + function getShares(address _account) public view returns(uint256) { + return shares_[_account]; + } + + /** + * @return the amount already released to an account. + */ + function getReleased(address _account) public view returns(uint256) { + return released_[_account]; + } + + /** + * @return the address of a payee. + */ + function getPayee(uint256 index) public view returns(address) { + return payees_[index]; + } + /** * @dev Claim your share of the balance. */ function claim() public { address payee = msg.sender; - require(shares[payee] > 0); + require(shares_[payee] > 0); - uint256 totalReceived = address(this).balance.add(totalReleased); + uint256 totalReceived = address(this).balance.add(totalReleased_); uint256 payment = totalReceived.mul( - shares[payee]).div( - totalShares).sub( - released[payee] + shares_[payee]).div( + totalShares_).sub( + released_[payee] ); require(payment != 0); assert(address(this).balance >= payment); - released[payee] = released[payee].add(payment); - totalReleased = totalReleased.add(payment); + released_[payee] = released_[payee].add(payment); + totalReleased_ = totalReleased_.add(payment); payee.transfer(payment); } @@ -67,10 +102,10 @@ contract SplitPayment { function _addPayee(address _payee, uint256 _shares) internal { require(_payee != address(0)); require(_shares > 0); - require(shares[_payee] == 0); + require(shares_[_payee] == 0); - payees.push(_payee); - shares[_payee] = _shares; - totalShares = totalShares.add(_shares); + payees_.push(_payee); + shares_[_payee] = _shares; + totalShares_ = totalShares_.add(_shares); } } diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index e74d68f674b..d96413e4148 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -28,6 +28,10 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); context('active state', function () { + it('has beneficiary', async function () { + (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); + }); + it('accepts deposits', async function () { await this.escrow.deposit(refundee1, { from: owner, value: amount }); diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index bb09ea73cac..c713ff48cc6 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -53,11 +53,11 @@ 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.equal(0); + (await this.contract.getShares(payee1)).should.be.bignumber.not.equal(0); }); it('should not store shares if address is not payee', async function () { - (await this.contract.shares.call(nonpayee1)).should.be.bignumber.equal(0); + (await this.contract.getShares(nonpayee1)).should.be.bignumber.equal(0); }); it('should throw if no funds to claim', async function () { @@ -96,7 +96,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0); // check correct funds released accounting - (await this.contract.totalReleased.call()).should.be.bignumber.equal(initBalance); + (await this.contract.getTotalReleased()).should.be.bignumber.equal(initBalance); }); }); }); From 1c1399cf94d44093af6a9144abc31874c7632108 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Tue, 4 Sep 2018 04:55:07 +0000 Subject: [PATCH 35/67] Add missing tests --- test/payment/SplitPayment.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index c713ff48cc6..fcbf6c58af1 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -46,6 +46,17 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, this.contract = await SplitPayment.new(this.payees, this.shares); }); + it('should have total shares', async function () { + (await this.contract.getTotalShares()).should.be.bignumber.equal(20 + 10 + 70); + }); + + it('should have payees', async function () { + this.payees.forEach(async (payee, index) => { + (await this.getPayee(index)).should.be.equal(payee); + (await this.contract.getReleased(payee)).should.be.bignumber.equal(0); + }); + }); + it('should accept payments', async function () { await ethSendTransaction({ from: owner, to: this.contract.address, value: amount }); From cbe4f369631bf2ab9ba3e327f8706411c532c5fb Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Tue, 4 Sep 2018 18:39:30 +0000 Subject: [PATCH 36/67] add missing test --- test/payment/RefundEscrow.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index d96413e4148..392d07603cb 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -28,8 +28,9 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] }); context('active state', function () { - it('has beneficiary', async function () { + it('has beneficiary and state', async function () { (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); + (await this.escrow.getState()).should.be.bignumber.equal(0); }); it('accepts deposits', async function () { From 869b364f63a2b3d93cea75dd47dc68bee29bdfcd Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 02:23:44 +0000 Subject: [PATCH 37/67] Do not prefix getters --- contracts/bounties/BreakInvariantBounty.sol | 2 +- contracts/drafts/TokenVesting.sol | 2 +- contracts/lifecycle/Pausable.sol | 2 +- contracts/ownership/CanReclaimToken.sol | 2 +- contracts/ownership/Claimable.sol | 2 +- contracts/ownership/DelayedClaimable.sol | 4 ++-- contracts/ownership/Heritable.sol | 10 +++++----- contracts/ownership/Ownable.sol | 2 +- contracts/ownership/Superuser.sol | 2 +- contracts/payment/RefundEscrow.sol | 4 ++-- contracts/payment/SplitPayment.sol | 10 +++++----- contracts/token/ERC20/ERC20Mintable.sol | 2 +- test/access/SignatureBouncer.test.js | 2 +- test/crowdsale/MintedCrowdsale.test.js | 2 +- test/ownership/Claimable.test.js | 6 +++--- test/ownership/DelayedClaimable.test.js | 20 ++++++++++---------- test/ownership/Ownable.behavior.js | 6 +++--- test/ownership/Superuser.test.js | 4 ++-- test/payment/RefundEscrow.test.js | 4 ++-- test/payment/SplitPayment.test.js | 10 +++++----- test/token/ERC20/ERC20Pausable.test.js | 10 +++++----- 21 files changed, 54 insertions(+), 54 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 1f330ffd378..5301a2d51a4 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -50,7 +50,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() public onlyOwner { - selfdestruct(getOwner()); + selfdestruct(owner()); } /** diff --git a/contracts/drafts/TokenVesting.sol b/contracts/drafts/TokenVesting.sol index 08f2c64d988..cfe7697d093 100644 --- a/contracts/drafts/TokenVesting.sol +++ b/contracts/drafts/TokenVesting.sol @@ -142,7 +142,7 @@ contract TokenVesting is Ownable { revoked_[_token] = true; - _token.safeTransfer(getOwner(), refund); + _token.safeTransfer(owner(), refund); emit Revoked(); } diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 3e5676f7f87..37caf1e0a52 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -18,7 +18,7 @@ contract Pausable is Ownable { /** * @return true if the contract is paused, false otherwise. */ - function isPaused() public view returns(bool) { + function paused() public view returns(bool) { return paused_; } diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index 11452cdeb9b..e4f7e88a638 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -20,7 +20,7 @@ contract CanReclaimToken is Ownable { */ function reclaimToken(IERC20 _token) external onlyOwner { uint256 balance = _token.balanceOf(this); - _token.safeTransfer(getOwner(), balance); + _token.safeTransfer(owner(), balance); } } diff --git a/contracts/ownership/Claimable.sol b/contracts/ownership/Claimable.sol index fd7c2ffbb39..7c19d288076 100644 --- a/contracts/ownership/Claimable.sol +++ b/contracts/ownership/Claimable.sol @@ -23,7 +23,7 @@ contract Claimable is Ownable { /** * @return the address of the pending owner. */ - function getPendingOwner() public view returns(address) { + function pendingOwner() public view returns(address) { return pendingOwner_; } diff --git a/contracts/ownership/DelayedClaimable.sol b/contracts/ownership/DelayedClaimable.sol index b965856490a..b373e0067ea 100644 --- a/contracts/ownership/DelayedClaimable.sol +++ b/contracts/ownership/DelayedClaimable.sol @@ -16,14 +16,14 @@ contract DelayedClaimable is Claimable { /** * @return the start of the claimable period. */ - function getStart() public view returns(uint256) { + function start() public view returns(uint256) { return start_; } /** * @return the end of the claimable period. */ - function getEnd() public view returns(uint256) { + function end() public view returns(uint256) { return end_; } diff --git a/contracts/ownership/Heritable.sol b/contracts/ownership/Heritable.sol index fc3d561fcad..d015468f5ca 100644 --- a/contracts/ownership/Heritable.sol +++ b/contracts/ownership/Heritable.sol @@ -51,9 +51,9 @@ contract Heritable is Ownable { } function setHeir(address _newHeir) public onlyOwner { - require(_newHeir != getOwner()); + require(_newHeir != owner()); heartbeat(); - emit HeirChanged(getOwner(), _newHeir); + emit HeirChanged(owner(), _newHeir); heir_ = _newHeir; } @@ -87,7 +87,7 @@ contract Heritable is Ownable { */ function proclaimDeath() public onlyHeir { require(_ownerLives()); - emit OwnerProclaimedDead(getOwner(), heir_, timeOfDeath_); + emit OwnerProclaimedDead(owner(), heir_, timeOfDeath_); // solium-disable-next-line security/no-block-members timeOfDeath_ = block.timestamp; } @@ -96,7 +96,7 @@ contract Heritable is Ownable { * @dev Owner can send a heartbeat if they were mistakenly pronounced dead. */ function heartbeat() public onlyOwner { - emit OwnerHeartbeated(getOwner()); + emit OwnerHeartbeated(owner()); timeOfDeath_ = 0; } @@ -107,7 +107,7 @@ contract Heritable is Ownable { require(!_ownerLives()); // solium-disable-next-line security/no-block-members require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_); - emit HeirOwnershipClaimed(getOwner(), heir_); + emit HeirOwnershipClaimed(owner(), heir_); _transferOwnership(heir_); timeOfDeath_ = 0; } diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index 9a7d60d8978..f89013a1a53 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -28,7 +28,7 @@ contract Ownable { /** * @return the address of the owner. */ - function getOwner() public view returns(address) { + function owner() public view returns(address) { return owner_; } diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index c0fddf3ab5c..b1f1fa4b52d 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -27,7 +27,7 @@ contract Superuser is Ownable, RBAC { } modifier onlyOwnerOrSuperuser() { - require(msg.sender == getOwner() || isSuperuser(msg.sender)); + require(msg.sender == owner() || isSuperuser(msg.sender)); _; } diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index d9c11db5395..802dd471128 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -32,14 +32,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { /** * @return the current state of the escrow. */ - function getState() public view returns(State) { + function state() public view returns(State) { return state_; } /** * @return the beneficiary of the escrow. */ - function getBeneficiary() public view returns(address) { + function beneficiary() public view returns(address) { return beneficiary_; } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 8b1c53017de..1655b738383 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -38,35 +38,35 @@ contract SplitPayment { /** * @return the total shares of the contract. */ - function getTotalShares() public view returns(uint256) { + function totalShares() public view returns(uint256) { return totalShares_; } /** * @return the total amount already released. */ - function getTotalReleased() public view returns(uint256) { + function totalReleased() public view returns(uint256) { return totalReleased_; } /** * @return the shares of an account. */ - function getShares(address _account) public view returns(uint256) { + function shares(address _account) public view returns(uint256) { return shares_[_account]; } /** * @return the amount already released to an account. */ - function getReleased(address _account) public view returns(uint256) { + function released(address _account) public view returns(uint256) { return released_[_account]; } /** * @return the address of a payee. */ - function getPayee(uint256 index) public view returns(address) { + function payee(uint256 index) public view returns(address) { return payees_[index]; } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 93f0d28e977..518936699e4 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier hasMintPermission() { - require(msg.sender == getOwner()); + require(msg.sender == owner()); _; } diff --git a/test/access/SignatureBouncer.test.js b/test/access/SignatureBouncer.test.js index 19143c850b0..798ab0ac9fa 100644 --- a/test/access/SignatureBouncer.test.js +++ b/test/access/SignatureBouncer.test.js @@ -20,7 +20,7 @@ contract('Bouncer', function ([_, owner, anyone, bouncerAddress, authorizedUser] context('management', function () { it('has a default owner of self', async function () { - (await this.bouncer.getOwner()).should.equal(owner); + (await this.bouncer.owner()).should.equal(owner); }); it('allows the owner to add a bouncer', async function () { diff --git a/test/crowdsale/MintedCrowdsale.test.js b/test/crowdsale/MintedCrowdsale.test.js index 9f3c8204229..50a37eaef72 100644 --- a/test/crowdsale/MintedCrowdsale.test.js +++ b/test/crowdsale/MintedCrowdsale.test.js @@ -21,7 +21,7 @@ contract('MintedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be token owner', async function () { - (await this.token.getOwner()).should.equal(this.crowdsale.address); + (await this.token.owner()).should.equal(this.crowdsale.address); }); shouldBehaveLikeMintedCrowdsale([_, investor, wallet, purchaser], rate, value); diff --git a/test/ownership/Claimable.test.js b/test/ownership/Claimable.test.js index 40659532676..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.getOwner()).should.not.equal(0); + (await claimable.owner()).should.not.equal(0); }); it('changes pendingOwner after transfer', async function () { await claimable.transferOwnership(newOwner, { from: owner }); - (await claimable.getPendingOwner()).should.equal(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.getOwner()).should.equal(newOwner); + (await claimable.owner()).should.equal(newOwner); }); }); }); diff --git a/test/ownership/DelayedClaimable.test.js b/test/ownership/DelayedClaimable.test.js index 039f1ae672c..e65ac86c41d 100644 --- a/test/ownership/DelayedClaimable.test.js +++ b/test/ownership/DelayedClaimable.test.js @@ -17,35 +17,35 @@ contract('DelayedClaimable', function ([_, owner, newOwner]) { await this.delayedClaimable.transferOwnership(newOwner, { from: owner }); await this.delayedClaimable.setLimits(0, 1000, { from: owner }); - (await this.delayedClaimable.getEnd()).should.be.bignumber.equal(1000); + (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); + (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.getEnd()).should.be.bignumber.equal(1000); + (await this.delayedClaimable.end()).should.be.bignumber.equal(1000); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(0); + (await this.delayedClaimable.start()).should.be.bignumber.equal(0); - (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await this.delayedClaimable.claimOwnership({ from: newOwner }); - (await this.delayedClaimable.getOwner()).should.equal(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.getEnd()).should.be.bignumber.equal(110); + (await this.delayedClaimable.end()).should.be.bignumber.equal(110); - (await this.delayedClaimable.getStart()).should.be.bignumber.equal(100); + (await this.delayedClaimable.start()).should.be.bignumber.equal(100); - (await this.delayedClaimable.getPendingOwner()).should.equal(newOwner); + (await this.delayedClaimable.pendingOwner()).should.equal(newOwner); await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner })); - (await this.delayedClaimable.getOwner()).should.not.equal(newOwner); + (await this.delayedClaimable.owner()).should.not.equal(newOwner); }); it('set end and start invalid values fail', async function () { diff --git a/test/ownership/Ownable.behavior.js b/test/ownership/Ownable.behavior.js index ebb550b0f25..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.getOwner()).should.equal(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.getOwner()).should.equal(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.getOwner()).should.equal(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 3b4e6ede62d..5e4d2c32456 100644 --- a/test/ownership/Superuser.test.js +++ b/test/ownership/Superuser.test.js @@ -40,7 +40,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.getOwner()).should.equal(newOwner); + (await this.superuser.owner()).should.equal(newOwner); }); it('should change owner after the owner transfers the ownership', async function () { @@ -49,7 +49,7 @@ contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) 'OwnershipTransferred' ); - (await this.superuser.getOwner()).should.equal(newOwner); + (await this.superuser.owner()).should.equal(newOwner); }); }); diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index 392d07603cb..7730d8242a0 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -29,8 +29,8 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2] context('active state', function () { it('has beneficiary and state', async function () { - (await this.escrow.getBeneficiary()).should.be.equal(beneficiary); - (await this.escrow.getState()).should.be.bignumber.equal(0); + (await this.escrow.beneficiary()).should.be.equal(beneficiary); + (await this.escrow.state()).should.be.bignumber.equal(0); }); it('accepts deposits', async function () { diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index fcbf6c58af1..f1f08601e3d 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -47,13 +47,13 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, }); it('should have total shares', async function () { - (await this.contract.getTotalShares()).should.be.bignumber.equal(20 + 10 + 70); + (await this.contract.totalShares()).should.be.bignumber.equal(20 + 10 + 70); }); it('should have payees', async function () { this.payees.forEach(async (payee, index) => { - (await this.getPayee(index)).should.be.equal(payee); - (await this.contract.getReleased(payee)).should.be.bignumber.equal(0); + (await this.payee(index)).should.be.equal(payee); + (await this.contract.released(payee)).should.be.bignumber.equal(0); }); }); @@ -64,11 +64,11 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, }); it('should store shares if address is payee', async function () { - (await this.contract.getShares(payee1)).should.be.bignumber.not.equal(0); + (await this.contract.shares(payee1)).should.be.bignumber.not.equal(0); }); it('should not store shares if address is not payee', async function () { - (await this.contract.getShares(nonpayee1)).should.be.bignumber.equal(0); + (await this.contract.shares(nonpayee1)).should.be.bignumber.equal(0); }); it('should throw if no funds to claim', async function () { diff --git a/test/token/ERC20/ERC20Pausable.test.js b/test/token/ERC20/ERC20Pausable.test.js index b11ecb3e7b6..8fbe4f70876 100644 --- a/test/token/ERC20/ERC20Pausable.test.js +++ b/test/token/ERC20/ERC20Pausable.test.js @@ -13,7 +13,7 @@ contract('ERC20Pausable', 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.isPaused()).should.equal(true); + (await this.token.paused()).should.equal(true); }); it('emits a Pause event', async function () { @@ -55,7 +55,7 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { it('unpauses the token', async function () { await this.token.unpause({ from }); - (await this.token.isPaused()).should.equal(false); + (await this.token.paused()).should.equal(false); }); it('emits an Unpause event', async function () { @@ -87,18 +87,18 @@ contract('ERC20Pausable', function ([_, owner, recipient, anotherAccount]) { describe('paused', function () { it('is not paused by default', async function () { - (await this.token.isPaused({ from })).should.equal(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.isPaused({ from })).should.equal(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.isPaused()).should.equal(false); + (await this.token.paused()).should.equal(false); }); }); From 526ff31cea3bd078d32863c153589af826bc3b17 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 03:02:17 +0000 Subject: [PATCH 38/67] Fix tests. --- test/payment/SplitPayment.test.js | 2 +- test/token/ERC20/ERC20Mintable.behavior.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index f1f08601e3d..a284530996a 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -107,7 +107,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0); // check correct funds released accounting - (await this.contract.getTotalReleased()).should.be.bignumber.equal(initBalance); + (await this.contract.totalReleased()).should.be.bignumber.equal(initBalance); }); }); }); diff --git a/test/token/ERC20/ERC20Mintable.behavior.js b/test/token/ERC20/ERC20Mintable.behavior.js index cb982ed26f1..63fb861b551 100644 --- a/test/token/ERC20/ERC20Mintable.behavior.js +++ b/test/token/ERC20/ERC20Mintable.behavior.js @@ -13,7 +13,7 @@ function shouldBehaveLikeERC20Mintable (owner, minter, [anyone]) { describe('as a basic mintable token', function () { describe('after token creation', function () { it('sender should be token owner', async function () { - (await this.token.getOwner({ from: owner })).should.equal(owner); + (await this.token.owner({ from: owner })).should.equal(owner); }); }); From b4d64d431d9d040e2bf314628d294c820c7a3875 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 01:24:02 +0000 Subject: [PATCH 39/67] Improve encapsulation on Crowdsales --- contracts/crowdsale/Crowdsale.sol | 58 ++++++++++++++----- .../distribution/FinalizableCrowdsale.sol | 13 ++++- .../distribution/PostDeliveryCrowdsale.sol | 15 +++-- .../distribution/RefundableCrowdsale.sol | 17 ++++-- .../crowdsale/emission/AllowanceCrowdsale.sol | 15 +++-- .../crowdsale/emission/MintedCrowdsale.sol | 3 +- .../price/IncreasingPriceCrowdsale.sol | 26 +++++++-- .../crowdsale/validation/CappedCrowdsale.sol | 15 +++-- .../IndividuallyCappedCrowdsale.sol | 18 +++--- test/examples/SampleCrowdsale.test.js | 8 +-- 10 files changed, 134 insertions(+), 54 deletions(-) diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index 8bf055ada87..adfb59e95ea 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -22,19 +22,16 @@ contract Crowdsale { using SafeERC20 for IERC20; // The token being sold - IERC20 public token; + IERC20 private token_; // Address where funds are collected - address public wallet; + address private 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 ERC20Detailed token with 3 decimals called TOK - // 1 wei will give you 1 unit, or 0.001 TOK. - uint256 public rate; + uint256 private rate_; // Amount of wei raised - uint256 public weiRaised; + uint256 private weiRaised_; /** * Event for token purchase logging @@ -52,6 +49,9 @@ contract Crowdsale { /** * @param _rate Number of token units a buyer gets per wei + * @dev 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 ERC20Detailed token + * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK. * @param _wallet Address where collected funds will be forwarded to * @param _token Address of the token being sold */ @@ -60,9 +60,9 @@ contract Crowdsale { require(_wallet != address(0)); require(_token != address(0)); - rate = _rate; - wallet = _wallet; - token = _token; + rate_ = _rate; + wallet_ = _wallet; + token_ = _token; } // ----------------------------------------- @@ -76,6 +76,34 @@ contract Crowdsale { buyTokens(msg.sender); } + /** + * @return the token being sold. + */ + function getToken() public view returns(IERC20) { + return token_; + } + + /** + * @return the address where funds are collected. + */ + function getWallet() public view returns(address) { + return wallet_; + } + + /** + * @return the number of token units a buyer gets per wei. + */ + function getRate() public view returns(uint256) { + return rate_; + } + + /** + * @return the mount of wei raised. + */ + function getWeiRaised() public view returns (uint256) { + return weiRaised_; + } + /** * @dev low level token purchase ***DO NOT OVERRIDE*** * @param _beneficiary Address performing the token purchase @@ -89,7 +117,7 @@ contract Crowdsale { uint256 tokens = _getTokenAmount(weiAmount); // update state - weiRaised = weiRaised.add(weiAmount); + weiRaised_ = weiRaised_.add(weiAmount); _processPurchase(_beneficiary, tokens); emit TokensPurchased( @@ -113,7 +141,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: * super._preValidatePurchase(_beneficiary, _weiAmount); - * require(weiRaised.add(_weiAmount) <= cap); + * require(getWeiRaised().add(_weiAmount) <= cap); * @param _beneficiary Address performing the token purchase * @param _weiAmount Value in wei involved in the purchase */ @@ -152,7 +180,7 @@ contract Crowdsale { ) internal { - token.safeTransfer(_beneficiary, _tokenAmount); + token_.safeTransfer(_beneficiary, _tokenAmount); } /** @@ -191,13 +219,13 @@ contract Crowdsale { function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) { - return _weiAmount.mul(rate); + return _weiAmount.mul(rate_); } /** * @dev Determines how ETH is stored/forwarded on purchases. */ function _forwardFunds() internal { - wallet.transfer(msg.value); + wallet_.transfer(msg.value); } } diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 651069d41af..42d37b8ee15 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -13,22 +13,29 @@ import "../validation/TimedCrowdsale.sol"; contract FinalizableCrowdsale is Ownable, TimedCrowdsale { using SafeMath for uint256; - bool public isFinalized = false; + bool private finalized_ = false; event CrowdsaleFinalized(); + /** + * @return true if the crowdsale is finalized, false otherwise. + */ + function isFinalized() public view returns(bool) { + return finalized_; + } + /** * @dev Must be called after crowdsale ends, to do some extra finalization * work. Calls the contract's finalization function. */ function finalize() public onlyOwner { - require(!isFinalized); + require(!finalized_); require(hasClosed()); _finalization(); emit CrowdsaleFinalized(); - isFinalized = true; + finalized_ = true; } /** diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index b09d09709a5..6971669aedb 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -12,19 +12,26 @@ import "../../math/SafeMath.sol"; contract PostDeliveryCrowdsale is TimedCrowdsale { using SafeMath for uint256; - mapping(address => uint256) public balances; + mapping(address => uint256) private balances_; /** * @dev Withdraw tokens only after crowdsale ends. */ function withdrawTokens() public { require(hasClosed()); - uint256 amount = balances[msg.sender]; + uint256 amount = balances_[msg.sender]; require(amount > 0); - balances[msg.sender] = 0; + balances_[msg.sender] = 0; _deliverTokens(msg.sender, amount); } + /** + * @return the balance of an account. + */ + function getBalance(address _account) public view returns(uint256) { + return balances_[_account]; + } + /** * @dev Overrides parent by storing balances instead of issuing tokens right away. * @param _beneficiary Token purchaser @@ -36,7 +43,7 @@ contract PostDeliveryCrowdsale is TimedCrowdsale { ) internal { - balances[_beneficiary] = balances[_beneficiary].add(_tokenAmount); + balances_[_beneficiary] = balances_[_beneficiary].add(_tokenAmount); } } diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 05f95ca8584..690afd2910d 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -15,7 +15,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { using SafeMath for uint256; // minimum amount of funds to be raised in weis - uint256 public goal; + uint256 private goal_; // refund escrow used to hold funds while crowdsale is running RefundEscrow private escrow_; @@ -26,15 +26,22 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ constructor(uint256 _goal) public { require(_goal > 0); - escrow_ = new RefundEscrow(wallet); - goal = _goal; + escrow_ = new RefundEscrow(getWallet()); + goal_ = _goal; + } + + /** + * @return minimum amount of funds to be raised in wei. + */ + function getGoal() public view returns(uint256) { + return goal_; } /** * @dev Investors can claim refunds here if crowdsale is unsuccessful */ function claimRefund() public { - require(isFinalized); + require(isFinalized()); require(!goalReached()); escrow_.withdraw(msg.sender); @@ -45,7 +52,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @return Whether funding goal was reached */ function goalReached() public view returns (bool) { - return weiRaised >= goal; + return getWeiRaised() >= goal_; } /** diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index 4e48638ab74..776933948ac 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -14,7 +14,7 @@ contract AllowanceCrowdsale is Crowdsale { using SafeMath for uint256; using SafeERC20 for IERC20; - address public tokenWallet; + address private tokenWallet_; /** * @dev Constructor, takes token wallet address. @@ -22,7 +22,14 @@ contract AllowanceCrowdsale is Crowdsale { */ constructor(address _tokenWallet) public { require(_tokenWallet != address(0)); - tokenWallet = _tokenWallet; + tokenWallet_ = _tokenWallet; + } + + /** + * @return the address of the wallet that will hold the tokens. + */ + function getTokenWallet() public view returns(address) { + return tokenWallet_; } /** @@ -30,7 +37,7 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return token.allowance(tokenWallet, this); + return getToken().allowance(tokenWallet_, this); } /** @@ -44,6 +51,6 @@ contract AllowanceCrowdsale is Crowdsale { ) internal { - token.safeTransferFrom(tokenWallet, _beneficiary, _tokenAmount); + getToken().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); } } diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 9f3a8b4b115..5718fca1ede 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -23,6 +23,7 @@ contract MintedCrowdsale is Crowdsale { internal { // Potentially dangerous assumption about the type of the token. - require(ERC20Mintable(address(token)).mint(_beneficiary, _tokenAmount)); + require( + ERC20Mintable(address(getToken())).mint(_beneficiary, _tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index 841f09bc36b..dd75917873f 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -13,8 +13,8 @@ import "../../math/SafeMath.sol"; contract IncreasingPriceCrowdsale is TimedCrowdsale { using SafeMath for uint256; - uint256 public initialRate; - uint256 public finalRate; + uint256 private initialRate_; + uint256 private finalRate_; /** * @dev Constructor, takes initial and final rates of tokens received per wei contributed. @@ -24,8 +24,22 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { constructor(uint256 _initialRate, uint256 _finalRate) public { require(_finalRate > 0); require(_initialRate >= _finalRate); - initialRate = _initialRate; - finalRate = _finalRate; + initialRate_ = _initialRate; + finalRate_ = _finalRate; + } + + /** + * @return the initial rate of the crowdsale. + */ + function getInitialRate() public view returns(uint256) { + return initialRate_; + } + + /** + * @return the final rate of the crowdsale. + */ + function getFinalRate() public view returns (uint256) { + return finalRate_; } /** @@ -37,8 +51,8 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { // solium-disable-next-line security/no-block-members uint256 elapsedTime = block.timestamp.sub(openingTime); uint256 timeRange = closingTime.sub(openingTime); - uint256 rateRange = initialRate.sub(finalRate); - return initialRate.sub(elapsedTime.mul(rateRange).div(timeRange)); + uint256 rateRange = initialRate_.sub(finalRate_); + return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange)); } /** diff --git a/contracts/crowdsale/validation/CappedCrowdsale.sol b/contracts/crowdsale/validation/CappedCrowdsale.sol index 1df0e3eb311..9e158143e04 100644 --- a/contracts/crowdsale/validation/CappedCrowdsale.sol +++ b/contracts/crowdsale/validation/CappedCrowdsale.sol @@ -11,7 +11,7 @@ import "../Crowdsale.sol"; contract CappedCrowdsale is Crowdsale { using SafeMath for uint256; - uint256 public cap; + uint256 private cap_; /** * @dev Constructor, takes maximum amount of wei accepted in the crowdsale. @@ -19,7 +19,14 @@ contract CappedCrowdsale is Crowdsale { */ constructor(uint256 _cap) public { require(_cap > 0); - cap = _cap; + cap_ = _cap; + } + + /** + * @return the cap of the crowdsale. + */ + function getCap() public view returns(uint256) { + return cap_; } /** @@ -27,7 +34,7 @@ contract CappedCrowdsale is Crowdsale { * @return Whether the cap was reached */ function capReached() public view returns (bool) { - return weiRaised >= cap; + return getWeiRaised() >= cap_; } /** @@ -42,7 +49,7 @@ contract CappedCrowdsale is Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(weiRaised.add(_weiAmount) <= cap); + require(getWeiRaised().add(_weiAmount) <= cap_); } } diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index 3c7884c658a..f0c3fdd4c43 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -12,8 +12,8 @@ import "../../ownership/Ownable.sol"; contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { using SafeMath for uint256; - mapping(address => uint256) public contributions; - mapping(address => uint256) public caps; + mapping(address => uint256) private contributions_; + mapping(address => uint256) private caps_; /** * @dev Sets a specific user's maximum contribution. @@ -21,7 +21,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { * @param _cap Wei limit for individual contribution */ function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner { - caps[_beneficiary] = _cap; + caps_[_beneficiary] = _cap; } /** @@ -37,7 +37,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { onlyOwner { for (uint256 i = 0; i < _beneficiaries.length; i++) { - caps[_beneficiaries[i]] = _cap; + caps_[_beneficiaries[i]] = _cap; } } @@ -47,7 +47,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { * @return Current cap for individual user */ function getUserCap(address _beneficiary) public view returns (uint256) { - return caps[_beneficiary]; + return caps_[_beneficiary]; } /** @@ -58,7 +58,7 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { function getUserContribution(address _beneficiary) public view returns (uint256) { - return contributions[_beneficiary]; + return contributions_[_beneficiary]; } /** @@ -73,7 +73,8 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(contributions[_beneficiary].add(_weiAmount) <= caps[_beneficiary]); + require( + contributions_[_beneficiary].add(_weiAmount) <= caps_[_beneficiary]); } /** @@ -88,7 +89,8 @@ contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { internal { super._updatePurchasingState(_beneficiary, _weiAmount); - contributions[_beneficiary] = contributions[_beneficiary].add(_weiAmount); + contributions_[_beneficiary] = contributions_[_beneficiary].add( + _weiAmount); } } diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index f7b88080fb3..bfca735a992 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -45,10 +45,10 @@ contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime); (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime); - (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); - (await this.crowdsale.wallet()).should.be.equal(wallet); - (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); - (await this.crowdsale.cap()).should.be.bignumber.equal(CAP); + (await this.crowdsale.getRate()).should.be.bignumber.equal(RATE); + (await this.crowdsale.getWallet()).should.be.equal(wallet); + (await this.crowdsale.getGoal()).should.be.bignumber.equal(GOAL); + (await this.crowdsale.getCap()).should.be.bignumber.equal(CAP); }); it('should not accept payments before start', async function () { From 2e2d634c6eca8b437bb094a34b9801cc3840b7de Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 02:31:01 +0000 Subject: [PATCH 40/67] add missing tests --- test/crowdsale/AllowanceCrowdsale.test.js | 4 ++++ test/crowdsale/IncreasingPriceCrowdsale.test.js | 5 +++++ test/crowdsale/PostDeliveryCrowdsale.test.js | 2 ++ 3 files changed, 11 insertions(+) diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index b0d68a9b15c..7a49f7e827c 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -25,6 +25,10 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); describe('accepting payments', function () { + it.only('should have token wallet', async function () { + (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); + }); + it('should accept sends', async function () { await this.crowdsale.send(value); }); diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index e68d451bd46..ef647c18a28 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -55,6 +55,11 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) await this.token.transfer(this.crowdsale.address, tokenSupply); }); + it('should have initial and final rate', async function () { + (await this.crowdsale.getInitialRate()).should.be.bignumber.equal(initialRate); + (await this.crowdsale.getFinalRate()).should.be.bignumber.equal(finalRate); + }); + it('at start', async function () { await increaseTimeTo(this.startTime); await this.crowdsale.buyTokens(investor, { value, from: purchaser }); diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index ebc41111c3d..843ea0b73c4 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -47,6 +47,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('does not immediately assign tokens to beneficiaries', async function () { + (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(value); (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); }); @@ -61,6 +62,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { it('allows beneficiaries to withdraw tokens', async function () { await this.crowdsale.withdrawTokens({ from: investor }); + (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(0); (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); }); From 4be20f71bdffaa5fe34e73121b1acf612dbc0baa Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 02:49:14 +0000 Subject: [PATCH 41/67] remove only --- test/crowdsale/AllowanceCrowdsale.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 7a49f7e827c..64988d0f112 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -25,7 +25,7 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); describe('accepting payments', function () { - it.only('should have token wallet', async function () { + it('should have token wallet', async function () { (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); }); From 85594778ced44e76d818dd367c93faa36bed22fb Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 02:09:36 +0000 Subject: [PATCH 42/67] Do not prefix getters --- contracts/crowdsale/Crowdsale.sol | 10 +++++----- .../crowdsale/distribution/FinalizableCrowdsale.sol | 2 +- .../crowdsale/distribution/PostDeliveryCrowdsale.sol | 2 +- .../crowdsale/distribution/RefundableCrowdsale.sol | 8 ++++---- contracts/crowdsale/emission/AllowanceCrowdsale.sol | 6 +++--- contracts/crowdsale/emission/MintedCrowdsale.sol | 2 +- contracts/crowdsale/price/IncreasingPriceCrowdsale.sol | 4 ++-- contracts/crowdsale/validation/CappedCrowdsale.sol | 6 +++--- test/crowdsale/AllowanceCrowdsale.test.js | 2 +- test/crowdsale/IncreasingPriceCrowdsale.test.js | 4 ++-- test/crowdsale/PostDeliveryCrowdsale.test.js | 4 ++-- test/examples/SampleCrowdsale.test.js | 8 ++++---- 12 files changed, 29 insertions(+), 29 deletions(-) diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index adfb59e95ea..0b133c0aa28 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -79,28 +79,28 @@ contract Crowdsale { /** * @return the token being sold. */ - function getToken() public view returns(IERC20) { + function token() public view returns(IERC20) { return token_; } /** * @return the address where funds are collected. */ - function getWallet() public view returns(address) { + function wallet() public view returns(address) { return wallet_; } /** * @return the number of token units a buyer gets per wei. */ - function getRate() public view returns(uint256) { + function rate() public view returns(uint256) { return rate_; } /** * @return the mount of wei raised. */ - function getWeiRaised() public view returns (uint256) { + function weiRaised() public view returns (uint256) { return weiRaised_; } @@ -141,7 +141,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: * super._preValidatePurchase(_beneficiary, _weiAmount); - * require(getWeiRaised().add(_weiAmount) <= cap); + * require(weiRaised().add(_weiAmount) <= cap); * @param _beneficiary Address performing the token purchase * @param _weiAmount Value in wei involved in the purchase */ diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 42d37b8ee15..693e0b498e1 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -20,7 +20,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { /** * @return true if the crowdsale is finalized, false otherwise. */ - function isFinalized() public view returns(bool) { + function finalized() public view returns(bool) { return finalized_; } diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index 6971669aedb..382c6210c89 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -28,7 +28,7 @@ contract PostDeliveryCrowdsale is TimedCrowdsale { /** * @return the balance of an account. */ - function getBalance(address _account) public view returns(uint256) { + function balanceOf(address _account) public view returns(uint256) { return balances_[_account]; } diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 690afd2910d..8ffcfb592c6 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -26,14 +26,14 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ constructor(uint256 _goal) public { require(_goal > 0); - escrow_ = new RefundEscrow(getWallet()); + escrow_ = new RefundEscrow(wallet()); goal_ = _goal; } /** * @return minimum amount of funds to be raised in wei. */ - function getGoal() public view returns(uint256) { + function goal() public view returns(uint256) { return goal_; } @@ -41,7 +41,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @dev Investors can claim refunds here if crowdsale is unsuccessful */ function claimRefund() public { - require(isFinalized()); + require(finalized()); require(!goalReached()); escrow_.withdraw(msg.sender); @@ -52,7 +52,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @return Whether funding goal was reached */ function goalReached() public view returns (bool) { - return getWeiRaised() >= goal_; + return weiRaised() >= goal_; } /** diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index 776933948ac..54db50af1c9 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -28,7 +28,7 @@ contract AllowanceCrowdsale is Crowdsale { /** * @return the address of the wallet that will hold the tokens. */ - function getTokenWallet() public view returns(address) { + function tokenWallet() public view returns(address) { return tokenWallet_; } @@ -37,7 +37,7 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return getToken().allowance(tokenWallet_, this); + return token().allowance(tokenWallet_, this); } /** @@ -51,6 +51,6 @@ contract AllowanceCrowdsale is Crowdsale { ) internal { - getToken().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); + token().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); } } diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 5718fca1ede..1b676e07767 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -24,6 +24,6 @@ contract MintedCrowdsale is Crowdsale { { // Potentially dangerous assumption about the type of the token. require( - ERC20Mintable(address(getToken())).mint(_beneficiary, _tokenAmount)); + ERC20Mintable(address(token())).mint(_beneficiary, _tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index dd75917873f..7cc07f083d5 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -31,14 +31,14 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { /** * @return the initial rate of the crowdsale. */ - function getInitialRate() public view returns(uint256) { + function initialRate() public view returns(uint256) { return initialRate_; } /** * @return the final rate of the crowdsale. */ - function getFinalRate() public view returns (uint256) { + function finalRate() public view returns (uint256) { return finalRate_; } diff --git a/contracts/crowdsale/validation/CappedCrowdsale.sol b/contracts/crowdsale/validation/CappedCrowdsale.sol index 9e158143e04..99b188a9883 100644 --- a/contracts/crowdsale/validation/CappedCrowdsale.sol +++ b/contracts/crowdsale/validation/CappedCrowdsale.sol @@ -25,7 +25,7 @@ contract CappedCrowdsale is Crowdsale { /** * @return the cap of the crowdsale. */ - function getCap() public view returns(uint256) { + function cap() public view returns(uint256) { return cap_; } @@ -34,7 +34,7 @@ contract CappedCrowdsale is Crowdsale { * @return Whether the cap was reached */ function capReached() public view returns (bool) { - return getWeiRaised() >= cap_; + return weiRaised() >= cap_; } /** @@ -49,7 +49,7 @@ contract CappedCrowdsale is Crowdsale { internal { super._preValidatePurchase(_beneficiary, _weiAmount); - require(getWeiRaised().add(_weiAmount) <= cap_); + require(weiRaised().add(_weiAmount) <= cap_); } } diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 64988d0f112..ee6a063f2bd 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -26,7 +26,7 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW describe('accepting payments', function () { it('should have token wallet', async function () { - (await this.crowdsale.getTokenWallet()).should.be.equal(tokenWallet); + (await this.crowdsale.tokenWallet()).should.be.equal(tokenWallet); }); it('should accept sends', async function () { diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index ef647c18a28..c62c17d89bc 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -56,8 +56,8 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) }); it('should have initial and final rate', async function () { - (await this.crowdsale.getInitialRate()).should.be.bignumber.equal(initialRate); - (await this.crowdsale.getFinalRate()).should.be.bignumber.equal(finalRate); + (await this.crowdsale.initialRate()).should.be.bignumber.equal(initialRate); + (await this.crowdsale.finalRate()).should.be.bignumber.equal(finalRate); }); it('at start', async function () { diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index 843ea0b73c4..11058928ade 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -47,7 +47,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('does not immediately assign tokens to beneficiaries', async function () { - (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(value); + (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value); (await this.token.balanceOf(investor)).should.be.bignumber.equal(0); }); @@ -62,7 +62,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { it('allows beneficiaries to withdraw tokens', async function () { await this.crowdsale.withdrawTokens({ from: investor }); - (await this.crowdsale.getBalance(investor)).should.be.bignumber.equal(0); + (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(0); (await this.token.balanceOf(investor)).should.be.bignumber.equal(value); }); diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index bfca735a992..f7b88080fb3 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -45,10 +45,10 @@ contract('SampleCrowdsale', function ([_, owner, wallet, investor]) { (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime); (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime); - (await this.crowdsale.getRate()).should.be.bignumber.equal(RATE); - (await this.crowdsale.getWallet()).should.be.equal(wallet); - (await this.crowdsale.getGoal()).should.be.bignumber.equal(GOAL); - (await this.crowdsale.getCap()).should.be.bignumber.equal(CAP); + (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); + (await this.crowdsale.wallet()).should.be.equal(wallet); + (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); + (await this.crowdsale.cap()).should.be.bignumber.equal(CAP); }); it('should not accept payments before start', async function () { From edf8ea5055cca482fa355b5e29a20fb31fa29b1f Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 00:24:53 +0000 Subject: [PATCH 43/67] Improve encapsulation on BreakInvariantBounty --- contracts/bounties/BreakInvariantBounty.sol | 14 +++++++++++--- test/BreakInvariantBounty.test.js | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 5301a2d51a4..96909fdcd6e 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -9,7 +9,7 @@ import "../payment/PullPayment.sol"; * @dev This bounty will pay out to a researcher if they break invariant logic of the contract. */ contract BreakInvariantBounty is PullPayment, Ownable { - bool public claimed; + bool private claimed_; mapping(address => address) public researchers; event TargetCreated(address createdAddress); @@ -18,7 +18,15 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Fallback function allowing the contract to receive funds, if they haven't already been claimed. */ function() external payable { - require(!claimed); + require(!claimed_); + } + + /** + * @dev Determine if the bounty was claimed. + * @return true if the bounty was claimed, false otherwise. + */ + function wasClaimed() public view returns(bool) { + return claimed_; } /** @@ -43,7 +51,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { // Check Target contract invariants require(!_target.checkInvariant()); _asyncTransfer(researcher, address(this).balance); - claimed = true; + claimed_ = true; } /** diff --git a/test/BreakInvariantBounty.test.js b/test/BreakInvariantBounty.test.js index 1f0e36aa9e8..f8635aa19cf 100644 --- a/test/BreakInvariantBounty.test.js +++ b/test/BreakInvariantBounty.test.js @@ -64,7 +64,7 @@ contract('BreakInvariantBounty', function ([_, owner, researcher, nonTarget]) { it('can claim reward', async function () { await this.bounty.claim(this.targetAddress, { from: researcher }); - const claim = await this.bounty.claimed(); + const claim = await this.bounty.wasClaimed(); claim.should.equal(true); From b64cdefbede6aff223a67d875317eefb69bb335c Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Mon, 3 Sep 2018 00:29:24 +0000 Subject: [PATCH 44/67] Make researchers private --- contracts/bounties/BreakInvariantBounty.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 96909fdcd6e..479f19e5b6f 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -10,7 +10,7 @@ import "../payment/PullPayment.sol"; */ contract BreakInvariantBounty is PullPayment, Ownable { bool private claimed_; - mapping(address => address) public researchers; + mapping(address => address) private researchers; event TargetCreated(address createdAddress); From 148ef6492783f49758defcae35e35c819dca0593 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 01:54:17 +0000 Subject: [PATCH 45/67] Do not prefix getters --- contracts/bounties/BreakInvariantBounty.sol | 2 +- test/BreakInvariantBounty.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 479f19e5b6f..05a19045cd2 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -25,7 +25,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Determine if the bounty was claimed. * @return true if the bounty was claimed, false otherwise. */ - function wasClaimed() public view returns(bool) { + function claimed() public view returns(bool) { return claimed_; } diff --git a/test/BreakInvariantBounty.test.js b/test/BreakInvariantBounty.test.js index f8635aa19cf..1f0e36aa9e8 100644 --- a/test/BreakInvariantBounty.test.js +++ b/test/BreakInvariantBounty.test.js @@ -64,7 +64,7 @@ contract('BreakInvariantBounty', function ([_, owner, researcher, nonTarget]) { it('can claim reward', async function () { await this.bounty.claim(this.targetAddress, { from: researcher }); - const claim = await this.bounty.wasClaimed(); + const claim = await this.bounty.claimed(); claim.should.equal(true); From 4080385001c43c6b93cb08c9d23b865053b285e8 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 05:58:11 +0000 Subject: [PATCH 46/67] tmp --- contracts/access/Whitelist.sol | 40 +++---- contracts/access/rbac/RBAC.sol | 48 ++++----- contracts/access/rbac/Roles.sol | 16 +-- contracts/bounties/BreakInvariantBounty.sol | 20 ++-- contracts/crowdsale/Crowdsale.sol | 114 ++++++++++---------- 5 files changed, 119 insertions(+), 119 deletions(-) diff --git a/contracts/access/Whitelist.sol b/contracts/access/Whitelist.sol index 3d9b3da5c6f..8d0c0f912a0 100644 --- a/contracts/access/Whitelist.sol +++ b/contracts/access/Whitelist.sol @@ -13,81 +13,81 @@ import "../access/rbac/RBAC.sol"; contract Whitelist is Ownable, RBAC { // Name of the whitelisted role. - string private constant ROLE_WHITELISTED = "whitelist"; + string private constant _ROLE_WHITELISTED = "whitelist"; /** * @dev Throws if operator is not whitelisted. - * @param _operator address + * @param operator address */ - modifier onlyIfWhitelisted(address _operator) { - checkRole(_operator, ROLE_WHITELISTED); + modifier onlyIfWhitelisted(address operator) { + checkRole(operator, _ROLE_WHITELISTED); _; } /** * @dev add an address to the whitelist - * @param _operator address + * @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) + function addAddressToWhitelist(address operator) public onlyOwner { - _addRole(_operator, ROLE_WHITELISTED); + _addRole(operator, _ROLE_WHITELISTED); } /** * @dev Determine if an account is whitelisted. * @return true if the account is whitelisted, false otherwise. */ - function isWhitelisted(address _operator) + function isWhitelisted(address operator) public view returns (bool) { - return hasRole(_operator, ROLE_WHITELISTED); + return hasRole(operator, _ROLE_WHITELISTED); } /** * @dev add addresses to the whitelist - * @param _operators addresses + * @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) + function addAddressesToWhitelist(address[] operators) public onlyOwner { - for (uint256 i = 0; i < _operators.length; i++) { - addAddressToWhitelist(_operators[i]); + for (uint256 i = 0; i < operators.length; i++) { + addAddressToWhitelist(operators[i]); } } /** * @dev remove an address from the whitelist - * @param _operator address + * @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) + function removeAddressFromWhitelist(address operator) public onlyOwner { - _removeRole(_operator, ROLE_WHITELISTED); + _removeRole(operator, _ROLE_WHITELISTED); } /** * @dev remove addresses from the whitelist - * @param _operators addresses + * @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) + function removeAddressesFromWhitelist(address[] operators) public onlyOwner { - for (uint256 i = 0; i < _operators.length; i++) { - removeAddressFromWhitelist(_operators[i]); + for (uint256 i = 0; i < operators.length; i++) { + removeAddressFromWhitelist(operators[i]); } } diff --git a/contracts/access/rbac/RBAC.sol b/contracts/access/rbac/RBAC.sol index 8ecefaa99f0..df4c2497902 100644 --- a/contracts/access/rbac/RBAC.sol +++ b/contracts/access/rbac/RBAC.sol @@ -15,82 +15,82 @@ import "./Roles.sol"; contract RBAC { using Roles for Roles.Role; - mapping (string => Roles.Role) private roles; + 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 + * @param operator address + * @param role the name of the role * // reverts */ - function checkRole(address _operator, string _role) + function checkRole(address operator, string role) public view { - roles[_role].check(_operator); + _roles[role].check(operator); } /** * @dev determine if addr has role - * @param _operator address - * @param _role the name of the role + * @param operator address + * @param role the name of the role * @return bool */ - function hasRole(address _operator, string _role) + function hasRole(address operator, string role) public view returns (bool) { - return roles[_role].has(_operator); + return _roles[role].has(operator); } /** * @dev add a role to an address - * @param _operator address - * @param _role the name of the role + * @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); - emit RoleAdded(_operator, _role); + _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 + * @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); - emit RoleRemoved(_operator, _role); + _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 + * @param role the name of the role * // reverts */ - modifier onlyRole(string _role) + modifier onlyRole(string role) { - checkRole(msg.sender, _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 + * @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) { + // modifier onlyRoles(string[] roles) { // bool hasAnyRole = false; // for (uint8 i = 0; i < _roles.length; i++) { // if (hasRole(msg.sender, _roles[i])) { diff --git a/contracts/access/rbac/Roles.sol b/contracts/access/rbac/Roles.sol index de46894095f..93bffbec6d5 100644 --- a/contracts/access/rbac/Roles.sol +++ b/contracts/access/rbac/Roles.sol @@ -15,41 +15,41 @@ library Roles { /** * @dev give an account access to this role */ - function add(Role storage _role, address _account) + function add(Role storage role, address account) internal { - _role.bearer[_account] = true; + role.bearer[account] = true; } /** * @dev remove an account's access to this role */ - function remove(Role storage _role, address _account) + function remove(Role storage role, address account) internal { - _role.bearer[_account] = false; + role.bearer[account] = false; } /** * @dev check if an account has this role * // reverts */ - function check(Role storage _role, address _account) + function check(Role storage role, address account) internal view { - require(has(_role, _account)); + require(has(role, account)); } /** * @dev check if an account has this role * @return bool */ - function has(Role storage _role, address _account) + function has(Role storage role, address account) internal view returns (bool) { - return _role.bearer[_account]; + return role.bearer[account]; } } diff --git a/contracts/bounties/BreakInvariantBounty.sol b/contracts/bounties/BreakInvariantBounty.sol index 05a19045cd2..21c9dfd739a 100644 --- a/contracts/bounties/BreakInvariantBounty.sol +++ b/contracts/bounties/BreakInvariantBounty.sol @@ -9,8 +9,8 @@ import "../payment/PullPayment.sol"; * @dev This bounty will pay out to a researcher if they break invariant logic of the contract. */ contract BreakInvariantBounty is PullPayment, Ownable { - bool private claimed_; - mapping(address => address) private researchers; + bool private _claimed; + mapping(address => address) private _researchers; event TargetCreated(address createdAddress); @@ -18,7 +18,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @dev Fallback function allowing the contract to receive funds, if they haven't already been claimed. */ function() external payable { - require(!claimed_); + require(!_claimed); } /** @@ -26,7 +26,7 @@ contract BreakInvariantBounty is PullPayment, Ownable { * @return true if the bounty was claimed, false otherwise. */ function claimed() public view returns(bool) { - return claimed_; + return _claimed; } /** @@ -36,22 +36,22 @@ contract BreakInvariantBounty is PullPayment, Ownable { */ function createTarget() public returns(Target) { Target target = Target(_deployContract()); - researchers[target] = msg.sender; + _researchers[target] = msg.sender; emit TargetCreated(target); return target; } /** * @dev Transfers the contract funds to the researcher that proved the contract is broken. - * @param _target contract + * @param target contract */ - function claim(Target _target) public { - address researcher = researchers[_target]; + function claim(Target target) public { + address researcher = _researchers[target]; require(researcher != address(0)); // Check Target contract invariants - require(!_target.checkInvariant()); + require(!target.checkInvariant()); _asyncTransfer(researcher, address(this).balance); - claimed_ = true; + _claimed = true; } /** diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index 0b133c0aa28..8dc7be02432 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -22,16 +22,16 @@ contract Crowdsale { using SafeERC20 for IERC20; // The token being sold - IERC20 private token_; + IERC20 private _token; // Address where funds are collected - address private wallet_; + address private _wallet; // How many token units a buyer gets per wei. - uint256 private rate_; + uint256 private _rate; // Amount of wei raised - uint256 private weiRaised_; + uint256 private _weiRaised; /** * Event for token purchase logging @@ -48,21 +48,21 @@ contract Crowdsale { ); /** - * @param _rate Number of token units a buyer gets per wei + * @param rate Number of token units a buyer gets per wei * @dev 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 ERC20Detailed token * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK. - * @param _wallet Address where collected funds will be forwarded to - * @param _token Address of the token being sold + * @param wallet Address where collected funds will be forwarded to + * @param token Address of the token being sold */ - constructor(uint256 _rate, address _wallet, IERC20 _token) public { - require(_rate > 0); - require(_wallet != address(0)); - require(_token != address(0)); - - rate_ = _rate; - wallet_ = _wallet; - token_ = _token; + constructor(uint256 rate, address wallet, IERC20 token) public { + require(rate > 0); + require(wallet != address(0)); + require(token != address(0)); + + _rate = rate; + _wallet = wallet; + _token = token; } // ----------------------------------------- @@ -80,57 +80,57 @@ contract Crowdsale { * @return the token being sold. */ function token() public view returns(IERC20) { - return token_; + return _token; } /** * @return the address where funds are collected. */ function wallet() public view returns(address) { - return wallet_; + return _wallet; } /** * @return the number of token units a buyer gets per wei. */ function rate() public view returns(uint256) { - return rate_; + return _rate; } /** * @return the mount of wei raised. */ function weiRaised() public view returns (uint256) { - return weiRaised_; + return _weiRaised; } /** * @dev low level token purchase ***DO NOT OVERRIDE*** - * @param _beneficiary Address performing the token purchase + * @param beneficiary Address performing the token purchase */ - function buyTokens(address _beneficiary) public payable { + function buyTokens(address beneficiary) public payable { uint256 weiAmount = msg.value; - _preValidatePurchase(_beneficiary, weiAmount); + _preValidatePurchase(beneficiary, weiAmount); // calculate token amount to be created uint256 tokens = _getTokenAmount(weiAmount); // update state - weiRaised_ = weiRaised_.add(weiAmount); + _weiRaised = _weiRaised.add(weiAmount); - _processPurchase(_beneficiary, tokens); + _processPurchase(beneficiary, tokens); emit TokensPurchased( msg.sender, - _beneficiary, + beneficiary, weiAmount, tokens ); - _updatePurchasingState(_beneficiary, weiAmount); + _updatePurchasingState(beneficiary, weiAmount); _forwardFunds(); - _postValidatePurchase(_beneficiary, weiAmount); + _postValidatePurchase(beneficiary, weiAmount); } // ----------------------------------------- @@ -140,29 +140,29 @@ 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: - * super._preValidatePurchase(_beneficiary, _weiAmount); - * require(weiRaised().add(_weiAmount) <= cap); - * @param _beneficiary Address performing the token purchase - * @param _weiAmount Value in wei involved in the purchase + * super._preValidatePurchase(beneficiary, weiAmount); + * require(weiRaised().add(weiAmount) <= cap); + * @param beneficiary Address performing the token purchase + * @param weiAmount Value in wei involved in the purchase */ function _preValidatePurchase( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal { - require(_beneficiary != address(0)); - require(_weiAmount != 0); + require(beneficiary != address(0)); + require(weiAmount != 0); } /** * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met. - * @param _beneficiary Address performing the token purchase - * @param _weiAmount Value in wei involved in the purchase + * @param beneficiary Address performing the token purchase + * @param weiAmount Value in wei involved in the purchase */ function _postValidatePurchase( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal { @@ -171,40 +171,40 @@ contract Crowdsale { /** * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens. - * @param _beneficiary Address performing the token purchase - * @param _tokenAmount Number of tokens to be emitted + * @param beneficiary Address performing the token purchase + * @param tokenAmount Number of tokens to be emitted */ function _deliverTokens( - address _beneficiary, - uint256 _tokenAmount + address beneficiary, + uint256 tokenAmount ) internal { - token_.safeTransfer(_beneficiary, _tokenAmount); + token_.safeTransfer(beneficiary, tokenAmount); } /** * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens. - * @param _beneficiary Address receiving the tokens - * @param _tokenAmount Number of tokens to be purchased + * @param beneficiary Address receiving the tokens + * @param tokenAmount Number of tokens to be purchased */ function _processPurchase( - address _beneficiary, - uint256 _tokenAmount + address beneficiary, + uint256 tokenAmount ) internal { - _deliverTokens(_beneficiary, _tokenAmount); + _deliverTokens(beneficiary, tokenAmount); } /** * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.) - * @param _beneficiary Address receiving the tokens - * @param _weiAmount Value in wei involved in the purchase + * @param beneficiary Address receiving the tokens + * @param weiAmount Value in wei involved in the purchase */ function _updatePurchasingState( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal { @@ -213,19 +213,19 @@ contract Crowdsale { /** * @dev Override to extend the way in which ether is converted to tokens. - * @param _weiAmount Value in wei to be converted into tokens + * @param weiAmount Value in wei to be converted into tokens * @return Number of tokens that can be purchased with the specified _weiAmount */ - function _getTokenAmount(uint256 _weiAmount) + function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) { - return _weiAmount.mul(rate_); + return weiAmount.mul(_rate); } /** * @dev Determines how ETH is stored/forwarded on purchases. */ function _forwardFunds() internal { - wallet_.transfer(msg.value); + _wallet.transfer(msg.value); } } From 24761a56790295e304e566beb50859c31486c25f Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 06:00:45 +0000 Subject: [PATCH 47/67] remove isMinter --- contracts/token/ERC20/ERC20Mintable.sol | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 0ecabe3f4d1..569bea522f5 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier onlyMinter() { - require(isMinter()); + require(isOwner(msg.sender)); _; } @@ -33,13 +33,6 @@ contract ERC20Mintable is ERC20, Ownable { return mintingFinished_; } - /** - * @return true if `msg.sender` is the minter. - */ - function isMinter() public view returns(bool) { - return msg.sender == owner(); - } - /** * @dev Function to mint tokens * @param _to The address that will receive the minted tokens. From c14d59784079cb759f6e85868eba9cb8d09f0838 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 13:19:57 +0000 Subject: [PATCH 48/67] fix is owner call --- contracts/token/ERC20/ERC20Mintable.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 569bea522f5..8b80dd80ae5 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable { } modifier onlyMinter() { - require(isOwner(msg.sender)); + require(isOwner()); _; } From ba7fa16029355cf21b7666fbde61df064ef540b5 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Wed, 5 Sep 2018 13:43:30 +0000 Subject: [PATCH 49/67] fix isOpen --- test/crowdsale/TimedCrowdsale.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/crowdsale/TimedCrowdsale.test.js b/test/crowdsale/TimedCrowdsale.test.js index 5c246482f4a..4c7eff303c2 100644 --- a/test/crowdsale/TimedCrowdsale.test.js +++ b/test/crowdsale/TimedCrowdsale.test.js @@ -50,7 +50,6 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { }); it('should be ended only after end', async function () { - (await this.crowdsale.isOpen()).should.equal(true); (await this.crowdsale.hasClosed()).should.equal(false); await increaseTimeTo(this.afterClosingTime); (await this.crowdsale.isOpen()).should.equal(false); @@ -59,12 +58,14 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { describe('accepting payments', function () { it('should reject payments before start', async function () { + (await this.crowdsale.isOpen()).should.equal(false); 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.isOpen()).should.equal(true); await this.crowdsale.send(value); await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); }); From 7a37725e54f092272b323640d49eb03bb2eebc2e Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Thu, 6 Sep 2018 00:29:05 +0000 Subject: [PATCH 50/67] Fix merge --- contracts/payment/SplitPayment.sol | 35 ------------------------------ 1 file changed, 35 deletions(-) diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index b01fe7fcce3..0d4a2e0956d 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -70,41 +70,6 @@ contract SplitPayment { return payees_[index]; } - /** - * @dev Claim your share of the balance. - */ - function totalShares() public view returns(uint256) { - return totalShares_; - } - - /** - * @return the total amount already released. - */ - function totalReleased() public view returns(uint256) { - return totalReleased_; - } - - /** - * @return the shares of an account. - */ - function shares(address _account) public view returns(uint256) { - return shares_[_account]; - } - - /** - * @return the amount already released to an account. - */ - function released(address _account) public view returns(uint256) { - return released_[_account]; - } - - /** - * @return the address of a payee. - */ - function payee(uint256 index) public view returns(address) { - return payees_[index]; - } - /** * @dev Release one of the payee's proportional payment. * @param _payee Whose payments will be released. From 093a9d5bf110f222169cdfc1db1e60fa3677e73d Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Thu, 6 Sep 2018 00:49:02 +0000 Subject: [PATCH 51/67] tmp --- .../distribution/FinalizableCrowdsale.sol | 8 ++--- .../distribution/PostDeliveryCrowdsale.sol | 26 +++++++-------- .../distribution/RefundableCrowdsale.sol | 32 +++++++++---------- .../crowdsale/emission/AllowanceCrowdsale.sol | 24 +++++++------- .../crowdsale/emission/MintedCrowdsale.sol | 10 +++--- .../price/IncreasingPriceCrowdsale.sol | 32 +++++++++---------- 6 files changed, 66 insertions(+), 66 deletions(-) diff --git a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol index 693e0b498e1..49931e9ec44 100644 --- a/contracts/crowdsale/distribution/FinalizableCrowdsale.sol +++ b/contracts/crowdsale/distribution/FinalizableCrowdsale.sol @@ -13,7 +13,7 @@ import "../validation/TimedCrowdsale.sol"; contract FinalizableCrowdsale is Ownable, TimedCrowdsale { using SafeMath for uint256; - bool private finalized_ = false; + bool private _finalized = false; event CrowdsaleFinalized(); @@ -21,7 +21,7 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { * @return true if the crowdsale is finalized, false otherwise. */ function finalized() public view returns(bool) { - return finalized_; + return _finalized; } /** @@ -29,13 +29,13 @@ contract FinalizableCrowdsale is Ownable, TimedCrowdsale { * work. Calls the contract's finalization function. */ function finalize() public onlyOwner { - require(!finalized_); + require(!_finalized); require(hasClosed()); _finalization(); emit CrowdsaleFinalized(); - finalized_ = true; + _finalized = true; } /** diff --git a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol index aca5e5e8294..5a7d89c7ee0 100644 --- a/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol +++ b/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol @@ -12,39 +12,39 @@ import "../../math/SafeMath.sol"; contract PostDeliveryCrowdsale is TimedCrowdsale { using SafeMath for uint256; - mapping(address => uint256) private balances_; + mapping(address => uint256) private _balances; /** * @dev Withdraw tokens only after crowdsale ends. - * @param _beneficiary Whose tokens will be withdrawn. + * @param beneficiary Whose tokens will be withdrawn. */ - function withdrawTokens(address _beneficiary) public { + function withdrawTokens(address beneficiary) public { require(hasClosed()); - uint256 amount = balances_[_beneficiary]; + uint256 amount = _balances[beneficiary]; require(amount > 0); - balances_[_beneficiary] = 0; - _deliverTokens(_beneficiary, amount); + _balances[beneficiary] = 0; + _deliverTokens(beneficiary, amount); } /** * @return the balance of an account. */ - function balanceOf(address _account) public view returns(uint256) { - return balances_[_account]; + function balanceOf(address account) public view returns(uint256) { + return _balances[account]; } /** * @dev Overrides parent by storing balances instead of issuing tokens right away. - * @param _beneficiary Token purchaser - * @param _tokenAmount Amount of tokens purchased + * @param beneficiary Token purchaser + * @param tokenAmount Amount of tokens purchased */ function _processPurchase( - address _beneficiary, - uint256 _tokenAmount + address beneficiary, + uint256 tokenAmount ) internal { - balances_[_beneficiary] = balances_[_beneficiary].add(_tokenAmount); + _balances[beneficiary] = _balances[beneficiary].add(tokenAmount); } } diff --git a/contracts/crowdsale/distribution/RefundableCrowdsale.sol b/contracts/crowdsale/distribution/RefundableCrowdsale.sol index 6279fbbdeb9..1bd7a19962a 100644 --- a/contracts/crowdsale/distribution/RefundableCrowdsale.sol +++ b/contracts/crowdsale/distribution/RefundableCrowdsale.sol @@ -15,37 +15,37 @@ contract RefundableCrowdsale is FinalizableCrowdsale { using SafeMath for uint256; // minimum amount of funds to be raised in weis - uint256 private goal_; + uint256 private _goal; // refund escrow used to hold funds while crowdsale is running - RefundEscrow private escrow_; + RefundEscrow private _escrow; /** * @dev Constructor, creates RefundEscrow. - * @param _goal Funding goal + * @param goal Funding goal */ - constructor(uint256 _goal) public { - require(_goal > 0); - escrow_ = new RefundEscrow(wallet()); - goal_ = _goal; + constructor(uint256 goal) public { + require(goal > 0); + _escrow = new RefundEscrow(wallet()); + _goal = goal; } /** * @return minimum amount of funds to be raised in wei. */ function goal() public view returns(uint256) { - return goal_; + return _goal; } /** * @dev Investors can claim refunds here if crowdsale is unsuccessful - * @param _beneficiary Whose refund will be claimed. + * @param beneficiary Whose refund will be claimed. */ - function claimRefund(address _beneficiary) public { + function claimRefund(address beneficiary) public { require(finalized()); require(!goalReached()); - escrow_.withdraw(_beneficiary); + _escrow.withdraw(beneficiary); } /** @@ -53,7 +53,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @return Whether funding goal was reached */ function goalReached() public view returns (bool) { - return weiRaised() >= goal_; + return weiRaised() >= _goal; } /** @@ -61,10 +61,10 @@ contract RefundableCrowdsale is FinalizableCrowdsale { */ function _finalization() internal { if (goalReached()) { - escrow_.close(); - escrow_.beneficiaryWithdraw(); + _escrow.close(); + _escrow.beneficiaryWithdraw(); } else { - escrow_.enableRefunds(); + _escrow.enableRefunds(); } super._finalization(); @@ -74,7 +74,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { * @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 54db50af1c9..2af6752fbfe 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -14,22 +14,22 @@ contract AllowanceCrowdsale is Crowdsale { using SafeMath for uint256; using SafeERC20 for IERC20; - address private tokenWallet_; + address private _tokenWallet; /** * @dev Constructor, takes token wallet address. - * @param _tokenWallet Address holding the tokens, which has approved allowance to the crowdsale + * @param tokenWallet Address holding the tokens, which has approved allowance to the crowdsale */ - constructor(address _tokenWallet) public { - require(_tokenWallet != address(0)); - tokenWallet_ = _tokenWallet; + constructor(address tokenWallet) public { + require(tokenWallet != address(0)); + _tokenWallet = tokenWallet; } /** * @return the address of the wallet that will hold the tokens. */ function tokenWallet() public view returns(address) { - return tokenWallet_; + return _tokenWallet; } /** @@ -37,20 +37,20 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return token().allowance(tokenWallet_, this); + return token().allowance(_tokenWallet, this); } /** * @dev Overrides parent behavior by transferring tokens from wallet. - * @param _beneficiary Token purchaser - * @param _tokenAmount Amount of tokens purchased + * @param beneficiary Token purchaser + * @param tokenAmount Amount of tokens purchased */ function _deliverTokens( - address _beneficiary, - uint256 _tokenAmount + address beneficiary, + uint256 tokenAmount ) internal { - token().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount); + token().safeTransferFrom(_tokenWallet, beneficiary, tokenAmount); } } diff --git a/contracts/crowdsale/emission/MintedCrowdsale.sol b/contracts/crowdsale/emission/MintedCrowdsale.sol index 1b676e07767..a57c7b80727 100644 --- a/contracts/crowdsale/emission/MintedCrowdsale.sol +++ b/contracts/crowdsale/emission/MintedCrowdsale.sol @@ -13,17 +13,17 @@ contract MintedCrowdsale is Crowdsale { /** * @dev Overrides delivery by minting tokens upon purchase. - * @param _beneficiary Token purchaser - * @param _tokenAmount Number of tokens to be minted + * @param beneficiary Token purchaser + * @param tokenAmount Number of tokens to be minted */ function _deliverTokens( - address _beneficiary, - uint256 _tokenAmount + address beneficiary, + uint256 tokenAmount ) internal { // Potentially dangerous assumption about the type of the token. require( - ERC20Mintable(address(token())).mint(_beneficiary, _tokenAmount)); + ERC20Mintable(address(token())).mint(beneficiary, tokenAmount)); } } diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index 7cc07f083d5..20043035a42 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -13,33 +13,33 @@ import "../../math/SafeMath.sol"; contract IncreasingPriceCrowdsale is TimedCrowdsale { using SafeMath for uint256; - uint256 private initialRate_; - uint256 private finalRate_; + uint256 private _initialRate; + uint256 private _finalRate; /** * @dev Constructor, takes initial and final rates of tokens received per wei contributed. - * @param _initialRate Number of tokens a buyer gets per wei at the start of the crowdsale - * @param _finalRate Number of tokens a buyer gets per wei at the end of the crowdsale + * @param initialRate Number of tokens a buyer gets per wei at the start of the crowdsale + * @param finalRate Number of tokens a buyer gets per wei at the end of the crowdsale */ - constructor(uint256 _initialRate, uint256 _finalRate) public { - require(_finalRate > 0); - require(_initialRate >= _finalRate); - initialRate_ = _initialRate; - finalRate_ = _finalRate; + constructor(uint256 initialRate, uint256 finalRate) public { + require(finalRate > 0); + require(initialRate >= finalRate); + _initialRate = initialRate; + _finalRate = finalRate; } /** * @return the initial rate of the crowdsale. */ function initialRate() public view returns(uint256) { - return initialRate_; + return _initialRate; } /** * @return the final rate of the crowdsale. */ function finalRate() public view returns (uint256) { - return finalRate_; + return _finalRate; } /** @@ -51,20 +51,20 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { // solium-disable-next-line security/no-block-members uint256 elapsedTime = block.timestamp.sub(openingTime); uint256 timeRange = closingTime.sub(openingTime); - uint256 rateRange = initialRate_.sub(finalRate_); - return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange)); + uint256 rateRange = _initialRate.sub(_finalRate); + return _initialRate.sub(elapsedTime.mul(rateRange).div(timeRange)); } /** * @dev Overrides parent method taking into account variable rate. - * @param _weiAmount The value in wei to be converted into tokens + * @param weiAmount The value in wei to be converted into tokens * @return The number of tokens _weiAmount wei will buy at present time */ - function _getTokenAmount(uint256 _weiAmount) + function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) { uint256 currentRate = getCurrentRate(); - return currentRate.mul(_weiAmount); + return currentRate.mul(weiAmount); } } From 52352ce5ce8cd7aedd559bbc5bb3b5595ad19380 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Thu, 6 Sep 2018 01:06:52 +0000 Subject: [PATCH 52/67] Improve encapsulation on TimedCrowdsale --- .../price/IncreasingPriceCrowdsale.sol | 4 +-- .../crowdsale/validation/TimedCrowdsale.sol | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index 7cc07f083d5..ac910d1c855 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -49,8 +49,8 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { */ function getCurrentRate() public view returns (uint256) { // solium-disable-next-line security/no-block-members - uint256 elapsedTime = block.timestamp.sub(openingTime); - uint256 timeRange = closingTime.sub(openingTime); + uint256 elapsedTime = block.timestamp.sub(openingTime()); + uint256 timeRange = closingTime.sub(openingTime()); uint256 rateRange = initialRate_.sub(finalRate_); return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange)); } diff --git a/contracts/crowdsale/validation/TimedCrowdsale.sol b/contracts/crowdsale/validation/TimedCrowdsale.sol index be2dd7e81db..fd049fc6cb7 100644 --- a/contracts/crowdsale/validation/TimedCrowdsale.sol +++ b/contracts/crowdsale/validation/TimedCrowdsale.sol @@ -11,8 +11,8 @@ import "../Crowdsale.sol"; contract TimedCrowdsale is Crowdsale { using SafeMath for uint256; - uint256 public openingTime; - uint256 public closingTime; + uint256 private openingTime_; + uint256 private closingTime_; /** * @dev Reverts if not in crowdsale time range. @@ -33,8 +33,22 @@ contract TimedCrowdsale is Crowdsale { require(_openingTime >= block.timestamp); require(_closingTime >= _openingTime); - openingTime = _openingTime; - closingTime = _closingTime; + openingTime_ = _openingTime; + closingTime_ = _closingTime; + } + + /** + * @return the crowdsale opening time. + */ + function openingTime() public view returns(uint256) { + return openingTime_; + } + + /** + * @return the crowdsale closing time. + */ + function closingTime() public view returns(uint256) { + return closingTime_; } /** @@ -42,7 +56,7 @@ contract TimedCrowdsale is Crowdsale { */ function isOpen() public view returns (bool) { // solium-disable-next-line security/no-block-members - return block.timestamp >= openingTime && block.timestamp <= closingTime; + return block.timestamp >= openingTime_ && block.timestamp <= closingTime_; } /** @@ -51,7 +65,7 @@ contract TimedCrowdsale is Crowdsale { */ function hasClosed() public view returns (bool) { // solium-disable-next-line security/no-block-members - return block.timestamp > closingTime; + return block.timestamp > closingTime_; } /** From 63b93ccc1d55ec7abcaafcdd268869d82319f201 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Thu, 6 Sep 2018 01:13:08 +0000 Subject: [PATCH 53/67] Add missing parentheses --- contracts/crowdsale/price/IncreasingPriceCrowdsale.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol index ac910d1c855..ad448b319a4 100644 --- a/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol +++ b/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol @@ -50,7 +50,7 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale { function getCurrentRate() public view returns (uint256) { // solium-disable-next-line security/no-block-members uint256 elapsedTime = block.timestamp.sub(openingTime()); - uint256 timeRange = closingTime.sub(openingTime()); + uint256 timeRange = closingTime().sub(openingTime()); uint256 rateRange = initialRate_.sub(finalRate_); return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange)); } From 093bcd37aee6f6da1336b93603c46d48a554b6b3 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Thu, 6 Sep 2018 02:55:42 +0000 Subject: [PATCH 54/67] Use prefix underscore for state variables and no underscore for parameters --- CODE_STYLE.md | 26 +- contracts/crowdsale/Crowdsale.sol | 2 +- .../IndividuallyCappedCrowdsale.sol | 62 ++--- .../crowdsale/validation/TimedCrowdsale.sol | 36 +-- .../validation/WhitelistedCrowdsale.sol | 12 +- contracts/cryptography/ECDSA.sol | 20 +- contracts/cryptography/MerkleProof.sol | 20 +- contracts/drafts/ERC1046/TokenMetadata.sol | 8 +- contracts/drafts/SignatureBouncer.sol | 64 ++--- contracts/drafts/TokenVesting.sol | 116 ++++----- contracts/examples/RBACWithAdmin.sol | 26 +- contracts/examples/SampleCrowdsale.sol | 24 +- contracts/introspection/ERC165Checker.sol | 79 +++--- contracts/introspection/IERC165.sol | 4 +- .../SupportsInterfaceWithLookup.sol | 16 +- contracts/lifecycle/Pausable.sol | 12 +- contracts/math/Math.sol | 14 +- contracts/math/SafeMath.sol | 28 +-- contracts/mocks/AllowanceCrowdsaleImpl.sol | 12 +- contracts/mocks/AutoIncrementingImpl.sol | 6 +- contracts/mocks/CappedCrowdsaleImpl.sol | 12 +- contracts/mocks/ConditionalEscrowMock.sol | 10 +- contracts/mocks/DetailedERC20Mock.sol | 8 +- contracts/mocks/ECDSAMock.sol | 8 +- .../ERC165/ERC165InterfacesSupported.sol | 16 +- contracts/mocks/ERC165CheckerMock.sol | 12 +- contracts/mocks/ERC20BurnableMock.sol | 4 +- contracts/mocks/ERC20Mock.sol | 16 +- contracts/mocks/ERC20PausableMock.sol | 4 +- contracts/mocks/ERC20WithMetadataMock.sol | 4 +- contracts/mocks/ERC721BasicMock.sol | 8 +- contracts/mocks/ERC721Mock.sol | 20 +- contracts/mocks/ERC721PausableMock.sol | 12 +- contracts/mocks/ERC721ReceiverMock.sol | 30 +-- contracts/mocks/FinalizableCrowdsaleImpl.sol | 14 +- contracts/mocks/ForceEther.sol | 4 +- .../mocks/IncreasingPriceCrowdsaleImpl.sol | 18 +- .../mocks/IndividuallyCappedCrowdsaleImpl.sol | 8 +- contracts/mocks/MathMock.sol | 12 +- contracts/mocks/MerkleProofWrapper.sol | 8 +- contracts/mocks/MintedCrowdsaleImpl.sol | 8 +- contracts/mocks/PostDeliveryCrowdsaleImpl.sol | 14 +- contracts/mocks/PullPaymentMock.sol | 4 +- contracts/mocks/RBACCappedTokenMock.sol | 4 +- contracts/mocks/RBACMock.sol | 20 +- contracts/mocks/ReentrancyAttack.sol | 4 +- contracts/mocks/ReentrancyMock.sol | 16 +- contracts/mocks/RefundableCrowdsaleImpl.sol | 18 +- contracts/mocks/SafeERC20Helper.sol | 20 +- contracts/mocks/SafeMathMock.sol | 16 +- contracts/mocks/SignatureBouncerMock.sol | 26 +- .../mocks/SupportsInterfaceWithLookupMock.sol | 4 +- contracts/mocks/TimedCrowdsaleImpl.sol | 14 +- contracts/mocks/WhitelistedCrowdsaleImpl.sol | 8 +- contracts/ownership/CanReclaimToken.sol | 8 +- contracts/ownership/Ownable.sol | 28 +-- contracts/ownership/Superuser.sol | 26 +- contracts/payment/ConditionalEscrow.sol | 10 +- contracts/payment/Escrow.sol | 26 +- contracts/payment/PullPayment.sol | 24 +- contracts/payment/RefundEscrow.sol | 42 ++-- contracts/payment/SplitPayment.sol | 74 +++--- contracts/token/ERC20/ERC20.sol | 156 ++++++------ contracts/token/ERC20/ERC20Burnable.sol | 20 +- contracts/token/ERC20/ERC20Capped.sol | 22 +- contracts/token/ERC20/ERC20Detailed.sol | 20 +- contracts/token/ERC20/ERC20Mintable.sol | 20 +- contracts/token/ERC20/ERC20Pausable.sol | 32 +-- contracts/token/ERC20/IERC20.sol | 10 +- contracts/token/ERC20/RBACMintableToken.sol | 20 +- contracts/token/ERC20/SafeERC20.sol | 26 +- contracts/token/ERC20/TokenTimelock.sol | 32 +-- contracts/token/ERC721/ERC721.sol | 148 +++++------ contracts/token/ERC721/ERC721Basic.sol | 232 +++++++++--------- contracts/token/ERC721/ERC721Pausable.sol | 20 +- contracts/token/ERC721/IERC721.sol | 14 +- contracts/token/ERC721/IERC721Basic.sol | 26 +- contracts/token/ERC721/IERC721Receiver.sol | 16 +- contracts/utils/Address.sol | 6 +- contracts/utils/AutoIncrementing.sol | 6 +- contracts/utils/ReentrancyGuard.sol | 8 +- 81 files changed, 1023 insertions(+), 1040 deletions(-) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 46aaa2e6bd2..2eaac05c93b 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -16,35 +16,19 @@ Any exception or additions specific to our project are documented below. * Try to avoid acronyms and abbreviations. -* Parameters must be prefixed with an underscore. - - ``` - 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. +* Private state variables should have an underscore prefix. ``` contract TestContract { - uint256 internal internalVar_; - uint256 private privateVar_; + uint256 private _privateVar; } ``` - Variables declared in a function should not follow this rule. +* Parameters must not be prefixed with an underscore. ``` - function test() { - uint256 functionVar; - ... + function test(uint256 testParameter1, uint256 testParameter2) { + ... } ``` diff --git a/contracts/crowdsale/Crowdsale.sol b/contracts/crowdsale/Crowdsale.sol index 8dc7be02432..36515c81354 100644 --- a/contracts/crowdsale/Crowdsale.sol +++ b/contracts/crowdsale/Crowdsale.sol @@ -180,7 +180,7 @@ contract Crowdsale { ) internal { - token_.safeTransfer(beneficiary, tokenAmount); + _token.safeTransfer(beneficiary, tokenAmount); } /** diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index f0c3fdd4c43..2cd4d0aca47 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -12,85 +12,85 @@ import "../../ownership/Ownable.sol"; contract IndividuallyCappedCrowdsale is Ownable, Crowdsale { using SafeMath for uint256; - mapping(address => uint256) private contributions_; - mapping(address => uint256) private caps_; + mapping(address => uint256) private _contributions; + mapping(address => uint256) private _caps; /** * @dev Sets a specific user's maximum contribution. - * @param _beneficiary Address to be capped - * @param _cap Wei limit for individual contribution + * @param beneficiary Address to be capped + * @param cap Wei limit for individual contribution */ - function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner { - caps_[_beneficiary] = _cap; + function setUserCap(address beneficiary, uint256 cap) external onlyOwner { + _caps[beneficiary] = cap; } /** * @dev Sets a group of users' maximum contribution. - * @param _beneficiaries List of addresses to be capped - * @param _cap Wei limit for individual contribution + * @param beneficiaries List of addresses to be capped + * @param cap Wei limit for individual contribution */ function setGroupCap( - address[] _beneficiaries, - uint256 _cap + address[] beneficiaries, + uint256 cap ) external onlyOwner { - for (uint256 i = 0; i < _beneficiaries.length; i++) { - caps_[_beneficiaries[i]] = _cap; + for (uint256 i = 0; i < beneficiaries.length; i++) { + _caps[beneficiaries[i]] = cap; } } /** * @dev Returns the cap of a specific user. - * @param _beneficiary Address whose cap is to be checked + * @param beneficiary Address whose cap is to be checked * @return Current cap for individual user */ - function getUserCap(address _beneficiary) public view returns (uint256) { - return caps_[_beneficiary]; + function getUserCap(address beneficiary) public view returns (uint256) { + return _caps[beneficiary]; } /** * @dev Returns the amount contributed so far by a sepecific user. - * @param _beneficiary Address of contributor + * @param beneficiary Address of contributor * @return User contribution so far */ - function getUserContribution(address _beneficiary) + function getUserContribution(address beneficiary) public view returns (uint256) { - return contributions_[_beneficiary]; + return _contributions[beneficiary]; } /** * @dev Extend parent behavior requiring purchase to respect the user's funding cap. - * @param _beneficiary Token purchaser - * @param _weiAmount Amount of wei contributed + * @param beneficiary Token purchaser + * @param weiAmount Amount of wei contributed */ function _preValidatePurchase( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal { - super._preValidatePurchase(_beneficiary, _weiAmount); + super._preValidatePurchase(beneficiary, weiAmount); require( - contributions_[_beneficiary].add(_weiAmount) <= caps_[_beneficiary]); + _contributions[beneficiary].add(weiAmount) <= _caps[beneficiary]); } /** * @dev Extend parent behavior to update user contributions - * @param _beneficiary Token purchaser - * @param _weiAmount Amount of wei contributed + * @param beneficiary Token purchaser + * @param weiAmount Amount of wei contributed */ function _updatePurchasingState( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal { - super._updatePurchasingState(_beneficiary, _weiAmount); - contributions_[_beneficiary] = contributions_[_beneficiary].add( - _weiAmount); + super._updatePurchasingState(beneficiary, weiAmount); + _contributions[beneficiary] = _contributions[beneficiary].add( + weiAmount); } } diff --git a/contracts/crowdsale/validation/TimedCrowdsale.sol b/contracts/crowdsale/validation/TimedCrowdsale.sol index fd049fc6cb7..3253dbaf28c 100644 --- a/contracts/crowdsale/validation/TimedCrowdsale.sol +++ b/contracts/crowdsale/validation/TimedCrowdsale.sol @@ -11,8 +11,8 @@ import "../Crowdsale.sol"; contract TimedCrowdsale is Crowdsale { using SafeMath for uint256; - uint256 private openingTime_; - uint256 private closingTime_; + uint256 private _openingTime; + uint256 private _closingTime; /** * @dev Reverts if not in crowdsale time range. @@ -25,30 +25,30 @@ contract TimedCrowdsale is Crowdsale { /** * @dev Constructor, takes crowdsale opening and closing times. - * @param _openingTime Crowdsale opening time - * @param _closingTime Crowdsale closing time + * @param openingTime Crowdsale opening time + * @param closingTime Crowdsale closing time */ - constructor(uint256 _openingTime, uint256 _closingTime) public { + constructor(uint256 openingTime, uint256 closingTime) public { // solium-disable-next-line security/no-block-members - require(_openingTime >= block.timestamp); - require(_closingTime >= _openingTime); + require(openingTime >= block.timestamp); + require(closingTime >= openingTime); - openingTime_ = _openingTime; - closingTime_ = _closingTime; + _openingTime = openingTime; + _closingTime = closingTime; } /** * @return the crowdsale opening time. */ function openingTime() public view returns(uint256) { - return openingTime_; + return _openingTime; } /** * @return the crowdsale closing time. */ function closingTime() public view returns(uint256) { - return closingTime_; + return _closingTime; } /** @@ -56,7 +56,7 @@ contract TimedCrowdsale is Crowdsale { */ function isOpen() public view returns (bool) { // solium-disable-next-line security/no-block-members - return block.timestamp >= openingTime_ && block.timestamp <= closingTime_; + return block.timestamp >= _openingTime && block.timestamp <= _closingTime; } /** @@ -65,22 +65,22 @@ contract TimedCrowdsale is Crowdsale { */ function hasClosed() public view returns (bool) { // solium-disable-next-line security/no-block-members - return block.timestamp > closingTime_; + return block.timestamp > _closingTime; } /** * @dev Extend parent behavior requiring to be within contributing period - * @param _beneficiary Token purchaser - * @param _weiAmount Amount of wei contributed + * @param beneficiary Token purchaser + * @param weiAmount Amount of wei contributed */ function _preValidatePurchase( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal onlyWhileOpen { - super._preValidatePurchase(_beneficiary, _weiAmount); + super._preValidatePurchase(beneficiary, weiAmount); } } diff --git a/contracts/crowdsale/validation/WhitelistedCrowdsale.sol b/contracts/crowdsale/validation/WhitelistedCrowdsale.sol index e3d1de08e7f..dd77e3a1be0 100644 --- a/contracts/crowdsale/validation/WhitelistedCrowdsale.sol +++ b/contracts/crowdsale/validation/WhitelistedCrowdsale.sol @@ -11,17 +11,17 @@ import "../../access/Whitelist.sol"; 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 + * @param beneficiary Token beneficiary + * @param weiAmount Amount of wei contributed */ function _preValidatePurchase( - address _beneficiary, - uint256 _weiAmount + address beneficiary, + uint256 weiAmount ) internal - onlyIfWhitelisted(_beneficiary) + onlyIfWhitelisted(beneficiary) { - super._preValidatePurchase(_beneficiary, _weiAmount); + super._preValidatePurchase(beneficiary, weiAmount); } } diff --git a/contracts/cryptography/ECDSA.sol b/contracts/cryptography/ECDSA.sol index fc14498bc4c..61d19f20c97 100644 --- a/contracts/cryptography/ECDSA.sol +++ b/contracts/cryptography/ECDSA.sol @@ -12,10 +12,10 @@ library ECDSA { /** * @dev Recover signer address from a message by using their signature - * @param _hash bytes32 message, the hash is the signed message. What is recovered is the signer address. - * @param _signature bytes signature, the signature is generated using web3.eth.sign() + * @param hash bytes32 message, the hash is the signed message. What is recovered is the signer address. + * @param signature bytes signature, the signature is generated using web3.eth.sign() */ - function recover(bytes32 _hash, bytes _signature) + function recover(bytes32 hash, bytes signature) internal pure returns (address) @@ -25,7 +25,7 @@ library ECDSA { uint8 v; // Check the signature length - if (_signature.length != 65) { + if (signature.length != 65) { return (address(0)); } @@ -34,9 +34,9 @@ library ECDSA { // currently is to use assembly. // solium-disable-next-line security/no-inline-assembly assembly { - r := mload(add(_signature, 32)) - s := mload(add(_signature, 64)) - v := byte(0, mload(add(_signature, 96))) + r := mload(add(signature, 32)) + s := mload(add(signature, 64)) + v := byte(0, mload(add(signature, 96))) } // Version of signature should be 27 or 28, but 0 and 1 are also possible versions @@ -49,7 +49,7 @@ library ECDSA { return (address(0)); } else { // solium-disable-next-line arg-overflow - return ecrecover(_hash, v, r, s); + return ecrecover(hash, v, r, s); } } @@ -58,7 +58,7 @@ library ECDSA { * @dev prefix a bytes32 value with "\x19Ethereum Signed Message:" * and hash the result */ - function toEthSignedMessageHash(bytes32 _hash) + function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) @@ -66,7 +66,7 @@ library ECDSA { // 32 is the length in bytes of hash, // enforced by the type signature above return keccak256( - abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash) + abi.encodePacked("\x19Ethereum Signed Message:\n32", hash) ); } } diff --git a/contracts/cryptography/MerkleProof.sol b/contracts/cryptography/MerkleProof.sol index 26a8b350c93..3f4e86140c4 100644 --- a/contracts/cryptography/MerkleProof.sol +++ b/contracts/cryptography/MerkleProof.sol @@ -10,23 +10,23 @@ library MerkleProof { /** * @dev Verifies a Merkle proof proving the existence of a leaf in a Merkle tree. Assumes that each pair of leaves * and each pair of pre-images are sorted. - * @param _proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree - * @param _root Merkle root - * @param _leaf Leaf of Merkle tree + * @param proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree + * @param root Merkle root + * @param leaf Leaf of Merkle tree */ function verifyProof( - bytes32[] _proof, - bytes32 _root, - bytes32 _leaf + bytes32[] proof, + bytes32 root, + bytes32 leaf ) internal pure returns (bool) { - bytes32 computedHash = _leaf; + bytes32 computedHash = leaf; - for (uint256 i = 0; i < _proof.length; i++) { - bytes32 proofElement = _proof[i]; + for (uint256 i = 0; i < proof.length; i++) { + bytes32 proofElement = proof[i]; if (computedHash < proofElement) { // Hash(current computed hash + current element of the proof) @@ -38,6 +38,6 @@ library MerkleProof { } // Check if the computed hash (root) is equal to the provided root - return computedHash == _root; + return computedHash == root; } } diff --git a/contracts/drafts/ERC1046/TokenMetadata.sol b/contracts/drafts/ERC1046/TokenMetadata.sol index 6efb4ed72db..0c51d18e811 100644 --- a/contracts/drafts/ERC1046/TokenMetadata.sol +++ b/contracts/drafts/ERC1046/TokenMetadata.sol @@ -15,15 +15,15 @@ contract ERC20TokenMetadata is IERC20 { contract ERC20WithMetadata is ERC20TokenMetadata { - string private tokenURI_ = ""; + string private _tokenURI = ""; - constructor(string _tokenURI) + constructor(string tokenURI) public { - tokenURI_ = _tokenURI; + _tokenURI = tokenURI; } function tokenURI() external view returns (string) { - return tokenURI_; + return _tokenURI; } } diff --git a/contracts/drafts/SignatureBouncer.sol b/contracts/drafts/SignatureBouncer.sol index 0b91dcdb3fb..99bfe2ae4a0 100644 --- a/contracts/drafts/SignatureBouncer.sol +++ b/contracts/drafts/SignatureBouncer.sol @@ -33,37 +33,37 @@ contract SignatureBouncer is Ownable, RBAC { using ECDSA for bytes32; // Name of the bouncer role. - string private constant ROLE_BOUNCER = "bouncer"; + 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; + 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; + uint256 private constant _SIGNATURE_SIZE = 96; /** * @dev requires that a valid signature of a bouncer was provided */ - modifier onlyValidSignature(bytes _signature) + modifier onlyValidSignature(bytes signature) { - require(_isValidSignature(msg.sender, _signature)); + require(_isValidSignature(msg.sender, signature)); _; } /** * @dev requires that a valid signature with a specifed method of a bouncer was provided */ - modifier onlyValidSignatureAndMethod(bytes _signature) + modifier onlyValidSignatureAndMethod(bytes signature) { - require(_isValidSignatureAndMethod(msg.sender, _signature)); + require(_isValidSignatureAndMethod(msg.sender, signature)); _; } /** * @dev requires that a valid signature with a specifed method and params of a bouncer was provided */ - modifier onlyValidSignatureAndData(bytes _signature) + modifier onlyValidSignatureAndData(bytes signature) { - require(_isValidSignatureAndData(msg.sender, _signature)); + require(_isValidSignatureAndData(msg.sender, signature)); _; } @@ -71,43 +71,43 @@ 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); + 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) + function addBouncer(address bouncer) public onlyOwner { - require(_bouncer != address(0)); - _addRole(_bouncer, ROLE_BOUNCER); + require(bouncer != address(0)); + _addRole(bouncer, _ROLE_BOUNCER); } /** * @dev allows the owner to remove bouncer addresses */ - function removeBouncer(address _bouncer) + function removeBouncer(address bouncer) 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 account, bytes signature) internal view returns (bool) { return _isValidDataHash( - keccak256(abi.encodePacked(address(this), _address)), - _signature + keccak256(abi.encodePacked(address(this), account)), + signature ); } @@ -115,39 +115,39 @@ 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 account, bytes signature) internal view returns (bool) { - bytes memory data = new bytes(METHOD_ID_SIZE); + bytes memory data = new bytes(_METHOD_ID_SIZE); for (uint i = 0; i < data.length; i++) { data[i] = msg.data[i]; } return _isValidDataHash( - keccak256(abi.encodePacked(address(this), _address, data)), - _signature + keccak256(abi.encodePacked(address(this), account, data)), + signature ); } /** * @dev is the signature of `this + sender + methodId + params(s)` from a bouncer? - * @notice the _signature parameter of the method being validated must be the "last" parameter + * @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 account, bytes signature) internal view returns (bool) { - require(msg.data.length > SIGNATURE_SIZE); - bytes memory data = new bytes(msg.data.length - SIGNATURE_SIZE); + require(msg.data.length > _SIGNATURE_SIZE); + bytes memory data = new bytes(msg.data.length - _SIGNATURE_SIZE); for (uint i = 0; i < data.length; i++) { data[i] = msg.data[i]; } return _isValidDataHash( - keccak256(abi.encodePacked(address(this), _address, data)), - _signature + keccak256(abi.encodePacked(address(this), account, data)), + signature ); } @@ -156,14 +156,14 @@ 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) { - address signer = _hash + address signer = hash .toEthSignedMessageHash() - .recover(_signature); + .recover(signature); return isBouncer(signer); } } diff --git a/contracts/drafts/TokenVesting.sol b/contracts/drafts/TokenVesting.sol index cfe7697d093..75b2aaa6613 100644 --- a/contracts/drafts/TokenVesting.sol +++ b/contracts/drafts/TokenVesting.sol @@ -21,107 +21,107 @@ contract TokenVesting is Ownable { event Revoked(); // beneficiary of tokens after they are released - address private beneficiary_; + address private _beneficiary; - uint256 private cliff_; - uint256 private start_; - uint256 private duration_; + uint256 private _cliff; + uint256 private _start; + uint256 private _duration; - bool private revocable_; + bool private _revocable; - mapping (address => uint256) private released_; - mapping (address => bool) private revoked_; + mapping (address => uint256) private _released; + mapping (address => bool) private _revoked; /** * @dev Creates a vesting contract that vests its balance of any ERC20 token to the - * _beneficiary, gradually in a linear fashion until _start + _duration. By then all + * beneficiary, gradually in a linear fashion until start + duration. By then all * of the balance will have vested. - * @param _beneficiary address of the beneficiary to whom vested tokens are transferred - * @param _cliffDuration duration in seconds of the cliff in which tokens will begin to vest - * @param _start the time (as Unix time) at which point vesting starts - * @param _duration duration in seconds of the period in which the tokens will vest - * @param _revocable whether the vesting is revocable or not + * @param beneficiary address of the beneficiary to whom vested tokens are transferred + * @param cliffDuration duration in seconds of the cliff in which tokens will begin to vest + * @param start the time (as Unix time) at which point vesting starts + * @param duration duration in seconds of the period in which the tokens will vest + * @param revocable whether the vesting is revocable or not */ constructor( - address _beneficiary, - uint256 _start, - uint256 _cliffDuration, - uint256 _duration, - bool _revocable + address beneficiary, + uint256 start, + uint256 cliffDuration, + uint256 duration, + bool revocable ) public { - require(_beneficiary != address(0)); - require(_cliffDuration <= _duration); - - beneficiary_ = _beneficiary; - revocable_ = _revocable; - duration_ = _duration; - cliff_ = _start.add(_cliffDuration); - start_ = _start; + require(beneficiary != address(0)); + require(cliffDuration <= duration); + + _beneficiary = beneficiary; + _revocable = revocable; + _duration = duration; + _cliff = start.add(cliffDuration); + _start = start; } /** * @return the beneficiary of the tokens. */ function beneficiary() public view returns(address) { - return beneficiary_; + return _beneficiary; } /** * @return the cliff time of the token vesting. */ function cliff() public view returns(uint256) { - return cliff_; + return _cliff; } /** * @return the start time of the token vesting. */ function start() public view returns(uint256) { - return start_; + return _start; } /** * @return the duration of the token vesting. */ function duration() public view returns(uint256) { - return duration_; + return _duration; } /** * @return true if the vesting is revocable. */ function revocable() public view returns(bool) { - return revocable_; + return _revocable; } /** * @return the amount of the token released. */ - function released(address _token) public view returns(uint256) { - return released_[_token]; + function released(address token) public view returns(uint256) { + return _released[token]; } /** * @return true if the token is revoked. */ - function revoked(address _token) public view returns(bool) { - return revoked_[_token]; + function revoked(address token) public view returns(bool) { + return _revoked[token]; } /** * @notice Transfers vested tokens to beneficiary. - * @param _token ERC20 token which is being vested + * @param token ERC20 token which is being vested */ - function release(IERC20 _token) public { - uint256 unreleased = releasableAmount(_token); + function release(IERC20 token) public { + uint256 unreleased = releasableAmount(token); require(unreleased > 0); - released_[_token] = released_[_token].add(unreleased); + _released[token] = _released[token].add(unreleased); - _token.safeTransfer(beneficiary_, unreleased); + token.safeTransfer(_beneficiary, unreleased); emit Released(unreleased); } @@ -129,46 +129,46 @@ contract TokenVesting is Ownable { /** * @notice Allows the owner to revoke the vesting. Tokens already vested * remain in the contract, the rest are returned to the owner. - * @param _token ERC20 token which is being vested + * @param token ERC20 token which is being vested */ - function revoke(IERC20 _token) public onlyOwner { - require(revocable_); - require(!revoked_[_token]); + function revoke(IERC20 token) public onlyOwner { + require(_revocable); + require(!_revoked[token]); - uint256 balance = _token.balanceOf(address(this)); + uint256 balance = token.balanceOf(address(this)); - uint256 unreleased = releasableAmount(_token); + uint256 unreleased = releasableAmount(token); uint256 refund = balance.sub(unreleased); - revoked_[_token] = true; + _revoked[token] = true; - _token.safeTransfer(owner(), refund); + token.safeTransfer(owner(), refund); emit Revoked(); } /** * @dev Calculates the amount that has already vested but hasn't been released yet. - * @param _token ERC20 token which is being vested + * @param token ERC20 token which is being vested */ - function releasableAmount(IERC20 _token) public view returns (uint256) { - return vestedAmount(_token).sub(released_[_token]); + function releasableAmount(IERC20 token) public view returns (uint256) { + return vestedAmount(token).sub(_released[token]); } /** * @dev Calculates the amount that has already vested. - * @param _token ERC20 token which is being vested + * @param token ERC20 token which is being vested */ - function vestedAmount(IERC20 _token) public view returns (uint256) { - uint256 currentBalance = _token.balanceOf(this); - uint256 totalBalance = currentBalance.add(released_[_token]); + function vestedAmount(IERC20 token) public view returns (uint256) { + uint256 currentBalance = token.balanceOf(this); + uint256 totalBalance = currentBalance.add(_released[token]); - if (block.timestamp < cliff_) { + if (block.timestamp < _cliff) { return 0; - } else if (block.timestamp >= start_.add(duration_) || revoked_[_token]) { + } else if (block.timestamp >= _start.add(_duration) || _revoked[token]) { return totalBalance; } else { - return totalBalance.mul(block.timestamp.sub(start_)).div(duration_); + return totalBalance.mul(block.timestamp.sub(_start)).div(_duration); } } } diff --git a/contracts/examples/RBACWithAdmin.sol b/contracts/examples/RBACWithAdmin.sol index 83c2dcef7a8..b3235006762 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 private constant ROLE_ADMIN = "admin"; + string private constant _ROLE_ADMIN = "admin"; /** * @dev modifier to scope access to admins @@ -27,7 +27,7 @@ contract RBACWithAdmin is RBAC { */ modifier onlyAdmin() { - checkRole(msg.sender, ROLE_ADMIN); + checkRole(msg.sender, _ROLE_ADMIN); _; } @@ -37,37 +37,37 @@ 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); + 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 + * @param account the account that will have the role + * @param roleName the name of the role */ - function adminAddRole(address _account, string _roleName) + function adminAddRole(address account, string roleName) public onlyAdmin { - _addRole(_account, _roleName); + _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 + * @param account the account that will no longer have the role + * @param roleName the name of the role */ - function adminRemoveRole(address _account, string _roleName) + function adminRemoveRole(address account, string roleName) public onlyAdmin { - _removeRole(_account, _roleName); + _removeRole(account, roleName); } } diff --git a/contracts/examples/SampleCrowdsale.sol b/contracts/examples/SampleCrowdsale.sol index 564c6a0873e..633382abc13 100644 --- a/contracts/examples/SampleCrowdsale.sol +++ b/contracts/examples/SampleCrowdsale.sol @@ -39,22 +39,22 @@ contract SampleCrowdsaleToken is ERC20Mintable { contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale, MintedCrowdsale { constructor( - uint256 _openingTime, - uint256 _closingTime, - uint256 _rate, - address _wallet, - uint256 _cap, - ERC20Mintable _token, - uint256 _goal + uint256 openingTime, + uint256 closingTime, + uint256 rate, + address wallet, + uint256 cap, + ERC20Mintable token, + uint256 goal ) public - Crowdsale(_rate, _wallet, _token) - CappedCrowdsale(_cap) - TimedCrowdsale(_openingTime, _closingTime) - RefundableCrowdsale(_goal) + Crowdsale(rate, wallet, token) + CappedCrowdsale(cap) + TimedCrowdsale(openingTime, closingTime) + RefundableCrowdsale(goal) { //As goal needs to be met for a successful crowdsale //the value needs to less or equal than a cap which is limit for accepted funds - require(_goal <= _cap); + require(goal <= cap); } } diff --git a/contracts/introspection/ERC165Checker.sol b/contracts/introspection/ERC165Checker.sol index 9bb88130936..36bc2095981 100644 --- a/contracts/introspection/ERC165Checker.sol +++ b/contracts/introspection/ERC165Checker.sol @@ -8,9 +8,9 @@ pragma solidity ^0.4.24; */ library ERC165Checker { // As per the EIP-165 spec, no interface should ever match 0xffffffff - bytes4 private constant InterfaceId_Invalid = 0xffffffff; + bytes4 private constant _InterfaceId_Invalid = 0xffffffff; - bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7; + bytes4 private constant _InterfaceId_ERC165 = 0x01ffc9a7; /** * 0x01ffc9a7 === * bytes4(keccak256('supportsInterface(bytes4)')) @@ -19,59 +19,59 @@ library ERC165Checker { /** * @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 + * @param account The address of the contract to query for support of ERC165 + * @return true if the contract at account implements ERC165 */ - function supportsERC165(address _address) + function supportsERC165(address account) 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); + return supportsERC165Interface(account, _InterfaceId_ERC165) && + !supportsERC165Interface(account, _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 + * @param account 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 account indicates support of the interface with + * identifier interfaceId, false otherwise * @dev Interface identification is specified in ERC-165. */ - function supportsInterface(address _address, bytes4 _interfaceId) + function supportsInterface(address account, 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); + return supportsERC165(account) && + supportsERC165Interface(account, 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 + * @param account 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 account indicates support all interfaces in the + * interfaceIds list, false otherwise * @dev Interface identification is specified in ERC-165. */ - function supportsInterfaces(address _address, bytes4[] _interfaceIds) + function supportsInterfaces(address account, bytes4[] interfaceIds) internal view returns (bool) { // query support of ERC165 itself - if (!supportsERC165(_address)) { + if (!supportsERC165(account)) { return false; } // query support of each interface in _interfaceIds - for (uint256 i = 0; i < _interfaceIds.length; i++) { - if (!supportsERC165Interface(_address, _interfaceIds[i])) { + for (uint256 i = 0; i < interfaceIds.length; i++) { + if (!supportsERC165Interface(account, interfaceIds[i])) { return false; } } @@ -82,47 +82,47 @@ library ERC165Checker { /** * @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 + * @param account 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 account indicates support of the interface with + * identifier interfaceId, false otherwise + * @dev Assumes that account 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) + function supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) { // success determines whether the staticcall succeeded and result determines - // whether the contract at _address indicates support of _interfaceId + // whether the contract at account indicates support of _interfaceId (bool success, bool result) = callERC165SupportsInterface( - _address, _interfaceId); + account, 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 + * @param account 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 + * @return result true if the STATICCALL succeeded and the contract at account + * indicates support of the interface with identifier interfaceId, false otherwise */ function callERC165SupportsInterface( - address _address, - bytes4 _interfaceId + address account, + bytes4 interfaceId ) private view returns (bool success, bool result) { bytes memory encodedParams = abi.encodeWithSelector( - InterfaceId_ERC165, - _interfaceId + _InterfaceId_ERC165, + interfaceId ); // solium-disable-next-line security/no-inline-assembly @@ -135,7 +135,7 @@ library ERC165Checker { success := staticcall( 30000, // 30k gas - _address, // To addr + account, // To addr encodedParams_data, encodedParams_size, output, @@ -146,4 +146,3 @@ library ERC165Checker { } } } - diff --git a/contracts/introspection/IERC165.sol b/contracts/introspection/IERC165.sol index f3361f0a46d..4d8b5b69f4c 100644 --- a/contracts/introspection/IERC165.sol +++ b/contracts/introspection/IERC165.sol @@ -9,11 +9,11 @@ interface IERC165 { /** * @notice Query if a contract implements an interface - * @param _interfaceId The interface identifier, as specified in ERC-165 + * @param interfaceId The interface identifier, as specified in ERC-165 * @dev Interface identification is specified in ERC-165. This function * uses less than 30,000 gas. */ - function supportsInterface(bytes4 _interfaceId) + function supportsInterface(bytes4 interfaceId) external view returns (bool); diff --git a/contracts/introspection/SupportsInterfaceWithLookup.sol b/contracts/introspection/SupportsInterfaceWithLookup.sol index 6fca22ac8ca..63223c7dadb 100644 --- a/contracts/introspection/SupportsInterfaceWithLookup.sol +++ b/contracts/introspection/SupportsInterfaceWithLookup.sol @@ -10,7 +10,7 @@ import "./IERC165.sol"; */ contract SupportsInterfaceWithLookup is IERC165 { - bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7; + bytes4 private constant _InterfaceId_ERC165 = 0x01ffc9a7; /** * 0x01ffc9a7 === * bytes4(keccak256('supportsInterface(bytes4)')) @@ -19,7 +19,7 @@ contract SupportsInterfaceWithLookup is IERC165 { /** * @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 @@ -28,27 +28,27 @@ contract SupportsInterfaceWithLookup is IERC165 { constructor() public { - _registerInterface(InterfaceId_ERC165); + _registerInterface(_InterfaceId_ERC165); } /** * @dev implement supportsInterface(bytes4) using a lookup table */ - function supportsInterface(bytes4 _interfaceId) + function supportsInterface(bytes4 interfaceId) external view returns (bool) { - return supportedInterfaces_[_interfaceId]; + return _supportedInterfaces[interfaceId]; } /** * @dev private method for registering an interface */ - function _registerInterface(bytes4 _interfaceId) + function _registerInterface(bytes4 interfaceId) internal { - require(_interfaceId != 0xffffffff); - supportedInterfaces_[_interfaceId] = true; + require(interfaceId != 0xffffffff); + _supportedInterfaces[interfaceId] = true; } } diff --git a/contracts/lifecycle/Pausable.sol b/contracts/lifecycle/Pausable.sol index 37caf1e0a52..b2ff73f543d 100644 --- a/contracts/lifecycle/Pausable.sol +++ b/contracts/lifecycle/Pausable.sol @@ -12,21 +12,21 @@ contract Pausable is Ownable { event Paused(); event Unpaused(); - bool private paused_ = false; + bool private _paused = false; /** * @return true if the contract is paused, false otherwise. */ function paused() public view returns(bool) { - return paused_; + return _paused; } /** * @dev Modifier to make a function callable only when the contract is not paused. */ modifier whenNotPaused() { - require(!paused_); + require(!_paused); _; } @@ -34,7 +34,7 @@ contract Pausable is Ownable { * @dev Modifier to make a function callable only when the contract is paused. */ modifier whenPaused() { - require(paused_); + require(_paused); _; } @@ -42,7 +42,7 @@ contract Pausable is Ownable { * @dev called by the owner to pause, triggers stopped state */ function pause() public onlyOwner whenNotPaused { - paused_ = true; + _paused = true; emit Paused(); } @@ -50,7 +50,7 @@ contract Pausable is Ownable { * @dev called by the owner to unpause, returns to normal state */ function unpause() public onlyOwner whenPaused { - paused_ = false; + _paused = false; emit Unpaused(); } } diff --git a/contracts/math/Math.sol b/contracts/math/Math.sol index 101b35d49ca..78ef1ae2b4d 100644 --- a/contracts/math/Math.sol +++ b/contracts/math/Math.sol @@ -6,16 +6,16 @@ pragma solidity ^0.4.24; * @dev Assorted math operations */ library Math { - function max(uint256 _a, uint256 _b) internal pure returns (uint256) { - return _a >= _b ? _a : _b; + function max(uint256 a, uint256 b) internal pure returns (uint256) { + return a >= b ? a : b; } - function min(uint256 _a, uint256 _b) internal pure returns (uint256) { - return _a < _b ? _a : _b; + function min(uint256 a, uint256 b) internal pure returns (uint256) { + return a < b ? a : b; } - function average(uint256 _a, uint256 _b) internal pure returns (uint256) { - // (_a + _b) / 2 can overflow, so we distribute - return (_a / 2) + (_b / 2) + ((_a % 2 + _b % 2) / 2); + function average(uint256 a, uint256 b) internal pure returns (uint256) { + // (a + b) / 2 can overflow, so we distribute + return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); } } diff --git a/contracts/math/SafeMath.sol b/contracts/math/SafeMath.sol index da591c3b207..d183fa361bf 100644 --- a/contracts/math/SafeMath.sol +++ b/contracts/math/SafeMath.sol @@ -10,16 +10,16 @@ library SafeMath { /** * @dev Multiplies two numbers, reverts on overflow. */ - function mul(uint256 _a, uint256 _b) internal pure returns (uint256) { + function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 - if (_a == 0) { + if (a == 0) { return 0; } - uint256 c = _a * _b; - require(c / _a == _b); + uint256 c = a * b; + require(c / a == b); return c; } @@ -27,10 +27,10 @@ library SafeMath { /** * @dev Integer division of two numbers truncating the quotient, reverts on division by zero. */ - function div(uint256 _a, uint256 _b) internal pure returns (uint256) { - require(_b > 0); // Solidity only automatically asserts when dividing by 0 - uint256 c = _a / _b; - // assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold + function div(uint256 a, uint256 b) internal pure returns (uint256) { + require(b > 0); // Solidity only automatically asserts when dividing by 0 + uint256 c = a / b; + // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } @@ -38,9 +38,9 @@ library SafeMath { /** * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend). */ - function sub(uint256 _a, uint256 _b) internal pure returns (uint256) { - require(_b <= _a); - uint256 c = _a - _b; + function sub(uint256 a, uint256 b) internal pure returns (uint256) { + require(b <= a); + uint256 c = a - b; return c; } @@ -48,9 +48,9 @@ library SafeMath { /** * @dev Adds two numbers, reverts on overflow. */ - function add(uint256 _a, uint256 _b) internal pure returns (uint256) { - uint256 c = _a + _b; - require(c >= _a); + function add(uint256 a, uint256 b) internal pure returns (uint256) { + uint256 c = a + b; + require(c >= a); return c; } diff --git a/contracts/mocks/AllowanceCrowdsaleImpl.sol b/contracts/mocks/AllowanceCrowdsaleImpl.sol index 872dd66e193..be01ccdf938 100644 --- a/contracts/mocks/AllowanceCrowdsaleImpl.sol +++ b/contracts/mocks/AllowanceCrowdsaleImpl.sol @@ -7,14 +7,14 @@ import "../crowdsale/emission/AllowanceCrowdsale.sol"; contract AllowanceCrowdsaleImpl is AllowanceCrowdsale { constructor ( - uint256 _rate, - address _wallet, - IERC20 _token, - address _tokenWallet + uint256 rate, + address wallet, + IERC20 token, + address tokenWallet ) public - Crowdsale(_rate, _wallet, _token) - AllowanceCrowdsale(_tokenWallet) + Crowdsale(rate, wallet, token) + AllowanceCrowdsale(tokenWallet) { } diff --git a/contracts/mocks/AutoIncrementingImpl.sol b/contracts/mocks/AutoIncrementingImpl.sol index 86c498f6954..ea15e8e394c 100644 --- a/contracts/mocks/AutoIncrementingImpl.sol +++ b/contracts/mocks/AutoIncrementingImpl.sol @@ -9,13 +9,13 @@ contract AutoIncrementingImpl { uint256 public theId; // use whatever key you want to track your counters - mapping(string => AutoIncrementing.Counter) private counters; + mapping(string => AutoIncrementing.Counter) private _counters; - function doThing(string _key) + function doThing(string key) public returns (uint256) { - theId = counters[_key].nextId(); + theId = _counters[key].nextId(); return theId; } } diff --git a/contracts/mocks/CappedCrowdsaleImpl.sol b/contracts/mocks/CappedCrowdsaleImpl.sol index a05fbd7d25f..e92cec204f9 100644 --- a/contracts/mocks/CappedCrowdsaleImpl.sol +++ b/contracts/mocks/CappedCrowdsaleImpl.sol @@ -7,14 +7,14 @@ import "../crowdsale/validation/CappedCrowdsale.sol"; contract CappedCrowdsaleImpl is CappedCrowdsale { constructor ( - uint256 _rate, - address _wallet, - IERC20 _token, - uint256 _cap + uint256 rate, + address wallet, + IERC20 token, + uint256 cap ) public - Crowdsale(_rate, _wallet, _token) - CappedCrowdsale(_cap) + Crowdsale(rate, wallet, token) + CappedCrowdsale(cap) { } diff --git a/contracts/mocks/ConditionalEscrowMock.sol b/contracts/mocks/ConditionalEscrowMock.sol index dfcab519503..d3723ad7d4a 100644 --- a/contracts/mocks/ConditionalEscrowMock.sol +++ b/contracts/mocks/ConditionalEscrowMock.sol @@ -6,13 +6,13 @@ import "../payment/ConditionalEscrow.sol"; // mock class using ConditionalEscrow contract ConditionalEscrowMock is ConditionalEscrow { - mapping(address => bool) public allowed; + mapping(address => bool) private _allowed; - function setAllowed(address _payee, bool _allowed) public { - allowed[_payee] = _allowed; + function setAllowed(address payee, bool allowed) public { + _allowed[payee] = allowed; } - function withdrawalAllowed(address _payee) public view returns (bool) { - return allowed[_payee]; + function withdrawalAllowed(address payee) public view returns (bool) { + return _allowed[payee]; } } diff --git a/contracts/mocks/DetailedERC20Mock.sol b/contracts/mocks/DetailedERC20Mock.sol index e9aab88e95d..082c451be3c 100644 --- a/contracts/mocks/DetailedERC20Mock.sol +++ b/contracts/mocks/DetailedERC20Mock.sol @@ -6,11 +6,11 @@ import "../token/ERC20/ERC20Detailed.sol"; contract ERC20DetailedMock is ERC20, ERC20Detailed { constructor( - string _name, - string _symbol, - uint8 _decimals + string name, + string symbol, + uint8 decimals ) - ERC20Detailed(_name, _symbol, _decimals) + ERC20Detailed(name, symbol, decimals) public {} } diff --git a/contracts/mocks/ECDSAMock.sol b/contracts/mocks/ECDSAMock.sol index 42570569e0e..fa3f223488f 100644 --- a/contracts/mocks/ECDSAMock.sol +++ b/contracts/mocks/ECDSAMock.sol @@ -7,19 +7,19 @@ import "../cryptography/ECDSA.sol"; contract ECDSAMock { using ECDSA for bytes32; - function recover(bytes32 _hash, bytes _signature) + function recover(bytes32 hash, bytes signature) public pure returns (address) { - return _hash.recover(_signature); + return hash.recover(signature); } - function toEthSignedMessageHash(bytes32 _hash) + function toEthSignedMessageHash(bytes32 hash) public pure returns (bytes32) { - return _hash.toEthSignedMessageHash(); + return hash.toEthSignedMessageHash(); } } diff --git a/contracts/mocks/ERC165/ERC165InterfacesSupported.sol b/contracts/mocks/ERC165/ERC165InterfacesSupported.sol index 9f472b64825..6573d972cdc 100644 --- a/contracts/mocks/ERC165/ERC165InterfacesSupported.sol +++ b/contracts/mocks/ERC165/ERC165InterfacesSupported.sol @@ -37,33 +37,33 @@ contract SupportsInterfaceWithLookupMock is IERC165 { /** * @dev implement supportsInterface(bytes4) using a lookup table */ - function supportsInterface(bytes4 _interfaceId) + function supportsInterface(bytes4 interfaceId) external view returns (bool) { - return supportedInterfaces[_interfaceId]; + return supportedInterfaces[interfaceId]; } /** * @dev private method for registering an interface */ - function _registerInterface(bytes4 _interfaceId) + function _registerInterface(bytes4 interfaceId) internal { - require(_interfaceId != 0xffffffff); - supportedInterfaces[_interfaceId] = true; + require(interfaceId != 0xffffffff); + supportedInterfaces[interfaceId] = true; } } contract ERC165InterfacesSupported is SupportsInterfaceWithLookupMock { - constructor (bytes4[] _interfaceIds) + constructor (bytes4[] interfaceIds) public { - for (uint256 i = 0; i < _interfaceIds.length; i++) { - _registerInterface(_interfaceIds[i]); + for (uint256 i = 0; i < interfaceIds.length; i++) { + _registerInterface(interfaceIds[i]); } } } diff --git a/contracts/mocks/ERC165CheckerMock.sol b/contracts/mocks/ERC165CheckerMock.sol index 13ab1ccd60a..f4d6219f9f3 100644 --- a/contracts/mocks/ERC165CheckerMock.sol +++ b/contracts/mocks/ERC165CheckerMock.sol @@ -6,27 +6,27 @@ import "../introspection/ERC165Checker.sol"; contract ERC165CheckerMock { using ERC165Checker for address; - function supportsERC165(address _address) + function supportsERC165(address account) public view returns (bool) { - return _address.supportsERC165(); + return account.supportsERC165(); } - function supportsInterface(address _address, bytes4 _interfaceId) + function supportsInterface(address account, bytes4 interfaceId) public view returns (bool) { - return _address.supportsInterface(_interfaceId); + return account.supportsInterface(interfaceId); } - function supportsInterfaces(address _address, bytes4[] _interfaceIds) + function supportsInterfaces(address account, bytes4[] interfaceIds) public view returns (bool) { - return _address.supportsInterfaces(_interfaceIds); + return account.supportsInterfaces(interfaceIds); } } diff --git a/contracts/mocks/ERC20BurnableMock.sol b/contracts/mocks/ERC20BurnableMock.sol index 79819b748c7..bbc7c4d1a64 100644 --- a/contracts/mocks/ERC20BurnableMock.sol +++ b/contracts/mocks/ERC20BurnableMock.sol @@ -5,8 +5,8 @@ import "../token/ERC20/ERC20Burnable.sol"; contract ERC20BurnableMock is ERC20Burnable { - constructor(address _initialAccount, uint256 _initialBalance) public { - _mint(_initialAccount, _initialBalance); + constructor(address initialAccount, uint256 initialBalance) public { + _mint(initialAccount, initialBalance); } } diff --git a/contracts/mocks/ERC20Mock.sol b/contracts/mocks/ERC20Mock.sol index 600169743b3..046730750de 100644 --- a/contracts/mocks/ERC20Mock.sol +++ b/contracts/mocks/ERC20Mock.sol @@ -6,20 +6,20 @@ import "../token/ERC20/ERC20.sol"; // mock class using ERC20 contract ERC20Mock is ERC20 { - constructor(address _initialAccount, uint256 _initialBalance) public { - _mint(_initialAccount, _initialBalance); + constructor(address initialAccount, uint256 initialBalance) public { + _mint(initialAccount, initialBalance); } - function mint(address _account, uint256 _amount) public { - _mint(_account, _amount); + function mint(address account, uint256 amount) public { + _mint(account, amount); } - function burn(address _account, uint256 _amount) public { - _burn(_account, _amount); + function burn(address account, uint256 amount) public { + _burn(account, amount); } - function burnFrom(address _account, uint256 _amount) public { - _burnFrom(_account, _amount); + function burnFrom(address account, uint256 amount) public { + _burnFrom(account, amount); } } diff --git a/contracts/mocks/ERC20PausableMock.sol b/contracts/mocks/ERC20PausableMock.sol index a60f8a12da6..1dde49403cd 100644 --- a/contracts/mocks/ERC20PausableMock.sol +++ b/contracts/mocks/ERC20PausableMock.sol @@ -6,8 +6,8 @@ import "../token/ERC20/ERC20Pausable.sol"; // mock class using ERC20Pausable contract ERC20PausableMock is ERC20Pausable { - constructor(address _initialAccount, uint _initialBalance) public { - _mint(_initialAccount, _initialBalance); + constructor(address initialAccount, uint initialBalance) public { + _mint(initialAccount, initialBalance); } } diff --git a/contracts/mocks/ERC20WithMetadataMock.sol b/contracts/mocks/ERC20WithMetadataMock.sol index e42e46f9697..6272c7745d7 100644 --- a/contracts/mocks/ERC20WithMetadataMock.sol +++ b/contracts/mocks/ERC20WithMetadataMock.sol @@ -5,8 +5,8 @@ import "../drafts/ERC1046/TokenMetadata.sol"; contract ERC20WithMetadataMock is ERC20, ERC20WithMetadata { - constructor(string _tokenURI) public - ERC20WithMetadata(_tokenURI) + constructor(string tokenURI) public + ERC20WithMetadata(tokenURI) { } } diff --git a/contracts/mocks/ERC721BasicMock.sol b/contracts/mocks/ERC721BasicMock.sol index 87add3adb7e..523dacc245d 100644 --- a/contracts/mocks/ERC721BasicMock.sol +++ b/contracts/mocks/ERC721BasicMock.sol @@ -8,11 +8,11 @@ import "../token/ERC721/ERC721Basic.sol"; * This mock just provides a public mint and burn functions for testing purposes */ contract ERC721BasicMock is ERC721Basic { - function mint(address _to, uint256 _tokenId) public { - super._mint(_to, _tokenId); + function mint(address to, uint256 tokenId) public { + super._mint(to, tokenId); } - function burn(uint256 _tokenId) public { - super._burn(ownerOf(_tokenId), _tokenId); + function burn(uint256 tokenId) public { + super._burn(ownerOf(tokenId), tokenId); } } diff --git a/contracts/mocks/ERC721Mock.sol b/contracts/mocks/ERC721Mock.sol index b6c138fd2cd..bad0f7e6e66 100644 --- a/contracts/mocks/ERC721Mock.sol +++ b/contracts/mocks/ERC721Mock.sol @@ -13,23 +13,23 @@ contract ERC721Mock is ERC721 { ERC721(name, symbol) { } - function mint(address _to, uint256 _tokenId) public { - _mint(_to, _tokenId); + function mint(address to, uint256 tokenId) public { + _mint(to, tokenId); } - function burn(uint256 _tokenId) public { - _burn(ownerOf(_tokenId), _tokenId); + function burn(uint256 tokenId) public { + _burn(ownerOf(tokenId), tokenId); } - function exists(uint256 _tokenId) public view returns (bool) { - return _exists(_tokenId); + function exists(uint256 tokenId) public view returns (bool) { + return _exists(tokenId); } - function setTokenURI(uint256 _tokenId, string _uri) public { - _setTokenURI(_tokenId, _uri); + function setTokenURI(uint256 tokenId, string uri) public { + _setTokenURI(tokenId, uri); } - function removeTokenFrom(address _from, uint256 _tokenId) public { - _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 index ef2722f4ace..b6923e3667c 100644 --- a/contracts/mocks/ERC721PausableMock.sol +++ b/contracts/mocks/ERC721PausableMock.sol @@ -8,15 +8,15 @@ import "../token/ERC721/ERC721Pausable.sol"; * 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 mint(address to, uint256 tokenId) public { + super._mint(to, tokenId); } - function burn(uint256 _tokenId) public { - super._burn(ownerOf(_tokenId), _tokenId); + function burn(uint256 tokenId) public { + super._burn(ownerOf(tokenId), tokenId); } - function exists(uint256 _tokenId) public view returns (bool) { - return super._exists(_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 3d747f32b11..4e21ff6ac95 100644 --- a/contracts/mocks/ERC721ReceiverMock.sol +++ b/contracts/mocks/ERC721ReceiverMock.sol @@ -4,8 +4,8 @@ import "../token/ERC721/IERC721Receiver.sol"; contract ERC721ReceiverMock is IERC721Receiver { - bytes4 internal retval_; - bool internal reverts_; + bytes4 private _retval; + bool private _reverts; event Received( address operator, @@ -15,28 +15,28 @@ contract ERC721ReceiverMock is IERC721Receiver { uint256 gas ); - constructor(bytes4 _retval, bool _reverts) public { - retval_ = _retval; - reverts_ = _reverts; + constructor(bytes4 retval, bool reverts) public { + _retval = retval; + _reverts = reverts; } function onERC721Received( - address _operator, - address _from, - uint256 _tokenId, - bytes _data + address operator, + address from, + uint256 tokenId, + bytes data ) public returns(bytes4) { - require(!reverts_); + require(!_reverts); emit Received( - _operator, - _from, - _tokenId, - _data, + operator, + from, + tokenId, + 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 aa419b82211..1f4894e537e 100644 --- a/contracts/mocks/FinalizableCrowdsaleImpl.sol +++ b/contracts/mocks/FinalizableCrowdsaleImpl.sol @@ -7,15 +7,15 @@ import "../crowdsale/distribution/FinalizableCrowdsale.sol"; contract FinalizableCrowdsaleImpl is FinalizableCrowdsale { constructor ( - uint256 _openingTime, - uint256 _closingTime, - uint256 _rate, - address _wallet, - ERC20Mintable _token + uint256 openingTime, + uint256 closingTime, + uint256 rate, + address wallet, + ERC20Mintable token ) public - Crowdsale(_rate, _wallet, _token) - TimedCrowdsale(_openingTime, _closingTime) + Crowdsale(rate, wallet, token) + TimedCrowdsale(openingTime, closingTime) { } diff --git a/contracts/mocks/ForceEther.sol b/contracts/mocks/ForceEther.sol index 2823ae94877..41cde563c54 100644 --- a/contracts/mocks/ForceEther.sol +++ b/contracts/mocks/ForceEther.sol @@ -10,7 +10,7 @@ contract ForceEther { constructor() public payable { } - function destroyAndSend(address _recipient) public { - selfdestruct(_recipient); + function destroyAndSend(address recipient) public { + selfdestruct(recipient); } } diff --git a/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol b/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol index 95e4e367c54..29d71ec8a3d 100644 --- a/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol +++ b/contracts/mocks/IncreasingPriceCrowdsaleImpl.sol @@ -7,17 +7,17 @@ import "../math/SafeMath.sol"; contract IncreasingPriceCrowdsaleImpl is IncreasingPriceCrowdsale { constructor ( - uint256 _openingTime, - uint256 _closingTime, - address _wallet, - IERC20 _token, - uint256 _initialRate, - uint256 _finalRate + uint256 openingTime, + uint256 closingTime, + address wallet, + IERC20 token, + uint256 initialRate, + uint256 finalRate ) public - Crowdsale(_initialRate, _wallet, _token) - TimedCrowdsale(_openingTime, _closingTime) - IncreasingPriceCrowdsale(_initialRate, _finalRate) + Crowdsale(initialRate, wallet, token) + TimedCrowdsale(openingTime, closingTime) + IncreasingPriceCrowdsale(initialRate, finalRate) { } diff --git a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol index b4b470f7ca6..f0ac133e8e8 100644 --- a/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol +++ b/contracts/mocks/IndividuallyCappedCrowdsaleImpl.sol @@ -7,12 +7,12 @@ import "../crowdsale/validation/IndividuallyCappedCrowdsale.sol"; contract IndividuallyCappedCrowdsaleImpl is IndividuallyCappedCrowdsale { constructor ( - uint256 _rate, - address _wallet, - IERC20 _token + uint256 rate, + address wallet, + IERC20 token ) public - Crowdsale(_rate, _wallet, _token) + Crowdsale(rate, wallet, token) { } diff --git a/contracts/mocks/MathMock.sol b/contracts/mocks/MathMock.sol index 53f61a5f686..60341bc6514 100644 --- a/contracts/mocks/MathMock.sol +++ b/contracts/mocks/MathMock.sol @@ -5,15 +5,15 @@ import "../../contracts/math/Math.sol"; contract MathMock { - function max(uint256 _a, uint256 _b) public pure returns (uint256) { - return Math.max(_a, _b); + function max(uint256 a, uint256 b) public pure returns (uint256) { + return Math.max(a, b); } - function min(uint256 _a, uint256 _b) public pure returns (uint256) { - return Math.min(_a, _b); + function min(uint256 a, uint256 b) public pure returns (uint256) { + return Math.min(a, b); } - function average(uint256 _a, uint256 _b) public pure returns (uint256) { - return Math.average(_a, _b); + function average(uint256 a, uint256 b) public pure returns (uint256) { + return Math.average(a, b); } } diff --git a/contracts/mocks/MerkleProofWrapper.sol b/contracts/mocks/MerkleProofWrapper.sol index fe72d75f87f..5d74fedc27e 100644 --- a/contracts/mocks/MerkleProofWrapper.sol +++ b/contracts/mocks/MerkleProofWrapper.sol @@ -6,14 +6,14 @@ import { MerkleProof } from "../cryptography/MerkleProof.sol"; contract MerkleProofWrapper { function verifyProof( - bytes32[] _proof, - bytes32 _root, - bytes32 _leaf + bytes32[] proof, + bytes32 root, + bytes32 leaf ) public pure returns (bool) { - return MerkleProof.verifyProof(_proof, _root, _leaf); + return MerkleProof.verifyProof(proof, root, leaf); } } diff --git a/contracts/mocks/MintedCrowdsaleImpl.sol b/contracts/mocks/MintedCrowdsaleImpl.sol index 77e3430b52e..afdd3b8c764 100644 --- a/contracts/mocks/MintedCrowdsaleImpl.sol +++ b/contracts/mocks/MintedCrowdsaleImpl.sol @@ -7,12 +7,12 @@ import "../crowdsale/emission/MintedCrowdsale.sol"; contract MintedCrowdsaleImpl is MintedCrowdsale { constructor ( - uint256 _rate, - address _wallet, - ERC20Mintable _token + uint256 rate, + address wallet, + ERC20Mintable token ) public - Crowdsale(_rate, _wallet, _token) + Crowdsale(rate, wallet, token) { } diff --git a/contracts/mocks/PostDeliveryCrowdsaleImpl.sol b/contracts/mocks/PostDeliveryCrowdsaleImpl.sol index b4dea2700e5..c6dca89a520 100644 --- a/contracts/mocks/PostDeliveryCrowdsaleImpl.sol +++ b/contracts/mocks/PostDeliveryCrowdsaleImpl.sol @@ -7,15 +7,15 @@ import "../crowdsale/distribution/PostDeliveryCrowdsale.sol"; contract PostDeliveryCrowdsaleImpl is PostDeliveryCrowdsale { constructor ( - uint256 _openingTime, - uint256 _closingTime, - uint256 _rate, - address _wallet, - IERC20 _token + uint256 openingTime, + uint256 closingTime, + uint256 rate, + address wallet, + IERC20 token ) public - TimedCrowdsale(_openingTime, _closingTime) - Crowdsale(_rate, _wallet, _token) + TimedCrowdsale(openingTime, closingTime) + Crowdsale(rate, wallet, token) { } diff --git a/contracts/mocks/PullPaymentMock.sol b/contracts/mocks/PullPaymentMock.sol index 639095dcbb8..d6da3ed70f9 100644 --- a/contracts/mocks/PullPaymentMock.sol +++ b/contracts/mocks/PullPaymentMock.sol @@ -10,8 +10,8 @@ contract PullPaymentMock is PullPayment { constructor() public payable { } // test helper function to call asyncTransfer - function callTransfer(address _dest, uint256 _amount) public { - _asyncTransfer(_dest, _amount); + function callTransfer(address dest, uint256 amount) public { + _asyncTransfer(dest, amount); } } diff --git a/contracts/mocks/RBACCappedTokenMock.sol b/contracts/mocks/RBACCappedTokenMock.sol index 8d182c6e5be..bf6896dd7c5 100644 --- a/contracts/mocks/RBACCappedTokenMock.sol +++ b/contracts/mocks/RBACCappedTokenMock.sol @@ -5,8 +5,8 @@ import "../token/ERC20/ERC20Capped.sol"; contract RBACCappedTokenMock is ERC20Capped, RBACMintableToken { - constructor(uint256 _cap) - ERC20Capped(_cap) + constructor(uint256 cap) + ERC20Capped(cap) public {} } diff --git a/contracts/mocks/RBACMock.sol b/contracts/mocks/RBACMock.sol index f4acc927915..9e9e595b3a1 100644 --- a/contracts/mocks/RBACMock.sol +++ b/contracts/mocks/RBACMock.sol @@ -5,24 +5,24 @@ import "../examples/RBACWithAdmin.sol"; contract RBACMock is RBACWithAdmin { - string internal constant ROLE_ADVISOR = "advisor"; + string private constant _ROLE_ADVISOR = "advisor"; modifier onlyAdminOrAdvisor() { require( isAdmin(msg.sender) || - hasRole(msg.sender, ROLE_ADVISOR) + hasRole(msg.sender, _ROLE_ADVISOR) ); _; } - constructor(address[] _advisors) + 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); + for (uint256 i = 0; i < advisors.length; i++) { + _addRole(advisors[i], _ROLE_ADVISOR); } } @@ -35,7 +35,7 @@ contract RBACMock is RBACWithAdmin { function onlyAdvisorsCanDoThis() external - onlyRole(ROLE_ADVISOR) + onlyRole(_ROLE_ADVISOR) view { } @@ -55,15 +55,15 @@ contract RBACMock is RBACWithAdmin { } // admins can remove advisor's role - function removeAdvisor(address _account) + 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); + checkRole(account, _ROLE_ADVISOR); // remove the advisor's role - _removeRole(_account, ROLE_ADVISOR); + _removeRole(account, _ROLE_ADVISOR); } } diff --git a/contracts/mocks/ReentrancyAttack.sol b/contracts/mocks/ReentrancyAttack.sol index 96700bd16c1..05ef748e7a1 100644 --- a/contracts/mocks/ReentrancyAttack.sol +++ b/contracts/mocks/ReentrancyAttack.sol @@ -3,9 +3,9 @@ pragma solidity ^0.4.24; contract ReentrancyAttack { - function callSender(bytes4 _data) public { + function callSender(bytes4 data) public { // solium-disable-next-line security/no-low-level-calls - require(msg.sender.call(abi.encodeWithSelector(_data))); + require(msg.sender.call(abi.encodeWithSelector(data))); } } diff --git a/contracts/mocks/ReentrancyMock.sol b/contracts/mocks/ReentrancyMock.sol index 93afdd6dc28..130a8fd90f3 100644 --- a/contracts/mocks/ReentrancyMock.sol +++ b/contracts/mocks/ReentrancyMock.sol @@ -16,26 +16,26 @@ contract ReentrancyMock is ReentrancyGuard { count(); } - function countLocalRecursive(uint256 _n) public nonReentrant { - if (_n > 0) { + function countLocalRecursive(uint256 n) public nonReentrant { + if (n > 0) { count(); - countLocalRecursive(_n - 1); + countLocalRecursive(n - 1); } } - function countThisRecursive(uint256 _n) public nonReentrant { - if (_n > 0) { + function countThisRecursive(uint256 n) public nonReentrant { + if (n > 0) { count(); // solium-disable-next-line security/no-low-level-calls - bool result = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", _n - 1)); + bool result = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1)); require(result == true); } } - function countAndCall(ReentrancyAttack _attacker) public nonReentrant { + function countAndCall(ReentrancyAttack attacker) public nonReentrant { count(); bytes4 func = bytes4(keccak256("callback()")); - _attacker.callSender(func); + attacker.callSender(func); } function count() private { diff --git a/contracts/mocks/RefundableCrowdsaleImpl.sol b/contracts/mocks/RefundableCrowdsaleImpl.sol index b581031bfd5..631f29d0428 100644 --- a/contracts/mocks/RefundableCrowdsaleImpl.sol +++ b/contracts/mocks/RefundableCrowdsaleImpl.sol @@ -7,17 +7,17 @@ import "../crowdsale/distribution/RefundableCrowdsale.sol"; contract RefundableCrowdsaleImpl is RefundableCrowdsale { constructor ( - uint256 _openingTime, - uint256 _closingTime, - uint256 _rate, - address _wallet, - ERC20Mintable _token, - uint256 _goal + uint256 openingTime, + uint256 closingTime, + uint256 rate, + address wallet, + ERC20Mintable token, + uint256 goal ) public - Crowdsale(_rate, _wallet, _token) - TimedCrowdsale(_openingTime, _closingTime) - RefundableCrowdsale(_goal) + Crowdsale(rate, wallet, token) + TimedCrowdsale(openingTime, closingTime) + RefundableCrowdsale(goal) { } diff --git a/contracts/mocks/SafeERC20Helper.sol b/contracts/mocks/SafeERC20Helper.sol index dfc2fb2c0b5..0009fd01c4e 100644 --- a/contracts/mocks/SafeERC20Helper.sol +++ b/contracts/mocks/SafeERC20Helper.sol @@ -61,35 +61,35 @@ contract ERC20SucceedingMock is IERC20 { contract SafeERC20Helper { using SafeERC20 for IERC20; - IERC20 internal failing_; - IERC20 internal succeeding_; + IERC20 private _failing; + IERC20 private _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/SafeMathMock.sol b/contracts/mocks/SafeMathMock.sol index d69d6e5cc3d..c5464ec16ec 100644 --- a/contracts/mocks/SafeMathMock.sol +++ b/contracts/mocks/SafeMathMock.sol @@ -6,20 +6,20 @@ import "../math/SafeMath.sol"; contract SafeMathMock { - function mul(uint256 _a, uint256 _b) public pure returns (uint256) { - return SafeMath.mul(_a, _b); + function mul(uint256 a, uint256 b) public pure returns (uint256) { + return SafeMath.mul(a, b); } - function div(uint256 _a, uint256 _b) public pure returns (uint256) { - return SafeMath.div(_a, _b); + function div(uint256 a, uint256 b) public pure returns (uint256) { + return SafeMath.div(a, b); } - function sub(uint256 _a, uint256 _b) public pure returns (uint256) { - return SafeMath.sub(_a, _b); + function sub(uint256 a, uint256 b) public pure returns (uint256) { + return SafeMath.sub(a, b); } - function add(uint256 _a, uint256 _b) public pure returns (uint256) { - return SafeMath.add(_a, _b); + function add(uint256 a, uint256 b) public pure returns (uint256) { + return SafeMath.add(a, b); } function mod(uint256 a, uint256 b) public pure returns (uint256) { diff --git a/contracts/mocks/SignatureBouncerMock.sol b/contracts/mocks/SignatureBouncerMock.sol index d9fea81f402..f9bf6d4b637 100644 --- a/contracts/mocks/SignatureBouncerMock.sol +++ b/contracts/mocks/SignatureBouncerMock.sol @@ -4,54 +4,54 @@ import "../drafts/SignatureBouncer.sol"; contract SignatureBouncerMock is SignatureBouncer { - function checkValidSignature(address _address, bytes _signature) + function checkValidSignature(address account, bytes signature) public view returns (bool) { - return _isValidSignature(_address, _signature); + return _isValidSignature(account, signature); } - function onlyWithValidSignature(bytes _signature) + function onlyWithValidSignature(bytes signature) public - onlyValidSignature(_signature) + onlyValidSignature(signature) view { } - function checkValidSignatureAndMethod(address _address, bytes _signature) + function checkValidSignatureAndMethod(address account, bytes signature) public view returns (bool) { - return _isValidSignatureAndMethod(_address, _signature); + return _isValidSignatureAndMethod(account, signature); } - function onlyWithValidSignatureAndMethod(bytes _signature) + function onlyWithValidSignatureAndMethod(bytes signature) public - onlyValidSignatureAndMethod(_signature) + onlyValidSignatureAndMethod(signature) view { } function checkValidSignatureAndData( - address _address, + address account, bytes, uint, - bytes _signature + bytes signature ) public view returns (bool) { - return _isValidSignatureAndData(_address, _signature); + return _isValidSignatureAndData(account, signature); } - function onlyWithValidSignatureAndData(uint, bytes _signature) + function onlyWithValidSignatureAndData(uint, bytes signature) public - onlyValidSignatureAndData(_signature) + onlyValidSignatureAndData(signature) view { diff --git a/contracts/mocks/SupportsInterfaceWithLookupMock.sol b/contracts/mocks/SupportsInterfaceWithLookupMock.sol index e1c95fc07f0..77708939c8d 100644 --- a/contracts/mocks/SupportsInterfaceWithLookupMock.sol +++ b/contracts/mocks/SupportsInterfaceWithLookupMock.sol @@ -4,9 +4,9 @@ import "../introspection/SupportsInterfaceWithLookup.sol"; contract SupportsInterfaceWithLookupMock is SupportsInterfaceWithLookup { - function registerInterface(bytes4 _interfaceId) + function registerInterface(bytes4 interfaceId) public { - _registerInterface(_interfaceId); + _registerInterface(interfaceId); } } diff --git a/contracts/mocks/TimedCrowdsaleImpl.sol b/contracts/mocks/TimedCrowdsaleImpl.sol index b99178aee7c..296484ed1f4 100644 --- a/contracts/mocks/TimedCrowdsaleImpl.sol +++ b/contracts/mocks/TimedCrowdsaleImpl.sol @@ -7,15 +7,15 @@ import "../crowdsale/validation/TimedCrowdsale.sol"; contract TimedCrowdsaleImpl is TimedCrowdsale { constructor ( - uint256 _openingTime, - uint256 _closingTime, - uint256 _rate, - address _wallet, - IERC20 _token + uint256 openingTime, + uint256 closingTime, + uint256 rate, + address wallet, + IERC20 token ) public - Crowdsale(_rate, _wallet, _token) - TimedCrowdsale(_openingTime, _closingTime) + Crowdsale(rate, wallet, token) + TimedCrowdsale(openingTime, closingTime) { } diff --git a/contracts/mocks/WhitelistedCrowdsaleImpl.sol b/contracts/mocks/WhitelistedCrowdsaleImpl.sol index 25cd5118aea..4333fd4770d 100644 --- a/contracts/mocks/WhitelistedCrowdsaleImpl.sol +++ b/contracts/mocks/WhitelistedCrowdsaleImpl.sol @@ -8,11 +8,11 @@ import "../crowdsale/Crowdsale.sol"; contract WhitelistedCrowdsaleImpl is Crowdsale, WhitelistedCrowdsale { constructor ( - uint256 _rate, - address _wallet, - IERC20 _token + uint256 rate, + address wallet, + IERC20 token ) - Crowdsale(_rate, _wallet, _token) + Crowdsale(rate, wallet, token) public { } diff --git a/contracts/ownership/CanReclaimToken.sol b/contracts/ownership/CanReclaimToken.sol index e4f7e88a638..cdbeb547bf9 100644 --- a/contracts/ownership/CanReclaimToken.sol +++ b/contracts/ownership/CanReclaimToken.sol @@ -16,11 +16,11 @@ contract CanReclaimToken is Ownable { /** * @dev Reclaim all ERC20 compatible tokens - * @param _token ERC20 The address of the token contract + * @param token ERC20 The address of the token contract */ - function reclaimToken(IERC20 _token) external onlyOwner { - uint256 balance = _token.balanceOf(this); - _token.safeTransfer(owner(), balance); + function reclaimToken(IERC20 token) external onlyOwner { + uint256 balance = token.balanceOf(this); + token.safeTransfer(owner(), balance); } } diff --git a/contracts/ownership/Ownable.sol b/contracts/ownership/Ownable.sol index c6ea3e16fa0..e7d2810de8b 100644 --- a/contracts/ownership/Ownable.sol +++ b/contracts/ownership/Ownable.sol @@ -7,7 +7,7 @@ pragma solidity ^0.4.24; * functions, this simplifies the implementation of "user permissions". */ contract Ownable { - address private owner_; + address private _owner; event OwnershipRenounced(address indexed previousOwner); @@ -22,14 +22,14 @@ contract Ownable { * account. */ constructor() public { - owner_ = msg.sender; + _owner = msg.sender; } /** * @return the address of the owner. */ function owner() public view returns(address) { - return owner_; + return _owner; } /** @@ -44,7 +44,7 @@ contract Ownable { * @return true if `msg.sender` is the owner of the contract. */ function isOwner() public view returns(bool) { - return msg.sender == owner_; + return msg.sender == _owner; } /** @@ -54,25 +54,25 @@ contract Ownable { * modifier anymore. */ function renounceOwnership() public onlyOwner { - emit OwnershipRenounced(owner_); - owner_ = address(0); + emit OwnershipRenounced(_owner); + _owner = address(0); } /** * @dev Allows the current owner to transfer control of the contract to a newOwner. - * @param _newOwner The address to transfer ownership to. + * @param newOwner The address to transfer ownership to. */ - function transferOwnership(address _newOwner) public onlyOwner { - _transferOwnership(_newOwner); + function transferOwnership(address newOwner) public onlyOwner { + _transferOwnership(newOwner); } /** * @dev Transfers control of the contract to a newOwner. - * @param _newOwner The address to transfer ownership to. + * @param newOwner The address to transfer ownership to. */ - function _transferOwnership(address _newOwner) internal { - require(_newOwner != address(0)); - emit OwnershipTransferred(owner_, _newOwner); - owner_ = _newOwner; + function _transferOwnership(address newOwner) internal { + require(newOwner != address(0)); + emit OwnershipTransferred(_owner, newOwner); + _owner = newOwner; } } diff --git a/contracts/ownership/Superuser.sol b/contracts/ownership/Superuser.sol index b1f1fa4b52d..8996395e5e9 100644 --- a/contracts/ownership/Superuser.sol +++ b/contracts/ownership/Superuser.sol @@ -12,17 +12,17 @@ import "../access/rbac/RBAC.sol"; * A superuser can transfer his role to a new address. */ contract Superuser is Ownable, RBAC { - string private constant ROLE_SUPERUSER = "superuser"; + string private constant _ROLE_SUPERUSER = "superuser"; constructor () public { - _addRole(msg.sender, ROLE_SUPERUSER); + _addRole(msg.sender, _ROLE_SUPERUSER); } /** * @dev Throws if called by any account that's not a superuser. */ modifier onlySuperuser() { - checkRole(msg.sender, ROLE_SUPERUSER); + checkRole(msg.sender, _ROLE_SUPERUSER); _; } @@ -34,29 +34,29 @@ contract Superuser is Ownable, RBAC { /** * @dev getter to determine if an account has superuser role */ - function isSuperuser(address _account) + function isSuperuser(address account) public view returns (bool) { - return hasRole(_account, ROLE_SUPERUSER); + 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. + * @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); + 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. + * @param newOwner The address to transfer ownership to. */ - function transferOwnership(address _newOwner) public onlyOwnerOrSuperuser { - _transferOwnership(_newOwner); + function transferOwnership(address newOwner) public onlyOwnerOrSuperuser { + _transferOwnership(newOwner); } } diff --git a/contracts/payment/ConditionalEscrow.sol b/contracts/payment/ConditionalEscrow.sol index 9278a69a39e..9fb4a495694 100644 --- a/contracts/payment/ConditionalEscrow.sol +++ b/contracts/payment/ConditionalEscrow.sol @@ -11,12 +11,12 @@ contract ConditionalEscrow is Escrow { /** * @dev Returns whether an address is allowed to withdraw their funds. To be * implemented by derived contracts. - * @param _payee The destination address of the funds. + * @param payee The destination address of the funds. */ - function withdrawalAllowed(address _payee) public view returns (bool); + function withdrawalAllowed(address payee) public view returns (bool); - function withdraw(address _payee) public { - require(withdrawalAllowed(_payee)); - super.withdraw(_payee); + function withdraw(address payee) public { + require(withdrawalAllowed(payee)); + super.withdraw(payee); } } diff --git a/contracts/payment/Escrow.sol b/contracts/payment/Escrow.sol index dc3fbfef57b..36577fa9865 100644 --- a/contracts/payment/Escrow.sol +++ b/contracts/payment/Escrow.sol @@ -17,35 +17,35 @@ contract Escrow is Ownable { 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]; + function depositsOf(address payee) public view returns (uint256) { + return _deposits[payee]; } /** * @dev Stores the sent amount as credit to be withdrawn. - * @param _payee The destination address of the funds. + * @param payee The destination address of the funds. */ - function deposit(address _payee) public onlyOwner payable { + function deposit(address payee) public onlyOwner payable { uint256 amount = msg.value; - deposits_[_payee] = deposits_[_payee].add(amount); + _deposits[payee] = _deposits[payee].add(amount); - emit Deposited(_payee, amount); + emit Deposited(payee, amount); } /** * @dev Withdraw accumulated balance for a payee. - * @param _payee The address whose funds will be withdrawn and transferred to. + * @param payee The address whose funds will be withdrawn and transferred to. */ - function withdraw(address _payee) public onlyOwner { - uint256 payment = deposits_[_payee]; + function withdraw(address payee) public onlyOwner { + uint256 payment = _deposits[payee]; assert(address(this).balance >= payment); - deposits_[_payee] = 0; + _deposits[payee] = 0; - _payee.transfer(payment); + payee.transfer(payment); - emit Withdrawn(_payee, payment); + emit Withdrawn(payee, payment); } } diff --git a/contracts/payment/PullPayment.sol b/contracts/payment/PullPayment.sol index 4c6df8150e0..8399fa4e114 100644 --- a/contracts/payment/PullPayment.sol +++ b/contracts/payment/PullPayment.sol @@ -9,34 +9,34 @@ import "./Escrow.sol"; * contract and use _asyncTransfer instead of send or transfer. */ contract PullPayment { - Escrow private escrow; + Escrow private _escrow; constructor() public { - escrow = new Escrow(); + _escrow = new Escrow(); } /** * @dev Withdraw accumulated balance. - * @param _payee Whose balance will be withdrawn. + * @param payee Whose balance will be withdrawn. */ - function withdrawPayments(address _payee) public { - escrow.withdraw(_payee); + function withdrawPayments(address payee) public { + _escrow.withdraw(payee); } /** * @dev Returns the credit owed to an address. - * @param _dest The creditor's address. + * @param dest The creditor's address. */ - function payments(address _dest) public view returns (uint256) { - return escrow.depositsOf(_dest); + function payments(address dest) public view returns (uint256) { + return _escrow.depositsOf(dest); } /** * @dev Called by the payer to store the sent amount as credit to be pulled. - * @param _dest The destination address of the funds. - * @param _amount The amount to transfer. + * @param dest The destination address of the funds. + * @param amount The amount to transfer. */ - function _asyncTransfer(address _dest, uint256 _amount) internal { - escrow.deposit.value(_amount)(_dest); + function _asyncTransfer(address dest, uint256 amount) internal { + _escrow.deposit.value(amount)(dest); } } diff --git a/contracts/payment/RefundEscrow.sol b/contracts/payment/RefundEscrow.sol index 802dd471128..55f4da04b90 100644 --- a/contracts/payment/RefundEscrow.sol +++ b/contracts/payment/RefundEscrow.sol @@ -16,40 +16,40 @@ contract RefundEscrow is Ownable, ConditionalEscrow { event Closed(); event RefundsEnabled(); - State private state_; - address private beneficiary_; + State private _state; + address private _beneficiary; /** * @dev Constructor. - * @param _beneficiary The beneficiary of the deposits. + * @param beneficiary The beneficiary of the deposits. */ - constructor(address _beneficiary) public { - require(_beneficiary != address(0)); - beneficiary_ = _beneficiary; - state_ = State.Active; + constructor(address beneficiary) public { + require(beneficiary != address(0)); + _beneficiary = beneficiary; + _state = State.Active; } /** * @return the current state of the escrow. */ function state() public view returns(State) { - return state_; + return _state; } /** * @return the beneficiary of the escrow. */ function beneficiary() public view returns(address) { - return beneficiary_; + return _beneficiary; } /** * @dev Stores funds that may later be refunded. - * @param _refundee The address funds will be sent to if a refund occurs. + * @param refundee The address funds will be sent to if a refund occurs. */ - function deposit(address _refundee) public payable { - require(state_ == State.Active); - super.deposit(_refundee); + function deposit(address refundee) public payable { + require(_state == State.Active); + super.deposit(refundee); } /** @@ -57,8 +57,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * further deposits. */ function close() public onlyOwner { - require(state_ == State.Active); - state_ = State.Closed; + require(_state == State.Active); + _state = State.Closed; emit Closed(); } @@ -66,8 +66,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Allows for refunds to take place, rejecting further deposits. */ function enableRefunds() public onlyOwner { - require(state_ == State.Active); - state_ = State.Refunding; + require(_state == State.Active); + _state = State.Refunding; emit RefundsEnabled(); } @@ -75,14 +75,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow { * @dev Withdraws the beneficiary's funds. */ function beneficiaryWithdraw() public { - require(state_ == State.Closed); - beneficiary_.transfer(address(this).balance); + require(_state == State.Closed); + _beneficiary.transfer(address(this).balance); } /** * @dev Returns whether refundees can withdraw their deposits (be refunded). */ - function withdrawalAllowed(address _payee) public view returns (bool) { - return state_ == State.Refunding; + function withdrawalAllowed(address payee) public view returns (bool) { + return _state == State.Refunding; } } diff --git a/contracts/payment/SplitPayment.sol b/contracts/payment/SplitPayment.sol index 0d4a2e0956d..19df9b7064a 100644 --- a/contracts/payment/SplitPayment.sol +++ b/contracts/payment/SplitPayment.sol @@ -11,22 +11,22 @@ import "../math/SafeMath.sol"; contract SplitPayment { using SafeMath for uint256; - uint256 private totalShares_ = 0; - uint256 private totalReleased_ = 0; + uint256 private _totalShares = 0; + uint256 private _totalReleased = 0; - mapping(address => uint256) private shares_; - mapping(address => uint256) private released_; - address[] private payees_; + mapping(address => uint256) private _shares; + mapping(address => uint256) private _released; + address[] private _payees; /** * @dev Constructor */ - constructor(address[] _payees, uint256[] _shares) public payable { - require(_payees.length == _shares.length); - require(_payees.length > 0); + constructor(address[] payees, uint256[] shares) public payable { + require(payees.length == shares.length); + require(payees.length > 0); - for (uint256 i = 0; i < _payees.length; i++) { - _addPayee(_payees[i], _shares[i]); + for (uint256 i = 0; i < payees.length; i++) { + _addPayee(payees[i], shares[i]); } } @@ -39,72 +39,72 @@ contract SplitPayment { * @return the total shares of the contract. */ function totalShares() public view returns(uint256) { - return totalShares_; + return _totalShares; } /** * @return the total amount already released. */ function totalReleased() public view returns(uint256) { - return totalReleased_; + return _totalReleased; } /** * @return the shares of an account. */ - function shares(address _account) public view returns(uint256) { - return shares_[_account]; + function shares(address account) public view returns(uint256) { + return _shares[account]; } /** * @return the amount already released to an account. */ - function released(address _account) public view returns(uint256) { - return released_[_account]; + function released(address account) public view returns(uint256) { + return _released[account]; } /** * @return the address of a payee. */ function payee(uint256 index) public view returns(address) { - return payees_[index]; + return _payees[index]; } /** * @dev Release one of the payee's proportional payment. - * @param _payee Whose payments will be released. + * @param account Whose payments will be released. */ - function release(address _payee) public { - require(shares_[_payee] > 0); + function release(address account) public { + require(_shares[account] > 0); - uint256 totalReceived = address(this).balance.add(totalReleased_); + uint256 totalReceived = address(this).balance.add(_totalReleased); uint256 payment = totalReceived.mul( - shares_[_payee]).div( - totalShares_).sub( - released_[_payee] + _shares[account]).div( + _totalShares).sub( + _released[account] ); require(payment != 0); assert(address(this).balance >= payment); - released_[_payee] = released_[_payee].add(payment); - totalReleased_ = totalReleased_.add(payment); + _released[account] = _released[account].add(payment); + _totalReleased = _totalReleased.add(payment); - _payee.transfer(payment); + account.transfer(payment); } /** * @dev Add a new payee to the contract. - * @param _payee The address of the payee to add. - * @param _shares The number of shares owned by the payee. + * @param account The address of the payee to add. + * @param shares_ The number of shares owned by the payee. */ - function _addPayee(address _payee, uint256 _shares) internal { - require(_payee != address(0)); - require(_shares > 0); - require(shares_[_payee] == 0); - - payees_.push(_payee); - shares_[_payee] = _shares; - totalShares_ = totalShares_.add(_shares); + function _addPayee(address account, uint256 shares_) internal { + require(account != address(0)); + require(shares_ > 0); + require(_shares[account] == 0); + + _payees.push(account); + _shares[account] = shares_; + _totalShares = _totalShares.add(shares_); } } diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 0201cc0b849..2f541cc8d4c 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -14,57 +14,57 @@ import "../../math/SafeMath.sol"; contract ERC20 is IERC20 { using SafeMath for uint256; - mapping (address => uint256) private balances_; + mapping (address => uint256) private _balances; - mapping (address => mapping (address => uint256)) private allowed_; + mapping (address => mapping (address => uint256)) private _allowed; - uint256 private totalSupply_; + uint256 private _totalSupply; /** * @dev Total number of tokens in existence */ function totalSupply() public view returns (uint256) { - return totalSupply_; + return _totalSupply; } /** * @dev Gets the balance of the specified address. - * @param _owner The address to query the the balance of. + * @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]; + 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. + * @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 + address owner, + address spender ) public view returns (uint256) { - return allowed_[_owner][_spender]; + 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. + * @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)); + 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); + _balances[msg.sender] = _balances[msg.sender].sub(value); + _balances[to] = _balances[to].add(value); + emit Transfer(msg.sender, to, value); return true; } @@ -74,37 +74,37 @@ contract ERC20 is IERC20 { * 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. + * @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); + 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 + * @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 + 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); + 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; } @@ -114,19 +114,19 @@ contract ERC20 is IERC20 { * 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. + * @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 + 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]); + _allowed[msg.sender][spender] = ( + _allowed[msg.sender][spender].add(addedValue)); + emit Approval(msg.sender, spender, _allowed[msg.sender][spender]); return true; } @@ -136,23 +136,23 @@ contract ERC20 is IERC20 { * 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. + * @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 + address spender, + uint256 subtractedValue ) public returns (bool) { - uint256 oldValue = allowed_[msg.sender][_spender]; - if (_subtractedValue >= oldValue) { - allowed_[msg.sender][_spender] = 0; + uint256 oldValue = _allowed[msg.sender][spender]; + if (subtractedValue >= oldValue) { + _allowed[msg.sender][spender] = 0; } else { - allowed_[msg.sender][_spender] = oldValue.sub(_subtractedValue); + _allowed[msg.sender][spender] = oldValue.sub(subtractedValue); } - emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]); + emit Approval(msg.sender, spender, _allowed[msg.sender][spender]); return true; } @@ -160,45 +160,45 @@ contract ERC20 is IERC20 { * @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. + * @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 _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. + * @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 _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); + _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. + * 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]); + 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); + _allowed[account][msg.sender] = _allowed[account][msg.sender].sub( + amount); + _burn(account, amount); } } diff --git a/contracts/token/ERC20/ERC20Burnable.sol b/contracts/token/ERC20/ERC20Burnable.sol index 1fc83b77a00..ee2f98b8273 100644 --- a/contracts/token/ERC20/ERC20Burnable.sol +++ b/contracts/token/ERC20/ERC20Burnable.sol @@ -13,27 +13,27 @@ contract ERC20Burnable is ERC20 { /** * @dev Burns a specific amount of tokens. - * @param _value The amount of token to be burned. + * @param value The amount of token to be burned. */ - function burn(uint256 _value) public { - _burn(msg.sender, _value); + function burn(uint256 value) public { + _burn(msg.sender, value); } /** * @dev Burns a specific amount of tokens from the target address and decrements allowance - * @param _from address The address which you want to send tokens from - * @param _value uint256 The amount of token to be burned + * @param from address The address which you want to send tokens from + * @param value uint256 The amount of token to be burned */ - function burnFrom(address _from, uint256 _value) public { - _burnFrom(_from, _value); + function burnFrom(address from, uint256 value) public { + _burnFrom(from, value); } /** * @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 TokensBurned(_who, _value); + function _burn(address who, uint256 value) internal { + super._burn(who, value); + emit TokensBurned(who, value); } } diff --git a/contracts/token/ERC20/ERC20Capped.sol b/contracts/token/ERC20/ERC20Capped.sol index 23631f9049c..285c5df0fcf 100644 --- a/contracts/token/ERC20/ERC20Capped.sol +++ b/contracts/token/ERC20/ERC20Capped.sol @@ -9,36 +9,36 @@ import "./ERC20Mintable.sol"; */ contract ERC20Capped is ERC20Mintable { - uint256 private cap_; + uint256 private _cap; - constructor(uint256 _cap) public { - require(_cap > 0); - cap_ = _cap; + constructor(uint256 cap) public { + require(cap > 0); + _cap = cap; } /** * @return the cap for the token minting. */ function cap() public view returns(uint256) { - return cap_; + return _cap; } /** * @dev Function to mint tokens - * @param _to The address that will receive the minted tokens. - * @param _amount The amount of tokens to mint. + * @param to The address that will receive the minted tokens. + * @param amount The amount of tokens to mint. * @return A boolean that indicates if the operation was successful. */ function mint( - address _to, - uint256 _amount + address to, + uint256 amount ) public returns (bool) { - require(totalSupply().add(_amount) <= cap_); + require(totalSupply().add(amount) <= _cap); - return super.mint(_to, _amount); + return super.mint(to, amount); } } diff --git a/contracts/token/ERC20/ERC20Detailed.sol b/contracts/token/ERC20/ERC20Detailed.sol index 6becdee1086..ba411aa1fcc 100644 --- a/contracts/token/ERC20/ERC20Detailed.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -10,34 +10,34 @@ import "./IERC20.sol"; * just as on Ethereum all the operations are done in wei. */ contract ERC20Detailed is IERC20 { - string private name_; - string private symbol_; - uint8 private decimals_; + string private _name; + string private _symbol; + uint8 private _decimals; - constructor(string _name, string _symbol, uint8 _decimals) public { - name_ = _name; - symbol_ = _symbol; - decimals_ = _decimals; + constructor(string name, string symbol, uint8 decimals) public { + _name = name; + _symbol = symbol; + _decimals = decimals; } /** * @return the name of the token. */ function name() public view returns(string) { - return name_; + return _name; } /** * @return the symbol of the token. */ function symbol() public view returns(string) { - return symbol_; + return _symbol; } /** * @return the number of decimals of the token. */ function decimals() public view returns(uint8) { - return decimals_; + return _decimals; } } diff --git a/contracts/token/ERC20/ERC20Mintable.sol b/contracts/token/ERC20/ERC20Mintable.sol index 8b80dd80ae5..47e4fce9901 100644 --- a/contracts/token/ERC20/ERC20Mintable.sol +++ b/contracts/token/ERC20/ERC20Mintable.sol @@ -13,11 +13,11 @@ contract ERC20Mintable is ERC20, Ownable { event Mint(address indexed to, uint256 amount); event MintFinished(); - bool private mintingFinished_ = false; + bool private _mintingFinished = false; modifier onlyBeforeMintingFinished() { - require(!mintingFinished_); + require(!_mintingFinished); _; } @@ -30,26 +30,26 @@ contract ERC20Mintable is ERC20, Ownable { * @return true if the minting is finished. */ function mintingFinished() public view returns(bool) { - return mintingFinished_; + return _mintingFinished; } /** * @dev Function to mint tokens - * @param _to The address that will receive the minted tokens. - * @param _amount The amount of tokens to mint. + * @param to The address that will receive the minted tokens. + * @param amount The amount of tokens to mint. * @return A boolean that indicates if the operation was successful. */ function mint( - address _to, - uint256 _amount + address to, + uint256 amount ) public onlyMinter onlyBeforeMintingFinished returns (bool) { - _mint(_to, _amount); - emit Mint(_to, _amount); + _mint(to, amount); + emit Mint(to, amount); return true; } @@ -63,7 +63,7 @@ contract ERC20Mintable is ERC20, Ownable { onlyBeforeMintingFinished returns (bool) { - mintingFinished_ = true; + _mintingFinished = true; emit MintFinished(); return true; } diff --git a/contracts/token/ERC20/ERC20Pausable.sol b/contracts/token/ERC20/ERC20Pausable.sol index d631fe1d960..3f56183ee08 100644 --- a/contracts/token/ERC20/ERC20Pausable.sol +++ b/contracts/token/ERC20/ERC20Pausable.sol @@ -11,58 +11,58 @@ import "../../lifecycle/Pausable.sol"; contract ERC20Pausable is ERC20, Pausable { function transfer( - address _to, - uint256 _value + address to, + uint256 value ) public whenNotPaused returns (bool) { - return super.transfer(_to, _value); + return super.transfer(to, value); } function transferFrom( - address _from, - address _to, - uint256 _value + address from, + address to, + uint256 value ) public whenNotPaused returns (bool) { - return super.transferFrom(_from, _to, _value); + return super.transferFrom(from, to, value); } function approve( - address _spender, - uint256 _value + address spender, + uint256 value ) public whenNotPaused returns (bool) { - return super.approve(_spender, _value); + return super.approve(spender, value); } function increaseApproval( - address _spender, - uint _addedValue + address spender, + uint addedValue ) public whenNotPaused returns (bool success) { - return super.increaseApproval(_spender, _addedValue); + return super.increaseApproval(spender, addedValue); } function decreaseApproval( - address _spender, - uint _subtractedValue + address spender, + uint subtractedValue ) public whenNotPaused returns (bool success) { - return super.decreaseApproval(_spender, _subtractedValue); + return super.decreaseApproval(spender, subtractedValue); } } diff --git a/contracts/token/ERC20/IERC20.sol b/contracts/token/ERC20/IERC20.sol index 155c1cd9dbc..34d68fd62cb 100644 --- a/contracts/token/ERC20/IERC20.sol +++ b/contracts/token/ERC20/IERC20.sol @@ -8,17 +8,17 @@ pragma solidity ^0.4.24; interface IERC20 { function totalSupply() external view returns (uint256); - function balanceOf(address _who) external view returns (uint256); + function balanceOf(address who) external view returns (uint256); - function allowance(address _owner, address _spender) + function allowance(address owner, address spender) external view returns (uint256); - function transfer(address _to, uint256 _value) external returns (bool); + function transfer(address to, uint256 value) external returns (bool); - function approve(address _spender, uint256 _value) + function approve(address spender, uint256 value) external returns (bool); - function transferFrom(address _from, address _to, uint256 _value) + function transferFrom(address from, address to, uint256 value) external returns (bool); event Transfer( diff --git a/contracts/token/ERC20/RBACMintableToken.sol b/contracts/token/ERC20/RBACMintableToken.sol index 65e16f564f8..4c19ca03518 100644 --- a/contracts/token/ERC20/RBACMintableToken.sol +++ b/contracts/token/ERC20/RBACMintableToken.sol @@ -13,36 +13,36 @@ contract RBACMintableToken is ERC20Mintable, RBAC { /** * A constant role name for indicating minters. */ - string private constant ROLE_MINTER = "minter"; + string private constant _ROLE_MINTER = "minter"; /** * @dev override the Mintable token modifier to add role based logic */ modifier onlyMinter() { - checkRole(msg.sender, ROLE_MINTER); + checkRole(msg.sender, _ROLE_MINTER); _; } /** * @return true if the account is a minter, false otherwise. */ - function isMinter(address _account) public view returns(bool) { - return hasRole(_account, ROLE_MINTER); + function isMinter(address account) public view returns(bool) { + return hasRole(account, _ROLE_MINTER); } /** * @dev add a minter role to an address - * @param _minter address + * @param minter address */ - function addMinter(address _minter) public onlyOwner { - _addRole(_minter, ROLE_MINTER); + function addMinter(address minter) public onlyOwner { + _addRole(minter, _ROLE_MINTER); } /** * @dev remove a minter role from an address - * @param _minter address + * @param minter address */ - function removeMinter(address _minter) public onlyOwner { - _removeRole(_minter, ROLE_MINTER); + function removeMinter(address minter) public onlyOwner { + _removeRole(minter, _ROLE_MINTER); } } diff --git a/contracts/token/ERC20/SafeERC20.sol b/contracts/token/ERC20/SafeERC20.sol index 451cf136c81..48b15fba933 100644 --- a/contracts/token/ERC20/SafeERC20.sol +++ b/contracts/token/ERC20/SafeERC20.sol @@ -12,33 +12,33 @@ import "./IERC20.sol"; */ library SafeERC20 { function safeTransfer( - IERC20 _token, - address _to, - uint256 _value + IERC20 token, + address to, + uint256 value ) internal { - require(_token.transfer(_to, _value)); + require(token.transfer(to, value)); } function safeTransferFrom( - IERC20 _token, - address _from, - address _to, - uint256 _value + IERC20 token, + address from, + address to, + uint256 value ) internal { - require(_token.transferFrom(_from, _to, _value)); + require(token.transferFrom(from, to, value)); } function safeApprove( - IERC20 _token, - address _spender, - uint256 _value + IERC20 token, + address spender, + uint256 value ) internal { - require(_token.approve(_spender, _value)); + require(token.approve(spender, value)); } } diff --git a/contracts/token/ERC20/TokenTimelock.sol b/contracts/token/ERC20/TokenTimelock.sol index d8f96fa6668..9989ce77ada 100644 --- a/contracts/token/ERC20/TokenTimelock.sol +++ b/contracts/token/ERC20/TokenTimelock.sol @@ -12,47 +12,47 @@ contract TokenTimelock { using SafeERC20 for IERC20; // ERC20 basic token contract being held - IERC20 private token_; + IERC20 private _token; // beneficiary of tokens after they are released - address private beneficiary_; + address private _beneficiary; // timestamp when token release is enabled - uint256 private releaseTime_; + uint256 private _releaseTime; constructor( - IERC20 _token, - address _beneficiary, - uint256 _releaseTime + IERC20 token, + address beneficiary, + uint256 releaseTime ) public { // solium-disable-next-line security/no-block-members - require(_releaseTime > block.timestamp); - token_ = _token; - beneficiary_ = _beneficiary; - releaseTime_ = _releaseTime; + require(releaseTime > block.timestamp); + _token = token; + _beneficiary = beneficiary; + _releaseTime = releaseTime; } /** * @return the token being held. */ function token() public view returns(IERC20) { - return token_; + return _token; } /** * @return the beneficiary of the tokens. */ function beneficiary() public view returns(address) { - return beneficiary_; + return _beneficiary; } /** * @return the time when the tokens are released. */ function releaseTime() public view returns(uint256) { - return releaseTime_; + return _releaseTime; } /** @@ -60,11 +60,11 @@ contract TokenTimelock { */ function release() public { // solium-disable-next-line security/no-block-members - require(block.timestamp >= releaseTime_); + require(block.timestamp >= _releaseTime); - uint256 amount = token_.balanceOf(address(this)); + uint256 amount = _token.balanceOf(address(this)); require(amount > 0); - token_.safeTransfer(beneficiary_, amount); + _token.safeTransfer(_beneficiary, amount); } } diff --git a/contracts/token/ERC721/ERC721.sol b/contracts/token/ERC721/ERC721.sol index a2c63ef5527..ea21e69de99 100644 --- a/contracts/token/ERC721/ERC721.sol +++ b/contracts/token/ERC721/ERC721.sol @@ -14,27 +14,27 @@ import "../../introspection/SupportsInterfaceWithLookup.sol"; contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { // Token name - string internal name_; + string internal _name; // Token symbol - string internal symbol_; + string internal _symbol; // Mapping from owner to list of owned token IDs - mapping(address => uint256[]) private ownedTokens_; + mapping(address => uint256[]) private _ownedTokens; // Mapping from token ID to index of the owner tokens list - mapping(uint256 => uint256) private ownedTokensIndex_; + mapping(uint256 => uint256) private _ownedTokensIndex; // Array with all token ids, used for enumeration - uint256[] private allTokens_; + uint256[] private _allTokens; // Mapping from token id to position in the allTokens array - mapping(uint256 => uint256) private allTokensIndex_; + mapping(uint256 => uint256) private _allTokensIndex; // Optional mapping for token URIs - mapping(uint256 => string) private tokenURIs_; + mapping(uint256 => string) private _tokenURIs; - bytes4 private constant InterfaceId_ERC721Enumerable = 0x780e9d63; + bytes4 private constant _InterfaceId_ERC721Enumerable = 0x780e9d63; /** * 0x780e9d63 === * bytes4(keccak256('totalSupply()')) ^ @@ -42,7 +42,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { * bytes4(keccak256('tokenByIndex(uint256)')) */ - bytes4 private constant InterfaceId_ERC721Metadata = 0x5b5e139f; + bytes4 private constant _InterfaceId_ERC721Metadata = 0x5b5e139f; /** * 0x5b5e139f === * bytes4(keccak256('name()')) ^ @@ -53,13 +53,13 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { /** * @dev Constructor function */ - constructor(string _name, string _symbol) public { - name_ = _name; - symbol_ = _symbol; + 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); + _registerInterface(_InterfaceId_ERC721Enumerable); + _registerInterface(_InterfaceId_ERC721Metadata); } /** @@ -67,7 +67,7 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { * @return string representing the token name */ function name() external view returns (string) { - return name_; + return _name; } /** @@ -75,35 +75,35 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { * @return string representing the token symbol */ function symbol() external view returns (string) { - return symbol_; + 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 + * @param tokenId uint256 ID of the token to query */ - function tokenURI(uint256 _tokenId) public view returns (string) { - require(_exists(_tokenId)); - return tokenURIs_[_tokenId]; + 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 + * @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 + address owner, + uint256 index ) public view returns (uint256) { - require(_index < balanceOf(_owner)); - return ownedTokens_[_owner][_index]; + require(index < balanceOf(owner)); + return _ownedTokens[owner][index]; } /** @@ -111,107 +111,107 @@ contract ERC721 is SupportsInterfaceWithLookup, ERC721Basic, IERC721 { * @return uint256 representing the total amount of tokens */ function totalSupply() public view returns (uint256) { - return allTokens_.length; + 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 + * @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]; + 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 + * @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; + 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 + * @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; + 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 + * @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); + 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]; + uint256 tokenIndex = _ownedTokensIndex[tokenId]; + uint256 lastTokenIndex = _ownedTokens[from].length.sub(1); + uint256 lastToken = _ownedTokens[from][lastTokenIndex]; - ownedTokens_[_from][tokenIndex] = lastToken; + _ownedTokens[from][tokenIndex] = lastToken; // This also deletes the contents at the last position of the array - ownedTokens_[_from].length--; + _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; + _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 + * @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); + function _mint(address to, uint256 tokenId) internal { + super._mint(to, tokenId); - allTokensIndex_[_tokenId] = allTokens_.length; - allTokens_.push(_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 + * @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); + function _burn(address owner, uint256 tokenId) internal { + super._burn(owner, tokenId); // Clear metadata (if any) - if (bytes(tokenURIs_[_tokenId]).length != 0) { - delete tokenURIs_[_tokenId]; + 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]; + uint256 tokenIndex = _allTokensIndex[tokenId]; + uint256 lastTokenIndex = _allTokens.length.sub(1); + uint256 lastToken = _allTokens[lastTokenIndex]; - allTokens_[tokenIndex] = lastToken; - allTokens_[lastTokenIndex] = 0; + _allTokens[tokenIndex] = lastToken; + _allTokens[lastTokenIndex] = 0; - allTokens_.length--; - allTokensIndex_[_tokenId] = 0; - allTokensIndex_[lastToken] = tokenIndex; + _allTokens.length--; + _allTokensIndex[tokenId] = 0; + _allTokensIndex[lastToken] = tokenIndex; } } diff --git a/contracts/token/ERC721/ERC721Basic.sol b/contracts/token/ERC721/ERC721Basic.sol index fcc8397ea87..3a1512fb3f6 100644 --- a/contracts/token/ERC721/ERC721Basic.sol +++ b/contracts/token/ERC721/ERC721Basic.sol @@ -18,21 +18,21 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { // 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; + bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; // Mapping from token ID to owner - mapping (uint256 => address) private tokenOwner_; + mapping (uint256 => address) private _tokenOwner; // Mapping from token ID to approved address - mapping (uint256 => address) private tokenApprovals_; + mapping (uint256 => address) private _tokenApprovals; // Mapping from owner to number of owned token - mapping (address => uint256) private ownedTokensCount_; + mapping (address => uint256) private _ownedTokensCount; // Mapping from owner to operator approvals - mapping (address => mapping (address => bool)) private operatorApprovals_; + mapping (address => mapping (address => bool)) private _operatorApprovals; - bytes4 private constant InterfaceId_ERC721 = 0x80ac58cd; + bytes4 private constant _InterfaceId_ERC721 = 0x80ac58cd; /* * 0x80ac58cd === * bytes4(keccak256('balanceOf(address)')) ^ @@ -50,26 +50,26 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { public { // register the supported interfaces to conform to ERC721 via ERC165 - _registerInterface(InterfaceId_ERC721); + _registerInterface(_InterfaceId_ERC721); } /** * @dev Gets the balance of the specified address - * @param _owner address to query the balance of + * @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]; + 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 + * @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]; + function ownerOf(uint256 tokenId) public view returns (address) { + address owner = _tokenOwner[tokenId]; require(owner != address(0)); return owner; } @@ -79,79 +79,79 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * 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 + * @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); + 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); + _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 + * @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]; + 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 + * @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); + 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 + * @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 + address owner, + address operator ) public view returns (bool) { - return operatorApprovals_[_owner][_operator]; + 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 + * @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 + address from, + address to, + uint256 tokenId ) public { - require(_isApprovedOrOwner(msg.sender, _tokenId)); - require(_to != address(0)); + require(_isApprovedOrOwner(msg.sender, tokenId)); + require(to != address(0)); - _clearApproval(_from, _tokenId); - _removeTokenFrom(_from, _tokenId); - _addTokenTo(_to, _tokenId); + _clearApproval(from, tokenId); + _removeTokenFrom(from, tokenId); + _addTokenTo(to, tokenId); - emit Transfer(_from, _to, _tokenId); + emit Transfer(from, to, tokenId); } /** @@ -162,19 +162,19 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * 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 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 + address from, + address to, + uint256 tokenId ) public { // solium-disable-next-line arg-overflow - safeTransferFrom(_from, _to, _tokenId, ""); + safeTransferFrom(from, to, tokenId, ""); } /** @@ -184,141 +184,141 @@ contract ERC721Basic is SupportsInterfaceWithLookup, IERC721Basic { * `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 + * @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 + address from, + address to, + uint256 tokenId, + bytes data ) public { - transferFrom(_from, _to, _tokenId); + transferFrom(from, to, tokenId); // solium-disable-next-line arg-overflow - require(_checkAndCallSafeTransfer(_from, _to, _tokenId, _data)); + 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 + * @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]; + 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 + * @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 + address spender, + uint256 tokenId ) internal view returns (bool) { - address owner = ownerOf(_tokenId); + 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) + 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 + * @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); + 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 + * @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); + 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 + * @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); + 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 + * @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); + 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 + * @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); + 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 + * @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 + address from, + address to, + uint256 tokenId, + bytes data ) internal returns (bool) { - if (!_to.isContract()) { + if (!to.isContract()) { return true; } - bytes4 retval = IERC721Receiver(_to).onERC721Received( - msg.sender, _from, _tokenId, _data); - return (retval == ERC721_RECEIVED); + bytes4 retval = IERC721Receiver(to).onERC721Received( + msg.sender, from, tokenId, data); + return (retval == _ERC721_RECEIVED); } } diff --git a/contracts/token/ERC721/ERC721Pausable.sol b/contracts/token/ERC721/ERC721Pausable.sol index 4604597d923..0378373a957 100644 --- a/contracts/token/ERC721/ERC721Pausable.sol +++ b/contracts/token/ERC721/ERC721Pausable.sol @@ -10,33 +10,33 @@ import "../../lifecycle/Pausable.sol"; **/ contract ERC721Pausable is ERC721Basic, Pausable { function approve( - address _to, - uint256 _tokenId + address to, + uint256 tokenId ) public whenNotPaused { - super.approve(_to, _tokenId); + super.approve(to, tokenId); } function setApprovalForAll( - address _to, - bool _approved + address to, + bool approved ) public whenNotPaused { - super.setApprovalForAll(_to, _approved); + super.setApprovalForAll(to, approved); } function transferFrom( - address _from, - address _to, - uint256 _tokenId + address from, + address to, + uint256 tokenId ) public whenNotPaused { - super.transferFrom(_from, _to, _tokenId); + super.transferFrom(from, to, tokenId); } } diff --git a/contracts/token/ERC721/IERC721.sol b/contracts/token/ERC721/IERC721.sol index 24b7e01ceeb..ebf7060a199 100644 --- a/contracts/token/ERC721/IERC721.sol +++ b/contracts/token/ERC721/IERC721.sol @@ -10,14 +10,14 @@ import "./IERC721Basic.sol"; contract IERC721Enumerable is IERC721Basic { function totalSupply() public view returns (uint256); function tokenOfOwnerByIndex( - address _owner, - uint256 _index + address owner, + uint256 index ) public view - returns (uint256 _tokenId); + returns (uint256 tokenId); - function tokenByIndex(uint256 _index) public view returns (uint256); + function tokenByIndex(uint256 index) public view returns (uint256); } @@ -26,9 +26,9 @@ contract IERC721Enumerable is IERC721Basic { * @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); + function name() external view returns (string); + function symbol() external view returns (string); + function tokenURI(uint256 tokenId) public view returns (string); } diff --git a/contracts/token/ERC721/IERC721Basic.sol b/contracts/token/ERC721/IERC721Basic.sol index 6e71d1cf589..9dc6d946885 100644 --- a/contracts/token/ERC721/IERC721Basic.sol +++ b/contracts/token/ERC721/IERC721Basic.sol @@ -25,26 +25,26 @@ contract IERC721Basic is IERC165 { bool approved ); - function balanceOf(address _owner) public view returns (uint256 _balance); - function ownerOf(uint256 _tokenId) public view returns (address _owner); + 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 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) + 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) + 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 + address from, + address to, + uint256 tokenId, + bytes data ) public; } diff --git a/contracts/token/ERC721/IERC721Receiver.sol b/contracts/token/ERC721/IERC721Receiver.sol index b4c228d88f1..0914713a8f1 100644 --- a/contracts/token/ERC721/IERC721Receiver.sol +++ b/contracts/token/ERC721/IERC721Receiver.sol @@ -21,17 +21,17 @@ contract IERC721Receiver { * transfer. Return of other than the magic value MUST result in the * transaction being reverted. * Note: the contract address is always the message sender. - * @param _operator The address which called `safeTransferFrom` function - * @param _from The address which previously owned the token - * @param _tokenId The NFT identifier which is being transferred - * @param _data Additional data with no specified format + * @param operator The address which called `safeTransferFrom` function + * @param from The address which previously owned the token + * @param tokenId The NFT identifier which is being transferred + * @param data Additional data with no specified format * @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` */ function onERC721Received( - address _operator, - address _from, - uint256 _tokenId, - bytes _data + address operator, + address from, + uint256 tokenId, + bytes data ) public returns(bytes4); diff --git a/contracts/utils/Address.sol b/contracts/utils/Address.sol index 7c972200518..1f27c93f101 100644 --- a/contracts/utils/Address.sol +++ b/contracts/utils/Address.sol @@ -10,10 +10,10 @@ library Address { * Returns whether the target address is a contract * @dev This function will return false if invoked during the constructor of a contract, * as the code is not actually created until after the constructor finishes. - * @param _account address of the account to check + * @param account address of the account to check * @return whether the target address is a contract */ - function isContract(address _account) internal view returns (bool) { + function isContract(address account) internal view returns (bool) { uint256 size; // XXX Currently there is no better way to check if there is a contract in an address // than to check the size of the code at that address. @@ -22,7 +22,7 @@ library Address { // TODO Check this again before the Serenity release, because all addresses will be // contracts then. // solium-disable-next-line security/no-inline-assembly - assembly { size := extcodesize(_account) } + assembly { size := extcodesize(account) } return size > 0; } diff --git a/contracts/utils/AutoIncrementing.sol b/contracts/utils/AutoIncrementing.sol index 8c01861fb68..f739e539d8e 100644 --- a/contracts/utils/AutoIncrementing.sol +++ b/contracts/utils/AutoIncrementing.sol @@ -19,11 +19,11 @@ library AutoIncrementing { uint256 prevId; // default: 0 } - function nextId(Counter storage _counter) + function nextId(Counter storage counter) internal returns (uint256) { - _counter.prevId = _counter.prevId + 1; - return _counter.prevId; + counter.prevId = counter.prevId + 1; + return counter.prevId; } } diff --git a/contracts/utils/ReentrancyGuard.sol b/contracts/utils/ReentrancyGuard.sol index c588df5b925..7f232cce579 100644 --- a/contracts/utils/ReentrancyGuard.sol +++ b/contracts/utils/ReentrancyGuard.sol @@ -10,7 +10,7 @@ pragma solidity ^0.4.24; contract ReentrancyGuard { /// @dev counter to allow mutex lock with only one SSTORE operation - uint256 private guardCounter = 1; + uint256 private _guardCounter = 1; /** * @dev Prevents a contract from calling itself, directly or indirectly. @@ -21,10 +21,10 @@ contract ReentrancyGuard { * wrapper marked as `nonReentrant`. */ modifier nonReentrant() { - guardCounter += 1; - uint256 localCounter = guardCounter; + _guardCounter += 1; + uint256 localCounter = _guardCounter; _; - require(localCounter == guardCounter); + require(localCounter == _guardCounter); } } 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 55/67] 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 56/67] 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 369b8d6293e5dac5754cb03c85dc3deab1f5a0f7 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Thu, 6 Sep 2018 12:15:34 -0300 Subject: [PATCH 57/67] remove duplicate function definition --- contracts/crowdsale/validation/TimedCrowdsale.sol | 8 -------- 1 file changed, 8 deletions(-) diff --git a/contracts/crowdsale/validation/TimedCrowdsale.sol b/contracts/crowdsale/validation/TimedCrowdsale.sol index 4804a51eed9..c9726d9402b 100644 --- a/contracts/crowdsale/validation/TimedCrowdsale.sol +++ b/contracts/crowdsale/validation/TimedCrowdsale.sol @@ -58,14 +58,6 @@ contract TimedCrowdsale is Crowdsale { return block.timestamp >= openingTime_ && block.timestamp <= closingTime_; } - /** - * @return true if the crowdsale is open, false otherwise. - */ - function isOpen() public view returns (bool) { - // solium-disable-next-line security/no-block-members - return block.timestamp >= openingTime && block.timestamp <= closingTime; - } - /** * @dev Checks whether the period in which the crowdsale is open has already elapsed. * @return Whether crowdsale period has elapsed From b2f350dca6afeb26393388bcc0cdad2a36ed407c Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Wed, 5 Sep 2018 11:17:18 -0300 Subject: [PATCH 58/67] 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 59/67] 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 60/67] 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 61/67] 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 62/67] 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 63/67] 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 1002c20ffab1c654fbbae54fc285c9d867571853 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Fri, 7 Sep 2018 06:53:39 +0000 Subject: [PATCH 64/67] fix lint --- contracts/token/ERC20/ERC20Detailed.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/token/ERC20/ERC20Detailed.sol b/contracts/token/ERC20/ERC20Detailed.sol index 6643babb506..ba411aa1fcc 100644 --- a/contracts/token/ERC20/ERC20Detailed.sol +++ b/contracts/token/ERC20/ERC20Detailed.sol @@ -26,12 +26,14 @@ contract ERC20Detailed is IERC20 { 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. */ From 58524e39dbc4c5ac81325573f0143ac2f5ff6e12 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Fri, 7 Sep 2018 07:03:49 +0000 Subject: [PATCH 65/67] Remove underscore --- contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol index 066da70e0df..d2033984f5d 100644 --- a/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol +++ b/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol @@ -21,7 +21,7 @@ contract IndividuallyCappedCrowdsale is Crowdsale, CapperRole { * @param cap Wei limit for individual contribution */ function setCap(address beneficiary, uint256 cap) external onlyCapper { - _caps[beneficiary] = _cap; + _caps[beneficiary] = cap; } /** From 2458d72cbd5d98d7577d950f39a2a64482ad6b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Fri, 7 Sep 2018 11:19:31 -0300 Subject: [PATCH 66/67] Delete CapperRole.test.js --- 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'); -}); From 58e10505684aa26f20969342abef6523f4a698a9 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 7 Sep 2018 11:31:15 -0300 Subject: [PATCH 67/67] fix increaseApproval --- contracts/token/ERC20/ERC20Pausable.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/token/ERC20/ERC20Pausable.sol b/contracts/token/ERC20/ERC20Pausable.sol index dbb7ddc62da..32932355d8b 100644 --- a/contracts/token/ERC20/ERC20Pausable.sol +++ b/contracts/token/ERC20/ERC20Pausable.sol @@ -44,7 +44,7 @@ contract ERC20Pausable is ERC20, Pausable { return super.approve(spender, value); } - function increaseApproval( + function increaseAllowance( address spender, uint addedValue )