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);
}
}