Warning! Error encountered during contract execution [Reverted]

I m trying to Fix this issue !

Warning! Error encountered during contract execution [Reverted]

https://ropsten.etherscan.io/tx/0xc5066bc1f1072d0db77139bdee38eb84ab542027d895ed1c0a112a5d88855067

Please any one help me fix this

Hi,

Please could you provide us with your contract code and also describe what function you were trying to call in these transactions. It will be quicker for both of us if we are on the same page.

Regards
A.Malik

am following the tutorial but using Reactjs… and much completed just getting the error in buying nfts… when meta mask popup is opening it is showing transaction error : exception thrown in contract code

after confirming the transaction , item is removed from EthNFTOwners but not from the EthNFTTransfers… and also i am not getting what is EthNFTTransfers if somebody clear my concept…will be grateful for that as I didn’t know anything about blockchain before landing on this project…

console logging this error after completing the transaction

image
this is contract code…

//Contract based on https://docs.openzeppelin.com/contracts/3.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol";

contract Marketplace {
    struct ProductItem{
        uint256 id;
        address tokenAddress;
        uint256 tokenId;
        address payable seller;
        uint256 price;
        bool purchased;
    }
    ProductItem[] public products;

    mapping(address => mapping(uint => bool)) activeItems;

    event productCreated(uint256 id, uint256 tokenId, address tokenAddress, uint256 price);
    event productSold(uint256 id, address buyer, uint256 price);

    modifier OnlyItemOwner(address tokenAddress, uint256 tokenId){
        IERC721 tokenContract = IERC721(tokenAddress);
        require(tokenContract.ownerOf(tokenId) == msg.sender);
        _;
    }

    modifier HasTransferApproval(address tokenAddress, uint256 tokenId){
        IERC721 tokenContract = IERC721(tokenAddress);
        require(tokenContract.getApproved(tokenId) == address(this));
        _;
    }

    modifier ItemExists(uint256 id){
        require(id < products.length && products[id].id == id, "Could not find Item");
        _;
    }

    modifier IsForSale(uint256 id){
        require(products[id].purchased == false, "Item is already sold");
        _;
    }

    function itemAdded(uint256 tokenId, address tokenAddress, uint256 price) OnlyItemOwner(tokenAddress, tokenId) HasTransferApproval(tokenAddress, tokenId) external returns (uint256){
        require(activeItems[tokenAddress][tokenId] == false, "Item is already up for sale");
        uint256 newItemId = products.length;
        products.push(ProductItem(newItemId, tokenAddress, tokenId, payable (msg.sender), price, false));
        activeItems[tokenAddress][tokenId] = true;

        assert(products[newItemId].id == newItemId);
        emit productCreated(newItemId, tokenId, tokenAddress, price);
        return newItemId;
    }

    function buyItem(uint256 id) payable external ItemExists(id) IsForSale(id) HasTransferApproval(products[id].tokenAddress, products[id].tokenId){
        require(msg.value >= products[id].price, "Not enough Funds sent");
        require(msg.sender != products[id].seller);

        products[id].purchased = true;
        activeItems[products[id].tokenAddress][products[id].tokenId] = false;
        IERC721(products[id].tokenAddress).safeTransferFrom(products[id].seller, msg.sender, products[id].tokenId);
        products[id].seller.transfer(msg.value);

        emit productSold(id, msg.sender , products[id].price);
    }

}

and this is buy function

export const buyItems = async (tokenId,price) => {
    try {

        const web3 = await Moralis.Web3.enable();

        const currentUser = await Moralis.User.current();
        const userAddress = currentUser.get('ethAddress')

        const priceInWei = web3.utils.toWei(price,'ether')

        window.marketplaceContract = new web3.eth.Contract(marketplaceABI, marketplaceRemixAddress);
        const transfer = await window.marketplaceContract.methods.buyItem(tokenId).send({from:userAddress, value:priceInWei})

        console.log('trf',transfer)
        
    } catch (error) {
        console.log(error)
    }
}

Hi,

Most likely than not, one of your require statements are getting hit thus reverting your transaction. Since the transaction revert did not have a message attached to it, I’m guessing the require statement that is getting hit is –

Perhaps you can put a message near the require statement and see if that gets returned to confirm it –

require(msg.sender != products[id].seller, "This is a test require statement message");

To resolve this make sure to not buy the NFT item from the same address as you’re using to sell it.

Hope this helps.

Also, please make sure to test the contract locally on ganache (or remix) before you deploy on the test network. It’s easier to debug in those scenarios.

Thanks.

1 Like

okay i will try this and let you know ! about the Progress! :slight_smile: