Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::chain::asset Struct Reference

#include <asset.hpp>

Inheritance diagram for sysio::chain::asset:
Collaboration diagram for sysio::chain::asset:

Public Member Functions

 asset (share_type a=0, symbol id=symbol(CORE_SYMBOL))
 
bool is_amount_within_range () const
 
bool is_valid () const
 
double to_real () const
 
uint8_t decimals () const
 
string symbol_name () const
 
int64_t precision () const
 
const symbolget_symbol () const
 
share_type get_amount () const
 
string to_string () const
 
assetoperator+= (const asset &o)
 
assetoperator-= (const asset &o)
 
asset operator- () const
 
void reflector_init () const
 

Static Public Member Functions

static asset from_string (const string &from)
 

Static Public Attributes

static constexpr int64_t max_amount = (1LL << 62) - 1
 

Friends

struct fc::reflector< asset >
 
bool operator== (const asset &a, const asset &b)
 
bool operator< (const asset &a, const asset &b)
 
bool operator<= (const asset &a, const asset &b)
 
bool operator!= (const asset &a, const asset &b)
 
bool operator> (const asset &a, const asset &b)
 
bool operator>= (const asset &a, const asset &b)
 
asset operator- (const asset &a, const asset &b)
 
asset operator+ (const asset &a, const asset &b)
 
std::ostream & operator<< (std::ostream &out, const asset &a)
 

Detailed Description

asset includes amount and currency symbol

asset::from_string takes a string of the form "10.0000 CUR" and constructs an asset with amount = 10 and symbol(4,"CUR")

Definition at line 17 of file asset.hpp.

Constructor & Destructor Documentation

◆ asset()

sysio::chain::asset::asset ( share_type a = 0,
symbol id = symbol(CORE_SYMBOL) )
inlineexplicit

Definition at line 21 of file asset.hpp.

21 :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 }
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
bool valid() const
Definition symbol.hpp:83
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
bool is_amount_within_range() const
Definition asset.hpp:26
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ decimals()

uint8_t sysio::chain::asset::decimals ( ) const

Definition at line 7 of file asset.cpp.

7 {
8 return sym.decimals();
9}
uint8_t decimals() const
Definition symbol.hpp:93
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_string()

asset sysio::chain::asset::from_string ( const string & from)
static

Definition at line 31 of file asset.cpp.

32{
33 try {
34 string s = fc::trim(from);
35
36 // Find space in order to split amount and symbol
37 auto space_pos = s.find(' ');
38 SYS_ASSERT((space_pos != string::npos), asset_type_exception, "Asset's amount and symbol should be separated with space");
39 auto symbol_str = fc::trim(s.substr(space_pos + 1));
40 auto amount_str = s.substr(0, space_pos);
41
42 // Ensure that if decimal point is used (.), decimal fraction is specified
43 auto dot_pos = amount_str.find('.');
44 if (dot_pos != string::npos) {
45 SYS_ASSERT((dot_pos != amount_str.size() - 1), asset_type_exception, "Missing decimal fraction after decimal point");
46 }
47
48 // Parse symbol
49 string precision_digit_str;
50 if (dot_pos != string::npos) {
51 precision_digit_str = sysio::chain::to_string(amount_str.size() - dot_pos - 1);
52 } else {
53 precision_digit_str = "0";
54 }
55
56 string symbol_part = precision_digit_str + ',' + symbol_str;
57 symbol sym = symbol::from_string(symbol_part);
58
59 // Parse amount
60 safe<int64_t> int_part, fract_part;
61 if (dot_pos != string::npos) {
62 int_part = fc::to_int64(amount_str.substr(0, dot_pos));
63 fract_part = fc::to_int64(amount_str.substr(dot_pos + 1));
64 if (amount_str[0] == '-') fract_part *= -1;
65 } else {
66 int_part = fc::to_int64(amount_str);
67 }
68
69 safe<int64_t> amount = int_part;
70 amount *= safe<int64_t>(sym.precision());
71 amount += fract_part;
72
73 return asset(amount.value, sym);
74 }
76}
static symbol from_string(const string &from)
Definition symbol.hpp:68
#define FC_CAPTURE_LOG_AND_RETHROW(...)
fc::string to_string(double)
Definition string.cpp:131
fc::string trim(const fc::string &)
Definition string.cpp:152
int64_t to_int64(const fc::string &)
Definition string.cpp:92
asset(share_type a=0, symbol id=symbol(CORE_SYMBOL))
Definition asset.hpp:21
char * s
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_amount()

share_type sysio::chain::asset::get_amount ( ) const
inline

Definition at line 35 of file asset.hpp.

35{ return amount; }

◆ get_symbol()

