From 63fd84f7f14e7b77f57364d5f5463b5e6455cac5 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 15 Nov 2020 16:15:11 +0100 Subject: [PATCH 1/8] policy: add CFeeRate::SatsToString helper without units --- src/policy/feerate.cpp | 4 ++++ src/policy/feerate.h | 3 +++ src/test/amount_tests.cpp | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/src/policy/feerate.cpp b/src/policy/feerate.cpp index eb0cba5c67..34795a84fc 100644 --- a/src/policy/feerate.cpp +++ b/src/policy/feerate.cpp @@ -43,3 +43,7 @@ std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const default: return strprintf("%d.%08d %s/kvB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT); } } + +std::string CFeeRate::SatsToString() const { + return strprintf("%d.%03d", nSatoshisPerK / 1000, nSatoshisPerK % 1000); +} diff --git a/src/policy/feerate.h b/src/policy/feerate.h index d742a43acc..8264a869af 100644 --- a/src/policy/feerate.h +++ b/src/policy/feerate.h @@ -68,7 +68,10 @@ public: friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; } CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; } + /** Return the fee rate in sat/vB or BTC/kvB, with units, as a string. */ std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const; + /** Return the fee rate in sat/vB, without units, as a string. */ + std::string SatsToString() const; friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.nSatoshisPerK); } friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.nSatoshisPerK); } diff --git a/src/test/amount_tests.cpp b/src/test/amount_tests.cpp index e5ab1cfb90..21d8b708ae 100644 --- a/src/test/amount_tests.cpp +++ b/src/test/amount_tests.cpp @@ -139,4 +139,12 @@ BOOST_AUTO_TEST_CASE(ToStringTest) BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB"); } +BOOST_AUTO_TEST_CASE(SatsToStringTest) +{ + BOOST_CHECK_EQUAL(CFeeRate(1).SatsToString(), "0.001"); + BOOST_CHECK_EQUAL(CFeeRate(70).SatsToString(), "0.070"); + BOOST_CHECK_EQUAL(CFeeRate(3141).SatsToString(), "3.141"); + BOOST_CHECK_EQUAL(CFeeRate(10002).SatsToString(), "10.002"); +} + BOOST_AUTO_TEST_SUITE_END() From d302fef9a37869fa2ddd3fce13a02ea461814516 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 25 Nov 2020 01:09:17 +0000 Subject: [PATCH 2/8] core_io: Add ValueFromFeeRate helper --- src/core_io.h | 2 ++ src/core_write.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/core_io.h b/src/core_io.h index 9305bb7239..ea252705bc 100644 --- a/src/core_io.h +++ b/src/core_io.h @@ -13,6 +13,7 @@ class CBlock; class CBlockHeader; +class CFeeRate; class CScript; class CTransaction; struct CMutableTransaction; @@ -41,6 +42,7 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header); // core_write.cpp UniValue ValueFromAmount(const CAmount amount); +UniValue ValueFromFeeRate(const CFeeRate& fee_rate); std::string FormatScript(const CScript& script); std::string EncodeHexTx(const CTransaction& tx); std::string SighashToStr(unsigned char sighash_type); diff --git a/src/core_write.cpp b/src/core_write.cpp index 253dfde100..61357f47ce 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include