You can use the following steps to calculate tax:
Get the current price of the token using a Dex like Uniswap by calling the getAmountsOut function on the Uniswap router contract, pass in the amount of ETH you want to swap for token, and the addresses of the token and ETH. This gives you the expected amount of tokens you will receive for the swap, containing any slippage and fees.
do the swap on the DEX, either by calling the swapExactETHForTokens function on router contract or using MetaMask.
Calculate the tax by subtracting the actual amount of tokens received from the expected amount of tokens based on the price. lets say, if you expected to receive 100 tokens based on the price, but only received 80 tokens, the tax would be 20%.
Now to find out how many tokens you actually received when simulating a swap on Hardhat, you can use the getAmountsOut function as well. This function returns an array of token amounts, with second element being the expected amount of tokens received for the swap. This value should match the actual amount of tokens you receive after simulating the swap.
If the value of amounts returned by getAmountsOut is not equal to the amount of tokens you see in MetaMask after the swap, it is may be that the difference is due to network fees or slippage. To calculate the actual tax, you will need to use the actual amount of tokens received and compare it to the expected amount based on the price.