BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
asset.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
26 
27 namespace graphene { namespace protocol {
28 
29  struct price;
30 
31  struct asset
32  {
33  asset( share_type a = 0, asset_id_type id = asset_id_type() )
34  :amount(a),asset_id(id){}
35 
37  asset_id_type asset_id;
38 
39  asset& operator += ( const asset& o )
40  {
41  FC_ASSERT( asset_id == o.asset_id );
42  amount += o.amount;
43  return *this;
44  }
45  asset& operator -= ( const asset& o )
46  {
47  FC_ASSERT( asset_id == o.asset_id );
48  amount -= o.amount;
49  return *this;
50  }
51  asset operator -()const { return asset( -amount, asset_id ); }
52 
53  friend bool operator == ( const asset& a, const asset& b )
54  {
55  return std::tie( a.asset_id, a.amount ) == std::tie( b.asset_id, b.amount );
56  }
57  friend bool operator < ( const asset& a, const asset& b )
58  {
59  FC_ASSERT( a.asset_id == b.asset_id );
60  return a.amount < b.amount;
61  }
62  friend inline bool operator <= ( const asset& a, const asset& b )
63  {
64  return !(b < a);
65  }
66 
67  friend inline bool operator != ( const asset& a, const asset& b )
68  {
69  return !(a == b);
70  }
71  friend inline bool operator > ( const asset& a, const asset& b )
72  {
73  return (b < a);
74  }
75  friend inline bool operator >= ( const asset& a, const asset& b )
76  {
77  return !(a < b);
78  }
79 
80  friend asset operator - ( const asset& a, const asset& b )
81  {
82  FC_ASSERT( a.asset_id == b.asset_id );
83  return asset( a.amount - b.amount, a.asset_id );
84  }
85  friend asset operator + ( const asset& a, const asset& b )
86  {
87  FC_ASSERT( a.asset_id == b.asset_id );
88  return asset( a.amount + b.amount, a.asset_id );
89  }
90 
91  static share_type scaled_precision( uint8_t precision );
92 
93  asset multiply_and_round_up( const price& p )const;
94  };
95 
108  struct price
109  {
110  explicit price(const asset& _base = asset(), const asset& _quote = asset())
111  : base(_base),quote(_quote){}
112 
115 
116  static price max(asset_id_type base, asset_id_type quote );
117  static price min(asset_id_type base, asset_id_type quote );
118 
119  static price call_price(const asset& debt, const asset& collateral, uint16_t collateral_ratio);
120 
122  static price unit_price(asset_id_type a = asset_id_type()) { return price(asset(1, a), asset(1, a)); }
123 
124  price max()const { return price::max( base.asset_id, quote.asset_id ); }
125  price min()const { return price::min( base.asset_id, quote.asset_id ); }
126 
127  double to_real()const { return double(base.amount.value)/double(quote.amount.value); }
128 
129  bool is_null()const;
132  void validate( bool check_upper_bound = false )const;
133  };
134 
135  price operator / ( const asset& base, const asset& quote );
136  inline price operator~( const price& p ) { return price{p.quote,p.base}; }
137 
138  bool operator < ( const price& a, const price& b );
139  bool operator == ( const price& a, const price& b );
140 
141  inline bool operator > ( const price& a, const price& b ) { return (b < a); }
142  inline bool operator <= ( const price& a, const price& b ) { return !(b < a); }
143  inline bool operator >= ( const price& a, const price& b ) { return !(a < b); }
144  inline bool operator != ( const price& a, const price& b ) { return !(a == b); }
145 
146  asset operator * ( const asset& a, const price& b );
147 
148  price operator * ( const price& p, const ratio_type& r );
149  price operator / ( const price& p, const ratio_type& r );
150 
151  inline price& operator *= ( price& p, const ratio_type& r )
152  { p = p * r; return p; }
153  inline price& operator /= ( price& p, const ratio_type& r )
154  { p = p / r; return p; }
155 
160  struct price_feed
161  {
176 
181 
184 
187 
190 
222 
259  price margin_call_order_price(const fc::optional<uint16_t>& margin_call_fee_ratio)const;
260 
263  ratio_type margin_call_order_ratio( const fc::optional<uint16_t>& margin_call_fee_ratio )const;
264 
282  ratio_type margin_call_pays_ratio(const fc::optional<uint16_t>& margin_call_fee_ratio)const;
283 
288 
291  bool margin_call_params_equal( const price_feed& b ) const
292  {
293  if( this == &b )
294  return true;
297  }
299 
300  void validate() const;
301  bool is_for( asset_id_type asset_id ) const;
302  private:
304  uint16_t get_margin_call_price_numerator(const fc::optional<uint16_t>& margin_call_fee_ratio)const;
305  };
306 
307 } }
308 
309 FC_REFLECT( graphene::protocol::asset, (amount)(asset_id) )
310 FC_REFLECT( graphene::protocol::price, (base)(quote) )
311 
313  (settlement_price)(maintenance_collateral_ratio)(maximum_short_squeeze_ratio)(core_exchange_rate) )
314 
graphene::protocol::operator<
bool operator<(const price &a, const price &b)
Definition: asset.cpp:45
graphene::protocol::asset::operator<
friend bool operator<(const asset &a, const asset &b)
Definition: asset.hpp:57
graphene::protocol::asset::operator>
friend bool operator>(const asset &a, const asset &b)
Definition: asset.hpp:71
GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO
#define GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO
Stop calling when collateral only pays off 150% of the debt.
Definition: config.hpp:117
graphene::protocol::asset::operator-=
asset & operator-=(const asset &o)
Definition: asset.hpp:45
graphene::protocol::price_feed::core_exchange_rate
price core_exchange_rate
Price at which automatically exchanging this asset for CORE from fee pool occurs (used for paying fee...
Definition: asset.hpp:183
graphene::protocol::price
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
graphene::protocol::asset::scaled_precision
static share_type scaled_precision(uint8_t precision)
Definition: asset.cpp:363
graphene::protocol::price_feed::margin_call_params_equal
bool margin_call_params_equal(const price_feed &b) const
Definition: asset.hpp:291
graphene::protocol::asset::operator+
friend asset operator+(const asset &a, const asset &b)
Definition: asset.hpp:85
graphene::protocol::price_feed::maintenance_collateralization
price maintenance_collateralization() const
Definition: asset.cpp:342
graphene::protocol::operator/
price operator/(const asset &base, const asset &quote)
Definition: asset.cpp:100
graphene::protocol::operator<=
bool operator<=(const price &a, const price &b)
Definition: asset.hpp:142
graphene::protocol::price_feed::settlement_price
price settlement_price
Definition: asset.hpp:180
graphene::protocol::asset::operator!=
friend bool operator!=(const asset &a, const asset &b)
Definition: asset.hpp:67
graphene::protocol::asset::asset
asset(share_type a=0, asset_id_type id=asset_id_type())
Definition: asset.hpp:33
graphene::protocol::price_feed::max_short_squeeze_price
price max_short_squeeze_price() const
Definition: asset.cpp:300
graphene::protocol::price_feed::validate
void validate() const
Definition: asset.cpp:251
graphene::protocol::price::unit_price
static price unit_price(asset_id_type a=asset_id_type())
The unit price for an asset type A is defined to be a price such that for any asset m,...
Definition: asset.hpp:122
types.hpp
graphene::protocol::asset::operator<=
friend bool operator<=(const asset &a, const asset &b)
Definition: asset.hpp:62
graphene::protocol::asset::operator>=
friend bool operator>=(const asset &a, const asset &b)
Definition: asset.hpp:75
graphene::protocol::price::call_price
static price call_price(const asset &debt, const asset &collateral, uint16_t collateral_ratio)
Definition: asset.cpp:216
graphene::protocol::asset::multiply_and_round_up
asset multiply_and_round_up(const price &p) const
Multiply and round up.
Definition: asset.cpp:77
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO
#define GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO
Call when collateral only pays off 175% the debt.
Definition: config.hpp:116
graphene::protocol::price_feed::maximum_short_squeeze_ratio
uint16_t maximum_short_squeeze_ratio
Definition: asset.hpp:189
graphene::protocol::price::is_null
bool is_null() const
Definition: asset.cpp:229
graphene::protocol::operator~
price operator~(const price &p)
Definition: asset.hpp:136
graphene::protocol::price_feed
defines market parameters for margin positions
Definition: asset.hpp:160
graphene::protocol::operator==
bool operator==(const price &a, const price &b)
Definition: asset.cpp:34
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
graphene::protocol::price::to_real
double to_real() const
Definition: asset.hpp:127
graphene::protocol::operator>=
bool operator>=(const price &a, const price &b)
Definition: asset.hpp:143
graphene::protocol::asset::operator+=
asset & operator+=(const asset &o)
Definition: asset.hpp:39
graphene::protocol::operator>
bool operator>(const price &a, const price &b)
Definition: asset.hpp:141
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::protocol::price::validate
void validate(bool check_upper_bound=false) const
Check if the object is valid.
Definition: asset.cpp:235
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
graphene::protocol::price_feed::maintenance_collateral_ratio
uint16_t maintenance_collateral_ratio
Definition: asset.hpp:186
graphene::protocol::price::max
price max() const
Definition: asset.hpp:124
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
graphene::protocol::price::base
asset base
Definition: asset.hpp:113
graphene::protocol::price_feed::margin_call_pays_ratio
ratio_type margin_call_pays_ratio(const fc::optional< uint16_t > &margin_call_fee_ratio) const
Definition: asset.cpp:334
graphene::protocol::price::min
price min() const
Definition: asset.hpp:125
graphene::protocol::price_feed::max_short_squeeze_price_before_hf_1270
price max_short_squeeze_price_before_hf_1270() const
Definition: asset.cpp:282
graphene::protocol::asset::operator-
asset operator-() const
Definition: asset.hpp:51
fc::optional< uint16_t >
graphene::protocol::asset::operator==
friend bool operator==(const asset &a, const asset &b)
Definition: asset.hpp:53
graphene::protocol::price::price
price(const asset &_base=asset(), const asset &_quote=asset())
Definition: asset.hpp:110
graphene::protocol::asset
Definition: asset.hpp:31
graphene::protocol::price_feed::is_for
bool is_for(asset_id_type asset_id) const
Definition: asset.cpp:266
fc::safe::value
T value
Definition: safe.hpp:28
graphene::protocol::operator*=
price & operator*=(price &p, const ratio_type &r)
Definition: asset.hpp:151
graphene::protocol::price_feed::margin_call_order_ratio
ratio_type margin_call_order_ratio(const fc::optional< uint16_t > &margin_call_fee_ratio) const
Definition: asset.cpp:326
graphene::protocol::operator/=
price & operator/=(price &p, const ratio_type &r)
Definition: asset.hpp:153
graphene::protocol::ratio_type
boost::rational< int32_t > ratio_type
Definition: types.hpp:150
graphene::protocol::price::quote
asset quote
Definition: asset.hpp:114
graphene
Definition: api.cpp:48
graphene::protocol::operator!=
bool operator!=(const address &a, const address &b)
Definition: address.hpp:61
graphene::protocol::price_feed::margin_call_order_price
price margin_call_order_price(const fc::optional< uint16_t > &margin_call_fee_ratio) const
Definition: asset.cpp:308
graphene::protocol::operator*
asset operator*(const asset &a, const price &b)
Multiply and round down.
Definition: asset.cpp:58
fc::safe
Definition: safe.hpp:26