Search in sources :

Example 11 with Wallet

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()));
}
Also used : Wallet(org.knowm.xchange.dto.account.Wallet) ArrayList(java.util.ArrayList) FtxAccountDto(org.knowm.xchange.ftx.dto.account.FtxAccountDto) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) AccountInfo(org.knowm.xchange.dto.account.AccountInfo) HashSet(java.util.HashSet)

Example 12 with Wallet

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();
}
Also used : Wallet(org.knowm.xchange.dto.account.Wallet) Balance(org.knowm.xchange.dto.account.Balance) AccountInfo(org.knowm.xchange.dto.account.AccountInfo) Test(org.junit.Test)

Example 13 with Wallet

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);
}
Also used : LunoBalance(org.knowm.xchange.luno.dto.account.LunoBalance) Wallet(org.knowm.xchange.dto.account.Wallet) ArrayList(java.util.ArrayList) Exchange(org.knowm.xchange.Exchange) Balance(org.knowm.xchange.dto.account.Balance) LunoBalance(org.knowm.xchange.luno.dto.account.LunoBalance) AccountInfo(org.knowm.xchange.dto.account.AccountInfo)

Example 14 with Wallet

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;
}
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 15 with Wallet

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

Wallet (org.knowm.xchange.dto.account.Wallet)47 Balance (org.knowm.xchange.dto.account.Balance)36 AccountInfo (org.knowm.xchange.dto.account.AccountInfo)34 BigDecimal (java.math.BigDecimal)29 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 Date (java.util.Date)10 Currency (org.knowm.xchange.currency.Currency)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)5 List (java.util.List)4 Map (java.util.Map)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 IOException (java.io.IOException)3 Arrays (java.util.Arrays)3 FileNotFoundException (java.io.FileNotFoundException)2 ZERO (java.math.BigDecimal.ZERO)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2