0

I'm minting an NFT and listed it on my NFTMarketplace, after minting I keep getting ERC721: invalid token ID, whenever I try to create the sale on the market place. this is my nft code

  // SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./MarketTest.sol";

  /* Mints a token and lists it in the marketplace */
   contract CreateNft is ERC721URIStorage {

    NFTMarketplace public marketTest; 

    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    Counters.Counter private _itemsSold;

    constructor(address _contractAAddress, string memory _name, string memory _symbol ) ERC721(_name, _symbol) {
        marketTest = NFTMarketplace(_contractAAddress);
    }

    function createToken(string memory tokenURI, uint256 price)
        public
        payable
        returns (uint)
    {
        _tokenIds.increment();
        uint256 newTokenId = _tokenIds.current();

        _mint(msg.sender, newTokenId);
       addDeedMetadata(newTokenId, tokenURI);
        marketTest.createMarketItem(newTokenId, price);
        return newTokenId;
    }

       function addDeedMetadata(uint256 _tokenId, string memory _uri) public returns(bool){
        _setTokenURI(_tokenId, _uri);
        return true;
    }
   }

and this is the code that creates the market sale

  // SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

import "hardhat/console.sol";

contract BullchordMarketPlace  is ERC721URIStorage{
    event MarketItemCreated(
        uint256 indexed tokenId,
        address seller,
        address owner,
        uint256 price,
        bool sold
    );
    using Counters for Counters.Counter;

    //initiate the listing price : optional
    uint256 public listingPrice = 0.05 ether;

    // Array with all marketItem
    MarketItem[] public marketItems;

    // MarketItem struct which holds all the required info
    struct MarketItem {
        uint _tokenId;
        address payable owner;
        address payable buyer;
        address nftAddress;
        bool sold;
        uint256 price;
    }

    //holds bidder and amount
    struct Bid {
        address payable from;
        uint256 amount;
    }

constructor() ERC721("Bullchord Music NFT", "BMNFT"){}
    // Mapping from auction index to user bids
    mapping(uint256 => Bid[]) public auctionBids;

    // Mapping from market index to a list of marketItem
    mapping(uint256 => MarketItem) public idToMarketItems;
    // Mapping from market index to a list of owned marketItem
    mapping(address => uint[]) public marketItemsOwner;

    /**
     * @dev Guarantees msg.sender is owner of the given auction
     * @param _marketItemId uint ID of the marketItem to validate its ownership belongs to msg.sender
     */
    modifier isOwner(uint _marketItemId) {
        require(marketItems[_marketItemId].owner == msg.sender);
        _;
    }

    /**
     * @dev Disallow payments to this contract directly
     */
    fallback() external {
        revert();
    }

    /**
     * @dev Gets the total number of marketNft owned by an address
     * @param _owner address of the owner
     * @return uint total number of marketItems
     */
    function getMarketCountOfOwner(
        address _owner
    ) public view returns (uint256) {
        marketItemsOwner[_owner].length;
    }

    function createMarketNft(
        uint256 _tokenId,
        address marketNft,
        uint256 price
    ) public payable {
        require(price > 0, "Price must be higher than 0");
        //optional
        // require(msg.value > listingPrice, "Listing price not met");
            // Check if the caller (msg.sender) is the owner of the NFT
            
        require(ownerOf(_tokenId) == msg.sender, "You are not the owner of this NFT");
        idToMarketItems[_tokenId] = MarketItem(
            _tokenId,
            payable(msg.sender),
            payable(address(this)),
            marketNft,
            false,
            price
        );

        marketItemsOwner[msg.sender].push(_tokenId);
        _transfer(msg.sender, address(this), _tokenId); 
    }
// 0xa8eC6FFEEf90f12B59C81b501F97BcDea26B3d9b


    function buyMusicNft(uint256 _tokenId) public payable {
        // accessing the MarketItem struct
        MarketItem storage marketItem = idToMarketItems[_tokenId];

        //we check that the item havent being sold
        require(!marketItem.sold, "NFT is already sold");

        // we check the price is equal or greater than the value
        require(msg.value >= marketItem.price, "Insufficient Ether sent");

        _transfer(address(this), msg.sender, _tokenId); 

        marketItem.sold = true;
        //transfering the amount to the owner of the nft
        marketItem.owner.transfer(msg.value);
    }

}

2
  • I assume those are two separate contracts, can you post both contracts full code, and tell a bit more about the exact method you're using to list your token?
    – Foxxxey
    Sep 20 at 12:18
  • I just updated the market code, I minted the NFT seperately and passed it in as param to the create market sale, I also tried passing the marketPlace address to the NFt mint contract, I got the same error.
    – Santos
    Sep 20 at 12:30

1 Answer 1

0

You defined two separate contracts and each of them inherets from ERC721URIStorage.

contract CreateNft is ERC721URIStorage { ... }

contract BullchordMarketPlace  is ERC721URIStorage { ... }

You are minting the NFT in CreateNft contract, and add it in BullchordMarketPlace contract which means that there is no NFTs minted yet in this contract, and I think this is the problem.

You can't making the marketplace inherits the ERC721, you should make the marketplace separated from the NFTs, and deal with them using the interface.

It is better to make the NFT marketplace has an access to the NFT, and not the owner, which means that NFT marketplace can transfer the NFT of the user but the NFT is still owned by the user.

Here is a function that list item in an NFT marketplace contract.

/**
 * @notice Method for listing NFT
 * @param nftAddress Address of NFT contract
 * @param tokenId Token ID of NFT
 * @param price sale price for each item
 */
function listItem(
    address nftAddress,
    uint256 tokenId,
    uint256 price
)
    external
    notListed(nftAddress, tokenId) // this modifiers to check that the NFT is not already listed in the market
    isOwner(nftAddress, tokenId, msg.sender) // modifier that checks that the NFT owner is the caller
{
    if (price <= 0) {
        revert('NFTmarketplace: price must be above zero');
    }
    // interacting with the NFT using the interace (IERC721)
    IERC721 nft = IERC721(nftAddress);

    // that marketplace should have an access to the NFT in order to be able to transfer it when a buy occuars
    if (!(nft.isApprovedForAll(msg.sender, address(this)) || nft.getApproved(tokenId) == address(this))) {
        revert("ERC721: NFTmarketplace don't have access to this item");
    }

    // Add the NFT to listed items in the marketplace
    s_listings[nftAddress][tokenId] = Listing(price, msg.sender);

    // Emit an event that an NFT is successfully listed
    emit ItemListed(msg.sender, nftAddress, tokenId, price);
}
2
  • This works!!, thank you so much. Can I use this function for re-listing after it has been purchased by a user.
    – Santos
    Sep 20 at 19:40
  • When a purchase occurs, you remove the Item from your listings. So If the new owner tries to list it, it will not give errors. You can check the marketplace I made as a reference, but The code is not audited yet. github.com/Al-Qa-qa/NFTify
    – Al-Qa'qa'
    Sep 21 at 7:16

Your Answer

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

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