From c47e8246cf60a5de33fd5abc4449385c56a0de33 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 28 May 2020 17:48:34 +0200 Subject: [PATCH] cli: add AmountFromValue() and ValueFromAmount() Github-Pull: #19092 Rebased-From: bbf0afade89d948f7ce377b662b0f9b4050db0bc --- src/bitcoin-cli.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index d6ff642ae1..c291eb231f 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -906,6 +907,25 @@ static void ParseError(const UniValue& error, std::string& strPrint, int& nRet) nRet = abs(error["code"].getInt()); } +static CAmount AmountFromValue(const UniValue& value) +{ + CAmount amount{0}; + if (!ParseFixedPoint(value.getValStr(), 8, &amount)) + throw std::runtime_error("Invalid amount"); + if (!MoneyRange(amount)) + throw std::runtime_error("Amount out of range"); + return amount; +} + +static UniValue ValueFromAmount(const CAmount& amount) +{ + bool sign{amount < 0}; + int64_t n_abs{sign ? -amount : amount}; + int64_t quotient{n_abs / COIN}; + int64_t remainder{n_abs % COIN}; + return UniValue(UniValue::VNUM, strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder)); +} + /** * GetWalletBalances calls listwallets; if more than one wallet is loaded, it then * fetches mine.trusted balances for each loaded wallet and pushes them to `result`.