Batching transactions
Batch/BatchAll functions
The batchTxHex
and batchAllTxHex
functions can be implemented into the transaction execution process. Both functions operate similarly and require the same inputs. The key distinction lies in the execution sequence: the "batch" variant proceeds with transactions until encountering a failure, ceasing any further actions from the batch. The first successful transactions from the batch will be kept in the blockchain state. In contrast, "batchAll" attempts all transactions. If any transaction fails, it will revert to the preceding successful block without registering any information, thereby leaving the blockchain's state unaltered. The recommended approach is typically to opt for the batchAll transaction. This is the safest way to register all the transactions from the batch, otherwise, you will need to manually handle performed and failed transactions.
Quick summary:
In case of failure within a batch transaction, the process stops where it is, and the chain records the first validated transactions.
In case of failure within a batchAll transaction, the chain reverts each validated extrinsics from the batch and the blockchain's state remains unaltered.
To get a deep understanding of how batching works on the Ternoa chain, read this article.
Similarly, batchTx
and batchAllTx
functions are available, providing transaction hashes, not in hexadecimal format. These functions mirror each other in functionality.
For instance, in scenarios involving the creation of many NFTs, it's advantageous to group multiple operations into a single transaction rather than processing each NFT individually. This method is not only cost-effective in terms of transaction fees, but it also significantly reduces the time spent on these operations.
Example: Batching a mint of NFT
We'd typically use the batchAllTxHex
function, especially when we want the process to halt and revert in the event of an error, while also receiving the result in hexadecimal format. However, it's important to note that the same code can be applied with batchTxHex, batchTx, or batchAllTx, as all these functions are compatible with the process.
It's crucial to remember that a BatchTx Extrinsic is marked as successful even if it gets interrupted. The blockchain will issue an ExtrinsicSuccess
event. A recommended practice is to monitor for a BatchCompleted
event and use a findEvent() function to ascertain whether a BatchInterrupted
event occurred during the batch. If a BatchCompleted
event is present, it indicates that the batch was not interrupted. In the absence of this event, it suggests that the batch was interrupted at some point, and one should review the event list for details.
Last updated