Changeset View
Changeset View
Standalone View
Standalone View
src/amount.h
Show First 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | public: | ||||
// serialization support | // serialization support | ||||
SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); } | SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); } | ||||
}; | }; | ||||
static constexpr Amount SATOSHI = Amount::satoshi(); | static constexpr Amount SATOSHI = Amount::satoshi(); | ||||
static constexpr Amount CASH = 100 * SATOSHI; | static constexpr Amount CASH = 100 * SATOSHI; | ||||
static constexpr Amount COIN = 100000000 * SATOSHI; | static constexpr Amount COIN = 100000000 * SATOSHI; | ||||
class Currency { | |||||
Amount m_unit, m_subunit; | |||||
uint8_t m_decimals; | |||||
public: | |||||
constexpr Currency(Amount unit, Amount subunit, uint8_t decimals) | |||||
: m_unit(unit), m_subunit(subunit), m_decimals(decimals) {} | |||||
static const Currency &get(); | |||||
//! the base unit ex: 1 Bitcoin = 100'000'000 satoshis | |||||
const Amount &unit() const { return m_unit; } | |||||
//! the subunit ex: for Bitcoin, 1 satoshi | |||||
const Amount &subunit() const { return m_subunit; } | |||||
//! # of decimals ex: 1 Bitcoin can have 8 decimal places | |||||
uint8_t decimals() const { return m_decimals; } | |||||
}; | |||||
Fabien: A few things here:
- I had to read the comments to figure out how a unit could be an Amount… | |||||
/** | /** | ||||
* No amount larger than this (in satoshi) is valid. | * No amount larger than this (in satoshi) is valid. | ||||
* | * | ||||
* Note that this constant is *not* the total money supply, which in Bitcoin | * Note that this constant is *not* the total money supply, which in Bitcoin | ||||
* currently happens to be less than 21,000,000 BCH for various reasons, but | * currently happens to be less than 21,000,000 BCH for various reasons, but | ||||
* rather a sanity check. As this sanity check is used by consensus-critical | * rather a sanity check. As this sanity check is used by consensus-critical | ||||
* validation code, the exact value of the MAX_MONEY constant is consensus | * validation code, the exact value of the MAX_MONEY constant is consensus | ||||
* critical; in unusual circumstances like a(nother) overflow bug that allowed | * critical; in unusual circumstances like a(nother) overflow bug that allowed | ||||
* for the creation of coins out of thin air modification could lead to a fork. | * for the creation of coins out of thin air modification could lead to a fork. | ||||
*/ | */ | ||||
static const Amount MAX_MONEY = 21000000 * COIN; | static const Amount MAX_MONEY = 21000000 * COIN; | ||||
inline bool MoneyRange(const Amount nValue) { | inline bool MoneyRange(const Amount nValue) { | ||||
return nValue >= Amount::zero() && nValue <= MAX_MONEY; | return nValue >= Amount::zero() && nValue <= MAX_MONEY; | ||||
} | } | ||||
#endif // BITCOIN_AMOUNT_H | #endif // BITCOIN_AMOUNT_H |
A few things here: