From 09c51e7f7dff9cb90436408640ff8e342e4495aa Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 15 Nov 2020 16:15:11 +0100 Subject: [PATCH] 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 2e50172914..f11ac0225e 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; 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()