Skip to content

Commit

Permalink
Merge pull request #1895 from ManfredKarrer/support-removed-assets
Browse files Browse the repository at this point in the history
Support handling of removed assets.
  • Loading branch information
ManfredKarrer authored Nov 8, 2018
2 parents dc9762b + 36df44d commit ac6d812
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 185 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeManager;
import bisq.core.trade.statistics.AssetTradeActivityCheck;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
import bisq.core.user.User;
Expand Down Expand Up @@ -150,6 +151,7 @@ public interface BisqSetupCompleteListener {
private final PriceAlert priceAlert;
private final MarketAlerts marketAlerts;
private final VoteResultService voteResultService;
private final AssetTradeActivityCheck tradeActivityCheck;
private final BSFormatter formatter;
@Setter
@Nullable
Expand Down Expand Up @@ -223,6 +225,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
PriceAlert priceAlert,
MarketAlerts marketAlerts,
VoteResultService voteResultService,
AssetTradeActivityCheck tradeActivityCheck,
BSFormatter formatter) {


Expand Down Expand Up @@ -259,6 +262,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
this.priceAlert = priceAlert;
this.marketAlerts = marketAlerts;
this.voteResultService = voteResultService;
this.tradeActivityCheck = tradeActivityCheck;
this.formatter = formatter;
}

Expand Down Expand Up @@ -624,6 +628,7 @@ public void onBalanceChanged(Coin balance, Transaction tx) {
}

tradeStatisticsManager.onAllServicesInitialized();
tradeActivityCheck.onAllServicesInitialized();

accountAgeWitnessService.onAllServicesInitialized();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package bisq.core.dao.governance.asset;

import bisq.core.app.BisqEnvironment;
import bisq.core.dao.state.DaoStateService;
import bisq.core.locale.CurrencyUtil;

import bisq.common.proto.persistable.PersistedDataHost;
Expand All @@ -34,10 +33,7 @@

