Skip to content

Commit 57a404a

Browse files
committed
remove inverted crypto prices; always use price per XMR (base/quote)
1 parent 954b51e commit 57a404a

File tree

87 files changed

+519
-693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+519
-693
lines changed

core/src/main/java/haveno/core/account/witness/AccountAgeWitnessService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ private boolean verifyPeersTradeLimit(Offer offer,
654654
Date peersCurrentDate,
655655
ErrorMessageHandler errorMessageHandler) {
656656
checkNotNull(offer);
657-
final String currencyCode = offer.getCurrencyCode();
657+
final String currencyCode = offer.getCounterCurrencyCode();
658658
final BigInteger defaultMaxTradeLimit = offer.getPaymentMethod().getMaxTradeLimit(currencyCode);
659659
BigInteger peersCurrentTradeLimit = defaultMaxTradeLimit;
660660
if (!hasTradeLimitException(peersWitness)) {
@@ -673,7 +673,7 @@ private boolean verifyPeersTradeLimit(Offer offer,
673673
"\nPeers trade limit=" + peersCurrentTradeLimit +
674674
"\nOffer ID=" + offer.getShortId() +
675675
"\nPaymentMethod=" + offer.getPaymentMethod().getId() +
676-
"\nCurrencyCode=" + offer.getCurrencyCode();
676+
"\nCurrencyCode=" + offer.getCounterCurrencyCode();
677677
log.warn(msg);
678678
errorMessageHandler.handleErrorMessage(msg);
679679
}

core/src/main/java/haveno/core/api/CoreOffersService.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ List<OpenOffer> getMyOffers() {
149149
List<OpenOffer> getMyOffers(String direction, String currencyCode) {
150150
return getMyOffers().stream()
151151
.filter(o -> offerMatchesDirectionAndCurrency(o.getOffer(), direction, currencyCode))
152-
.sorted(openOfferPriceComparator(direction, CurrencyUtil.isTraditionalCurrency(currencyCode)))
152+
.sorted(openOfferPriceComparator(direction))
153153
.collect(Collectors.toList());
154154
}
155155

@@ -336,7 +336,7 @@ private void placeOffer(Offer offer,
336336
String sourceOfferId,
337337
Consumer<Transaction> resultHandler,
338338
ErrorMessageHandler errorMessageHandler) {
339-
long triggerPriceAsLong = PriceUtil.getMarketPriceAsLong(triggerPriceAsString, offer.getCurrencyCode());
339+
long triggerPriceAsLong = PriceUtil.getMarketPriceAsLong(triggerPriceAsString, offer.getCounterCurrencyCode());
340340
openOfferManager.placeOffer(offer,
341341
useSavingsWallet,
342342
triggerPriceAsLong,
@@ -353,8 +353,7 @@ private boolean offerMatchesDirectionAndCurrency(Offer offer,
353353
if ("".equals(direction)) direction = null;
354354
if ("".equals(currencyCode)) currencyCode = null;
355355
var offerOfWantedDirection = direction == null || offer.getDirection().name().equalsIgnoreCase(direction);
356-
var counterAssetCode = CurrencyUtil.isCryptoCurrency(currencyCode) ? offer.getOfferPayload().getBaseCurrencyCode() : offer.getOfferPayload().getCounterCurrencyCode();
357-
var offerInWantedCurrency = currencyCode == null || counterAssetCode.equalsIgnoreCase(currencyCode);
356+
var offerInWantedCurrency = currencyCode == null || offer.getCounterCurrencyCode().equalsIgnoreCase(currencyCode);
358357
return offerOfWantedDirection && offerInWantedCurrency;
359358
}
360359

@@ -366,17 +365,12 @@ private Comparator<Offer> priceComparator(String direction) {
366365
: priceComparator.get();
367366
}
368367

369-
private Comparator<OpenOffer> openOfferPriceComparator(String direction, boolean isTraditional) {
368+
private Comparator<OpenOffer> openOfferPriceComparator(String direction) {
370369
// A buyer probably wants to see sell orders in price ascending order.
371370
// A seller probably wants to see buy orders in price descending order.
372-
if (isTraditional)
373-
return direction.equalsIgnoreCase(OfferDirection.BUY.name())
374-
? openOfferPriceComparator.get().reversed()
375-
: openOfferPriceComparator.get();
376-
else
377-
return direction.equalsIgnoreCase(OfferDirection.SELL.name())
378-
? openOfferPriceComparator.get().reversed()
379-
: openOfferPriceComparator.get();
371+
return direction.equalsIgnoreCase(OfferDirection.BUY.name())
372+
? openOfferPriceComparator.get().reversed()
373+
: openOfferPriceComparator.get();
380374
}
381375

382376
private long priceStringToLong(String priceAsString, String currencyCode) {

core/src/main/java/haveno/core/api/CorePriceService.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public double getMarketPrice(String currencyCode) throws ExecutionException, Int
7878
} else if (!marketPrice.isExternallyProvidedPrice()) {
7979
throw new IllegalArgumentException("Price is not available externally: " + currencyCode); // TODO: return more complex Price type including price double and isExternal boolean
8080
}
81-
return mapPriceFeedServicePrice(marketPrice.getPrice(), marketPrice.getCurrencyCode());
81+
return marketPrice.getPrice();
8282
}
8383

8484
/**
@@ -87,8 +87,7 @@ public double getMarketPrice(String currencyCode) throws ExecutionException, Int
8787
public List<MarketPriceInfo> getMarketPrices() throws ExecutionException, InterruptedException, TimeoutException {
8888
return priceFeedService.requestAllPrices().values().stream()
8989
.map(marketPrice -> {
90-
double mappedPrice = mapPriceFeedServicePrice(marketPrice.getPrice(), marketPrice.getCurrencyCode());
91-
return new MarketPriceInfo(marketPrice.getCurrencyCode(), mappedPrice);
90+
return new MarketPriceInfo(marketPrice.getCurrencyCode(), marketPrice.getPrice());
9291
})
9392
.collect(Collectors.toList());
9493
}
@@ -102,12 +101,13 @@ public MarketDepthInfo getMarketDepth(String currencyCode) throws ExecutionExcep
102101
// Offer price can be null (if price feed unavailable), thus a null-tolerant comparator is used.
103102
Comparator<Offer> offerPriceComparator = Comparator.comparing(Offer::getPrice, Comparator.nullsLast(Comparator.naturalOrder()));
104103

104+
// TODO: remove this!!!
105105
// Trading xmr-traditional is considered as buying/selling XMR, but trading xmr-crypto is
106106
// considered as buying/selling crypto. Because of this, when viewing a xmr-crypto pair,
107107
// the buy column is actually the sell column and vice versa. To maintain the expected
108108
// ordering, we have to reverse the price comparator.
109-
boolean isCrypto = CurrencyUtil.isCryptoCurrency(currencyCode);
110-
if (isCrypto) offerPriceComparator = offerPriceComparator.reversed();
109+
//boolean isCrypto = CurrencyUtil.isCryptoCurrency(currencyCode);
110+
//if (isCrypto) offerPriceComparator = offerPriceComparator.reversed();
111111

112112
// Offer amounts are used for the secondary sort. They are sorted from high to low.
113113
Comparator<Offer> offerAmountComparator = Comparator.comparing(Offer::getAmount).reversed();
@@ -130,11 +130,11 @@ public MarketDepthInfo getMarketDepth(String currencyCode) throws ExecutionExcep
130130
double amount = (double) offer.getAmount().longValueExact() / LongMath.pow(10, HavenoUtils.XMR_SMALLEST_UNIT_EXPONENT);
131131
accumulatedAmount += amount;
132132
double priceAsDouble = (double) price.getValue() / LongMath.pow(10, price.smallestUnitExponent());
133-
buyTM.put(mapPriceFeedServicePrice(priceAsDouble, currencyCode), accumulatedAmount);
133+
buyTM.put(priceAsDouble, accumulatedAmount);
134134
}
135135
};
136136

137-
// Create buyer hashmap {key:price, value:count}, uses TreeMap to sort by key (asc)
137+
// Create seller hashmap {key:price, value:count}, uses TreeMap to sort by key (asc)
138138
accumulatedAmount = 0;
139139
LinkedHashMap<Double,Double> sellTM = new LinkedHashMap<Double,Double>();
140140
for(Offer offer: sellOffers){
@@ -143,7 +143,7 @@ public MarketDepthInfo getMarketDepth(String currencyCode) throws ExecutionExcep
143143
double amount = (double) offer.getAmount().longValueExact() / LongMath.pow(10, HavenoUtils.XMR_SMALLEST_UNIT_EXPONENT);
144144
accumulatedAmount += amount;
145145
double priceAsDouble = (double) price.getValue() / LongMath.pow(10, price.smallestUnitExponent());
146-
sellTM.put(mapPriceFeedServicePrice(priceAsDouble, currencyCode), accumulatedAmount);
146+
sellTM.put(priceAsDouble, accumulatedAmount);
147147
}
148148
};
149149

@@ -157,20 +157,5 @@ public MarketDepthInfo getMarketDepth(String currencyCode) throws ExecutionExcep
157157

158158
return new MarketDepthInfo(currencyCode, buyPrices, buyDepth, sellPrices, sellDepth);
159159
}
160-
161-
/**
162-
* PriceProvider returns different values for crypto and traditional,
163-
* e.g. 1 XMR = X USD
164-
* but 1 DOGE = X XMR
165-
* Here we convert all to:
166-
* 1 XMR = X (FIAT or CRYPTO)
167-
*/
168-
private double mapPriceFeedServicePrice(double price, String currencyCode) {
169-
if (CurrencyUtil.isTraditionalCurrency(currencyCode)) {
170-
return price;
171-
}
172-
return price == 0 ? 0 : 1 / price;
173-
// TODO PriceProvider.getAll() could provide these values directly when the original values are not needed for the 'desktop' UI anymore
174-
}
175160
}
176161

core/src/main/java/haveno/core/api/CoreTradesService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void takeOffer(Offer offer,
123123
BigInteger amount = amountAsLong == 0 ? offer.getAmount() : BigInteger.valueOf(amountAsLong);
124124

125125
// adjust amount for fixed-price offer (based on TakeOfferViewModel)
126-
String currencyCode = offer.getCurrencyCode();
126+
String currencyCode = offer.getCounterCurrencyCode();
127127
OfferDirection direction = offer.getOfferPayload().getDirection();
128128
long maxTradeLimit = offerUtil.getMaxTradeLimit(paymentAccount, currencyCode, direction, offer.hasBuyerAsTakerWithoutDeposit());
129129
if (offer.getPrice() != null) {
@@ -133,7 +133,7 @@ void takeOffer(Offer offer,
133133
&& !amount.equals(offer.getMinAmount()) && !amount.equals(amount)) {
134134
// We only apply the rounding if the amount is variable (minAmount is lower as amount).
135135
// Otherwise we could get an amount lower then the minAmount set by rounding
136-
amount = CoinUtil.getRoundedAmount(amount, offer.getPrice(), maxTradeLimit, offer.getCurrencyCode(), offer.getPaymentMethodId());
136+
amount = CoinUtil.getRoundedAmount(amount, offer.getPrice(), maxTradeLimit, offer.getCounterCurrencyCode(), offer.getPaymentMethodId());
137137
}
138138
}
139139

core/src/main/java/haveno/core/api/model/OfferInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static OfferInfo toOfferInfo(Offer offer) {
129129
public static OfferInfo toMyOfferInfo(OpenOffer openOffer) {
130130
// An OpenOffer is always my offer.
131131
var offer = openOffer.getOffer();
132-
var currencyCode = offer.getCurrencyCode();
132+
var currencyCode = offer.getCounterCurrencyCode();
133133
var isActivated = !openOffer.isDeactivated();
134134
Optional<Price> optionalTriggerPrice = openOffer.getTriggerPrice() > 0
135135
? Optional.of(Price.valueOf(currencyCode, openOffer.getTriggerPrice()))
@@ -150,7 +150,7 @@ public static OfferInfo toMyOfferInfo(OpenOffer openOffer) {
150150
private static OfferInfoBuilder getBuilder(Offer offer) {
151151
// OfferInfo protos are passed to API client, and some field
152152
// values are converted to displayable, unambiguous form.
153-
var currencyCode = offer.getCurrencyCode();
153+
var currencyCode = offer.getCounterCurrencyCode();
154154
var preciseOfferPrice = reformatMarketPrice(
155155
requireNonNull(offer.getPrice()).toPlainString(),
156156
currencyCode);

core/src/main/java/haveno/core/api/model/TradeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class TradeInfo implements Payload {
5757

5858
private static final Function<Trade, String> toPreciseTradePrice = (trade) ->
5959
reformatMarketPrice(requireNonNull(trade.getPrice()).toPlainString(),
60-
trade.getOffer().getCurrencyCode());
60+
trade.getOffer().getCounterCurrencyCode());
6161

6262
// Haveno v1 trade protocol fields (some are in common with the BSQ Swap protocol).
6363
private final OfferInfo offer;

core/src/main/java/haveno/core/locale/CurrencyUtil.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static Optional<TraditionalCurrency> getTraditionalCurrency(String curren
284284
}
285285

286286
/**
287-
* We return true if it is BTC or any of our currencies available in the assetRegistry.
287+
* We return true if it is XMR or any of our currencies available in the assetRegistry.
288288
* For removed assets it would fail as they are not found but we don't want to conclude that they are traditional then.
289289
* As the caller might not deal with the case that a currency can be neither a cryptoCurrency nor Traditional if not found
290290
* we return true as well in case we have no traditional currency for the code.
@@ -514,28 +514,19 @@ public static List<CryptoCurrency> getActiveSortedCryptoCurrencies(FilterManager
514514
}
515515

516516
public static String getCurrencyPair(String currencyCode) {
517-
if (isTraditionalCurrency(currencyCode))
518-
return Res.getBaseCurrencyCode() + "/" + currencyCode;
519-
else
520-
return currencyCode + "/" + Res.getBaseCurrencyCode();
517+
return Res.getBaseCurrencyCode() + "/" + currencyCode;
521518
}
522519

523520
public static String getCounterCurrency(String currencyCode) {
524-
if (isTraditionalCurrency(currencyCode))
525-
return currencyCode;
526-
else
527-
return Res.getBaseCurrencyCode();
521+
return currencyCode;
528522
}
529523

530524
public static String getPriceWithCurrencyCode(String currencyCode) {
531525
return getPriceWithCurrencyCode(currencyCode, "shared.priceInCurForCur");
532526
}
533527

534528
public static String getPriceWithCurrencyCode(String currencyCode, String translationKey) {
535-
if (isCryptoCurrency(currencyCode))
536-
return Res.get(translationKey, Res.getBaseCurrencyCode(), currencyCode);
537-
else
538-
return Res.get(translationKey, currencyCode, Res.getBaseCurrencyCode());
529+
return Res.get(translationKey, currencyCode, Res.getBaseCurrencyCode());
539530
}
540531

541532
public static String getOfferVolumeCode(String currencyCode) {

core/src/main/java/haveno/core/monetary/CryptoExchangeRate.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CryptoExchangeRate {
3232
*/
3333

3434
public final Coin coin;
35-
public final CryptoMoney crypto;
35+
public final CryptoMoney cryptoMoney;
3636

3737
/**
3838
* Construct exchange rate. This amount of coin is worth that amount of crypto.
@@ -43,7 +43,7 @@ public CryptoExchangeRate(Coin coin, CryptoMoney crypto) {
4343
checkArgument(crypto.isPositive());
4444
checkArgument(crypto.currencyCode != null, "currency code required");
4545
this.coin = coin;
46-
this.crypto = crypto;
46+
this.cryptoMoney = crypto;
4747
}
4848

4949
/**
@@ -59,13 +59,13 @@ public CryptoExchangeRate(CryptoMoney crypto) {
5959
* @throws ArithmeticException if the converted crypto amount is too high or too low.
6060
*/
6161
public CryptoMoney coinToCrypto(Coin convertCoin) {
62-
BigInteger converted = BigInteger.valueOf(coin.value)
63-
.multiply(BigInteger.valueOf(convertCoin.value))
64-
.divide(BigInteger.valueOf(crypto.value));
62+
final BigInteger converted = BigInteger.valueOf(convertCoin.value)
63+
.multiply(BigInteger.valueOf(cryptoMoney.value))
64+
.divide(BigInteger.valueOf(coin.value));
6565
if (converted.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
6666
|| converted.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
6767
throw new ArithmeticException("Overflow");
68-
return CryptoMoney.valueOf(crypto.currencyCode, converted.longValue());
68+
return CryptoMoney.valueOf(cryptoMoney.currencyCode, converted.longValue());
6969
}
7070

7171
/**
@@ -74,12 +74,11 @@ public CryptoMoney coinToCrypto(Coin convertCoin) {
7474
* @throws ArithmeticException if the converted coin amount is too high or too low.
7575
*/
7676
public Coin cryptoToCoin(CryptoMoney convertCrypto) {
77-
checkArgument(convertCrypto.currencyCode.equals(crypto.currencyCode), "Currency mismatch: %s vs %s",
78-
convertCrypto.currencyCode, crypto.currencyCode);
77+
checkArgument(convertCrypto.currencyCode.equals(cryptoMoney.currencyCode), "Currency mismatch: %s vs %s",
78+
convertCrypto.currencyCode, cryptoMoney.currencyCode);
7979
// Use BigInteger because it's much easier to maintain full precision without overflowing.
80-
BigInteger converted = BigInteger.valueOf(crypto.value)
81-
.multiply(BigInteger.valueOf(convertCrypto.value))
82-
.divide(BigInteger.valueOf(coin.value));
80+
final BigInteger converted = BigInteger.valueOf(convertCrypto.value).multiply(BigInteger.valueOf(coin.value))
81+
.divide(BigInteger.valueOf(cryptoMoney.value));
8382
if (converted.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
8483
|| converted.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
8584
throw new ArithmeticException("Overflow");

core/src/main/java/haveno/core/monetary/Price.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Price subtract(Price other) {
136136

137137
public String toFriendlyString() {
138138
return monetary instanceof CryptoMoney ?
139-
((CryptoMoney) monetary).toFriendlyString() + "/XMR" :
139+
((CryptoMoney) monetary).toFriendlyString().replace(((CryptoMoney) monetary).currencyCode, "") + "XMR/" + ((CryptoMoney) monetary).currencyCode :
140140
((TraditionalMoney) monetary).toFriendlyString().replace(((TraditionalMoney) monetary).currencyCode, "") + "XMR/" + ((TraditionalMoney) monetary).currencyCode;
141141
}
142142

0 commit comments

Comments
 (0)