Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
asset.hpp
Go to the documentation of this file.
1#pragma once
5
6namespace sysio { namespace chain {
7
18{
19 static constexpr int64_t max_amount = (1LL << 62) - 1;
20
21 explicit asset(share_type a = 0, symbol id = symbol(CORE_SYMBOL)) :amount(a), sym(id) {
22 SYS_ASSERT( is_amount_within_range(), asset_type_exception, "magnitude of asset amount must be less than 2^62" );
23 SYS_ASSERT( sym.valid(), asset_type_exception, "invalid symbol" );
24 }
25
26 bool is_amount_within_range()const { return -max_amount <= amount && amount <= max_amount; }
27 bool is_valid()const { return is_amount_within_range() && sym.valid(); }
28
29 double to_real()const { return static_cast<double>(amount) / precision(); }
30
31 uint8_t decimals()const;
32 string symbol_name()const;
33 int64_t precision()const;
34 const symbol& get_symbol() const { return sym; }
35 share_type get_amount()const { return amount; }
36
37 static asset from_string(const string& from);
38 string to_string()const;
39
41 {
42 SYS_ASSERT(get_symbol() == o.get_symbol(), asset_type_exception, "addition between two different asset is not allowed");
43 amount += o.amount;
44 return *this;
45 }
46
48 {
49 SYS_ASSERT(get_symbol() == o.get_symbol(), asset_type_exception, "subtraction between two different asset is not allowed");
50 amount -= o.amount;
51 return *this;
52 }
53 asset operator -()const { return asset(-amount, get_symbol()); }
54
55 friend bool operator == (const asset& a, const asset& b)
56 {
57 return std::tie(a.get_symbol(), a.amount) == std::tie(b.get_symbol(), b.amount);
58 }
59 friend bool operator < (const asset& a, const asset& b)
60 {
61 SYS_ASSERT(a.get_symbol() == b.get_symbol(), asset_type_exception, "logical operation between two different asset is not allowed");
62 return std::tie(a.amount,a.get_symbol()) < std::tie(b.amount,b.get_symbol());
63 }
64 friend bool operator <= (const asset& a, const asset& b) { return (a == b) || (a < b); }
65 friend bool operator != (const asset& a, const asset& b) { return !(a == b); }
66 friend bool operator > (const asset& a, const asset& b) { return !(a <= b); }
67 friend bool operator >= (const asset& a, const asset& b) { return !(a < b); }
68
69 friend asset operator - (const asset& a, const asset& b) {
70 SYS_ASSERT(a.get_symbol() == b.get_symbol(), asset_type_exception, "subtraction between two different asset is not allowed");
71 return asset(a.amount - b.amount, a.get_symbol());
72 }
73
74 friend asset operator + (const asset& a, const asset& b) {
75 SYS_ASSERT(a.get_symbol() == b.get_symbol(), asset_type_exception, "addition between two different asset is not allowed");
76 return asset(a.amount + b.amount, a.get_symbol());
77 }
78
79 friend std::ostream& operator << (std::ostream& out, const asset& a) { return out << a.to_string(); }
80
81 friend struct fc::reflector<asset>;
82
83 void reflector_init()const {
84 SYS_ASSERT( is_amount_within_range(), asset_type_exception, "magnitude of asset amount must be less than 2^62" );
85 SYS_ASSERT( sym.valid(), asset_type_exception, "invalid symbol" );
86 }
87
88private:
89 share_type amount;
90 symbol sym;
91
92};
93
100
101bool operator < (const asset& a, const asset& b);
102bool operator <= (const asset& a, const asset& b);
103
104}} // namespace sysio::chain
105
106namespace fc {
107inline void to_variant(const sysio::chain::asset& var, fc::variant& vo) { vo = var.to_string(); }
111}
112
113namespace fc {
115 if( var.is_array() ) {
116 const auto& va = var.get_array();
117 from_variant(va.at(0), vo.quantity);
118 from_variant(va.at(1), vo.contract);
119 } else {
120 const auto& vars = var.get_object();
121 from_variant(vars["quantity"], vo.quantity);
122 from_variant(vars["contract"], vo.contract);
123 }
124}
125}
126
127FC_REFLECT(sysio::chain::asset, (amount)(sym))
128FC_REFLECT(sysio::chain::extended_asset, (quantity)(contract) )
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
bool is_array() const
Definition variant.cpp:368
variant_object & get_object()
Definition variant.cpp:554
const string & get_string() const
Definition variant.cpp:606
variants & get_array()
Definition variant.cpp:496
bool valid() const
Definition symbol.hpp:83
uint64_t id
Definition code_cache.cpp:0
namespace sysio::chain
Definition authority.cpp:3
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
bool operator<(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:21
bool operator<=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:25
int64_t share_type
Definition types.hpp:240
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
signed __int64 int64_t
Definition stdint.h:135
unsigned char uint8_t
Definition stdint.h:124
defines visit functions for T Unless this is specialized, visit() will not be defined for T.
Definition reflect.hpp:33
bool is_valid() const
Definition asset.hpp:27
bool is_amount_within_range() const
Definition asset.hpp:26
string symbol_name() const
Definition asset.cpp:11
asset & operator-=(const asset &o)
Definition asset.hpp:47
string to_string() const
Definition asset.cpp:19
friend bool operator<(const asset &a, const asset &b)
Definition asset.hpp:59
int64_t precision() const
Definition asset.cpp:15
uint8_t decimals() const
Definition asset.cpp:7
asset operator-() const
Definition asset.hpp:53
friend asset operator+(const asset &a, const asset &b)
Definition asset.hpp:74
asset & operator+=(const asset &o)
Definition asset.hpp:40
const symbol & get_symbol() const
Definition asset.hpp:34
friend bool operator>(const asset &a, const asset &b)
Definition asset.hpp:66
friend bool operator==(const asset &a, const asset &b)
Definition asset.hpp:55
friend bool operator<=(const asset &a, const asset &b)
Definition asset.hpp:64
static constexpr int64_t max_amount
Definition asset.hpp:19
friend bool operator>=(const asset &a, const asset &b)
Definition asset.hpp:67
static asset from_string(const string &from)
Definition asset.cpp:31
friend std::ostream & operator<<(std::ostream &out, const asset &a)
Definition asset.hpp:79
void reflector_init() const
Definition asset.hpp:83
share_type get_amount() const
Definition asset.hpp:35
friend bool operator!=(const asset &a, const asset &b)
Definition asset.hpp:65
asset(share_type a=0, symbol id=symbol(CORE_SYMBOL))
Definition asset.hpp:21
double to_real() const
Definition asset.hpp:29
extended_asset(asset a, name n)
Definition asset.hpp:96
Immutable except for fc::from_variant.
Definition name.hpp:43