Class Query<O,T extends Query<O,T>>

java.lang.Object
com.hedera.hashgraph.sdk.Executable<T,Query,Response,O>
com.hedera.hashgraph.sdk.Query<O,T>
Type Parameters:
O - The output type of the query.
T - The type of the query itself. Used to enable chaining.
Direct Known Subclasses:
AccountBalanceQuery, AccountInfoQuery, AccountRecordsQuery, ContractByteCodeQuery, ContractCallQuery, ContractInfoQuery, FileContentsQuery, FileInfoQuery, LiveHashQuery, NetworkVersionInfoQuery, ScheduleInfoQuery, TokenInfoQuery, TokenNftInfoQuery, TopicInfoQuery, TransactionReceiptQuery, TransactionRecordQuery

public abstract class Query<O,T extends Query<O,T>> extends Executable<T,Query,Response,O>
Base class for all queries that can be submitted to Hedera.
  • Field Details

    • paymentTransactionId

      @Nullable protected TransactionId paymentTransactionId
      The transaction ID
    • paymentTransactions

      @Nullable protected List<Transaction> paymentTransactions
      List of payment transactions
  • Constructor Details

    • Query

      Query()
      Constructor.
  • Method Details

    • setQueryPayment

      public T setQueryPayment(Hbar queryPayment)
      Set an explicit payment amount for this query.

      The client will submit exactly this amount for the payment of this query. Hedera will not return any remainder.

      Parameters:
      queryPayment - The explicit payment amount to set
      Returns:
      this
    • setMaxQueryPayment

      public T setMaxQueryPayment(Hbar maxQueryPayment)
      Set the maximum payment allowable for this query.

      When a query is executed without an explicit setQueryPayment(Hbar) call, the client will first request the cost of the given query from the node it will be submitted to and attach a payment for that amount from the operator account on the client.

      If the returned value is greater than this value, a MaxQueryPaymentExceededException will be thrown from Executable.execute(Client) or returned in the second callback of Executable.executeAsync(Client, Consumer, Consumer).

      Set to 0 to disable automatic implicit payments.

      Parameters:
      maxQueryPayment - The maximum payment amount to set
      Returns:
      this
    • getCost

      public Hbar getCost(Client client) throws TimeoutException, PrecheckStatusException
      Fetch the expected cost.
      Parameters:
      client - the client
      Returns:
      the cost in hbar
      Throws:
      TimeoutException - when the transaction times out
      PrecheckStatusException - when the precheck fails
    • getCost

      public Hbar getCost(Client client, Duration timeout) throws TimeoutException, PrecheckStatusException
      Fetch the expected cost.
      Parameters:
      client - the client
      timeout - The timeout after which the execution attempt will be cancelled.
      Returns:
      the cost in hbar
      Throws:
      TimeoutException - when the transaction times out
      PrecheckStatusException - when the precheck fails
    • getCostAsync

      public CompletableFuture<Hbar> getCostAsync(Client client)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      Returns:
      Future result of the cost in hbar
    • getCostAsync

      public CompletableFuture<Hbar> getCostAsync(Client client, Duration timeout)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      timeout - The timeout after which the execution attempt will be cancelled.
      Returns:
      Future result of the cost in hbar
    • getCostAsync

      public void getCostAsync(Client client, Duration timeout, BiConsumer<Hbar,Throwable> callback)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      timeout - The timeout after which the execution attempt will be cancelled.
      callback - a BiConsumer which handles the result or error.
    • getCostAsync

      public void getCostAsync(Client client, BiConsumer<Hbar,Throwable> callback)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      callback - a BiConsumer which handles the result or error.
    • getCostAsync

      public void getCostAsync(Client client, Duration timeout, Consumer<Hbar> onSuccess, Consumer<Throwable> onFailure)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      timeout - The timeout after which the execution attempt will be cancelled.
      onSuccess - a Consumer which consumes the result on success.
      onFailure - a Consumer which consumes the error on failure.
    • getCostAsync

      public void getCostAsync(Client client, Consumer<Hbar> onSuccess, Consumer<Throwable> onFailure)
      Fetch the expected cost asynchronously.
      Parameters:
      client - the client
      onSuccess - a Consumer which consumes the result on success.
      onFailure - a Consumer which consumes the error on failure.
    • isPaymentRequired

      boolean isPaymentRequired()
      Does this query require a payment?
      Returns:
      does this query require a payment
    • onMakeRequest

      abstract void onMakeRequest(Query.Builder queryBuilder, QueryHeader header)
      Called in makeRequest() just before the query is built. The intent is for the derived class to assign their data variant to the query.
    • mapResponseHeader

      abstract ResponseHeader mapResponseHeader(Response response)
      The derived class should access its response header and return.
    • mapRequestHeader

      abstract QueryHeader mapRequestHeader(Query request)
      The derived class should access its request header and return.
    • validateChecksums

      abstract void validateChecksums(Client client) throws BadEntityIdException
      Validate the checksums.
      Throws:
      BadEntityIdException
    • getOperatorFromClient

      Client.Operator getOperatorFromClient(Client client)
      Retrieve the operator from the configured client.
      Parameters:
      client - the configured client
      Returns:
      the operator
    • onExecute

      void onExecute(Client client) throws TimeoutException, PrecheckStatusException
      Specified by:
      onExecute in class Executable<T extends Query<O,T>,Query,Response,O>
      Throws:
      TimeoutException
      PrecheckStatusException
    • onExecuteAsync

      CompletableFuture<Void> onExecuteAsync(Client client)

      Note: This method requires API level 33 or higher. It will not work on devices running API versions below 31 because it uses features introduced in API level 31 (Android 12).

      *
      Specified by:
      onExecuteAsync in class Executable<T extends Query<O,T>,Query,Response,O>
    • getPaymentTransaction

      Transaction getPaymentTransaction(int index)
      Retrieve the transaction at the given index.
      Parameters:
      index - the index
      Returns:
      the transaction
    • makeRequest

      final Query makeRequest()
      Specified by:
      makeRequest in class Executable<T extends Query<O,T>,Query,Response,O>
    • mapResponseStatus

      Status mapResponseStatus(Response response)
      Specified by:
      mapResponseStatus in class Executable<T extends Query<O,T>,Query,Response,O>
    • getTransactionIdInternal

      @Nullable TransactionId getTransactionIdInternal()
      Specified by:
      getTransactionIdInternal in class Executable<T extends Query<O,T>,Query,Response,O>
    • getPaymentTransactionId

      @Nullable public TransactionId getPaymentTransactionId()
      Extract the transaction id.
      Returns:
      the transaction id
    • setPaymentTransactionId

      @Nullable public T setPaymentTransactionId(TransactionId paymentTransactionId)
      Assign the transaction id.
      Parameters:
      paymentTransactionId - the transaction id
      Returns:
      this
    • toString

      public String toString()
      Overrides:
      toString in class Object