@Slf4j
public class AssetService implements PersistedDataHost {


private final Storage<RemovedAssetsList> storage;
private final DaoStateService daoStateService;
@Getter
private final RemovedAssetsList removedAssetsList = new RemovedAssetsList();

Expand All @@ -47,9 +43,8 @@ public class AssetService implements PersistedDataHost {
///////////////////////////////////////////////////////////////////////////////////////////

@Inject
public AssetService(Storage<RemovedAssetsList> storage, DaoStateService daoStateService) {
public AssetService(Storage<RemovedAssetsList> storage) {
this.storage = storage;
this.daoStateService = daoStateService;
}


Expand Down
55 changes: 45 additions & 10 deletions core/src/main/java/bisq/core/locale/CurrencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.Getter;
Expand Down Expand Up @@ -73,12 +72,11 @@ public static List<FiatCurrency> getAllSortedFiatCurrencies() {
}

private static List<FiatCurrency> createAllSortedFiatCurrenciesList() {
Set<FiatCurrency> set = CountryUtil.getAllCountries().stream()
return CountryUtil.getAllCountries().stream()
.map(country -> getCurrencyByCountryCode(country.code))
.collect(Collectors.toSet());
List<FiatCurrency> list = new ArrayList<>(set);
list.sort(TradeCurrency::compareTo);
return list;
.distinct()
.sorted(TradeCurrency::compareTo)
.collect(Collectors.toList());
}

public static List<FiatCurrency> getMainFiatCurrencies() {
Expand Down Expand Up @@ -323,8 +321,40 @@ public static Optional<FiatCurrency> getFiatCurrency(String currencyCode) {
}

@SuppressWarnings("WeakerAccess")
/**
* We return true if it is BTC or any of our currencies available in the assetRegistry.
* For removed assets it would fail as they are not found but we don't want to conclude that they are fiat then.
* As the caller might not deal with the case that a currency can be neither a cryptoCurrency nor Fiat if not found
* we return true as well in case we have no fiat currency for the code.
*
* As we use a boolean result for isCryptoCurrency and isFiatCurrency we do not treat missing currencies correctly.
* To throw an exception might be an option but that will require quite a lot of code change, so we don't do that
* for the moment, but could be considered for the future. Another maybe better option is to introduce a enum which
* contains 3 entries (CryptoCurrency, Fiat, Undefined).
*/
public static boolean isCryptoCurrency(String currencyCode) {
return getCryptoCurrency(currencyCode).isPresent();
// Some tests call that method with null values. Should be fixed in the tests but to not break them return false.
if (currencyCode == null)
return false;

// BTC is not part of our assetRegistry so treat it extra here. Other old base currencies (LTC, DOGE, DASH)
// are not supported anymore so we can ignore that case.
if (currencyCode.equals("BTC"))
return true;

// If we find the code in our assetRegistry we return true.
// It might be that an asset was removed from the assetsRegistry, we deal with such cases below by checking if
// it is a fiat currency
if (getCryptoCurrency(currencyCode).isPresent())
return true;

// In case the code is from a removed asset we cross check if there exist a fiat currency with that code,
// if we don't find a fiat currency we treat it as a crypto currency.
if (!getFiatCurrency(currencyCode).isPresent())
return true;

// If we would have found a fiat currency we return false
return false;
}

public static Optional<CryptoCurrency> getCryptoCurrency(String currencyCode) {
Expand All @@ -351,10 +381,14 @@ public static FiatCurrency getCurrencyByCountryCode(String countryCode) {
return new FiatCurrency(currency.getCurrencyCode());
}

public static String getNameByCode(String currencyCode) {
if (isCryptoCurrency(currencyCode))
return getCryptoCurrency(currencyCode).get().getName();

public static String getNameByCode(String currencyCode) {
if (isCryptoCurrency(currencyCode)) {
// We might not find the name in case we have a call for a removed asset.
// If BTC is the code (used in tests) we also want return Bitcoin as name.
String btcOrRemovedAsset = "BTC".equals(currencyCode) ? "Bitcoin" : Res.get("shared.na");
return getCryptoCurrency(currencyCode).map(TradeCurrency::getName).orElse(btcOrRemovedAsset);
}
try {
return Currency.getInstance(currencyCode).getDisplayName();
} catch (Throwable t) {
Expand Down Expand Up @@ -453,6 +487,7 @@ public static Optional<Asset> findAsset(String tickerSymbol, BaseCurrencyNetwork
.findAny();
}

// Excludes all assets which got removed by DAO voting
public static List<CryptoCurrency> getWhiteListedSortedCryptoCurrencies(AssetService assetService) {
return getAllSortedCryptoCurrencies().stream()
.filter(e -> !assetService.isAssetRemoved(e.getCode()))
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/bisq/core/offer/Offer.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ public String getCountryCode() {
}

public String getCurrencyCode() {
return CurrencyUtil.isCryptoCurrency(offerPayload.getBaseCurrencyCode()) ?
offerPayload.getBaseCurrencyCode() :
offerPayload.getCounterCurrencyCode();
return offerPayload.getBaseCurrencyCode().equals("BTC") ?
offerPayload.getCounterCurrencyCode() :
offerPayload.getBaseCurrencyCode();
}

public long getProtocolVersion() {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/offer/OfferPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public static PB.OfferPayload.Direction toProtoMessage(Direction direction) {
private final boolean useMarketBasedPrice;
private final long amount;
private final long minAmount;

// For fiat offer the baseCurrencyCode is BTC and the counterCurrencyCode is the fiat currency
// For altcoin offers it is the opposite. baseCurrencyCode is the altcoin and the counterCurrencyCode is BTC.
private final String baseCurrencyCode;
private final String counterCurrencyCode;

private final List<NodeAddress> arbitratorNodeAddresses;
private final List<NodeAddress> mediatorNodeAddresses;
private final String paymentMethodId;
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/bisq/core/trade/TradeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.core.payment.AccountAgeWitnessStorageService;
import bisq.core.trade.closed.ClosedTradableManager;
import bisq.core.trade.failed.FailedTradesManager;
import bisq.core.trade.statistics.AssetTradeActivityCheck;
import bisq.core.trade.statistics.ReferralIdService;
import bisq.core.trade.statistics.TradeStatistics2StorageService;
import bisq.core.trade.statistics.TradeStatisticsManager;
Expand Down Expand Up @@ -50,6 +51,7 @@ protected void configure() {
bind(AccountAgeWitnessService.class).in(Singleton.class);
bind(ReferralIdService.class).in(Singleton.class);
bind(AccountAgeWitnessStorageService.class).in(Singleton.class);
bind(AssetTradeActivityCheck.class).in(Singleton.class);
bindConstant().annotatedWith(named(AppOptionKeys.DUMP_STATISTICS)).to(environment.getRequiredProperty(AppOptionKeys.DUMP_STATISTICS));
}
}
Loading

0 comments on commit ac6d812

Please sign in to comment.