BitShares-Core  4.0.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  extern const int64_t scaled_precision_lut[];
30 
31  struct price;
32 
33  struct asset
34  {
35  asset( share_type a = 0, asset_id_type id = asset_id_type() )
36  :amount(a),asset_id(id){}
37 
39  asset_id_type asset_id;
40 
41  asset& operator += ( const asset& o )
42  {
43  FC_ASSERT( asset_id == o.asset_id );
44  amount += o.amount;
45  return *this;
46  }
47  asset& operator -= ( const asset& o )
48  {
49  FC_ASSERT( asset_id == o.asset_id );
50  amount -= o.amount;
51  return *this;
52  }
53  asset operator -()const { return asset( -amount, asset_id ); }
54 
55  friend bool operator == ( const asset& a, const asset& b )
56  {
57  return std::tie( a.asset_id, a.amount ) == std::tie( b.asset_id, b.amount );
58  }
59  friend bool operator < ( const asset& a, const asset& b )
60  {
61  FC_ASSERT( a.asset_id == b.asset_id );
62  return a.amount < b.amount;
63  }
64  friend inline bool operator <= ( const asset& a, const asset& b )
65  {
66  return !(b < a);
67  }
68 
69  friend inline bool operator != ( const asset& a, const asset& b )
70  {
71  return !(a == b);
72  }
73  friend inline bool operator > ( const asset& a, const asset& b )
74  {
75  return (b < a);
76  }
77  friend inline bool operator >= ( const asset& a, const asset& b )
78  {
79  return !(a < b);
80  }
81 
82  friend asset operator - ( const asset& a, const asset& b )
83  {
84  FC_ASSERT( a.asset_id == b.asset_id );
85  return asset( a.amount - b.amount, a.asset_id );
86  }
87  friend asset operator + ( const asset& a, const asset& b )
88  {
89  FC_ASSERT( a.asset_id == b.asset_id );
90  return asset( a.amount + b.amount, a.asset_id );
91  }
92 
93  static share_type scaled_precision( uint8_t precision )
94  {
95  FC_ASSERT( precision < 19 );
96  return scaled_precision_lut[ precision ];
97  }
98 
99  asset multiply_and_round_up( const price& p )const;
100  };
101 
114  struct price
115  {
116  explicit price(const asset& _base = asset(), const asset& _quote = asset())
117  : base(_base),quote(_quote){}
118 
121 
122  static price max(asset_id_type base, asset_id_type quote );
123  static price min(asset_id_type base, asset_id_type quote );
124 
125  static price call_price(const asset& debt, const asset& collateral, uint16_t collateral_ratio);
126 
128  static price unit_price(asset_id_type a = asset_id_type()) { return price(asset(1, a), asset(1, a)); }
129 
130  price max()const { return price::max( base.asset_id, quote.asset_id ); }
131  price min()const { return price::min( base.asset_id, quote.asset_id ); }
132 
133  double to_real()const { return double(base.amount.value)/double(quote.amount.value); }
134 
135  bool is_null()const;
136  void validate()const;
137  };
138 
139  price operator / ( const asset& base, const asset& quote );
140  inline price operator~( const price& p ) { return price{p.quote,p.base}; }
141 
142  bool operator < ( const price& a, const price& b );
143  bool operator == ( const price& a, const price& b );
144 
145  inline bool operator > ( const price& a, const price& b ) { return (b < a); }
146  inline bool operator <= ( const price& a, const price& b ) { return !(b < a); }
147  inline bool operator >= ( const price& a, const price& b ) { return !(a < b); }
148  inline bool operator != ( const price& a, const price& b ) { return !(a == b); }
149 
150  asset operator * ( const asset& a, const price& b );
151 
152  price operator * ( const price& p, const ratio_type& r );
153  price operator / ( const price& p, const ratio_type& r );
154 
155  inline price& operator *= ( price& p, const ratio_type& r )
156  { return p = p * r; }
157  inline price& operator /= ( price& p, const ratio_type& r )
158  { return p = p / r; }
159 
164  struct price_feed
165  {
180 
185 
188 
190  uint16_t maintenance_collateral_ratio = GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO;
191 
193  uint16_t maximum_short_squeeze_ratio = GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO;
194 
213  price max_short_squeeze_price()const;
225  price max_short_squeeze_price_before_hf_1270()const;
226 
263  price margin_call_order_price(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  };
303 
304 } }
305 
307 FC_REFLECT( graphene::protocol::price, (base)(quote) )
308 
310  (settlement_price)(maintenance_collateral_ratio)(maximum_short_squeeze_ratio)(core_exchange_rate) )
311 
uint16_t maximum_short_squeeze_ratio
Definition: asset.hpp:193
price & operator/=(price &p, const ratio_type &r)
Definition: asset.hpp:157
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
price operator~(const price &p)
Definition: asset.hpp:140
asset operator*(const asset &a, const price &b)
Multiply and round down.
Definition: asset.cpp:59
boost::rational< int32_t > ratio_type
Definition: types.hpp:133
Definition: api.cpp:56
friend asset operator+(const asset &a, const asset &b)
Definition: asset.hpp:87
price & operator*=(price &p, const ratio_type &r)
Definition: asset.hpp:155
#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:98
asset & operator+=(const asset &o)
Definition: asset.hpp:41
friend bool operator>(const asset &a, const asset &b)
Definition: asset.hpp:73
price min() const
Definition: asset.hpp:131
friend bool operator<(const asset &a, const asset &b)
Definition: asset.hpp:59
asset operator-() const
Definition: asset.hpp:53
price(const asset &_base=asset(), const asset &_quote=asset())
Definition: asset.hpp:116
uint16_t maintenance_collateral_ratio
Definition: asset.hpp:190
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:73
friend bool operator!=(const asset &a, const asset &b)
Definition: asset.hpp:69
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:114
friend bool operator>=(const asset &a, const asset &b)
Definition: asset.hpp:77
friend bool operator<=(const asset &a, const asset &b)
Definition: asset.hpp:64
#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:55
const int64_t scaled_precision_lut[19]
Definition: asset.cpp:342
#define GRAPHENE_DEFAULT_MAX_SHORT_SQUEEZE_RATIO
Definition: config.hpp:117
asset(share_type a=0, asset_id_type id=asset_id_type())
Definition: asset.hpp:35
defines market parameters for margin positions
Definition: asset.hpp:164
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:128
asset_id_type asset_id
Definition: asset.hpp:39
bool margin_call_params_equal(const price_feed &b) const
Definition: asset.hpp:291
static share_type scaled_precision(uint8_t precision)
Definition: asset.hpp:93
double to_real() const
Definition: asset.hpp:133
asset & operator-=(const asset &o)
Definition: asset.hpp:47
price max() const
Definition: asset.hpp:130
T value
Definition: safe.hpp:22
asset multiply_and_round_up(const price &p) const
Multiply and round up.
Definition: asset.cpp:78
price core_exchange_rate
Price at which automatically exchanging this asset for CORE from fee pool occurs (used for paying fee...
Definition: asset.hpp:187