Search in sources :

Example 51 with Coin

use of org.bitcoinj.core.Coin in project bitcoin-wallet by bitcoin-wallet.

the class SweepWalletFragment method handleSweep.

private void handleSweep() {
    setState(State.PREPARATION);
    final SendRequest sendRequest = SendRequest.emptyWallet(application.getWallet().freshReceiveAddress());
    sendRequest.feePerKb = fees.get(FeeCategory.NORMAL);
    new SendCoinsOfflineTask(walletToSweep, backgroundHandler) {

        @Override
        protected void onSuccess(final Transaction transaction) {
            sentTransaction = transaction;
            setState(State.SENDING);
            sentTransaction.getConfidence().addEventListener(sentTransactionConfidenceListener);
            application.processDirectTransaction(sentTransaction);
        }

        @Override
        protected void onInsufficientMoney(@Nullable final Coin missing) {
            setState(State.FAILED);
            showInsufficientMoneyDialog();
        }

        @Override
        protected void onEmptyWalletFailed() {
            setState(State.FAILED);
            showInsufficientMoneyDialog();
        }

        @Override
        protected void onFailure(final Exception exception) {
            setState(State.FAILED);
            final DialogBuilder dialog = DialogBuilder.warn(activity, R.string.send_coins_error_msg);
            dialog.setMessage(exception.toString());
            dialog.setNeutralButton(R.string.button_dismiss, null);
            dialog.show();
        }

        @Override
        protected void onInvalidEncryptionKey() {
            // cannot happen
            throw new RuntimeException();
        }

        private void showInsufficientMoneyDialog() {
            final DialogBuilder dialog = DialogBuilder.warn(activity, R.string.sweep_wallet_fragment_insufficient_money_title);
            dialog.setMessage(R.string.sweep_wallet_fragment_insufficient_money_msg);
            dialog.setNeutralButton(R.string.button_dismiss, null);
            dialog.show();
        }
    }.sendCoinsOffline(// send asynchronously
    sendRequest);
}
Also used : Coin(org.bitcoinj.core.Coin) SendRequest(org.bitcoinj.wallet.SendRequest) Transaction(org.bitcoinj.core.Transaction) WalletTransaction(org.bitcoinj.wallet.WalletTransaction) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder) VerificationException(org.bitcoinj.core.VerificationException)

Example 52 with Coin

use of org.bitcoinj.core.Coin in project bitcoin-wallet by bitcoin-wallet.

the class WalletBalanceWidgetProvider method onAppWidgetOptionsChanged.

@Override
public void onAppWidgetOptionsChanged(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId, final Bundle newOptions) {
    if (newOptions != null)
        log.info("app widget {} options changed: minWidth={}", appWidgetId, newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH));
    final WalletApplication application = (WalletApplication) context.getApplicationContext();
    final Coin balance = application.getWallet().getBalance(BalanceType.ESTIMATED);
    updateWidget(context, appWidgetManager, appWidgetId, newOptions, balance);
}
Also used : Coin(org.bitcoinj.core.Coin)

Example 53 with Coin

use of org.bitcoinj.core.Coin in project bitcoin-wallet by bitcoin-wallet.

the class DynamicFeeLoader method loadInBackground.

