Facing issue related to sold Items

Hello everyone,
I am facing one issue with buying an NFT, Let's say the first NFT Collection's address is: "0xea6a5FAdCE6053bcFF973D6Cc0822d6485AD63e1"
And I have created 2 tokens here.
Now 2nd NFT Collection's address is: "0xac8E150204C6838bD67E7F43aa99ecAbdE29bD1d"
Here I have minted 5 NFTs so let's say now I bought 1st token from the first NFT Collection and it was successfully bought after that I tried to buy 1st token from the 2nd NFT Collection but I am facing an error called "Item is sold" However I have bought 1st token from the 1st collection not from the 2nd so what kind of behavior is this? Can anyone please explain and help me to resolve this issue?
You can check my contract code from here:

 function bulkMintERC721(
    address tokenAddress,
    uint256 start,
    uint256 end
) public {
    uint256 count = 0;
    for (uint256 i = start; i < end; i++) {
        uint256 tokenId = NFT(tokenAddress).safeMint(msg.sender);
        contractTokenIds[tokenAddress].push(tokenId);
        collectionsOfTokenId[tokenAddress] = tokenId;
        count++;
    }
    getNFTCount = count;
}
 function createMarketItem(
    address nftContractAddress,
    uint256 start,
    uint256 end,
    uint256 price
) public nonReentrant {
    for (uint256 i = start; i < end; i++) {
        uint256 tokenId = contractTokenIds[nftContractAddress][i];
        uint256 itemId = _ItemIdsCounter;
        marketItems[itemId] = MarketItem(
            itemId,
            nftContractAddress,
            tokenId,
            payable(msg.sender),
            payable(address(0)),
            price,
            false
        );
        _ItemIdsCounter++;

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

        emit MarketItemCreated(
            itemId,
            nftContractAddress,
            tokenId,
            msg.sender,
            address(0),
            price
        );
    }
}

  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:)

Exactly the behavior which you have implemented in your contract.

It sounds like you need to define one item per collection (instead of one item for all collections).

1 Like

Okay, let me give it a try!

1 Like

Thank you so much @barakman

1 Like