The transaction has been reverted to the initial state error

Hey guys, I am facing an error which is

transact to LotteryEscrowParent.callPurchaseItem errored: VM error: revert.

revert
	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.

In the following function:-

function callPurchaseItem(
         uint256 tokenId,
         address tokenAddress,
         address collectionContract,
         uint256 price
      )public{
LotteryEscrow(tokenAddress).purchaseItem(tokenId,payable(msg.sender),collectionContract, price);
      }
function purchaseItem(uint256 tokenId, address payable to, address collectionContract, uint256 price) external {
        //uint256 _totalPrice = getTotalPrice(tokenId);
        MarketItem memory item = marketItems[tokenId];
        require(!item.sold, "item already sold");
        payable(item.seller).transfer(price);
        item.sold = true;
        IERC721(item.nftContract).transferFrom(item.seller, to, tokenId);
        marketItems[tokenId].owner = payable(to);
        soldItems[collectionContract].push(tokenId);
        emit Bought(address(this), item.tokenId, price, item.seller, to);
    }

Any help! Thank you:)

The functions themselves should also be payable.

callPurchaseItem should be public payable and the other likely should be external payable

Yeah, tried that but it's still giving the exact error.

The error can be the result of a bunch of different things in your code, for example:

  • Incorrect tokenAddress in LotteryEscrow(tokenAddress).purchaseItem
  • marketItems being an array with length equal to or smaller than tokenId
  • Insufficient ETH in your contract upon executing payable(item.seller).transfer(price)
  • item.seller being a contract with no fallback or receive functions

And we have no way of telling, because a lot of the relevant code - onchain AND offchain - is missing.