Search in sources :

Example 1 with FeeCategory

use of de.schildbach.wallet.ui.send.FeeCategory 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 2 with FeeCategory

use of de.schildbach.wallet.ui.send.FeeCategory 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)

Aggregations

FeeCategory (de.schildbach.wallet.ui.send.FeeCategory)2 IOException (java.io.IOException)2 Coin (org.bitcoinj.core.Coin)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1