-
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.
fix compilation and execution issues in main
setup cryptoya api price provider fixing module structure to compile with the fixed bisq submodule adding cryptoya pricenode provider Fix reporting of rate, test works Algorithm to decide rate based on fetched data applied code review suggestions
- Loading branch information
Showing
6 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
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 @@ | ||
/* | ||
* 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.spot.providers; | ||
|
||
import bisq.price.spot.ExchangeRate; | ||
import bisq.price.spot.ExchangeRateProvider; | ||
import bisq.price.util.cryptoya.CryptoYaMarketData; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Duration; | ||
import java.util.Date; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* CryptoYa is used only for Argentina Peso (ARS). | ||
* Currency controls in the country forces a black market where the currency can be traded freely. | ||
* Official and easily available rates cannot be trusted, therefore a specific fetch needs to be done | ||
* for these scenarios. | ||
* This ExchangeRateProvider provides a real market rate (black or "blue") for ARS/BTC | ||
*/ | ||
@Component | ||
class CryptoYa extends ExchangeRateProvider { | ||
|
||
private static final String ARS_BTC_URL = "https://criptoya.com/api/btc/ars/0.1"; | ||
|
||
private final RestTemplate restTemplate = new RestTemplate(); | ||
|
||
public CryptoYa(Environment env) { | ||
super(env, "CRYPTOYA", "cryptoya", Duration.ofMinutes(1)); | ||
} | ||
|
||
/** | ||
* | ||
* @return average price buy/sell price averaging different providers suported by cryptoya api | ||
* which uses the free market (or blue, or unofficial) ARS price for BTC | ||
*/ | ||
@Override | ||
public Set<ExchangeRate> doGet() { | ||
Set<ExchangeRate> result = new HashSet<>(); | ||
String key = "ARS"; | ||
Double rate = getARSBlueMarketData().averageBlueRate(); | ||
if (rate > 0.0d) { | ||
result.add(new ExchangeRate( | ||
key, | ||
BigDecimal.valueOf(rate), | ||
new Date(), | ||
this.getName() | ||
)); | ||
} | ||
return result; | ||
} | ||
|
||
private CryptoYaMarketData getARSBlueMarketData() { | ||
return restTemplate.exchange( | ||
RequestEntity | ||
.get(UriComponentsBuilder | ||
.fromUriString(CryptoYa.ARS_BTC_URL).build() | ||
.toUri()) | ||
.build(), | ||
new ParameterizedTypeReference<CryptoYaMarketData>() { | ||
} | ||
).getBody(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/bisq/price/util/cryptoya/CryptoYaMarketData.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,58 @@ | ||
package bisq.price.util.cryptoya; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
@Getter | ||
@Setter | ||
public class CryptoYaMarketData { | ||
|
||
private CryptoYaTicker argenbtc; | ||
private CryptoYaTicker buenbit; | ||
private CryptoYaTicker ripio; | ||
private CryptoYaTicker ripioexchange; | ||
private CryptoYaTicker satoshitango; | ||
private CryptoYaTicker cryptomkt; | ||
private CryptoYaTicker decrypto; | ||
private CryptoYaTicker latamex; | ||
private CryptoYaTicker bitso; | ||
private CryptoYaTicker letsbit; | ||
private CryptoYaTicker fiwind; | ||
private CryptoYaTicker lemoncash; | ||
private CryptoYaTicker bitmonedero; | ||
private CryptoYaTicker belo; | ||
private CryptoYaTicker tiendacrypto; | ||
private CryptoYaTicker saldo; | ||
private CryptoYaTicker kriptonmarket; | ||
private CryptoYaTicker calypso; | ||
private CryptoYaTicker bybit; | ||
private CryptoYaTicker binance; | ||
|
||
/** | ||
* | ||
* @return the avg ask price from all the exchanges that have updated ask prices (not older than 1 day) | ||
* if no market data available returns 0 | ||
*/ | ||
public Double averageBlueRate() { | ||
// filter more than 1 day old values with yesterday UTC timestamp | ||
Long yesterdayTimestamp = Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond(); | ||
return streamLatestAvailableMarkets(yesterdayTimestamp).mapToDouble(CryptoYaTicker::getAsk) | ||
.average() | ||
.orElse(0.0d); | ||
} | ||
|
||
private Stream<CryptoYaTicker> streamLatestAvailableMarkets(Long startingTime) { | ||
return Stream.of(argenbtc, buenbit, ripio, ripioexchange, satoshitango, | ||
cryptomkt, decrypto, latamex, bitso, letsbit, fiwind, | ||
lemoncash, bitmonedero, belo, tiendacrypto, saldo, | ||
kriptonmarket, calypso, bybit, binance) | ||
.filter(Objects::nonNull) | ||
.filter(rate -> rate.getTime() > startingTime); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/bisq/price/util/cryptoya/CryptoYaTicker.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,20 @@ | ||
package bisq.price.util.cryptoya; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class CryptoYaTicker { | ||
|
||
private Double ask; | ||
|
||
private Double totalAsk; | ||
|
||
private Double bid; | ||
|
||
private Double totalBid; | ||
|
||
private Integer time; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.spot.providers; | ||
|
||
import bisq.price.AbstractExchangeRateProviderTest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.env.StandardEnvironment; | ||
|
||
@Slf4j | ||
public class CryptoYaTest extends AbstractExchangeRateProviderTest { | ||
|
||
@Test | ||
public void doGet_successfulCall() { | ||
doGet_successfulCall(new CryptoYa(new StandardEnvironment())); | ||
} | ||
|
||
} |