From 798468824e791fae5a4f3cce1d236213d7ec9405 Mon Sep 17 00:00:00 2001 From: yuanxueqi Date: Wed, 14 Oct 2020 18:32:34 +0800 Subject: [PATCH] july's update --- CMakeLists.txt | 2 +- demo/account/accountdemo.cpp | 9 ++- include/client/WebsocketTradeClient.h | 15 +++++ include/client/accountClient.h | 7 +- include/include.h | 5 ++ .../request/accounts/assetValuationRequest.h | 12 ++++ .../request/accounts/pointTransferRequest.h | 13 ++++ include/response/accounts/assetValuation.h | 12 ++++ include/response/accounts/pointAccount.h | 22 +++++++ include/response/reference/symbol.h | 6 ++ .../response/websocketTrade/tradeClearing.h | 34 ++++++++++ src/client/accountClient.cpp | 65 +++++++++++++++++++ src/client/referenceClient.cpp | 6 ++ src/client/websocketOrdersClient.cpp | 13 +++- src/client/websocketTradeClient.cpp | 64 ++++++++++++++++++ 15 files changed, 279 insertions(+), 6 deletions(-) create mode 100644 include/client/WebsocketTradeClient.h create mode 100644 include/request/accounts/assetValuationRequest.h create mode 100644 include/request/accounts/pointTransferRequest.h create mode 100644 include/response/accounts/assetValuation.h create mode 100644 include/response/accounts/pointAccount.h create mode 100644 include/response/websocketTrade/tradeClearing.h create mode 100644 src/client/websocketTradeClient.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4030fce..348e51e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(huobi LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(huobi_VERSION_MAJOR 2) -set(huobi_VERSION_MINOR 2) +set(huobi_VERSION_MINOR 3) configure_file( "${PROJECT_SOURCE_DIR}/huobiConfig.h.in" diff --git a/demo/account/accountdemo.cpp b/demo/account/accountdemo.cpp index 9d3a8c4..6dae6cd 100644 --- a/demo/account/accountdemo.cpp +++ b/demo/account/accountdemo.cpp @@ -4,7 +4,7 @@ using namespace std; int main() { AccountClient accountClient{APIKEY, SECRETKEY}; - vector accounts= accountClient.getAccounts(); + vector accounts = accountClient.getAccounts(); cout << accounts[0].type << endl; vector balanceVec = accountClient.getBalance(12345); @@ -22,9 +22,12 @@ int main() { FuturesTransferRequest futuresTransferRequest{"usdt", "0.2", "futures-to-pro"}; cout << accountClient.futuresTransfer(futuresTransferRequest) << endl; - vector accountAndBalanceVec= accountClient.getSubuidAccount(12345); + vector accountAndBalanceVec = accountClient.getSubuidAccount(12345); cout << accountAndBalanceVec[0].symbol << endl; - + AssetValuationRequest request; + request.accountType = "spot"; + AssetValuation assetValuation = accountClient.getAssetValuation(request); + cout << "i have " << assetValuation.balance << " in " << assetValuation.timestamp << endl; return 0; } diff --git a/include/client/WebsocketTradeClient.h b/include/client/WebsocketTradeClient.h new file mode 100644 index 0000000..44858af --- /dev/null +++ b/include/client/WebsocketTradeClient.h @@ -0,0 +1,15 @@ + +#ifndef HUOBI_WEBSOCKETTRADECLIENT_H +#define HUOBI_WEBSOCKETTRADECLIENT_H + +#include "include.h" + +struct WebsocketTradeClient { + WebsocketTradeClient(char *accessKey, char *secretKey) : signature{accessKey, secretKey} { + } + + void subTradeClearing(const char* symbol, int mode,const std::function &handler); + + Signature signature; +}; +#endif //HUOBI_WEBSOCKETTRADECLIENT_H diff --git a/include/client/accountClient.h b/include/client/accountClient.h index f4fc1e8..0e09da7 100644 --- a/include/client/accountClient.h +++ b/include/client/accountClient.h @@ -9,7 +9,6 @@ using namespace std; struct AccountClient { AccountClient(char *accessKey, char *secretKey) : signature{accessKey, secretKey} { } - vector getAccounts(); vector getBalance(long accountId); @@ -24,6 +23,12 @@ struct AccountClient { AccountTransferResponse accountTransfer(AccountTransferRequest &request); + long pointTransfer(PointTransferRequest &request); + + PointAccount getPointAccount(long subUid); + + AssetValuation getAssetValuation(AssetValuationRequest &request); + private: Signature signature; diff --git a/include/include.h b/include/include.h index 5b1fc06..e5b57d5 100644 --- a/include/include.h +++ b/include/include.h @@ -33,6 +33,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -81,6 +85,7 @@ #include #include #include +#include #include diff --git a/include/request/accounts/assetValuationRequest.h b/include/request/accounts/assetValuationRequest.h new file mode 100644 index 0000000..177f88d --- /dev/null +++ b/include/request/accounts/assetValuationRequest.h @@ -0,0 +1,12 @@ + +#ifndef HUOBI_ASSETVALUATIONREQUEST_H +#define HUOBI_ASSETVALUATIONREQUEST_H + +#include + +struct AssetValuationRequest{ + std::string accountType; + std::string valuationCurrency; + long subUid; +}; +#endif //HUOBI_ASSETVALUATIONREQUEST_H diff --git a/include/request/accounts/pointTransferRequest.h b/include/request/accounts/pointTransferRequest.h new file mode 100644 index 0000000..4dd91f2 --- /dev/null +++ b/include/request/accounts/pointTransferRequest.h @@ -0,0 +1,13 @@ + +#ifndef HUOBI_POINTTRANSFERREQUEST_H +#define HUOBI_POINTTRANSFERREQUEST_H + +#include + +struct PointTransferRequest{ + long fromUid; + std::string amount; + long toUid; + long groupId; +}; +#endif //HUOBI_POINTTRANSFERREQUEST_H diff --git a/include/response/accounts/assetValuation.h b/include/response/accounts/assetValuation.h new file mode 100644 index 0000000..23e96bd --- /dev/null +++ b/include/response/accounts/assetValuation.h @@ -0,0 +1,12 @@ + +#ifndef HUOBI_ASSETVALUATION_H +#define HUOBI_ASSETVALUATION_H + +#include + +struct AssetValuation { + std::string balance; + long timestamp; +}; + +#endif //HUOBI_ASSETVALUATION_H diff --git a/include/response/accounts/pointAccount.h b/include/response/accounts/pointAccount.h new file mode 100644 index 0000000..71d15a4 --- /dev/null +++ b/include/response/accounts/pointAccount.h @@ -0,0 +1,22 @@ + +#ifndef HUOBI_POINTACCOUNT_H +#define HUOBI_POINTACCOUNT_H + +#include +#include + +struct Group{ + long groupId; + long expiryDate; + std::string remainAmt; +}; +struct PointAccount{ + long accountId; + std::string accountStatus; + std::string acctBalance; + std::vector groupIds; +}; + + + +#endif //HUOBI_POINTACCOUNT_H diff --git a/include/response/reference/symbol.h b/include/response/reference/symbol.h index 7011c32..08c81f8 100644 --- a/include/response/reference/symbol.h +++ b/include/response/reference/symbol.h @@ -17,5 +17,11 @@ struct Symbol { double maxOrderAmt; double minOrderValue; int leverageRatio; + double limitOrderMinOrderAmt; + double limitOrderMaxOrderAmt; + double sellMarketMinOrderAmt ; + double sellMarketMaxOrderAmt ; + double buyMarketMaxOrderAmt; + double maxOrderValue; }; #endif //HUOBI_SYMBOL_H diff --git a/include/response/websocketTrade/tradeClearing.h b/include/response/websocketTrade/tradeClearing.h new file mode 100644 index 0000000..e1578e2 --- /dev/null +++ b/include/response/websocketTrade/tradeClearing.h @@ -0,0 +1,34 @@ + +#ifndef HUOBI_TRADECLEARING_H +#define HUOBI_TRADECLEARING_H + +#include + +struct TradeClearing { + std::string eventType; + std::string symbol; + long orderId; + std::string tradePrice; + std::string tradeVolume; + std::string orderSide; + std::string orderType; + bool aggressor; + long tradeId; + long tradeTime; + std::string transactFee; + std::string feeCurrency; + std::string feeDeduct; + std::string feeDeductType; + long accountId; + std::string source; + std::string orderPrice; + std::string orderSize; + std::string orderValue; + std::string clientOrderId; + std::string stopPrice; + std::string operator_; + long orderCreateTime; + std::string orderStatus; + std::string remainAmt; +}; +#endif //HUOBI_TRADECLEARING_H diff --git a/src/client/accountClient.cpp b/src/client/accountClient.cpp index ddfcf3d..a6b709a 100644 --- a/src/client/accountClient.cpp +++ b/src/client/accountClient.cpp @@ -211,3 +211,68 @@ AccountTransferResponse AccountClient::accountTransfer(AccountTransferRequest &r accountTransferResponse.transactTime = atoi(data["transact-time"].GetString()); return accountTransferResponse; } + +long AccountClient::pointTransfer(PointTransferRequest &request) { + string url = SPLICE("/v2/point/transfer?"); + rapidjson::StringBuffer strBuf; + rapidjson::Writer writer(strBuf); + writer.StartObject(); + writer.Key("fromUid"); + writer.String(to_string(request.fromUid).c_str()); + writer.Key("toUid"); + writer.String(to_string(request.toUid).c_str()); + writer.Key("groupId"); + writer.Int64(request.groupId); + writer.Key("amount"); + writer.String(request.amount.c_str()); + writer.EndObject(); + url.append(signature.createSignatureParam(POST, "/v2/point/transfer", std::map())); + string response = Rest::perform_post(url.c_str(), strBuf.GetString()); + Document d; + Value &data = d.Parse(response.c_str())["data"]; + return atol(data["transactId"].GetString()); +} + +PointAccount AccountClient::getPointAccount(long subUid) { + std::map paramMap; + string url = SPLICE("/v2/point/account?"); + paramMap["subUid"] = to_string(subUid).c_str(); + url.append(signature.createSignatureParam(GET, "/v2/point/account", paramMap)); + string response = Rest::perform_get(url.c_str()); + Document d; + Value &data = d.Parse(response.c_str())["data"]; + PointAccount pointAccount; + pointAccount.accountId = atol(data["accountId"].GetString()); + pointAccount.accountStatus = data["accountStatus"].GetString(); + pointAccount.acctBalance = data["acctBalance"].GetString(); + for (int i = 0; i < data["groupIds"].Size(); i++) { + Group group; + group.groupId = atol(data["groupIds"][i]["groupId"].GetString()); + group.expiryDate = atol(data["groupIds"][i]["expiryDate"].GetString()); + group.remainAmt = data["groupIds"][i]["remainAmt"].GetString(); + pointAccount.groupIds.push_back(group); + } + return pointAccount; +} + +AssetValuation AccountClient::getAssetValuation(AssetValuationRequest &request) { + std::map paramMap; + string url = SPLICE("/v2/account/asset-valuation?"); + paramMap["accountType"] = request.accountType.c_str(); + if (!request.valuationCurrency.empty()) { + paramMap["valuationCurrency"] = request.valuationCurrency.c_str(); + } + if (request.subUid) { + paramMap["subUid"] = to_string(request.subUid).c_str(); + } + url.append(signature.createSignatureParam(GET, "/v2/account/asset-valuation", paramMap)); + string response = Rest::perform_get(url.c_str()); + Document d; + Value &data = d.Parse(response.c_str())["data"]; + AssetValuation assetValuation; + assetValuation.timestamp = atol(data["timestamp"].GetString()); + assetValuation.balance = data["balance"].GetString(); + return assetValuation; +} + + diff --git a/src/client/referenceClient.cpp b/src/client/referenceClient.cpp index ae2e86a..566f839 100644 --- a/src/client/referenceClient.cpp +++ b/src/client/referenceClient.cpp @@ -22,6 +22,12 @@ std::vector ReferenceClient::getSymbols() { symbol.minOrderAmt = atof(data[i]["min-order-amt"].GetString()); symbol.maxOrderAmt = atof(data[i]["max-order-amt"].GetString()); symbol.minOrderValue = atof(data[i]["min-order-value"].GetString()); + symbol.limitOrderMinOrderAmt = atof(data[i]["limit-order-min-order-amt"].GetString()); + symbol.limitOrderMaxOrderAmt = atof(data[i]["limit-order-max-order-amt"].GetString()); + symbol.sellMarketMinOrderAmt = atof(data[i]["sell-market-min-order-amt"].GetString()); + symbol.sellMarketMaxOrderAmt = atof(data[i]["sell-market-max-order-amt"].GetString()); + symbol.buyMarketMaxOrderAmt = atof(data[i]["buy-market-max-order-amt"].GetString()); + symbol.maxOrderValue = atof(data[i]["max-order-value"].GetString()); if (data[i].HasMember("leverage-ratio")) symbol.leverageRatio = atoi(data[i]["leverage-ratio"].GetString()); vec.push_back(symbol); diff --git a/src/client/websocketOrdersClient.cpp b/src/client/websocketOrdersClient.cpp index 3dde43e..034698a 100644 --- a/src/client/websocketOrdersClient.cpp +++ b/src/client/websocketOrdersClient.cpp @@ -10,7 +10,7 @@ void WebsocketOrdersClient::subOrders(const char *symbol, const std::function &handler) { + string topic; + topic.append("trade.clearing#").append(symbol).append("#").append(to_string(mode)); + std::thread th(WebsocketHelper::monitor, topic, signature, [handler](Value &value) { + Value &data = value["data"]; + TradeClearing tradeClearing; + tradeClearing.eventType = data["eventType"].GetString(); + tradeClearing.symbol = data["symbol"].GetString(); + if (data.HasMember("orderId")) + tradeClearing.orderId = atol(data["orderId"].GetString()); + if (data.HasMember("clientOrderId")) + tradeClearing.clientOrderId = data["clientOrderId"].GetString(); + if (data.HasMember("orderPrice")) + tradeClearing.orderPrice = data["orderPrice"].GetString(); + if (data.HasMember("orderSize")) + tradeClearing.orderSize = data["orderSize"].GetString(); + tradeClearing.orderStatus = data["orderStatus"].GetString(); + if (data.HasMember("orderCreateTime")) + tradeClearing.orderCreateTime = atol(data["orderCreateTime"].GetString()); + if (data.HasMember("tradePrice")) + tradeClearing.tradePrice = data["tradePrice"].GetString(); + if (data.HasMember("tradeVolume")) + tradeClearing.tradeVolume = data["tradeVolume"].GetString(); + if (data.HasMember("tradeId")) + tradeClearing.tradeId = atol(data["tradeId"].GetString()); + if (data.HasMember("tradeTime")) + tradeClearing.tradeTime = atol(data["tradeTime"].GetString()); + if (data.HasMember("aggressor")) + tradeClearing.aggressor = data["aggressor"].GetBool(); + if (data.HasMember("orderStatus")) + tradeClearing.orderStatus = data["orderStatus"].GetString(); + if (data.HasMember("remainAmt")) + tradeClearing.remainAmt = data["remainAmt"].GetString(); + if (data.HasMember("orderSide")) + tradeClearing.orderSide = data["orderSide"].GetString(); + if (data.HasMember("remainAmt")) + tradeClearing.remainAmt = data["remainAmt"].GetString(); + if (data.HasMember("orderValue")) + tradeClearing.orderValue = data["orderValue"].GetString(); + if (data.HasMember("accountId")) + tradeClearing.accountId = atol(data["accountId"].GetString()); + if (data.HasMember("transactFee")) + tradeClearing.transactFee = data["transactFee"].GetString(); + if (data.HasMember("feeCurrency")) + tradeClearing.feeCurrency = data["feeCurrency"].GetString(); + if (data.HasMember("feeDeduct")) + tradeClearing.feeDeduct = data["feeDeduct"].GetString(); + if (data.HasMember("feeDeductType")) + tradeClearing.feeDeductType = data["feeDeductType"].GetString(); + if (data.HasMember("source")) + tradeClearing.source = data["source"].GetString(); + if (data.HasMember("stopPrice")) + tradeClearing.stopPrice = data["stopPrice"].GetString(); + if (data.HasMember("operator")) + tradeClearing.operator_ = data["operator"].GetString(); + handler(tradeClearing); + }); + th.detach(); +} +