Facing an execution error

Hey everyone, I am facing an error called:

"Transaction submission failed: Error: missing revert data (action="estimateGas", data=null, reason=null, transaction={ "data": "0x327249000000000000000000000000005d7e1d2cbcc0e13ebd0453152be36636d23f90500000000000000000000000000000000000000000000000000000000000000000", "from": "0x1a5516BeDB0CaAB297c8cecaBdFe96325469EF2d", "to": "0x8ffaee855F945aD8B7e9064F8A6A81c205DE24c4" }, invocation=null, revert=null, code=CALL_EXCEPTION, version=6.4.0)
    at makeError (webpack-internal:///(:3000/app-pages-browser)/./node_modules/ethers/lib.commonjs/utils/errors.js:121:21)

When calling purchaseItem function to buy an NFT in my NFT Marketplace.
Could you please check the below code?

address payable public feeAccount =  payable(address(this)); // Sendig 2% fee to contract itself
uint256 public feePercent = 2;
function purchaseItem(address nftContract, uint256 tokenId) external payable nonReentrant{
        uint256 _totalPrice = getTotalPrice(tokenId);
        MarketItem storage item = marketItems[tokenId];
           require(
            msg.value >= _totalPrice,
            "not enough matic to cover item price and market fee"
        );
        require(!item.sold, "item already sold");
        item.seller.transfer(item.price);
        feeAccount.transfer(_totalPrice - item.price);
        item.sold = true; 
        IERC721(nftContract).transferFrom(address(this), msg.sender, item.tokenId);
        allSoldItems.push(tokenId);
        emit Bought(item.itemId, nftContract, item.tokenId, item.price, item.seller, 
        msg.sender);
    }

Frontend code:

async function purchaseItem(address, tokenID) {
  const provider = new ethers.BrowserProvider(window.ethereum);
  const signer = await provider.getSigner();
  const MarketpaceContract = new ethers.Contract(
    MarketplaceContractAddress,
    MarketplaceContractABI,
    signer
  );
  try {
    const totalPrice = await MarketpaceContract.getTotalPrice(tokenID);
    console.log(tokenID, address, totalPrice, "callPurchaseArguments");
    console.log(totalPrice,"value");
    const purchaseItemTx = await MarketpaceContract.purchaseItem(address, tokenID, {
      value: totalPrice,
    });

    const txReceipt = await purchaseItemTx.wait();
    console.log(txReceipt, "txReceipt");
    if (txReceipt && txReceipt.status === 1) {
      console.log("NFT purchased");
    } else {
      console.log("Transaction failed or was dropped");
    }
  } catch (error) {
    console.error("Transaction submission failed:", error);
    console.log("Error code:", error.code);
    console.log("Error data:", error.data);
    if (error.transaction) {
      console.log("Transaction data:", error.transaction);
    }
  }
}

I have attached a snapshot of the console value as well.

Thank you:)

Please provide in PLAINTEXT:

  • The chain/network (e.g., ethereum/mainnet)
  • The address of your contract on that chain/network
  • The input values that you are passing to the contract function
  • The hash of your (failed) transaction
  1. polygon
  2. Contract Address : 0x8ffaee855F945aD8B7e9064F8A6A81c205DE24c4
  3. Argument values : 0 0x5d7e1D2CBCc0e13EBD0453152Be36636D23f9050 1020000000000000n
  4. data :
    "0x327249000000000000000000000000005d7e1d2cbcc0e13ebd0453152be36636d23f90500000000000000000000000000000000000000000000000000000000000000000"

That's a chain, which has several different networks (e.g., mumbai is a testing network).
Please specify the network, or by the least provide a link to your contract on that network.
And again - please specify the transaction hash or a link it (on the same network of course).

Oh yeah, I am sorry about that, I am using Mumbai testnet.

I got an error before the transaction got executed so transaction hash is not available in the details of the error.

Before executing this:

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

You need to execute this:

IERC721(nftContract).approve(address(this), item.tokenId);

Note that I've actually explained this to you in an answer to one of your previous questions.

Yep, I tried that as well but facing same error

I tried in Remix and got this error:

execution reverted
	The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

I have provided the total price in Wei value during the function call.

You can run the whole thing under a testing infrastructure like Truffle or HardHat or Forge, which will allow you to investigate the problem in an orderly fashion or manner, rather than guessing what might be causing it.

BTW, if you verify and publish your contract source code on mumbai.polygonscan, then you might actually get the exact error-message (for example, "item already sold"), indicating exactly what the problem is.

Yep, It's solved now.

1 Like

Consider explaining what the problem was and how you were able to solve it, so as to make this information useful for other readers.

Yep, sure.
I solved the execution reverted error by using call instead of transfer here. Let me share the function code.

 function purchaseItem(address nftContract, uint256 tokenId) external payable nonReentrant {
    uint256 _totalPrice = getTotalPrice(tokenId);
    MarketItem storage item = marketItems[tokenId];

    require(msg.value >= _totalPrice, "not enough ether to cover item price and market fee");
    require(!item.sold, "item already sold");

    // Transfer funds to the seller
    (bool successSeller, ) = item.seller.call{value: item.price}("");
    require(successSeller, "Transfer to seller failed");

    // Transfer funds to the fee account
    (bool successFee, ) = feeAccount.call{value: _totalPrice - item.price}("");
    require(successFee, "Transfer to fee account failed");

    item.sold = true; 

    // Use 'call' to transfer the NFT
    (bool successTransfer, ) = address(IERC721(nftContract)).call(
        abi.encodeWithSignature("transferFrom(address,address,uint256)", address(this), msg.sender, tokenId)
    );
    require(successTransfer, "NFT transfer failed");

    marketItems[tokenId].owner = payable(msg.sender);
    allSoldItems.push(tokenId);
    emit Bought(item.itemId, nftContract, item.tokenId, item.price, item.seller, msg.sender);
}

Thank you!