Skip to content

Commit 9a215b7

Browse files
committed
last transaction
1 parent dcd652b commit 9a215b7

File tree

2 files changed

+14
-180
lines changed

2 files changed

+14
-180
lines changed
Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,11 @@
11
package com.openelements.hedera.base.mirrornode;
22

3-
import java.util.List;
43
import java.util.Objects;
4+
import org.jspecify.annotations.NonNull;
55

6-
import com.fasterxml.jackson.annotation.JsonCreator;
7-
import com.fasterxml.jackson.annotation.JsonProperty;
8-
import org.jspecify.annotations.Nullable;
9-
10-
public record TransactionInfo( @Nullable Byte bytes,
11-
long chargedTxFee,
12-
String consensusTimestamp,
13-
String entityId,
14-
String maxFee,
15-
String memoBase64,
16-
String name,
17-
List<NftTransfers> nftTransfers,
18-
String node,
19-
int nonce,
20-
String parentConsensusTimestamp,
21-
String result,
22-
boolean scheduled,
23-
@Nullable List<Object> stakingRewardTransfers,
24-
@Nullable List<Object> tokenTransfers,
25-
String transactionHash,
26-
String transactionId,
27-
List<Transfers> transfers,
28-
String validDurationSeconds,
29-
String validStartTimestamp
30-
) {
31-
32-
@JsonCreator
33-
public TransactionInfo(
34-
@JsonProperty("bytes") @Nullable Byte bytes,
35-
@JsonProperty("charged_tx_fee") long chargedTxFee,
36-
@JsonProperty("consensus_timestamp") String consensusTimestamp,
37-
@JsonProperty("entity_id") String entityId,
38-
@JsonProperty("max_fee") String maxFee,
39-
@JsonProperty("memo_base64") String memoBase64,
40-
@JsonProperty("name") String name,
41-
@JsonProperty("nft_transfers") List<NftTransfers> nftTransfers,
42-
@JsonProperty("node") String node,
43-
@JsonProperty("nonce") int nonce,
44-
@JsonProperty("parent_consensus_timestamp") String parentConsensusTimestamp,
45-
@JsonProperty("result") String result,
46-
@JsonProperty("scheduled") boolean scheduled,
47-
@JsonProperty("staking_reward_transfers") @Nullable List<Object> stakingRewardTransfers,
48-
@JsonProperty("token_transfers") @Nullable List<Object> tokenTransfers,
49-
@JsonProperty("transaction_hash") String transactionHash,
50-
@JsonProperty("transaction_id") String transactionId,
51-
@JsonProperty("transfers") List<Transfers> transfers,
52-
@JsonProperty("valid_duration_seconds") String validDurationSeconds,
53-
@JsonProperty("valid_start_timestamp") String validStartTimestamp
54-
){
55-
this.bytes= bytes;
56-
this.chargedTxFee = chargedTxFee;
57-
this.consensusTimestamp= consensusTimestamp;
58-
this.entityId= entityId;
59-
this.maxFee= maxFee;
60-
this.memoBase64= memoBase64;
61-
this.name= name;
62-
this.nftTransfers= nftTransfers;
63-
this.node= node;
64-
this.nonce= nonce;
65-
this.parentConsensusTimestamp= parentConsensusTimestamp;
66-
this.result= result;
67-
this.scheduled= scheduled;
68-
this.stakingRewardTransfers= stakingRewardTransfers;
69-
this.tokenTransfers= tokenTransfers;
70-
this.transactionHash= transactionHash;
71-
this.transactionId= transactionId;
72-
this.transfers= transfers;
73-
this.validDurationSeconds= validDurationSeconds;
74-
this.validStartTimestamp= validStartTimestamp;
6+
public record TransactionInfo(@NonNull String transactionId) {
757

8+
public TransactionInfo {
769
Objects.requireNonNull(transactionId, "transactionId must not be null");
7710
}
78-
}
79-
80-
class NftTransfers{
81-
private final boolean isApproval;
82-
private final String receiverAccountId;
83-
private final String senderAccountId;
84-
private final int serialNumber;
85-
private final String tokenId;
86-
87-
@JsonCreator
88-
public NftTransfers( @JsonProperty("is_approval") boolean isApproval,
89-
@JsonProperty("receiver_account_id") String receiverAccountId,
90-
@JsonProperty("sender_account_id") @Nullable String senderAccountId,
91-
@JsonProperty("serial_number") int serialNumber,
92-
@JsonProperty("token_id") String tokenId
93-
) {
94-
this.isApproval = isApproval;
95-
this.receiverAccountId = receiverAccountId;
96-
this.senderAccountId = senderAccountId;
97-
this.serialNumber = serialNumber;
98-
this.tokenId = tokenId;
99-
}
100-
}
101-
102-
class Transfers{
103-
private final String account;
104-
private final long amount;
105-
private final boolean isApproved;
106-
107-
@JsonCreator
108-
public Transfers( @JsonProperty("account") String account,
109-
@JsonProperty("amount") long amount,
110-
@JsonProperty("is_approved") boolean isApproved
111-
) {
112-
this.account = account;
113-
this.amount = amount;
114-
this.isApproved = isApproved;
115-
}
116-
}
11+
}

hedera-spring/src/main/java/com/openelements/hedera/spring/implementation/MirrorNodeClientImpl.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package 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;
313
import java.io.IOException;
414
import java.net.URI;
515
import java.util.List;
@@ -10,7 +20,6 @@
1020
import java.util.Spliterators;
1121
import java.util.function.Function;
1222
import java.util.stream.StreamSupport;
13-
1423
import org.jspecify.annotations.NonNull;
1524
import org.springframework.http.HttpStatus;
1625
import org.springframework.http.HttpStatusCode;
@@ -19,18 +28,6 @@
1928
import org.springframework.web.client.RestClient;
2029
import 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-
3431
public 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

Comments
 (0)