BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface 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 
186  uint16_t maintenance_collateral_ratio = GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO;
187 
189  uint16_t maximum_short_squeeze_ratio = GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO;
190 
209  price max_short_squeeze_price()const;
221  price max_short_squeeze_price_before_hf_1270()const;
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 
287  price maintenance_collateralization()const;
288 
291  bool margin_call_params_equal( const price_feed& b ) const
292  {
293  if( this == &b )
294  return true;
295  return std::tie( settlement_price, maintenance_collateral_ratio, maximum_short_squeeze_ratio ) ==
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 
310 FC_REFLECT( graphene::protocol::price, (base)(quote) )
311 
313  (settlement_price)(maintenance_collateral_ratio)(maximum_short_squeeze_ratio)(core_exchange_rate) )
314 
uint16_t maximum_short_squeeze_ratio
Definition: asset.hpp:189
price & operator/=(price &p, const ratio_type &r)
Definition: asset.hpp:153
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
price operator~(const price &p)
Definition: asset.hpp:136
asset operator*(const asset &a, const price &b)
Multiply and round down.
Definition: asset.cpp:58
asset operator-() const
Definition: asset.hpp:51
boost::rational< int32_t > ratio_type
Definition: types.hpp:150
price min() const
Definition: asset.hpp:125
Definition: api.cpp:48
friend asset operator+(const asset &a, const asset &b)
Definition: asset.hpp:85
price & operator*=(price &p, const ratio_type &r)
Definition: asset.hpp:151
#define GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO
Call when collateral only pays off 175% the debt.
Definition: config.hpp:116
price operator/(const asset &base, const asset &quote)
Definition: asset.cpp:100
asset & operator+=(const asset &o)
Definition: asset.hpp:39
friend bool operator>(const asset &a, const asset &b)
Definition: asset.hpp:71
asset multiply_and_round_up(const price &p) const
Multiply and round up.
Definition: asset.cpp:77
price max() const
Definition: asset.hpp:124
friend bool operator<(const asset &a, const asset &b)
Definition: asset.hpp:57
price(const asset &_base=asset(), const asset &_quote=asset())
Definition: asset.hpp:110
static share_type scaled_precision(uint8_t precision)
Definition: asset.cpp:363
uint16_t maintenance_collateral_ratio
Definition: asset.hpp:186
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
friend bool operator!=(const asset &a, const asset &b)
Definition: asset.hpp:67
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
friend bool operator>=(const asset &a, const asset &b)
Definition: asset.hpp:75
friend bool operator<=(const asset &a, const asset &b)
Definition: asset.hpp:62
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
friend bool operator==(const asset &a, const asset &b)
Definition: asset.hpp:53
#define GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO
Stop calling when collateral only pays off 150% of the debt.
Definition: config.hpp:117
asset(share_type a=0, asset_id_type id=asset_id_type())
Definition: asset.hpp:33
double to_real() const
Definition: asset.hpp:127
defines market parameters for margin positions
Definition: asset.hpp:160
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
asset_id_type asset_id
Definition: asset.hpp:37
asset & operator-=(const asset &o)
Definition: asset.hpp:45
T value
Definition: safe.hpp:22
bool margin_call_params_equal(const price_feed &b) const
Definition: asset.hpp:291
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