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