Skip to content

Commit

Permalink
More strict checking of ExchangeName creation from 2 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 22, 2024
1 parent 327a33a commit 7c09065
Show file tree
Hide file tree
Showing 25 changed files with 136 additions and 81 deletions.
10 changes: 5 additions & 5 deletions src/api-objects/include/ordersconstraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OrderConstraintsBitmap {
void set(ConstraintType constraintType) { _bmp |= (1U << static_cast<uint8_t>(constraintType)); }

bool isConstrained(ConstraintType constraintType) const {
return _bmp & (1U << static_cast<uint8_t>(constraintType));
return (_bmp & (1U << static_cast<uint8_t>(constraintType))) != decltype(_bmp){};
}

bool empty() const { return _bmp == 0U; }
Expand All @@ -40,7 +40,7 @@ class OrderConstraintsBitmap {

bool isOrderIdOnlyDependent() const { return _bmp == kIdConstrained; }

bool operator==(const OrderConstraintsBitmap &) const = default;
bool operator==(const OrderConstraintsBitmap &) const noexcept = default;

private:
uint8_t _bmp{};
Expand All @@ -61,9 +61,9 @@ class OrdersConstraints {
bool isPlacedTimeAfterDefined() const { return _placedAfter != TimePoint::min(); }
bool isPlacedTimeBeforeDefined() const { return _placedBefore != TimePoint::max(); }

bool validatePlacedTime(TimePoint t) const { return t >= _placedAfter && t <= _placedBefore; }
bool validatePlacedTime(TimePoint tp) const { return tp >= _placedAfter && tp <= _placedBefore; }

bool validateTime(TimePoint t) const { return validatePlacedTime(t); }
bool validateTime(TimePoint tp) const { return validatePlacedTime(tp); }

bool validateCur(CurrencyCode cur) const { return currencyCode().isNeutral() || cur == currencyCode(); }

Expand All @@ -81,7 +81,7 @@ class OrdersConstraints {
bool isCur2Defined() const { return !_cur2.isNeutral(); }
bool isMarketDefined() const { return isCurDefined() && isCur2Defined(); }

Market market() const { return Market(_cur1, _cur2); }
Market market() const { return {_cur1, _cur2}; }

string curStr1() const { return _cur1.str(); }
string curStr2() const { return _cur2.str(); }
Expand Down
1 change: 1 addition & 0 deletions src/api/interface/test/exchangeretriever_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "cct_string.hpp"
#include "coincenterinfo.hpp"
#include "commonapi.hpp"
#include "exchange-names.hpp"
#include "exchange.hpp"
#include "exchangeconfigmap.hpp"
#include "exchangeconfigparser.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/include/coincenter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "apikeysprovider.hpp"
#include "coincenterinfo.hpp"
#include "commonapi.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "exchangepool.hpp"
#include "exchangesorchestrator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/engine/include/coincentercommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "coincentercommandtype.hpp"
#include "currencycode.hpp"
#include "depositsconstraints.hpp"
#include "exchangename.hpp"
#include "exchange-names.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"
#include "ordersconstraints.hpp"
Expand Down
25 changes: 13 additions & 12 deletions src/engine/include/exchangesorchestrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <optional>
#include <span>

#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "exchangeretriever.hpp"
#include "market.hpp"
#include "queryresulttypes.hpp"
Expand All @@ -25,24 +27,24 @@ class ExchangesOrchestrator {
MarketOrderBookConversionRates getMarketOrderBooks(Market mk, ExchangeNameSpan exchangeNames,
CurrencyCode equiCurrencyCode, std::optional<int> depth);

BalancePerExchange getBalance(std::span<const ExchangeName> privateExchangeNames,
BalancePerExchange getBalance(ExchangeNameSpan privateExchangeNames,
const BalanceOptions &balanceOptions = BalanceOptions());

WalletPerExchange getDepositInfo(std::span<const ExchangeName> privateExchangeNames, CurrencyCode depositCurrency);
WalletPerExchange getDepositInfo(ExchangeNameSpan privateExchangeNames, CurrencyCode depositCurrency);

ClosedOrdersPerExchange getClosedOrders(std::span<const ExchangeName> privateExchangeNames,
ClosedOrdersPerExchange getClosedOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &closedOrdersConstraints);

OpenedOrdersPerExchange getOpenedOrders(std::span<const ExchangeName> privateExchangeNames,
OpenedOrdersPerExchange getOpenedOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &openedOrdersConstraints);

NbCancelledOrdersPerExchange cancelOrders(std::span<const ExchangeName> privateExchangeNames,
NbCancelledOrdersPerExchange cancelOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &ordersConstraints);

DepositsPerExchange getRecentDeposits(std::span<const ExchangeName> privateExchangeNames,
DepositsPerExchange getRecentDeposits(ExchangeNameSpan privateExchangeNames,
const DepositsConstraints &depositsConstraints);

WithdrawsPerExchange getRecentWithdraws(std::span<const ExchangeName> privateExchangeNames,
WithdrawsPerExchange getRecentWithdraws(ExchangeNameSpan privateExchangeNames,
const WithdrawsConstraints &withdrawsConstraints);

MonetaryAmountPerExchange getConversion(MonetaryAmount amount, CurrencyCode targetCurrencyCode,
Expand All @@ -60,16 +62,15 @@ class ExchangesOrchestrator {
UniquePublicSelectedExchanges getExchangesTradingMarket(Market mk, ExchangeNameSpan exchangeNames);

TradeResultPerExchange trade(MonetaryAmount from, bool isPercentageTrade, CurrencyCode toCurrency,
std::span<const ExchangeName> privateExchangeNames, const TradeOptions &tradeOptions);
ExchangeNameSpan privateExchangeNames, const TradeOptions &tradeOptions);

TradeResultPerExchange smartBuy(MonetaryAmount endAmount, std::span<const ExchangeName> privateExchangeNames,
TradeResultPerExchange smartBuy(MonetaryAmount endAmount, ExchangeNameSpan privateExchangeNames,
const TradeOptions &tradeOptions);

TradeResultPerExchange smartSell(MonetaryAmount startAmount, bool isPercentageTrade,
std::span<const ExchangeName> privateExchangeNames,
const TradeOptions &tradeOptions);
ExchangeNameSpan privateExchangeNames, const TradeOptions &tradeOptions);

TradedAmountsVectorWithFinalAmountPerExchange dustSweeper(std::span<const ExchangeName> privateExchangeNames,
TradedAmountsVectorWithFinalAmountPerExchange dustSweeper(ExchangeNameSpan privateExchangeNames,
CurrencyCode currencyCode);

DeliveredWithdrawInfoWithExchanges withdraw(MonetaryAmount grossAmount, bool isPercentageWithdraw,
Expand Down
2 changes: 1 addition & 1 deletion src/engine/include/stringoptionparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "cct_string.hpp"
#include "cct_vector.hpp"
#include "currencycode.hpp"
#include "exchangename.hpp"
#include "exchange-names.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/engine/include/transferablecommandresult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "cct_smallvector.hpp"
#include "cct_type_traits.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "monetaryamount.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/engine/src/coincenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "coincenterinfo.hpp"
#include "currencycode.hpp"
#include "depositsconstraints.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "exchangepublicapi.hpp"
#include "exchangeretriever.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/engine/src/coincentercommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "coincentercommandtype.hpp"
#include "currencycode.hpp"
#include "depositsconstraints.hpp"
#include "exchangename.hpp"
#include "exchange-names.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"
#include "ordersconstraints.hpp"
Expand Down
27 changes: 13 additions & 14 deletions src/engine/src/exchangesorchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "currencycode.hpp"
#include "currencyexchangeflatset.hpp"
#include "depositsconstraints.hpp"
#include "exchange-names.hpp"
#include "exchange.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapitypes.hpp"
Expand Down Expand Up @@ -175,7 +176,7 @@ MarketOrderBookConversionRates ExchangesOrchestrator::getMarketOrderBooks(Market
return ret;
}

BalancePerExchange ExchangesOrchestrator::getBalance(std::span<const ExchangeName> privateExchangeNames,
BalancePerExchange ExchangesOrchestrator::getBalance(ExchangeNameSpan privateExchangeNames,
const BalanceOptions &balanceOptions) {
CurrencyCode equiCurrency = balanceOptions.equiCurrency();
bool withBalanceInUse =
Expand All @@ -202,7 +203,7 @@ BalancePerExchange ExchangesOrchestrator::getBalance(std::span<const ExchangeNam
return ret;
}

WalletPerExchange ExchangesOrchestrator::getDepositInfo(std::span<const ExchangeName> privateExchangeNames,
WalletPerExchange ExchangesOrchestrator::getDepositInfo(ExchangeNameSpan privateExchangeNames,
CurrencyCode depositCurrency) {
log::info("Query {} deposit information from {}", depositCurrency,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand Down Expand Up @@ -244,7 +245,7 @@ WalletPerExchange ExchangesOrchestrator::getDepositInfo(std::span<const Exchange
return ret;
}

ClosedOrdersPerExchange ExchangesOrchestrator::getClosedOrders(std::span<const ExchangeName> privateExchangeNames,
ClosedOrdersPerExchange ExchangesOrchestrator::getClosedOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &closedOrdersConstraints) {
log::info("Query closed orders matching {} on {}", closedOrdersConstraints,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand All @@ -261,7 +262,7 @@ ClosedOrdersPerExchange ExchangesOrchestrator::getClosedOrders(std::span<const E
return ret;
}

OpenedOrdersPerExchange ExchangesOrchestrator::getOpenedOrders(std::span<const ExchangeName> privateExchangeNames,
OpenedOrdersPerExchange ExchangesOrchestrator::getOpenedOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &openedOrdersConstraints) {
log::info("Query opened orders matching {} on {}", openedOrdersConstraints,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand All @@ -278,7 +279,7 @@ OpenedOrdersPerExchange ExchangesOrchestrator::getOpenedOrders(std::span<const E
return ret;
}

NbCancelledOrdersPerExchange ExchangesOrchestrator::cancelOrders(std::span<const ExchangeName> privateExchangeNames,
NbCancelledOrdersPerExchange ExchangesOrchestrator::cancelOrders(ExchangeNameSpan privateExchangeNames,
const OrdersConstraints &ordersConstraints) {
log::info("Cancel opened orders matching {} on {}", ordersConstraints,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand All @@ -293,7 +294,7 @@ NbCancelledOrdersPerExchange ExchangesOrchestrator::cancelOrders(std::span<const
return nbOrdersCancelled;
}

DepositsPerExchange ExchangesOrchestrator::getRecentDeposits(std::span<const ExchangeName> privateExchangeNames,
DepositsPerExchange ExchangesOrchestrator::getRecentDeposits(ExchangeNameSpan privateExchangeNames,
const DepositsConstraints &depositsConstraints) {
log::info("Query recent deposits matching {} on {}", depositsConstraints,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand All @@ -309,7 +310,7 @@ DepositsPerExchange ExchangesOrchestrator::getRecentDeposits(std::span<const Exc
return ret;
}

WithdrawsPerExchange ExchangesOrchestrator::getRecentWithdraws(std::span<const ExchangeName> privateExchangeNames,
WithdrawsPerExchange ExchangesOrchestrator::getRecentWithdraws(ExchangeNameSpan privateExchangeNames,
const WithdrawsConstraints &withdrawsConstraints) {
log::info("Query recent withdraws matching {} on {}", withdrawsConstraints,
ConstructAccumulatedExchangeNames(privateExchangeNames));
Expand Down Expand Up @@ -564,8 +565,7 @@ ExchangeAmountMarketsPathVector CreateExchangeAmountMarketsPathVector(ExchangeRe
} // namespace

TradeResultPerExchange ExchangesOrchestrator::trade(MonetaryAmount from, bool isPercentageTrade,
CurrencyCode toCurrency,
std::span<const ExchangeName> privateExchangeNames,
CurrencyCode toCurrency, ExchangeNameSpan privateExchangeNames,
const TradeOptions &tradeOptions) {
if (privateExchangeNames.size() == 1 && !isPercentageTrade) {
// In this special case we don't need to call the balance - call trade directly
Expand Down Expand Up @@ -614,8 +614,7 @@ TradeResultPerExchange ExchangesOrchestrator::trade(MonetaryAmount from, bool is
return LaunchAndCollectTrades(_threadPool, exchangeAmountMarketsPathVector.begin(), it, toCurrency, tradeOptions);
}

TradeResultPerExchange ExchangesOrchestrator::smartBuy(MonetaryAmount endAmount,
std::span<const ExchangeName> privateExchangeNames,
TradeResultPerExchange ExchangesOrchestrator::smartBuy(MonetaryAmount endAmount, ExchangeNameSpan privateExchangeNames,
const TradeOptions &tradeOptions) {
const CurrencyCode toCurrency = endAmount.currencyCode();
BalancePerExchange balancePerExchange = getBalance(privateExchangeNames);
Expand Down Expand Up @@ -717,7 +716,7 @@ TradeResultPerExchange ExchangesOrchestrator::smartBuy(MonetaryAmount endAmount,
}

TradeResultPerExchange ExchangesOrchestrator::smartSell(MonetaryAmount startAmount, bool isPercentageTrade,
std::span<const ExchangeName> privateExchangeNames,
ExchangeNameSpan privateExchangeNames,
const TradeOptions &tradeOptions) {
const CurrencyCode fromCurrency = startAmount.currencyCode();
// Retrieve amount per start amount currency for each exchange
Expand Down Expand Up @@ -804,8 +803,8 @@ TradeResultPerExchange ExchangesOrchestrator::smartSell(MonetaryAmount startAmou
return LaunchAndCollectTrades(_threadPool, trades.begin(), trades.end(), tradeOptions);
}

TradedAmountsVectorWithFinalAmountPerExchange ExchangesOrchestrator::dustSweeper(
std::span<const ExchangeName> privateExchangeNames, CurrencyCode currencyCode) {
TradedAmountsVectorWithFinalAmountPerExchange ExchangesOrchestrator::dustSweeper(ExchangeNameSpan privateExchangeNames,
CurrencyCode currencyCode) {
log::info("Query {} dust sweeper from {}", currencyCode, ConstructAccumulatedExchangeNames(privateExchangeNames));

ExchangeRetriever::SelectedExchanges selExchanges = _exchangeRetriever.select(
Expand Down
1 change: 1 addition & 0 deletions src/engine/src/stringoptionparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cct_string.hpp"
#include "cct_vector.hpp"
#include "currencycode.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/src/transferablecommandresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cct_exception.hpp"
#include "cct_log.hpp"
#include "coincentercommand.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "monetaryamount.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/engine/test/coincentercommandfactory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "coincentercommandtype.hpp"
#include "coincenteroptions.hpp"
#include "currencycode.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "monetaryamount.hpp"
#include "ordersconstraints.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/test/exchangesorchestrator_public_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "currencycode.hpp"
#include "currencyexchange.hpp"
#include "currencyexchangeflatset.hpp"
#include "exchange-names.hpp"
#include "exchange.hpp"
#include "exchangedata_test.hpp"
#include "exchangename.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/test/exchangesorchestrator_trade_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "balanceoptions.hpp"
#include "cct_exception.hpp"
#include "currencycode.hpp"
#include "exchange-names.hpp"
#include "exchange.hpp"
#include "exchangedata_test.hpp"
#include "exchangename.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/test/stringoptionparser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cct_string.hpp"
#include "cct_vector.hpp"
#include "currencycode.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/test/transferablecommandresult_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cct_exception.hpp"
#include "coincentercommand.hpp"
#include "coincentercommandtype.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "monetaryamount.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/objects/include/coincentercommandtype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string_view>

namespace cct {

enum class CoincenterCommandType : int8_t {
kHealthCheck,
kCurrencies,
Expand Down
16 changes: 16 additions & 0 deletions src/objects/include/exchange-names.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <span>

#include "cct_smallvector.hpp"
#include "cct_string.hpp"
#include "exchangename.hpp"

namespace cct {

using ExchangeNameSpan = std::span<const ExchangeName>;
using ExchangeNames = SmallVector<ExchangeName, 1>;

string ConstructAccumulatedExchangeNames(ExchangeNameSpan exchangeNames);

} // namespace cct
Loading

0 comments on commit 7c09065

Please sign in to comment.