I am deploying a contract in which, if a wallet holds a certain combination of tokens, there is a function where they can all be burnt and then a single NFT is made in their place.
So if this is the function:
for (uint256 i = 0; i < tokenlist.length; i++){
burn(tokenlist[i]);
}
mint(msg.sender, newItemId);
I'd like to know if there is some way to ensure that tokens are only burnt if the final line (minting) goes through. i.e. - let all the burning/minting happen subject to none of the transactions failing.
My concern is that if someone submits 5 tokens to be burnt in order to mint 1, perhaps 2 tokens could be burnt, and then the remainder of transactions fail, leaving the person with 3 unburnt tokens and no new token minted.
Alternatively, if you know the total amount to be burnt, you can just check (through a require statement) that the total supply has reduced by that amount after the burn has completed instead of checking it in every iteration.
You can also check if the burn function itself has a require statement e.g. require that the balance of the account is >= the amount to be burnt. If this require statement exists then you don't need another require statement as the require statement in the burn function will throw if the balance is insufficient.
If these token contracts are deployed w/ solidity 0.8.0 and later, you don't even need this require statement. If the account only has 3 tokens and it tries to burn 5, it will underflow and thus fail automatically.