Class MirrorNodeTokenBalance

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

public final class MirrorNodeTokenBalance extends Object
The balance an account holds in a single token, as reported by the mirror node REST API.

Returned by MirrorNodeTokenBalanceQuery.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final long
    The balance the account holds in this token.
    final @Nullable Integer
    The number of decimal places the token is divided into, or null if the mirror node does not report it -- which includes the case where the account is not associated with the token.
    final TokenId
    The token this balance is for.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MirrorNodeTokenBalance(TokenId tokenId, long balance, @Nullable Integer decimals, boolean associated)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static MirrorNodeTokenBalance
    fromJson(com.google.gson.JsonObject root, TokenId requestedTokenId)
    Create a token balance from a mirror node REST JSON payload.
    long
    Extract the balance.
    @Nullable Integer
    Extract the token's decimal places.
    Extract the token id.
    boolean
    Whether the account holds a relationship to this token at all.
    (package private) static MirrorNodeTokenBalance
    A balance for a token the account holds no relationship to -- including the case where the mirror node does not know the account at all.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • tokenId

      public final TokenId tokenId
      The token this balance is for.
    • balance

      public final long balance
      The balance the account holds in this token.

      For a FUNGIBLE_COMMON token this is the amount in the token's smallest denomination. For a NON_FUNGIBLE_UNIQUE token this is the number of NFTs the account holds.

      Zero when the account holds no relationship to the token -- check isAssociated() to tell that case apart from an associated account whose balance happens to be zero.

    • decimals

      public final @Nullable Integer decimals
      The number of decimal places the token is divided into, or null if the mirror node does not report it -- which includes the case where the account is not associated with the token.
  • Constructor Details

    • MirrorNodeTokenBalance

      MirrorNodeTokenBalance(TokenId tokenId, long balance, @Nullable Integer decimals, boolean associated)
      Constructor.
      Parameters:
      tokenId - the token this balance is for
      balance - the balance held, in the token's smallest denomination or as an NFT count
      decimals - the token's decimal places, may be null
      associated - whether the account holds a relationship to the token at all
  • Method Details

    • fromJson

      static MirrorNodeTokenBalance fromJson(com.google.gson.JsonObject root, TokenId requestedTokenId)
      Create a token balance from a mirror node REST JSON payload.

      The mirror node answers with an empty tokens array when the entity exists but holds no relationship to the requested token. That is reported as isAssociated() == false with a zero balance, rather than as an error. A non-existent entity is a 404 and never reaches here.

      Parameters:
      root - the JSON object returned by GET /api/v1/accounts/{id}/tokens
      requestedTokenId - the token that was queried, used when the response carries no relationship
      Returns:
      the new token balance
    • notAssociated

      static MirrorNodeTokenBalance notAssociated(TokenId tokenId)
      A balance for a token the account holds no relationship to -- including the case where the mirror node does not know the account at all.
      Parameters:
      tokenId - the token that was queried
      Returns:
      a zero, non-associated balance
    • isAssociated

      public boolean isAssociated()
      Whether the account holds a relationship to this token at all.

      Use this to distinguish "never associated with the token" from "associated, balance zero" -- both report a balance of zero.

      Returns:
      true if the account is associated with the token
    • getTokenId

      public TokenId getTokenId()
      Extract the token id.
      Returns:
      the token this balance is for
    • getBalance

      public long getBalance()
      Extract the balance.
      Returns:
      the balance held, in the token's smallest denomination or as an NFT count
    • getDecimals

      public @Nullable Integer getDecimals()
      Extract the token's decimal places.
      Returns:
      the number of decimal places, or null if not reported
    • toString

      public String toString()
      Overrides:
      toString in class Object