diff --git a/src/api-objects/include/tradeoptions.hpp b/src/api-objects/include/tradeoptions.hpp index e3d0543e..6dc7e4b6 100644 --- a/src/api-objects/include/tradeoptions.hpp +++ b/src/api-objects/include/tradeoptions.hpp @@ -68,7 +68,7 @@ class TradeOptions { string str(bool placeRealOrderInSimulationMode) const; - bool operator==(const TradeOptions &) const = default; + bool operator==(const TradeOptions &) const noexcept = default; private: Duration _maxTradeTime = kDefaultTradeDuration; diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index 7e0e56c0..00a65b6a 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -8,6 +8,13 @@ target_link_libraries(coincenter_engine PUBLIC coincenter_api-interface) target_link_libraries(coincenter_engine PUBLIC coincenter_objects) target_include_directories(coincenter_engine PUBLIC include) +add_unit_test( + coincenteroptions_test + test/coincenteroptions_test.cpp + LIBRARIES + coincenter_engine +) + add_unit_test( commandlineoptionsparser_test test/commandlineoptionsparser_test.cpp @@ -57,4 +64,4 @@ add_unit_test( test/stringoptionparser_test.cpp LIBRARIES coincenter_engine -) \ No newline at end of file +) diff --git a/src/engine/include/balanceperexchangeportfolio.hpp b/src/engine/include/balanceperexchangeportfolio.hpp index 660e4be2..96957ee7 100644 --- a/src/engine/include/balanceperexchangeportfolio.hpp +++ b/src/engine/include/balanceperexchangeportfolio.hpp @@ -1,6 +1,5 @@ #pragma once -#include "cct_const.hpp" #include "cct_json.hpp" #include "queryresulttypes.hpp" #include "simpletable.hpp" diff --git a/src/engine/include/coincenteroptions.hpp b/src/engine/include/coincenteroptions.hpp index 384b5a89..76019dbc 100644 --- a/src/engine/include/coincenteroptions.hpp +++ b/src/engine/include/coincenteroptions.hpp @@ -1,150 +1,46 @@ #pragma once -#include #include +#include #include #include -#include "apioutputtype.hpp" -#include "cct_const.hpp" +#include "coincentercommandtype.hpp" +#include "coincenteroptionsdef.hpp" #include "commandlineoption.hpp" -#include "exchangeinfomap.hpp" #include "exchangepublicapi.hpp" -#include "loadconfiguration.hpp" -#include "static_string_view_helpers.hpp" -#include "staticcommandlineoptioncheck.hpp" -#include "stringhelpers.hpp" #include "timedef.hpp" #include "tradeoptions.hpp" #include "withdrawoptions.hpp" namespace cct { -std::string_view SelectDefaultDataDir() noexcept; - -struct CoincenterCmdLineOptions { - static constexpr std::string_view kDefaultMonitoringIPAddress = "0.0.0.0"; // in Docker, localhost does not work - static constexpr int kDefaultMonitoringPort = 9091; // Prometheus default push port - static constexpr Duration kDefaultRepeatTime = TimeInS(1); - - static constexpr int64_t kDefaultTradeTimeout = - std::chrono::duration_cast(TradeOptions().maxTradeTime()).count(); - static constexpr int64_t kMinUpdatePriceTime = - std::chrono::duration_cast(TradeOptions().minTimeBetweenPriceUpdates()).count(); - static constexpr int64_t kDefaultRepeatDurationSeconds = - std::chrono::duration_cast(kDefaultRepeatTime).count(); - - static constexpr std::string_view kOutput1 = "Output format. One of ("; - static constexpr std::string_view kOutput2 = ") (default configured in general config file)"; - static constexpr std::string_view kOutput = - JoinStringView_v, kApiOutputTypeTableStr, - CharToStringView_v<'|'>, kApiOutputTypeJsonStr, kOutput2>; - - static constexpr std::string_view kData1 = "Use given 'data' directory instead of the one chosen at build time '"; - static constexpr std::string_view kData = JoinStringView_v>; - - static constexpr std::string_view kRepeat1 = "Set delay between each repeat (default: "; - static constexpr std::string_view kRepeat2 = "s)"; - - static constexpr std::string_view kRepeat = - JoinStringView_v, kRepeat2>; - - static constexpr std::string_view kLastTradesN1 = "Change number of last trades to query (default: "; - static constexpr std::string_view kLastTradesN = - JoinStringView_v, - CharToStringView_v<')'>>; - - static constexpr std::string_view kSmartBuy1 = - "Attempt to buy the specified amount in total, on matching exchange accounts (all are considered if none " - "provided)." - " The base currencies will be chosen according to the '"; - static constexpr std::string_view kSmartBuy2 = "' array defined in '"; - static constexpr std::string_view kSmartBuy3 = - "' file. " - "Standard trade options are compatible to customize the trade, and if enabled, multi trade can be used."; - static constexpr std::string_view kSmartBuy = - JoinStringView_v; - - static constexpr std::string_view kSmartSell1 = - "Attempt to sell the specified amount in total (or percentage with '%'), on matching exchange accounts (all are " - "considered if none provided)." - " The payment currencies will be chosen according to the '"; - static constexpr std::string_view kSmartSell = - JoinStringView_v; - - static constexpr std::string_view kTradeTimeout1 = "Adjust trade timeout (default: "; - static constexpr std::string_view kTradeTimeout2 = "s). Remaining orders will be cancelled after the timeout."; - static constexpr std::string_view kTradeTimeout = - JoinStringView_v, kTradeTimeout2>; - - static constexpr std::string_view kTradeUpdatePrice1 = - "Set the min time allowed between two limit price updates (default: "; - static constexpr std::string_view kTradeUpdatePrice2 = - "s). Avoids cancelling / placing new orders too often with high volumes which can be counter productive " - "sometimes."; - static constexpr std::string_view kTradeUpdatePrice = - JoinStringView_v, kTradeUpdatePrice2>; - - static constexpr std::string_view kSimulationMode1 = "Activates simulation mode only (default: "; - static constexpr std::string_view kSimulationMode2 = TradeOptions().isSimulation() ? "true" : "false"; - static constexpr std::string_view kSimulationMode3 = - "). For some exchanges, exchange can be queried in this " - "mode to validate the trade input options."; - static constexpr std::string_view kSimulationMode = - JoinStringView_v; - - static constexpr std::string_view kWithdraw1 = - "Withdraw amount from exchange 'from' to exchange 'to'." - " Amount is gross, including fees, and can be absolute or percentage of all available amount. Address and tag " - "will be retrieved" - " automatically from destination exchange and can additionally check if it matches an entry in '"; - static constexpr std::string_view kWithdraw2 = "' file."; - static constexpr std::string_view kWithdraw = JoinStringView_v; - - static constexpr std::string_view kWithdrawRefreshTime1 = - "Time interval for regular withdraw status checking during synchronous withdrawal. Default is "; - static constexpr int64_t kDefaultWithdrawRefreshTimeSeconds = - std::chrono::duration_cast(WithdrawOptions().withdrawRefreshTime()).count(); - static constexpr std::string_view kWithdrawRefreshTime2 = "s."; - static constexpr std::string_view kWithdrawRefreshTime = - JoinStringView_v, - kWithdrawRefreshTime2>; - - static constexpr std::string_view kDustSweeper = - "Attempts to clean small remaining amount of given currency on each given exchange." - " The amount is considered 'small' and eligible for dust sweeper process if the 'dustAmountsThreshold' is " - "set in " - "the config file for this currency and if starting available amount is lower than this defined threshold." - " Dust sweeper process is iterative, involving at most 'dustSweeperMaxNbTrades' max trades to be set as well " - "in " - "the config file."; - - static constexpr std::string_view kMonitoringPort1 = "Specify port of metric gateway instance (default: "; - static constexpr std::string_view kMonitoringPort = - JoinStringView_v, - CharToStringView_v<')'>>; - - static constexpr std::string_view kMonitoringIP1 = "Specify IP (v4) of metric gateway instance (default: "; - static constexpr std::string_view kMonitoringIP = - JoinStringView_v>; - - static void PrintVersion(std::string_view programName) noexcept; +class CoincenterCmdLineOptions { + public: + static std::ostream& PrintVersion(std::string_view programName, std::ostream& os) noexcept; + + constexpr CoincenterCmdLineOptions() noexcept = default; bool isSmartTrade() const noexcept; - void mergeGlobalWith(const CoincenterCmdLineOptions& other); + TradeOptions computeTradeOptions() const; + WithdrawOptions computeWithdrawOptions() const; + + std::string_view getDataDir() const { return dataDir.empty() ? SelectDefaultDataDir() : dataDir; } + + std::pair getTradeArgStr() const; + + void mergeGlobalWith(const CoincenterCmdLineOptions& rhs); - std::string_view dataDir = SelectDefaultDataDir(); + std::string_view dataDir; std::string_view apiOutputType; std::string_view logConsole; std::string_view logFile; std::optional noSecrets; - Duration repeatTime = kDefaultRepeatTime; + Duration repeatTime = CoincenterCmdLineOptionsDefinitions::kDefaultRepeatTime; - std::string_view monitoringAddress = kDefaultMonitoringIPAddress; + std::string_view monitoringAddress = CoincenterCmdLineOptionsDefinitions::kDefaultMonitoringIPAddress; std::string_view monitoringUsername; std::string_view monitoringPassword; @@ -185,8 +81,8 @@ struct CoincenterCmdLineOptions { Duration minAge{}; Duration maxAge{}; - std::string_view withdraw; - std::string_view withdrawAll; + std::string_view withdrawApply; + std::string_view withdrawApplyAll; std::string_view withdrawFee; Duration withdrawRefreshTime{WithdrawOptions().withdrawRefreshTime()}; @@ -199,312 +95,25 @@ struct CoincenterCmdLineOptions { CommandLineOptionalInt repeats; int nbLastTrades = api::ExchangePublic::kNbLastTradesDefault; - int monitoringPort = kDefaultMonitoringPort; + int monitoringPort = CoincenterCmdLineOptionsDefinitions::kDefaultMonitoringPort; int orderbookDepth = 0; bool forceMultiTrade = false; bool forceSingleTrade = false; bool tradeTimeoutMatch = false; bool tradeSim{TradeOptions().isSimulation()}; - bool tradeAsync = false; // trade fire and forget mode + bool async = false; bool help = false; bool version = false; bool useMonitoring = false; bool withBalanceInUse = false; - bool withdrawAsync = false; // withdraw fire and forget mode -}; -template -struct CoincenterAllowedOptions { - using CommandLineOptionWithValue = AllowedCommandLineOptionsBase::CommandLineOptionWithValue; - - static constexpr CommandLineOptionWithValue value[] = { - {{{"General", 100}, "help", 'h', "", "Display this information"}, &OptValueType::help}, - {{{"General", 200}, "--data", "", CoincenterCmdLineOptions::kData}, &OptValueType::dataDir}, - {{{"General", 300}, - "--log", - 'v', - "", - "Sets the log level in the console during all execution. " - "Possible values are: (off|critical|error|warning|info|debug|trace) or " - "(0-6) (overrides .log.console in general config file)"}, - &OptValueType::logConsole}, - {{{"General", 400}, "--log-console", "", "Synonym of --log"}, &OptValueType::logConsole}, - {{{"General", 400}, - "--log-file", - "", - "Sets the log level in files during all execution (overrides .log.file in general config file). " - "Number of rotating files to keep and their size is configurable in the general config file"}, - &OptValueType::logFile}, - {{{"General", 500}, "--output", 'o', "", CoincenterCmdLineOptions::kOutput}, &OptValueType::apiOutputType}, - {{{"General", 700}, - "--no-secrets", - "<[exch1,...]>", - "Do not load secrets for specified exchanges.\n" - "If no exchange is specified, no key will be loaded at all"}, - &OptValueType::noSecrets}, - {{{"General", 800}, - "--repeat", - 'r', - "<[n]>", - "Indicates how many repeats to perform for mutable data (such as market data)\n" - "Modifying requests such as trades and withdraws are not impacted by this option. " - "This is useful for monitoring for instance. 'n' is optional, if not given, will repeat endlessly"}, - &OptValueType::repeats}, - {{{"General", 900}, "--repeat-time", "