Skip to content

Commit

Permalink
getPricesOfTokensByContractAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
jansoren committed Sep 20, 2023
1 parent ba1ba2c commit b2c0a46
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/no/jansoren/defillama/CoinsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package no.jansoren.defillama;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import no.jansoren.defillama.model.coins.Coins;
import no.jansoren.defillama.model.protocols.BaseClient;

import java.net.http.HttpClient;

public class CoinsClient extends BaseClient {

public CoinsClient(HttpClient httpClient, ObjectMapper objectMapper) {
super(httpClient, objectMapper);
}

public Coins getPricesOfTokensByContractAddress(String coins, String searchWidth) {
return get("https://coins.llama.fi/prices/current/"+coins+"?searchWidth="+searchWidth, new TypeReference<>(){});
}
}
24 changes: 24 additions & 0 deletions src/main/java/no/jansoren/defillama/model/coins/Coin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.jansoren.defillama.model.coins;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;

public record Coin(

@JsonProperty("symbol")
String symbol,

@JsonProperty("price")
BigDecimal price,

@JsonProperty("decimals")
Integer decimals,

@JsonProperty("confidence")
BigDecimal confidence,

@JsonProperty("timestamp")
Integer timestamp
) {
}
12 changes: 12 additions & 0 deletions src/main/java/no/jansoren/defillama/model/coins/Coins.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 Coins(

@JsonProperty("coins")
Map<String, Coin> coins
){
}
30 changes: 30 additions & 0 deletions src/test/java/no/jansoren/defillama/CoinsClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package no.jansoren.defillama;

import com.fasterxml.jackson.databind.ObjectMapper;
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;

public class CoinsClientTest {

private CoinsClient client;

@BeforeEach
void before() {
var httpClient = HttpClient.newBuilder().build();
var objectMapper = new ObjectMapper();
client = new CoinsClient(httpClient, objectMapper);
}

@Test
void testGetPricesOfTokensByContractAddress() {
var contractAddresses = "ethereum:0xdF574c24545E5FfEcb9a659c229253D4111d87e1,coingecko:ethereum,bsc:0x762539b45a1dcce3d36d080f74d1aed37844b878,ethereum:0xdB25f211AB05b1c97D595516F45794528a807ad8";
var searchWidth = "4h";
Coins coins = client.getPricesOfTokensByContractAddress(contractAddresses, searchWidth);
Assertions.assertNotNull(coins);
}

}

0 comments on commit b2c0a46

Please sign in to comment.