Skip to content
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
31 changes: 4 additions & 27 deletions src/mastercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ using namespace mastercore;
#include "mastercore_tx.h"
#include "mastercore_sp.h"
#include "mastercore_errors.h"
#include "mastercore_parse_string.h"

// part of 'breakout' feature
static const int nBlockTop = 0;
Expand Down Expand Up @@ -297,35 +298,11 @@ bool isNonMainNet()
return (TestNet() || RegTest());
}

// mostly taken from Bitcoin's FormatMoney()
string FormatDivisibleMP(int64_t n, bool fSign)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
int64_t n_abs = (n > 0 ? n : -n);
int64_t quotient = n_abs/COIN;
int64_t remainder = n_abs%COIN;
string str = strprintf("%d.%08d", quotient, remainder);

if (!fSign) return str;

if (n < 0)
str.insert((unsigned int)0, 1, '-');
else
str.insert((unsigned int)0, 1, '+');
return str;
}

std::string mastercore::FormatIndivisibleMP(int64_t n)
{
string str = strprintf("%ld", n);
return str;
}

// TODO: move into mastercore_parse_string.h/.cpp
std::string FormatMP(unsigned int property, int64_t n, bool fSign)
{
if (isPropertyDivisible(property)) return FormatDivisibleMP(n, fSign);
else return FormatIndivisibleMP(n);
if (isPropertyDivisible(property)) return FormatDivisibleAmount(n, fSign);
else return FormatIndivisibleAmount(n);
}

string const CMPSPInfo::watermarkKey("watermark");
Expand Down
11 changes: 7 additions & 4 deletions src/mastercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef _MASTERCOIN
#define _MASTERCOIN 1

#include "mastercore_parse_string.h"

#include "netbase.h"
#include "protocol.h"

Expand Down Expand Up @@ -132,8 +134,7 @@ enum FILETYPES {
#define MASTERCOIN_CURRENCY_TMSC 2

// forward declarations
std::string FormatDivisibleMP(int64_t n, bool fSign = false);
std::string FormatMP(unsigned int, int64_t n, bool fSign = false);
std::string FormatMP(unsigned int, int64_t n, bool fSign = false); // TODO: move into mastercore_parse_string.h/.cpp
uint256 send_MP(const string &FromAddress, const string &ToAddress, const string &RedeemAddress, unsigned int CurrencyID, uint64_t Amount);
int64_t feeCheck(const string &address);

Expand Down Expand Up @@ -240,7 +241,10 @@ typedef struct
if (bDivisible)
{
printf("%22s [SO_RESERVE= %22s , ACCEPT_RESERVE= %22s ] %22s\n",
FormatDivisibleMP(money, true).c_str(), FormatDivisibleMP(so_r, true).c_str(), FormatDivisibleMP(a_r, true).c_str(), FormatDivisibleMP(pending, true).c_str());
mastercore::FormatDivisibleAmount(money, true).c_str(),
mastercore::FormatDivisibleAmount(so_r, true).c_str(),
mastercore::FormatDivisibleAmount(a_r, true).c_str(),
mastercore::FormatDivisibleAmount(pending, true).c_str());
}
else
{
Expand Down Expand Up @@ -389,7 +393,6 @@ string getPropertyName(unsigned int propertyId);
bool isCrowdsaleActive(unsigned int propertyId);
bool isCrowdsalePurchase(uint256 txid, string address, int64_t *propertyId = NULL, int64_t *userTokens = NULL, int64_t *issuerTokens = NULL);
bool isMPinBlockRange(int starting_block, int ending_block, bool bDeleteFound);
std::string FormatIndivisibleMP(int64_t n);

int ClassB_send(const string &senderAddress, const string &receiverAddress, const string &redemptionAddress, const vector<unsigned char> &data, uint256 & txid, int64_t additional = 0);

Expand Down
50 changes: 50 additions & 0 deletions src/mastercore_parse_string.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "mastercore_parse_string.h"

#include "util.h"

#include <stdint.h>
#include <algorithm>
#include <string>
Expand All @@ -8,6 +10,54 @@

namespace mastercore
{
std::string FormatDivisibleAmount(int64_t n)
{
// Prepend sign only for negative numbers
bool is_negative = (n < 0);

return FormatDivisibleAmount(n, is_negative);
}

std::string FormatDivisibleAmount(int64_t n, bool sign)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
int64_t n_abs = (n > 0 ? n : -n);
int64_t quotient = n_abs / COIN;
int64_t remainder = n_abs % COIN;
std::string str = tfm::format("%ld.%08d", quotient, remainder);

if (!sign)
return str;

if (n < 0)
str.insert((unsigned int) 0, 1, '-');
else
str.insert((unsigned int) 0, 1, '+');

return str;
}

std::string FormatIndivisibleAmount(int64_t n)
{
return tfm::format("%ld", n);
}

std::string FormatTokenAmount(int64_t n)
{
// Default token amounts are formatted as indivisible
// token which aligns well with an integer input
return FormatIndivisibleAmount(n);
}

std::string FormatTokenAmount(int64_t n, bool divisible)
{
if (divisible)
return FormatDivisibleAmount(n);

return FormatIndivisibleAmount(n);
}

int64_t StrToInt64(const std::string& str, bool divisible)
{
// copy original, so it remains unchanged
Expand Down
20 changes: 19 additions & 1 deletion src/mastercore_parse_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@

namespace mastercore
{
// TODO: add FormatMP() -- currently depends on isPropertyDivisible()

// Formats a value as divisible token amount with 8 digits.
// Per default a minus sign is prepended, if n is negative.
// A positive or negative sign can be enforced.
std::string FormatDivisibleAmount(int64_t n);
std::string FormatDivisibleAmount(int64_t n, bool sign);

// Formats a value as indivisible token amount with leading
// minus sign, if n is negative.
std::string FormatIndivisibleAmount(int64_t n);

// Formats a value as token amount and prepends a minus sign,
// if the value is negative. Divisible amounts have 8 digits.
// Per default n is formatted as indivisible amount.
std::string FormatTokenAmount(int64_t n);
std::string FormatTokenAmount(int64_t n, bool divisible);

// Converts strings to 64 bit wide interger.
// Divisible and indivisible amounts are accepted.
// 1 indivisible unit equals 0.00000001 divisible units.
Expand All @@ -17,4 +35,4 @@ namespace mastercore
int64_t StrToInt64(const std::string& str, bool divisible);
}

#endif // _MASTERCOIN_PARSE_STRRING
#endif // _MASTERCOIN_PARSE_STRING
Loading