Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support _sat for satoshi level denominations #831

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,19 @@ CAmount AmountFromValue(const UniValue& value)
{
if (!value.isNum() && !value.isStr())
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
std::string val_string = value.getValStr();

// Look for _sat denomination
int sat_pos = val_string.find("_sat");
if (val_string.size() >= 4 && ((unsigned int)sat_pos == val_string.size() - 4)) {
val_string = val_string.substr(0, sat_pos);
}
CAmount amount;
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
if (!ParseFixedPoint(val_string, 8, &amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
if (sat_pos != std::string::npos) {
amount /= COIN;
}
if (!MoneyRange(amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
return amount;
Expand Down
11 changes: 11 additions & 0 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e+11")), UniValue); //overflow error
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e11")), UniValue); //overflow error signless
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("93e+9")), UniValue); //overflow error

// _sat values
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0_sat")), 0LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1_sat")), 1LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10_sat")), 10LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("100_sat")), 100LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("123_sat")), 123LL);

BOOST_CHECK_THROW(AmountFromValue(ValueFromString("123_sat_1")), UniValue);
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("2100000000000001_sat")), UniValue);
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("123_sat_sat")), UniValue);
}

BOOST_AUTO_TEST_CASE(json_parse_errors)
Expand Down