BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
market_object.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
25 
26 #include <graphene/chain/types.hpp>
29 
30 #include <boost/multi_index/composite_key.hpp>
31 
32 namespace graphene { namespace chain {
33 
34 using namespace graphene::db;
35 
44 class limit_order_object : public abstract_object<limit_order_object>
45 {
46  public:
47  static constexpr uint8_t space_id = protocol_ids;
48  static constexpr uint8_t type_id = limit_order_object_type;
49 
51  account_id_type seller;
56 
57  pair<asset_id_type,asset_id_type> get_market()const
58  {
59  auto tmp = std::make_pair( sell_price.base.asset_id, sell_price.quote.asset_id );
60  if( tmp.first > tmp.second ) std::swap( tmp.first, tmp.second );
61  return tmp;
62  }
63 
64  asset amount_for_sale()const { return asset( for_sale, sell_price.base.asset_id ); }
65  asset amount_to_receive()const { return amount_for_sale() * sell_price; }
66  asset_id_type sell_asset_id()const { return sell_price.base.asset_id; }
67  asset_id_type receive_asset_id()const { return sell_price.quote.asset_id; }
68 };
69 
70 struct by_price;
71 struct by_expiration;
72 struct by_account;
73 struct by_account_price;
74 typedef multi_index_container<
76  indexed_by<
77  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
78  ordered_unique< tag<by_expiration>,
79  composite_key< limit_order_object,
80  member< limit_order_object, time_point_sec, &limit_order_object::expiration>,
81  member< object, object_id_type, &object::id>
82  >
83  >,
84  ordered_unique< tag<by_price>,
85  composite_key< limit_order_object,
86  member< limit_order_object, price, &limit_order_object::sell_price>,
87  member< object, object_id_type, &object::id>
88  >,
89  composite_key_compare< std::greater<price>, std::less<object_id_type> >
90  >,
91  // index used by APIs
92  ordered_unique< tag<by_account>,
93  composite_key< limit_order_object,
94  member<limit_order_object, account_id_type, &limit_order_object::seller>,
95  member<object, object_id_type, &object::id>
96  >
97  >,
98  // index used by APIs
99  ordered_unique< tag<by_account_price>,
100  composite_key< limit_order_object,
101  member<limit_order_object, account_id_type, &limit_order_object::seller>,
102  member<limit_order_object, price, &limit_order_object::sell_price>,
103  member<object, object_id_type, &object::id>
104  >,
105  composite_key_compare<std::less<account_id_type>, std::greater<price>, std::less<object_id_type>>
106  >
107  >
109 
111 
119 class call_order_object : public abstract_object<call_order_object>
120 {
121  public:
122  static constexpr uint8_t space_id = protocol_ids;
123  static constexpr uint8_t type_id = call_order_object_type;
124 
125  asset get_collateral()const { return asset( collateral, call_price.base.asset_id ); }
126  asset get_debt()const { return asset( debt, debt_type() ); }
127  asset amount_to_receive()const { return get_debt(); }
128  asset_id_type debt_type()const { return call_price.quote.asset_id; }
129  asset_id_type collateral_type()const { return call_price.base.asset_id; }
130  price collateralization()const { return get_collateral() / get_debt(); }
131 
132  account_id_type borrower;
136 
138 
139  pair<asset_id_type,asset_id_type> get_market()const
140  {
141  auto tmp = std::make_pair( call_price.base.asset_id, call_price.quote.asset_id );
142  if( tmp.first > tmp.second ) std::swap( tmp.first, tmp.second );
143  return tmp;
144  }
145 
156  share_type get_max_debt_to_cover( price match_price,
157  price feed_price,
158  const uint16_t maintenance_collateral_ratio,
159  const optional<price>& maintenance_collateralization = optional<price>() )const;
160 };
161 
168 class force_settlement_object : public abstract_object<force_settlement_object>
169 {
170  public:
171  static constexpr uint8_t space_id = protocol_ids;
172  static constexpr uint8_t type_id = force_settlement_object_type;
173 
174  account_id_type owner;
177 
178  asset_id_type settlement_asset_id()const
179  { return balance.asset_id; }
180 };
181 
189 class collateral_bid_object : public abstract_object<collateral_bid_object>
190 {
191  public:
192  static constexpr uint8_t space_id = implementation_ids;
193  static constexpr uint8_t type_id = impl_collateral_bid_object_type;
194 
195  asset get_additional_collateral()const { return inv_swan_price.base; }
196  asset get_debt_covered()const { return inv_swan_price.quote; }
197  asset_id_type debt_type()const { return inv_swan_price.quote.asset_id; }
198 
199  account_id_type bidder;
200  price inv_swan_price; // Collateral / Debt
201 };
202 
203 struct by_collateral;
204 struct by_account;
205 struct by_price;
206 typedef multi_index_container<
208  indexed_by<
209  ordered_unique< tag<by_id>,
210  member< object, object_id_type, &object::id > >,
211  ordered_unique< tag<by_price>,
212  composite_key< call_order_object,
213  member< call_order_object, price, &call_order_object::call_price>,
214  member< object, object_id_type, &object::id>
215  >,
216  composite_key_compare< std::less<price>, std::less<object_id_type> >
217  >,
218  ordered_unique< tag<by_account>,
219  composite_key< call_order_object,
220  member< call_order_object, account_id_type, &call_order_object::borrower >,
221  const_mem_fun< call_order_object, asset_id_type, &call_order_object::debt_type>
222  >
223  >,
224  ordered_unique< tag<by_collateral>,
225  composite_key< call_order_object,
226  const_mem_fun< call_order_object, price, &call_order_object::collateralization >,
227  member< object, object_id_type, &object::id >
228  >
229  >
230  >
232 
234 typedef multi_index_container<
236  indexed_by<
237  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
238  ordered_unique< tag<by_account>,
239  composite_key< force_settlement_object,
240  member<force_settlement_object, account_id_type, &force_settlement_object::owner>,
241  member< object, object_id_type, &object::id >
242  >
243  >,
244  ordered_unique< tag<by_expiration>,
245  composite_key< force_settlement_object,
246  const_mem_fun<force_settlement_object, asset_id_type, &force_settlement_object::settlement_asset_id>,
247  member<force_settlement_object, time_point_sec, &force_settlement_object::settlement_date>,
248  member< object, object_id_type, &object::id >
249  >
250  >
251  >
253 
254 typedef multi_index_container<
256  indexed_by<
257  ordered_unique< tag<by_id>,
258  member< object, object_id_type, &object::id > >,
259  ordered_unique< tag<by_account>,
260  composite_key< collateral_bid_object,
261  const_mem_fun< collateral_bid_object, asset_id_type, &collateral_bid_object::debt_type>,
262  member< collateral_bid_object, account_id_type, &collateral_bid_object::bidder>
263  >
264  >,
265  ordered_unique< tag<by_price>,
266  composite_key< collateral_bid_object,
267  const_mem_fun< collateral_bid_object, asset_id_type, &collateral_bid_object::debt_type>,
268  member< collateral_bid_object, price, &collateral_bid_object::inv_swan_price >,
269  member< object, object_id_type, &object::id >
270  >,
271  composite_key_compare< std::less<asset_id_type>, std::greater<price>, std::less<object_id_type> >
272  >
273  >
275 
279 
280 } } // graphene::chain
281 
286 
291 
asset_id_type receive_asset_id() const
optional< uint16_t > target_collateral_ratio
maximum CR to maintain when selling collateral on margin call
Definition: api.cpp:56
tracks debt and call price information
generic_index< collateral_bid_object, collateral_bid_object_multi_index_type > collateral_bid_index
#define MAP_OBJECT_ID_TO_TYPE(OBJECT)
Definition: object_id.hpp:98
multi_index_container< limit_order_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_expiration >, composite_key< limit_order_object, member< limit_order_object, time_point_sec,&limit_order_object::expiration >, member< object, object_id_type,&object::id > > >, ordered_unique< tag< by_price >, composite_key< limit_order_object, member< limit_order_object, price,&limit_order_object::sell_price >, member< object, object_id_type,&object::id > >, composite_key_compare< std::greater< price >, std::less< object_id_type > > >, ordered_unique< tag< by_account >, composite_key< limit_order_object, member< limit_order_object, account_id_type,&limit_order_object::seller >, member< object, object_id_type,&object::id > > >, ordered_unique< tag< by_account_price >, composite_key< limit_order_object, member< limit_order_object, account_id_type,&limit_order_object::seller >, member< limit_order_object, price,&limit_order_object::sell_price >, member< object, object_id_type,&object::id > >, composite_key_compare< std::less< account_id_type >, std::greater< price >, std::less< object_id_type > > > >> limit_order_multi_index_type
pair< asset_id_type, asset_id_type > get_market() const
multi_index_container< call_order_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_price >, composite_key< call_order_object, member< call_order_object, price,&call_order_object::call_price >, member< object, object_id_type,&object::id > >, composite_key_compare< std::less< price >, std::less< object_id_type > > >, ordered_unique< tag< by_account >, composite_key< call_order_object, member< call_order_object, account_id_type,&call_order_object::borrower >, const_mem_fun< call_order_object, asset_id_type,&call_order_object::debt_type > > >, ordered_unique< tag< by_collateral >, composite_key< call_order_object, const_mem_fun< call_order_object, price,&call_order_object::collateralization >, member< object, object_id_type,&object::id > > > >> call_order_multi_index_type
price call_price
Collateral / Debt.
generic_index< limit_order_object, limit_order_multi_index_type > limit_order_index
multi_index_container< force_settlement_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_account >, composite_key< force_settlement_object, member< force_settlement_object, account_id_type,&force_settlement_object::owner >, member< object, object_id_type,&object::id > > >, ordered_unique< tag< by_expiration >, composite_key< force_settlement_object, const_mem_fun< force_settlement_object, asset_id_type,&force_settlement_object::settlement_asset_id >, member< force_settlement_object, time_point_sec,&force_settlement_object::settlement_date >, member< object, object_id_type,&object::id > > > >> force_settlement_object_multi_index_type
share_type for_sale
asset id is sell_price.base.asset_id
asset_id_type collateral_type() const
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
FC_REFLECT_TYPENAME(fc::log_message)
share_type debt
call_price.quote.asset_id, access via get_debt
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:73
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:114
asset_id_type debt_type() const
pair< asset_id_type, asset_id_type > get_market() const
tracks bitassets scheduled for force settlement at some point in the future.
bids of collateral for debt after a black swan
Use the Curiously Recurring Template Pattern to automatically add the ability to clone, serialize, and move objects polymorphically.
Definition: object.hpp:86
multi_index_container< collateral_bid_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_account >, composite_key< collateral_bid_object, const_mem_fun< collateral_bid_object, asset_id_type,&collateral_bid_object::debt_type >, member< collateral_bid_object, account_id_type,&collateral_bid_object::bidder > > >, ordered_unique< tag< by_price >, composite_key< collateral_bid_object, const_mem_fun< collateral_bid_object, asset_id_type,&collateral_bid_object::debt_type >, member< collateral_bid_object, price,&collateral_bid_object::inv_swan_price >, member< object, object_id_type,&object::id > >, composite_key_compare< std::less< asset_id_type >, std::greater< price >, std::less< object_id_type > > > >> collateral_bid_object_multi_index_type
asset_id_type sell_asset_id() const
share_type collateral
call_price.base.asset_id, access via get_collateral
generic_index< force_settlement_object, force_settlement_object_multi_index_type > force_settlement_index
asset_id_type asset_id
Definition: asset.hpp:39
generic_index< call_order_object, call_order_multi_index_type > call_order_index
share_type deferred_fee
fee converted to CORE
asset deferred_paid_fee
originally paid fee
an offer to sell a amount of a asset at a specified exchange rate by a certain timeThis limit_order_o...