'How to call Openzeppelin safeTransferFrom() function with Royalty to avoid error, ERC721: owner query for nonexistent token?

I am creating a smart contract using Openzeppelin NFT standard and code copied from Tatum, where the safeTransferFrom function looks like this,

function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata dataBytes
    ) public payable virtual override {
        uint256 index;
        uint256 value; // price 1000 matic
        uint256 percentSum;
        IERC20 token;
        (index, value) = _bytesCheck(dataBytes);

        // return error if token id is odd
        if (tokenId % 2 == 1) {
            
            //if block time is earlier than 2023 march 1 timestamp
            if (block.timestamp < 1677628800) {
                revert("You have to wait until 2023 March 1 to mint this token");
            }
          
        }

        if (_customToken[tokenId] != address(0)) {
            token = IERC20(_customToken[tokenId]);
        }
        if (_cashbackRecipients[tokenId].length > 0) {
            for (uint256 i = 0; i < _cashbackValues[tokenId].length; i++) {
                uint256 iPercent = (_cashbackValues[tokenId][i] * value) /
                    10000;
                if (iPercent >= _fixedValues[tokenId][i]) {
                    percentSum += iPercent;
                } else {
                    percentSum += _fixedValues[tokenId][i];
                }
            }
            if (_customToken[tokenId] == address(0)) {
                if (percentSum > msg.value) {
                    payable(from).transfer(msg.value);
                    revert(
                        "Value should be greater than or equal to cashback value"
                    );
                }
            } else {
                if (percentSum > token.allowance(to, address(this))) {
                    revert(
                        "Insufficient ERC20 allowance balance for paying for the asset."
                    );
                }
            }
            for (uint256 i = 0; i < _cashbackRecipients[tokenId].length; i++) {
                // transferring cashback to authors
                uint256 cbvalue = (_cashbackValues[tokenId][i] * value) / 10000;
                if (_customToken[tokenId] == address(0)) {
                    cbvalue = _cashbackCalculator(
                        cbvalue,
                        _fixedValues[tokenId][i]
                    );
                    payable(_cashbackRecipients[tokenId][i]).transfer(cbvalue);
                } else {
                    cbvalue = _cashbackCalculator(
                        cbvalue,
                        _fixedValues[tokenId][i]
                    );

                    token.transferFrom(
                        to,
                        _cashbackRecipients[tokenId][i],
                        cbvalue
                    );
                }
            }
            if (_customToken[tokenId] != address(0) && msg.value > 0) {
                payable(from).transfer(msg.value);
            }
            if (_customToken[tokenId] == address(0) && msg.value > percentSum) {
                payable(from).transfer(msg.value - percentSum);
            }
        }
        _safeTransfer(from, to, tokenId, dataBytes);
        string calldata dataString = string(dataBytes);
        _appendTokenData(tokenId, dataString);
        emit TransferWithProvenance(tokenId, to, dataString[:index], value);
    }

It pays royalty using the ERC721 standard. I am calling this function like this,

import { ethers } from "hardhat";

const contractAddressRoyalty = "0x1903344651b356ce3b755458008c0fe74f8cc1c9";

const trans = async () => {

  const CannesRoyalty = await ethers.getContractAt(
    "Cant",
    contractAddressRoyalty
  );

  const name = await CannesRoyalty.symbol();

  //   // transfer
  const safeTransferFrom = await CantRoyalty[
    "safeTransferFrom(address,address,uint256)"
  ](
    "0x888a7E4DAE1d9009694dEdf240F976EC498D7D90",
    "0x7542A9d09589B21b5a671e14254318D2016A0A0c",
    4,
    {
      from: "0x888a7E4DAE1d9009694dEdf240F976EC498D7D90",
      gasLimit: 2000000,
      gasPrice: ethers.utils.parseUnits("10", "gwei"),
      value: ethers.utils.parseEther("0"),
    }
  );
  console.log(safeTransferFrom);
  console.log(name);
};

trans();

I am getting this error, https://mumbai.polygonscan.com/tx/0xdefbb122ab45bb85c29ffda0a14b61b77564f2748ece65f87067e24b4624cfd5

Message - Fail with error 'ERC721: owner query for nonexistent token'

But the token is minted. I have also approved. I don't know if I am messing up the input values. I have tried tokenId, 4 and "4". But seems no luck.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source