Class MirrorNodeAccountBalanceQuery
GET /api/v1/balances?account.id=...).
This is the replacement for AccountBalanceQuery, which relies on the consensus node
CryptoService/cryptoGetBalance endpoint that the network is retiring.
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), as the balances endpoint supports them directly and no separate
setter is needed.
Only the HBAR balance is returned. Token balances are not covered by this query.
An account the mirror node does not know fails with a PrecheckStatusException carrying
Status.INVALID_ACCOUNT_ID, the same error AccountBalanceQuery reported. Note that the
balances endpoint answers with an empty result rather than a 404, so this is the SDK's mapping of that
empty result — not a status the mirror node itself returned.
Eventual consistency: the mirror node reflects network state with a small lag, typically
a few seconds. Applications that need an immediate post-transaction balance must allow for it. The lag
applies to the account's existence as well as to its balance: a freshly created account can
transiently fail with Status.INVALID_ACCOUNT_ID until the mirror node has ingested it, so code
that queries an account right after creating it should retry rather than treat the first failure as
final.
This query is free.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionExecutes the query with the user supplied client.Executes the query with the user supplied client and timeout.executeAsync(Client client) Executes the query asynchronously with the user supplied client.executeAsync(Client client, Duration timeout) Executes the query asynchronously with the user supplied client and timeout.Return the account's id.intExtract the maximum number of attempts.Extract the maximum backoff duration.setAccountId(AccountId accountId) The ID of the account for which the balance is being requested.setMaxAttempts(int maxAttempts) Set the maximum number of attempts for the query.setMaxBackoff(Duration maxBackoff) Set the maximum backoff duration for retry attempts.toString()
-
Constructor Details
-
MirrorNodeAccountBalanceQuery
public MirrorNodeAccountBalanceQuery()Constructor.
-
-
Method Details
-
getAccountId
Return the account's id.- Returns:
accountId
-
setAccountId
The ID of the account for which the balance is being requested.Accepts
shard.realm.num, an EVM address, or a public key alias. Contract IDs are also accepted — the balances endpoint resolves them, so no separate setter is needed; convert withnew AccountId(contractId.shard, contractId.realm, contractId.num).- Parameters:
accountId- the account id to set- Returns:
this
-
getMaxAttempts
public int getMaxAttempts()Extract the maximum number of attempts.- Returns:
- the maximum number of attempts
-
setMaxAttempts
Set the maximum number of attempts for the query.- Parameters:
maxAttempts- the maximum number of attempts- Returns:
this
-
getMaxBackoff
Extract the maximum backoff duration.- Returns:
- the maximum backoff duration
-
setMaxBackoff
Set the maximum backoff duration for retry attempts.- Parameters:
maxBackoff- the maximum backoff duration- Returns:
this
-
execute
public MirrorNodeAccountBalance execute(Client client) throws PrecheckStatusException, ExecutionException, InterruptedException Executes the query with the user supplied client.- Parameters:
client- the client with which this will be executed- Returns:
- the retrieved
MirrorNodeAccountBalance - Throws:
PrecheckStatusException- withStatus.INVALID_ACCOUNT_IDif the mirror node knows no such accountExecutionException- if the query failsInterruptedException- if the thread is interrupted
-
execute
public MirrorNodeAccountBalance execute(Client client, Duration timeout) throws PrecheckStatusException, ExecutionException, InterruptedException Executes the query with the user supplied client and timeout.- Parameters:
client- the client with which this will be executedtimeout- the maximum duration for each individual HTTP request- Returns:
- the retrieved
MirrorNodeAccountBalance - Throws:
PrecheckStatusException- withStatus.INVALID_ACCOUNT_IDif the mirror node knows no such accountExecutionException- if the query failsInterruptedException- if the thread is interrupted
-
executeAsync
Executes the query asynchronously with the user supplied client.If the mirror node knows no such account the future completes exceptionally with a
PrecheckStatusExceptioncarryingStatus.INVALID_ACCOUNT_ID— surfaced as the cause of aCompletionExceptionfromjoin(), or of anExecutionExceptionfromget().- Parameters:
client- the client with which this will be executed- Returns:
- a future representing the retrieved
MirrorNodeAccountBalance
-
executeAsync
Executes the query asynchronously with the user supplied client and timeout.If the mirror node knows no such account the future completes exceptionally with a
PrecheckStatusExceptioncarryingStatus.INVALID_ACCOUNT_ID— surfaced as the cause of aCompletionExceptionfromjoin(), or of anExecutionExceptionfromget().- Parameters:
client- the client with which this will be executedtimeout- the maximum duration for each individual HTTP request- Returns:
- a future representing the retrieved
MirrorNodeAccountBalance
-
toString
-