Class Transaction<T extends Transaction<T>>

Type Parameters:
T - The type of the transaction. Used to enable chaining.
Direct Known Subclasses:
AbstractTokenTransferTransaction, AccountAllowanceAdjustTransaction, AccountAllowanceApproveTransaction, AccountAllowanceDeleteTransaction, AccountCreateTransaction, AccountDeleteTransaction, AccountUpdateTransaction, BatchTransaction, ChunkedTransaction, ContractCreateTransaction, ContractDeleteTransaction, ContractExecuteTransaction, ContractUpdateTransaction, EthereumTransaction, FileCreateTransaction, FileDeleteTransaction, FileUpdateTransaction, FreezeTransaction, HookStoreTransaction, LiveHashAddTransaction, LiveHashDeleteTransaction, NodeCreateTransaction, NodeDeleteTransaction, NodeUpdateTransaction, PendingAirdropLogic, PrngTransaction, RegisteredNodeCreateTransaction, RegisteredNodeDeleteTransaction, RegisteredNodeUpdateTransaction, ScheduleCreateTransaction, ScheduleDeleteTransaction, ScheduleSignTransaction, SystemDeleteTransaction, SystemUndeleteTransaction, TokenAssociateTransaction, TokenBurnTransaction, TokenCreateTransaction, TokenDeleteTransaction, TokenDissociateTransaction, TokenFeeScheduleUpdateTransaction, TokenFreezeTransaction, TokenGrantKycTransaction, TokenMintTransaction, TokenPauseTransaction, TokenRejectTransaction, TokenRevokeKycTransaction, TokenUnfreezeTransaction, TokenUnpauseTransaction, TokenUpdateNftsTransaction, TokenUpdateTransaction, TokenWipeTransaction, TopicCreateTransaction, TopicDeleteTransaction, TopicUpdateTransaction

