From 6529e8c09a26f8142ec3dc7d82cb695d69bc594d Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 21 Sep 2024 13:18:38 -0400 Subject: [PATCH] :sparkles: Add public mint toggle --- src/ProtoGravaNFT.sol | 25 ++++++++++++++++++++++++- src/test/ProtoGravaNFT.t.sol | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/ProtoGravaNFT.sol b/src/ProtoGravaNFT.sol index 7655d0e..fce3f11 100644 --- a/src/ProtoGravaNFT.sol +++ b/src/ProtoGravaNFT.sol @@ -43,6 +43,10 @@ library Events { /// @notice Emitted after default format is changed /// @param newDefaultFormat for all tokens event DefaultFormatChanged(string newDefaultFormat); + + /// @notice Emitted after public minting is toggled + /// @param isPublicMintEnabled for all tokens + event PublicMintToggled(bool isPublicMintEnabled); } /// @title ProtoGravaNFT @@ -72,6 +76,8 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { /// @notice Description string public description; + bool public isPublicMintEnabled; + /*////////////////////////////////////////////////////////////// MODIFIERS //////////////////////////////////////////////////////////////*/ @@ -89,6 +95,13 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { _; } + /// @notice Throws if called when public minting is disabled + modifier onlyWhenPublicMintEnabled() { + if (!isPublicMintEnabled && msg.sender != _owner) + revert PublicMintDisabled(); + _; + } + /*////////////////////////////////////////////////////////////// ERRORS //////////////////////////////////////////////////////////////*/ @@ -111,6 +124,9 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { /// @notice Thrown if user attempts to mint more than one token error OnePerUser(); + /// @notice Thrown if public minting is disabled + error PublicMintDisabled(); + /*////////////////////////////////////////////////////////////// CONSTRUCTOR //////////////////////////////////////////////////////////////*/ @@ -124,6 +140,7 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { ) ERC721(_name, _symbol) { defaultFormat = Defaults.DEFAULT_FOR_DEFAULT_IMAGE; description = Defaults.DEFAULT_DESCRIPTION; + isPublicMintEnabled = true; } /// @notice Get total non-burned supply of token @@ -304,7 +321,7 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { /* solhint-enable quotes */ /// @notice Mint a token - function mint() external { + function mint() external onlyWhenPublicMintEnabled { if (totalMinted + 1 >= MAX_TOTAL_MINTED) revert NoTokensLeft(); if (balanceOf(msg.sender) > 0) revert OnePerUser(); @@ -358,6 +375,12 @@ contract ProtoGravaNFT is ERC721, LilENS, LilOwnable, LilHash { return formattedTokenURI; } + /// @notice Toggle public minting + function ownerTogglePublicMint() public onlyContractOwner { + isPublicMintEnabled = !isPublicMintEnabled; + emit Events.PublicMintToggled(isPublicMintEnabled); + } + /// @notice Update default Gravatar image format for future tokens /// @param newDefaultFormat for Gravatar image API function ownerSetDefaultFormat( diff --git a/src/test/ProtoGravaNFT.t.sol b/src/test/ProtoGravaNFT.t.sol index c3a0c6a..6a3f06d 100644 --- a/src/test/ProtoGravaNFT.t.sol +++ b/src/test/ProtoGravaNFT.t.sol @@ -109,6 +109,21 @@ contract ProtoGravNFTTestContract is ProtoGravaNFTTest { assertEq(charlie.getAddress(), charlieAddress); } + /// @notice Test that public minting is enabled by default + function testPublicMintDisabled() public view { + assertTrue(protogravanft.isPublicMintEnabled()); + } + + /// @notice Test that public minting can be disabled + function testPublicMintToggle() public { + protogravanft.ownerTogglePublicMint(); + assertTrue(!protogravanft.isPublicMintEnabled()); + vm.expectRevert(abi.encodeWithSignature("PublicMintDisabled()")); + alice.mint(); + protogravanft.mint(); + assertEq(protogravanft.balanceOf(address(this)), 1); + } + /// @notice Allow Alice to mint a token for approved hash function testAliceMint() public { // Collect Alice balance of tokens before mint