nit in the first commit: I think it would be good to add a short comment about the
- precision (sat per kvB), and
- that this is rounding down,
because callers would presumably care about the rounding behavior, and maybe even want to modify it?
Maybe: /// Stringify with an accuracy of sat/kvB. (Rounds down)
Also, the unit tests from the second commit would be better to squash into this commit, otherwise, it is harder to review.
Also, the impl can be shorter (instead of 20 lines, it can be ~10 lines):
{
const auto format_feerate = [sat_per_kvb = GetFeePerK()]<CAmount divisor, int decimals>() {
const char* sign{sat_per_kvb < 0 ? "-" : ""};
const CAmount quotient{std::abs(sat_per_kvb / divisor)};
const CAmount remainder{std::abs(sat_per_kvb % divisor)};
return UniValue{VNum, strprintf("%s%d.%0*d", sign, quotient, decimals, remainder)};
};
switch (fee_rate_format) {
case FeeRateFormat::BTC_KVB: return format_feerate.operator()</*divisor=*/COIN, /*decimals=*/8>();
case FeeRateFormat::SAT_VB: return format_feerate.operator()</*divisor=*/1000, /*decimals=*/3>();
} // no default case, so the compiler can warn about missing cases
assert(false);
}
(Feel free to use normal arg passing instead of the lambda template arg passing)