java.lang.Object
Execute multiple transactions in a single consensus event. This allows for atomic execution of multiple
transactions, where they either all succeed or all fail together.
Requirements:
- All inner transactions must be frozen before being added to the batch
- All inner transactions must have a batch key set
- All inner transactions must be signed as required for each individual transaction
- The BatchTransaction must be signed by all batch keys of the inner transactions
- Certain transaction types (FreezeTransaction, BatchTransaction) are not allowed in a batch
Important notes:
- Fees are assessed for each inner transaction separately
- The maximum number of inner transactions in a batch is limited to 25
- Inner transactions cannot be scheduled transactions
Example usage:
var batchKey = PrivateKey.generateED25519();
// Create and prepare inner transaction
var transaction = new TransferTransaction()
.addHbarTransfer(sender, amount.negated())
.addHbarTransfer(receiver, amount)
.batchify(client, batchKey);
// Create and execute batch transaction
var response = new BatchTransaction()
.addInnerTransaction(transaction)
.freezeWith(client)
.sign(batchKey)
.execute(client);
-
Nested Class Summary
Nested classes/interfaces inherited from class com.hedera.hashgraph.sdk.Transaction
Transaction.SignableNodeTransactionBodyBytesNested classes/interfaces inherited from class com.hedera.hashgraph.sdk.Executable
Executable.GrpcRequest -
Field Summary
Fields inherited from class com.hedera.hashgraph.sdk.Transaction
customFeeLimits, DEFAULT_AUTO_RENEW_PERIOD, defaultMaxTransactionFee, DUMMY_ACCOUNT_ID, DUMMY_TRANSACTION_ID, frozenBodyBuilder, innerSignedTransactions, outerTransactions, publicKeys, regenerateTransactionId, signers, sigPairLists, sourceTransactionBody, transactionIdsFields inherited from class com.hedera.hashgraph.sdk.Executable
attemptedAllNodes, blockingUnaryCall, grpcDeadline, logger, maxAttempts, maxBackoff, minBackoff, nodeAccountIds, nodes, random, RST_STREAM -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty BatchTransaction.BatchTransaction(TransactionBody txBody) Constructor for internal use when recreating a transaction from a TransactionBody.Constructor for internal use when recreating a transaction from a TransactionBody. -
Method Summary
Modifier and TypeMethodDescriptionaddInnerTransaction(Transaction<?> transaction) Append a transaction to the list of transactions this BatchTransaction will execute.(package private) AtomicBatchTransactionBodybuild()Create the builder.Get the list of transaction IDs of each inner transaction of this BatchTransaction.Get the list of transactions this BatchTransaction is currently configured to execute.(package private) io.grpc.MethodDescriptor<Transaction, TransactionResponse> Called to direct the invocation of the query to the appropriate gRPC service.(package private) voidInitialize from the transaction body.(package private) voidonFreeze(TransactionBody.Builder bodyBuilder) Called inTransaction.freezeWith(Client)just before the transaction body is built.(package private) voidonScheduled(SchedulableTransactionBody.Builder scheduled) Called inTransaction.schedule()when converting transaction into a scheduled version.setInnerTransactions(List<Transaction> transactions) Set the list of transactions to be executed as part of this BatchTransaction.(package private) voidvalidateChecksums(Client client) Methods inherited from class com.hedera.hashgraph.sdk.Transaction
addSignature, addSignature, batchify, buildAllTransactions, buildTransaction, doSchedule, estimateFee, freeze, freezeWith, fromBytes, fromScheduledTransaction, generateTransactionIds, getBatchKey, getDefaultMaxTransactionFee, getExecutionState, getHighVolume, getMaxTransactionFee, getRegenerateTransactionId, getRequiredChunks, getSignableNodeBodyBytesList, getSignatures, getSignaturesAtOffset, getTransactionBodySize, getTransactionHash, getTransactionHashPerNode, getTransactionId, getTransactionIdInternal, getTransactionMemo, getTransactionSize, getTransactionValidDuration, hash, isBatchedAndNotBatchTransaction, isFrozen, keyAlreadySigned, makeRequest, mapResponse, mapResponseStatus, onExecute, onExecuteAsync, regenerateTransactionId, requireNotFrozen, requireOneNodeAccountId, schedule, setBatchKey, setHighVolume, setMaxTransactionFee, setNodeAccountIds, setRegenerateTransactionId, setTransactionId, setTransactionMemo, setTransactionValidDuration, sign, signTransaction, signWith, signWithOperator, spawnBodyBuilder, toBytes, toString, wipeTransactionListsMethods inherited from class com.hedera.hashgraph.sdk.Executable
advanceRequest, checkNodeAccountIds, execute, execute, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, getGrpcRequest, getMaxAttempts, getMaxBackoff, getMaxRetry, getMinBackoff, getNodeAccountIds, getNodeForExecute, grpcDeadline, logTransaction, mergeFromClient, setGrpcDeadline, setLogger, setMaxAttempts, setMaxBackoff, setMaxRetry, setMinBackoff, setNodesFromNodeAccountIds, setRequestListener, setResponseListener, shouldRetryExceptionally
-
Constructor Details
-
BatchTransaction
public BatchTransaction()Creates a new empty BatchTransaction. -
BatchTransaction
BatchTransaction(LinkedHashMap<TransactionId, LinkedHashMap<AccountId, throws com.google.protobuf.InvalidProtocolBufferExceptionTransaction>> txs) Constructor for internal use when recreating a transaction from a TransactionBody.- Parameters:
txs- Compound list of transaction id's list of (AccountId, Transaction) records- Throws:
com.google.protobuf.InvalidProtocolBufferException- when there is an issue with the protobuf
-
BatchTransaction
BatchTransaction(TransactionBody txBody) throws com.google.protobuf.InvalidProtocolBufferException Constructor for internal use when recreating a transaction from a TransactionBody.- Parameters:
txBody- protobuf TransactionBody- Throws:
com.google.protobuf.InvalidProtocolBufferException- when there is an issue with the protobuf
-
-
Method Details
-
setInnerTransactions
Set the list of transactions to be executed as part of this BatchTransaction.Requirements for each inner transaction:
- Must be frozen (use
Transaction.freeze()orTransaction.freezeWith(Client)) - Must have a batch key set (use
Transaction.setBatchKey(Key)} orTransaction.batchify(Client, Key)) - Must not be a blacklisted transaction type
Note: This method creates a defensive copy of the provided list.
- Parameters:
transactions- The list of transactions to be executed- Returns:
this- Throws:
NullPointerException- if transactions is nullIllegalStateException- if this transaction is frozenIllegalStateException- if any inner transaction is not frozen or missing a batch keyIllegalArgumentException- if any transaction is of a blacklisted type
- Must be frozen (use
-
addInnerTransaction
Append a transaction to the list of transactions this BatchTransaction will execute.Requirements for the inner transaction:
- Must be frozen (use
Transaction.freeze()orTransaction.freezeWith(Client)) - Must have a batch key set (use
Transaction.setBatchKey(Key)} orTransaction.batchify(Client, Key)) - Must not be a blacklisted transaction type
- Parameters:
transaction- The transaction to be added- Returns:
this- Throws:
NullPointerException- if transaction is nullIllegalStateException- if this transaction is frozenIllegalStateException- if the inner transaction is not frozen or missing a batch keyIllegalArgumentException- if the transaction is of a blacklisted type
- Must be frozen (use
-
getInnerTransactions
Get the list of transactions this BatchTransaction is currently configured to execute.Note: This returns the actual list of transactions. Modifications to this list will affect the batch transaction if it is not frozen.
- Returns:
- The list of inner transactions
-
getInnerTransactionIds
Get the list of transaction IDs of each inner transaction of this BatchTransaction.This method is particularly useful after execution to:
- Track individual transaction results
- Query receipts for specific inner transactions
- Monitor the status of each transaction in the batch
NOTE: Transaction IDs will only be meaningful after the batch transaction has been executed or the IDs have been explicitly set on the inner transactions.
- Returns:
- The list of inner transaction IDs
-
validateChecksums
- Specified by:
validateChecksumsin classTransaction<BatchTransaction>- Throws:
BadEntityIdException
-
initFromTransactionBody
void initFromTransactionBody() throws com.google.protobuf.InvalidProtocolBufferExceptionInitialize from the transaction body.- Throws:
com.google.protobuf.InvalidProtocolBufferException
-
getMethodDescriptor
io.grpc.MethodDescriptor<Transaction,TransactionResponse> getMethodDescriptor()Description copied from class:ExecutableCalled to direct the invocation of the query to the appropriate gRPC service.- Specified by:
getMethodDescriptorin classExecutable<BatchTransaction,Transaction, TransactionResponse, TransactionResponse>
-
build
AtomicBatchTransactionBody build()Create the builder.- Returns:
- the transaction builder
-
onFreeze
Description copied from class:TransactionCalled inTransaction.freezeWith(Client)just before the transaction body is built. The intent is for the derived class to assign their data variant to the transaction body.- Specified by:
onFreezein classTransaction<BatchTransaction>
-
onScheduled
Description copied from class:TransactionCalled inTransaction.schedule()when converting transaction into a scheduled version.- Specified by:
onScheduledin classTransaction<BatchTransaction>
-