Search in sources :

Example 1 with GeminiBalancesResponse

use of org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse in project XChange by knowm.

the class GeminiAccountServiceRaw method getGeminiAccountInfo.

public GeminiBalancesResponse[] getGeminiAccountInfo() throws IOException {
    try {
        GeminiBalancesRequest request = new GeminiBalancesRequest(String.valueOf(exchange.getNonceFactory().createValue()));
        GeminiBalancesResponse[] balances = gemini.balances(apiKey, payloadCreator, signatureCreator, request);
        return balances;
    } catch (GeminiException e) {
        throw handleException(e);
    }
}
Also used : GeminiBalancesRequest(org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesRequest) GeminiBalancesResponse(org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse) GeminiException(org.knowm.xchange.gemini.v1.dto.GeminiException)

Example 2 with GeminiBalancesResponse

use of org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse in project XChange by knowm.

the class GeminiAdapters method adaptWallet.

public static Wallet adaptWallet(GeminiBalancesResponse[] response) {
    // {total, available}
    Map<String, BigDecimal[]> balancesByCurrency = new HashMap<>();
    // each of those may be partially frozen/available
    for (GeminiBalancesResponse balance : response) {
        String currencyName = balance.getCurrency().toUpperCase();
        BigDecimal[] balanceDetail = balancesByCurrency.get(currencyName);
        if (balanceDetail == null) {
            balanceDetail = new BigDecimal[] { balance.getAmount(), balance.getAvailable() };
        } else {
            balanceDetail[0] = balanceDetail[0].add(balance.getAmount());
            balanceDetail[1] = balanceDetail[1].add(balance.getAvailable());
        }
        balancesByCurrency.put(currencyName, balanceDetail);
    }
    List<Balance> balances = new ArrayList<>(balancesByCurrency.size());
    for (Entry<String, BigDecimal[]> entry : balancesByCurrency.entrySet()) {
        String currencyName = entry.getKey();
        BigDecimal[] balanceDetail = entry.getValue();
        BigDecimal balanceTotal = balanceDetail[0];
        BigDecimal balanceAvailable = balanceDetail[1];
        balances.add(new Balance(Currency.getInstance(currencyName), balanceTotal, balanceAvailable));
    }
    return Wallet.Builder.from(balances).build();
}
Also used : GeminiBalancesResponse(org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal)

Example 3 with GeminiBalancesResponse

use of org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse in project XChange by knowm.

the class GeminiAdaptersTest method shouldAdaptBalances.

@Test
public void shouldAdaptBalances() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = GeminiWalletJSONTest.class.getResourceAsStream("/org/knowm/xchange/gemini/v1/account/example-account-info-balance.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    GeminiBalancesResponse[] response = mapper.readValue(is, GeminiBalancesResponse[].class);
    Wallet wallet = GeminiAdapters.adaptWallet(response);
    assertEquals(2, wallet.getBalances().size());
    assertEquals(new BigDecimal("105.5"), wallet.getBalance(Currency.USD).getTotal());
    assertEquals(new BigDecimal("55.5"), wallet.getBalance(Currency.USD).getAvailable());
    assertEquals(new BigDecimal("50"), wallet.getBalance(Currency.BTC).getTotal());
    assertEquals(new BigDecimal("30"), wallet.getBalance(Currency.BTC).getAvailable());
}
Also used : GeminiBalancesResponse(org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse) InputStream(java.io.InputStream) Wallet(org.knowm.xchange.dto.account.Wallet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) GeminiWalletJSONTest(org.knowm.xchange.gemini.v1.dto.account.GeminiWalletJSONTest)

Aggregations

GeminiBalancesResponse (org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesResponse)3 BigDecimal (java.math.BigDecimal)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Balance (org.knowm.xchange.dto.account.Balance)1 Wallet (org.knowm.xchange.dto.account.Wallet)1 GeminiException (org.knowm.xchange.gemini.v1.dto.GeminiException)1 GeminiBalancesRequest (org.knowm.xchange.gemini.v1.dto.account.GeminiBalancesRequest)1 GeminiWalletJSONTest (org.knowm.xchange.gemini.v1.dto.account.GeminiWalletJSONTest)1