Skip to content

Commit

Permalink
Cleaning up code and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jansoren committed Sep 30, 2023
1 parent 597a712 commit bc918f1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/main/java/no/jansoren/defillama/CoinsClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,20 +16,20 @@ public CoinsClient(HttpClient httpClient, ObjectMapper objectMapper) {
super(httpClient, objectMapper);
}

public Map<String, Coin> 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<String, Coin> 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<String, Coin> 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<String, Coin> 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<String, Object> queryParams = new HashMap<>();
Expand All @@ -40,7 +40,7 @@ public Map<String, Coin> 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<String, Object> queryParams) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/no/jansoren/defillama/ProtocolsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +22,7 @@ public List<ProtocolItem> getProtocols() {
}

public Protocol getProtocol(String protocol) {
return get(HOSTNAME_API+"/protocol/"+protocol);
return get(HOSTNAME_API+"/protocol/"+protocol, Protocol.class);
}

public List<Tvl> getTvlForAllChains() {
Expand All @@ -32,8 +33,8 @@ public List<Tvl> 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<TvlItem> getTvlForChains() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ public BaseDefiLlamaClient(HttpClient httpClient, ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

protected <T> T get(String uri, Class<T> 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> T get(String uri) {
return get(uri, new TypeReference<>(){});
}

protected <T> T get(String uri, TypeReference<T> valueTypeRef) {
try {
return httpClient.sendAsync(createGetRequest(uri), HttpResponse.BodyHandlers.ofString())
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/no/jansoren/defillama/CoinsClientTest.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -24,7 +23,7 @@ void before() {
void testGetPricesOfTokensByContractAddress() {
var contractAddresses = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8";
var searchWidth = "4h";
Map<String, Coin> coins = client.getPricesOfTokensByContractAddress(contractAddresses, searchWidth);
Coins coins = client.getPricesOfTokensByContractAddress(contractAddresses, searchWidth);
Assertions.assertNotNull(coins);
}

Expand All @@ -33,22 +32,22 @@ void testGetHistoricalPricesOfTokensByContractAddress() {
var contractAddresses = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8";
var timestamp = 1648680149;
var searchWidth = "4h";
Map<String, Coin> coins = client.getHistoricalPricesOfTokensByContractAddress(contractAddresses, timestamp, searchWidth);
Coins coins = client.getHistoricalPricesOfTokensByContractAddress(contractAddresses, timestamp, searchWidth);
Assertions.assertNotNull(coins);
}

@Test
void testGetHistoricalPricesForMultipleTokens() {
var contractAddresses = "{\"avax:0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e\": [1666876743, 1666862343], \"coingecko:ethereum\": [1666869543, 1666862343]}";
var searchWidth = "600";
Map<String, Coin> 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<String, Coin> coins = client.getPricesAtRegularTimeIntervals(coinsStr, 1664364537, null, 10, "2d", "600");
Coins coins = client.getPricesAtRegularTimeIntervals(coinsStr, 1664364537, null, 10, "2d", "600");
Assertions.assertNotNull(coins);
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/no/jansoren/defillama/ProtocolsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,7 +50,7 @@ void testGetTvlForChain() {

@Test
void testGetTvlOfProtocol() {
Double tvl = client.getTvlOfProtocol("uniswap");
BigDecimal tvl = client.getTvlOfProtocol("uniswap");
Assertions.assertNotNull(tvl);
}

Expand Down

0 comments on commit bc918f1

Please sign in to comment.