Skip to content

Commit e061e27

Browse files
committed
rpc: Make ValueFromAmount always return 8 decimals
This is the format that was always returned to JSON clients. The difference was not noticed before, because VREAL values are post-processed by univalue. By implementing the functionality directly it breaks the dependency of rpcserver on utilmoneystr. FormatMoney is now only used for debugging purposes. To test, port over the formatting tests from util_tests.cpp to rpc_tests.cpp.
1 parent dcc495e commit e061e27

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/rpcserver.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "sync.h"
1212
#include "ui_interface.h"
1313
#include "util.h"
14-
#include "utilmoneystr.h"
1514
#include "utilstrencodings.h"
1615
#ifdef ENABLE_WALLET
1716
#include "wallet/wallet.h"
@@ -133,7 +132,12 @@ CAmount AmountFromValue(const UniValue& value)
133132

134133
UniValue ValueFromAmount(const CAmount& amount)
135134
{
136-
return UniValue(UniValue::VREAL, FormatMoney(amount));
135+
bool sign = amount < 0;
136+
int64_t n_abs = (sign ? -amount : amount);
137+
int64_t quotient = n_abs / COIN;
138+
int64_t remainder = n_abs % COIN;
139+
return UniValue(UniValue::VNUM,
140+
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
137141
}
138142

139143
uint256 ParseHashV(const UniValue& v, string strName)

src/test/rpc_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
120120
BOOST_CHECK(ValueFromAmount(100000000LL).write() == "1.00000000");
121121
BOOST_CHECK(ValueFromAmount(2099999999999990LL).write() == "20999999.99999990");
122122
BOOST_CHECK(ValueFromAmount(2099999999999999LL).write() == "20999999.99999999");
123+
124+
BOOST_CHECK_EQUAL(ValueFromAmount(0).write(), "0.00000000");
125+
BOOST_CHECK_EQUAL(ValueFromAmount((COIN/10000)*123456789).write(), "12345.67890000");
126+
BOOST_CHECK_EQUAL(ValueFromAmount(-COIN).write(), "-1.00000000");
127+
BOOST_CHECK_EQUAL(ValueFromAmount(-COIN/10).write(), "-0.10000000");
128+
129+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000000).write(), "100000000.00000000");
130+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000000).write(), "10000000.00000000");
131+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000000).write(), "1000000.00000000");
132+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000).write(), "100000.00000000");
133+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000).write(), "10000.00000000");
134+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000).write(), "1000.00000000");
135+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100).write(), "100.00000000");
136+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10).write(), "10.00000000");
137+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN).write(), "1.00000000");
138+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10).write(), "0.10000000");
139+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100).write(), "0.01000000");
140+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000).write(), "0.00100000");
141+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000).write(), "0.00010000");
142+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000).write(), "0.00001000");
143+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
144+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
145+
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
123146
}
124147

125148
static UniValue ValueFromString(const std::string &str)

0 commit comments

Comments
 (0)