Skip to content

Commit 362eb08

Browse files
committed
Upstream?: Tests(unit) for AmountFromValue/ValueFromAmount with 0 decimals
1 parent f71994c commit 362eb08

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/rpc/server.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,27 @@ void RPCTypeCheckObj(const UniValue& o,
117117
}
118118
}
119119

120-
CAmount AmountFromValue(const UniValue& value)
120+
CAmount AmountFromValue(const UniValue& value, const bool f8Decimals)
121121
{
122122
if (!value.isNum() && !value.isStr())
123123
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
124124
CAmount amount;
125-
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
125+
// TODO Decimals: Just don't call ParseFixedPoint if !f8Decimals
126+
int decimals = f8Decimals ? 8 : 0;
127+
if (!ParseFixedPoint(value.getValStr(), decimals, &amount))
126128
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
127129
if (!MoneyRange(amount))
128130
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
129131
return amount;
130132
}
131133

132-
UniValue ValueFromAmount(const CAmount& amount)
134+
UniValue ValueFromAmount(const CAmount& amount, const bool f8Decimals)
133135
{
134136
bool sign = amount < 0;
137+
if (!f8Decimals) {
138+
assert(!sign); // FIX throw exception ?
139+
return UniValue(UniValue::VNUM, strprintf("%d", amount));
140+
}
135141
int64_t n_abs = (sign ? -amount : amount);
136142
int64_t quotient = n_abs / COIN;
137143
int64_t remainder = n_abs % COIN;

src/rpc/server.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strNa
181181
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
182182

183183
extern int64_t nWalletUnlockTime;
184-
extern CAmount AmountFromValue(const UniValue& value);
185-
extern UniValue ValueFromAmount(const CAmount& amount);
184+
CAmount AmountFromValue(const UniValue& value, const bool f8Decimals=true);
185+
UniValue ValueFromAmount(const CAmount& amount, const bool f8Decimals=true);
186186
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
187187
extern std::string HelpRequiringPassphrase();
188188
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);

src/test/rpc_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
132132

133133
BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
134134
{
135+
BOOST_CHECK(ValueFromAmount(0LL, false).write() == "0");
136+
BOOST_CHECK(ValueFromAmount(1LL, false).write() == "1");
137+
BOOST_CHECK(ValueFromAmount(17622195LL, false).write() == "17622195");
138+
BOOST_CHECK(ValueFromAmount(50000000LL, false).write() == "50000000");
139+
BOOST_CHECK(ValueFromAmount(89898989LL, false).write() == "89898989");
140+
BOOST_CHECK(ValueFromAmount(100000000LL, false).write() == "100000000"); // 1 CURRENCY_UNIT
141+
BOOST_CHECK(ValueFromAmount(100000000000000LL, false).write() == "100000000000000"); // 1 M CURRENCY_UNIT
142+
BOOST_CHECK(ValueFromAmount(2100000000000000LL, false).write() == "2100000000000000"); // 21 M CURRENCY_UNIT
143+
BOOST_CHECK(ValueFromAmount(10000000000000000LL, false).write() == "10000000000000000"); // 100 M CURRENCY_UNIT (100 000 000 0000 0000 MINIMAL_UNIT)
144+
BOOST_CHECK(ValueFromAmount(2099999999999990LL, false).write() == "2099999999999990");
145+
BOOST_CHECK(ValueFromAmount(2099999999999999LL, false).write() == "2099999999999999");
146+
135147
BOOST_CHECK(ValueFromAmount(0LL).write() == "0.00000000");
136148
BOOST_CHECK(ValueFromAmount(1LL).write() == "0.00000001");
137149
BOOST_CHECK(ValueFromAmount(17622195LL).write() == "0.17622195");
@@ -174,6 +186,19 @@ static UniValue ValueFromString(const std::string &str)
174186

175187
BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
176188
{
189+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-1"), false), UniValue);
190+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0"), false), 0LL);
191+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1"), false), 1LL);
192+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("17622195"), false), 17622195LL);
193+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("50000000"), false), 50000000LL);
194+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("89898989"), false), 89898989LL);
195+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("100000000"), false), 100000000LL); // 1 CURRENCY_UNIT
196+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("100000000000000"), false), 100000000000000LL); // 1 M CURRENCY_UNIT
197+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2100000000000000"), false), 2100000000000000LL); // 21 M CURRENCY_UNIT
198+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2099999999999999"), false), 2099999999999999LL);
199+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2099999999999990"), false), 2099999999999990LL);
200+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("209999999999999"), false), 209999999999999LL);
201+
177202
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-0.00000001")), UniValue);
178203
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0")), 0LL);
179204
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000000")), 0LL);

0 commit comments

Comments
 (0)