public Balance build() { if (frozen == null) { if (total == null || available == null) { frozen = BigDecimal.ZERO; } } return new Balance( currency, total, available, frozen, borrowed, loaned, withdrawing, depositing); } }
public static Builder from(Balance balance) { return new Builder() .currency(balance.getCurrency()) .total(balance.getTotal()) .available(balance.getAvailable()) .frozen(balance.getFrozen()) .borrowed(balance.getBorrowed()) .loaned(balance.getLoaned()) .withdrawing(balance.getWithdrawing()) .depositing(balance.getDepositing()); }
/** * Returns the amount of the <code>currency</code> in this balance that may be withdrawn. Equal to * <code>available - borrowed</code>. * * @return the amount that is available to withdraw. */ public BigDecimal getAvailableForWithdrawal() { return getAvailable().subtract(getBorrowed()); }
private static void generic(AccountService accountService) throws IOException { // Get the account information AccountInfo accountInfo = accountService.getAccountInfo(); System.out.println("Account balances: (available / available for withdrawal / total)"); Wallet wallet = accountInfo.getWallet(); Map<Currency, Balance> balances = wallet.getBalances(); for (Map.Entry<Currency, Balance> entry : balances.entrySet()) { Balance balance = entry.getValue(); System.out.format( "%s balance: %s / %s / %s\n", entry.getKey().getCurrencyCode(), balance.getAvailable(), balance.getAvailableForWithdrawal(), balance.getTotal()); } }
public static Wallet adaptWallet(GateioFunds bterAccountInfo) { List<Balance> balances = new ArrayList<>(); for (Entry<String, BigDecimal> funds : bterAccountInfo.getAvailableFunds().entrySet()) { Currency currency = Currency.getInstance(funds.getKey().toUpperCase()); BigDecimal amount = funds.getValue(); BigDecimal locked = bterAccountInfo.getLockedFunds().get(currency.toString()); balances.add(new Balance(currency, null, amount, locked == null ? BigDecimal.ZERO : locked)); } for (Entry<String, BigDecimal> funds : bterAccountInfo.getLockedFunds().entrySet()) { Currency currency = Currency.getInstance(funds.getKey().toUpperCase()); if (balances.stream().noneMatch(balance -> balance.getCurrency().equals(currency))) { BigDecimal amount = funds.getValue(); balances.add(new Balance(currency, null, BigDecimal.ZERO, amount)); } } return new Wallet(balances); }
private static void generic(AccountService accountService) throws IOException { AccountInfo accountInfo = accountService.getAccountInfo(); System.out.println("Wallet: " + accountInfo); System.out.println( "ETH balance: " + accountInfo.getWallet().getBalance(Currency.ETH).getAvailable()); }
/** * Constructs a {@link Wallet}. * * @param id the wallet id * @param name a descriptive name for the wallet * @param balances the balances, the currencies of the balances should not be duplicated. */ public Wallet(String id, String name, Collection<Balance> balances) { this.id = id; if (name == null) { this.name = id; } else { this.name = name; } if (balances.size() == 0) { this.balances = Collections.emptyMap(); } else if (balances.size() == 1) { Balance balance = balances.iterator().next(); this.balances = Collections.singletonMap(balance.getCurrency(), balance); } else { this.balances = new HashMap<>(); for (Balance balance : balances) { if (this.balances.containsKey(balance.getCurrency())) // this class could merge balances, but probably better to catch mistakes and let the // exchange merge them throw new IllegalArgumentException("duplicate balances in wallet"); this.balances.put(balance.getCurrency(), balance); } } }
/** * Returns the amount of the <code>currency</code> in this balance that may be withdrawn. Equal to * <code>available - borrowed</code>. * * @return the amount that is available to withdraw. */ public BigDecimal getAvailableForWithdrawal() { return getAvailable().subtract(getBorrowed()); }
private static void generic(AccountService accountService) throws IOException { // Get the account information AccountInfo accountInfo = accountService.getAccountInfo(); System.out.println("Wallet: " + accountInfo); System.out.println( "BTC balance: " + accountInfo.getWallet().getBalance(Currency.BTC).getAvailable()); }
/** * Constructs a {@link Wallet}. * * @param id the wallet id * @param name a descriptive name for the wallet * @param balances the balances, the currencies of the balances should not be duplicated. */ public Wallet(String id, String name, Collection<Balance> balances) { this.id = id; if (name == null) { this.name = id; } else { this.name = name; } if (balances.size() == 0) { this.balances = Collections.emptyMap(); } else if (balances.size() == 1) { Balance balance = balances.iterator().next(); this.balances = Collections.singletonMap(balance.getCurrency(), balance); } else { this.balances = new HashMap<>(); for (Balance balance : balances) { if (this.balances.containsKey(balance.getCurrency())) // this class could merge balances, but probably better to catch mistakes and let the // exchange merge them throw new IllegalArgumentException("duplicate balances in wallet"); this.balances.put(balance.getCurrency(), balance); } } }
/** * Returns a zero balance. * * @param currency the balance currency. * @return a zero balance. */ public static Balance zero(Currency currency) { return new Balance( currency, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); }
public static Builder from(Balance balance) { return new Builder() .currency(balance.getCurrency()) .total(balance.getTotal()) .available(balance.getAvailable()) .frozen(balance.getFrozen()) .borrowed(balance.getBorrowed()) .loaned(balance.getLoaned()) .withdrawing(balance.getWithdrawing()) .depositing(balance.getDepositing()); }
private static void generic(AccountService accountService) throws IOException { AccountInfo accountInfo = accountService.getAccountInfo(); System.out.println("Wallet: " + accountInfo); System.out.println( "ETH balance: " + accountInfo.getWallet().getBalance(Currency.ETH).getAvailable()); }
public static Balance adaptBalance( Map<String, String> balances, Map<String, String> reserved, String ccy) { Currency currency = Currency.getInstance(ccy); BigDecimal available = new BigDecimal(balances.get(ccy)); BigDecimal frozen = new BigDecimal(reserved.get(ccy)); return new Balance(currency, available.add(frozen), available, frozen); }
private Balance mapBalance(AcxAccount acc) { return new Balance( Currency.getInstance(acc.currency), acc.balance.add(acc.locked), acc.balance, acc.locked); }
public static Wallet adaptWallet(Map<String, BigDecimal> krakenWallet) { List<Balance> balances = new ArrayList<>(krakenWallet.size()); for (Entry<String, BigDecimal> balancePair : krakenWallet.entrySet()) { Currency currency = adaptCurrency(balancePair.getKey()); Balance balance = new Balance(currency, balancePair.getValue()); balances.add(balance); } return new Wallet(balances); }
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); }
public static List<Balance> adaptPoloniexBalances( HashMap<String, PoloniexBalance> poloniexBalances) { List<Balance> balances = new ArrayList<>(); for (Map.Entry<String, PoloniexBalance> item : poloniexBalances.entrySet()) { Currency currency = Currency.getInstance(item.getKey()); balances.add( new Balance( currency, null, item.getValue().getAvailable(), item.getValue().getOnOrders())); } return balances; }
public Balance getBalance(Currency currency) { if (currency.equals(Currency.XBT)) { return new Balance(currency, btcBalance, btcAvailable, btcReserved); } else if (currency.equals(Currency.GBP)) { return new Balance(currency, gbpBalance, gbpAvailable, gbpReserved); } else if (currency.equals(Currency.EUR)) { return new Balance(currency, eurBalance, eurAvailable, eurReserved); } else if (currency.equals(Currency.USD)) { return new Balance(currency, usdBalance, usdAvailable, usdReserved); } else if (currency.equals(Currency.BCH)) { return new Balance(currency, bchBalance, bchAvailable, bchReserved); } else if (currency.equals(Currency.XRP)) { return new Balance(currency, xrpBalance, xrpAvailable, xrpReserved); } else if (currency.equals(Currency.LTC)) { return new Balance(currency, ltcBalance, ltcAvailable, ltcReserved); } else if (currency.equals(Currency.ETH)) { return new Balance(currency, ethBalance, ethAvailable, ethReserved); } else { throw new IllegalArgumentException("Unsupported currency: " + currency); } } }
public static Wallet adaptWallet(WexAccountInfo wexAccountInfo) { List<Balance> balances = new ArrayList<>(); Map<String, BigDecimal> funds = wexAccountInfo.getFunds(); for (String lcCurrency : funds.keySet()) { /* BTC-E signals DASH as DSH. This is a different coin. Translate in correct DASH name */ BigDecimal fund = funds.get(lcCurrency); Currency currency = adaptCurrencyIn(lcCurrency); balances.add(new Balance(currency, fund)); } return new Wallet(balances); }