From fade94d11a5b93113975c4b2f62a357a70d03191 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Wed, 14 Feb 2024 17:13:34 +0100 Subject: [PATCH] rpc: Add ParseFeeRate helper --- src/rpc/util.cpp | 7 +++++++ src/rpc/util.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 5e2e9e5f64c..a64ec133cf0 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -75,6 +75,13 @@ CAmount AmountFromValue(const UniValue& value, int decimals) return amount; } +CFeeRate ParseFeeRate(const UniValue& json) +{ + CAmount val{AmountFromValue(json)}; + if (val >= COIN) throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee rates larger than or equal to 1BTC/kvB are not accepted"); + return CFeeRate{val}; +} + uint256 ParseHashV(const UniValue& v, std::string_view name) { const std::string& strHex(v.get_str()); diff --git a/src/rpc/util.h b/src/rpc/util.h index e2d5ed333cc..ad3ed97b2e9 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -103,6 +103,11 @@ std::vector ParseHexO(const UniValue& o, std::string_view strKey) * @returns a CAmount if the various checks pass. */ CAmount AmountFromValue(const UniValue& value, int decimals = 8); +/** + * Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB). + * Reject negative values or rates larger than 1BTC/kvB. + */ +CFeeRate ParseFeeRate(const UniValue& json); using RPCArgList = std::vector>; std::string HelpExampleCli(const std::string& methodname, const std::string& args);