Skip to content

Commit e8b4b0c

Browse files
committed
queryNftsByAccountAndTokenId fixed
1 parent 1954a97 commit e8b4b0c

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ public List<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws H
5959
public List<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId) throws HederaException {
6060
Objects.requireNonNull(accountId, "accountId must not be null");
6161
Objects.requireNonNull(tokenId, "tokenId must not be null");
62+
final String host = mirrorNodeEndpoint.substring(8).split("\\:")[0];
63+
final String port = mirrorNodeEndpoint.substring(8).split("\\:")[1];
6264
final String body = restClient.get()
63-
.uri(uriBuilder -> uriBuilder.path(mirrorNodeEndpoint + "/api/v1/tokens/" + tokenId + "/nfts")
65+
.uri(uriBuilder -> uriBuilder
66+
.scheme("https")
67+
.host(host)
68+
.port(port)
69+
.path("/api/v1/tokens/" + tokenId + "/nfts")
6470
.queryParam("account.id", accountId)
6571
.build())
6672
.header("accept", "application/json")
6773
.retrieve()
6874
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
6975
throw new RuntimeException("Client error: " + response.getStatusText());
7076
}).body(String.class);
71-
System.out.println(body);
72-
//TODO: JSON PARSING
73-
return List.of();
77+
return fromJson(body);
7478
}
7579

7680
@Override
@@ -83,26 +87,27 @@ public List<Nft> queryNftsByTokenId(@NonNull TokenId tokenId) throws HederaExcep
8387
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
8488
throw new RuntimeException("Client error: " + response.getStatusText());
8589
}).body(String.class);
90+
return fromJson(body);
91+
}
92+
93+
private List<Nft> fromJson(final String json) {
8694
try {
87-
final JsonNode rootNode = objectMapper.readTree(body);
95+
final JsonNode rootNode = objectMapper.readTree(json);
8896
return StreamSupport.stream(
8997
Spliterators.spliteratorUnknownSize(rootNode.get("nfts").iterator(), Spliterator.ORDERED),
9098
false).map(nftNode -> {
91-
try {
92-
final TokenId parsedTokenId = TokenId.fromString(nftNode.get("token_id").asText());
93-
if (!tokenId.equals(parsedTokenId)) {
94-
throw new RuntimeException("Token ID mismatch: " + tokenId + " != " + parsedTokenId);
95-
}
96-
final AccountId account = AccountId.fromString(nftNode.get("account_id").asText());
97-
final long serial = nftNode.get("serial_number").asLong();
98-
final byte[] metadata = nftNode.get("metadata").binaryValue();
99-
return new Nft(tokenId, serial, account, metadata);
100-
} catch (final Exception e) {
101-
throw new RuntimeException("Error parsing NFT", e);
102-
}
99+
try {
100+
final TokenId parsedTokenId = TokenId.fromString(nftNode.get("token_id").asText());
101+
final AccountId account = AccountId.fromString(nftNode.get("account_id").asText());
102+
final long serial = nftNode.get("serial_number").asLong();
103+
final byte[] metadata = nftNode.get("metadata").binaryValue();
104+
return new Nft(parsedTokenId, serial, account, metadata);
105+
} catch (final Exception e) {
106+
throw new RuntimeException("Error parsing NFT", e);
107+
}
103108
}).toList();
104109
} catch (final Exception e) {
105-
throw new HederaException("Error parsing body as JSON: " + body, e);
110+
throw new IllegalArgumentException("Error parsing body as JSON: " + json, e);
106111
}
107112
}
108113

hedera-spring/src/test/java/com/openelements/hedera/spring/test/NftRepositoryTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.openelements.hedera.spring.test;
22

3+
import com.hedera.hashgraph.sdk.AccountId;
34
import com.hedera.hashgraph.sdk.TokenId;
45
import com.openelements.hedera.base.Account;
56
import com.openelements.hedera.base.AccountClient;
@@ -27,6 +28,9 @@ public class NftRepositoryTests {
2728
@Autowired
2829
private HederaTestUtils hederaTestUtils;
2930

31+
@Value("${spring.hedera.accountId}")
32+
private String accountId;
33+
3034
@Test
3135
void findByTokenId() throws Exception {
3236
//given
@@ -47,4 +51,26 @@ void findByTokenId() throws Exception {
4751
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
4852
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
4953
}
54+
55+
@Test
56+
void findByTokenIdAndAccountId() throws Exception {
57+
//given
58+
final String name = "Tokemon cards";
59+
final String symbol = "TOK";
60+
final String metadata1 = "https://example.com/metadata1";
61+
final String metadata2 = "https://example.com/metadata2";
62+
final TokenId tokenId = nftClient.createNftType(name, symbol);
63+
final List<Long> serial = nftClient.mintNfts(tokenId, List.of(metadata1, metadata2));
64+
final AccountId owner = AccountId.fromString(accountId);
65+
hederaTestUtils.waitForMirrorNodeRecords();
66+
67+
//when
68+
final List<Nft> result = nftRepository.findByOwnerAndType(owner, tokenId);
69+
70+
//then
71+
Assertions.assertNotNull(result);
72+
Assertions.assertEquals(2, result.size());
73+
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
74+
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
75+
}
5076
}

0 commit comments

Comments
 (0)