Initial commit.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
aux_source_directory(. SOURCE_LIST)
|
||||
aux_source_directory(wallets SOURCE_LIST_WALLETS)
|
||||
aux_source_directory(exchanges SOURCE_LIST_EXCHANGES)
|
||||
|
||||
add_executable(${EXECUTABLE} ${SOURCE_LIST} ${SOURCE_LIST_WALLETS} ${SOURCE_LIST_EXCHANGES})
|
||||
target_link_libraries(${EXECUTABLE} ${LIBS})
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "exchange.h"
|
||||
#include "exchanges/kraken.h"
|
||||
|
||||
|
||||
SQLPP_ALIAS_PROVIDER(bal);
|
||||
|
||||
ExchangesManager::ExchangesManager(int userId)
|
||||
{
|
||||
m_userId = userId;
|
||||
}
|
||||
|
||||
ExchangesManager::~ExchangesManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExchangesManager::analyzeUserAccounts()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_accounts = TableExchangesAccounts{};
|
||||
|
||||
// Identify coins, we need one analyzer object per exchange.
|
||||
list<ExchangeHandler*> handlers;
|
||||
for (const auto& row: db.run(select(exchanges_accounts.exchange_id,exchanges_accounts.account_id).from(exchanges_accounts).where(exchanges_accounts.user_id == m_userId)))
|
||||
{
|
||||
int exchangeId = row.exchange_id;
|
||||
int accountId = row.account_id;
|
||||
ExchangeHandler* handler;
|
||||
switch (exchangeId)
|
||||
{
|
||||
case CPFM_EXCHANGE_ID_KRAKEN:
|
||||
handler = new ExchangeHandlerKraken(m_userId,accountId);
|
||||
handlers.push_back(handler);
|
||||
break;
|
||||
default:
|
||||
lerr << "Unsupported exchange: " << exchangeId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto const& handler: handlers)
|
||||
{
|
||||
handler->analyzeUserData();
|
||||
delete handler;
|
||||
}
|
||||
}
|
||||
|
||||
ExchangeHandler::ExchangeHandler(int userId, int accountId)
|
||||
{
|
||||
m_userId = userId;
|
||||
m_accountId = accountId;
|
||||
}
|
||||
|
||||
ExchangeHandler::~ExchangeHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExchangeHandler::getLedgersTotals()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_ledgers = TableExchangesLedgers{};
|
||||
const auto coins = TableCoins{};
|
||||
|
||||
map<int,Money> calcBalances;
|
||||
map<int,Money> sqlCalcBalances;
|
||||
map<int,Money> balances;
|
||||
|
||||
for (const auto& row: db.run(select(all_of(exchanges_ledgers)).from(exchanges_ledgers).unconditionally()))
|
||||
{
|
||||
Money amount(row.amount);
|
||||
Money fee(row.fee);
|
||||
Money x = calcBalances[row.coin_id];
|
||||
|
||||
Money prevBalance(x);
|
||||
x = x + amount - fee;
|
||||
|
||||
calcBalances[row.coin_id] = x;
|
||||
balances[row.coin_id] = x;
|
||||
}
|
||||
|
||||
for (const auto& row: db.run(select(exchanges_ledgers.coin_id,sum(exchanges_ledgers.amount-exchanges_ledgers.fee).as(bal)).from(exchanges_ledgers).unconditionally().group_by(exchanges_ledgers.coin_id)))
|
||||
{
|
||||
Money x(row.bal);
|
||||
sqlCalcBalances[row.coin_id] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandler::getTradesTotals()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_trades = TableExchangesTrades{};
|
||||
const auto coins = TableCoins{};
|
||||
|
||||
map<int,Money> medianBuyPrices;
|
||||
map<int,Money> balances;
|
||||
map<int,Money> earnings;
|
||||
|
||||
map<int,std::string> coinNames;
|
||||
for (const auto& row: db.run(select(all_of(coins)).from(coins).unconditionally()))
|
||||
{
|
||||
coinNames[row.coin_id] = row.coin_short;
|
||||
}
|
||||
|
||||
for (const auto& row: db.run(select(all_of(exchanges_trades)).from(exchanges_trades).unconditionally()))
|
||||
{
|
||||
Money price(row.price);
|
||||
Money cost(row.cost);
|
||||
Money fee(row.fee);
|
||||
Money volume(row.volume);
|
||||
|
||||
int baseCoinId = row.base_coin_id;
|
||||
int quoteCoinId = row.quote_coin_id;
|
||||
int tradeType = row.type;
|
||||
|
||||
std::string sType;
|
||||
|
||||
Money prevMedianPrice(medianBuyPrices[baseCoinId]);
|
||||
Money prevBalance(balances[baseCoinId]);
|
||||
|
||||
if (tradeType == CPFM_TRADE_TYPE_BUY)
|
||||
{
|
||||
sType = "buy";
|
||||
medianBuyPrices[baseCoinId] = (medianBuyPrices[baseCoinId]*balances[baseCoinId] + price*volume) / (balances[baseCoinId]+volume);
|
||||
balances[baseCoinId] += volume;
|
||||
}
|
||||
else if (tradeType == CPFM_TRADE_TYPE_SELL)
|
||||
{
|
||||
sType = "sale";
|
||||
// median price does not change on sale, except if we reach balance 0. It should adjust correctly on next buy.
|
||||
|
||||
if (balances[baseCoinId] < volume)
|
||||
{
|
||||
lerr << "Trying to sell " << volume << " but balance is " << balances[baseCoinId];
|
||||
}
|
||||
else
|
||||
{
|
||||
balances[baseCoinId] -= volume;
|
||||
earnings[baseCoinId] += (price-medianBuyPrices[baseCoinId])*volume;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandler::emptyLedgers()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_ledgers = TableExchangesLedgers{};
|
||||
|
||||
db.run(remove_from(exchanges_ledgers).unconditionally());
|
||||
linfo << "Emptied Ledgers DB table";
|
||||
}
|
||||
|
||||
void ExchangeHandler::emptyTrades()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_trades = TableExchangesTrades{};
|
||||
|
||||
db.run(remove_from(exchanges_trades).unconditionally());
|
||||
linfo << "Emptied Trades DB table";
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_EXCHANGE_H_INCLUDED
|
||||
#define CPFM_EXCHANGE_H_INCLUDED
|
||||
|
||||
#include "pf.h"
|
||||
|
||||
#define CPFM_EXCHANGE_ID_KRAKEN 1
|
||||
|
||||
#define CPFM_LEDGER_OPERATION_TYPE_DEPOSIT 1
|
||||
#define CPFM_LEDGER_OPERATION_TYPE_WITHDRAW 2
|
||||
#define CPFM_LEDGER_OPERATION_TYPE_TRADE 3
|
||||
#define CPFM_LEDGER_OPERATION_TYPE_TRANSFER 4 // Happens for example on hardforks. Like a deposit, but not initiated by the user.
|
||||
|
||||
#define CPFM_TRADE_TYPE_BUY 1
|
||||
#define CPFM_TRADE_TYPE_SELL 2
|
||||
|
||||
#define CPFM_TRADE_ORDER_TYPE_LIMIT 1
|
||||
#define CPFM_TRADE_ORDER_TYPE_MARKET 2
|
||||
|
||||
class ExchangesManager
|
||||
{
|
||||
public:
|
||||
ExchangesManager(int userId);
|
||||
virtual ~ExchangesManager();
|
||||
|
||||
void analyzeUserAccounts();
|
||||
|
||||
protected:
|
||||
int m_userId;
|
||||
};
|
||||
|
||||
class ExchangeHandler
|
||||
{
|
||||
public:
|
||||
ExchangeHandler(int userId, int accountId);
|
||||
virtual ~ExchangeHandler();
|
||||
|
||||
virtual void analyzeUserData() = 0;
|
||||
|
||||
protected:
|
||||
void getLedgersTotals();
|
||||
void emptyLedgers();
|
||||
|
||||
void getTradesTotals();
|
||||
void emptyTrades();
|
||||
|
||||
int m_userId;
|
||||
int m_accountId;
|
||||
};
|
||||
|
||||
#endif // CPFM_EXCHANGE_H_INCLUDED
|
||||
@@ -0,0 +1,366 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
* Copyright (c) 2013 - Marco E. <marco.esposito@gmail.com>
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "kraken.h"
|
||||
|
||||
ExchangeHandlerKraken::ExchangeHandlerKraken(int userId, int accountId) : ExchangeHandler(userId, accountId)
|
||||
{
|
||||
getAccountDataFromDB();
|
||||
}
|
||||
|
||||
ExchangeHandlerKraken::~ExchangeHandlerKraken()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountDataFromDB()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_accounts = TableExchangesAccounts{};
|
||||
|
||||
for (const auto& row: db.run(select(all_of(exchanges_accounts)).from(exchanges_accounts).where(exchanges_accounts.account_id == m_accountId)))
|
||||
{
|
||||
m_apiKey = row.api_key;
|
||||
m_apiPrivateKey = row.api_private_key;
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::analyzeUserData()
|
||||
{
|
||||
getAccountBalance();
|
||||
analyzeAccountBalance();
|
||||
|
||||
//getAccountTrades();
|
||||
//analyzeAccountTrades();
|
||||
|
||||
//getAccountLedgers();
|
||||
//analyzeAccountLedgers();
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::analyzeAccountBalance()
|
||||
{
|
||||
if (!bfs::exists(getCacheFilename("balance")))
|
||||
{
|
||||
lerr << "Cached balance file not found.";
|
||||
return;
|
||||
}
|
||||
|
||||
ifstream f;
|
||||
f.open(getCacheFilename("balance"));
|
||||
json::value jvalue = json::value::parse(f);
|
||||
f.close();
|
||||
|
||||
analyzeAccountBalanceJSON(jvalue);
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::analyzeAccountBalanceJSON(json::value jvalue)
|
||||
{
|
||||
if (jvalue["error"].size())
|
||||
{
|
||||
lerr << "Error in Kraken balance answer: " << jvalue["error"][0].as_string();
|
||||
return;
|
||||
}
|
||||
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto exchanges_balances = TableExchangesBalances{};
|
||||
|
||||
for(auto it = jvalue["result"].as_object().cbegin(); it != jvalue["result"].as_object().cend(); ++it)
|
||||
{
|
||||
const json::value &v = it->second;
|
||||
|
||||
string coinName = it->first;
|
||||
Money m(v.as_string());
|
||||
|
||||
int coinId = 0;
|
||||
if (!coinName.compare("XXBT"))
|
||||
coinId = CPFM_COIN_ID_BTC;
|
||||
else if (!coinName.compare("XLTC"))
|
||||
coinId = CPFM_COIN_ID_LTC;
|
||||
else if (!coinName.compare("ZEUR"))
|
||||
coinId = CPFM_COIN_ID_EUR;
|
||||
else if (!coinName.compare("XICN"))
|
||||
coinId = CPFM_COIN_ID_ICN;
|
||||
else if (!coinName.compare("XETC"))
|
||||
coinId = CPFM_COIN_ID_ETC;
|
||||
else if (!coinName.compare("XETH"))
|
||||
coinId = CPFM_COIN_ID_ETH;
|
||||
else if (!coinName.compare("BCH"))
|
||||
coinId = CPFM_COIN_ID_BCH;
|
||||
|
||||
if (!coinId)
|
||||
{
|
||||
lerr << "Unknown coin [" << coinName << "]. Ignoring balance for this coin.";
|
||||
}
|
||||
else
|
||||
{
|
||||
db(insert_into(exchanges_balances).set(
|
||||
exchanges_balances.account_id = m_accountId,
|
||||
exchanges_balances.coin_id = coinId,
|
||||
exchanges_balances.balance = m.toBoostMpf()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__int64 ExchangeHandlerKraken::getNonce()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
string ExchangeHandlerKraken::getCacheFilename(string operation)
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "data/cache/kraken/" << m_userId << "-" << operation;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountBalance()
|
||||
{
|
||||
linfo << "Getting user balance.";
|
||||
|
||||
if (!bfs::exists(getCacheFilename("balance")))
|
||||
{
|
||||
getAccountBalanceFromAPI();
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountLedgers()
|
||||
{
|
||||
linfo << "Getting user ledgers.";
|
||||
|
||||
if (!bfs::exists(getCacheFilename("ledgers")))
|
||||
{
|
||||
getAccountLedgersFromAPI();
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountTrades()
|
||||
{
|
||||
linfo << "Getting user trades.";
|
||||
|
||||
if (!bfs::exists(getCacheFilename("trades")))
|
||||
{
|
||||
getAccountTradesFromAPI();
|
||||
}
|
||||
}
|
||||
|
||||
vector<unsigned char> ExchangeHandlerKraken::sha256(const string& data)
|
||||
{
|
||||
vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
|
||||
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init(&ctx);
|
||||
SHA256_Update(&ctx, data.c_str(), data.length());
|
||||
SHA256_Final(digest.data(), &ctx);
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
vector<unsigned char> ExchangeHandlerKraken::b64_decode(const string& data)
|
||||
{
|
||||
BIO* b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
||||
|
||||
BIO* bmem = BIO_new_mem_buf((void*)data.c_str(),data.length());
|
||||
bmem = BIO_push(b64, bmem);
|
||||
|
||||
std::vector<unsigned char> output(data.length());
|
||||
int decoded_size = BIO_read(bmem, output.data(), output.size());
|
||||
BIO_free_all(bmem);
|
||||
|
||||
if (decoded_size < 0)
|
||||
throw std::runtime_error("failed while decoding base64.");
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
string ExchangeHandlerKraken::b64_encode(const vector<unsigned char>& data)
|
||||
{
|
||||
BIO* b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
||||
|
||||
BIO* bmem = BIO_new(BIO_s_mem());
|
||||
b64 = BIO_push(b64, bmem);
|
||||
|
||||
BIO_write(b64, data.data(), data.size());
|
||||
BIO_flush(b64);
|
||||
|
||||
BUF_MEM* bptr = NULL;
|
||||
BIO_get_mem_ptr(b64, &bptr);
|
||||
|
||||
std::string output(bptr->data, bptr->length);
|
||||
BIO_free_all(b64);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
vector<unsigned char> ExchangeHandlerKraken::hmac_sha512(const vector<unsigned char>& data, const vector<unsigned char>& key)
|
||||
{
|
||||
unsigned int len = EVP_MAX_MD_SIZE;
|
||||
vector<unsigned char> digest(len);
|
||||
|
||||
HMAC_CTX ctx;
|
||||
HMAC_CTX_init(&ctx);
|
||||
|
||||
HMAC_Init_ex(&ctx, key.data(), key.size(), EVP_sha512(), NULL);
|
||||
HMAC_Update(&ctx, data.data(), data.size());
|
||||
HMAC_Final(&ctx, digest.data(), &len);
|
||||
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
string ExchangeHandlerKraken::getSignature(const string& sRequestURL, const __int64& nonce, const string& sBody)
|
||||
{
|
||||
vector<unsigned char> data(sRequestURL.begin(), sRequestURL.end());
|
||||
|
||||
stringstream ss;
|
||||
ss << nonce;
|
||||
string sNonce = ss.str();
|
||||
vector<unsigned char> nonce_postdata = sha256(sNonce + sBody);
|
||||
|
||||
data.insert(data.end(), nonce_postdata.begin(), nonce_postdata.end());
|
||||
|
||||
return b64_encode( hmac_sha512(data, b64_decode(m_apiPrivateKey)) );
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountBalanceFromAPI()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/0/private/Balance";
|
||||
http_client apiclient(KRAKEN_API_URL);
|
||||
|
||||
__int64 nonce = getNonce();
|
||||
stringstream ss;
|
||||
ss << "nonce=" << nonce;
|
||||
string signature = getSignature(sRequestURL,nonce,ss.str());
|
||||
|
||||
http_request request(methods::POST);
|
||||
request.headers().add("API-Key", m_apiKey);
|
||||
request.headers().add("API-Sign", signature);
|
||||
request.set_request_uri(sRequestURL);
|
||||
request.set_body( ss.str(), "application/x-www-form-urlencoded" );
|
||||
|
||||
apiclient.request(request).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename("balance"));
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query Kraken account balance";
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountLedgersFromAPI()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/0/private/Ledgers";
|
||||
http_client apiclient(KRAKEN_API_URL);
|
||||
|
||||
__int64 nonce = getNonce();
|
||||
stringstream ss;
|
||||
ss << "nonce=" << nonce;
|
||||
string signature = getSignature(sRequestURL,nonce,ss.str());
|
||||
|
||||
http_request request(methods::POST);
|
||||
request.headers().add("API-Key", m_apiKey);
|
||||
request.headers().add("API-Sign", signature);
|
||||
request.set_request_uri(sRequestURL);
|
||||
request.set_body( ss.str(), "application/x-www-form-urlencoded" );
|
||||
|
||||
apiclient.request(request).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename("ledgers"));
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query Kraken account balance";
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeHandlerKraken::getAccountTradesFromAPI()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/0/private/TradesHistory";
|
||||
http_client apiclient(KRAKEN_API_URL);
|
||||
|
||||
__int64 nonce = getNonce();
|
||||
stringstream ss;
|
||||
ss << "nonce=" << nonce;
|
||||
string signature = getSignature(sRequestURL,nonce,ss.str());
|
||||
|
||||
http_request request(methods::POST);
|
||||
request.headers().add("API-Key", m_apiKey);
|
||||
request.headers().add("API-Sign", signature);
|
||||
request.set_request_uri(sRequestURL);
|
||||
request.set_body( ss.str(), "application/x-www-form-urlencoded" );
|
||||
|
||||
apiclient.request(request).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename("trades"));
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query Kraken account balance";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_EXCHANGE_KRAKEN_H_INCLUDED
|
||||
#define CPFM_EXCHANGE_KRAKEN_H_INCLUDED
|
||||
|
||||
#include "exchange.h"
|
||||
|
||||
#define KRAKEN_API_URL "https://api.kraken.com"
|
||||
|
||||
class ExchangeHandlerKraken : public ExchangeHandler
|
||||
{
|
||||
public:
|
||||
ExchangeHandlerKraken(int userId, int accountId);
|
||||
virtual ~ExchangeHandlerKraken();
|
||||
|
||||
virtual void analyzeUserData();
|
||||
|
||||
private:
|
||||
__int64 getNonce();
|
||||
string getSignature(const string& sRequestURL, const __int64& nonce, const string& sBody);
|
||||
string getCacheFilename(string operation);
|
||||
|
||||
void getAccountBalance();
|
||||
void getAccountBalanceFromAPI();
|
||||
|
||||
void getAccountLedgers();
|
||||
void getAccountLedgersFromAPI();
|
||||
|
||||
void getAccountTrades();
|
||||
void getAccountTradesFromAPI();
|
||||
|
||||
void analyzeAccountBalance();
|
||||
void analyzeAccountBalanceJSON(json::value jvalue);
|
||||
|
||||
void getAccountDataFromDB();
|
||||
|
||||
string m_apiKey;
|
||||
string m_apiPrivateKey;
|
||||
|
||||
vector<unsigned char> sha256(const string& data);
|
||||
vector<unsigned char> b64_decode(const string& data);
|
||||
string b64_encode(const vector<unsigned char>& data);
|
||||
vector<unsigned char> hmac_sha512(const vector<unsigned char>& data, const vector<unsigned char>& key);
|
||||
};
|
||||
|
||||
#endif // CPFM_EXCHANGE_KRAKEN_H_INCLUDED
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include "log.h"
|
||||
|
||||
using namespace farm;
|
||||
using namespace log;
|
||||
|
||||
const char* ErrorLogger::name() { return "ERROR"; }
|
||||
const char* ErrorLogger::color() { return L_Red; }
|
||||
const char* WarningLogger::name() { return "WARN "; }
|
||||
const char* WarningLogger::color() { return L_Yellow; }
|
||||
const char* InfoLogger::name() { return "INFO "; }
|
||||
const char* InfoLogger::color() { return L_White; }
|
||||
const char* DebugLogger::name() { return "DEBUG"; }
|
||||
const char* DebugLogger::color() { return L_Teal; }
|
||||
|
||||
LoggerBase::LoggerBase()
|
||||
{
|
||||
m_logFileName = "pf.log";
|
||||
}
|
||||
|
||||
LoggerBase::~LoggerBase()
|
||||
{
|
||||
writeLogToOutput();
|
||||
//writeLogToFile();
|
||||
}
|
||||
|
||||
string LoggerBase::getDateTimeString()
|
||||
{
|
||||
time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
char buf[100];
|
||||
if (strftime(buf, 100, "%y-%m-%d %T", localtime(&rawTime)) == 0)
|
||||
buf[0] = 0;
|
||||
|
||||
string s(buf);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void LoggerBase::writeLogToOutput()
|
||||
{
|
||||
cout << m_buffer.str() << L_Reset << endl;
|
||||
}
|
||||
|
||||
void LoggerBase::writeLogToFile()
|
||||
{
|
||||
ofstream f(m_logFileName,ios::app);
|
||||
|
||||
if (!f.fail())
|
||||
{
|
||||
f << m_buffer.str() << L_Reset << endl;
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_LOG_H_INCLUDED
|
||||
#define CPFM_LOG_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define llog(X) farm::log::Logger<X>()
|
||||
#define lerr llog(farm::log::ErrorLogger)
|
||||
#define linfo llog(farm::log::InfoLogger)
|
||||
#define lwarn llog(farm::log::WarningLogger)
|
||||
#define ldebug llog(farm::log::DebugLogger)
|
||||
|
||||
namespace farm { namespace log {
|
||||
|
||||
#define L_Reset "\x1b[0m" // Text Reset
|
||||
|
||||
// Regular Colors
|
||||
#define L_Black "\x1b[30m" // Black
|
||||
#define L_Coal "\x1b[90m" // Black
|
||||
#define L_Gray "\x1b[37m" // White
|
||||
#define L_White "\x1b[97m" // White
|
||||
#define L_Maroon "\x1b[31m" // Red
|
||||
#define L_Red "\x1b[91m" // Red
|
||||
#define L_Green "\x1b[32m" // Green
|
||||
#define L_Lime "\x1b[92m" // Green
|
||||
#define L_Orange "\x1b[33m" // Yellow
|
||||
#define L_Yellow "\x1b[93m" // Yellow
|
||||
#define L_Navy "\x1b[34m" // Blue
|
||||
#define L_Blue "\x1b[94m" // Blue
|
||||
#define L_Violet "\x1b[35m" // Purple
|
||||
#define L_Purple "\x1b[95m" // Purple
|
||||
#define L_Teal "\x1b[36m" // Cyan
|
||||
#define L_Cyan "\x1b[96m" // Cyan
|
||||
|
||||
#define L_BlackBold "\x1b[1;30m" // Black
|
||||
#define L_CoalBold "\x1b[1;90m" // Black
|
||||
#define L_GrayBold "\x1b[1;37m" // White
|
||||
#define L_WhiteBold "\x1b[1;97m" // White
|
||||
#define L_MaroonBold "\x1b[1;31m" // Red
|
||||
#define L_RedBold "\x1b[1;91m" // Red
|
||||
#define L_GreenBold "\x1b[1;32m" // Green
|
||||
#define L_LimeBold "\x1b[1;92m" // Green
|
||||
#define L_OrangeBold "\x1b[1;33m" // Yellow
|
||||
#define L_YellowBold "\x1b[1;93m" // Yellow
|
||||
#define L_NavyBold "\x1b[1;34m" // Blue
|
||||
#define L_BlueBold "\x1b[1;94m" // Blue
|
||||
#define L_VioletBold "\x1b[1;35m" // Purple
|
||||
#define L_PurpleBold "\x1b[1;95m" // Purple
|
||||
#define L_TealBold "\x1b[1;36m" // Cyan
|
||||
#define L_CyanBold "\x1b[1;96m" // Cyan
|
||||
|
||||
// Background
|
||||
#define L_OnBlack "\x1b[40m" // Black
|
||||
#define L_OnCoal "\x1b[100m" // Black
|
||||
#define L_OnGray "\x1b[47m" // White
|
||||
#define L_OnWhite "\x1b[107m" // White
|
||||
#define L_OnMaroon "\x1b[41m" // Red
|
||||
#define L_OnRed "\x1b[101m" // Red
|
||||
#define L_OnGreen "\x1b[42m" // Green
|
||||
#define L_OnLime "\x1b[102m" // Green
|
||||
#define L_OnOrange "\x1b[43m" // Yellow
|
||||
#define L_OnYellow "\x1b[103m" // Yellow
|
||||
#define L_OnNavy "\x1b[44m" // Blue
|
||||
#define L_OnBlue "\x1b[104m" // Blue
|
||||
#define L_OnViolet "\x1b[45m" // Purple
|
||||
#define L_OnPurple "\x1b[105m" // Purple
|
||||
#define L_OnTeal "\x1b[46m" // Cyan
|
||||
#define L_OnCyan "\x1b[106m" // Cyan
|
||||
|
||||
// Underline
|
||||
#define L_BlackUnder "\x1b[4;30m" // Black
|
||||
#define L_GrayUnder "\x1b[4;37m" // White
|
||||
#define L_MaroonUnder "\x1b[4;31m" // Red
|
||||
#define L_GreenUnder "\x1b[4;32m" // Green
|
||||
#define L_OrangeUnder "\x1b[4;33m" // Yellow
|
||||
#define L_NavyUnder "\x1b[4;34m" // Blue
|
||||
#define L_VioletUnder "\x1b[4;35m" // Purple
|
||||
#define L_TealUnder "\x1b[4;36m" // Cyan
|
||||
|
||||
|
||||
struct DefaultLogger { static const char* name(); static const char* color(); };
|
||||
struct ErrorLogger: public DefaultLogger { static const char* name(); static const char* color(); };
|
||||
struct WarningLogger: public DefaultLogger { static const char* name(); static const char* color(); };
|
||||
struct InfoLogger: public DefaultLogger { static const char* name(); static const char* color(); };
|
||||
struct DebugLogger: public DefaultLogger { static const char* name(); static const char* color(); };
|
||||
|
||||
class LoggerBase
|
||||
{
|
||||
public:
|
||||
LoggerBase();
|
||||
virtual ~LoggerBase();
|
||||
|
||||
protected:
|
||||
string getDateTimeString();
|
||||
|
||||
stringstream m_buffer;
|
||||
|
||||
private:
|
||||
void writeLogToOutput();
|
||||
void writeLogToFile();
|
||||
|
||||
string m_logFileName;
|
||||
};
|
||||
|
||||
template <class channel>
|
||||
class Logger : public LoggerBase
|
||||
{
|
||||
public:
|
||||
Logger()
|
||||
{
|
||||
m_name = channel::name();
|
||||
m_color = channel::color();
|
||||
|
||||
m_buffer << getDateTimeString() << " | " << m_color << m_name << L_Reset << " | ";
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Logger& operator<<(T const& t) { m_buffer << t; return *this; }
|
||||
|
||||
private:
|
||||
string m_name;
|
||||
string m_color;
|
||||
};
|
||||
|
||||
}} // namespace end
|
||||
|
||||
#endif // CPFM_LOG_H_INCLUDED
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include "pf.h"
|
||||
|
||||
static volatile int stopProgram = 0;
|
||||
PortfolioManager* portfolioManager = NULL;
|
||||
|
||||
void signalHandler(int sig)
|
||||
{
|
||||
stopProgram = 1;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
portfolioManager = new PortfolioManager();
|
||||
|
||||
signal(SIGINT, signalHandler);
|
||||
signal(SIGTERM, signalHandler);
|
||||
|
||||
portfolioManager->doTestStuff();
|
||||
|
||||
/*
|
||||
portfolioManager->startThread();
|
||||
|
||||
while (!stopProgram)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SwitchToThread();
|
||||
Sleep (1000);
|
||||
#else
|
||||
pthread_yield();
|
||||
sleep (1);
|
||||
#endif
|
||||
}
|
||||
|
||||
portfolioManager->stopThread();
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
#include "money.h"
|
||||
|
||||
Money::Money ()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Money::Money (bmp::mpf_float_50 x)
|
||||
{
|
||||
m_amount = x;
|
||||
}
|
||||
|
||||
Money::Money (int x)
|
||||
{
|
||||
m_amount = x;
|
||||
}
|
||||
|
||||
Money::Money (const std::string& s)
|
||||
{
|
||||
bmp::mpf_float_50 x(s);
|
||||
m_amount = x;
|
||||
}
|
||||
|
||||
Money::Money (const web::json::value& x)
|
||||
{
|
||||
if (x.is_string())
|
||||
{
|
||||
bmp::mpf_float_50 a(x.as_string());
|
||||
m_amount = a;
|
||||
}
|
||||
else if (x.is_integer())
|
||||
{
|
||||
m_amount = x.as_integer();
|
||||
}
|
||||
else if (x.is_double())
|
||||
{
|
||||
// Can't use double, or precision will be lost.
|
||||
// And way too much time will be spent wondering why a simple multiplication does not work as expected.
|
||||
bmp::mpf_float_50 a(x.serialize());
|
||||
m_amount = a;
|
||||
}
|
||||
else
|
||||
{
|
||||
lerr << "Unsupported json conversion [" << x << "]. Setting value to 0.";
|
||||
m_amount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Money::~Money()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Money& Money::operator=(const bmp::mpf_float_50& x)
|
||||
{
|
||||
m_amount = x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator=(const std::string& s)
|
||||
{
|
||||
bmp::mpf_float_50 x(s);
|
||||
m_amount = x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator=(const int& x)
|
||||
{
|
||||
m_amount = x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator+=(const Money& x)
|
||||
{
|
||||
m_amount+=x.m_amount;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator-=(const Money& x)
|
||||
{
|
||||
m_amount-=x.m_amount;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator*=(const Money& x)
|
||||
{
|
||||
m_amount*=x.m_amount;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Money& Money::operator/=(const Money& x)
|
||||
{
|
||||
m_amount/=x.m_amount;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const Money& a, const int& b)
|
||||
{
|
||||
if (b == 0)
|
||||
{
|
||||
return (abs(a.m_amount) < std::numeric_limits<bmp::mpf_float_50>::epsilon());
|
||||
}
|
||||
else
|
||||
{
|
||||
return (a.m_amount == b);
|
||||
}
|
||||
}
|
||||
|
||||
bool operator<(const Money& a, const int& b)
|
||||
{
|
||||
// First, check if equal. We need to rely on epsilon.
|
||||
if (abs(a.m_amount-b) < std::numeric_limits<bmp::mpf_float_50>::epsilon())
|
||||
return false;
|
||||
|
||||
return (a.m_amount<b);
|
||||
}
|
||||
|
||||
bool operator==(const Money& a, const Money& b)
|
||||
{
|
||||
if (abs(b.m_amount) < std::numeric_limits<bmp::mpf_float_50>::epsilon())
|
||||
{
|
||||
return (abs(a.m_amount) < std::numeric_limits<bmp::mpf_float_50>::epsilon());
|
||||
}
|
||||
else
|
||||
{
|
||||
return (a.m_amount == b.m_amount);
|
||||
}
|
||||
}
|
||||
|
||||
bool operator<(const Money& a, const Money& b)
|
||||
{
|
||||
// First, check if equal. We need to rely on epsilon.
|
||||
if (abs(a.m_amount-b.m_amount) < std::numeric_limits<bmp::mpf_float_50>::epsilon())
|
||||
return false;
|
||||
|
||||
return (a.m_amount<b.m_amount);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Money& obj)
|
||||
{
|
||||
if (obj == 0)
|
||||
{
|
||||
os << "0";
|
||||
}
|
||||
else
|
||||
{
|
||||
stringstream ss;
|
||||
ss << std::fixed << std::setprecision(18) << obj.m_amount;
|
||||
os << ss.str();
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_MONEY_H_INCLUDED
|
||||
#define CPFM_MONEY_H_INCLUDED
|
||||
|
||||
#include <sstream>
|
||||
#include <boost/multiprecision/gmp.hpp>
|
||||
#include <cpprest/json.h>
|
||||
|
||||
using namespace std;
|
||||
namespace bmp = boost::multiprecision;
|
||||
|
||||
class Money
|
||||
{
|
||||
public:
|
||||
Money();
|
||||
Money(bmp::mpf_float_50 x);
|
||||
Money(const std::string& s);
|
||||
Money(const web::json::value& x);
|
||||
Money(int x);
|
||||
virtual ~Money();
|
||||
|
||||
Money& operator=(const bmp::mpf_float_50& x);
|
||||
Money& operator=(const std::string& s);
|
||||
Money& operator=(const int& x);
|
||||
|
||||
Money& operator+=(const Money& x);
|
||||
friend Money operator+(Money a, const Money& b) { a += b; return a; }
|
||||
|
||||
Money& operator-=(const Money& x);
|
||||
friend Money operator-(Money a, const Money& b) { a -= b; return a; }
|
||||
|
||||
Money& operator*=(const Money& x);
|
||||
friend Money operator*(Money a, const Money& b) { a *= b; return a; }
|
||||
|
||||
Money& operator/=(const Money& x);
|
||||
friend Money operator/(Money a, const Money& b) { a /= b; return a; }
|
||||
|
||||
Money operator-() { return Money(-m_amount); }
|
||||
|
||||
friend bool operator==(const Money& a, const int& b);
|
||||
friend bool operator!=(const Money& a, const int& b) { return !operator==(a,b); }
|
||||
friend bool operator<(const Money& a, const int& b);
|
||||
friend bool operator>(const Money& a, const int& b) { return operator<(b,a); }
|
||||
friend bool operator<=(const Money& a, const int& b) { return !operator>(a,b); }
|
||||
friend bool operator>=(const Money& a, const int& b) { return !operator<(a,b); }
|
||||
|
||||
friend bool operator==(const Money& a, const Money& b);
|
||||
friend bool operator!=(const Money& a, const Money& b) { return !operator==(a,b); }
|
||||
friend bool operator<(const Money& a, const Money& b);
|
||||
friend bool operator>(const Money& a, const Money& b) { return operator<(b,a); }
|
||||
friend bool operator<=(const Money& a, const Money& b) { return !operator>(a,b); }
|
||||
friend bool operator>=(const Money& a, const Money& b) { return !operator<(a,b); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Money& obj);
|
||||
|
||||
bmp::mpf_float_50& toBoostMpf() { return m_amount; }
|
||||
|
||||
private:
|
||||
bmp::mpf_float_50 m_amount;
|
||||
};
|
||||
|
||||
#endif // CPFM_MONEY_H_INCLUDED
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "pf.h"
|
||||
#include "wallet.h"
|
||||
#include "exchange.h"
|
||||
#include "exchanges/kraken.h"
|
||||
#include "pricesource.h"
|
||||
|
||||
|
||||
PortfolioManager::PortfolioManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PortfolioManager::~PortfolioManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void PortfolioManager::doTestStuff()
|
||||
{
|
||||
// Test user 1
|
||||
emptyUserBalances(1);
|
||||
|
||||
ExchangesManager exchangesManager(1);
|
||||
exchangesManager.analyzeUserAccounts();
|
||||
|
||||
WalletsManager walletsManager(1);
|
||||
walletsManager.analyzeUserWallets();
|
||||
|
||||
displayUserBalances(1);
|
||||
|
||||
// Test user 2
|
||||
emptyUserBalances(2);
|
||||
|
||||
ExchangesManager exchangesManagerTwo(2);
|
||||
exchangesManagerTwo.analyzeUserAccounts();
|
||||
|
||||
WalletsManager walletsManagerTwo(2);
|
||||
walletsManagerTwo.analyzeUserWallets();
|
||||
|
||||
displayUserBalances(2);
|
||||
}
|
||||
|
||||
void PortfolioManager::emptyUserBalances(int userId)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
const auto exchanges_balances = TableExchangesBalances{};
|
||||
|
||||
db.run(remove_from(wallets_balances).unconditionally());
|
||||
linfo << "Emptied wallets balances DB table for user " << userId;
|
||||
|
||||
db.run(remove_from(exchanges_balances).unconditionally());
|
||||
linfo << "Emptied exchanges balances DB table for user " << userId;
|
||||
}
|
||||
|
||||
void PortfolioManager::displayUserBalances(int userId)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
const auto exchanges_accounts = TableExchangesAccounts{};
|
||||
const auto exchanges_balances = TableExchangesBalances{};
|
||||
|
||||
map<int,Money> totalBalances;
|
||||
for (const auto& row: db.run(select(wallets_balances.balance, wallets_balances.coin_id).from(wallets_balances.cross_join(wallets)).where(wallets_balances.wallet_id == wallets.wallet_id and wallets.user_id == userId)))
|
||||
{
|
||||
Money m(row.balance);
|
||||
totalBalances[row.coin_id] += m;
|
||||
}
|
||||
|
||||
for (const auto& row: db.run(select(exchanges_balances.balance, exchanges_balances.coin_id).from(exchanges_balances.cross_join(exchanges_accounts)).where(exchanges_balances.account_id == exchanges_accounts.account_id and exchanges_accounts.user_id == userId)))
|
||||
{
|
||||
Money m(row.balance);
|
||||
totalBalances[row.coin_id] += m;
|
||||
}
|
||||
|
||||
Money total;
|
||||
PriceSourceCryptoWatch ps;
|
||||
for (const auto& balance: totalBalances)
|
||||
{
|
||||
if (balance.second != 0)
|
||||
{
|
||||
cout << getCoinName(balance.first) << " : " << balance.second << " | " << balance.second * ps.getCoinPrice(balance.first) << endl;
|
||||
total += balance.second * ps.getCoinPrice(balance.first);
|
||||
}
|
||||
}
|
||||
cout << "---------------" << endl;
|
||||
cout << "Total : " << total << " EUR." << endl;
|
||||
}
|
||||
|
||||
void PortfolioManager::run()
|
||||
{
|
||||
while (!m_bStopThread)
|
||||
{
|
||||
if (m_bStopThread)
|
||||
break;
|
||||
|
||||
#ifdef _WIN32
|
||||
SwitchToThread();
|
||||
Sleep (1000);
|
||||
#else
|
||||
pthread_yield();
|
||||
sleep (1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
string getCoinName(int coinId)
|
||||
{
|
||||
string s;
|
||||
|
||||
switch(coinId)
|
||||
{
|
||||
case CPFM_COIN_ID_BTC:
|
||||
s = "BTC";
|
||||
break;
|
||||
case CPFM_COIN_ID_LTC:
|
||||
s = "LTC";
|
||||
break;
|
||||
case CPFM_COIN_ID_EUR:
|
||||
s = "EUR";
|
||||
break;
|
||||
case CPFM_COIN_ID_ICN:
|
||||
s = "ICN";
|
||||
break;
|
||||
case CPFM_COIN_ID_ETC:
|
||||
s = "ETC";
|
||||
break;
|
||||
case CPFM_COIN_ID_ETH:
|
||||
s = "ETH";
|
||||
break;
|
||||
case CPFM_COIN_ID_BCH:
|
||||
s = "BCH";
|
||||
break;
|
||||
case CPFM_COIN_ID_BTG:
|
||||
s = "BTG";
|
||||
break;
|
||||
default:
|
||||
s = "Error!";
|
||||
break;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_CPFM_H_INCLUDED
|
||||
#define CPFM_CPFM_H_INCLUDED
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#include <boost/multiprecision/gmp.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <cpprest/http_client.h>
|
||||
#include <cpprest/json.h>
|
||||
|
||||
#include "run.h"
|
||||
#include "sql.h"
|
||||
#include "log.h"
|
||||
#include "money.h"
|
||||
|
||||
#define CPFM_COIN_ID_BTC 1
|
||||
#define CPFM_COIN_ID_LTC 2
|
||||
#define CPFM_COIN_ID_EUR 3
|
||||
#define CPFM_COIN_ID_ICN 4
|
||||
#define CPFM_COIN_ID_ETC 5
|
||||
#define CPFM_COIN_ID_ETH 6
|
||||
#define CPFM_COIN_ID_BCH 7
|
||||
#define CPFM_COIN_ID_BTG 8
|
||||
|
||||
namespace bmp = boost::multiprecision;
|
||||
namespace bfs = boost::filesystem;
|
||||
using namespace std;
|
||||
using namespace web;
|
||||
using namespace web::http;
|
||||
using namespace web::http::client;
|
||||
using namespace boost::posix_time;
|
||||
|
||||
class PortfolioManager : public Runnable
|
||||
{
|
||||
public:
|
||||
PortfolioManager();
|
||||
virtual ~PortfolioManager();
|
||||
|
||||
void doTestStuff();
|
||||
void emptyUserBalances(int userId);
|
||||
void displayUserBalances(int userId);
|
||||
|
||||
protected:
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
string getCoinName(int coinId);
|
||||
|
||||
#endif // CPFM_CPFM_H_INCLUDED
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "pricesource.h"
|
||||
|
||||
PriceSource::PriceSource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PriceSource::~PriceSource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Money PriceSource::getCoinPrice(int coinId)
|
||||
{
|
||||
getData();
|
||||
return getCoinPriceFromData(coinId);
|
||||
}
|
||||
|
||||
PriceSourceCryptoWatch::PriceSourceCryptoWatch()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PriceSourceCryptoWatch::~PriceSourceCryptoWatch()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
string PriceSourceCryptoWatch::getCacheFilename()
|
||||
{
|
||||
return "data/cache/cryptowatch.prices";
|
||||
}
|
||||
|
||||
void PriceSourceCryptoWatch::getData()
|
||||
{
|
||||
if (bfs::exists(getCacheFilename()))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/markets/prices";
|
||||
http_client apiclient("https://api.cryptowat.ch");
|
||||
apiclient.request(methods::GET,sRequestURL).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename());
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query cryptowat.ch prices";
|
||||
}
|
||||
}
|
||||
|
||||
Money PriceSourceCryptoWatch::getCoinPriceFromData(int coinId)
|
||||
{
|
||||
ifstream f;
|
||||
f.open(getCacheFilename());
|
||||
json::value jvalue = json::value::parse(f);
|
||||
f.close();
|
||||
|
||||
Money m;
|
||||
|
||||
switch (coinId)
|
||||
{
|
||||
case CPFM_COIN_ID_BTC:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:btceur"]);
|
||||
m = x;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_LTC:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:ltceur"]);
|
||||
m = x;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_EUR:
|
||||
{
|
||||
m = 1;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_ICN:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:icnbtc"]);
|
||||
Money y (jvalue["result"]["kraken:btceur"]);
|
||||
ldebug << "ICN is not quoted in EUR. BTC price is : " << x;
|
||||
m = x*y;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_ETC:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:etceur"]);
|
||||
m = x;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_ETH:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:etheur"]);
|
||||
m = x;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_BCH:
|
||||
{
|
||||
Money x (jvalue["result"]["kraken:bcheur"]);
|
||||
m = x;
|
||||
break;
|
||||
}
|
||||
case CPFM_COIN_ID_BTG:
|
||||
{
|
||||
Money x (jvalue["result"]["bitfinex:btgbtc"]);
|
||||
Money y (jvalue["result"]["kraken:btceur"]);
|
||||
ldebug << "BTG is not quoted in EUR. BTC price is : " << x;
|
||||
m = x*y;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
m = 0;
|
||||
lerr << "Unsupported coin: " << coinId << ". Returning 0.";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_PRICESOURCE_H_INCLUDED
|
||||
#define CPFM_PRICESOURCE_H_INCLUDED
|
||||
|
||||
#include "pf.h"
|
||||
|
||||
class PriceSource
|
||||
{
|
||||
public:
|
||||
PriceSource();
|
||||
~PriceSource();
|
||||
|
||||
Money getCoinPrice(int coinId);
|
||||
|
||||
protected:
|
||||
virtual void getData() = 0;
|
||||
virtual Money getCoinPriceFromData(int coinId) = 0;
|
||||
};
|
||||
|
||||
class PriceSourceCryptoWatch : public PriceSource
|
||||
{
|
||||
public:
|
||||
PriceSourceCryptoWatch();
|
||||
~PriceSourceCryptoWatch();
|
||||
|
||||
protected:
|
||||
virtual void getData();
|
||||
virtual Money getCoinPriceFromData(int coinId);
|
||||
string getCacheFilename();
|
||||
};
|
||||
|
||||
#endif // CPFM_PRICESOURCE_H_INCLUDED
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_RUN_H_INCLUDED
|
||||
#define CPFM_RUN_H_INCLUDED
|
||||
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
class Runnable
|
||||
{
|
||||
public:
|
||||
Runnable() { m_bStopThread = false; };
|
||||
~Runnable() { };
|
||||
|
||||
void startThread()
|
||||
{
|
||||
m_threadObj = this;
|
||||
pthread_create (&m_threadId, NULL, &threadMethod, m_threadObj);
|
||||
}
|
||||
|
||||
virtual void stopThread()
|
||||
{
|
||||
// maybe something here
|
||||
m_bStopThread = true;
|
||||
pthread_join (m_threadId,NULL);
|
||||
}
|
||||
|
||||
bool isTimeReached (time_t timeToCheck)
|
||||
{
|
||||
time_t timeNow = time(NULL);
|
||||
return (timeNow > timeToCheck);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void run() = 0;
|
||||
|
||||
static void* threadMethod(void* arg)
|
||||
{
|
||||
((Runnable*)arg)->run();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Runnable* m_threadObj;
|
||||
bool m_bStopThread;
|
||||
pthread_t m_threadId;
|
||||
};
|
||||
|
||||
|
||||
#endif // CPFM_RUN_H_INCLUDED
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sql.h"
|
||||
|
||||
std::shared_ptr<mysql::connection_config> getMysqlConfig()
|
||||
{
|
||||
auto sqlConfig = std::make_shared<mysql::connection_config>();
|
||||
sqlConfig->user = "pf";
|
||||
sqlConfig->database = "pf";
|
||||
sqlConfig->password = "pf";
|
||||
//sqlConfig->debug = true;
|
||||
|
||||
return sqlConfig;
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "wallet.h"
|
||||
#include "wallets/btc.h"
|
||||
#include "wallets/bch.h"
|
||||
#include "wallets/eth.h"
|
||||
|
||||
WalletsManager::WalletsManager(int userId)
|
||||
{
|
||||
m_userId = userId;
|
||||
}
|
||||
|
||||
WalletsManager::~WalletsManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WalletsManager::analyzeUserWallets()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
|
||||
// Identify coins, we need one analyzer object per coin.
|
||||
list<WalletHandler*> handlers;
|
||||
for (const auto& row: db.run(select(wallets.type_id).from(wallets).where(wallets.user_id == m_userId).group_by(wallets.type_id)))
|
||||
{
|
||||
int typeId = row.type_id;
|
||||
WalletHandler* walletHandler;
|
||||
switch (typeId)
|
||||
{
|
||||
case CPFM_WALLET_TYPE_ID_BTC:
|
||||
walletHandler = new WalletHandlerBTC(m_userId);
|
||||
handlers.push_back(walletHandler);
|
||||
break;
|
||||
case CPFM_WALLET_TYPE_ID_BCH:
|
||||
walletHandler = new WalletHandlerBCH(m_userId);
|
||||
handlers.push_back(walletHandler);
|
||||
break;
|
||||
case CPFM_WALLET_TYPE_ID_ETH:
|
||||
walletHandler = new WalletHandlerETH(m_userId);
|
||||
handlers.push_back(walletHandler);
|
||||
break;
|
||||
default:
|
||||
lerr << "Unsupported wallet type: " << typeId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
emptyWalletsTx();
|
||||
|
||||
for (auto const& handler: handlers)
|
||||
{
|
||||
handler->analyzeUserWallets();
|
||||
delete handler;
|
||||
}
|
||||
}
|
||||
|
||||
void WalletsManager::emptyWalletsTx()
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
|
||||
list<int> userWallets;
|
||||
for (const auto& row: db.run(select(wallets.wallet_id).from(wallets).where(wallets.user_id == m_userId)))
|
||||
{
|
||||
userWallets.push_back(row.wallet_id);
|
||||
}
|
||||
|
||||
for (const auto& id: userWallets)
|
||||
{
|
||||
db.run(remove_from(wallets_tx).where(wallets_tx.wallet_id == id));
|
||||
}
|
||||
|
||||
linfo << "Emptied Wallets Tx DB table for user " << m_userId;
|
||||
}
|
||||
|
||||
WalletHandler::WalletHandler(int userId)
|
||||
{
|
||||
m_userId = userId;
|
||||
}
|
||||
|
||||
WalletHandler::~WalletHandler()
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_WALLET_H_INCLUDED
|
||||
#define CPFM_WALLET_H_INCLUDED
|
||||
|
||||
#include "pf.h"
|
||||
|
||||
#define CPFM_WALLET_TYPE_ID_BTC 1
|
||||
#define CPFM_WALLET_TYPE_ID_ETH 2
|
||||
#define CPFM_WALLET_TYPE_ID_BCH 3
|
||||
|
||||
class WalletsManager
|
||||
{
|
||||
public:
|
||||
WalletsManager(int userId);
|
||||
~WalletsManager();
|
||||
|
||||
void analyzeUserWallets();
|
||||
|
||||
private:
|
||||
void emptyWalletsTx();
|
||||
|
||||
int m_userId;
|
||||
};
|
||||
|
||||
class WalletHandler
|
||||
{
|
||||
public:
|
||||
WalletHandler(int userId);
|
||||
virtual ~WalletHandler();
|
||||
|
||||
virtual void analyzeUserWallets() = 0;
|
||||
|
||||
protected:
|
||||
int m_userId;
|
||||
};
|
||||
|
||||
#endif // CPFM_WALLET_H_INCLUDED
|
||||
@@ -0,0 +1,316 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "wallets/bch.h"
|
||||
|
||||
WalletHandlerBCH::WalletHandlerBCH(int userId) : WalletHandler(userId)
|
||||
{
|
||||
}
|
||||
|
||||
WalletHandlerBCH::~WalletHandlerBCH()
|
||||
{
|
||||
}
|
||||
|
||||
void WalletHandlerBCH::analyzeUserWallets()
|
||||
{
|
||||
linfo << "Analyzing user " << m_userId << " BCH wallets.";
|
||||
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
const auto wallets_addresses = TableWalletsAddresses{};
|
||||
|
||||
map<int, list<string>> userWallets;
|
||||
list<string> userAddresses;
|
||||
for (const auto &row : db.run(select(wallets.wallet_id, wallets.type_id, wallets_addresses.address).from(wallets.cross_join(wallets_addresses)).where(wallets.user_id == m_userId and wallets.wallet_id == wallets_addresses.wallet_id and wallets.type_id == CPFM_WALLET_TYPE_ID_BCH)))
|
||||
{
|
||||
userWallets[row.wallet_id].push_back(row.address);
|
||||
userAddresses.push_back(row.address);
|
||||
}
|
||||
|
||||
for (const auto &w : userWallets)
|
||||
{
|
||||
addUserWalletDataToDB(w.first, w.second, userAddresses);
|
||||
analyzeUserWalletData(w.first);
|
||||
}
|
||||
}
|
||||
|
||||
void WalletHandlerBCH::analyzeUserWalletData(int walletId)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
|
||||
// Loop is on blockchain tx id, so first, get blockhain tx id list.
|
||||
list<string> blockchainTxs;
|
||||
for (const auto &row : db.run(select(wallets_tx.blockchain_tx_id).from(wallets_tx).where(wallets_tx.wallet_id == walletId).group_by(wallets_tx.blockchain_tx_id).order_by(wallets_tx.timestamp.asc())))
|
||||
{
|
||||
blockchainTxs.push_back(row.blockchain_tx_id);
|
||||
}
|
||||
|
||||
ldebug << "------------------------------------------------------------";
|
||||
|
||||
Money walletBalance = 0;
|
||||
Money walletInputs = 0;
|
||||
Money walletOutputs = 0;
|
||||
Money walletOutputFees = 0;
|
||||
for (const auto &txId: blockchainTxs)
|
||||
{
|
||||
Money txAmount = 0;
|
||||
Money txFee = 0;
|
||||
|
||||
for (const auto &row : db.run(select(wallets_tx.amount, wallets_tx.fee).from(wallets_tx).where(wallets_tx.blockchain_tx_id == txId and wallets_tx.wallet_id == walletId)))
|
||||
{
|
||||
Money amount(row.amount);
|
||||
Money fee(row.fee);
|
||||
|
||||
if (txFee == 0)
|
||||
txFee = fee;
|
||||
|
||||
txAmount += amount;
|
||||
}
|
||||
|
||||
txAmount += txFee;
|
||||
|
||||
if (txAmount < 0)
|
||||
walletOutputs += txAmount;
|
||||
else
|
||||
walletInputs += txAmount;
|
||||
|
||||
walletOutputFees += txFee;
|
||||
|
||||
walletBalance = walletInputs + walletOutputs - walletOutputFees;
|
||||
|
||||
string sReason = "?";
|
||||
|
||||
if (txAmount < 0)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
ldebug << "Tx: " << txId
|
||||
<< ". Amount: " << txAmount
|
||||
<< ". Outputs: " << walletOutputs
|
||||
<< ". Inputs: " << walletInputs
|
||||
<< ". Fees: " << txFee
|
||||
<< ". Balance: " << walletBalance
|
||||
<< ". Reason: " << sReason;
|
||||
*/
|
||||
}
|
||||
|
||||
linfo << "Wallet " << walletId << " balance is: " << walletBalance;
|
||||
|
||||
db(insert_into(wallets_balances).set(wallets_balances.wallet_id = walletId, wallets_balances.coin_id = CPFM_COIN_ID_BCH, wallets_balances.balance = walletBalance.toBoostMpf()));
|
||||
}
|
||||
|
||||
void WalletHandlerBCH::addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses)
|
||||
{
|
||||
linfo << "Analyzing wallet " << walletId << ".";
|
||||
|
||||
for (const auto &a : walletAddresses)
|
||||
{
|
||||
AddressHandlerBCH addr(a, walletId);
|
||||
addr.setWalletAddresses(walletAddresses);
|
||||
addr.setUserAddresses(userAddresses);
|
||||
addr.addAddressDataToDB();
|
||||
}
|
||||
}
|
||||
|
||||
AddressHandlerBCH::AddressHandlerBCH(string address, int walletId)
|
||||
{
|
||||
m_address = address;
|
||||
m_walletId = walletId;
|
||||
}
|
||||
|
||||
AddressHandlerBCH::~AddressHandlerBCH()
|
||||
{
|
||||
}
|
||||
|
||||
string AddressHandlerBCH::getCacheFilename()
|
||||
{
|
||||
string cacheFilename("data/cache/bch/" + m_address);
|
||||
return cacheFilename;
|
||||
}
|
||||
|
||||
void AddressHandlerBCH::getAddressData()
|
||||
{
|
||||
linfo << "Getting data for address: " << m_address << ".";
|
||||
|
||||
if (!bfs::exists(getCacheFilename()))
|
||||
{
|
||||
getBlockchainAddressData();
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerBCH::addAddressDataToDB()
|
||||
{
|
||||
linfo << "Analyzing data for address: " << m_address << ".";
|
||||
|
||||
getAddressData();
|
||||
addCachedAddressDataToDB();
|
||||
}
|
||||
|
||||
void AddressHandlerBCH::getBlockchainAddressData()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/insight-api/txs/?address=";
|
||||
sRequestURL += m_address;
|
||||
http_client apiclient("https://cashexplorer.bitcoin.com");
|
||||
apiclient.request(methods::GET, sRequestURL).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask) {
|
||||
ofstream f;
|
||||
f.open(getCacheFilename());
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch (const http::http_exception &e)
|
||||
{
|
||||
lerr << "Failed to query cashexplorer.bitcoin.com about " << m_address;
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerBCH::addCachedAddressDataToDB()
|
||||
{
|
||||
ifstream f;
|
||||
f.open(getCacheFilename());
|
||||
json::value jvalue = json::value::parse(f);
|
||||
f.close();
|
||||
|
||||
addAddressDataJSONToDB(jvalue);
|
||||
}
|
||||
|
||||
void AddressHandlerBCH::addAddressDataJSONToDB(json::value jvalue)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
|
||||
string myAddr = m_address;
|
||||
linfo << "Analyzing address: " << myAddr;
|
||||
|
||||
for (int i = 0; i < jvalue["txs"].size(); i++)
|
||||
{
|
||||
string txid = jvalue["txs"][i]["txid"].as_string();
|
||||
int timestamp = jvalue["txs"][i]["time"].as_integer();
|
||||
|
||||
Money txTotalOutputAmount;
|
||||
Money txTotalInputAmount;
|
||||
Money txMyOutputAmount;
|
||||
Money txMyInputAmount;
|
||||
bool bToMyAddr = false;
|
||||
bool bFromMyAddr = false;
|
||||
list<string> inputAddrList;
|
||||
list<string> outputAddrList;
|
||||
|
||||
for (int j = 0; j < jvalue["txs"][i]["vin"].size(); j++)
|
||||
{
|
||||
string inputAddr = jvalue["txs"][i]["vin"][j]["addr"].as_string();
|
||||
Money amount(jvalue["txs"][i]["vin"][j]["value"]);
|
||||
txTotalInputAmount += amount;
|
||||
|
||||
if (inputAddr == myAddr)
|
||||
{
|
||||
bFromMyAddr = true;
|
||||
txMyInputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
inputAddrList.push_back(inputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < jvalue["txs"][i]["vout"].size(); j++)
|
||||
{
|
||||
string outputAddr = jvalue["txs"][i]["vout"][j]["scriptPubKey"]["addresses"][0].as_string();
|
||||
Money amount(jvalue["txs"][i]["vout"][j]["value"]);
|
||||
txTotalOutputAmount += amount;
|
||||
|
||||
if (outputAddr == myAddr)
|
||||
{
|
||||
bToMyAddr = true;
|
||||
txMyOutputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
outputAddrList.push_back(outputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
Money amount;
|
||||
Money fee;
|
||||
if (bFromMyAddr)
|
||||
{
|
||||
amount = -txMyInputAmount;
|
||||
fee = txTotalInputAmount - txTotalOutputAmount;
|
||||
|
||||
for (auto const &addr : inputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const &userAddr : m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
lwarn << "User configuration is missing this address: " << addr;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto const &addr : outputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const &userAddr : m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
linfo << "Potential other address to analyze: " << L_Cyan << addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bToMyAddr)
|
||||
{
|
||||
amount = txMyOutputAmount;
|
||||
fee = 0;
|
||||
}
|
||||
|
||||
db(insert_into(wallets_tx).set(wallets_tx.wallet_id = m_walletId, wallets_tx.blockchain_tx_id = txid, wallets_tx.amount = amount.toBoostMpf(), wallets_tx.fee = fee.toBoostMpf(), wallets_tx.timestamp = timestamp));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_WALLET_BCH_H_INCLUDED
|
||||
#define CPFM_WALLET_BCH_H_INCLUDED
|
||||
|
||||
#include "wallet.h"
|
||||
|
||||
|
||||
class WalletHandlerBCH : public WalletHandler
|
||||
{
|
||||
public:
|
||||
WalletHandlerBCH(int userId);
|
||||
virtual ~WalletHandlerBCH();
|
||||
|
||||
virtual void analyzeUserWallets();
|
||||
|
||||
private:
|
||||
void addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses);
|
||||
void analyzeUserWalletData(int walletId);
|
||||
};
|
||||
|
||||
class AddressHandlerBCH
|
||||
{
|
||||
public:
|
||||
AddressHandlerBCH(string address, int walletId);
|
||||
virtual ~AddressHandlerBCH();
|
||||
|
||||
void getAddressData();
|
||||
void addAddressDataToDB();
|
||||
void setWalletAddresses(list<string> walletAddresses) { m_walletAddresses = walletAddresses; }
|
||||
void setUserAddresses(list<string> userAddresses) { m_userAddresses = userAddresses; }
|
||||
|
||||
private:
|
||||
void getBlockchainAddressData();
|
||||
void addCachedAddressDataToDB();
|
||||
void addAddressDataJSONToDB(json::value jvalue);
|
||||
string getCacheFilename();
|
||||
|
||||
string m_address;
|
||||
int m_walletId;
|
||||
list<string> m_userAddresses;
|
||||
list<string> m_walletAddresses;
|
||||
};
|
||||
|
||||
#endif // CPFM_WALLET_BTC_H_INCLUDED
|
||||
@@ -0,0 +1,332 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "wallets/btc.h"
|
||||
|
||||
WalletHandlerBTC::WalletHandlerBTC(int userId) : WalletHandler(userId)
|
||||
{
|
||||
}
|
||||
|
||||
WalletHandlerBTC::~WalletHandlerBTC()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WalletHandlerBTC::analyzeUserWallets()
|
||||
{
|
||||
linfo << "Analyzing user " << m_userId << " BTC wallets.";
|
||||
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
const auto wallets_addresses = TableWalletsAddresses{};
|
||||
|
||||
map<int,list<string>> userWallets;
|
||||
list<string> userAddresses;
|
||||
for (const auto& row: db.run(select(wallets.wallet_id,wallets.type_id,wallets_addresses.address).from(wallets.cross_join(wallets_addresses)).where(wallets.user_id == m_userId and wallets.wallet_id == wallets_addresses.wallet_id and wallets.type_id == CPFM_WALLET_TYPE_ID_BTC)))
|
||||
{
|
||||
userWallets[row.wallet_id].push_back(row.address);
|
||||
userAddresses.push_back(row.address);
|
||||
}
|
||||
|
||||
for (const auto& w: userWallets)
|
||||
{
|
||||
addUserWalletDataToDB(w.first,w.second,userAddresses);
|
||||
analyzeUserWalletData(w.first);
|
||||
}
|
||||
}
|
||||
|
||||
void WalletHandlerBTC::analyzeUserWalletData(int walletId)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
|
||||
// Loop is on blockchain tx id, so first, get blockchain tx list.
|
||||
list<string> blockchainTxs;
|
||||
for (const auto& row: db.run(select(wallets_tx.blockchain_tx_id).from(wallets_tx).where(wallets_tx.wallet_id == walletId).group_by(wallets_tx.blockchain_tx_id).order_by(wallets_tx.timestamp.asc())))
|
||||
{
|
||||
blockchainTxs.push_back(row.blockchain_tx_id);
|
||||
}
|
||||
|
||||
ldebug << "------------------------------------------------------------";
|
||||
|
||||
Money walletBalance = 0;
|
||||
Money walletInputs = 0;
|
||||
Money walletOutputs = 0;
|
||||
Money walletOutputFees = 0;
|
||||
for (const auto& txId: blockchainTxs)
|
||||
{
|
||||
Money txAmount = 0;
|
||||
Money txFee = 0;
|
||||
|
||||
for (const auto& row: db.run(select(wallets_tx.amount, wallets_tx.fee).from(wallets_tx).where(wallets_tx.blockchain_tx_id == txId and wallets_tx.wallet_id == walletId)))
|
||||
{
|
||||
Money amount(row.amount);
|
||||
Money fee(row.fee);
|
||||
|
||||
if (txFee == 0)
|
||||
txFee = fee;
|
||||
|
||||
txAmount += amount;
|
||||
}
|
||||
|
||||
txAmount += txFee;
|
||||
|
||||
if (txAmount<0)
|
||||
walletOutputs += txAmount;
|
||||
else
|
||||
walletInputs += txAmount;
|
||||
|
||||
walletOutputFees += txFee;
|
||||
|
||||
walletBalance = walletInputs + walletOutputs - walletOutputFees;
|
||||
|
||||
string sReason = "?";
|
||||
|
||||
if (txAmount < 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
ldebug << "Tx: " << txId
|
||||
<< ". Amount: " << txAmount
|
||||
<< ". Outputs: " << walletOutputs
|
||||
<< ". Inputs: " << walletInputs
|
||||
<< ". Fees: " << txFee
|
||||
<< ". Balance: " << walletBalance
|
||||
<< ". Reason: " << sReason;
|
||||
*/
|
||||
}
|
||||
|
||||
linfo << "Wallet " << walletId << " balance is: " << walletBalance;
|
||||
|
||||
db(insert_into(wallets_balances).set(
|
||||
wallets_balances.wallet_id = walletId,
|
||||
wallets_balances.coin_id = CPFM_COIN_ID_BTC,
|
||||
wallets_balances.balance = walletBalance.toBoostMpf()
|
||||
));
|
||||
}
|
||||
|
||||
void WalletHandlerBTC::addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses)
|
||||
{
|
||||
linfo << "Analyzing wallet " << walletId << ".";
|
||||
|
||||
for (const auto& a: walletAddresses)
|
||||
{
|
||||
AddressHandlerBTC addr(a,walletId);
|
||||
addr.setWalletAddresses(walletAddresses);
|
||||
addr.setUserAddresses(userAddresses);
|
||||
addr.addAddressDataToDB();
|
||||
}
|
||||
}
|
||||
|
||||
AddressHandlerBTC::AddressHandlerBTC(string address, int walletId)
|
||||
{
|
||||
m_address = address;
|
||||
m_walletId = walletId;
|
||||
}
|
||||
|
||||
AddressHandlerBTC::~AddressHandlerBTC()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
string AddressHandlerBTC::getCacheFilename()
|
||||
{
|
||||
string cacheFilename("data/cache/btc/" + m_address);
|
||||
return cacheFilename;
|
||||
}
|
||||
|
||||
void AddressHandlerBTC::getAddressData()
|
||||
{
|
||||
linfo << "Getting data for address: " << m_address << ".";
|
||||
|
||||
if (!bfs::exists(getCacheFilename()))
|
||||
{
|
||||
getBlockchainAddressData();
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerBTC::addAddressDataToDB()
|
||||
{
|
||||
linfo << "Analyzing data for address: " << m_address << ".";
|
||||
|
||||
getAddressData();
|
||||
addCachedAddressDataToDB();
|
||||
}
|
||||
|
||||
void AddressHandlerBTC::getBlockchainAddressData()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/fr/rawaddr/";
|
||||
sRequestURL += m_address;
|
||||
http_client apiclient("https://blockchain.info/");
|
||||
apiclient.request(methods::GET,sRequestURL).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename());
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query blockchain.info about " << m_address;
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerBTC::addCachedAddressDataToDB()
|
||||
{
|
||||
ifstream f;
|
||||
f.open(getCacheFilename());
|
||||
json::value jvalue = json::value::parse(f);
|
||||
f.close();
|
||||
|
||||
addAddressDataJSONToDB(jvalue);
|
||||
}
|
||||
|
||||
void AddressHandlerBTC::addAddressDataJSONToDB(json::value jvalue)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
|
||||
string myAddr = jvalue["address"].as_string();
|
||||
linfo << "Analyzing address: " << myAddr;
|
||||
|
||||
Money balance = jvalue["final_balance"].as_integer();;
|
||||
|
||||
for (int i=0;i<jvalue["txs"].size();i++)
|
||||
{
|
||||
string hash = jvalue["txs"][i]["hash"].as_string();
|
||||
int timestamp = jvalue["txs"][i]["time"].as_integer();
|
||||
|
||||
Money txTotalOutputAmount;
|
||||
Money txTotalInputAmount;
|
||||
Money txMyOutputAmount;
|
||||
Money txMyInputAmount;
|
||||
bool bToMyAddr = false;
|
||||
bool bFromMyAddr = false;
|
||||
list<string> inputAddrList;
|
||||
list<string> outputAddrList;
|
||||
|
||||
for (int j=0;j<jvalue["txs"][i]["inputs"].size();j++)
|
||||
{
|
||||
string inputAddr = jvalue["txs"][i]["inputs"][j]["prev_out"]["addr"].as_string();
|
||||
__int64 amount = jvalue["txs"][i]["inputs"][j]["prev_out"]["value"].as_integer();
|
||||
txTotalInputAmount += amount;
|
||||
|
||||
if (inputAddr == myAddr)
|
||||
{
|
||||
bFromMyAddr = true;
|
||||
txMyInputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
inputAddrList.push_back(inputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j=0;j<jvalue["txs"][i]["out"].size();j++)
|
||||
{
|
||||
string outputAddr = jvalue["txs"][i]["out"][j]["addr"].as_string();
|
||||
__int64 amount = jvalue["txs"][i]["out"][j]["value"].as_integer();
|
||||
txTotalOutputAmount += amount;
|
||||
|
||||
if (outputAddr == myAddr)
|
||||
{
|
||||
bToMyAddr = true;
|
||||
txMyOutputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
outputAddrList.push_back(outputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
Money amount;
|
||||
Money fee;
|
||||
if (bFromMyAddr)
|
||||
{
|
||||
amount = -txMyInputAmount/100000000;
|
||||
fee = txTotalInputAmount/100000000 - txTotalOutputAmount/100000000;
|
||||
|
||||
for (auto const& addr: inputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const& userAddr: m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
lwarn << "User configuration is missing this address: " << addr;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto const& addr: outputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const& userAddr: m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
linfo << "Potential other address to analyze: " << L_Cyan << addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bToMyAddr)
|
||||
{
|
||||
amount = txMyOutputAmount/100000000;
|
||||
fee = 0;
|
||||
}
|
||||
|
||||
db(insert_into(wallets_tx).set(
|
||||
wallets_tx.wallet_id = m_walletId,
|
||||
wallets_tx.blockchain_tx_id = hash,
|
||||
wallets_tx.amount = amount.toBoostMpf(),
|
||||
wallets_tx.fee = fee.toBoostMpf(),
|
||||
wallets_tx.timestamp = timestamp
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_WALLET_BTC_H_INCLUDED
|
||||
#define CPFM_WALLET_BTC_H_INCLUDED
|
||||
|
||||
#include "wallet.h"
|
||||
|
||||
|
||||
class WalletHandlerBTC : public WalletHandler
|
||||
{
|
||||
public:
|
||||
WalletHandlerBTC(int userId);
|
||||
virtual ~WalletHandlerBTC();
|
||||
|
||||
virtual void analyzeUserWallets();
|
||||
|
||||
private:
|
||||
void addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses);
|
||||
void analyzeUserWalletData(int walletId);
|
||||
};
|
||||
|
||||
class AddressHandlerBTC
|
||||
{
|
||||
public:
|
||||
AddressHandlerBTC(string address, int walletId);
|
||||
virtual ~AddressHandlerBTC();
|
||||
|
||||
void getAddressData();
|
||||
void addAddressDataToDB();
|
||||
void setWalletAddresses(list<string> walletAddresses) { m_walletAddresses = walletAddresses; }
|
||||
void setUserAddresses(list<string> userAddresses) { m_userAddresses = userAddresses; }
|
||||
|
||||
private:
|
||||
void getBlockchainAddressData();
|
||||
void addCachedAddressDataToDB();
|
||||
void addAddressDataJSONToDB(json::value jvalue);
|
||||
string getCacheFilename();
|
||||
|
||||
string m_address;
|
||||
int m_walletId;
|
||||
list<string> m_userAddresses;
|
||||
list<string> m_walletAddresses;
|
||||
};
|
||||
|
||||
#endif // CPFM_WALLET_BTC_H_INCLUDED
|
||||
@@ -0,0 +1,353 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "wallets/eth.h"
|
||||
|
||||
WalletHandlerETH::WalletHandlerETH(int userId) : WalletHandler(userId)
|
||||
{
|
||||
}
|
||||
|
||||
WalletHandlerETH::~WalletHandlerETH()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WalletHandlerETH::analyzeUserWallets()
|
||||
{
|
||||
linfo << "Analyzing user " << m_userId << " ETH wallets.";
|
||||
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets = TableWallets{};
|
||||
const auto wallets_addresses = TableWalletsAddresses{};
|
||||
|
||||
map<int,list<string>> userWallets;
|
||||
list<string> userAddresses;
|
||||
for (const auto& row: db.run(select(wallets.wallet_id,wallets.type_id,wallets_addresses.address).from(wallets.cross_join(wallets_addresses)).where(wallets.user_id == m_userId and wallets.wallet_id == wallets_addresses.wallet_id and wallets.type_id == CPFM_WALLET_TYPE_ID_ETH)))
|
||||
{
|
||||
userWallets[row.wallet_id].push_back(row.address);
|
||||
userAddresses.push_back(row.address);
|
||||
}
|
||||
|
||||
for (const auto& w: userWallets)
|
||||
{
|
||||
addUserWalletDataToDB(w.first,w.second,userAddresses);
|
||||
//analyzeUserWalletData(w.first);
|
||||
}
|
||||
}
|
||||
|
||||
void WalletHandlerETH::analyzeUserWalletData(int walletId)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
|
||||
// Loop is on blockchain tx id, so first, get blockchain tx list.
|
||||
list<string> blockchainTxs;
|
||||
for (const auto& row: db.run(select(wallets_tx.blockchain_tx_id).from(wallets_tx).where(wallets_tx.wallet_id == walletId).group_by(wallets_tx.blockchain_tx_id).order_by(wallets_tx.timestamp.asc())))
|
||||
{
|
||||
blockchainTxs.push_back(row.blockchain_tx_id);
|
||||
}
|
||||
|
||||
ldebug << "------------------------------------------------------------";
|
||||
|
||||
Money walletBalance = 0;
|
||||
Money walletInputs = 0;
|
||||
Money walletOutputs = 0;
|
||||
Money walletOutputFees = 0;
|
||||
for (const auto& txId: blockchainTxs)
|
||||
{
|
||||
Money txAmount = 0;
|
||||
Money txFee = 0;
|
||||
|
||||
for (const auto& row: db.run(select(wallets_tx.amount, wallets_tx.fee).from(wallets_tx).where(wallets_tx.blockchain_tx_id == txId and wallets_tx.wallet_id == walletId)))
|
||||
{
|
||||
Money amount(row.amount);
|
||||
Money fee(row.fee);
|
||||
|
||||
if (txFee == 0)
|
||||
txFee = fee;
|
||||
|
||||
txAmount += amount;
|
||||
}
|
||||
|
||||
txAmount += txFee;
|
||||
|
||||
if (txAmount<0)
|
||||
walletOutputs += txAmount;
|
||||
else
|
||||
walletInputs += txAmount;
|
||||
|
||||
walletOutputFees += txFee;
|
||||
|
||||
walletBalance = walletInputs + walletOutputs - walletOutputFees;
|
||||
|
||||
string sReason = "?";
|
||||
|
||||
if (txAmount < 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
ldebug << "Tx: " << txId
|
||||
<< ". Amount: " << txAmount
|
||||
//<< ". Outputs: " << walletOutputs
|
||||
//<< ". Inputs: " << walletInputs
|
||||
<< ". Fees: " << txFee
|
||||
<< ". Balance: " << walletBalance
|
||||
<< ". Reason: " << sReason;
|
||||
*/
|
||||
}
|
||||
|
||||
linfo << "Wallet " << walletId << " balance is: " << walletBalance;
|
||||
|
||||
db(insert_into(wallets_balances).set(
|
||||
wallets_balances.wallet_id = walletId,
|
||||
wallets_balances.coin_id = CPFM_COIN_ID_ETH,
|
||||
wallets_balances.balance = walletBalance.toBoostMpf()
|
||||
));
|
||||
}
|
||||
|
||||
void WalletHandlerETH::addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses)
|
||||
{
|
||||
linfo << "Analyzing wallet " << walletId << ".";
|
||||
|
||||
for (const auto& a: walletAddresses)
|
||||
{
|
||||
AddressHandlerETH addr(a,walletId);
|
||||
addr.setWalletAddresses(walletAddresses);
|
||||
addr.setUserAddresses(userAddresses);
|
||||
addr.addAddressDataToDB();
|
||||
}
|
||||
}
|
||||
|
||||
AddressHandlerETH::AddressHandlerETH(string address, int walletId)
|
||||
{
|
||||
m_address = address;
|
||||
m_walletId = walletId;
|
||||
}
|
||||
|
||||
AddressHandlerETH::~AddressHandlerETH()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
string AddressHandlerETH::getCacheFilename()
|
||||
{
|
||||
string cacheFilename("data/cache/eth/" + m_address);
|
||||
return cacheFilename;
|
||||
}
|
||||
|
||||
void AddressHandlerETH::getAddressData()
|
||||
{
|
||||
linfo << "Getting data for address: " << m_address << ".";
|
||||
|
||||
if (!bfs::exists(getCacheFilename()))
|
||||
{
|
||||
getBlockchainAddressData();
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerETH::addAddressDataToDB()
|
||||
{
|
||||
linfo << "Analyzing data for address: " << m_address << ".";
|
||||
|
||||
getAddressData();
|
||||
addCachedAddressDataToDB();
|
||||
}
|
||||
|
||||
void AddressHandlerETH::getBlockchainAddressData()
|
||||
{
|
||||
try
|
||||
{
|
||||
string sRequestURL = "/api?module=account&action=balance&address=";
|
||||
sRequestURL += m_address;
|
||||
http_client apiclient("https://api.etherscan.io");
|
||||
apiclient.request(methods::GET,sRequestURL).then([](http_response response)
|
||||
{
|
||||
if (response.status_code() == status_codes::OK)
|
||||
{
|
||||
return response.extract_json();
|
||||
}
|
||||
return pplx::task_from_result(json::value());
|
||||
})
|
||||
.then([this](pplx::task<json::value> previousTask)
|
||||
{
|
||||
ofstream f;
|
||||
f.open(getCacheFilename());
|
||||
f << previousTask.get();
|
||||
f.close();
|
||||
})
|
||||
.wait();
|
||||
}
|
||||
catch(const http::http_exception& e)
|
||||
{
|
||||
lerr << "Failed to query cashexplorer.bitcoin.com about " << m_address;
|
||||
}
|
||||
}
|
||||
|
||||
void AddressHandlerETH::addCachedAddressDataToDB()
|
||||
{
|
||||
ifstream f;
|
||||
f.open(getCacheFilename());
|
||||
json::value jvalue = json::value::parse(f);
|
||||
f.close();
|
||||
|
||||
addAddressDataJSONToDB(jvalue);
|
||||
}
|
||||
|
||||
void AddressHandlerETH::addAddressDataJSONToDB(json::value jvalue)
|
||||
{
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
|
||||
string myAddr = m_address;
|
||||
linfo << "Analyzing address: " << myAddr;
|
||||
|
||||
if (!jvalue["message"].as_string().compare("OK") && !jvalue["status"].as_string().compare("1"))
|
||||
{
|
||||
Money walletBalance (jvalue["result"]);
|
||||
Money divider("1000000000000000000");
|
||||
walletBalance /= divider;
|
||||
|
||||
linfo << "Wallet " << m_walletId << " balance is: " << walletBalance;
|
||||
|
||||
mysql::connection db(getMysqlConfig());
|
||||
const auto wallets_tx = TableWalletsTx{};
|
||||
const auto wallets_balances = TableWalletsBalances{};
|
||||
|
||||
db(insert_into(wallets_balances).set(
|
||||
wallets_balances.wallet_id = m_walletId,
|
||||
wallets_balances.coin_id = CPFM_COIN_ID_ETH,
|
||||
wallets_balances.balance = walletBalance.toBoostMpf()
|
||||
));
|
||||
}
|
||||
|
||||
/*
|
||||
//Money balance = jvalue["final_balance"].as_integer();;
|
||||
|
||||
for (int i=0;i<jvalue["txs"].size();i++)
|
||||
{
|
||||
string txid = jvalue["txs"][i]["txid"].as_string();
|
||||
int timestamp = jvalue["txs"][i]["time"].as_integer();
|
||||
|
||||
Money txTotalOutputAmount;
|
||||
Money txTotalInputAmount;
|
||||
Money txMyOutputAmount;
|
||||
Money txMyInputAmount;
|
||||
bool bToMyAddr = false;
|
||||
bool bFromMyAddr = false;
|
||||
list<string> inputAddrList;
|
||||
list<string> outputAddrList;
|
||||
|
||||
for (int j=0;j<jvalue["txs"][i]["vin"].size();j++)
|
||||
{
|
||||
string inputAddr = jvalue["txs"][i]["vin"][j]["addr"].as_string();
|
||||
Money amount (jvalue["txs"][i]["vin"][j]["value"]);
|
||||
txTotalInputAmount += amount;
|
||||
|
||||
if (inputAddr == myAddr)
|
||||
{
|
||||
bFromMyAddr = true;
|
||||
txMyInputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
inputAddrList.push_back(inputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j=0;j<jvalue["txs"][i]["vout"].size();j++)
|
||||
{
|
||||
string outputAddr = jvalue["txs"][i]["vout"][j]["scriptPubKey"]["addresses"][0].as_string();
|
||||
Money amount(jvalue["txs"][i]["vout"][j]["value"]);
|
||||
txTotalOutputAmount += amount;
|
||||
|
||||
if (outputAddr == myAddr)
|
||||
{
|
||||
bToMyAddr = true;
|
||||
txMyOutputAmount += amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
outputAddrList.push_back(outputAddr);
|
||||
}
|
||||
}
|
||||
|
||||
Money amount;
|
||||
Money fee;
|
||||
if (bFromMyAddr)
|
||||
{
|
||||
amount = -txMyInputAmount;
|
||||
fee = txTotalInputAmount - txTotalOutputAmount;
|
||||
|
||||
for (auto const& addr: inputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const& userAddr: m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
lwarn << "User configuration is missing this address: " << addr;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto const& addr: outputAddrList)
|
||||
{
|
||||
bool bFoundInUserAddresses = false;
|
||||
for (auto const& userAddr: m_userAddresses)
|
||||
{
|
||||
if (!userAddr.compare(addr))
|
||||
{
|
||||
bFoundInUserAddresses = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFoundInUserAddresses)
|
||||
{
|
||||
linfo << "Potential other address to analyze: " << L_Cyan << addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bToMyAddr)
|
||||
{
|
||||
amount = txMyOutputAmount;
|
||||
fee = 0;
|
||||
}
|
||||
|
||||
db(insert_into(wallets_tx).set(
|
||||
wallets_tx.wallet_id = m_walletId,
|
||||
wallets_tx.tx_hash = txid,
|
||||
wallets_tx.amount = amount.toBoostMpf(),
|
||||
wallets_tx.fee = fee.toBoostMpf(),
|
||||
wallets_tx.timestamp = timestamp
|
||||
));
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018, evilny0
|
||||
*
|
||||
* This file is part of cpfm.
|
||||
*
|
||||
* cpfm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* cpm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with cpfm. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CPFM_WALLET_ETH_H_INCLUDED
|
||||
#define CPFM_WALLET_ETH_H_INCLUDED
|
||||
|
||||
#include "wallet.h"
|
||||
|
||||
|
||||
class WalletHandlerETH : public WalletHandler
|
||||
{
|
||||
public:
|
||||
WalletHandlerETH(int userId);
|
||||
virtual ~WalletHandlerETH();
|
||||
|
||||
virtual void analyzeUserWallets();
|
||||
|
||||
private:
|
||||
void addUserWalletDataToDB(int walletId, list<string> walletAddresses, list<string> userAddresses);
|
||||
void analyzeUserWalletData(int walletId);
|
||||
};
|
||||
|
||||
class AddressHandlerETH
|
||||
{
|
||||
public:
|
||||
AddressHandlerETH(string address, int walletId);
|
||||
virtual ~AddressHandlerETH();
|
||||
|
||||
void getAddressData();
|
||||
void addAddressDataToDB();
|
||||
void setWalletAddresses(list<string> walletAddresses) { m_walletAddresses = walletAddresses; }
|
||||
void setUserAddresses(list<string> userAddresses) { m_userAddresses = userAddresses; }
|
||||
|
||||
private:
|
||||
void getBlockchainAddressData();
|
||||
void addCachedAddressDataToDB();
|
||||
void addAddressDataJSONToDB(json::value jvalue);
|
||||
string getCacheFilename();
|
||||
|
||||
string m_address;
|
||||
int m_walletId;
|
||||
list<string> m_userAddresses;
|
||||
list<string> m_walletAddresses;
|
||||
};
|
||||
|
||||
#endif // CPFM_WALLET_ETH_H_INCLUDED
|
||||
Reference in New Issue
Block a user