Skip to content

Commit

Permalink
[Code cleaning] - Fix shadow warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Nov 12, 2024
1 parent bf09e2b commit a1d0bab
Show file tree
Hide file tree
Showing 26 changed files with 662 additions and 54 deletions.
4 changes: 2 additions & 2 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ Refer to the hardcoded default json example as a model in case of doubt.

##### Notes

- `updateFrequency` is itself a json document containing all duration values as query frequencies.
See [ExchangeConfig default file](src/objects/src/exchangeconfigdefault.hpp) as an example for the syntax.
- `updateFrequency` is itself a json document containing all duration values as query frequencies.
See [ExchangeConfig default file](src/basic-objects/include/exchange-config-default.hpp) as an example for the syntax.
- Unused and not explicitly set values (so, when loaded from default values) from your personal `exchangeconfig.json` file will be logged for information about what will actually be used by `coincenter`.
2 changes: 1 addition & 1 deletion src/api/common/src/exchangeprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ PlaceOrderInfo ExchangePrivate::computeSimulatedMatchedPlacedOrderInfo(MonetaryA
const bool isSell = tradeInfo.tradeContext.side == TradeSide::kSell;

MonetaryAmount toAmount = isSell ? volume.convertTo(price) : volume;
ExchangeConfig::FeeType feeType = isTakerStrategy ? ExchangeConfig::FeeType::kTaker : ExchangeConfig::FeeType::kMaker;
ExchangeConfig::FeeType feeType = isTakerStrategy ? ExchangeConfig::FeeType::Taker : ExchangeConfig::FeeType::Maker;
toAmount = _coincenterInfo.exchangeConfig(_exchangePublic.name()).applyFee(toAmount, feeType);
PlaceOrderInfo placeOrderInfo(OrderInfo(TradedAmounts(isSell ? volume : volume.toNeutral() * price, toAmount)),
OrderId("SimulatedOrderId"));
Expand Down
2 changes: 1 addition & 1 deletion src/api/common/src/exchangepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::optional<MonetaryAmount> ExchangePublic::convert(MonetaryAmount from, Curre
return std::nullopt;
}
const ExchangeConfig::FeeType feeType =
priceOptions.isTakerStrategy() ? ExchangeConfig::FeeType::kTaker : ExchangeConfig::FeeType::kMaker;
priceOptions.isTakerStrategy() ? ExchangeConfig::FeeType::Taker : ExchangeConfig::FeeType::Maker;

if (marketOrderBookMap.empty()) {
std::lock_guard<std::recursive_mutex> guard(_publicRequestsMutex);
Expand Down
2 changes: 1 addition & 1 deletion src/api/common/test/exchangeprivateapi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ TEST_F(ExchangePrivateTest, SimulatedOrderShouldNotCallPlaceOrder) {

// In simulation mode, fee is applied
MonetaryAmount toAmount =
exchangePublic.exchangeConfig().applyFee(from.toNeutral() * askPrice1, ExchangeConfig::FeeType::kMaker);
exchangePublic.exchangeConfig().applyFee(from.toNeutral() * askPrice1, ExchangeConfig::FeeType::Maker);

EXPECT_EQ(exchangePrivate.trade(from, market.quote(), tradeOptions), TradedAmounts(from, toAmount));
}
Expand Down
14 changes: 7 additions & 7 deletions src/api/common/test/exchangepublicapi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ TEST_F(ExchangePublicConvertTest, ConvertSimple) {
exchangePublic.convert(from, toCurrency, conversionPath, fiats, marketOrderBookMap, priceOptions);
ASSERT_TRUE(ret.has_value());
MonetaryAmount res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);
EXPECT_EQ(ret, std::optional<MonetaryAmount>(res));
}

Expand All @@ -210,9 +210,9 @@ TEST_F(ExchangePublicConvertTest, ConvertDouble) {
ASSERT_TRUE(ret.has_value());

MonetaryAmount res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);
res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook2.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook2.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);
EXPECT_EQ(ret.value_or(MonetaryAmount{}), res);
}

Expand All @@ -232,7 +232,7 @@ TEST_F(ExchangePublicConvertTest, ConvertWithFiatAtBeginning) {

MonetaryAmount res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook3.convert(optRes.value_or(MonetaryAmount{}), priceOptions).value_or(MonetaryAmount{-1}),
ExchangeConfig::FeeType::kMaker);
ExchangeConfig::FeeType::Maker);

