Skip to content

Commit

Permalink
Remove unused maxBlocks param from pricenode
Browse files Browse the repository at this point in the history
As the new fee estimation API does not require this parameter
anymore, remove it and all references to it.

See bisq-network/projects#27
  • Loading branch information
cd2357 committed May 6, 2020
1 parent bb2484a commit d9e404c
Showing 1 changed file with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,22 @@
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();

private final int maxBlocks;

public BitcoinFeeRateProvider(Environment env) {
super(Duration.ofMinutes(refreshInterval(env)));
this.maxBlocks = maxBlocks(env);
}

protected FeeRate doGet() {
Expand All @@ -63,9 +58,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,37 +70,30 @@ 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) {
return Optional.ofNullable(
env.getProperty(CommandLinePropertySource.DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME, String[].class));
}

private static int maxBlocks(Environment env) {
private static long refreshInterval(Environment env) {
return args(env)
.filter(args -> args.length >= 1)
.map(args -> Integer.valueOf(args[0]))
.orElse(DEFAULT_MAX_BLOCKS);
}

private static long refreshInterval(Environment env) {
return args(env)
.filter(args -> args.length >= 2)
.map(args -> Integer.valueOf(args[1]))
.orElse(DEFAULT_REFRESH_INTERVAL);
}

Expand All @@ -115,7 +103,7 @@ class Controller extends PriceController {

@GetMapping(path = "/getParams")
public String getParams() {
return String.format("%s;%s", maxBlocks, refreshInterval.toMillis());
return String.format("%s", refreshInterval.toMillis());
}
}
}

0 comments on commit d9e404c

Please sign in to comment.