Contract Verification on PolygonScan

I encountered some similar issues with contract verification on Polygonscan, for a customizable ERC20 token factory that imports OpenZeppelin's ERC20. Here's a summary of what I found

I initially compiled the the contract with hardhat. In order to perform contract verification on Polygonscan, I had to flatten the import. I used the npx hardhat flatten contract.sol > output.sol command and inputted the the flattened contract, but kept getting the issue below, similar to the OP:

After numerous attempts at flattening and re-attempting verification, I realized that the entire flow would work if compiled by Remix instead of Hardhat.

So ultimately, I ended up using Remix to:

  1. recompile my token factory contract
  2. use that new ABI and bytecode instead for my token minter dapp (as the contract factory)
  3. use REMIX to flatten it
  4. complete contract verification on Polygonscan successfully

So now the verification works now because the flattened source code was generated from Remix, as was the original ABI and bytecode.

Made a video demo with my flow: https://youtu.be/l-mH7k4XUR8

My suspicion is that there could be a discrepancy between the solidity compiler version with the OpenZeppelin contract and the actual contract when doing it with hardhat.

Other things to make sure are consistent would be the solc version, optimization, license, etc.

Hope this helps!