Class MirrorNodeTokenBalanceQuery

java.lang.Object
com.hedera.hashgraph.sdk.MirrorNodeTokenBalanceQuery

public final class MirrorNodeTokenBalanceQuery extends Object
Get the balance an account holds in a single token from the mirror node REST API (GET /api/v1/accounts/{id}/tokens?token.id=...).

This is the token-balance counterpart to MirrorNodeAccountBalanceQuery, which returns HBAR. Together they replace AccountBalanceQuery.

Both setAccountId(AccountId) and setTokenId(TokenId) are required, so the query is always bounded to a single relationship and never paginates. Reading several tokens for one account means issuing several queries -- this is deliberate: enumerating a whole token portfolio would require unbounded pagination against an account with an arbitrarily large number of tokens.

The returned MirrorNodeTokenBalance.balance is the amount in the token's smallest denomination for FUNGIBLE_COMMON tokens, and the number of NFTs held for NON_FUNGIBLE_UNIQUE tokens. If the entity holds no relationship to the token, the balance is zero and MirrorNodeTokenBalance.isAssociated() is false.

The account may be identified by shard.realm.num, by an EVM address, or by a public key alias -- the mirror node resolves all three. Contract IDs are also accepted; pass them through setAccountId(AccountId), converting with new AccountId(contractId.shard, contractId.realm, contractId.num).

An entity the mirror node does not know -- because it does not exist, or because the mirror node has not ingested it yet -- reports isAssociated() == false with a zero balance rather than raising an error, matching how MirrorNodeAccountBalanceQuery reports zero for an unknown account. A wrong account id therefore reads as "no relationship" rather than failing.

Eventual consistency: the mirror node reflects network state with a small lag, typically a few seconds. An entity queried immediately after it is created will report as not associated until the mirror node catches up, so applications that need a definitive answer must allow for the lag rather than treating the first response as final.

HTTP is performed through the client's HttpTransport, so the retry budget, the timeout bounds and the connection pool are the ones on Client.getMirrorNodeHttpConfig(). The setters below override a single field of that budget each, and nothing else.

This query is free.

  • Constructor Details

    • MirrorNodeTokenBalanceQuery

      public MirrorNodeTokenBalanceQuery()
      Constructor.
  • Method Details

    • getAccountId

      public @Nullable AccountId getAccountId()
      Return the account's id.
      Returns:
      accountId
    • setAccountId

      public MirrorNodeTokenBalanceQuery setAccountId(AccountId accountId)
      The ID of the account whose token balance is being requested. Required.

      Accepts shard.realm.num, an EVM address, or a public key alias. Contract IDs are also accepted -- convert with new AccountId(contractId.shard, contractId.realm, contractId.num).

      Parameters:
      accountId - the account id to set
      Returns:
      this
    • getTokenId

      public @Nullable TokenId getTokenId()
      Return the token's id.
      Returns:
      tokenId
    • setTokenId

      public MirrorNodeTokenBalanceQuery setTokenId(TokenId tokenId)
      The ID of the token whose balance is being requested. Required.
      Parameters:
      tokenId - the token id to set
      Returns:
      this
    • getMaxAttempts

      public int getMaxAttempts()
      Extract the maximum number of attempts.
      Returns:
      the maximum number of attempts, per HTTP request
    • setMaxAttempts

      public MirrorNodeTokenBalanceQuery setMaxAttempts(int maxAttempts)
      Set the maximum number of attempts for the query.

      Overrides only this field of the client's retry policy; every other field is left at the client's value.

      Parameters:
      maxAttempts - the maximum number of attempts
      Returns:
      this
    • getMaxBackoff

      public Duration getMaxBackoff()
      Extract the maximum backoff duration.
      Returns:
      the maximum backoff duration
    • setMaxBackoff

      public MirrorNodeTokenBalanceQuery setMaxBackoff(Duration maxBackoff)
      Set the maximum backoff duration for retry attempts.

      Overrides only this field of the client's retry policy; every other field is left at the client's value.

      Parameters:
      maxBackoff - the maximum backoff duration
      Returns:
      this
    • execute

      Executes the query with the user supplied client.
      Parameters:
      client - the client with which this will be executed
      Returns:
      the retrieved MirrorNodeTokenBalance
      Throws:
      ExecutionException - if the query fails
      InterruptedException - if the thread is interrupted
    • execute

      Executes the query with the user supplied client and timeout.
      Parameters:
      client - the client with which this will be executed
      timeout - the budget for the whole call, retries and backoff included
      Returns:
      the retrieved MirrorNodeTokenBalance
      Throws:
      ExecutionException - if the query fails
      InterruptedException - if the thread is interrupted
    • executeAsync

      public CompletableFuture<MirrorNodeTokenBalance> executeAsync(Client client)
      Executes the query asynchronously with the user supplied client.
      Parameters:
      client - the client with which this will be executed
      Returns:
      a future representing the retrieved MirrorNodeTokenBalance
    • executeAsync

      public CompletableFuture<MirrorNodeTokenBalance> executeAsync(Client client, Duration timeout)
      Executes the query asynchronously with the user supplied client and timeout.
      Parameters:
      client - the client with which this will be executed
      timeout - the budget for the whole call, retries and backoff included
      Returns:
      a future representing the retrieved MirrorNodeTokenBalance
    • toString

      public String toString()
      Overrides:
      toString in class Object