Skip to content

Commit

Permalink
Merge pull request #53 from kubacech/feature/ET-862-1INCH
Browse files Browse the repository at this point in the history
ET-862 adding code property to Currency enum to be used as currency symbol
  • Loading branch information
generalbytes authored Jun 30, 2021
2 parents b205cc1 + 9bbea64 commit 61c7955
Show file tree
Hide file tree
Showing 44 changed files with 141 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indent_size = 4
tab_width = 4
trim_trailing_whitespace = true
insert_final_newline = false
max_line_length = 120
max_line_length = 140
ij_continuation_indent_size = 4
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
Expand Down
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="120"/>
<property name="max" value="140"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projectVersion=3.3.6
projectVersion=3.3.7
pf4jVersion=3.4.0
xchangeVersion=5.0.4
requiredEverytradeVersion=>=20210427
Expand Down
28 changes: 25 additions & 3 deletions plugin-api/src/main/java/io/everytrade/server/model/Currency.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -125,22 +126,33 @@ public enum Currency {
PAXG(false, Instant.parse("2019-09-26T00:00:00Z"), "PAX Gold"),
CAKE(false, Instant.parse("2021-01-30T00:00:00Z"), "PancakeSwap"),
BAL(false, Instant.parse("2020-06-24T00:00:00Z"), "Balancer"),
BEAM(false, Instant.parse("2019-01-18T00:00:00Z"), "Beam");
BEAM(false, Instant.parse("2019-01-18T00:00:00Z"), "Beam"),
_1INCH("1INCH",false, Instant.parse("2020-12-25T00:00:00Z"), "1inch");

private final String code;
private final int decimalDigits;
private final boolean fiat;
private final Instant introduction;
private final String description;

Currency(boolean fiat, Instant introduction, String description) {
this(fiat ? 2 : 8, fiat, introduction, description);
this(null, fiat ? 2 : 8, fiat, introduction, description);
}

Currency(int decimalDigits, boolean fiat, Instant introduction, String description) {
Currency(String code, boolean fiat, Instant introduction, String description) {
this(code, fiat ? 2 : 8, fiat, introduction, description);
}

Currency(String code, int decimalDigits, boolean fiat, Instant introduction, String description) {
this.decimalDigits = decimalDigits;
this.fiat = fiat;
this.introduction = introduction;
this.description = description;
this.code = code == null ? name() : code;
}

public String code() {
return code;
}

public int getDecimalDigits() {
Expand Down Expand Up @@ -177,4 +189,14 @@ public static Set<Currency> getFiatsExcept(Set<Currency> exceptions) {
.filter(it -> !exceptions.contains(it))
.collect(Collectors.toCollection(() -> EnumSet.noneOf(Currency.class)));
}

public static Currency fromCode(String code) {
Objects.requireNonNull(code, "code is null");
for (Currency c : values()) {
if (code.equals(c.code())) {
return c;
}
}
throw new IllegalArgumentException("No enum constant " + Currency.class.getCanonicalName() + "." + code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum CurrencyPosition {BASE, QUOTE}


public CurrencyPair(String base, String quote) {
this(Currency.valueOf(base), Currency.valueOf(quote));
this(Currency.fromCode(base), Currency.fromCode(quote));
}

public CurrencyPair(Currency base, Currency quote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public Rate(
) {
this(
value,
Currency.valueOf(base),
Currency.valueOf(quote),
Currency.fromCode(base),
Currency.fromCode(quote),
validityStartIncl,
validityEndExcl,
sourceType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public BlockchainApiTransactionBean(
id = transaction.getTxHash();
timestamp = Instant.ofEpochMilli(transaction.getTimestamp());
type = transaction.isDirectionSend() ? TransactionType.SELL : TransactionType.BUY;
this.base = Currency.valueOf(base.toUpperCase());
this.quote = Currency.valueOf(quote.toUpperCase());
this.base = Currency.fromCode(base.toUpperCase());
this.quote = Currency.fromCode(quote.toUpperCase());
this.price = null; // it will be automatically added from the market in everytrade.
feeCurrency = this.base;
originalAmount = Client.satoshisToBigDecimal(transaction.getAmount()).abs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ public String getFeeCurrency() {
}

public TransactionCluster toTransactionCluster() {
final Currency base = Currency.valueOf(Objects.requireNonNull(this.base));
final Currency quote = Currency.valueOf(Objects.requireNonNull(this.quote));
final Currency base = Currency.fromCode(Objects.requireNonNull(this.base));
final Currency quote = Currency.fromCode(Objects.requireNonNull(this.quote));
try {
new CurrencyPair(base, quote);
} catch (CurrencyPair.FiatCryptoCombinationException e) {
throw new DataValidationException(e.getMessage());
}

final Currency parsedFeeCurrency = Currency.valueOf(Objects.requireNonNull(feeCurrency));
final Currency parsedFeeCurrency = Currency.fromCode(Objects.requireNonNull(feeCurrency));
final List<ImportedTransactionBean> related;
final boolean ignoredFee;
if (parsedFeeCurrency == base || parsedFeeCurrency == quote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public String getStatus() {
}

public TransactionCluster toTransactionCluster() {
final Currency baseCurrency = Currency.valueOf(base);
final Currency quoteCurrency = Currency.valueOf(quote);
final Currency baseCurrency = Currency.fromCode(base);
final Currency quoteCurrency = Currency.fromCode(quote);
try {
new CurrencyPair(baseCurrency, quoteCurrency);
} catch (CurrencyPair.FiatCryptoCombinationException e) {
Expand All @@ -148,7 +148,7 @@ public TransactionCluster toTransactionCluster() {
quoteCurrency,
TransactionType.FEE,
expense,
Currency.valueOf(expenseCurrency),
Currency.fromCode(expenseCurrency),
getRemoteUid()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class OkexApiTransactionBean {
FAST_CURRENCY_PAIRS = new HashMap<>();
for (CurrencyPair currencyPair : CurrencyPair.getTradeablePairs()) {
FAST_CURRENCY_PAIRS.put(
String.format("%s-%s", currencyPair.getBase().name(), currencyPair.getQuote().name()), currencyPair
String.format("%s-%s", currencyPair.getBase().code(), currencyPair.getQuote().code()), currencyPair
);
}
}
Expand All @@ -52,7 +52,7 @@ public OkexApiTransactionBean(OrderInfo orderInfo) {
side = TransactionType.valueOf(orderInfo.getSide().toUpperCase());
instrumentIdBase = toCurrencyPair(orderInfo.getInstrument_id()).getBase();
instrumentIdQuote = toCurrencyPair(orderInfo.getInstrument_id()).getQuote();
feeCurrency = Currency.valueOf(orderInfo.getFee_currency());
feeCurrency = Currency.fromCode(orderInfo.getFee_currency());
filledSize = new BigDecimal(orderInfo.getFilled_size());
priceAvg = new BigDecimal(orderInfo.getPrice_avg());
fee = new BigDecimal(orderInfo.getFee());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ private TransactionType getTransactionType(Order.OrderType orderType) {

private Currency convert(org.knowm.xchange.currency.Currency currency) {
try {
return Currency.valueOf(currency.getCurrencyCode());
return Currency.fromCode(currency.getCurrencyCode());
} catch (IllegalArgumentException e) {
final org.knowm.xchange.currency.Currency currencyConverted =
org.knowm.xchange.currency.Currency.getInstance(currency.getCurrencyCode()).getCommonlyUsedCurrency();
return Currency.valueOf(currencyConverted.getCurrencyCode());
return Currency.fromCode(currencyConverted.getCurrencyCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BlockchainDownloader(
String importFeesFromWithdrawals
) {
Objects.requireNonNull(cryptoCurrency);
if (!SUPPORTED_CRYPTO.contains(Currency.valueOf(cryptoCurrency))) {
if (!SUPPORTED_CRYPTO.contains(Currency.fromCode(cryptoCurrency))) {
throw new IllegalArgumentException(String.format("Unsupported crypto currency %s.", cryptoCurrency));
}
this.cryptoCurrency = cryptoCurrency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ protected TransactionType detectTransactionType(Currency fromCurrency, Currency
} else {
throw new DataValidationException(
UNSUPPORTED_CURRENCY_PAIR
.concat(fromCurrency.name())
.concat(fromCurrency.code())
.concat("/")
.concat(toCurrency.name())
.concat(toCurrency.code())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void setFee(BigDecimal value) {
@Parsed(field = "Fee Coin")
public void setFeeCurrency(String value) {
try {
feeCoin = Currency.valueOf(value);
feeCoin = Currency.fromCode(value);
} catch (IllegalArgumentException e) {
feeCoin = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void setUid(String value) {
@Parsed(field = "PAIR")
public void setPair(String value) {
final String[] values = value.split("/");
pairBase = Currency.valueOf(values[0]);
pairQuote = Currency.valueOf(values[1]);
pairBase = Currency.fromCode(values[0]);
pairQuote = Currency.fromCode(values[1]);
}

@Parsed(field = "AMOUNT")
Expand All @@ -69,7 +69,7 @@ public void setFee(BigDecimal value) {
@Parsed(field = "FEE CURRENCY")
public void setFeeCurrency(String value) {
try {
feeCurrency = Currency.valueOf(value);
feeCurrency = Currency.fromCode(value);
} catch (IllegalArgumentException e) {
feeCurrency = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setTradedPrice(BigDecimal value) {

@Parsed(field = "Currency 1")
public void setCurrency1(String value) {
this.currency1 = Currency.valueOf(value);
this.currency1 = Currency.fromCode(value);
}

@Parsed(field = "Amount (Currency 1)", defaultNullRead = "0")
Expand All @@ -67,7 +67,7 @@ public void setFee(BigDecimal value) {

@Parsed(field = "Currency 2")
public void setCurrency2(String value) {
this.currency2 = Currency.valueOf(value);
this.currency2 = Currency.fromCode(value);
}

@Parsed(field = "Order ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setSymbol(String value) {
String mappedPair;
String replaceBase = findStarts(value);
if (replaceBase != null) {
mappedPair = value.replaceFirst(replaceBase, CURRENCIES.get(replaceBase).name());
mappedPair = value.replaceFirst(replaceBase, CURRENCIES.get(replaceBase).code());
} else {
mappedPair = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void setDate(Date date) {
public void setAmount(String amount) {
String[] amountParts = amount.split(" ");
String mBase = amountParts[1];
amountCurrency = Currency.valueOf(mBase);
amountCurrency = Currency.fromCode(mBase);
if (amountParts[0].isEmpty()) {
throw new DataValidationException("BaseQuantity can not be null or empty.");
}
Expand All @@ -56,7 +56,7 @@ public void setAmount(String amount) {
@Parsed(field = "Value")
public void setValue(String value) {
String[] valueParts = value.split(" ");
valueCurrency = Currency.valueOf(valueParts[1]);
valueCurrency = Currency.fromCode(valueParts[1]);
if (valueParts[0].isEmpty()) {
this.value = BigDecimal.ZERO;
} else {
Expand All @@ -66,7 +66,7 @@ public void setValue(String value) {

@Parsed(field = "Rate")
public void setRate(String rate) {
rateCurrency = Currency.valueOf(rate.split(" ")[1]);
rateCurrency = Currency.fromCode(rate.split(" ")[1]);
}

@Parsed(field = "Fee")
Expand All @@ -77,7 +77,7 @@ public void setFee(String fee) {
} else {
this.fee = new BigDecimal(feeParts[0]);
}
feeCurrency = Currency.valueOf(feeParts[1]);
feeCurrency = Currency.fromCode(feeParts[1]);
}

@Parsed(field = "Sub Type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void setExchange(String exchange) {
String[] exchangeParts = exchange.split("-");
String mQuote = exchangeParts[0];
String mBase = exchangeParts[1];
exchangeQuote = Currency.valueOf(mQuote);
exchangeBase = Currency.valueOf(mBase);
exchangeQuote = Currency.fromCode(mQuote);
exchangeBase = Currency.fromCode(mBase);
}

@Parsed(field = "Type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void setEchange(String exchange) {
String[] exchangeParts = exchange.split("-");
String mQuote = exchangeParts[0];
String mBase = exchangeParts[1];
exchangeQuote = Currency.valueOf(mQuote);
exchangeBase = Currency.valueOf(mBase);
exchangeQuote = Currency.fromCode(mQuote);
exchangeBase = Currency.fromCode(mBase);
}

@Parsed(field = "OrderType")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void setEchange(String exchange) {
String[] exchangeParts = exchange.split("-");
String mQuote = exchangeParts[0];
String mBase = exchangeParts[1];
exchangeQuote = Currency.valueOf(mQuote);
exchangeBase = Currency.valueOf(mBase);
exchangeQuote = Currency.fromCode(mQuote);
exchangeBase = Currency.fromCode(mBase);
}

@Parsed(field = "OrderType")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setTransactionType(String value) {

@Parsed(field = "Asset")
public void setAsset(String value) {
asset = Currency.valueOf(value);
asset = Currency.fromCode(value);
}

@Parsed(field = "Quantity Transacted")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void setTradeId(String value) {
@Parsed(field = "product")
public void setProduct(String value) {
final String[] split = value.split("-");
productBase = Currency.valueOf(split[0]);
productQuote = Currency.valueOf(split[1]);
productBase = Currency.fromCode(split[0]);
productQuote = Currency.fromCode(split[1]);
}

@Parsed(field = "side")
Expand All @@ -73,7 +73,7 @@ public void setSize(BigDecimal value) {

@Parsed(field = "size unit")
public void setSizeUnit(String value) {
sizeUnit = Currency.valueOf(value);
sizeUnit = Currency.fromCode(value);
}

@Parsed(field = "price")
Expand All @@ -90,22 +90,22 @@ public void setFee(BigDecimal value) {

@Parsed(field = "price/fee/total unit")
public void setPriceFeeTotalUnit(String value) {
setPriceFeeTotalUnit = Currency.valueOf(value);
setPriceFeeTotalUnit = Currency.fromCode(value);
}

@Override
public TransactionCluster toTransactionCluster() {
if (!sizeUnit.equals(productBase)) {
throw new DataValidationException(String.format(
BASE_DIFFERS_FROM_UNIT_SIZE,
productBase.name(), sizeUnit.name()
productBase.code(), sizeUnit.code()
));
}

if (!setPriceFeeTotalUnit.equals(productQuote)) {
throw new DataValidationException(String.format(
QUOTE_DIFFERS_FROM_PRICE_FEE_TOTAL_UNIT,
productQuote.name(), setPriceFeeTotalUnit.name())
productQuote.code(), setPriceFeeTotalUnit.code())
);
}
validateCurrencyPair(productBase, productQuote);
Expand Down
Loading

0 comments on commit 61c7955

Please sign in to comment.