Consider a batch operation that sends 3 transactions in sequence. Each transaction adds 10 to a shared counter starting at 0. What is the final counter value after all transactions succeed?
let counter = 0; const batch = [ () => { counter += 10; return counter; }, () => { counter += 10; return counter; }, () => { counter += 10; return counter; } ]; for (const tx of batch) { tx(); } console.log(counter);
Each transaction adds 10 to the counter. Think about how many times this happens.
The counter starts at 0. Each of the 3 transactions adds 10, so after all run, counter is 30.
Choose the correct description of batch operations in blockchain systems.
Think about why batching transactions is useful for cost and consistency.
Batch operations group multiple transactions so they execute together atomically, saving gas and ensuring either all succeed or all fail.
Given the following Solidity batch transfer function, why does only the first transfer succeed?
function batchTransfer(address[] memory recipients, uint256[] memory amounts) public {
require(recipients.length == amounts.length, "Mismatched arrays");
for (uint i = 0; i < recipients.length; i++) {
transfer(recipients[i], amounts[i]);
revert("Batch failed");
}
}Consider what happens when revert is called inside a loop.
The revert statement inside the loop causes the transaction to roll back immediately after the first transfer, so no subsequent transfers occur.
Choose the correct Solidity function syntax for a batch token transfer.
Remember how to declare array parameters in Solidity functions.
In Solidity, dynamic arrays passed to functions must specify the data location as memory. Option C correctly declares both arrays with memory.
Consider a batch transfer function that attempts to send tokens to multiple recipients. If a transfer fails, it skips that recipient and continues. Given the code below, how many transfers succeed?
uint successCount = 0; for (uint i = 0; i < recipients.length; i++) { try this.transfer(recipients[i], amounts[i]) { successCount++; } catch { continue; } } return successCount;
Assume the second transfer fails, others succeed.
The try-catch allows skipping failed transfers. If the second transfer fails and others succeed, total successful transfers are 2.