Add external estimate fee query
This commit is contained in:
parent
3fa3ff200b
commit
5318637519
3 changed files with 127 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ add_library( peerplays_sidechain
|
||||||
bitcoin/utils.cpp
|
bitcoin/utils.cpp
|
||||||
bitcoin/sign_bitcoin_transaction.cpp
|
bitcoin/sign_bitcoin_transaction.cpp
|
||||||
bitcoin/libbitcoin_client.cpp
|
bitcoin/libbitcoin_client.cpp
|
||||||
|
bitcoin/estimate_fee_external.cpp
|
||||||
common/rpc_client.cpp
|
common/rpc_client.cpp
|
||||||
common/utils.cpp
|
common/utils.cpp
|
||||||
hive/asset.cpp
|
hive/asset.cpp
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
|
||||||
|
#include <graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp>
|
||||||
|
|
||||||
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
|
#include <boost/property_tree/ptree.hpp>
|
||||||
|
|
||||||
|
#include <fc/log/logger.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
typedef std::function<uint64_t()> 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<std::string, get_fee_func_type> 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
|
||||||
Loading…
Reference in a new issue