public abstract class Transaction<T extends Transaction<T>> extends Executable<T,Transaction,TransactionResponse,TransactionResponse>
Base class for all transactions that may be built and submitted to Hedera.
  • Field Details

    • DEFAULT_AUTO_RENEW_PERIOD

      static final Duration DEFAULT_AUTO_RENEW_PERIOD
      Default auto renew duration for accounts, contracts, topics, and files (entities)
    • DUMMY_ACCOUNT_ID

      protected static final AccountId DUMMY_ACCOUNT_ID
      Dummy account ID used to assist in deserializing incomplete Transactions.
    • DUMMY_TRANSACTION_ID

      protected static final TransactionId DUMMY_TRANSACTION_ID
      Dummy transaction ID used to assist in deserializing incomplete Transactions.
    • sourceTransactionBody

      protected final TransactionBody sourceTransactionBody
      Transaction constructors end their work by setting sourceTransactionBody. The expectation is that the Transaction subclass constructor will pick up where the Transaction superclass constructor left off, and will unpack the data in the transaction body.
    • frozenBodyBuilder

      @Nullable protected TransactionBody.Builder frozenBodyBuilder
      The builder that gets re-used to build each outer transaction. freezeWith() will create the frozenBodyBuilder. The presence of frozenBodyBuilder indicates that this transaction is frozen.
    • outerTransactions

      protected List<Transaction> outerTransactions
      An SDK [Transaction] is composed of multiple, raw protobuf transactions. These should be functionally identical, except pointing to different nodes. When retrying a transaction after a network error or retry-able status response, we try a different transaction and thus a different node.
    • innerSignedTransactions

      protected List<SignedTransaction.Builder> innerSignedTransactions
      An SDK [Transaction] is composed of multiple, raw protobuf transactions. These should be functionally identical, except pointing to different nodes. When retrying a transaction after a network error or retry-able status response, we try a different transaction and thus a different node.
    • sigPairLists

      protected List<SignatureMap.Builder> sigPairLists
      A set of signatures corresponding to every unique public key used to sign the transaction.
    • transactionIds

      protected LockableList<TransactionId> transactionIds
      List of IDs for the transaction based on the operator because the transaction ID includes the operator's account
    • publicKeys

      protected List<PublicKey> publicKeys
      publicKeys and signers are parallel arrays. If the signer associated with a public key is null, that means that the private key associated with that public key has already contributed a signature to sigPairListBuilders, but the signer is not available (likely because this came from fromBytes())
    • signers

      protected List<Function<byte[],byte[]>> signers
      publicKeys and signers are parallel arrays. If the signer associated with a public key is null, that means that the private key associated with that public key has already contributed a signature to sigPairListBuilders, but the signer is not available (likely because this came from fromBytes())
    • defaultMaxTransactionFee

      protected Hbar defaultMaxTransactionFee
      The maximum transaction fee the client is willing to pay
    • regenerateTransactionId

      protected Boolean regenerateTransactionId
      Should the transaction id be regenerated
    • customFeeLimits

      List<CustomFeeLimit> customFeeLimits
  • Constructor Details

    • Transaction

      Transaction()
      Constructor.
    • Transaction

      Transaction(TransactionBody txBody)
      Constructor.
      Parameters:
      txBody - protobuf TransactionBody
    • Transaction

      Transaction(LinkedHashMap<TransactionId,LinkedHashMap<AccountId,Transaction>> txs) throws com.google.protobuf.InvalidProtocolBufferException
      Constructor.
      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
  • Method Details

    • fromBytes

      public static Transaction<?> fromBytes(byte[] bytes) throws com.google.protobuf.InvalidProtocolBufferException
      Create the correct transaction from a byte array.
      Parameters:
      bytes - the byte array
      Returns:
      the new transaction
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - when there is an issue with the protobuf
    • fromScheduledTransaction

      public static Transaction<?> fromScheduledTransaction(SchedulableTransactionBody scheduled)
      Create the correct transaction from a scheduled transaction.
      Parameters:
      scheduled - the scheduled transaction
      Returns:
      the new transaction
    • hash

      static byte[] hash(byte[] bytes)
      Generate a hash from a byte array.
      Parameters:
      bytes - the byte array
      Returns:
      the hash
    • doSchedule

      protected ScheduleCreateTransaction doSchedule(TransactionBody.Builder bodyBuilder)
      Converts transaction into a scheduled version
      Parameters:
      bodyBuilder - the transaction's body builder
      Returns:
      the scheduled transaction
    • isBatchedAndNotBatchTransaction

      protected boolean isBatchedAndNotBatchTransaction()
      Overrides:
      isBatchedAndNotBatchTransaction in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • schedule

      public ScheduleCreateTransaction schedule()
      Extract the scheduled transaction.
      Returns:
      the scheduled transaction
    • setNodeAccountIds

      public final T setNodeAccountIds(List<AccountId> nodeAccountIds)
      Set the account IDs of the nodes that this transaction will be submitted to.

      Providing an explicit node account ID interferes with client-side load balancing of the network. By default, the SDK will pre-generate a transaction for 1/3 of the nodes on the network. If a node is down, busy, or otherwise reports a fatal error, the SDK will try again with a different node.

      Overrides:
      setNodeAccountIds in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
      Parameters:
      nodeAccountIds - The list of node AccountIds to be set
      Returns:
      this
    • getTransactionValidDuration

      @Nullable public final Duration getTransactionValidDuration()
      Extract the valid transaction duration.
      Returns:
      the transaction valid duration
    • setTransactionValidDuration

      public final T setTransactionValidDuration(Duration validDuration)
      Sets the duration that this transaction is valid for.

      This is defaulted by the SDK to 120 seconds (or two minutes).

      Parameters:
      validDuration - The duration to be set
      Returns:
      this
    • getMaxTransactionFee

      @Nullable public final Hbar getMaxTransactionFee()
      Extract the maximum transaction fee.
      Returns:
      the maximum transaction fee
    • setMaxTransactionFee

      public final T setMaxTransactionFee(Hbar maxTransactionFee)
      Set the maximum transaction fee the operator (paying account) is willing to pay.
      Parameters:
      maxTransactionFee - the maximum transaction fee, in tinybars.
      Returns:
      this
    • getDefaultMaxTransactionFee

      public final Hbar getDefaultMaxTransactionFee()
      Extract the default maximum transaction fee.
      Returns:
      the default maximum transaction fee
    • getTransactionMemo

      public final String getTransactionMemo()
      Extract the memo for the transaction.
      Returns:
      the memo for the transaction
    • setTransactionMemo

      public final T setTransactionMemo(String memo)
      Set a note or description that should be recorded in the transaction record (maximum length of 100 characters).
      Parameters:
      memo - any notes or descriptions for this transaction.
      Returns:
      this
    • getHighVolume

      public final boolean getHighVolume()
      Extract the high-volume flag.
      Returns:
      true if high-volume throttles are enabled, false otherwise
    • setHighVolume

      public final T setHighVolume(boolean highVolume)
      If set to true, this transaction uses high-volume throttles and pricing for entity creation. It only affects supported transaction types; otherwise, it is ignored.
      Parameters:
      highVolume - true to enable high-volume throttles, false otherwise
      Returns:
      this
    • batchify

      public final T batchify(Client client, Key batchKey)
      batchify method is used to mark a transaction as part of a batch transaction or make it so-called inner transaction. The Transaction will be frozen and signed by the operator of the client.
      Parameters:
      client - sdk client
      batchKey - batch key
      Returns:
      this
    • setBatchKey

      public final T setBatchKey(Key batchKey)
      Set the key that will sign the batch of which this Transaction is a part of.
    • getBatchKey

      public Key getBatchKey()
      Get the key that will sign the batch of which this Transaction is a part of.
    • toBytes

      public byte[] toBytes()
      Extract a byte array representation.
      Returns:
      the byte array representation
    • getTransactionHash

      public byte[] getTransactionHash()
      Extract a byte array of the transaction hash.
      Returns:
      the transaction hash
    • getTransactionHashPerNode

      public Map<AccountId,byte[]> getTransactionHashPerNode()
      Extract the list of account id and hash records.
      Returns:
      the list of account id and hash records
    • getTransactionIdInternal

      final TransactionId getTransactionIdInternal()
      Specified by:
      getTransactionIdInternal in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • getTransactionId

      public final TransactionId getTransactionId()
      Extract the transaction id.
      Returns:
      the transaction id
    • setTransactionId

      public final T setTransactionId(TransactionId transactionId)
      Set the ID for this transaction.

      The transaction ID includes the operator's account ( the account paying the transaction fee). If two transactions have the same transaction ID, they won't both have an effect. One will complete normally and the other will fail with a duplicate transaction status.

      Normally, you should not use this method. Just before a transaction is executed, a transaction ID will be generated from the operator on the client.

      Parameters:
      transactionId - The TransactionId to be set
      Returns:
      this
      See Also:
    • getRegenerateTransactionId

      public final Boolean getRegenerateTransactionId()
      Should the transaction id be regenerated.
      Returns:
      should the transaction id be regenerated
    • setRegenerateTransactionId

      public final T setRegenerateTransactionId(boolean regenerateTransactionId)
      Regenerate the transaction id.
      Parameters:
      regenerateTransactionId - should the transaction id be regenerated
      Returns:
      this
    • sign

      public final T sign(PrivateKey privateKey)
      Sign the transaction.
      Parameters:
      privateKey - the private key
      Returns:
      the signed transaction
    • signWith

      public T signWith(PublicKey publicKey, UnaryOperator<byte[]> transactionSigner)
      Sign the transaction.
      Parameters:
      publicKey - the public key
      transactionSigner - the key list
      Returns:
      this
    • signWithOperator

      public T signWithOperator(Client client)
      Sign the transaction with the configured client.
      Parameters:
      client - the configured client
      Returns:
      the signed transaction
    • keyAlreadySigned

      protected boolean keyAlreadySigned(PublicKey key)
      Checks if a public key is already added to the transaction
      Parameters:
      key - the public key
      Returns:
      if the public key is already added
    • addSignature

      public T addSignature(PublicKey publicKey, byte[] signature)
      Add a signature to the transaction.
      Parameters:
      publicKey - the public key
      signature - the signature
      Returns:
      this
    • getSignaturesAtOffset

      protected Map<AccountId,Map<PublicKey,byte[]>> getSignaturesAtOffset(int offset)
    • getSignatures

      public Map<AccountId,Map<PublicKey,byte[]>> getSignatures()
      Extract list of account id and public keys.
      Returns:
      the list of account id and public keys
    • isFrozen

      protected boolean isFrozen()
      Check if transaction is frozen.
      Returns:
      is the transaction frozen
    • requireNotFrozen

      protected void requireNotFrozen()
      Throw an exception if the transaction is frozen.
    • requireOneNodeAccountId

      protected void requireOneNodeAccountId()
      Throw an exception if there is not exactly one node id set.
    • spawnBodyBuilder

      protected TransactionBody.Builder spawnBodyBuilder(@Nullable Client client)
    • freeze

      public T freeze()
      Freeze this transaction from further modification to prepare for signing or serialization.
      Returns:
      this
    • freezeWith

      public T freezeWith(@Nullable Client client)
      Freeze this transaction from further modification to prepare for signing or serialization.

      Will use the `Client`, if available, to generate a default Transaction ID and select 1/3 nodes to prepare this transaction for.

      Parameters:
      client - the configured client
      Returns:
      this
    • getRequiredChunks

      int getRequiredChunks()
      There must be at least one chunk.
      Returns:
      there is 1 required chunk
    • generateTransactionIds

      void generateTransactionIds(TransactionId initialTransactionId, int count)
      Generate transaction id's.
      Parameters:
      initialTransactionId - the initial transaction id
      count - the number of id's to generate.
    • wipeTransactionLists

      void wipeTransactionLists(int requiredChunks)
      Wipe / reset the transaction list.
      Parameters:
      requiredChunks - the number of required chunks
    • buildAllTransactions

      void buildAllTransactions()
      Build all the transactions.
    • buildTransaction

      void buildTransaction(int index)
      Will build the specific transaction at index This function is only ever called after the transaction is frozen.
      Parameters:
      index - the index of the transaction to be built
    • signTransaction

      void signTransaction(int index)
      Will sign the specific transaction at index This function is only ever called after the transaction is frozen.
      Parameters:
      index - the index of the transaction to sign
    • onFreeze

      abstract void onFreeze(TransactionBody.Builder bodyBuilder)
      Called in 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.
    • onScheduled

      abstract void onScheduled(SchedulableTransactionBody.Builder scheduled)
      Called in schedule() when converting transaction into a scheduled version.
    • makeRequest

      final Transaction makeRequest()
      Specified by:
      makeRequest in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • mapResponse

      TransactionResponse mapResponse(TransactionResponse transactionResponse, AccountId nodeId, Transaction request)
      Description copied from class: Executable
      Called after receiving the query response from Hedera. The derived class should map into its output type.
      Specified by:
      mapResponse in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • mapResponseStatus

      final Status mapResponseStatus(TransactionResponse transactionResponse)
      Specified by:
      mapResponseStatus in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • validateChecksums

      abstract void validateChecksums(Client client) throws BadEntityIdException
      Throws:
      BadEntityIdException
    • onExecute

      void onExecute(Client client)
      Prepare the transactions to be executed.
      Specified by:
      onExecute in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
      Parameters:
      client - the configured client
    • onExecuteAsync

      CompletableFuture<Void> onExecuteAsync(Client client)
      Specified by:
      onExecuteAsync in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • getExecutionState

      ExecutionState getExecutionState(Status status, TransactionResponse response)
      Description copied from class: Executable
      Default implementation, may be overridden in subclasses (especially for query case). Called just after receiving the query response from Hedera. By default it triggers a retry when the pre-check status is BUSY.
      Overrides:
      getExecutionState in class Executable<T extends Transaction<T>,Transaction,TransactionResponse,TransactionResponse>
    • regenerateTransactionId

      Transaction regenerateTransactionId(Client client)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • estimateFee

      public FeeEstimateQuery estimateFee()
      Create a FeeEstimateQuery pre-populated with this transaction.

      This is a convenience method equivalent to:

      
       new FeeEstimateQuery().setTransaction(transaction)
       
      Returns:
      a new FeeEstimateQuery with this transaction set
    • getTransactionSize

      public int getTransactionSize()
      This method retrieves the size of the transaction
      Returns:
    • getTransactionBodySize

      public int getTransactionBodySize()
      This method retrieves the transaction body size
      Returns:
    • getSignableNodeBodyBytesList

      public List<Transaction.SignableNodeTransactionBodyBytes> getSignableNodeBodyBytesList()
      Returns a list of SignableNodeTransactionBodyBytes objects for each signed transaction in the transaction list. The NodeID represents the node that this transaction is signed for. The TransactionID is useful for signing chunked transactions like FileAppendTransaction, since they can have multiple transaction ids.
      Returns:
      List of SignableNodeTransactionBodyBytes
      Throws:
      RuntimeException - if transaction is not frozen or protobuf parsing fails
    • addSignature

      public T addSignature(PublicKey publicKey, byte[] signature, TransactionId transactionID, AccountId nodeID)
      Adds a signature to the transaction for a specific transaction id and node id. This is useful for signing chunked transactions like FileAppendTransaction, since they can have multiple transaction ids.
      Parameters:
      publicKey - The public key to add signature for
      signature - The signature bytes
      transactionID - The specific transaction ID to match
      nodeID - The specific node ID to match
      Returns:
      The child transaction (this)
      Throws:
      RuntimeException - if unmarshaling fails or invalid signed transaction