From f826e90ac3fb05b6f43f132542376acda88c393d 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 41f4a4d06b..586d5df56f 100644 --- a/src/policy/feerate.h +++ b/src/policy/feerate.h @@ -70,7 +70,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; SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); } }; diff --git a/src/test/amount_tests.cpp b/src/test/amount_tests.cpp index 3815a5bba6..31832e9893 100644 --- a/src/test/amount_tests.cpp +++ b/src/test/amount_tests.cpp @@ -113,4 +113,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 ac3219aca3a10dd01db8bba17015f7c6a5c2c1f4 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 1f5ecbaea6..c8349c7594 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; @@ -50,6 +51,7 @@ bool ParseHashStr(const std::string& strHex, uint256& result); // 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, const int serializeFlags = 0); std::string SighashToStr(unsigned char sighash_type); diff --git a/src/core_write.cpp b/src/core_write.cpp index 7cf019a42e..4f5a6703a1 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include