Changeset View
Changeset View
Standalone View
Standalone View
src/amount.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Copyright (c) 2017-2019 The Bitcoin developers | // Copyright (c) 2017-2019 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <network.h> | #include <network.h> | ||||
#include <rpc/protocol.h> | |||||
#include <rpc/request.h> | |||||
#include <univalue.h> | #include <univalue.h> | ||||
#include <util/strencodings.h> | |||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
std::string Amount::ToString() const { | std::string Amount::ToString() const { | ||||
return strprintf("%d.%08d %s", *this / COIN, (*this % COIN) / SATOSHI, | return strprintf("%d.%08d %s", *this / COIN, (*this % COIN) / SATOSHI, | ||||
CURRENCY_UNIT); | CURRENCY_UNIT); | ||||
} | } | ||||
Amount::Amount(const UniValue &value) { | |||||
if (!value.isNum() && !value.isStr()) { | |||||
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string"); | |||||
} | |||||
if (!ParseFixedPoint(value.getValStr(), 8, &amount)) { | |||||
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); | |||||
} | |||||
if (!MoneyRange(*this)) { | |||||
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range"); | |||||
} | |||||
} | |||||
Amount::operator UniValue() const { | Amount::operator UniValue() const { | ||||
bool sign = *this < Amount::zero(); | bool sign = *this < Amount::zero(); | ||||
Amount n_abs(sign ? -amount : amount); | Amount n_abs(sign ? -amount : amount); | ||||
int64_t quotient = n_abs / COIN; | int64_t quotient = n_abs / COIN; | ||||
int64_t remainder = (n_abs % COIN) / SATOSHI; | int64_t remainder = (n_abs % COIN) / SATOSHI; | ||||
return UniValue(UniValue::VNUM, strprintf("%s%d.%08d", sign ? "-" : "", | return UniValue(UniValue::VNUM, strprintf("%s%d.%08d", sign ? "-" : "", | ||||
quotient, remainder)); | quotient, remainder)); | ||||
} | } |