0

I'm making an nft marketplace and I want the buyers to buy the nfts with WETH.

    function createMarketSale(address nftContract, uint256 itemId)
        public
        nonReentrant
    {
        require(
            msg.sender != idToMarketItem[itemId].seller,
            "you cannot buy this item, this is your's"
        );

        uint256 price = idToMarketItem[itemId].price;
        uint256 tokenId = idToMarketItem[itemId].tokenId;

        require(WETH.balanceOf(msg.sender) >= price, "Insufficient funds");

        WETH.transferFrom(
            msg.sender,
            idToMarketItem[itemId].seller,
            price
        );

        IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId);
    }

Is this supposed to work?

8
  • if the sender is not the owner of the token then he need approve first. you cannot approve and transfer from in the same tx and the owner should by himself call the approve method in the token contract
    – Majd TL
    Commented Dec 11, 2021 at 17:45
  • okay, so how do I make that happen?
    – eth_sign
    Commented Dec 11, 2021 at 18:42
  • you tell the owner to go to the token contract and approve the tokens for you or your contract :) , you cannot do it for them
    – Majd TL
    Commented Dec 11, 2021 at 19:21
  • that's like very poor user experience, is this how platforms like opensea and rarible do it?
    – eth_sign
    Commented Dec 11, 2021 at 19:24
  • you are talking about erc20 (not NFT), right ? erc20 has that known negative issue
    – Majd TL
    Commented Dec 11, 2021 at 20:44

0

Your Answer

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