Skip to content

Commit

Permalink
Switch to more accurate fee estimation endpoint
Browse files Browse the repository at this point in the history
The API endpoint for fee estimations has been changed to one that delivers more accurate fee estimations.

This is a temporary solution, until a more decentralized approach is found.

Fixes projects/issues/27
  • Loading branch information
cd2357 committed May 5, 2020
1 parent bb2484a commit e794691
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
@Component
class BitcoinFeeRateProvider extends FeeRateProvider {

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

private static final int DEFAULT_MAX_BLOCKS = 2;
private static final int DEFAULT_REFRESH_INTERVAL = 2;
Expand All @@ -63,9 +63,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 +75,18 @@ 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
// A more de-centralized solution discussed in 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,45 @@
/*
* 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);
}

}

0 comments on commit e794691

Please sign in to comment.