NFT Marketplace - "Transaction Error. Exception thrown in contract code."

Hey everyone, I’m still having an issue with “Transaction Error. Exception thrown in contract code.” when listing a NFT to the market place. I logged the error and this is what it says after I click confirm on Metamask despite the red error.

I’ve deployed the NFT and can see it on Opensea and the Moralis Boilerplate. I’m using the marketplaceBoilerplate.sol in the boilerplate, I’ve added all the necessary cloud functions and double checked them. Contract Abi’s and Addy set in the Provider via “strings”.

I tried adding the 1155 receiver function as suggested before but I don’t believe that was my error. It must be in the NFT code here is the sol contract for the 1155. It is a Moralis contract on the Mumbai test network. Any suggestions would be appreciated. :slight_smile:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;

import "";
import "";

contract PlanetEarth is ERC1155, Ownable {
    uint256 public constant Earth = 5;

    constructor() ERC1155("{id}.json") {
        _mint(msg.sender, Earth, 1, "");


    function mint(
        address account,
        uint256 id,
        uint256 amount
    ) public onlyOwner {
        _mint(account, id, amount, "");

    function burn(
        address account,
        uint256 id,
        uint256 amount
    ) public {
        require(msg.sender == account);
        _burn(account, id, amount);

this looks like an NFT contract, are you sure you call a function on this smart contract and not in another smart contract? I don’t see a list function here, only mint and burn

There are two contracts there is a Market Contract and a the ERC1155 contract. Take a look at the Marketplace code and let me know what you think.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "";
import "";
import "";

contract marketPlaceBoilerPlate is ReentrancyGuard {
    using Counters for Counters.Counter;
    Counters.Counter private _itemIds;
    Counters.Counter private _itemsSold;
     address public owner;
     constructor() {
         owner = msg.sender;
     struct MarketItem {
         uint itemId;
         address nftContract;
         uint256 tokenId;
         address payable seller;
         address payable owner;
         uint256 price;
         bool sold;
     mapping(uint256 => MarketItem) private idToMarketItem;
     event MarketItemCreated (
        uint indexed itemId,
        address indexed nftContract,
        uint256 indexed tokenId,
        address seller,
        address owner,
        uint256 price,
        bool sold
     event MarketItemSold (
         uint indexed itemId,
         address owner
    function createMarketItem(
        address nftContract,
        uint256 tokenId,
        uint256 price
        ) public payable nonReentrant {
            require(price > 0, "Price must be greater than 0");
            uint256 itemId = _itemIds.current();
            idToMarketItem[itemId] =  MarketItem(
            IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
            emit MarketItemCreated(
    function createMarketSale(
        address nftContract,
        uint256 itemId
        ) public payable nonReentrant {
            uint price = idToMarketItem[itemId].price;
            uint tokenId = idToMarketItem[itemId].tokenId;
            bool sold = idToMarketItem[itemId].sold;
            require(msg.value == price, "Please submit the asking price in order to complete the purchase");
            require(sold != true, "This sale has already finished");
            emit MarketItemSold(

            IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId);
            idToMarketItem[itemId].owner = payable(msg.sender);
            idToMarketItem[itemId].sold = true;
    function fetchMarketItems() public view returns (MarketItem[] memory) {
        uint itemCount = _itemIds.current();
        uint unsoldItemCount = _itemIds.current() - _itemsSold.current();
        uint currentIndex = 0;

        MarketItem[] memory items = new MarketItem[](unsoldItemCount);
        for (uint i = 0; i < itemCount; i++) {
            if (idToMarketItem[i + 1].owner == address(0)) {
                uint currentId = i + 1;
                MarketItem storage currentItem = idToMarketItem[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
        return items;


/// Thanks for inspiration:

You could get exception at this line if approve was not called for that token before making this transaction

Elaborate a bit more. Is it something you think I need to add to the NFT contract or take out of the Marketplace contract?

if that it is the case, then you have to make another transaction to approve the transfer of that token for the smart contract address, without that approve that transfer from the line below will fail because current contract address is not allowed to transfer that token that belongs to msg.sender.

That makes alot of sense. It’s like when you go to buy a coin on Uniswap and they ask that you approve the ability to spend your DAI or USDC before you make the swap… I get it now. So what do you think we should do? Just take this like out for now?

I was on OpenZep and I saw that there is a function called (setApprovalForAll(address operator, bool approved)… If what you are saying is the problem then I think adding this to the NFT contract might allow the approval first.

you don’t have to add it to the smart contract, you only have to call approve before calling that function from the smart contract that adds an item to marketplace. you don’t have to change the smart contract, only to call approve separately from the application interface.

I found it in the Zep link in the top of the contract… Now I gotta figure out how to call it before it lists…

  function setApprovalForAll(address operator, bool approved) public virtual override {
        _setApprovalForAll(_msgSender(), operator, approved);

     * @dev See {IERC1155-isApprovedForAll}.
    function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[account][operator];

usually you want to call the approve function to approve only a specific NFT and not all the NFTs, but it should work with setApprovalForAll too

I didn’t see an regular Approval in there just the All… But last question how do I call this function as a single button before the actual listing? Here is the code

import React, { useState } from "react";
import { useMoralis } from "react-moralis";
import { Card, Image, Tooltip, Modal, Input, Spin } from "antd";
import { useNFTBalance } from "hooks/useNFTBalance";
import { FileSearchOutlined, ShoppingCartOutlined } from "@ant-design/icons";
import { useMoralisDapp } from "providers/MoralisDappProvider/MoralisDappProvider";
import { getExplorer } from "helpers/networks";
import { useWeb3ExecuteFunction } from "react-moralis";
const { Meta } = Card;

const styles = {
  NFTs: {
    display: "flex",
    flexWrap: "wrap",
    WebkitBoxPack: "start",
    justifyContent: "flex-start",
    margin: "0 auto",
    maxWidth: "1000px",
    gap: "10px",

function NFTBalance() {
  const { NFTBalance } = useNFTBalance();
  const { chainId, marketAddress, contractABI } = useMoralisDapp();
  const { Moralis } = useMoralis();
  const [visible, setVisibility] = useState(false);
  const [nftToSend, setNftToSend] = useState(null);
  const [price, setPrice] = useState(1);
  const [loading, setLoading] = useState(false);
  const contractProcessor = useWeb3ExecuteFunction();
  const contractABIJson = JSON.parse(contractABI);
  const listItemFunction = "createMarketItem";
  const ItemImage = Moralis.Object.extend("ItemImages"); 

    async function list(nft, listPrice) {
      const p = listPrice * ("1e" + 18);
      const ops = {
      contractAddress: marketAddress,
      functionName: listItemFunction,
      abi: contractABIJson,
      params : {
        nftContract: nft.token_address,
        tokenId: nft.token_id,
        price: String(p)
    await contractProcessor.fetch({
      params: ops,
      onSuccess: () => {
      onError: (error) => {

  const handleSellClick = (nft) => {
  function succList() {
    let secondsToGo = 5;
    const modal = Modal.success({
      title: 'Success!',
      content: `Your NFT was listed on the marketplace`,
    setTimeout(() => {
    }, secondsToGo * 1000);     

  function failList() {
    let secondsToGo = 5;
    const modal = Modal.error({
      title: 'Error!',
      content: `There was a problem listing your NFT`,
    setTimeout(() => {
    }, secondsToGo * 1000);     

  function addItemImage(){
    const itemImage = new ItemImage();

    itemImage.set("image", nftToSend.image);
    itemImage.set("nftContract", nftToSend.token_address);
    itemImage.set("tokenId", nftToSend.token_id);

  return (
      <div style={styles.NFTs}>
        {NFTBalance &&
, index) => (
                <Tooltip title="View On Blockexplorer">
                    onClick={() =>
            `${getExplorer(chainId)}address/${nft.token_address}`, "_blank")
                <Tooltip title="List NFT for sale">
                  <ShoppingCartOutlined onClick={() => handleSellClick(nft)} />
              style={{ width: 240, border: "2px solid #e7eaf3" }}
                  src={nft?.image || "error"}
                  style={{ height: "240px" }}
              <Meta title={} description={nft.contract_type} />
        title={`List ${nftToSend?.name} #${nftToSend?.token_id} For Sale`}
        onCancel={() => setVisibility(false)}
        onOk={() => list(nftToSend, price)}
        <Spin spinning={loading}>
        <img src={`${nftToSend?.image}`} style={{width:"250px", margin:"auto", borderRadius:"10px", marginBottom:"15px"}} />
        <Input autoFocus placeholder="Listing Price in MATIC" onChange={(e) => setPrice(} />

export default NFTBalance;

this would be the approve function, you call it how you call the marketplace function, but with different parameters

Now that you show me this I realized I’m using 1155 NFTs and the Marketplace only has a 721 contract in the top… I’m assuming this is the problem now lol

Hey, I have been searching the forum about a similar problem. Is there a quick way to get the code in this NFT Marketplace to work with ERC1155 tokens?

@DevWill were you able to solve the issue? I am really struggling with this error