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;
}
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());
}
Aggregations