-1

I am building a NFT Marketplace

  • I use 2 smart contracts to :
    • first, mint the token
    • then, call setApprovalForAll() to authorize the marketplace contract to transfer the token

strangely, the owner is address(0) when it should be the msg.sender when I created the token last

" The transaction has been reverted to the initial state. Reason provided by the contract: "ERC721: invalid token ID". "

This the source code of the 2 smart contracts below :

  1. smart contract : NFT
  2. smart contract : NFTMarketplace

Thanks for the help

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

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

contract NFT is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    address contractAddress;

    constructor(address marketplaceAddress) ERC721("NFTMarketplace", "NFTM") {
        contractAddress = marketplaceAddress;
    }

    event TokenMinted (
        uint256 indexed tokenId,
        string tokenURI
    );

    function createToken(string memory tokenURI) public returns (uint) {
        uint256 currentTokenId = _tokenIds.current();
        _safeMint(msg.sender, currentTokenId);
        _setTokenURI(currentTokenId, tokenURI);
        setApprovalForAll(contractAddress, true);
        _tokenIds.increment(); 

        emit TokenMinted(currentTokenId, tokenURI);
        return currentTokenId;
    }

    function getCurrentToken() public view returns (uint256) {
        return _tokenIds.current();
    }
}

contract NFTMarketplace is ReentrancyGuard, ERC721 {

    using Counters for Counters.Counter;
    //_tokenIds variable has the most recent minted tokenId
   
    //Keeps track of the number of items sold on the marketplace
    Counters.Counter private _itemsSold;
    //owner is the contract address that created the smart contract
    address payable owner;
    //The fee charged by the marketplace to be allowed to list an NFT
    uint256 listPrice = 0.01 ether;

    constructor() ERC721("NFTMarketplace", "NFTM") {}

    //The structure to store info about a listed token
    struct Token {
        uint256 tokenId;
        string tokenURI;
        address nftContract;
        string name;
        address payable owner;
        uint256 price;
        bool isListed;
    }

    //the event emitted when a token is successfully listed
    event TokenListedSuccess (
        uint256 indexed tokenId,
        address nftContract,
        address owner,
        uint256 price,
        bool isListed
    );

    //This mapping maps tokenId to token info and is helpful when retrieving details about a tokenId
    mapping(uint256 => Token) private idToToken;

    function updateListPrice(uint256 _listPrice) public payable {
        require(owner == msg.sender, "Only owner can update listing price");
        listPrice = _listPrice;
    }

    //The first time a token is created, it is listed here
    // make it payable with money -- add require
    function listToken(address nftContract, uint256 currentTokenId, string memory tokenURI, string memory name, uint256 price) public payable {

        require(msg.value > 0, "Price must be at least 1 wei");
        require(msg.value == listPrice, "Price must be equal to listing price");

        idToToken[currentTokenId] = Token(
            currentTokenId,
            tokenURI,
            nftContract,
            name,
            payable(address(this)),
            listPrice, 
            true
        );
  
        emit TokenListedSuccess(
        currentTokenId,
        nftContract,
        msg.sender,
        listPrice, 
        true
        );
    }

    function buyNFT(address nftContract, uint256 itemId) public payable nonReentrant {
        uint price = idToToken[itemId].price;
        uint tokenId = idToToken[itemId].tokenId;
        address seller = ERC721.ownerOf(tokenId);
        address buyer = msg.sender;
        require(msg.value > 0, "You need to send some ether");
        require(buyer != seller,"You already own this nft");
        require(msg.value == price, "Please submit the asking price in order to complete the purchase");

        idToToken[itemId].isListed = false;
        idToToken[itemId].owner = payable(buyer);
        payable(seller).transfer(price);

        _itemsSold.increment();

        IERC721(nftContract).transferFrom(seller, buyer, tokenId);
    }

    /* ...rest of smart contract */
}
user9958772
  • 147
  • 2
  • 9

1 Answers1

0

I think you forget to assign "owner" value.
Typically, we set it in constructor. like

constructor() {
        i_owner = msg.sender; 
    }
Mac Yang
  • 147
  • 1
  • 1
  • 7