11package com .openelements .hedera .spring .implementation ;
22
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 ;
313import java .io .IOException ;
414import java .net .URI ;
515import java .util .List ;
1020import java .util .Spliterators ;
1121import java .util .function .Function ;
1222import java .util .stream .StreamSupport ;
13-
1423import org .jspecify .annotations .NonNull ;
1524import org .springframework .http .HttpStatus ;
1625import org .springframework .http .HttpStatusCode ;
1928import org .springframework .web .client .RestClient ;
2029import org .springframework .web .util .UriBuilder ;
2130
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 .AccountInfo ;
30- import com .openelements .hedera .base .mirrornode .MirrorNodeClient ;
31- import com .openelements .hedera .base .mirrornode .Page ;
32- import com .openelements .hedera .base .mirrornode .TransactionInfo ;
33-
3431public class MirrorNodeClientImpl implements MirrorNodeClient {
3532
3633 private final ObjectMapper objectMapper ;
@@ -91,14 +88,6 @@ public Optional<Nft> queryNftsByAccountAndTokenIdAndSerial(@NonNull final Accoun
9188 .filter (nft -> Objects .equals (nft .owner (), accountId ));
9289 }
9390
94- @ Override
95- public Page <TransactionInfo > queryTransactionsByAccount (@ NonNull final AccountId accountId ) throws HederaException {
96- Objects .requireNonNull (accountId , "accountId must not be null" );
97- final String path = "/api/v1/transactions?account.id=" + accountId .toString ();
98- final Function <JsonNode , List <TransactionInfo >> dataExtractionFunction = this ::extractTransactionInfoFromJsonNode ;
99- return new RestBasedPage <>(objectMapper , restClient .mutate ().clone (), path , dataExtractionFunction );
100- }
101-
10291 @ Override
10392 public Optional <TransactionInfo > queryTransaction (@ NonNull final String transactionId ) throws HederaException {
10493 Objects .requireNonNull (transactionId , "transactionId must not be null" );
@@ -119,13 +108,6 @@ public Optional<TransactionInfo> queryTransaction(@NonNull final String transact
119108
120109 }
121110
122- @ Override
123- public @ NonNull Optional <AccountInfo > queryAccount (@ NonNull AccountId accountId ) throws HederaException {
124- Objects .requireNonNull (accountId , "accountId must not be null" );
125- final JsonNode jsonNode = doGetCall ("/api/v1/accounts/" + accountId );
126- return jsonNodeToOptionalAccountINfo (jsonNode );
127- }
128-
129111 private JsonNode doGetCall (String path , Map <String , ?> params ) throws HederaException {
130112 return doGetCall (builder -> {
131113 UriBuilder uriBuilder = builder .path (path );
@@ -200,30 +182,6 @@ private Nft jsonNodeToNft(final JsonNode jsonNode) throws IOException {
200182 }
201183 }
202184
203- private @ NonNull Optional <AccountInfo > jsonNodeToOptionalAccountINfo (JsonNode jsonNode ) throws HederaException {
204- if (jsonNode == null || !jsonNode .fieldNames ().hasNext ()) {
205- return Optional .empty ();
206- }
207- try {
208- return Optional .of (jsonNodeToAccountInfo (jsonNode ));
209- } catch (final Exception e ) {
210- throw new HederaException ("Error parsing AccountInfo from JSON '" + jsonNode + "'" , e );
211- }
212- }
213-
214- private AccountInfo jsonNodeToAccountInfo (JsonNode jsonNode ) {
215- try {
216- final AccountId accountId = AccountId .fromString (jsonNode .get ("account" ).asText ());
217- final String evmAddress = jsonNode .get ("evm_address" ).asText ();
218- final long ethereumNonce = jsonNode .get ("ethereum_nonce" ).asLong ();
219- final long pendingReward = jsonNode .get ("pending_reward" ).asLong ();
220- final long balance = jsonNode .get ("balance" ).get ("balance" ).asLong ();
221- return new AccountInfo (accountId , evmAddress , balance , ethereumNonce , pendingReward );
222- } catch (final Exception e ) {
223- throw new IllegalArgumentException ("Error parsing NFT from JSON '" + jsonNode + "'" , e );
224- }
225- }
226-
227185 private List <Nft > getNfts (final JsonNode jsonNode ) {
228186 if (!jsonNode .has ("nfts" )) {
229187 return List .of ();
@@ -242,23 +200,4 @@ private List<Nft> getNfts(final JsonNode jsonNode) {
242200 }
243201 }).toList ();
244202 }
245-
246- private List <TransactionInfo > extractTransactionInfoFromJsonNode (JsonNode jsonNode ) {
247- if (!jsonNode .has ("transactions" )) {
248- return List .of ();
249- }
250- final JsonNode transactionsNode = jsonNode .get ("transactions" );
251- if (!transactionsNode .isArray ()) {
252- throw new IllegalArgumentException ("Transactions node is not an array: " + transactionsNode );
253- }
254- return StreamSupport .stream (Spliterators .spliteratorUnknownSize (transactionsNode .iterator (), Spliterator .ORDERED ), false )
255- .map (transactionNode -> {
256- try {
257- final String transactionId = transactionNode .get ("transaction_id" ).asText ();
258- return new TransactionInfo (transactionId );
259- } catch (final Exception e ) {
260- throw new RuntimeException ("Error parsing transaction from JSON '" + transactionNode + "'" , e );
261- }
262- }).toList ();
263- }
264203}
0 commit comments