Best practices for capping token supplies

I wonder if I miss something fundamental because this seems like a very basic feature, yet I find very little about it when searching:

As the topic indicates, I wonder how to best limit the number of tokens that can be minted. I could for example do something like this (assuming I wanted to cap the supply at 3 for every token in the contract), but I suspect there are better ways.

 function mint(address account, uint256 id, uint256 amount, bytes memory data)
        require(totalSupply(id) < 3, "No more supply");
        _mint(account, id, amount, data);

Anyone having some advice?

hey @thisisme
if you have correctly implemented the totalSupply() function then it looks fine

Thanks for your reply @FreezyEx

totalSupply is from @openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol in this case so not implemented by me.

And I would also guess this is fine but at the same time, I have a feeling there is a better way. For example: there seems to be issues estimating the gas consumption (at least using etherscan) when supply dries up...

You might be seeing an error about estimation but it might mean that the underlying function reverts. This could be because you hit the supply cap?

The function that you shared looks good to me.