From 0742c7840f03505597fd2de87db97f12597ef667 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 27 Apr 2021 10:54:44 +0200 Subject: [PATCH] rpc: enable passing decimals to AmountFromValue, add doxygen --- src/rpc/util.cpp | 4 ++-- src/rpc/util.h | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 069669bb3b..7cf25e0c82 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -74,12 +74,12 @@ void RPCTypeCheckObj(const UniValue& o, } } -CAmount AmountFromValue(const UniValue& value) +CAmount AmountFromValue(const UniValue& value, int decimals) { if (!value.isNum() && !value.isStr()) throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string"); CAmount amount; - if (!ParseFixedPoint(value.getValStr(), 8, &amount)) + if (!ParseFixedPoint(value.getValStr(), decimals, &amount)) throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); if (!MoneyRange(amount)) throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range"); diff --git a/src/rpc/util.h b/src/rpc/util.h index 8ec18b2f35..41cbcc6032 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -77,7 +77,14 @@ extern uint256 ParseHashO(const UniValue& o, std::string strKey); extern std::vector ParseHexV(const UniValue& v, std::string strName); extern std::vector ParseHexO(const UniValue& o, std::string strKey); -extern CAmount AmountFromValue(const UniValue& value); +/** + * Validate and return a CAmount from a UniValue number or string. + * + * @param[in] value UniValue number or string to parse. + * @param[in] decimals Number of significant digits (default: 8). + * @returns a CAmount if the various checks pass. + */ +extern CAmount AmountFromValue(const UniValue& value, int decimals = 8); using RPCArgList = std::vector>; extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);