DEV Community

Discussion on: How to Build a Full Stack NFT Marketplace - V2 (2022)

 
mateusasferreira profile image
Mateus Ferreira

Solved my issue yesterday by writing a custom function to tranfer tokens on my NFT contract. Like this:

contract NFT is ERC721URIStorage {
    (...)
    function transferToken(address from, address to, uint256 tokenId) external {
        require(ownerOf(tokenId) == from, "From address must be token owner");
        _transfer(from, to, tokenId);
    }
}
Enter fullscreen mode Exit fullscreen mode

And in my Market Contract:

import "./NFT.sol";

contract NFTMarket is ReentrancyGuard {
    (...)
    function putItemToResell(address nftContract, uint256 itemId, uint256 newPrice)
        public
        payable
        nonReentrant
        onlyItemOwner(itemId)
    {
        (...)
        uint256 tokenId = idToMarketItem[itemId].tokenId;

        NFT tokenContract = NFT(nftContract);

        tokenContract.transferToken(msg.sender, address(this), tokenId);        
    }

}
Enter fullscreen mode Exit fullscreen mode

Don't know all the implications of this solutions and if it may fail or cause security issues somehow but it solved my problem for know.

Thread Thread
 
ssf profile image
Sione

Thanks that solution works but I agree with the implications it may bring if any. Thank you though! Ill update you if I find another stable solution.

Thread Thread
 
mateusasferreira profile image
Mateus Ferreira

my pleasure! Keep me posted about the solutions you may find.

Thread Thread
 
javilonte profile image
Javilonte

Hi, thank you for posting this, i would like to use your function on my code but it says "onlyItemOwner(itemId)" never used, am i missing something? thanks again!

Thread Thread
 
mateusasferreira profile image
Mateus Ferreira

Hey there! its a modifier that you're missing. I added this to my contract so I can use it in functions to ensure only the item's owner can execute it:


    modifier onlyItemOwner(uint256 id) {
        require(
            idToMarketItem[id].owner == msg.sender,
            "Only product owner can do this operation"
        );
        _;
    }

Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
javilonte profile image
Javilonte

thanksss