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

Feature/ars pricenode #15

Merged
merged 2 commits into from
Aug 13, 2023
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
2 changes: 1 addition & 1 deletion bisq
Submodule bisq updated 1254 files
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ configurations.all {
}

dependencies {
implementation enforcedPlatform(project(':platform'))

// We need three subprojects from includeBuild('bisq'), with some of their transitive dependencies.
implementation 'bisq:assets'
implementation 'bisq:common'
Expand Down
9 changes: 9 additions & 0 deletions platform/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'java-platform'
}

dependencies {
constraints {
api 'com.google.guava:guava:30.1.1-jre'
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ pluginManagement {
includeBuild('bisq-gradle')
}
rootProject.name = 'bisq-pricenode'

includeBuild('bisq')
include 'platform'
86 changes: 86 additions & 0 deletions src/main/java/bisq/price/spot/providers/CryptoYa.java
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();
}
}
75 changes: 75 additions & 0 deletions src/main/java/bisq/price/util/cryptoya/CryptoYaMarketData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.util.cryptoya;
rodvar marked this conversation as resolved.
Show resolved Hide resolved


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);
}
}
37 changes: 37 additions & 0 deletions src/main/java/bisq/price/util/cryptoya/CryptoYaTicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.util.cryptoya;
rodvar marked this conversation as resolved.
Show resolved Hide resolved

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;

}
33 changes: 33 additions & 0 deletions src/test/java/bisq/price/spot/providers/CryptoYaTest.java
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()));
}

}