Bitcoin ABC 0.33.10
P2P Digital Currency
amount.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#include <consensus/amount.h>
8
9#include <common/args.h>
10#include <currencyunit.h>
11#include <univalue.h>
12
13#include <tinyformat.h>
14
15using namespace std::string_view_literals;
16
17static constexpr Currency BCHA{COIN, SATOSHI, 8, "BCHA"sv};
18static constexpr Currency XEC{100 * SATOSHI, SATOSHI, 2, "XEC"sv};
19
20static_assert(BCHA.baseunit > 1 * SATOSHI,
21 "BCHA base unit must be greater than 1 satoshi");
22static_assert(XEC.baseunit > 1 * SATOSHI,
23 "XEC base unit must be greater than 1 satoshi");
24
26 return gArgs.GetBoolArg("-ecash", DEFAULT_ECASH) ? XEC : BCHA;
27}
28
29std::string Amount::ToString() const {
30 const auto &currency = Currency::get();
31 return strprintf("%d.%0*d %s", *this / currency.baseunit, currency.decimals,
32 (*this % currency.baseunit) / currency.subunit,
33 std::string{currency.ticker});
34}
35
36Amount::operator UniValue() const {
37 const auto &currency = Currency::get();
38 int64_t quotient = *this / currency.baseunit;
39 int64_t remainder = (*this % currency.baseunit) / currency.subunit;
40 if (amount < 0) {
41 quotient = -quotient;
42 remainder = -remainder;
43 }
45 strprintf("%s%d.%0*d", amount < 0 ? "-" : "", quotient,
46 currency.decimals, remainder));
47}
48
49std::optional<Amount> Amount::CheckedAdd(const Amount &other) const noexcept {
50 auto result = ::CheckedAdd(this->amount, other.amount);
51 if (result.has_value()) {
52 return Amount(result.value());
53 }
54 return std::nullopt;
55}
56
57void Amount::SaturatingAdd(const Amount &other) noexcept {
58 amount = ::SaturatingAdd(this->amount, other.amount);
59}
static constexpr Currency BCHA
Definition: amount.cpp:17
static constexpr Currency XEC
Definition: amount.cpp:18
static constexpr Amount SATOSHI
Definition: amount.h:153
static constexpr Amount COIN
Definition: amount.h:154
ArgsManager gArgs
Definition: args.cpp:39
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:524
@ VNUM
Definition: univalue.h:34
constexpr bool DEFAULT_ECASH
Definition: currencyunit.h:10
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:23
T SaturatingAdd(const T i, const T j) noexcept
Definition: overflow.h:31
Definition: amount.h:23
void SaturatingAdd(const Amount &other) noexcept
Amount addition with integer saturation.
Definition: amount.cpp:57
std::string ToString() const
Definition: amount.cpp:29
std::optional< Amount > CheckedAdd(const Amount &other) const noexcept
Amount addition with integer overflow check.
Definition: amount.cpp:49
Amount baseunit
Definition: amount.h:157
static const Currency & get()
Definition: amount.cpp:25
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202