Make all transactions fail unless they all pass

Hi all!

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.

Hope this makes sense!

Is there any way to ensure this doesn't happen?

Thank you

Hi, welcome! :wave:

Is there a returning value for your function burn()? If has, maybe you can add a requirement, such as:

for (uint256 i = 0; i < tokenlist.length; i++){
    require(burn(tokenlist[i]),"Fail to burn");
}
mint(msg.sender, newItemId);

Or you can check the balance, if it burns successfully, its balance should decrease 1.

1 Like

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.

1 Like