Interface TokenSelection

  • All Implemented Interfaces:

    @DoNotImplement() 
    public interface TokenSelection
    
                        

    Defines a mechanism to allow flows to query the token cache. The query can be either to claim a list of ClaimedToken to spend, or to know the current balance, TokenBalance.

    The platform will provide an instance of TokenSelection to flows via property injection.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Constructor Detail

    • Method Detail

      • tryClaim

        @Nullable()@Suspendable() abstract TokenClaim tryClaim(@NotNull() String deduplicationId, @NotNull() TokenClaimCriteria criteria)

        Attempts to claim a set of tokens from the cache that satisfies the specified TokenClaimCriteria. The API allows a flow to query for a target amount of a given state/token type it wishes to spend. If available, a set of tokens will be selected that sum to at least the target amount specified. The tokens will be locked in the cache to prevent other flows from selecting them. A unique deterministic identifier for a token claim is provided per API call. Corda may rerun a token claim request on behalf of the calling flow in the event system instability. This ID will be used to deduplicate TokenClaims in this scenario. This ID needs to be unique per TokenClaim within a single flow.

        Parameters:
        deduplicationId - A unique deterministic identifier for a token claim.
        criteria - The TokenClaimCriteria used to select tokens.
        Returns:

        Returns a TokenClaim if enough tokens were claimed to satisfy the getTargetAmount, or null if the getTargetAmount could not be reached.

      • queryBalance

        @Nullable()@Suspendable() abstract TokenBalance queryBalance(@NotNull() TokenBalanceCriteria criteria)

        Calculates the balance of a pool of tokens taking into account only the tokens that satisfy the specified TokenBalanceCriteria. The API allows a flow to query the balance of a set or subset of tokens. Two values are calculated when the query is executed. One represents the available balance which only includes tokens that have not been spent nor claimed. While the other value represents the total balance which includes all tokens that have not been spent, i.e., the total balance is the available balance plus the balance of all claimed tokens.

        Parameters:
        criteria - The TokenBalanceCriteria used to select the tokens that should be used to calculate the balance.
        Returns:

        Returns the balance that was calculated as a TokenBalance.

        Example usage:

        • Kotlin:
          
          lateinit var tokenSelection: TokenSelection
          
          override fun call(requestBody: RestRequestBody): String {
          
          // Create a criteria describing the tokens to be taken
          // into consideration while calculating the balance.
          val criteria = TokenBalanceCriteria(
          "Currency",
          getIssuerHash(),
          getNotaryX500Name(),
          "USD"
          )
          
          // Call the token selection API to calculate the balance.
          val response = tokenSelection.queryBalance(criteria)
          
          // Print the results
          println("Balance available: ${response.availableBalance}, Total balance: ${response.totalBalance}")
          
          return "Done"
          }
          
        • Java:
          
          public TokenSelection tokenSelection;
          public String call(RestRequestBody requestBody) {
          
          // Create a criteria describing the tokens to be taken
          // into consideration while calculating the balance.
          TokenBalanceCriteria criteria = new TokenBalanceCriteria(
          "Currency",
          getIssuerHash(),
          getNotaryX500Name(),
          "USD"
          );
          
          // Call the token selection API to calculate the balance.
          TokenBalance response = tokenSelection.queryBalance(criteria);
          
          // Print the results.
          System.out.println("Balance available: " + response.getAvailableBalance() + "Total balance: " + response.getTotalBalance());
          
          return "Done";
          }