org.knowm.xchange.dto.account.Wallet Java Examples

The following examples show how to use org.knowm.xchange.dto.account.Wallet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TradingService.java    From arbitrader with MIT License 6 votes vote down vote up
BigDecimal getAccountBalance(Exchange exchange, Currency currency, int scale) throws IOException {
    AccountService accountService = exchange.getAccountService();

    for (Wallet wallet : accountService.getAccountInfo().getWallets().values()) {
        if (wallet.getBalances().containsKey(currency)) {
            return wallet.getBalance(currency).getAvailable()
                    .setScale(scale, RoundingMode.HALF_EVEN);
        }
    }

    LOGGER.error("{}: Unable to fetch {} balance",
        exchange.getExchangeSpecification().getExchangeName(),
        currency.getCurrencyCode());

    return BigDecimal.ZERO;
}
 
Example #2
Source File: HuobiAdapters.java    From zheshiyigeniubidexiangmu with MIT License 6 votes vote down vote up
public static Wallet adaptWallet(Map<String, HuobiBalanceSum> huobiWallet) {
  List<Balance> balances = new ArrayList<>(huobiWallet.size());
  for (Map.Entry<String, HuobiBalanceSum> record : huobiWallet.entrySet()) {
    try {
      Currency currency = adaptCurrency(record.getKey());
      Balance balance =
          new Balance(
              currency,
              record.getValue().getTotal(),
              record.getValue().getAvailable(),
              record.getValue().getFrozen());
      balances.add(balance);
    } catch (ExchangeException e) {
      // It might be a new currency. Ignore the exception and continue with other currency.
    }
  }
  return new Wallet(balances);
}
 
Example #3
Source File: CoinbaseAccountService.java    From zheshiyigeniubidexiangmu with MIT License 6 votes vote down vote up
@Override
public AccountInfo getAccountInfo() throws IOException {
  List<Wallet> wallets = new ArrayList<>();

  List<CoinbaseAccountData.CoinbaseAccount> coinbaseAccounts = getCoinbaseAccounts();
  for (CoinbaseAccountData.CoinbaseAccount coinbaseAccount : coinbaseAccounts) {
    CoinbaseAmount balance = coinbaseAccount.getBalance();
    Wallet wallet =
        new Wallet(
            coinbaseAccount.getId(),
            new Balance(Currency.getInstance(balance.getCurrency()), balance.getAmount()));
    wallets.add(wallet);
  }

  return new AccountInfo(wallets);
}
 
Example #4
Source File: XChangeAccountIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWallets() throws Exception {

    try (CamelContext camelctx = new DefaultCamelContext()) {

        Assume.assumeTrue(checkAPIConnection());

        camelctx.addRoutes(createRouteBuilder());
        camelctx.start();

        Assume.assumeTrue(hasAPICredentials(camelctx));

        ProducerTemplate template = camelctx.createProducerTemplate();
        List<Wallet> wallets = template.requestBody("direct:wallets", null, List.class);
        Assert.assertNotNull("Wallets not null", wallets);
    }
}
 
Example #5
Source File: BitmexAdapters.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
public static Wallet adaptWallet(Map<String, BigDecimal> bitmexWallet) {

    List<Balance> balances = new ArrayList<>(bitmexWallet.size());
    for (Entry<String, BigDecimal> balancePair : bitmexWallet.entrySet()) {
      Currency currency = adaptCurrency(balancePair.getKey());
      Balance balance = new Balance(currency, balancePair.getValue());
      balances.add(balance);
    }
    return new Wallet(balances);
  }
 
Example #6
Source File: BinanceAccountService.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
@Override
public AccountInfo getAccountInfo() throws IOException {
  Long recvWindow =
      (Long) exchange.getExchangeSpecification().getExchangeSpecificParametersItem("recvWindow");
  BinanceAccountInformation acc = super.account(recvWindow, getTimestamp());
  List<Balance> balances =
      acc.balances
          .stream()
          .map(b -> new Balance(b.getCurrency(), b.getTotal(), b.getAvailable()))
          .collect(Collectors.toList());
  return new AccountInfo(new Wallet(balances));
}
 
