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, +};