Skip to content

Commit

Permalink
Update RPC & QT Wallet to the new units #3386 (#3392)
Browse files Browse the repository at this point in the history
* Remove knano and mnano references from QT wallet #3386
  • Loading branch information
shryder authored Jul 26, 2021
1 parent 18640c1 commit 88c87f7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 44 deletions.
43 changes: 41 additions & 2 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ void nano::json_handler::process_request (bool unsafe_a)
{
mnano_to_raw (nano::kxrb_ratio);
}
else if (action == "nano_from_raw" || action == "rai_from_raw")
else if (action == "rai_from_raw")
{
mnano_from_raw (nano::xrb_ratio);
}
else if (action == "nano_to_raw" || action == "rai_to_raw")
else if (action == "rai_to_raw")
{
mnano_to_raw (nano::xrb_ratio);
}
Expand All @@ -121,6 +121,14 @@ void nano::json_handler::process_request (bool unsafe_a)
{
mnano_to_raw ();
}
else if (action == "nano_to_raw")
{
nano_to_raw ();
}
else if (action == "raw_to_nano")
{
raw_to_nano ();
}
else if (action == "password_valid")
{
password_valid ();
Expand Down Expand Up @@ -2731,6 +2739,7 @@ void nano::json_handler::ledger ()
void nano::json_handler::mnano_from_raw (nano::uint128_t ratio)
{
auto amount (amount_impl ());
response_l.put ("deprecated", "1");
if (!ec)
{
auto result (amount.number () / ratio);
Expand All @@ -2742,6 +2751,7 @@ void nano::json_handler::mnano_from_raw (nano::uint128_t ratio)
void nano::json_handler::mnano_to_raw (nano::uint128_t ratio)
{
auto amount (amount_impl ());
response_l.put ("deprecated", "1");
if (!ec)
{
auto result (amount.number () * ratio);
Expand All @@ -2757,6 +2767,35 @@ void nano::json_handler::mnano_to_raw (nano::uint128_t ratio)
response_errors ();
}

void nano::json_handler::nano_to_raw ()
{
auto amount (amount_impl ());
if (!ec)
{
auto result (amount.number () * nano::Mxrb_ratio);
if (result > amount.number ())
{
response_l.put ("amount", result.convert_to<std::string> ());
}
else
{
ec = nano::error_common::invalid_amount_big;
}
}
response_errors ();
}

void nano::json_handler::raw_to_nano ()
{
auto amount (amount_impl ());
if (!ec)
{
auto result (amount.number () / nano::Mxrb_ratio);
response_l.put ("amount", result.convert_to<std::string> ());
}
response_errors ();
}

/*
* @warning This is an internal/diagnostic RPC, do not rely on its interface being stable
*/
Expand Down
2 changes: 2 additions & 0 deletions nano/node/json_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
void ledger ();
void mnano_to_raw (nano::uint128_t = nano::Mxrb_ratio);
void mnano_from_raw (nano::uint128_t = nano::Mxrb_ratio);
void nano_to_raw ();
void raw_to_nano ();
void node_id ();
void node_id_delete ();
void password_change ();
Expand Down
46 changes: 11 additions & 35 deletions nano/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,16 +1458,8 @@ void nano_qt::wallet::change_rendering_ratio (nano::uint128_t const & rendering_
std::string nano_qt::wallet::format_balance (nano::uint128_t const & balance) const
{
auto balance_str = nano::amount (balance).format_balance (rendering_ratio, 3, false);
auto unit = std::string ("NANO");
if (rendering_ratio == nano::kxrb_ratio)
{
unit = std::string ("knano");
}
else if (rendering_ratio == nano::xrb_ratio)
{
unit = std::string ("nano");
}
else if (rendering_ratio == nano::raw_ratio)
auto unit = std::string ("nano");
if (rendering_ratio == nano::raw_ratio)
{
unit = std::string ("raw");
}
Expand Down Expand Up @@ -1751,8 +1743,6 @@ nano_qt::advanced_actions::advanced_actions (nano_qt::wallet & wallet_a) :
scale_layout (new QHBoxLayout),
scale_label (new QLabel ("Scale:")),
ratio_group (new QButtonGroup),
mnano_unit (new QRadioButton ("Mnano")),
knano_unit (new QRadioButton ("knano")),
nano_unit (new QRadioButton ("nano")),
raw_unit (new QRadioButton ("raw")),
back (new QPushButton ("Back")),
Expand All @@ -1775,17 +1765,11 @@ nano_qt::advanced_actions::advanced_actions (nano_qt::wallet & wallet_a) :
peers_back (new QPushButton ("Back")),
wallet (wallet_a)
{
ratio_group->addButton (mnano_unit);
ratio_group->addButton (knano_unit);
ratio_group->addButton (nano_unit);
ratio_group->addButton (raw_unit);
ratio_group->setId (mnano_unit, 0);
ratio_group->setId (knano_unit, 1);
ratio_group->setId (nano_unit, 2);
ratio_group->setId (raw_unit, 3);
scale_layout->addWidget (scale_label);
scale_layout->addWidget (mnano_unit);
scale_layout->addWidget (knano_unit);
scale_layout->addWidget (nano_unit);
scale_layout->addWidget (raw_unit);
scale_window->setLayout (scale_layout);
Expand Down Expand Up @@ -1839,25 +1823,11 @@ nano_qt::advanced_actions::advanced_actions (nano_qt::wallet & wallet_a) :
layout->addWidget (back);
window->setLayout (layout);

QObject::connect (mnano_unit, &QRadioButton::toggled, [this] () {
if (mnano_unit->isChecked ())
{
QSettings ().setValue (saved_ratio_key, ratio_group->id (mnano_unit));
this->wallet.change_rendering_ratio (nano::Mxrb_ratio);
}
});
QObject::connect (knano_unit, &QRadioButton::toggled, [this] () {
if (knano_unit->isChecked ())
{
QSettings ().setValue (saved_ratio_key, ratio_group->id (knano_unit));
this->wallet.change_rendering_ratio (nano::kxrb_ratio);
}
});
QObject::connect (nano_unit, &QRadioButton::toggled, [this] () {
if (nano_unit->isChecked ())
{
QSettings ().setValue (saved_ratio_key, ratio_group->id (nano_unit));
this->wallet.change_rendering_ratio (nano::xrb_ratio);
this->wallet.change_rendering_ratio (nano::Mxrb_ratio);
}
});
QObject::connect (raw_unit, &QRadioButton::toggled, [this] () {
Expand All @@ -1867,17 +1837,23 @@ nano_qt::advanced_actions::advanced_actions (nano_qt::wallet & wallet_a) :
this->wallet.change_rendering_ratio (nano::raw_ratio);
}
});
auto selected_ratio_id (QSettings ().value (saved_ratio_key, ratio_group->id (mnano_unit)).toInt ());
auto selected_ratio_id (QSettings ().value (saved_ratio_key, ratio_group->id (nano_unit)).toInt ());
auto selected_ratio_button = ratio_group->button (selected_ratio_id);
debug_assert (selected_ratio_button != nullptr);

// Make sure value is not out of bounds
if (selected_ratio_id < 0 || selected_ratio_id > 1)
{
QSettings ().setValue (saved_ratio_key, 0);
selected_ratio_id = 0;
}
if (selected_ratio_button)
{
selected_ratio_button->click ();
}
else
{
mnano_unit->click ();
nano_unit->click ();
}
QObject::connect (wallet_refresh, &QPushButton::released, [this] () {
this->wallet.accounts.refresh ();
Expand Down
2 changes: 0 additions & 2 deletions nano/qt/qt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ class advanced_actions
QHBoxLayout * scale_layout;
QLabel * scale_label;
QButtonGroup * ratio_group;
QRadioButton * mnano_unit;
QRadioButton * knano_unit;
QRadioButton * nano_unit;
QRadioButton * raw_unit;
QPushButton * back;
Expand Down
2 changes: 1 addition & 1 deletion nano/qt_test/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST (wallet, startup_balance)
wallet->needs_balance_refresh = true;
wallet->start ();
wallet->application.processEvents (QEventLoop::AllEvents);
ASSERT_EQ ("Balance: 0 NANO", wallet->self.balance_label->text ().toStdString ());
ASSERT_EQ ("Balance: 0 nano", wallet->self.balance_label->text ().toStdString ());
}

TEST (wallet, select_account)
Expand Down
8 changes: 4 additions & 4 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2465,17 +2465,17 @@ TEST (rpc, nano_to_raw)
request1.put ("action", "nano_to_raw");
request1.put ("amount", "1");
auto response1 (wait_response (system, rpc, request1));
ASSERT_EQ (nano::xrb_ratio.convert_to<std::string> (), response1.get<std::string> ("amount"));
ASSERT_EQ (nano::Mxrb_ratio.convert_to<std::string> (), response1.get<std::string> ("amount"));
}

TEST (rpc, nano_from_raw)
TEST (rpc, raw_to_nano)
{
nano::system system;
auto node1 = add_ipc_enabled_node (system);
auto [rpc, rpc_ctx] = add_rpc (system, node1);
boost::property_tree::ptree request1;
request1.put ("action", "nano_from_raw");
request1.put ("amount", nano::xrb_ratio.convert_to<std::string> ());
request1.put ("action", "raw_to_nano");
request1.put ("amount", nano::Mxrb_ratio.convert_to<std::string> ());
auto response1 (wait_response (system, rpc, request1));
ASSERT_EQ ("1", response1.get<std::string> ("amount"));
}
Expand Down

0 comments on commit 88c87f7

Please sign in to comment.