Skip to content

Commit bbf0afa

Browse files
committed
cli: add AmountFromValue() and ValueFromAmount()
1 parent 4b5c919 commit bbf0afa

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <config/bitcoin-config.h>
88
#endif
99

10+
#include <amount.h>
1011
#include <chainparamsbase.h>
1112
#include <clientversion.h>
1213
#include <optional.h>
@@ -508,6 +509,25 @@ static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
508509
nRet = abs(error["code"].get_int());
509510
}
510511

512+
static CAmount AmountFromValue(const UniValue& value)
513+
{
514+
CAmount amount{0};
515+
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
516+
throw std::runtime_error("Invalid amount");
517+
if (!MoneyRange(amount))
518+
throw std::runtime_error("Amount out of range");
519+
return amount;
520+
}
521+
522+
static UniValue ValueFromAmount(const CAmount& amount)
523+
{
524+
bool sign{amount < 0};
525+
int64_t n_abs{sign ? -amount : amount};
526+
int64_t quotient{n_abs / COIN};
527+
int64_t remainder{n_abs % COIN};
528+
return UniValue(UniValue::VNUM, strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
529+
}
530+
511531
/**
512532
* GetWalletBalances calls listwallets; if more than one wallet is loaded, it then
513533
* fetches mine.trusted balances for each loaded wallet and pushes them to `result`.

0 commit comments

Comments
 (0)