Example #7
Source File: OkCoinAdapters.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
public static AccountInfo adaptAccountInfoFutures(OkCoinFuturesUserInfoCross futureUserInfo) {
  OkCoinFuturesInfoCross info = futureUserInfo.getInfo();
  OkcoinFuturesFundsCross btcFunds = info.getBtcFunds();
  OkcoinFuturesFundsCross ltcFunds = info.getLtcFunds();
  OkcoinFuturesFundsCross bchFunds = info.getBchFunds();

  Balance btcBalance = new Balance(BTC, btcFunds.getAccountRights());
  Balance ltcBalance = new Balance(LTC, ltcFunds.getAccountRights());
  Balance bchBalance = new Balance(BCH, bchFunds.getAccountRights());

  return new AccountInfo(new Wallet(zeroUsdBalance, btcBalance, ltcBalance, bchBalance));
}
 
Example #8
Source File: CoinbaseAdapters.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
public static AccountInfo adaptAccountInfo(CoinbaseUser user) {

    final String username = user.getEmail();
    final CoinbaseMoney money = user.getBalance();
    final Balance balance =
        new Balance(Currency.getInstance(money.getCurrency()), money.getAmount());

    final AccountInfo accountInfoTemporaryName = new AccountInfo(username, new Wallet(balance));
    return accountInfoTemporaryName;
  }
 
Example #9
Source File: ExchangeBuilder.java    From arbitrader with MIT License 4 votes vote down vote up
public Exchange build() throws IOException {
    Exchange exchange = mock(Exchange.class);
    ExchangeSpecification specification = mock(ExchangeSpecification.class);
    ExchangeConfiguration metadata = new ExchangeConfiguration();
    MarketDataService marketDataService = mock(MarketDataService.class);

    metadata.setHomeCurrency(homeCurrency);
    metadata.setTradingPairs(tradingPairs);
    metadata.setMargin(isMarginSupported);

    when(exchange.getExchangeSpecification()).thenReturn(specification);
    when(specification.getExchangeName()).thenReturn(name);
    when(specification.getExchangeSpecificParametersItem(METADATA_KEY)).thenReturn(metadata);
    when(exchange.getMarketDataService()).thenReturn(marketDataService);

    if (tickerException != null) {
        when(marketDataService.getTicker(any())).thenThrow(tickerException);
        when(marketDataService.getTickers(any(Params.class))).thenThrow(tickerException);
    }

    if (tickers != null && !tickers.isEmpty()) {
        tickers.forEach(ticker -> {
            try {
                when(marketDataService.getTicker(eq(ticker.getCurrencyPair()))).thenReturn(ticker);
            } catch (IOException e) {
                // nothing to do here if we couldn't build the mock
            }
        });

        if (isGetTickersImplemented) {
            when(marketDataService.getTickers(any())).thenReturn(tickers);
        } else {
            when(marketDataService.getTickers(any())).thenThrow(new NotYetImplementedForExchangeException());
        }
    }

    if (bids != null || asks != null) {
        OrderBook orderBook = new OrderBook(
            new Date(),
            generateOrders(currencyPair, Order.OrderType.ASK),
            generateOrders(currencyPair, Order.OrderType.BID)
        );

        when(marketDataService.getOrderBook(eq(currencyPair))).thenReturn(orderBook);
    }

    if (!balances.isEmpty()) {
        Wallet wallet = Wallet.Builder.from(balances).build();
        AccountInfo accountInfo = new AccountInfo(wallet);
        AccountService accountService = mock(AccountService.class);

        when(accountService.getAccountInfo()).thenReturn(accountInfo);
        when(exchange.getAccountService()).thenReturn(accountService);
    }

    if (tickerStrategy != null) {
        when(specification.getExchangeSpecificParametersItem(TICKER_STRATEGY_KEY)).thenReturn(tickerStrategy);
    }

    if (exchangeMetaData != null) {
        when(exchange.getExchangeMetaData()).thenReturn(exchangeMetaData);
    }

    if (tradeService != null) {
        when(exchange.getTradeService()).thenReturn(tradeService);
    }

    return exchange;
}
 
Example #10
Source File: BiboxAdapters.java    From zheshiyigeniubidexiangmu with MIT License 4 votes vote down vote up
public static AccountInfo adaptAccountInfo(List<BiboxCoin> coins) {
  Wallet wallet = adaptWallet(coins);
  return new AccountInfo(wallet);
}
 
Example #11
Source File: BiboxAdapters.java    From zheshiyigeniubidexiangmu with MIT License 4 votes vote down vote up
private static Wallet adaptWallet(List<BiboxCoin> coins) {
  List<Balance> balances =
      coins.stream().map(BiboxAdapters::adaptBalance).collect(Collectors.toList());
  return new Wallet(balances);
}