22
33#include < format>
44#include < ranges>
5+ #include < stdexcept>
56#include < string>
67#include < vector>
78
1112namespace binance {
1213// TODO(mils): for keys, use `std::vector<unsigned char>` instead of string
1314
14- // / @brief load Binance configuration from env
15- // / (static member function)
16- // / @return Config object
15+ // static function
1716Config Config::from_env () {
18- const std::string apiKey = utils::Env::get_env_or_throw (" API_KEY" );
19- const std::string privateKey = utils::Env::get_env_or_throw (" PRIVATE_KEY_PATH" );
20- const std::string fixConfig = utils::Env::get_env_or_throw (" FIX_CONFIG_PATH" );
21- const std::string instStr = utils::Env::get_env_or_throw (" SYMBOLS" );
22-
23- spdlog::info (" fetched environment variable. key [API_KEY], value [{}]" ,
24- apiKey.substr (0 , 3 ) + " ..." + apiKey.substr (apiKey.length () - 3 ));
25- spdlog::info (" fetched environment variable. key [PRIVATE_KEY_PATH], value [{}]" ,
26- privateKey);
27- spdlog::info (" fetched environment variable. key [FIX_CONFIG_PATH], value [{}]" ,
28- fixConfig);
29- spdlog::info (" fetched environment variable. key [SYMBOLS], value [{}]" , instStr);
17+ const std::string api_key = utils::Env::get_env_or_throw (" API_KEY" );
18+ const std::string private_key = utils::Env::get_env_or_throw (" PRIVATE_KEY_PATH" );
19+ const std::string fix_config = utils::Env::get_env_or_throw (" FIX_CONFIG_PATH" );
20+ const std::string px_cpu_str = utils::Env::get_env_or_throw (" PX_SESSION_CPU" );
21+ const std::string tx_cpu_str = utils::Env::get_env_or_throw (" TX_SESSION_CPU" );
22+ const std::string inst_str = utils::Env::get_env_or_throw (" SYMBOLS" );
23+
24+ uint8_t px_cpu;
25+ std::errc px_ec =
26+ std::from_chars (px_cpu_str.data (), px_cpu_str.data () + px_cpu_str.size (), px_cpu)
27+ .ec ;
28+ if (px_ec != std::errc ()) {
29+ throw std::runtime_error (
30+ std::format (" could not parse px cpu, value [{}]" , px_cpu_str));
31+ }
32+
33+ uint8_t tx_cpu;
34+ std::errc tx_ec =
35+ std::from_chars (tx_cpu_str.data (), tx_cpu_str.data () + tx_cpu_str.size (), tx_cpu)
36+ .ec ;
37+ if (tx_ec != std::errc ()) {
38+ throw std::runtime_error (
39+ std::format (" could not parse tx cpu, value [{}]" , tx_cpu_str));
40+ }
41+
42+ spdlog::info (" fetched envar. key [API_KEY], value [{}]" ,
43+ api_key.substr (0 , 3 ) + " ..." + api_key.substr (api_key.length () - 3 ));
44+ spdlog::info (" fetched envar. key [PRIVATE_KEY_PATH], value [{}]" , private_key);
45+ spdlog::info (" fetched envar. key [FIX_CONFIG_PATH], value [{}]" , fix_config);
46+ spdlog::info (" fetched envar. key [SYMBOLS], value [{}]" , inst_str);
47+ spdlog::info (" fetched envar. key [PX_SESSION_CPU], value [{}]" , px_cpu_str);
48+ spdlog::info (" fetched envar. key [TX_SESSION_CPU], value [{}]" , tx_cpu_str);
3049
3150 std::vector<std::string> symbols;
32- for (auto inst : std::views::split (instStr , ' ,' )) {
51+ for (auto inst : std::views::split (inst_str , ' ,' )) {
3352 if (!inst.empty ()) {
3453 symbols.emplace_back (inst.begin (), inst.end ());
3554 }
@@ -38,7 +57,7 @@ Config Config::from_env() {
3857 spdlog::info (" MAX_DEPTH, value [{}]" , MAX_DEPTH);
3958
4059 // copy
41- return Config{apiKey, privateKey, fixConfig , symbols};
60+ return Config{api_key, private_key, fix_config , symbols, px_cpu, tx_cpu };
4261};
4362
4463} // namespace binance
0 commit comments