From fef012b523c887b73d16351fffb17252bc5b16d4 Mon Sep 17 00:00:00 2001 From: charvam Date: Tue, 13 Jul 2021 16:31:32 +0200 Subject: [PATCH] ET-767 Fixed Poloniex CSV parser BUY transaction fee. --- gradle.properties | 2 +- .../parser/exchange/bean/PoloniexBeanV2.java | 48 +++++++++++++------ .../exchange/bean/PoloniexBeanV2Test.java | 14 +++++- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/gradle.properties b/gradle.properties index 4b38e7a2..53acb679 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -projectVersion=3.5.0 +projectVersion=3.5.1 pf4jVersion=3.4.0 xchangeVersion=5.0.4 requiredEverytradeVersion=>=20210427 diff --git a/plugin-base/src/main/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2.java b/plugin-base/src/main/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2.java index bb488d39..cb302f38 100644 --- a/plugin-base/src/main/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2.java +++ b/plugin-base/src/main/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2.java @@ -23,6 +23,7 @@ public class PoloniexBeanV2 extends ExchangeBean { public static final String UNSUPPORTED_CATEGORY = "Unsupported category "; public static final String EXCHANGE_CATEGORY = "Exchange"; + public static final String UNSUPPORTED_FEE_CURRENCY = "Unsupported fee currency "; private Instant date; private Currency marketBase; private Currency marketQuote; @@ -94,20 +95,37 @@ public TransactionCluster toTransactionCluster() { validateCurrencyPair(marketBase, marketQuote); validatePositivity(amount,total); - final BigDecimal feeValue; - if (feeCurrency.equals(marketBase)) { - feeValue = amount.subtract(quoteTotalLessFee.abs()); - } else if (feeCurrency.equals(marketQuote)) { - feeValue = total.subtract(baseTotalLessFee.abs()); - } else { - throw new DataValidationException(String.format( - "Fee currency '%s' differs from base currency '%s' and quote currency '%s'.", - feeCurrency, - marketBase, - marketQuote + final BigDecimal totalPrice; + final BigDecimal fee; + if (TransactionType.BUY.equals(type)) { + totalPrice = total; + if (feeCurrency.equals(marketBase)) { + fee = amount.subtract(quoteTotalLessFee.abs()); + } else { + throw new DataValidationException(String.format( + UNSUPPORTED_FEE_CURRENCY + "'%s' for BUY transaction on pair '%s/%s'.", + feeCurrency, + marketBase, + marketQuote + )); + } + } else if (TransactionType.SELL.equals(type)) { + totalPrice = baseTotalLessFee.abs(); + if (feeCurrency.equals(marketQuote)) { + fee = total.subtract(baseTotalLessFee.abs()); + } else { + throw new DataValidationException(String.format( + UNSUPPORTED_FEE_CURRENCY + "'%s' for SELL transaction on pair '%s/%s'.", + feeCurrency, + marketBase, + marketQuote )); + } + } else { + throw new DataValidationException(String.format("Unsupported transaction type '%s'.", type)); } - validatePositivity(feeValue); + + validatePositivity(totalPrice, fee); return new TransactionCluster( new BuySellImportedTransactionBean( @@ -116,8 +134,8 @@ public TransactionCluster toTransactionCluster() { marketBase, //base marketQuote, //quote type, //action - quoteTotalLessFee.abs(), //base quantity - evalUnitPrice(baseTotalLessFee.abs(), quoteTotalLessFee.abs()) //unit price + amount, //base quantity + evalUnitPrice(totalPrice, amount) //unit price ), List.of( new FeeRebateImportedTransactionBean( @@ -126,7 +144,7 @@ public TransactionCluster toTransactionCluster() { marketBase, marketQuote, TransactionType.FEE, - feeValue, + fee, feeCurrency ) ) diff --git a/plugin-base/src/test/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2Test.java b/plugin-base/src/test/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2Test.java index 81e2c037..f36ef1df 100644 --- a/plugin-base/src/test/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2Test.java +++ b/plugin-base/src/test/java/io/everytrade/server/plugin/impl/everytrade/parser/exchange/bean/PoloniexBeanV2Test.java @@ -16,6 +16,7 @@ import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.ExchangeBean.ILLEGAL_NEGATIVE_VALUES; import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.ExchangeBean.UNSUPPORTED_CURRENCY_PAIR; import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.bean.PoloniexBeanV1.UNSUPPORTED_CATEGORY; +import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.bean.PoloniexBeanV2.UNSUPPORTED_FEE_CURRENCY; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -46,8 +47,8 @@ void testCorrectParsingRawTransactionBuy() { Currency.BTC, Currency.USDT, TransactionType.BUY, - new BigDecimal("0.56673303"), - new BigDecimal("9360.0977267727") + new BigDecimal("0.5671584"), + new BigDecimal("9353.0776336734") ), List.of( new FeeRebateImportedTransactionBean( @@ -122,4 +123,13 @@ void testNegativeValues() { final String error = parsingProblem.getMessage(); assertTrue(error.contains(ILLEGAL_NEGATIVE_VALUES.concat("[0, 1]"))); } + + @Test + void testNotAllowedFeeCurrency() { + final String row = "2020-01-27 20:39:14,BTC/USDT,Exchange,Sell,8955,0.32647158,2923.5529989,0.075%," + + "436733958991,2921.36033415,-0.32647158,BTC,2.19266474\n"; + final ParsingProblem parsingProblem = ParserTestUtils.getParsingProblem(HEADER_CORRECT + row); + final String error = parsingProblem.getMessage(); + assertTrue(error.contains(UNSUPPORTED_FEE_CURRENCY.concat("'BTC'"))); + } } \ No newline at end of file