EXPECT_EQ(ret.value_or(MonetaryAmount{}), res);
}
Expand All @@ -249,11 +249,11 @@ TEST_F(ExchangePublicConvertTest, ConvertWithFiatAtEnd) {
ASSERT_TRUE(ret.has_value());

MonetaryAmount res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook1.convert(from, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);
res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook4.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook4.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);
res = exchangePublic.exchangeConfig().applyFee(
marketOrderBook3.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::kMaker);
marketOrderBook3.convert(res, priceOptions).value_or(MonetaryAmount{-1}), ExchangeConfig::FeeType::Maker);

EXPECT_EQ(ret, fiatConverter.convert(res, toCurrency));
}
Expand Down
3 changes: 1 addition & 2 deletions src/api/exchanges/src/bithumbprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,7 @@ PlaceOrderInfo BithumbPrivate::placeOrder(MonetaryAmount /*from*/, MonetaryAmoun

// Volume is gross amount if from amount is in quote currency, we should remove the fees
if (fromCurrencyCode == mk.quote()) {
ExchangeConfig::FeeType feeType =
isTakerStrategy ? ExchangeConfig::FeeType::kTaker : ExchangeConfig::FeeType::kMaker;
ExchangeConfig::FeeType feeType = isTakerStrategy ? ExchangeConfig::FeeType::Taker : ExchangeConfig::FeeType::Maker;
const ExchangeConfig& exchangeConfig = _coincenterInfo.exchangeConfig(_exchangePublic.name());
volume = exchangeConfig.applyFee(volume, feeType);
}
Expand Down
3 changes: 1 addition & 2 deletions src/api/exchanges/src/upbitprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ void UpbitPrivate::applyFee(Market mk, CurrencyCode fromCurrencyCode, bool isTak
MonetaryAmount& volume) {
if (fromCurrencyCode == mk.quote()) {
// For 'buy', from amount is fee excluded
ExchangeConfig::FeeType feeType =
isTakerStrategy ? ExchangeConfig::FeeType::kTaker : ExchangeConfig::FeeType::kMaker;
ExchangeConfig::FeeType feeType = isTakerStrategy ? ExchangeConfig::FeeType::Taker : ExchangeConfig::FeeType::Maker;
const ExchangeConfig& exchangeConfig = _coincenterInfo.exchangeConfig(_exchangePublic.name());
if (isTakerStrategy) {
from = exchangeConfig.applyFee(from, feeType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#pragma once

#include "cct_json-container.hpp"
#include <string_view>

namespace cct {
struct ExchangeConfigDefault {
static json::container Prod() {
// Use a static method instead of an inline static const variable to avoid the infamous 'static initialization order
// fiasco' problem.

static const json::container kProd = R"(
struct ExchangeConfigDefault {
static constexpr std::string_view kProd = R"(
{
"asset": {
"default": {
Expand Down Expand Up @@ -142,17 +139,11 @@ struct ExchangeConfigDefault {
"validateDepositAddressesInFile": true
}
}
}
)"_json;
return kProd;
}
}
)";

/// ExchangeInfos for tests only. Some tests rely on provided values so changing them may make them fail.
static json::container Test() {
// Use a static method instead of an inline static const variable to avoid the infamous 'static initialization order
// fiasco' problem.

static const json::container kTest = R"(
static constexpr std::string_view kTest = R"(
{
"asset": {
"default": {
Expand Down Expand Up @@ -212,7 +203,7 @@ struct ExchangeConfigDefault {
"validateApiKey": true
}
},
"tradefees": {
"tradeFees": {
"default": {
"maker": "0.1",
"taker": "0.2"
Expand All @@ -224,8 +215,6 @@ struct ExchangeConfigDefault {
}
}
}
)"_json;
return kTest;
}
)";
};
} // namespace cct
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/objects/include/exchangeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace cct {
class ExchangeConfig {
public:
enum class FeeType : int8_t { kMaker, kTaker };
enum class FeeType : int8_t { Maker, Taker };
enum class MarketDataSerialization : int8_t { kYes, kNo };

struct APIUpdateFrequencies {
Expand Down Expand Up @@ -62,7 +62,7 @@ class ExchangeConfig {
/// Apply the general maker fee defined for this exchange on given MonetaryAmount.
/// In other words, convert a gross amount into a net amount with maker fees
MonetaryAmount applyFee(MonetaryAmount ma, FeeType feeType) const {
return ma * (feeType == FeeType::kMaker ? _generalMakerRatio : _generalTakerRatio);
return ma * (feeType == FeeType::Maker ? _generalMakerRatio : _generalTakerRatio);
}

MonetaryAmount getMakerFeeRatio() const { return _generalMakerRatio; }
Expand Down
3 changes: 2 additions & 1 deletion src/objects/include/exchangeconfigparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "currencycode.hpp"
#include "currencycodevector.hpp"
#include "durationstring.hpp"
#include "exchange-config.hpp"
#include "monetaryamount.hpp"
#include "monetaryamountbycurrencyset.hpp"

Expand All @@ -24,7 +25,7 @@ class TopLevelOption {
// Top level option names
static constexpr std::string_view kAssetsOptionStr = "asset";
static constexpr std::string_view kQueryOptionStr = "query";
static constexpr std::string_view kTradeFeesOptionStr = "tradefees";
static constexpr std::string_view kTradeFeesOptionStr = "tradeFees";
static constexpr std::string_view kWithdrawOptionStr = "withdraw";

/// @brief Create a TopLevelOption from given json data.
Expand Down
6 changes: 3 additions & 3 deletions src/objects/src/exchangeconfigmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include <utility>

#include "cct_const.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "exchange-config-default.hpp"
#include "exchange-config.hpp"
#include "exchangeconfig.hpp"
#include "exchangeconfigdefault.hpp"
#include "exchangeconfigparser.hpp"
#include "http-config.hpp"
#include "monetaryamountbycurrencyset.hpp"
Expand All @@ -24,7 +24,7 @@ namespace cct {
ExchangeConfigMap ComputeExchangeConfigMap(std::string_view fileName, const json::container &jsonData) {
ExchangeConfigMap map;

const json::container &prodDefault = ExchangeConfigDefault::Prod();
json::container prodDefault = json::container::parse(ExchangeConfigDefault::kProd);

TopLevelOption assetTopLevelOption(TopLevelOption::kAssetsOptionStr, prodDefault, jsonData);
TopLevelOption queryTopLevelOption(TopLevelOption::kQueryOptionStr, prodDefault, jsonData);
Expand Down
6 changes: 3 additions & 3 deletions src/objects/src/exchangeconfigparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "currencycodevector.hpp"
#include "exchangeconfigdefault.hpp"
#include "exchange-config-default.hpp"
#include "file.hpp"
#include "loadconfiguration.hpp"
#include "unreachable.hpp"
Expand All @@ -26,7 +26,7 @@ json::container LoadExchangeConfigData(const LoadConfiguration& loadConfiguratio
case LoadConfiguration::ExchangeConfigFileType::kProd: {
std::string_view filename = loadConfiguration.exchangeConfigFileName();
File exchangeConfigFile(loadConfiguration.dataDir(), File::Type::kStatic, filename, File::IfError::kNoThrow);
json::container jsonData = ExchangeConfigDefault::Prod();
json::container jsonData = json::container::parse(ExchangeConfigDefault::kProd);
json::container exchangeConfigJsonData = exchangeConfigFile.readAllJson();
if (exchangeConfigJsonData.empty()) {
// Create a file with default values. User can then update them as he wishes.
Expand All @@ -45,7 +45,7 @@ json::container LoadExchangeConfigData(const LoadConfiguration& loadConfiguratio
return exchangeConfigJsonData;
}
case LoadConfiguration::ExchangeConfigFileType::kTest:
return ExchangeConfigDefault::Test();
return json::container::parse(ExchangeConfigDefault::kTest);
default:
unreachable();
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/test/exchangeconfig_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ TEST_F(ExchangeConfigTest, ExcludedAssets) {
}

TEST_F(ExchangeConfigTest, TradeFees) {
EXPECT_EQ(binanceExchangeInfo.applyFee(MonetaryAmount("120.5 ETH"), ExchangeConfig::FeeType::kMaker),
EXPECT_EQ(binanceExchangeInfo.applyFee(MonetaryAmount("120.5 ETH"), ExchangeConfig::FeeType::Maker),
MonetaryAmount("120.3795 ETH"));
EXPECT_EQ(binanceExchangeInfo.applyFee(MonetaryAmount("2.356097 ETH"), ExchangeConfig::FeeType::kTaker),
EXPECT_EQ(binanceExchangeInfo.applyFee(MonetaryAmount("2.356097 ETH"), ExchangeConfig::FeeType::Taker),
MonetaryAmount("2.351384806 ETH"));
}

Expand Down
7 changes: 7 additions & 0 deletions src/schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ add_unit_test(
coincenter_schema
)

add_unit_test(
exchange-config_test
test/exchange-config_test.cpp
LIBRARIES
coincenter_schema
)

add_unit_test(
general-config_test
test/general-config_test.cpp
Expand Down
21 changes: 21 additions & 0 deletions src/schema/include/exchange-asset-config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "currencycodeset.hpp"

namespace cct::schema {

struct ExchangeAssetConfig {
using trivially_relocatable = is_trivially_relocatable<CurrencyCodeSet>::type;

void mergeWith(const ExchangeAssetConfig &other) {
allExclude.insert(other.allExclude.begin(), other.allExclude.end());
preferredPaymentCurrencies.insert(other.preferredPaymentCurrencies.begin(), other.preferredPaymentCurrencies.end());
withdrawExclude.insert(other.withdrawExclude.begin(), other.withdrawExclude.end());
}

CurrencyCodeSet allExclude;
CurrencyCodeSet preferredPaymentCurrencies;
CurrencyCodeSet withdrawExclude;
};

} // namespace cct::schema
93 changes: 93 additions & 0 deletions src/schema/include/exchange-config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once

#include <array>
#include <map>

#include "cct_const.hpp"
#include "cct_string.hpp"
#include "exchange-asset-config.hpp"
#include "exchange-query-config.hpp"
#include "exchange-tradefees-config.hpp"
#include "exchange-withdraw-config.hpp"

namespace cct {

namespace schema {

namespace details {

template <class T>
struct ExchangeConfigPart {
T def; // default is a reserved keyword - we override the json field name below
std::map<string, T, std::less<>> exchange;
};

struct AllExchangeConfigsOptional {
ExchangeConfigPart<ExchangeAssetConfig> asset;
ExchangeConfigPart<ExchangeQueryConfigOptional> query;
ExchangeConfigPart<ExchangeTradeFeesConfigOptional> tradeFees;
ExchangeConfigPart<ExchangeWithdrawConfigOptional> withdraw;
};

} // namespace details

struct ExchangeConfig {
ExchangeAssetConfig asset;
ExchangeQueryConfig query;
ExchangeTradeFeesConfig tradeFees;
ExchangeWithdrawConfig withdraw;
};

struct AllExchangeConfigs {
void mergeWith(const details::AllExchangeConfigsOptional &other) {
for (int exchangePos = 0; exchangePos < kNbSupportedExchanges; ++exchangePos) {
auto &exchangeConfig = exchangeConfigs[exchangePos];
std::string_view exchangeName = kSupportedExchanges[exchangePos];

auto assetIt = other.asset.exchange.find(exchangeName);
auto queryIt = other.query.exchange.find(exchangeName);
auto tradeFeesIt = other.tradeFees.exchange.find(exchangeName);
auto withdrawIt = other.withdraw.exchange.find(exchangeName);

exchangeConfig.asset.mergeWith(other.asset.def);
if (assetIt != other.asset.exchange.end()) {
exchangeConfig.asset.mergeWith(assetIt->second);
}

exchangeConfig.query.mergeWith(other.query.def);
if (queryIt != other.query.exchange.end()) {
exchangeConfig.query.mergeWith(queryIt->second);
}

exchangeConfig.tradeFees.mergeWith(other.tradeFees.def);
if (tradeFeesIt != other.tradeFees.exchange.end()) {
exchangeConfig.tradeFees.mergeWith(tradeFeesIt->second);
}

exchangeConfig.withdraw.mergeWith(other.withdraw.def);
if (withdrawIt != other.withdraw.exchange.end()) {
exchangeConfig.withdraw.mergeWith(withdrawIt->second);
}
}
}

const ExchangeConfig &operator[](ExchangeNameEnum exchangeName) const {
return exchangeConfigs[static_cast<int>(exchangeName)];
}

std::array<ExchangeConfig, kNbSupportedExchanges> exchangeConfigs;
};

} // namespace schema

class LoadConfiguration;

schema::AllExchangeConfigs ReadExchangeConfigs(const LoadConfiguration &loadConfiguration);

} // namespace cct

template <class T>
struct glz::meta<::cct::schema::details::ExchangeConfigPart<T>> {
using V = ::cct::schema::details::ExchangeConfigPart<T>;
static constexpr auto value = object("default", &V::def, "exchange", &V::exchange);
};
Loading

0 comments on commit a1d0bab

Please sign in to comment.