use of org.knowm.xchange.dto.account.Wallet in project XChange by knowm.
the class FtxAdapters method adaptAccountInfo.
public static AccountInfo adaptAccountInfo(FtxResponse<FtxAccountDto> ftxAccountDto, FtxResponse<List<FtxWalletBalanceDto>> ftxBalancesDto) {
List<Balance> ftxAccountInfo = new ArrayList<>();
List<Balance> ftxSpotBalances = new ArrayList<>();
FtxAccountDto result = ftxAccountDto.getResult();
ftxAccountInfo.add(new Balance(Currency.USD, result.getCollateral(), result.getFreeCollateral()));
ftxBalancesDto.getResult().forEach(ftxWalletBalanceDto -> ftxSpotBalances.add(new Balance(ftxWalletBalanceDto.getCoin(), ftxWalletBalanceDto.getTotal(), ftxWalletBalanceDto.getFree())));
BigDecimal totalPositionSize = result.getTotalPositionSize();
BigDecimal totalAccountValue = result.getTotalAccountValue();
BigDecimal currentLeverage = totalPositionSize.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : totalPositionSize.divide(totalAccountValue, 3, RoundingMode.HALF_EVEN);
Wallet accountWallet = Wallet.Builder.from(ftxAccountInfo).features(Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Wallet.WalletFeature.MARGIN_TRADING, Wallet.WalletFeature.MARGIN_FUNDING)))).maxLeverage(result.getLeverage()).currentLeverage(currentLeverage).id("margin").build();
Wallet spotWallet = Wallet.Builder.from(ftxSpotBalances).features(Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Wallet.WalletFeature.FUNDING, Wallet.WalletFeature.TRADING)))).id("spot").build();
return new AccountInfo(result.getUsername(), result.getTakerFee(), Collections.unmodifiableList(Arrays.asList(accountWallet, spotWallet)), Date.from(Instant.now()));
}
use of org.knowm.xchange.dto.account.Wallet in project XChange by knowm.
the class AccountMockedIntegrationTest method accountInfoTest.
@Test
public void accountInfoTest() throws Exception {
stubFor(get(urlPathEqualTo("/payment/balances")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile("balances.json")));
AccountInfo accountInfo = accountService.getAccountInfo();
assertThat(accountInfo).isNotNull();
Wallet wallet = accountInfo.getWallet();
assertThat(wallet).isNotNull();
Balance usdBalance = wallet.getBalance(Currency.USD);
assertThat(usdBalance).isNotNull();
assertThat(usdBalance.getTotal()).isNotNull().isPositive();
assertThat(usdBalance.getAvailable()).isNotNull().isPositive();
}
use of org.knowm.xchange.dto.account.Wallet in project XChange by knowm.
the class LunoAccountService method getAccountInfo.
@Override
public AccountInfo getAccountInfo() throws IOException {
LunoBalance lunoBalance = lunoAPI.balance();
List<Wallet> wallets = new ArrayList<>();
for (org.knowm.xchange.luno.dto.account.LunoBalance.Balance lb : lunoBalance.getBalance()) {
List<Balance> balances = new ArrayList<>();
balances.add(new Balance(LunoUtil.fromLunoCurrency(lb.asset), lb.balance, lb.balance.subtract(lb.reserved)));
wallets.add(Wallet.Builder.from(balances).id(lb.accountId).name(lb.name).build());
}
return new AccountInfo(exchange.getExchangeSpecification().getUserName(), wallets);
}
use of org.knowm.xchange.dto.account.Wallet 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.dto.account.Wallet 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