Skip to content

Commit

Permalink
Implement queryTransactionsByAccount method in MirrorNodeClientImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
belloibrahv committed Nov 7, 2024
1 parent 516b240 commit 90fa2b5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package com.openelements.hedera.spring.implementation;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.TokenId;
import com.openelements.hedera.base.HederaException;
import com.openelements.hedera.base.Nft;
import com.openelements.hedera.base.mirrornode.MirrorNodeClient;
import com.openelements.hedera.base.mirrornode.Page;
import com.openelements.hedera.base.mirrornode.TransactionInfo;
import java.io.IOException;
import java.net.URI;
import java.util.List;
Expand All @@ -20,6 +10,7 @@
import java.util.Spliterators;
import java.util.function.Function;
import java.util.stream.StreamSupport;

import org.jspecify.annotations.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
Expand All @@ -28,6 +19,17 @@
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.TokenId;
import com.openelements.hedera.base.HederaException;
import com.openelements.hedera.base.Nft;
import com.openelements.hedera.base.mirrornode.MirrorNodeClient;
import com.openelements.hedera.base.mirrornode.Page;
import com.openelements.hedera.base.mirrornode.TransactionInfo;

public class MirrorNodeClientImpl implements MirrorNodeClient {

private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -88,6 +90,14 @@ public Optional<Nft> queryNftsByAccountAndTokenIdAndSerial(@NonNull final Accoun
.filter(nft -> Objects.equals(nft.owner(), accountId));
}

@Override
public Page<TransactionInfo> queryTransactionsByAccount(@NonNull final AccountId accountId) throws HederaException {
Objects.requireNonNull(accountId, "accountId must not be null");
final String path = "/api/v1/transactions?account.id=" + accountId.toString();
final Function<JsonNode, List<TransactionInfo>> dataExtractionFunction = this::extractTransactionInfoFromJsonNode;
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction);
}

@Override
public Optional<TransactionInfo> queryTransaction(@NonNull final String transactionId) throws HederaException {
Objects.requireNonNull(transactionId, "transactionId must not be null");
Expand Down Expand Up @@ -190,4 +200,23 @@ private List<Nft> getNfts(final JsonNode jsonNode) {
}
}).toList();
}

private List<TransactionInfo> extractTransactionInfoFromJsonNode(JsonNode jsonNode) {
if (!jsonNode.has("transactions")) {
return List.of();
}
final JsonNode transactionsNode = jsonNode.get("transactions");
if (!transactionsNode.isArray()) {
throw new IllegalArgumentException("Transactions node is not an array: " + transactionsNode);
}
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(transactionsNode.iterator(), Spliterator.ORDERED), false)
.map(transactionNode -> {
try {
final String transactionId = transactionNode.get("transaction_id").asText();
return new TransactionInfo(transactionId);
} catch (final Exception e) {
throw new RuntimeException("Error parsing transaction from JSON '" + transactionNode + "'", e);
}
}).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.openelements.hedera.spring.test;

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.TokenId;
import com.openelements.hedera.base.Account;
import com.openelements.hedera.base.AccountClient;
import com.openelements.hedera.base.Nft;
import com.openelements.hedera.base.NftClient;
import com.openelements.hedera.base.NftRepository;
import com.openelements.hedera.base.mirrornode.Page;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.TokenId;
import com.openelements.hedera.base.Account;
import com.openelements.hedera.base.AccountClient;
import com.openelements.hedera.base.Nft;
import com.openelements.hedera.base.NftClient;
import com.openelements.hedera.base.NftRepository;
import com.openelements.hedera.base.mirrornode.Page;

@SpringBootTest(classes = TestConfig.class)
public class NftRepositoryTests {

Expand Down

0 comments on commit 90fa2b5

Please sign in to comment.