Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to more accurate fee estimation endpoint #4235

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void initialize() {
"Poloniex (https://poloniex.com)",
"Coinmarketcap (https://coinmarketcap.com)"));
if (isBtc)
addCompactTopLabelTextField(root, ++gridRow, Res.get("setting.about.feeEstimation.label"), "Earn.com (https://bitcoinfees.earn.com)");
addCompactTopLabelTextField(root, ++gridRow, Res.get("setting.about.feeEstimation.label"), "mempool.space (https://mempool.space)");

addTitledGroupBg(root, ++gridRow, 2, Res.get("setting.about.versionDetails"), Layout.GROUP_DISTANCE);
addCompactTopLabelTextField(root, gridRow, Res.get("setting.about.version"), Version.VERSION, Layout.TWICE_FIRST_ROW_AND_GROUP_DISTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@
import java.time.Duration;
import java.time.Instant;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

@Component
class BitcoinFeeRateProvider extends FeeRateProvider {

private static final long MIN_FEE_RATE = 10; // satoshi/byte
private static final long MAX_FEE_RATE = 1000;
static final long MIN_FEE_RATE = 10; // satoshi/byte
static final long MAX_FEE_RATE = 1000;

private static final int DEFAULT_MAX_BLOCKS = 2;
private static final int DEFAULT_REFRESH_INTERVAL = 2;

private final RestTemplate restTemplate = new RestTemplate();

// TODO: As of the switch to the mempool.space API this field and related members are
// now dead code and should be removed, including removing the positional
// command-line argument from startup scripts. Operators need to be notified of this
// when it happens.
private final int maxBlocks;

public BitcoinFeeRateProvider(Environment env) {
Expand All @@ -63,9 +66,9 @@ protected FeeRate doGet() {

private long getEstimatedFeeRate() {
return getFeeRatePredictions()
.filter(p -> p.get("maxDelay") <= maxBlocks)
.filter(p -> p.getKey().equalsIgnoreCase("halfHourFee"))
.map(Map.Entry::getValue)
.findFirst()
.map(p -> p.get("maxFee"))
.map(r -> {
log.info("latest fee rate prediction is {} sat/byte", r);
return r;
Expand All @@ -75,19 +78,19 @@ private long getEstimatedFeeRate() {
.orElse(MIN_FEE_RATE);
}

private Stream<Map<String, Long>> getFeeRatePredictions() {
private Stream<Map.Entry<String, Long>> getFeeRatePredictions() {
return restTemplate.exchange(
RequestEntity
.get(UriComponentsBuilder
// now using /fees/list because /fees/recommended estimates were too high
.fromUriString("https://bitcoinfees.earn.com/api/v1/fees/list")
// Temporarily call mempool.space centralized API endpoint as an
// alternative to the too-expensive bitcoinfees.earn.com until a more
// decentralized solution is available as per
// https://github.com/bisq-network/projects/issues/27
.fromUriString("https://mempool.space/api/v1/fees/recommended")
.build().toUri())
.header("User-Agent", "") // required to avoid 403
.build(),
new ParameterizedTypeReference<Map<String, List<Map<String, Long>>>>() {
}
).getBody().entrySet().stream()
.flatMap(e -> e.getValue().stream());
new ParameterizedTypeReference<Map<String, Long>>() { }
).getBody().entrySet().stream();
}

private static Optional<String[]> args(Environment env) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.price.mining.providers;

import bisq.price.mining.FeeRate;

import org.springframework.context.support.GenericXmlApplicationContext;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class BitcoinFeeRateProviderTest {

@Test
public void doGet_successfulCall() {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
BitcoinFeeRateProvider feeRateProvider = new BitcoinFeeRateProvider(ctx.getEnvironment());

// Make a call to the API, retrieve the recommended fee rate
// If the API call fails, or the response body cannot be parsed, the test will
// fail with an exception
FeeRate retrievedFeeRate = feeRateProvider.doGet();

// Check that the FeeRateProvider returns a fee within the defined parameters
assertTrue(retrievedFeeRate.getPrice() >= BitcoinFeeRateProvider.MIN_FEE_RATE);
assertTrue(retrievedFeeRate.getPrice() <= BitcoinFeeRateProvider.MAX_FEE_RATE);
}
}