From bc918f136ab9c22c741cf32e9fc9018b2541be9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Terje=20S=C3=B8rensen?= Date: Sat, 30 Sep 2023 12:13:31 +0200 Subject: [PATCH] Cleaning up code and fixing tests --- .../no/jansoren/defillama/CoinsClient.java | 18 +++++++++--------- .../no/jansoren/defillama/ProtocolsClient.java | 7 ++++--- .../model/protocols/BaseDefiLlamaClient.java | 11 +++++++++++ .../no/jansoren/defillama/CoinsClientTest.java | 11 +++++------ .../defillama/ProtocolsClientTest.java | 3 ++- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/java/no/jansoren/defillama/CoinsClient.java b/src/main/java/no/jansoren/defillama/CoinsClient.java index 378ad6f..f7b3303 100644 --- a/src/main/java/no/jansoren/defillama/CoinsClient.java +++ b/src/main/java/no/jansoren/defillama/CoinsClient.java @@ -1,7 +1,7 @@ package no.jansoren.defillama; import com.fasterxml.jackson.databind.ObjectMapper; -import no.jansoren.defillama.model.coins.Coin; +import no.jansoren.defillama.model.coins.Coins; import no.jansoren.defillama.model.protocols.BaseDefiLlamaClient; import java.net.URLEncoder; @@ -16,20 +16,20 @@ public CoinsClient(HttpClient httpClient, ObjectMapper objectMapper) { super(httpClient, objectMapper); } - public Map getPricesOfTokensByContractAddress(String coins, String searchWidth) { - return get(HOSTNAME_COINS+"/prices/current/"+coins+"?searchWidth="+searchWidth); + public Coins getPricesOfTokensByContractAddress(String coins, String searchWidth) { + return get(HOSTNAME_COINS+"/prices/current/"+coins+"?searchWidth="+searchWidth, Coins.class); } - public Map getHistoricalPricesOfTokensByContractAddress(String coins, int timestamp, String searchWidth) { - return get(HOSTNAME_COINS+"/prices/historical/" + timestamp + "/" +coins+"?searchWidth="+searchWidth); + public Coins getHistoricalPricesOfTokensByContractAddress(String coins, int timestamp, String searchWidth) { + return get(HOSTNAME_COINS+"/prices/historical/" + timestamp + "/" +coins+"?searchWidth="+searchWidth, Coins.class); } - public Map getHistoricalPricesForMultipleTokens(String coins, String searchWidth) { + public Coins getHistoricalPricesForMultipleTokens(String coins, String searchWidth) { var coinsEncoded = URLEncoder.encode(coins, StandardCharsets.UTF_8); - return get(HOSTNAME_COINS+"/batchHistorical?coins=" + coinsEncoded + "&searchWidth="+searchWidth); + return get(HOSTNAME_COINS+"/batchHistorical?coins=" + coinsEncoded + "&searchWidth="+searchWidth, Coins.class); } - public Map getPricesAtRegularTimeIntervals(String coins, Integer start, Integer end, Integer span, String period, String searchWidth) { + public Coins getPricesAtRegularTimeIntervals(String coins, Integer start, Integer end, Integer span, String period, String searchWidth) { String baseUrl = HOSTNAME_COINS+"/chart/" + coins; Map queryParams = new HashMap<>(); @@ -40,7 +40,7 @@ public Map getPricesAtRegularTimeIntervals(String coins, Integer s queryParams.put("searchWidth", searchWidth); String uri = queryParamsToString(baseUrl, queryParams); - return get(uri); + return get(uri, Coins.class); } private static String queryParamsToString(String baseUrl, Map queryParams) { diff --git a/src/main/java/no/jansoren/defillama/ProtocolsClient.java b/src/main/java/no/jansoren/defillama/ProtocolsClient.java index 414f173..610cf9e 100644 --- a/src/main/java/no/jansoren/defillama/ProtocolsClient.java +++ b/src/main/java/no/jansoren/defillama/ProtocolsClient.java @@ -7,6 +7,7 @@ import no.jansoren.defillama.model.protocols.Tvl; import no.jansoren.defillama.model.protocols.TvlItem; +import java.math.BigDecimal; import java.net.http.HttpClient; import java.util.List; @@ -21,7 +22,7 @@ public List getProtocols() { } public Protocol getProtocol(String protocol) { - return get(HOSTNAME_API+"/protocol/"+protocol); + return get(HOSTNAME_API+"/protocol/"+protocol, Protocol.class); } public List getTvlForAllChains() { @@ -32,8 +33,8 @@ public List getTvlForChain(String chain) { return get(HOSTNAME_API+"/v2/historicalChainTvl/"+chain); } - public Double getTvlOfProtocol(String protocol) { - return get(HOSTNAME_API+"/tvl/"+protocol); + public BigDecimal getTvlOfProtocol(String protocol) { + return get(HOSTNAME_API+"/tvl/"+protocol, BigDecimal.class); } public List getTvlForChains() { diff --git a/src/main/java/no/jansoren/defillama/model/protocols/BaseDefiLlamaClient.java b/src/main/java/no/jansoren/defillama/model/protocols/BaseDefiLlamaClient.java index 5fca110..fcce86e 100644 --- a/src/main/java/no/jansoren/defillama/model/protocols/BaseDefiLlamaClient.java +++ b/src/main/java/no/jansoren/defillama/model/protocols/BaseDefiLlamaClient.java @@ -23,9 +23,20 @@ public BaseDefiLlamaClient(HttpClient httpClient, ObjectMapper objectMapper) { this.objectMapper = objectMapper; } + protected T get(String uri, Class clz) { + try { + return httpClient.sendAsync(createGetRequest(uri), HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenApply(json -> fromJson(json, clz)) + .get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } protected T get(String uri) { return get(uri, new TypeReference<>(){}); } + protected T get(String uri, TypeReference valueTypeRef) { try { return httpClient.sendAsync(createGetRequest(uri), HttpResponse.BodyHandlers.ofString()) diff --git a/src/test/java/no/jansoren/defillama/CoinsClientTest.java b/src/test/java/no/jansoren/defillama/CoinsClientTest.java index cfa0fed..3e3eba6 100644 --- a/src/test/java/no/jansoren/defillama/CoinsClientTest.java +++ b/src/test/java/no/jansoren/defillama/CoinsClientTest.java @@ -1,13 +1,12 @@ package no.jansoren.defillama; import com.fasterxml.jackson.databind.ObjectMapper; -import no.jansoren.defillama.model.coins.Coin; +import no.jansoren.defillama.model.coins.Coins; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.net.http.HttpClient; -import java.util.Map; class CoinsClientTest { @@ -24,7 +23,7 @@ void before() { void testGetPricesOfTokensByContractAddress() { var contractAddresses = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8"; var searchWidth = "4h"; - Map coins = client.getPricesOfTokensByContractAddress(contractAddresses, searchWidth); + Coins coins = client.getPricesOfTokensByContractAddress(contractAddresses, searchWidth); Assertions.assertNotNull(coins); } @@ -33,7 +32,7 @@ void testGetHistoricalPricesOfTokensByContractAddress() { var contractAddresses = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8"; var timestamp = 1648680149; var searchWidth = "4h"; - Map coins = client.getHistoricalPricesOfTokensByContractAddress(contractAddresses, timestamp, searchWidth); + Coins coins = client.getHistoricalPricesOfTokensByContractAddress(contractAddresses, timestamp, searchWidth); Assertions.assertNotNull(coins); } @@ -41,14 +40,14 @@ void testGetHistoricalPricesOfTokensByContractAddress() { void testGetHistoricalPricesForMultipleTokens() { var contractAddresses = "{\"avax:0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e\": [1666876743, 1666862343], \"coingecko:ethereum\": [1666869543, 1666862343]}"; var searchWidth = "600"; - Map coins = client.getHistoricalPricesForMultipleTokens(contractAddresses, searchWidth); + Coins coins = client.getHistoricalPricesForMultipleTokens(contractAddresses, searchWidth); Assertions.assertNotNull(coins); } @Test void testGetPricesAtRegularTimeIntervals() { var coinsStr = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8"; - Map coins = client.getPricesAtRegularTimeIntervals(coinsStr, 1664364537, null, 10, "2d", "600"); + Coins coins = client.getPricesAtRegularTimeIntervals(coinsStr, 1664364537, null, 10, "2d", "600"); Assertions.assertNotNull(coins); } diff --git a/src/test/java/no/jansoren/defillama/ProtocolsClientTest.java b/src/test/java/no/jansoren/defillama/ProtocolsClientTest.java index 02dbbbc..fc2c2e5 100644 --- a/src/test/java/no/jansoren/defillama/ProtocolsClientTest.java +++ b/src/test/java/no/jansoren/defillama/ProtocolsClientTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.math.BigDecimal; import java.net.http.HttpClient; import java.util.List; @@ -49,7 +50,7 @@ void testGetTvlForChain() { @Test void testGetTvlOfProtocol() { - Double tvl = client.getTvlOfProtocol("uniswap"); + BigDecimal tvl = client.getTvlOfProtocol("uniswap"); Assertions.assertNotNull(tvl); }