Search in sources :

Example 1 with BitfinexBalancesResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse in project XChange by knowm.

the class BitfinexAdapters method adaptWallets.

public static List<Wallet> adaptWallets(BitfinexBalancesResponse[] response) {
    Map<String, Map<String, BigDecimal[]>> walletsBalancesMap = new HashMap<>();
    // each of those may be partially frozen/available
    for (BitfinexBalancesResponse balance : response) {
        String walletId = balance.getType();
        if (!walletsBalancesMap.containsKey(walletId)) {
            walletsBalancesMap.put(walletId, new HashMap<>());
        }
        Map<String, BigDecimal[]> balancesByCurrency = // {total, available}
        walletsBalancesMap.get(walletId);
        String currencyName = adaptBitfinexCurrency(balance.getCurrency());
        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<Wallet> wallets = new ArrayList<>();
    for (Entry<String, Map<String, BigDecimal[]>> walletData : walletsBalancesMap.entrySet()) {
        Map<String, BigDecimal[]> balancesByCurrency = walletData.getValue();
        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));
        }
        wallets.add(Wallet.Builder.from(balances).id(walletData.getKey()).build());
    }
    return wallets;
}
Also used : HashMap(java.util.HashMap) Wallet(org.knowm.xchange.dto.account.Wallet) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) BitfinexBalancesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse) Map(java.util.Map) HashMap(java.util.HashMap) Balance(org.knowm.xchange.dto.account.Balance)

Example 2 with BitfinexBalancesResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse in project XChange by knowm.

the class BitfinexAdaptersTest method shouldAdaptBalances.

@Test
public void shouldAdaptBalances() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = BitfinexWalletJSONTest.class.getResourceAsStream("/org/knowm/xchange/bitfinex/v1/dto/account/example-account-info-balance.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    BitfinexBalancesResponse[] response = mapper.readValue(is, BitfinexBalancesResponse[].class);
    List<Wallet> wallets = BitfinexAdapters.adaptWallets(response);
    Wallet exchangeWallet = wallets.stream().filter(wallet -> "exchange".equals(wallet.getId())).findFirst().orElse(null);
    assertNotNull("Exchange wallet is missing", exchangeWallet);
    Wallet tradingWallet = wallets.stream().filter(wallet -> "trading".equals(wallet.getId())).findFirst().orElse(null);
    assertNotNull("Trading wallet is missing", tradingWallet);
    Wallet depositWallet = wallets.stream().filter(wallet -> "deposit".equals(wallet.getId())).findFirst().orElse(null);
    assertNotNull("Deposit wallet is missing", depositWallet);
    Balance tradingUsdBalance = tradingWallet.getBalance(Currency.USD);
    assertNotNull(tradingUsdBalance);
    assertEquals(new BigDecimal("100"), tradingUsdBalance.getTotal());
    assertEquals(new BigDecimal("50"), tradingUsdBalance.getAvailable());
    Balance tradingBtcBalance = tradingWallet.getBalance(Currency.BTC);
    assertNotNull(tradingBtcBalance);
    assertEquals(BigDecimal.ZERO, tradingBtcBalance.getTotal());
    assertEquals(BigDecimal.ZERO, tradingBtcBalance.getAvailable());
    Balance exchangeUsdBalance = exchangeWallet.getBalance(Currency.USD);
    assertNotNull(exchangeUsdBalance);
    assertEquals(new BigDecimal("5.5"), exchangeUsdBalance.getTotal());
    assertEquals(new BigDecimal("5.5"), exchangeUsdBalance.getAvailable());
    Balance exchangeBtcBalance = exchangeWallet.getBalance(Currency.BTC);
    assertNotNull(exchangeBtcBalance);
    assertEquals(BigDecimal.ZERO, exchangeBtcBalance.getTotal());
    assertEquals(BigDecimal.ZERO, exchangeBtcBalance.getAvailable());
    Balance depositUsdBalance = depositWallet.getBalance(Currency.USD);
    assertNotNull(depositUsdBalance);
    assertEquals(new BigDecimal("69"), depositUsdBalance.getTotal());
    assertEquals(new BigDecimal("42"), depositUsdBalance.getAvailable());
    Balance depositBtcBalance = depositWallet.getBalance(Currency.BTC);
    assertNotNull(depositBtcBalance);
    assertEquals(new BigDecimal("50"), depositBtcBalance.getTotal());
    assertEquals(new BigDecimal("30"), depositBtcBalance.getAvailable());
}
Also used : InputStream(java.io.InputStream) Wallet(org.knowm.xchange.dto.account.Wallet) BitfinexBalancesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse) Balance(org.knowm.xchange.dto.account.Balance) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) BitfinexFeesJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest) BitfinexWalletJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest) Test(org.junit.Test)

Aggregations

BigDecimal (java.math.BigDecimal)2 BitfinexBalancesResponse (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse)2 Balance (org.knowm.xchange.dto.account.Balance)2 Wallet (org.knowm.xchange.dto.account.Wallet)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 BitfinexFeesJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest)1 BitfinexWalletJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest)1