Skip to content

Commit

Permalink
july's update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanxueqi committed Oct 14, 2020
1 parent 28ca7f7 commit 7984688
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 6 additions & 3 deletions demo/account/accountdemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace std;

int main() {
AccountClient accountClient{APIKEY, SECRETKEY};
vector<Account> accounts= accountClient.getAccounts();
vector<Account> accounts = accountClient.getAccounts();
cout << accounts[0].type << endl;

vector<Balance> balanceVec = accountClient.getBalance(12345);
Expand All @@ -22,9 +22,12 @@ int main() {
FuturesTransferRequest futuresTransferRequest{"usdt", "0.2", "futures-to-pro"};
cout << accountClient.futuresTransfer(futuresTransferRequest) << endl;

vector<AccountAndBalance> accountAndBalanceVec= accountClient.getSubuidAccount(12345);
vector<AccountAndBalance> 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;
}

15 changes: 15 additions & 0 deletions include/client/WebsocketTradeClient.h
Original file line number Diff line number Diff line change
@@ -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<void(const TradeClearing &)> &handler);

Signature signature;
};
#endif //HUOBI_WEBSOCKETTRADECLIENT_H
7 changes: 6 additions & 1 deletion include/client/accountClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ using namespace std;
struct AccountClient {
AccountClient(char *accessKey, char *secretKey) : signature{accessKey, secretKey} {
}

vector<Account> getAccounts();

vector<Balance> getBalance(long accountId);
Expand All @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions include/include.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include <request/subUser/managesubuser.h>
#include <request/accounts/accounttransferrequest.h>
#include <response/accounts/accounttransferresponse.h>
#include <request/accounts/pointTransferRequest.h>
#include <response/accounts/pointAccount.h>
#include <request/accounts/assetValuationRequest.h>
#include <response/accounts/assetValuation.h>

#include <response/wallet/depositaddress.h>
#include <response/wallet/withdrawquota.h>
Expand Down Expand Up @@ -81,6 +85,7 @@
#include <response/websocketMarket/BBO.h>
#include <response/websocketAsset/accountsUpdate.h>
#include <response/websocketOrders/ordersUpdate.h>
#include <response/websocketTrade/tradeClearing.h>


#include <request/subUser/subUserCreationRequest.h>
Expand Down
12 changes: 12 additions & 0 deletions include/request/accounts/assetValuationRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#ifndef HUOBI_ASSETVALUATIONREQUEST_H
#define HUOBI_ASSETVALUATIONREQUEST_H

#include <string>

struct AssetValuationRequest{
std::string accountType;
std::string valuationCurrency;
long subUid;
};
#endif //HUOBI_ASSETVALUATIONREQUEST_H
13 changes: 13 additions & 0 deletions include/request/accounts/pointTransferRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#ifndef HUOBI_POINTTRANSFERREQUEST_H
#define HUOBI_POINTTRANSFERREQUEST_H

#include <string>

struct PointTransferRequest{
long fromUid;
std::string amount;
long toUid;
long groupId;
};
#endif //HUOBI_POINTTRANSFERREQUEST_H
12 changes: 12 additions & 0 deletions include/response/accounts/assetValuation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#ifndef HUOBI_ASSETVALUATION_H
#define HUOBI_ASSETVALUATION_H

#include <string>

struct AssetValuation {
std::string balance;
long timestamp;
};

#endif //HUOBI_ASSETVALUATION_H
22 changes: 22 additions & 0 deletions include/response/accounts/pointAccount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#ifndef HUOBI_POINTACCOUNT_H
#define HUOBI_POINTACCOUNT_H

#include <string>
#include <vector>

struct Group{
long groupId;
long expiryDate;
std::string remainAmt;
};
struct PointAccount{
long accountId;
std::string accountStatus;
std::string acctBalance;
std::vector<Group> groupIds;
};



#endif //HUOBI_POINTACCOUNT_H
6 changes: 6 additions & 0 deletions include/response/reference/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions include/response/websocketTrade/tradeClearing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#ifndef HUOBI_TRADECLEARING_H
#define HUOBI_TRADECLEARING_H

#include <string>

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
65 changes: 65 additions & 0 deletions src/client/accountClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rapidjson::StringBuffer> 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<std::string, const char *>()));
string response = Rest::perform_post(url.c_str(), strBuf.GetString());
Document d;
Value &data = d.Parse<kParseNumbersAsStringsFlag>(response.c_str())["data"];
return atol(data["transactId"].GetString());
}

PointAccount AccountClient::getPointAccount(long subUid) {
std::map<std::string, const char *> 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<kParseNumbersAsStringsFlag>(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<std::string, const char *> 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<kParseNumbersAsStringsFlag>(response.c_str())["data"];
AssetValuation assetValuation;
assetValuation.timestamp = atol(data["timestamp"].GetString());
assetValuation.balance = data["balance"].GetString();
return assetValuation;
}


6 changes: 6 additions & 0 deletions src/client/referenceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ std::vector<Symbol> 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);
Expand Down
13 changes: 12 additions & 1 deletion src/client/websocketOrdersClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void WebsocketOrdersClient::subOrders(const char *symbol, const std::function<vo
ordersUpdate.eventType = data["eventType"].GetString();
ordersUpdate.symbol = data["symbol"].GetString();
if (data.HasMember("orderId"))
ordersUpdate.orderId = atol(data["orderId"].GetString());
ordersUpdate.orderId = atol(data["orderId"].GetString());
if (data.HasMember("clientOrderId"))
ordersUpdate.clientOrderId = data["clientOrderId"].GetString();
if (data.HasMember("orderPrice"))
Expand All @@ -36,6 +36,17 @@ void WebsocketOrdersClient::subOrders(const char *symbol, const std::function<vo
ordersUpdate.remainAmt = data["remainAmt"].GetString();
if (data.HasMember("lastActTime"))
ordersUpdate.lastActTime = atol(data["lastActTime"].GetString());
if (data.HasMember("orderSide"))
ordersUpdate.orderSide = data["orderSide"].GetString();
if (data.HasMember("errCode"))
ordersUpdate.errCode = atoi(data["errCode"].GetString());
if (data.HasMember("errMessage"))
ordersUpdate.errMessage = data["errMessage"].GetString();
if (data.HasMember("orderValue"))
ordersUpdate.orderValue = data["orderValue"].GetString();
if (data.HasMember("type"))
ordersUpdate.type = data["type"].GetString();

handler(ordersUpdate);
});
th.detach();
Expand Down
64 changes: 64 additions & 0 deletions src/client/websocketTradeClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "client/WebsocketTradeClient.h"


void WebsocketTradeClient::subTradeClearing(const char *symbol, int mode,
const std::function<void(const TradeClearing &)> &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();
}

0 comments on commit 7984688

Please sign in to comment.