-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add redundancy to ARS/BTC pricenode:
- Remove ban over ARS fiat - ExchangeRateProvider by default does not consider rates with blue markets. CryptoYimplementation does. - ExchangeRateService uses ubiquous place for generating the rates / consider bluemarket prices / update tests accordingly - avoid procesing of blue markets if its already handled by EP / move bluelytics to its own util package - bluelytics util is singleton and updates gap async less frequently (1hr)
- Loading branch information
Showing
11 changed files
with
262 additions
and
39 deletions.
There are no files selected for viewing
Submodule bisq
updated
17 files
+1 −1 | .github/workflows/build.yml | |
+1 −1 | .github/workflows/codacy-coverage-reporter.yml | |
+103 −1 | apitest/build.gradle | |
+4 −0 | build-logic/commons/build.gradle | |
+47 −0 | build-logic/commons/src/main/groovy/bisq.application.gradle | |
+7 −0 | build-logic/commons/src/main/groovy/bisq.javafx.gradle | |
+0 −473 | build.gradle | |
+1 −1 | cli/build.gradle | |
+40 −0 | common/build.gradle | |
+65 −0 | core/build.gradle | |
+1 −1 | daemon/build.gradle | |
+79 −1 | desktop/build.gradle | |
+2 −0 | gradle/libs.versions.toml | |
+5 −0 | gradle/verification-metadata.xml | |
+41 −0 | p2p/build.gradle | |
+19 −1 | seednode/build.gradle | |
+15 −1 | statsnode/build.gradle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/bisq/price/util/bluelytics/BlueLyticsUSDRate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package bisq.price.util.bluelytics; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Util singleton object to update and provider ARS/USD blue market gap against the oficial rate. | ||
* This is useful for example, to estimate ARS/BTC blue (real) market rate in a country with heavy currency controls | ||
*/ | ||
public final class BlueLyticsUSDRate { | ||
private static final long MIN_REFRESH_WINDOW = 3600000; // 1hr | ||
private static final String GET_USD_EXCHANGE_RATES_ARG_URL = "https://api.bluelytics.com.ar/v2/latest"; | ||
private static final BlueLyticsUSDRate instance = new BlueLyticsUSDRate(); | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
private final RestTemplate restTemplate = new RestTemplate(); | ||
private Double lastBlueGap; | ||
private Long lastRefresh; | ||
|
||
private BlueLyticsUSDRate() { | ||
lastRefresh = null; | ||
lastBlueGap = null; | ||
} | ||
|
||
public static BlueLyticsUSDRate getInstance() { | ||
return BlueLyticsUSDRate.instance; | ||
} | ||
|
||
/** | ||
* | ||
* @return current ARS/USD gap multiplier to get from official rate to free market rate. | ||
* If not available returns Nan | ||
*/ | ||
public Double blueGapMultiplier() { | ||
if (this.lastRefresh == null) { | ||
this.refreshBlueGap(); | ||
} else { | ||
this.maybeLaunchAsyncRefresh(); | ||
} | ||
return Objects.requireNonNullElse(this.lastBlueGap, Double.NaN); | ||
} | ||
|
||
/** | ||
* if enough time {@see BlueLyticsUSDRate.MIN_FRESH_WINDOW} has pass from the last refresh, launch async refresh | ||
*/ | ||
private void maybeLaunchAsyncRefresh() { | ||
if (this.lastRefresh != null && | ||
Date.from(Instant.now()).getTime() > this.lastRefresh + BlueLyticsUSDRate.MIN_REFRESH_WINDOW) { | ||
this.launchAsyncRefresh(); | ||
} | ||
} | ||
|
||
private synchronized void launchAsyncRefresh() { | ||
new Thread(this::refreshBlueGap).start(); | ||
} | ||
|
||
private void refreshBlueGap() { | ||
try { | ||
// the last_update value is different than the last one and also launch the update if 1 hour passed ? | ||
this.lastBlueGap = Objects.requireNonNull(restTemplate.exchange( | ||
RequestEntity | ||
.get(UriComponentsBuilder | ||
.fromUriString(BlueLyticsUSDRate.GET_USD_EXCHANGE_RATES_ARG_URL).build() | ||
.toUri()) | ||
.build(), | ||
new ParameterizedTypeReference<BlueLyticsUsdRates>() { | ||
} | ||
).getBody()).gapSellMultiplier(); | ||
this.lastRefresh = new Date().getTime(); | ||
} catch (Exception e) { | ||
this.logger.error("Failed to fetch updated bluelytics gap multiplier", e); | ||
} | ||
} | ||
|
||
@Override | ||
protected Object clone() throws CloneNotSupportedException { | ||
throw new CloneNotSupportedException("Cannot clone Singleton"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/bisq/price/util/bluelytics/BlueLyticsUsdRates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package bisq.price.util.bluelytics; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Date; | ||
|
||
@Getter | ||
@Setter | ||
public class BlueLyticsUsdRates { | ||
@Getter | ||
@Setter | ||
public static class USDRate { | ||
Double value_avg; | ||
Double value_sell; | ||
Double value_buy; | ||
} | ||
|
||
BlueLyticsUsdRates.USDRate oficial; | ||
BlueLyticsUsdRates.USDRate blue; | ||
Date last_update; | ||
|
||
/** | ||
* | ||
* @return the sell multiplier to go from oficial to blue market for ARS/USD | ||
* if its not available, returns NaN | ||
*/ | ||
public Double gapSellMultiplier() { | ||
return this.blue.value_sell / this.oficial.value_sell; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.