Add external estimate fee query

This commit is contained in:
hirunda 2022-10-09 22:45:34 +02:00
parent 5318637519
commit 983859c997
3 changed files with 82 additions and 10 deletions

View file

@ -28,12 +28,12 @@ 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;
if (fee != 0) {
return fee;
}
}
return 0;
@ -59,12 +59,16 @@ std::string estimate_fee_external::get_response(std::string url) {
uint64_t estimate_fee_external::parse_and_get_fee_1() {
//"https://www.bitgo.com/api/v2/btc/tx/fee"
uint64_t founded_fee = 0;
if (response.empty()) {
return founded_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();
@ -74,17 +78,78 @@ uint64_t estimate_fee_external::parse_and_get_fee_1() {
if (std::stoi(block_num) > target_block) {
return founded_fee;
}
std::cout << "Block num: " << block_num << " Fee: " << fee << std::endl;
}
return founded_fee;
}
uint64_t estimate_fee_external::parse_and_get_fee_2() {
// TODO
// https://bitcoiner.live/api/fees/estimates/latest
uint64_t founded_fee = 0;
if (response.empty()) {
return founded_fee;
}
std::stringstream response_ss(response);
boost::property_tree::ptree response_pt;
boost::property_tree::read_json(response_ss, response_pt);
for (const auto &tx_child : response_pt.get_child("estimates")) {
const auto &time_str = tx_child.first.data();
auto time = std::stoi(time_str);
auto block_num = time / 10;
if (tx_child.second.count("sat_per_vbyte")) {
auto founded_fee_str = tx_child.second.get_child("sat_per_vbyte").data();
founded_fee = std::stoi(founded_fee_str) * 1000;
}
if (block_num > target_block) {
return founded_fee;
}
}
return founded_fee;
}
uint64_t estimate_fee_external::parse_and_get_fee_3() {
// TODO
// https://api.blockchain.info/mempool/fees
if (response.empty()) {
return 0;
}
std::stringstream response_ss(response);
boost::property_tree::ptree response_pt;
boost::property_tree::read_json(response_ss, response_pt);
if (response_pt.get_child("limits").count("min") && response_pt.get_child("limits").count("max")) {
auto limits_min_str = response_pt.get_child("limits.min").data();
auto limits_max_str = response_pt.get_child("limits.max").data();
auto limits_min = std::stoi(limits_min_str);
auto limits_max = std::stoi(limits_max_str);
auto priority_max = (limits_max - (limits_min - 1)) / 2;
if (response_pt.count("regular") && response_pt.count("priority")) {
auto regular_str = response_pt.get_child("regular").data();
auto priority_str = response_pt.get_child("priority").data();
auto regular = std::stoi(regular_str);
auto priority = std::stoi(priority_str);
if (target_block > priority_max) {
return regular * 1000;
} else {
return priority * 1000;
}
}
}
return 0;
}
}} // namespace graphene::peerplays_sidechain

View file

@ -14,6 +14,7 @@
#include <fc/network/http/connection.hpp>
#include <graphene/peerplays_sidechain/bitcoin/bitcoin_address.hpp>
#include <graphene/peerplays_sidechain/bitcoin/estimate_fee_external.hpp>
namespace graphene { namespace peerplays_sidechain {
@ -114,6 +115,7 @@ public:
private:
bool is_test_net = false;
std::unique_ptr<estimate_fee_external> estimate_fee_ext;
};
// =============================================================================

View file

@ -337,10 +337,15 @@ bool bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_
}
bitcoin_libbitcoin_client::bitcoin_libbitcoin_client(std::string url) :
libbitcoin_client(url) {
estimate_fee_ext = std::unique_ptr<estimate_fee_external>();
}
uint64_t bitcoin_libbitcoin_client::estimatesmartfee(uint16_t conf_target) {
// TODO for now it's hard-coded value until final implementation
uint64_t fee = estimate_fee_ext->get_fee_external(conf_target);
if (fee != 0) {
return fee;
}
return 20000;
}