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 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.@Nullable AccountIdReturn the account's id.intExtract the maximum number of attempts.Extract the maximum backoff duration.@Nullable TokenIdReturn the token's id.setAccountId(AccountId accountId) The ID of the account whose token 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.setTokenId(TokenId tokenId) The ID of the token whose balance is being requested.toString()
-
Constructor Details
-
MirrorNodeTokenBalanceQuery
public MirrorNodeTokenBalanceQuery()Constructor.
-
-
Method Details
-
getAccountId
Return the account's id.- Returns:
accountId
-
setAccountId
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 withnew AccountId(contractId.shard, contractId.realm, contractId.num).- Parameters:
accountId- the account id to set- Returns:
this
-
getTokenId
Return the token's id.- Returns:
tokenId
-
setTokenId
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
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
Extract the maximum backoff duration.- Returns:
- the maximum backoff duration
-
setMaxBackoff
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
public MirrorNodeTokenBalance execute(Client client) throws ExecutionException, InterruptedException 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 failsInterruptedException- if the thread is interrupted
-
execute
public MirrorNodeTokenBalance execute(Client client, Duration timeout) throws ExecutionException, InterruptedException Executes the query with the user supplied client and timeout.- Parameters:
client- the client with which this will be executedtimeout- the budget for the whole call, retries and backoff included- Returns:
- the retrieved
MirrorNodeTokenBalance - Throws:
ExecutionException- if the query failsInterruptedException- if the thread is interrupted
-
executeAsync
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
Executes the query asynchronously with the user supplied client and timeout.- Parameters:
client- the client with which this will be executedtimeout- the budget for the whole call, retries and backoff included- Returns:
- a future representing the retrieved
MirrorNodeTokenBalance
-
toString
-