|
1 | 1 | package com.openelements.hedera.spring.implementation; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
4 | | -import com.fasterxml.jackson.databind.JsonNode; |
5 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
6 | | -import com.hedera.hashgraph.sdk.AccountId; |
7 | | -import com.hedera.hashgraph.sdk.TokenId; |
8 | | -import com.openelements.hedera.base.HederaException; |
9 | | -import com.openelements.hedera.base.Nft; |
10 | | -import com.openelements.hedera.base.mirrornode.MirrorNodeClient; |
11 | | -import com.openelements.hedera.base.mirrornode.Page; |
12 | | -import com.openelements.hedera.base.mirrornode.TransactionInfo; |
13 | 3 | import java.io.IOException; |
14 | 4 | import java.net.URI; |
15 | 5 | import java.util.List; |
|
20 | 10 | import java.util.Spliterators; |
21 | 11 | import java.util.function.Function; |
22 | 12 | import java.util.stream.StreamSupport; |
| 13 | + |
23 | 14 | import org.jspecify.annotations.NonNull; |
24 | 15 | import org.springframework.http.HttpStatus; |
25 | 16 | import org.springframework.http.HttpStatusCode; |
|
28 | 19 | import org.springframework.web.client.RestClient; |
29 | 20 | import org.springframework.web.util.UriBuilder; |
30 | 21 |
|
| 22 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.hedera.hashgraph.sdk.AccountId; |
| 26 | +import com.hedera.hashgraph.sdk.TokenId; |
| 27 | +import com.openelements.hedera.base.HederaException; |
| 28 | +import com.openelements.hedera.base.Nft; |
| 29 | +import com.openelements.hedera.base.mirrornode.MirrorNodeClient; |
| 30 | +import com.openelements.hedera.base.mirrornode.Page; |
| 31 | +import com.openelements.hedera.base.mirrornode.TransactionInfo; |
| 32 | + |
31 | 33 | public class MirrorNodeClientImpl implements MirrorNodeClient { |
32 | 34 |
|
33 | 35 | private final ObjectMapper objectMapper; |
@@ -88,6 +90,14 @@ public Optional<Nft> queryNftsByAccountAndTokenIdAndSerial(@NonNull final Accoun |
88 | 90 | .filter(nft -> Objects.equals(nft.owner(), accountId)); |
89 | 91 | } |
90 | 92 |
|
| 93 | + @Override |
| 94 | + public Page<TransactionInfo> queryTransactionsByAccount(@NonNull final AccountId accountId) throws HederaException { |
| 95 | + Objects.requireNonNull(accountId, "accountId must not be null"); |
| 96 | + final String path = "/api/v1/transactions?account.id=" + accountId.toString(); |
| 97 | + final Function<JsonNode, List<TransactionInfo>> dataExtractionFunction = this::extractTransactionInfoFromJsonNode; |
| 98 | + return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction); |
| 99 | + } |
| 100 | + |
91 | 101 | @Override |
92 | 102 | public Optional<TransactionInfo> queryTransaction(@NonNull final String transactionId) throws HederaException { |
93 | 103 | Objects.requireNonNull(transactionId, "transactionId must not be null"); |
@@ -190,4 +200,23 @@ private List<Nft> getNfts(final JsonNode jsonNode) { |
190 | 200 | } |
191 | 201 | }).toList(); |
192 | 202 | } |
| 203 | + |
| 204 | + private List<TransactionInfo> extractTransactionInfoFromJsonNode(JsonNode jsonNode) { |
| 205 | + if (!jsonNode.has("transactions")) { |
| 206 | + return List.of(); |
| 207 | + } |
| 208 | + final JsonNode transactionsNode = jsonNode.get("transactions"); |
| 209 | + if (!transactionsNode.isArray()) { |
| 210 | + throw new IllegalArgumentException("Transactions node is not an array: " + transactionsNode); |
| 211 | + } |
| 212 | + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(transactionsNode.iterator(), Spliterator.ORDERED), false) |
| 213 | + .map(transactionNode -> { |
| 214 | + try { |
| 215 | + final String transactionId = transactionNode.get("transaction_id").asText(); |
| 216 | + return new TransactionInfo(transactionId); |
| 217 | + } catch (final Exception e) { |
| 218 | + throw new RuntimeException("Error parsing transaction from JSON '" + transactionNode + "'", e); |
| 219 | + } |
| 220 | + }).toList(); |
| 221 | + } |
193 | 222 | } |
0 commit comments