Skip to content

Commit

Permalink
getPercentageChangeInPriceOverTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jansoren committed Sep 30, 2023
1 parent bc918f1 commit ec1b525
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
29 changes: 3 additions & 26 deletions src/main/java/no/jansoren/defillama/CoinsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import no.jansoren.defillama.model.coins.Coins;
import no.jansoren.defillama.model.coins.CoinsPercent;
import no.jansoren.defillama.model.protocols.BaseDefiLlamaClient;

import java.net.URLEncoder;
Expand Down Expand Up @@ -43,31 +44,7 @@ public Coins getPricesAtRegularTimeIntervals(String coins, Integer start, Intege
return get(uri, Coins.class);
}

private static String queryParamsToString(String baseUrl, Map<String, Object> queryParams) {
StringBuilder urlBuilder = new StringBuilder(baseUrl);
boolean isFirstParam = true;

for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
var key = entry.getKey();
var value = toString(entry.getValue());

if (value != null) {
if (isFirstParam) {
urlBuilder.append("?");
isFirstParam = false;
} else {
urlBuilder.append("&");
}
urlBuilder.append(key).append("=").append(value);
}
}
return urlBuilder.toString();
public CoinsPercent getPercentageChangeInPriceOverTime(String coins, int timestamp, boolean lookForward, String period) {
return get(HOSTNAME_COINS+"/percentage/"+coins+"?timestamp="+timestamp+"&lookForward="+lookForward+"&period="+period, CoinsPercent.class);
}

private static String toString(Object value) {
if(value != null)
return value.toString();
return null;
}

}
12 changes: 12 additions & 0 deletions src/main/java/no/jansoren/defillama/model/coins/CoinsPercent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package no.jansoren.defillama.model.coins;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

public record CoinsPercent(

@JsonProperty("coins")
Map<String, Double> coins
){
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class BaseDefiLlamaClient {
Expand Down Expand Up @@ -48,6 +49,33 @@ protected <T> T get(String uri, TypeReference<T> valueTypeRef) {
}
}

protected static String queryParamsToString(String baseUrl, Map<String, Object> queryParams) {
StringBuilder urlBuilder = new StringBuilder(baseUrl);
boolean isFirstParam = true;

for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
var key = entry.getKey();
var value = toString(entry.getValue());

if (value != null) {
if (isFirstParam) {
urlBuilder.append("?");
isFirstParam = false;
} else {
urlBuilder.append("&");
}
urlBuilder.append(key).append("=").append(value);
}
}
return urlBuilder.toString();
}

private static String toString(Object value) {
if(value != null)
return value.toString();
return null;
}

private HttpRequest createGetRequest(String uri) {
return HttpRequest.newBuilder()
.uri(createUri(uri))
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/no/jansoren/defillama/CoinsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import no.jansoren.defillama.model.coins.Coins;
import no.jansoren.defillama.model.coins.CoinsPercent;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,4 +52,11 @@ void testGetPricesAtRegularTimeIntervals() {
Assertions.assertNotNull(coins);
}

@Test
void testGetPercentageChangeInPriceOverTime(){
var coinsStr = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8";
CoinsPercent coins = client.getPercentageChangeInPriceOverTime(coinsStr, 1664364537, false, "3w");
Assertions.assertNotNull(coins);
}

}

0 comments on commit ec1b525

Please sign in to comment.