From 1285ea7804859c4a1b1f4917a75addfc5c37618a Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 29 Jul 2022 15:45:51 +0000 Subject: [PATCH] Add suggested changes --- contracts/modules/Minters/EditionMinter.sol | 4 +- .../FixedPricePermissionedSaleMinter.sol | 8 +- .../Minters/FixedPricePublicSaleMinter.sol | 6 +- tests/modules/Minters/EditionMinter.t.sol | 99 +++++++++++++++++++ .../FixedPricePermissionedSaleMinter.t.sol | 40 +------- .../Minters/FixedPricePublicSaleMinter.t.sol | 77 +-------------- 6 files changed, 117 insertions(+), 117 deletions(-) create mode 100644 tests/modules/Minters/EditionMinter.t.sol diff --git a/contracts/modules/Minters/EditionMinter.sol b/contracts/modules/Minters/EditionMinter.sol index a3d77704..9f822af3 100644 --- a/contracts/modules/Minters/EditionMinter.sol +++ b/contracts/modules/Minters/EditionMinter.sol @@ -23,7 +23,7 @@ abstract contract EditionMinter { error MintControllerNotFound(); /// @dev A mint controller is already assigned to this edition. - error MintControllerAlreadyExists(); + error MintControllerAlreadyExists(address controller); /// @dev Emitted when the mint `controller` for `edition` is changed. event MintControllerUpdated(address indexed edition, address indexed controller); @@ -45,7 +45,7 @@ abstract contract EditionMinter { /// /// - The `edition` must not have a controller. function _createEditionMintController(address edition) internal { - if (_controllers[edition] != address(0)) revert MintControllerAlreadyExists(); + if (_controllers[edition] != address(0)) revert MintControllerAlreadyExists(_controllers[edition]); _controllers[edition] = msg.sender; emit MintControllerUpdated(edition, msg.sender); } diff --git a/contracts/modules/Minters/FixedPricePermissionedSaleMinter.sol b/contracts/modules/Minters/FixedPricePermissionedSaleMinter.sol index e5343b2d..c27d5e41 100644 --- a/contracts/modules/Minters/FixedPricePermissionedSaleMinter.sol +++ b/contracts/modules/Minters/FixedPricePermissionedSaleMinter.sol @@ -11,7 +11,7 @@ contract FixedPricePermissionedSaleMinter is EditionMinter { using ECDSA for bytes32; error WrongEtherValue(); - error MintOutOfStock(); + error SoldOut(); error InvalidSignature(); @@ -67,12 +67,12 @@ contract FixedPricePermissionedSaleMinter is EditionMinter { bytes calldata signature ) public payable { EditionMintData storage data = editionMintData[edition]; - if ((data.totalMinted += quantity) > data.maxMinted) revert MintOutOfStock(); - if (data.price * quantity != msg.value) revert MintWithWrongEtherValue(); + if ((data.totalMinted += quantity) > data.maxMinted) revert SoldOut(); + if (data.price * quantity != msg.value) revert WrongEtherValue(); bytes32 hash = keccak256(abi.encode(msg.sender, edition)); hash = hash.toEthSignedMessageHash(); - if (hash.recover(signature) != data.signer) revert MintWithInvalidSignature(); + if (hash.recover(signature) != data.signer) revert InvalidSignature(); ISoundEditionV1(edition).mint{ value: msg.value }(edition, quantity); } diff --git a/contracts/modules/Minters/FixedPricePublicSaleMinter.sol b/contracts/modules/Minters/FixedPricePublicSaleMinter.sol index 605ba2a3..bf755a76 100644 --- a/contracts/modules/Minters/FixedPricePublicSaleMinter.sol +++ b/contracts/modules/Minters/FixedPricePublicSaleMinter.sol @@ -9,7 +9,7 @@ import "../../SoundEdition/ISoundEditionV1.sol"; contract FixedPricePublicSaleMinter is EditionMinter { error WrongEtherValue(); - error OutOfStock(); + error SoldOut(); error MintNotStarted(); @@ -69,8 +69,8 @@ contract FixedPricePublicSaleMinter is EditionMinter { function mint(address edition, uint32 quantity) public payable { EditionMintData storage data = editionMintData[edition]; - if ((data.totalMinted += quantity) > data.maxMinted) revert MintOutOfStock(); - if (data.price * quantity != msg.value) revert MintWithWrongEtherValue(); + if ((data.totalMinted += quantity) > data.maxMinted) revert SoldOut(); + if (data.price * quantity != msg.value) revert WrongEtherValue(); if (block.timestamp < data.startTime) revert MintNotStarted(); if (data.endTime < block.timestamp) revert MintHasEnded(); ISoundEditionV1(edition).mint{ value: msg.value }(edition, quantity); diff --git a/tests/modules/Minters/EditionMinter.t.sol b/tests/modules/Minters/EditionMinter.t.sol new file mode 100644 index 00000000..49bf90ed --- /dev/null +++ b/tests/modules/Minters/EditionMinter.t.sol @@ -0,0 +1,99 @@ +pragma solidity ^0.8.15; + +import "../../TestConfig.sol"; +import "../../../contracts/SoundEdition/SoundEditionV1.sol"; +import "../../../contracts/SoundCreator/SoundCreatorV1.sol"; +import "../../../contracts/modules/Minters/FixedPricePublicSaleMinter.sol"; + +contract EditionMinterTests is TestConfig, EditionMinter { + function createEditionMintController(address edition) external { + _createEditionMintController(edition); + } + + function deleteEditionMintController(address edition) external { + _deleteEditionMintController(edition); + } + + function test_createEditionMintControllerEmitsEvent(address edition) external { + address controller = getRandomAccount(1); + vm.expectEmit(false, false, false, true); + emit MintControllerUpdated(address(edition), controller); + vm.prank(controller); + this.createEditionMintController(address(edition)); + } + + function test_createEditionMintControllerChangesController(address edition) external { + address controller = getRandomAccount(1); + assertEq(this.editionMintController(edition), address(0)); + vm.prank(controller); + this.createEditionMintController(address(edition)); + assertEq(this.editionMintController(edition), controller); + } + + function test_createEditionMintControllerRevertsWhenAlreadyExists(address edition) external { + address controller0 = getRandomAccount(0); + address controller1 = getRandomAccount(1); + vm.prank(controller0); + this.createEditionMintController(address(edition)); + vm.expectRevert(abi.encodeWithSelector(EditionMinter.MintControllerAlreadyExists.selector, controller0)); + vm.prank(controller0); + this.createEditionMintController(address(edition)); + vm.prank(controller1); + vm.expectRevert(abi.encodeWithSelector(EditionMinter.MintControllerAlreadyExists.selector, controller0)); + this.createEditionMintController(address(edition)); + } + + function test_setEditionMintControllerEmitsEvent(address edition) external { + address controller0 = getRandomAccount(0); + address controller1 = getRandomAccount(1); + vm.prank(controller0); + this.createEditionMintController(address(edition)); + vm.expectEmit(false, false, false, true); + emit MintControllerUpdated(address(edition), controller1); + vm.prank(controller0); + this.setEditionMintController(address(edition), controller1); + } + + function test_setEditionMintControllerChangesController(address edition) external { + address controller0 = getRandomAccount(0); + address controller1 = getRandomAccount(1); + vm.prank(controller0); + this.createEditionMintController(address(edition)); + vm.prank(controller0); + this.setEditionMintController(address(edition), controller1); + assertEq(this.editionMintController(edition), controller1); + } + + function test_deleteEditionMintControllerEmitsEvent(address edition) external { + address controller = getRandomAccount(0); + vm.prank(controller); + this.createEditionMintController(address(edition)); + vm.expectEmit(false, false, false, true); + emit MintControllerUpdated(address(edition), address(0)); + vm.prank(controller); + this.deleteEditionMintController(address(edition)); + } + + function test_deleteEditionMintRevertsIfCallerUnauthorized(address edition) public { + address controller0 = getRandomAccount(0); + address controller1 = getRandomAccount(1); + vm.prank(controller0); + this.createEditionMintController(address(edition)); + + vm.prank(controller1); + vm.expectRevert(EditionMinter.MintControllerUnauthorized.selector); + this.deleteEditionMintController(address(edition)); + } + + function test_deleteEditionMintRevertsIfMintEditionDoesNotExist(address edition0, address edition1) public { + vm.assume(edition0 != edition1); + + address controller = getRandomAccount(0); + vm.prank(controller); + this.createEditionMintController(address(edition0)); + + vm.prank(controller); + vm.expectRevert(EditionMinter.MintControllerNotFound.selector); + this.deleteEditionMintController(address(edition1)); + } +} diff --git a/tests/modules/Minters/FixedPricePermissionedSaleMinter.t.sol b/tests/modules/Minters/FixedPricePermissionedSaleMinter.t.sol index b4980dd1..0337c777 100644 --- a/tests/modules/Minters/FixedPricePermissionedSaleMinter.t.sol +++ b/tests/modules/Minters/FixedPricePermissionedSaleMinter.t.sol @@ -61,36 +61,6 @@ contract FixedPricePermissionedSaleMinterTests is TestConfig { minter.createEditionMint(address(edition), PRICE, _signerAddress(), MAX_MINTED); } - function test_createEditionMintRevertsIfMintEditionExists() public { - (SoundEditionV1 edition, FixedPricePermissionedSaleMinter minter) = _createEditionAndMinter(); - - vm.expectRevert(EditionMinter.MintControllerAlreadyExists.selector); - minter.createEditionMint(address(edition), PRICE, _signerAddress(), MAX_MINTED); - } - - function test_deleteEditionMintRevertsIfCallerUnauthorized() public { - (SoundEditionV1 edition, FixedPricePermissionedSaleMinter minter) = _createEditionAndMinter(); - - address caller = getRandomAccount(1); - vm.prank(caller); - vm.expectRevert(EditionMinter.MintControllerUnauthorized.selector); - minter.deleteEditionMint(address(edition)); - - minter.setEditionMintController(address(edition), caller); - vm.prank(caller); - minter.deleteEditionMint(address(edition)); - } - - function test_deleteEditionMintRevertsIfMintEditionDoesNotExist() public { - (SoundEditionV1 edition, FixedPricePermissionedSaleMinter minter) = _createEditionAndMinter(); - - minter.deleteEditionMint(address(edition)); - - vm.expectRevert(EditionMinter.MintControllerNotFound.selector); - - minter.deleteEditionMint(address(edition)); - } - function test_mintWithoutCorrectSignatureReverts() public { (SoundEditionV1 edition, FixedPricePermissionedSaleMinter minter) = _createEditionAndMinter(); @@ -100,7 +70,7 @@ contract FixedPricePermissionedSaleMinterTests is TestConfig { vm.prank(caller); minter.mint{ value: PRICE }(address(edition), 1, sig); - vm.expectRevert(FixedPricePermissionedSaleMinter.MintWithInvalidSignature.selector); + vm.expectRevert(FixedPricePermissionedSaleMinter.InvalidSignature.selector); minter.mint{ value: PRICE }(address(edition), 1, sig); } @@ -111,25 +81,25 @@ contract FixedPricePermissionedSaleMinterTests is TestConfig { bytes memory sig = _getSignature(caller, address(edition)); vm.prank(caller); - vm.expectRevert(FixedPricePermissionedSaleMinter.MintWithWrongEtherValue.selector); + vm.expectRevert(FixedPricePermissionedSaleMinter.WrongEtherValue.selector); minter.mint{ value: PRICE * 2 }(address(edition), 1, sig); } - function test_mintDuringOutOfStockReverts() public { + function test_mintWhenSoldOutReverts() public { (SoundEditionV1 edition, FixedPricePermissionedSaleMinter minter) = _createEditionAndMinter(); address caller = getRandomAccount(1); bytes memory sig = _getSignature(caller, address(edition)); vm.prank(caller); - vm.expectRevert(FixedPricePermissionedSaleMinter.MintOutOfStock.selector); + vm.expectRevert(FixedPricePermissionedSaleMinter.SoldOut.selector); minter.mint{ value: PRICE * (MAX_MINTED + 1) }(address(edition), MAX_MINTED + 1, sig); vm.prank(caller); minter.mint{ value: PRICE * MAX_MINTED }(address(edition), MAX_MINTED, sig); vm.prank(caller); - vm.expectRevert(FixedPricePermissionedSaleMinter.MintOutOfStock.selector); + vm.expectRevert(FixedPricePermissionedSaleMinter.SoldOut.selector); minter.mint{ value: PRICE }(address(edition), 1, sig); } diff --git a/tests/modules/Minters/FixedPricePublicSaleMinter.t.sol b/tests/modules/Minters/FixedPricePublicSaleMinter.t.sol index eef53960..7200bd62 100644 --- a/tests/modules/Minters/FixedPricePublicSaleMinter.t.sol +++ b/tests/modules/Minters/FixedPricePublicSaleMinter.t.sol @@ -23,8 +23,6 @@ contract FixedPricePublicSaleMinterTests is TestConfig { uint32 maxMinted ); - event MintControllerUpdated(address indexed edition, address indexed controller); - function _createEditionAndMinter() internal returns (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) { edition = SoundEditionV1( soundCreator.createSound(SONG_NAME, SONG_SYMBOL, METADATA_MODULE, BASE_URI, CONTRACT_URI) @@ -51,73 +49,6 @@ contract FixedPricePublicSaleMinterTests is TestConfig { minter.createEditionMint(address(edition), PRICE, START_TIME, END_TIME, MAX_MINTED); } - function test_createEditionMintEmitsMintControllerUpdatedEvent() public { - SoundEditionV1 edition = SoundEditionV1( - soundCreator.createSound(SONG_NAME, SONG_SYMBOL, METADATA_MODULE, BASE_URI, CONTRACT_URI) - ); - - FixedPricePublicSaleMinter minter = new FixedPricePublicSaleMinter(); - - vm.expectEmit(false, false, false, true); - - emit MintControllerUpdated(address(edition), edition.owner()); - - minter.createEditionMint(address(edition), PRICE, START_TIME, END_TIME, MAX_MINTED); - } - - function test_createEditionMintRevertsIfMintEditionExists() public { - (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); - - vm.expectRevert(EditionMinter.MintControllerAlreadyExists.selector); - - minter.createEditionMint(address(edition), PRICE, START_TIME, END_TIME, MAX_MINTED); - } - - function test_deleteEditionMintEmitsMintControllerUpdatedEvent() public { - (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); - - vm.expectEmit(false, false, false, true); - - emit MintControllerUpdated(address(edition), address(0)); - - minter.deleteEditionMint(address(edition)); - } - - function test_setEditionMintControllerEmitsMintControllerUpdatedEvent() public { - (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); - - vm.expectEmit(false, false, false, true); - - address newController = getRandomAccount(1); - - emit MintControllerUpdated(address(edition), newController); - - minter.setEditionMintController(address(edition), newController); - } - - function test_deleteEditionMintRevertsIfCallerUnauthorized() public { - (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); - - address caller = getRandomAccount(1); - vm.prank(caller); - vm.expectRevert(EditionMinter.MintControllerUnauthorized.selector); - minter.deleteEditionMint(address(edition)); - - minter.setEditionMintController(address(edition), caller); - vm.prank(caller); - minter.deleteEditionMint(address(edition)); - } - - function test_deleteEditionMintRevertsIfMintEditionDoesNotExist() public { - (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); - - minter.deleteEditionMint(address(edition)); - - vm.expectRevert(EditionMinter.MintControllerNotFound.selector); - - minter.deleteEditionMint(address(edition)); - } - function test_mintBeforeStartTimeReverts() public { (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); @@ -148,21 +79,21 @@ contract FixedPricePublicSaleMinterTests is TestConfig { minter.mint{ value: PRICE }(address(edition), 1); } - function test_mintDuringOutOfStockReverts() public { + function test_mintWhenSoldOutReverts() public { (SoundEditionV1 edition, FixedPricePublicSaleMinter minter) = _createEditionAndMinter(); vm.warp(START_TIME); address caller = getRandomAccount(1); vm.prank(caller); - vm.expectRevert(FixedPricePublicSaleMinter.MintOutOfStock.selector); + vm.expectRevert(FixedPricePublicSaleMinter.SoldOut.selector); minter.mint{ value: PRICE * (MAX_MINTED + 1) }(address(edition), MAX_MINTED + 1); vm.prank(caller); minter.mint{ value: PRICE * MAX_MINTED }(address(edition), MAX_MINTED); vm.prank(caller); - vm.expectRevert(FixedPricePublicSaleMinter.MintOutOfStock.selector); + vm.expectRevert(FixedPricePublicSaleMinter.SoldOut.selector); minter.mint{ value: PRICE }(address(edition), 1); } @@ -173,7 +104,7 @@ contract FixedPricePublicSaleMinterTests is TestConfig { address caller = getRandomAccount(1); vm.prank(caller); - vm.expectRevert(FixedPricePublicSaleMinter.MintWithWrongEtherValue.selector); + vm.expectRevert(FixedPricePublicSaleMinter.WrongEtherValue.selector); minter.mint{ value: PRICE * 2 }(address(edition), 1); }