Skip to content

Commit

Permalink
Improve find total supply
Browse files Browse the repository at this point in the history
  • Loading branch information
wolthrax committed May 2, 2023
1 parent 856b7b8 commit 7548542
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class EndpointProperties {
@Setter
public static class CosmosSDK {
private String supply;
private String supplyPaginated;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.mapofzones.tokenmatcher.service.tokenprice.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mapofzones.tokenmatcher.common.properties.EndpointProperties;
import com.mapofzones.tokenmatcher.service.tokenprice.client.dto.SupplyTokenDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Slf4j
public class TokenSupplyClient implements ITokenPriceClient {
Expand All @@ -28,14 +30,50 @@ public TokenSupplyClient(RestTemplate tokenPriceRestTemplate,
public SupplyTokenDto findTokenPrice(String urlWithBaseToken, LocalDateTime lastTokenPriceTime) {

URI uri = URI.create(String.format(urlWithBaseToken, endpointProperties.getCosmosSDK().getSupply()));

Optional<String> supply = callApi(uri.toString(), false);

if (supply.isEmpty()) {
uri = URI.create(String.format(urlWithBaseToken, endpointProperties.getCosmosSDK().getSupplyPaginated()));
supply = callApi(uri.toString(), true);
}

return new SupplyTokenDto(supply.orElse(null));
}

private Optional<String> callApi(String uri, boolean arraySupply) {
try {
log.debug("Find supply: " + uri);
ResponseEntity<String> response = tokenPriceRestTemplate.getForEntity(uri, String.class);
JsonNode node = new ObjectMapper().readValue(response.getBody(), JsonNode.class);
return new SupplyTokenDto(node.get("amount").get("amount").asText());
} catch (JsonProcessingException | RestClientException e) {

if (arraySupply) {
String denom = uri.substring(uri.lastIndexOf("/") + 1);
uri = uri.substring(0, uri.lastIndexOf("/"));

ResponseEntity<String> response = tokenPriceRestTemplate.getForEntity(uri, String.class);
JsonNode arrayNode = new ObjectMapper().readValue(response.getBody(), JsonNode.class);
Iterator<JsonNode> iter = arrayNode.findValue("supply").elements();
List<JsonNode> jsonNodes = new ArrayList<>();
iter.forEachRemaining(jsonNodes::add);

JsonNode node = null;
for (JsonNode jsonNode : jsonNodes) {
if (jsonNode.get("denom").asText().equals(denom)) {
node = jsonNode;
break;
}
}

return Optional.of(node.get("amount").asText());
} else {
ResponseEntity<String> response = tokenPriceRestTemplate.getForEntity(uri, String.class);
JsonNode node = new ObjectMapper().readValue(response.getBody(), JsonNode.class);
return Optional.of(node.get("amount").get("amount").asText());
}

} catch (Exception e) {
log.debug("Json can't parce: " + uri);
return new SupplyTokenDto(null);
return Optional.empty();
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ token-price-finder:
endpoint:
cosmos-sdk:
supply: '/cosmos/bank/v1beta1/supply'
supply-paginated: '/cosmos/bank/v1beta1/supply?pagination.limit=1000000'
ibc:
denom-trace-beta: '/ibc/applications/transfer/v1beta1/denom_traces/%s'
denom-trace: '/ibc/apps/transfer/v1/denom_traces/%s'
Expand Down

0 comments on commit 7548542

Please sign in to comment.