cli: add AmountFromValue() and ValueFromAmount()

Github-Pull: #19092
Rebased-From: bbf0afade89d948f7ce377b662b0f9b4050db0bc
This commit is contained in:
Jon Atack 2020-05-28 17:48:34 +02:00 committed by Luke Dashjr
parent 833a20ecac
commit c47e8246cf

View File

@ -12,6 +12,7 @@
#include <common/url.h> #include <common/url.h>
#include <compat/compat.h> #include <compat/compat.h>
#include <compat/stdin.h> #include <compat/stdin.h>
#include <consensus/amount.h>
#include <policy/feerate.h> #include <policy/feerate.h>
#include <rpc/client.h> #include <rpc/client.h>
#include <rpc/mining.h> #include <rpc/mining.h>
@ -906,6 +907,25 @@ static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
nRet = abs(error["code"].getInt<int>()); nRet = abs(error["code"].getInt<int>());
} }
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 * 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`. * fetches mine.trusted balances for each loaded wallet and pushes them to `result`.