const symbol & sysio::chain::asset::get_symbol ( ) const
inline

Definition at line 34 of file asset.hpp.

34{ return sym; }
Here is the caller graph for this function:

◆ is_amount_within_range()

bool sysio::chain::asset::is_amount_within_range ( ) const
inline

Definition at line 26 of file asset.hpp.

26{ return -max_amount <= amount && amount <= max_amount; }
static constexpr int64_t max_amount
Definition asset.hpp:19
Here is the caller graph for this function:

◆ is_valid()

bool sysio::chain::asset::is_valid ( ) const
inline

Definition at line 27 of file asset.hpp.

27{ return is_amount_within_range() && sym.valid(); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator+=()

asset & sysio::chain::asset::operator+= ( const asset & o)
inline

Definition at line 40 of file asset.hpp.

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 }
const symbol & get_symbol() const
Definition asset.hpp:34
Here is the call graph for this function:

◆ operator-()

asset sysio::chain::asset::operator- ( ) const
inline

Definition at line 53 of file asset.hpp.

53{ return asset(-amount, get_symbol()); }
Here is the call graph for this function:

◆ operator-=()

asset & sysio::chain::asset::operator-= ( const asset & o)
inline

Definition at line 47 of file asset.hpp.

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 }
Here is the call graph for this function:

◆ precision()

int64_t sysio::chain::asset::precision ( ) const

Definition at line 15 of file asset.cpp.

15 {
16 return sym.precision();
17}
uint64_t precision() const
Definition symbol.hpp:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reflector_init()

void sysio::chain::asset::reflector_init ( ) const
inline

Definition at line 83 of file asset.hpp.

83 {
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 }
Here is the call graph for this function:

◆ symbol_name()

string sysio::chain::asset::symbol_name ( ) const

Definition at line 11 of file asset.cpp.

11 {
12 return sym.name();
13}
string name() const
Definition symbol.hpp:104
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_real()

double sysio::chain::asset::to_real ( ) const
inline

Definition at line 29 of file asset.hpp.

29{ return static_cast<double>(amount) / precision(); }
int64_t precision() const
Definition asset.cpp:15
Here is the call graph for this function:

◆ to_string()

string sysio::chain::asset::to_string ( ) const

Definition at line 19 of file asset.cpp.

19 {
20 string sign = amount < 0 ? "-" : "";
21 int64_t abs_amount = std::abs(amount);
22 string result = fc::to_string( static_cast<int64_t>(abs_amount) / precision());
23 if( decimals() )
24 {
25 auto fract = static_cast<int64_t>(abs_amount) % precision();
26 result += "." + fc::to_string(precision() + fract).erase(0,1);
27 }
28 return sign + result + " " + symbol_name();
29}
signed __int64 int64_t
Definition stdint.h:135
string symbol_name() const
Definition asset.cpp:11
uint8_t decimals() const
Definition asset.cpp:7
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ fc::reflector< asset >

friend struct fc::reflector< asset >
friend

Definition at line 79 of file asset.hpp.

◆ operator!=

bool operator!= ( const asset & a,
const asset & b )
friend

Definition at line 65 of file asset.hpp.

65{ return !(a == b); }

◆ operator+

asset operator+ ( const asset & a,
const asset & b )
friend

Definition at line 74 of file asset.hpp.

74 {
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 }

◆ operator-

asset operator- ( const asset & a,
const asset & b )
friend

Definition at line 69 of file asset.hpp.

69 {
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 }

◆ operator<

bool operator< ( const asset & a,
const asset & b )
friend

Definition at line 59 of file asset.hpp.

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 }

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const asset & a )
friend

Definition at line 79 of file asset.hpp.

79{ return out << a.to_string(); }

◆ operator<=

bool operator<= ( const asset & a,
const asset & b )
friend

Definition at line 64 of file asset.hpp.

64{ return (a == b) || (a < b); }

◆ operator==

bool operator== ( const asset & a,
const asset & b )
friend

Definition at line 55 of file asset.hpp.

56 {
57 return std::tie(a.get_symbol(), a.amount) == std::tie(b.get_symbol(), b.amount);
58 }

◆ operator>

bool operator> ( const asset & a,
const asset & b )
friend

Definition at line 66 of file asset.hpp.

66{ return !(a <= b); }

◆ operator>=

bool operator>= ( const asset & a,
const asset & b )
friend

Definition at line 67 of file asset.hpp.

67{ return !(a < b); }

Member Data Documentation

◆ max_amount

int64_t sysio::chain::asset::max_amount = (1LL << 62) - 1
staticconstexpr

Definition at line 19 of file asset.hpp.


The documentation for this struct was generated from the following files: