Im learning Solidity and I found an ERC721 contract that seems to have a very dangerous minting process. Am I correct in assuming that if User_a tries to mint 20 at the exact same time as User_b tries to mint 20, one of the transactions could fail if there was a price increase caused by the other user's successful transactions?
How do you combat this type of issue?
function adoptChubby(uint256 numChubbies) public payable {
require(totalSupply() < MAX_CHUBBIES, "Sale has already ended");
require(numChubbies > 0 && numChubbies <= 20, "You can adopt minimum 1, maximum 20 chubbies");
require(totalSupply().add(numChubbies) <= MAX_CHUBBIES, "Exceeds MAX_CHUBBIES");
require(msg.value >= calculatePrice().mul(numChubbies), "Ether value sent is below the price");
for (uint i = 0; i < numChubbies; i++) {
uint mintIndex = totalSupply();
_safeMint(msg.sender, mintIndex);
}
}