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

Fixes for issues #2741, #2944, #2955 #2976

Merged
merged 8 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
27 changes: 24 additions & 3 deletions assets/src/main/java/bisq/asset/coins/Counterparty.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@

package bisq.asset.coins;

import org.bitcoinj.core.NetworkParameters;

import bisq.asset.AddressValidationResult;
import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;

public class Counterparty extends Coin {

public Counterparty() {
super("Counterparty", "XCP", new DefaultAddressValidator());
public Counterparty(Network network, NetworkParameters networkParameters) {
super("Counterparty", "XCP", new XcpAddressValidator(networkParameters), network);
}

public static class XcpAddressValidator extends Base58BitcoinAddressValidator {

public XcpAddressValidator(NetworkParameters networkParameters) {
super(networkParameters);
}

@Override
public AddressValidationResult validate(String address) {
if (address == null || address.length() != 34 || !address.startsWith("1")) {
return AddressValidationResult.invalidAddress("XCP address must start with '1' and must have 34 characters.");
}

String addressAsBtc = address.substring(1, address.length());

return super.validate(addressAsBtc);
}
}
}
32 changes: 28 additions & 4 deletions assets/src/main/java/bisq/asset/coins/Decred.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,36 @@

package bisq.asset.coins;

import org.bitcoinj.core.NetworkParameters;

import bisq.asset.AddressValidationResult;
import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;

public class Decred extends Coin {

public Decred() {
super("Decred", "DCR", new DefaultAddressValidator());
}
public Decred(Network network, NetworkParameters networkParameters) {
super("Decred", "DCR", new DcrAddressValidator(networkParameters), network);
}

public static class DcrAddressValidator extends Base58BitcoinAddressValidator {

public DcrAddressValidator(NetworkParameters networkParameters) {
super(networkParameters);
}

@Override
public AddressValidationResult validate(String address) {
if (address == null || address.length() < 26 || address.length() > 36 || !address.startsWith("Dk")
|| !address.startsWith("Ds") || !address.startsWith("De") || !address.startsWith("DS")
|| !address.startsWith("Dc") || !address.startsWith("Pm")) {
return AddressValidationResult
.invalidAddress("DCR address must start with 'Dk' or 'Ds' or 'De' or 'DS' or 'Dc' or 'Pm' and must have 34 characters.");
}

String addressAsBtc = address.substring(1, address.length());

return super.validate(addressAsBtc);
}
}
}
4 changes: 2 additions & 2 deletions assets/src/main/java/bisq/asset/coins/EtherClassic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package bisq.asset.coins;

import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;
import bisq.asset.EtherAddressValidator;

public class EtherClassic extends Coin {

public EtherClassic() {
super("Ether Classic", "ETC", new DefaultAddressValidator());
super("Ether Classic", "ETC", new EtherAddressValidator());
}
}
26 changes: 23 additions & 3 deletions assets/src/main/java/bisq/asset/coins/Namecoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,32 @@

package bisq.asset.coins;

import org.bitcoinj.core.NetworkParameters;

import bisq.asset.AddressValidationResult;
import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;

public class Namecoin extends Coin {

public Namecoin() {
super("Namecoin", "NMC", new DefaultAddressValidator());
public Namecoin(Network network, NetworkParameters networkParameters) {
super("Namecoin", "NMC", new NmcAddressValidator(networkParameters), network);
}

public static class NmcAddressValidator extends Base58BitcoinAddressValidator {

public NmcAddressValidator(NetworkParameters networkParameters) {
super(networkParameters);
}

@Override
public AddressValidationResult validate(String address) {
if (address == null || address.length() != 34 || !address.startsWith("N") || !address.startsWith("M"))
return AddressValidationResult.invalidAddress("NMC address must start with 'N' or 'M' and must be 33 characters long.");

String addressAsBtc = address.substring(1, address.length());

return super.validate(addressAsBtc);
}
}
}
12 changes: 10 additions & 2 deletions assets/src/main/java/bisq/asset/coins/Siafund.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
package bisq.asset.coins;

import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;
import bisq.asset.RegexAddressValidator;

public class Siafund extends Coin {

public Siafund() {
super("Siafund", "SF", new DefaultAddressValidator());
super("Siafund", "SF", new SfAddressValidator());
}

public static class SfAddressValidator extends RegexAddressValidator {

public SfAddressValidator() {
super("^[0-9a-fA-F]{76}$");
}
}

}
27 changes: 24 additions & 3 deletions assets/src/main/java/bisq/asset/coins/Unobtanium.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@

package bisq.asset.coins;

import org.bitcoinj.core.NetworkParameters;

import bisq.asset.AddressValidationResult;
import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;

public class Unobtanium extends Coin {

public Unobtanium() {
super("Unobtanium", "UNO", new DefaultAddressValidator());
public Unobtanium(Network network, NetworkParameters networkParameters) {
super("Unobtanium", "UNO", new UnoAddressValidator(networkParameters), network);
}

public static class UnoAddressValidator extends Base58BitcoinAddressValidator {

public UnoAddressValidator(NetworkParameters networkParameters) {
super(networkParameters);
}

@Override
public AddressValidationResult validate(String address) {
if (address == null || address.length() != 34 || !address.startsWith("u")) {
return AddressValidationResult.invalidAddress("UNO address must start with 'u' and must have 34 characters.");
}

String addressAsBtc = address.substring(1, address.length());

return super.validate(addressAsBtc);
}
}
}
28 changes: 25 additions & 3 deletions assets/src/main/java/bisq/asset/coins/Zcoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,36 @@

package bisq.asset.coins;

import org.bitcoinj.core.NetworkParameters;

import bisq.asset.AddressValidationResult;
import bisq.asset.AltCoinAccountDisclaimer;
import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.DefaultAddressValidator;

@AltCoinAccountDisclaimer("account.altcoin.popup.XZC.msg")
public class Zcoin extends Coin {

public Zcoin() {
super("Zcoin", "XZC", new DefaultAddressValidator());
public Zcoin(Network network, NetworkParameters networkParameters) {
super("Zcoin", "XZC", new XzcAddressValidator(networkParameters), network);
}

public static class XzcAddressValidator extends Base58BitcoinAddressValidator {

public XzcAddressValidator(NetworkParameters networkParameters) {
super(networkParameters);
}

@Override
public AddressValidationResult validate(String address) {
if (address == null || address.length() != 34 || !address.startsWith("a")) {
return AddressValidationResult.invalidAddress("XZC address must start with 'a' and must have 34 characters.");
}

String addressAsBtc = address.substring(1, address.length());

return super.validate(addressAsBtc);
}
}

}
3 changes: 2 additions & 1 deletion core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ market.trades.tooltip.candle.close=Close:
market.trades.tooltip.candle.high=High:
market.trades.tooltip.candle.low=Low:
market.trades.tooltip.candle.average=Average:
market.trades.tooltip.candle.median=Median:
market.trades.tooltip.candle.date=Date:

####################################################################
Expand Down Expand Up @@ -610,7 +611,7 @@ message.state.SENT=Message sent
# suppress inspection "UnusedProperty"
message.state.ARRIVED=Message arrived at peer
# suppress inspection "UnusedProperty"
message.state.STORED_IN_MAILBOX=Message stored in mailbox
message.state.STORED_IN_MAILBOX=Message of payment sent but not yet received by peer
# suppress inspection "UnusedProperty"
message.state.ACKNOWLEDGED=Peer confirmed message receipt
# suppress inspection "UnusedProperty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.time.temporal.ChronoUnit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -304,20 +305,20 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
long accumulatedVolume = 0;
long accumulatedAmount = 0;
long numTrades = set.size();
List<Long> tradePrices = new ArrayList<>(set.size());

for (TradeStatistics2 item : set) {
long tradePriceAsLong = item.getTradePrice().getValue();
if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
low = (low != 0) ? Math.max(low, tradePriceAsLong) : tradePriceAsLong;
high = (high != 0) ? Math.min(high, tradePriceAsLong) : tradePriceAsLong;
} else {
low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong;
high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong;
}
// Previously a check was done which inverted the low and high for
// crytocurrencies.
low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong;
high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong;

accumulatedVolume += (item.getTradeVolume() != null) ? item.getTradeVolume().getValue() : 0;
accumulatedAmount += item.getTradeAmount().getValue();
tradePrices.add(item.getTradePrice().getValue());
}
Collections.sort(tradePrices);

List<TradeStatistics2> list = new ArrayList<>(set);
list.sort((o1, o2) -> (o1.getTradeDate().getTime() < o2.getTradeDate().getTime() ? -1 : (o1.getTradeDate().getTime() == o2.getTradeDate().getTime() ? 0 : 1)));
Expand All @@ -327,6 +328,9 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
}

long averagePrice;
Long[] prices = new Long[tradePrices.size()];
tradePrices.toArray(prices);
long medianPrice = findMedian(prices);
boolean isBullish;
if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
isBullish = close < open;
Expand All @@ -343,9 +347,20 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
String dateString = tickUnit.ordinal() > TickUnit.DAY.ordinal() ?
formatter.formatDateTimeSpan(dateFrom, dateTo) :
formatter.formatDate(dateFrom) + " - " + formatter.formatDate(dateTo);
return new CandleData(tick, open, close, high, low, averagePrice, accumulatedAmount, accumulatedVolume,
return new CandleData(tick, open, close, high, low, averagePrice, medianPrice, accumulatedAmount, accumulatedVolume,
numTrades, isBullish, dateString);
}

Long findMedian(Long[] prices) {
int middle = prices.length / 2;
long median;
if (prices.length % 2 == 1) {
median = prices[middle];
} else {
median = MathUtils.roundDoubleToLong((prices[middle - 1] + prices[middle]) / 2.0);
}
return median;
}

Date roundToTick(Date time, TickUnit tickUnit) {
ZonedDateTime zdt = time.toInstant().atZone(ZoneId.systemDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public class CandleData {
public final long high;
public final long low;
public final long average;
public final long median;
public final long accumulatedAmount;
public final long accumulatedVolume;
public final long numTrades;
public final boolean isBullish;
public final String date;

public CandleData(long tick, long open, long close, long high, long low, long average,
public CandleData(long tick, long open, long close, long high, long low, long average, long median,
long accumulatedAmount, long accumulatedVolume, long numTrades,
boolean isBullish, String date) {
this.tick = tick;
Expand All @@ -39,6 +40,7 @@ public CandleData(long tick, long open, long close, long high, long low, long av
this.high = high;
this.low = low;
this.average = average;
this.median = median;
this.accumulatedAmount = accumulatedAmount;
this.accumulatedVolume = accumulatedVolume;
this.numTrades = numTrades;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class CandleTooltip extends GridPane {
private final Label highValue = new AutoTooltipLabel();
private final Label lowValue = new AutoTooltipLabel();
private final Label averageValue = new AutoTooltipLabel();
private final Label medianValue = new AutoTooltipLabel();
private final Label dateValue = new AutoTooltipLabel();

CandleTooltip(StringConverter<Number> priceStringConverter) {
Expand All @@ -88,6 +89,7 @@ public class CandleTooltip extends GridPane {
Label high = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.high"));
Label low = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.low"));
Label average = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.average"));
Label median = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.median"));
Label date = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.date"));
setConstraints(open, 0, 0);
setConstraints(openValue, 1, 0);
Expand All @@ -99,8 +101,10 @@ public class CandleTooltip extends GridPane {
setConstraints(lowValue, 1, 3);
setConstraints(average, 0, 4);
setConstraints(averageValue, 1, 4);
setConstraints(date, 0, 5);
setConstraints(dateValue, 1, 5);
setConstraints(median, 0, 5);
setConstraints(medianValue, 1, 5);
setConstraints(date, 0, 6);
setConstraints(dateValue, 1, 6);

ColumnConstraints columnConstraints1 = new ColumnConstraints();
columnConstraints1.setHalignment(HPos.RIGHT);
Expand All @@ -109,7 +113,7 @@ public class CandleTooltip extends GridPane {
columnConstraints2.setHgrow(Priority.ALWAYS);
getColumnConstraints().addAll(columnConstraints1, columnConstraints2);

getChildren().addAll(open, openValue, close, closeValue, high, highValue, low, lowValue, average, averageValue, date, dateValue);
getChildren().addAll(open, openValue, close, closeValue, high, highValue, low, lowValue, average, averageValue, median, medianValue, date, dateValue);
}

public void update(CandleData candleData) {
Expand All @@ -118,6 +122,7 @@ public void update(CandleData candleData) {
highValue.setText(priceStringConverter.toString(candleData.high));
lowValue.setText(priceStringConverter.toString(candleData.low));
averageValue.setText(priceStringConverter.toString(candleData.average));
medianValue.setText(priceStringConverter.toString(candleData.median));
dateValue.setText(candleData.date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public void testGetCandleData() {
long close = Fiat.parseFiat("EUR", "580").value;
long high = Fiat.parseFiat("EUR", "600").value;
long average = Fiat.parseFiat("EUR", "550").value;
long median = Fiat.parseFiat("EUR", "550").value;
long amount = Coin.parseCoin("4").value;
long volume = Fiat.parseFiat("EUR", "2200").value;
boolean isBullish = true;
Expand All @@ -161,6 +162,7 @@ public void testGetCandleData() {
assertEquals(high, candleData.high);
assertEquals(low, candleData.low);
assertEquals(average, candleData.average);
assertEquals(median, candleData.median);
assertEquals(amount, candleData.accumulatedAmount);
assertEquals(volume, candleData.accumulatedVolume);
assertEquals(isBullish, candleData.isBullish);
Expand Down