double/string to monero integer convertion methods

This commit is contained in:
Ilya Kitaev 2016-06-16 16:42:33 +03:00
parent 3ac20a46b3
commit 3318addafa
3 changed files with 33 additions and 2 deletions

View file

@ -36,6 +36,7 @@
#include "mnemonics/electrum-words.h" #include "mnemonics/electrum-words.h"
#include <boost/format.hpp> #include <boost/format.hpp>
#include <sstream>
using namespace std; using namespace std;
using namespace cryptonote; using namespace cryptonote;
@ -122,6 +123,22 @@ string Wallet::displayAmount(uint64_t amount)
return cryptonote::print_money(amount); return cryptonote::print_money(amount);
} }
uint64_t Wallet::amountFromString(const string &amount)
{
uint64_t result;
cryptonote::parse_amount(result, amount);
return result;
}
uint64_t Wallet::amountFromDouble(double amount)
{
std::stringstream ss;
ss << std::fixed << std::setprecision(CRYPTONOTE_DISPLAY_DECIMAL_POINT) << amount;
return amountFromString(ss.str());
}
///////////////////////// WalletImpl implementation //////////////////////// ///////////////////////// WalletImpl implementation ////////////////////////
WalletImpl::WalletImpl(bool testnet) WalletImpl::WalletImpl(bool testnet)
:m_wallet(nullptr), m_status(Wallet::Status_Ok), m_trustedDaemon(false), :m_wallet(nullptr), m_status(Wallet::Status_Ok), m_trustedDaemon(false),

View file

@ -160,6 +160,8 @@ struct Wallet
virtual uint64_t balance() const = 0; virtual uint64_t balance() const = 0;
virtual uint64_t unlockedBalance() const = 0; virtual uint64_t unlockedBalance() const = 0;
static std::string displayAmount(uint64_t amount); static std::string displayAmount(uint64_t amount);
static uint64_t amountFromString(const std::string &amount);
static uint64_t amountFromDouble(double amount);
// TODO? // TODO?
// virtual uint64_t unlockedDustBalance() const = 0; // virtual uint64_t unlockedDustBalance() const = 0;
virtual bool refresh() = 0; virtual bool refresh() = 0;

View file

@ -247,7 +247,7 @@ TEST_F(WalletManagerTest, WalletManagerChangesPassword)
ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2));
ASSERT_TRUE(wmgr->closeWallet(wallet1)); ASSERT_TRUE(wmgr->closeWallet(wallet1));
Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS2); Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS2);
ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok);quint64
ASSERT_TRUE(wallet2->seed() == seed1); ASSERT_TRUE(wallet2->seed() == seed1);
ASSERT_TRUE(wmgr->closeWallet(wallet2)); ASSERT_TRUE(wmgr->closeWallet(wallet2));
Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS);
@ -390,7 +390,19 @@ TEST_F(WalletTest1, WalletRefresh)
} }
TEST_F(WalletTest1, WalletConvertsToString)
{
std::string strAmount = Bitmonero::Wallet::displayAmount(AMOUNT_5XMR);
ASSERT_TRUE(AMOUNT_5XMR == Bitmonero::Wallet::amountFromString(strAmount));
ASSERT_TRUE(AMOUNT_5XMR == Bitmonero::Wallet::amountFromDouble(5.0));
ASSERT_TRUE(AMOUNT_10XMR == Bitmonero::Wallet::amountFromDouble(10.0));
ASSERT_TRUE(AMOUNT_1XMR == Bitmonero::Wallet::amountFromDouble(1.0));
}
/*
TEST_F(WalletTest1, WalletTransaction) TEST_F(WalletTest1, WalletTransaction)
{ {
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
@ -415,7 +427,7 @@ TEST_F(WalletTest1, WalletTransaction)
ASSERT_FALSE(wallet1->balance() == balance); ASSERT_FALSE(wallet1->balance() == balance);
ASSERT_TRUE(wmgr->closeWallet(wallet1)); ASSERT_TRUE(wmgr->closeWallet(wallet1));
} }
*/
TEST_F(WalletTest1, WalletHistory) TEST_F(WalletTest1, WalletHistory)
{ {
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);