Error: VM Exception while processing transaction: revert -- Reason given: Panic: Arithmetic overflow

Hello, can someone please point me in the right direction? I have spent almost two weeks trying to resolve the issue.

I'm having Panic: Arithmetic overflow. everytime I call this function from my test script

function buyNft(uint256 tokenId) public payable nonReentrant {
       NftItem storage nftItem = _idToNftItem[tokenId];
       address owner = ERC721.ownerOf(tokenId);
       address creator = nftItem.creator;
       uint256 price = nftItem.price;
       uint256 royaltyFeePercentage = nftItem.royaltyFeePercentage;

       // Ensure the buyer is not the current owner
       require(msg.sender != owner, "You already own this NFT");

       // Ensure the buyer sent the correct amount
       require(msg.value == price, "Please submit the asking price");

       // Ensure the NFT is listed for sale
       require(nftItem.isListed, "Item is not listed for sale");

       // Mark the item as not listed anymore after the purchase
       nftItem.isListed = false;

       // Calculate royalty and seller amount
       uint256 royaltyAmount = (price * royaltyFeePercentage) / 100;
       uint256 sellerAmount = price - royaltyAmount;

       // Calculate the admin fee using the calculateAdminFee function
       uint256 adminFee = calculateAdminFee(price);

       // Transfer the admin fee to the admin address
       payable(adminAddress).transfer(adminFee);

       // Transfer the royalty fee to the creator
       payable(creator).transfer(royaltyAmount);

       // Transfer the remaining amount to the seller (previous owner)
       payable(owner).transfer(sellerAmount);

       // Safely transfer the NFT to the buyer
       _safeTransfer(owner, msg.sender, tokenId, "");

       // Emit the transfer event
       emit NftTransferred(tokenId, owner, msg.sender);

       // Emit an event indicating the NFT has been sold
       emit NftSold(tokenId, price, msg.sender, owner, creator);
   }

Test script:

const NftMarket = artifacts.require("NftMarket");
const Web3 = require("web3");

contract("NftMarket", (accounts) => {
  let nftMarket;
  const adminAddress = accounts[0];
  const user1 = accounts[1];
  const user2 = accounts[2];
  const user3 = accounts[3];
  let tokenId;

  before(async () => {
    nftMarket = await NftMarket.new(adminAddress);
  });

  describe("Buying NFT", () => {
    it("should allow a user to buy an NFT", async () => {
      const price = Web3.utils.toWei("2", "ether");

      // User2 buys the NFT
      const result = await nftMarket.buyNft(tokenId, { from: user2, value: price });

      const nftItem = await nftMarket.getNftItem(tokenId);

      assert.equal(nftItem.isListed, false, "NFT should be removed from sale after purchase");
      assert.equal(nftItem.owner, user2, "Owner should be updated to the buyer");

      const transferEvent = result.logs.find(log => log.event === "NftTransferred");
      assert.equal(transferEvent.args.from, user1, "The previous owner should be the seller");
      assert.equal(transferEvent.args.to, user2, "The new owner should be the buyer");
    });
  });
  
});

I'm using Ganache with truffle and @openzeppelin/contracts@5.1.0

Hi, welcome to the community! :wave:

Need more error messages, and I do not have the full code, so I can not have a test on my local environment to check what is wrong.