@Override
public Map<FeeCategory, Coin> loadInBackground() {
    try {
        final Map<FeeCategory, Coin> staticFees = parseFees(assets.open(Constants.Files.FEES_FILENAME));
        final File dynamicFeesFile = new File(getContext().getFilesDir(), Constants.Files.FEES_FILENAME);
        final File tempFile = new File(getContext().getCacheDir(), Constants.Files.FEES_FILENAME + ".temp");
        fetchDynamicFees(dynamicFeesUrl, tempFile, dynamicFeesFile, userAgent);
        if (!dynamicFeesFile.exists())
            return staticFees;
        // Check dynamic fees for sanity, based on the hardcoded fees.
        // The bounds are as follows (h is the respective hardcoded fee):
        // ECONOMIC: h/8 to h*4
        // NORMAL:   h/4 to h*4
        // PRIORITY: h/4 to h*8
        final Map<FeeCategory, Coin> dynamicFees = parseFees(new FileInputStream(dynamicFeesFile));
        for (final FeeCategory category : FeeCategory.values()) {
            final Coin staticFee = staticFees.get(category);
            final Coin dynamicFee = dynamicFees.get(category);
            if (dynamicFee == null) {
                dynamicFees.put(category, staticFee);
                log.warn("Dynamic fee category missing, using static: category {}, {}/kB", category, staticFee.toFriendlyString());
                continue;
            }
            final Coin upperBound = staticFee.shiftLeft(category == FeeCategory.PRIORITY ? 3 : 2);
            if (dynamicFee.isGreaterThan(upperBound)) {
                dynamicFees.put(category, upperBound);
                log.warn("Down-adjusting dynamic fee: category {} from {}/kB to {}/kB", category, dynamicFee.toFriendlyString(), upperBound.toFriendlyString());
                continue;
            }
            final Coin lowerBound = staticFee.shiftRight(category == FeeCategory.ECONOMIC ? 3 : 2);
            if (dynamicFee.isLessThan(lowerBound)) {
                dynamicFees.put(category, lowerBound);
                log.warn("Up-adjusting dynamic fee: category {} from {}/kB to {}/kB", category, dynamicFee.toFriendlyString(), lowerBound.toFriendlyString());
            }
        }
        return dynamicFees;
    } catch (final IOException x) {
        // Should not happen
        throw new RuntimeException(x);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) FeeCategory(de.schildbach.wallet.ui.send.FeeCategory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 54 with Coin

use of org.bitcoinj.core.Coin in project bitcoin-wallet by bitcoin-wallet.

the class DynamicFeeLoader method parseFees.

private static Map<FeeCategory, Coin> parseFees(final InputStream is) throws IOException {
    final Map<FeeCategory, Coin> dynamicFees = new HashMap<FeeCategory, Coin>();
    String line = null;
    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
        while (true) {
            line = reader.readLine();
            if (line == null)
                break;
            line = line.trim();
            if (line.length() == 0 || line.charAt(0) == '#')
                continue;
            final String[] fields = line.split("=");
            try {
                final FeeCategory category = FeeCategory.valueOf(fields[0]);
                final Coin rate = Coin.valueOf(Long.parseLong(fields[1]));
                dynamicFees.put(category, rate);
            } catch (IllegalArgumentException x) {
                log.warn("Cannot parse line, ignoring: '" + line + "'", x);
            }
        }
    } catch (final Exception x) {
        throw new RuntimeException("Error while parsing: '" + line + "'", x);
    } finally {
        is.close();
    }
    return dynamicFees;
}
Also used : Coin(org.bitcoinj.core.Coin) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) BufferedReader(java.io.BufferedReader) FeeCategory(de.schildbach.wallet.ui.send.FeeCategory) IOException(java.io.IOException)

Example 55 with Coin

use of org.bitcoinj.core.Coin in project bitcoin-wallet by bitcoin-wallet.

the class ExchangeRatesProvider method getExchangeRate.

public static ExchangeRate getExchangeRate(final Cursor cursor) {
    final String currencyCode = cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_CODE));
    final Coin rateCoin = Coin.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_COIN)));
    final Fiat rateFiat = Fiat.valueOf(currencyCode, cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_FIAT)));
    final String source = cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_SOURCE));
    return new ExchangeRate(new org.bitcoinj.utils.ExchangeRate(rateCoin, rateFiat), source);
}
Also used : Coin(org.bitcoinj.core.Coin) Fiat(org.bitcoinj.utils.Fiat)

Aggregations

Coin (org.bitcoinj.core.Coin)64 Transaction (org.bitcoinj.core.Transaction)16 AddressEntry (io.bitsquare.btc.AddressEntry)13 WalletService (io.bitsquare.btc.WalletService)13 Address (org.bitcoinj.core.Address)10 Popup (io.bitsquare.gui.main.overlays.popups.Popup)8 Inject (com.google.inject.Inject)7 Nullable (javax.annotation.Nullable)7 BalanceListener (io.bitsquare.btc.listeners.BalanceListener)6 Collectors (java.util.stream.Collectors)6 FXCollections (javafx.collections.FXCollections)6 ListChangeListener (javafx.collections.ListChangeListener)6 ObservableList (javafx.collections.ObservableList)6 BSFormatter (io.bitsquare.gui.util.BSFormatter)5 Offer (io.bitsquare.trade.offer.Offer)5 AddressEntry (io.bisq.core.btc.AddressEntry)4 Trade (io.bisq.core.trade.Trade)4 UserThread (io.bitsquare.common.UserThread)4 PaymentAccountUtil.isPaymentAccountValidForOffer (io.bisq.core.payment.PaymentAccountUtil.isPaymentAccountValidForOffer)3 DevFlags (io.bitsquare.app.DevFlags)3