private Optional findAnyMatchingAssetPair()

in service/src/main/java/org/apache/fineract/cn/stellarbridge/service/internal/horizonadapter/HorizonServerUtilities.java [216:260]


  private Optional<MatchingAssetPair> findAnyMatchingAssetPair(
      final BigDecimal amount,
      final Set<Asset> sourceAssets,
      final Set<Asset> targetAssets,
      final KeyPair sourceAccountKeyPair,
      final KeyPair targetAccountKeyPair) {
    if (sourceAssets.isEmpty())
      return Optional.empty();

    for (final Asset targetAsset : targetAssets) {
      Page<PathResponse> paths;
      try {
        paths = server.paths()
            .sourceAccount(sourceAccountKeyPair)
            .destinationAccount(targetAccountKeyPair)
            .destinationAsset(targetAsset)
            .destinationAmount(StellarAccountHelpers.bigDecimalToStellarBalance(amount))
            .execute();
      } catch (final IOException e) {
        return Optional.empty();
      }

      while (paths != null && paths.getRecords() != null) {
        for (final PathResponse path : paths.getRecords())
        {
          if (StellarAccountHelpers.stellarBalanceToBigDecimal(path.getSourceAmount()).compareTo(amount) <= 0)
          {
            if (sourceAssets.contains(path.getSourceAsset()))
            {
              return Optional.of(new MatchingAssetPair(path.getSourceAsset(), targetAsset));
            }
          }
        }

        try {
          paths = ((paths.getLinks() == null) || (paths.getLinks().getNext() == null)) ?
              null : paths.getNextPage();
        } catch (final URISyntaxException | IOException e) {
          return Optional.empty();
        }
      }
    }

    return Optional.empty();
  }