You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

amount.h 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_AMOUNT_H
  6. #define BITCOIN_AMOUNT_H
  7. #include "serialize.h"
  8. #include <stdlib.h>
  9. #include <string>
  10. typedef int64_t CAmount;
  11. static const CAmount COIN = 100000000;
  12. static const CAmount CENT = 1000000;
  13. extern const std::string CURRENCY_UNIT;
  14. /** No amount larger than this (in satoshi) is valid.
  15. *
  16. * Note that this constant is *not* the total money supply, which in Bitcoin
  17. * currently happens to be less than 21,000,000 BTC for various reasons, but
  18. * rather a sanity check. As this sanity check is used by consensus-critical
  19. * validation code, the exact value of the MAX_MONEY constant is consensus
  20. * critical; in unusual circumstances like a(nother) overflow bug that allowed
  21. * for the creation of coins out of thin air modification could lead to a fork.
  22. * */
  23. static const CAmount MAX_MONEY = 21000000 * COIN;
  24. inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
  25. /** Type-safe wrapper class for fee rates
  26. * (how much to pay based on transaction size)
  27. */
  28. class CFeeRate
  29. {
  30. private:
  31. CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
  32. public:
  33. CFeeRate() : nSatoshisPerK(0) { }
  34. explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
  35. CFeeRate(const CAmount& nFeePaid, size_t nSize);
  36. CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
  37. CAmount GetFee(size_t size) const; // unit returned is satoshis
  38. CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes
  39. friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
  40. friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
  41. friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
  42. friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
  43. friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
  44. CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
  45. std::string ToString() const;
  46. ADD_SERIALIZE_METHODS;
  47. template <typename Stream, typename Operation>
  48. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  49. READWRITE(nSatoshisPerK);
  50. }
  51. };
  52. #endif // BITCOIN_AMOUNT_H