From 5318637519388a9c7b6731d0ad0bcd01021bb954 Mon Sep 17 00:00:00 2001 From: hirunda Date: Fri, 7 Oct 2022 01:41:51 +0200 Subject: [PATCH] Add external estimate fee query --- .../peerplays_sidechain/CMakeLists.txt | 1 + .../bitcoin/estimate_fee_external.cpp | 90 +++++++++++++++++++ .../bitcoin/estimate_fee_external.hpp | 36 ++++++++ 3 files changed, 127 insertions(+) create mode 100644 libraries/plugins/peerplays_sidechain/bitcoin/estimate_fee_external.cpp create mode 100644 libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index 91df0828..7585a518 100644 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -16,6 +16,7 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp bitcoin/libbitcoin_client.cpp + bitcoin/estimate_fee_external.cpp common/rpc_client.cpp common/utils.cpp hive/asset.cpp diff --git a/libraries/plugins/peerplays_sidechain/bitcoin/estimate_fee_external.cpp b/libraries/plugins/peerplays_sidechain/bitcoin/estimate_fee_external.cpp new file mode 100644 index 00000000..146cf21e --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/bitcoin/estimate_fee_external.cpp @@ -0,0 +1,90 @@ + +#include + +#include +#include + +#include + +#include + +namespace graphene { +namespace peerplays_sidechain { + +static size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data) { + data->append((char *)ptr, size * nmemb); + return size * nmemb; +} + +estimate_fee_external::estimate_fee_external() { + curl = curl_easy_init(); +} + +estimate_fee_external::~estimate_fee_external() { + curl_easy_cleanup(curl); +} + +uint64_t estimate_fee_external::get_fee_external(uint16_t target_block) { + + this->target_block = target_block; + + uint64_t average_fee = 0; + + for (auto &url_fee_parser : url_get_fee_parsers) { + response = get_response(url_fee_parser.first); + uint64_t fee = url_fee_parser.second(); + average_fee = fee; + } + + return 0; +} + +std::string estimate_fee_external::get_response(std::string url) { + + std::string response; + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass"); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0"); + curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L); + curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + curl_easy_perform(curl); + } + return response; +} + +uint64_t estimate_fee_external::parse_and_get_fee_1() { + //"https://www.bitgo.com/api/v2/btc/tx/fee" + + std::stringstream response_ss(response); + boost::property_tree::ptree response_pt; + boost::property_tree::read_json(response_ss, response_pt); + + uint64_t founded_fee = 0; + + for (const auto &tx_child : response_pt.get_child("feeByBlockTarget")) { + const auto &block_num = tx_child.first.data(); + const auto &fee = tx_child.second.data(); + + founded_fee = std::stoi(fee); + + if (std::stoi(block_num) > target_block) { + return founded_fee; + } + + std::cout << "Block num: " << block_num << " Fee: " << fee << std::endl; + } +} + +uint64_t estimate_fee_external::parse_and_get_fee_2() { + // TODO +} + +uint64_t estimate_fee_external::parse_and_get_fee_3() { + // TODO +} + +}} // namespace graphene::peerplays_sidechain \ No newline at end of file diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp new file mode 100644 index 00000000..393a2443 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include +#include + +typedef std::function get_fee_func_type; + +namespace graphene { namespace peerplays_sidechain { + +class estimate_fee_external { +public: + estimate_fee_external(); + ~estimate_fee_external(); + uint64_t get_fee_external(uint16_t target_block); + +private: + std::string get_response(std::string url); + // Here add your custom parser for external url. Take care of incremental name + // and populate the list of url_parsers bellow paired with the function + uint64_t parse_and_get_fee_1(); + uint64_t parse_and_get_fee_2(); + uint64_t parse_and_get_fee_3(); + + const std::map url_get_fee_parsers{ + {"https://www.bitgo.com/api/v2/btc/tx/fee", std::bind(&estimate_fee_external::parse_and_get_fee_1, this)}, + {"https://bitcoiner.live/api/fees/estimates/latest", std::bind(&estimate_fee_external::parse_and_get_fee_2, this)}, + {"https://api.blockchain.info/mempool/fees", std::bind(&estimate_fee_external::parse_and_get_fee_3, this)}}; + + std::string response; + uint16_t target_block; + CURL *curl{nullptr}; +}; + +}} // namespace graphene::peerplays_sidechain \ No newline at end of file