0

I attempting to implement an NFT minter into my application, but want to set prices for minting an NFT. I'm using a smart contract template I found but haven't been able to figure out how to set prices. I've seen similar solutions for a smart contract that is like mine, but I can't seem to get prices to be set correctly.

pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract HiddenInPlainSight is ERC721, ERC721Enumerable, ERC721URIStorage {
    using SafeMath for uint256;
    uint public constant mintPrice = (4 ether/10);

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }
    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }
    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }
    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
    constructor() ERC721("Hidden In Plain Sight", "TLJ") {}
    function mint(string memory _uri) public payable {

        uint256 mintIndex = totalSupply();

        require(msg.value == mintPrice, "Provide more ETH");
        _safeMint(msg.sender, mintIndex);
        _setTokenURI(mintIndex, _uri);
    }
}

I added a require statement in the mint function but when I try to call the function, the transaction fails. Any ideas?

3 Answers 3

0

probably you're not calling the mint function with the exact value,

the function is successful with 400000000 gwei or 400000000000000000 Wei (top left of the screenshot). since require(msg.value == mintPrice, "Provide more ETH"); and mintPrice is 0.4 eth (400000000000000000 wei).

by the way, you are overriding all the functions without changing the logic so you can remove it. example:

   function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }

Remix

1
  • Thank you! I was overlooking that, this helped me a lot
    – beagle
    Mar 26, 2022 at 20:58
0

Why don't you consider using WETH instead of using ETH native token for payment?

Minter has to allow WETH amount to this contract.

Check out my code below.

pragma solidity =0.8.14; // fix compiler version ( Preventing known bugs)
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; // IERC20 interface

contract HiddenInPlainSight is ERC721, ERC721Enumerable, ERC721URIStorage {
    using SafeMath for uint256;
    uint public constant mintPrice = (4 ether/10);

    // WETH address on mainnet
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }
    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }
    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }
    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
    constructor() ERC721("Hidden In Plain Sight", "TLJ") {}
    function mint(string memory _uri) public {

        uint256 mintIndex = totalSupply();

        // Minter have to wrap their ETH before or implement wrapping for them
        require(IERC20(WETH).balanceOf(msg.sender) >= mintPrice, "Not enough Wrapped ETH");
        
        // transfer WETH from msg.sender to this vault
        (bool success) = IERC20(WETH).transferFrom(msg.sender, address(this), mintPrice);
        require(success);
        
        _safeMint(msg.sender, mintIndex);
        _setTokenURI(mintIndex, _uri);
    }
}
0

You can modify your mint function to accept ETH.

 function mint(string memory _uri) public payable {
        require(msg.value = _setMinimumValue, "Price too low!");
        uint256 mintIndex = totalSupply();

        // Minter have to wrap their ETH before or implement wrapping for them
        require(IERC20(WETH).balanceOf(msg.sender) >= mintPrice, "Not enough Wrapped ETH");
        
        // transfer WETH from msg.sender to this vault
        (bool success) = IERC20(WETH).transferFrom(msg.sender, address(this), mintPrice);
        require(success);
        
        _safeMint(msg.sender, mintIndex);
        _setTokenURI(mintIndex, _uri);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.