BitShares-Core  6.1.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, protocol_ids, limit_order_object_type>
45 {
46  public:
48  account_id_type seller;
53  bool is_settled_debt = false;
54 
55  pair<asset_id_type,asset_id_type> get_market()const
56  {
57  auto tmp = std::make_pair( sell_price.base.asset_id, sell_price.quote.asset_id );
58  if( tmp.first > tmp.second ) std::swap( tmp.first, tmp.second );
59  return tmp;
60  }
61 
62  asset amount_for_sale()const { return asset( for_sale, sell_price.base.asset_id ); }
63  asset amount_to_receive()const { return amount_for_sale() * sell_price; }
64  asset_id_type sell_asset_id()const { return sell_price.base.asset_id; }
65  asset_id_type receive_asset_id()const { return sell_price.quote.asset_id; }
66 };
67 
68 struct by_price;
69 struct by_expiration;
70 struct by_account;
71 struct by_account_price;
72 struct by_is_settled_debt;
73 typedef multi_index_container<
75  indexed_by<
76  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
77  ordered_unique< tag<by_expiration>,
78  composite_key< limit_order_object,
79  member< limit_order_object, time_point_sec, &limit_order_object::expiration>,
80  member< object, object_id_type, &object::id>
81  >
82  >,
83  ordered_unique< tag<by_price>,
84  composite_key< limit_order_object,
85  member< limit_order_object, price, &limit_order_object::sell_price>,
86  member< object, object_id_type, &object::id>
87  >,
88  composite_key_compare< std::greater<price>, std::less<object_id_type> >
89  >,
90  ordered_unique< tag<by_is_settled_debt>,
91  composite_key< limit_order_object,
92  member< limit_order_object, bool, &limit_order_object::is_settled_debt >,
93  const_mem_fun< limit_order_object, asset_id_type, &limit_order_object::receive_asset_id >,
94  member< object, object_id_type, &object::id>
95  >
96  >,
97  // index used by APIs
98  ordered_unique< tag<by_account>,
99  composite_key< limit_order_object,
100  member<limit_order_object, account_id_type, &limit_order_object::seller>,
101  member<object, object_id_type, &object::id>
102  >
103  >,
104  // index used by APIs
105  ordered_unique< tag<by_account_price>,
106  composite_key< limit_order_object,
107  member<limit_order_object, account_id_type, &limit_order_object::seller>,
108  member<limit_order_object, price, &limit_order_object::sell_price>,
109  member<object, object_id_type, &object::id>
110  >,
111  composite_key_compare<std::less<account_id_type>, std::greater<price>, std::less<object_id_type>>
112  >
113  >
115 
117 
125 class call_order_object : public abstract_object<call_order_object, protocol_ids, call_order_object_type>
126 {
127  public:
128  asset get_collateral()const { return asset( collateral, call_price.base.asset_id ); }
129  asset get_debt()const { return asset( debt, debt_type() ); }
130  asset amount_to_receive()const { return get_debt(); }
131  asset_id_type debt_type()const { return call_price.quote.asset_id; }
132  asset_id_type collateral_type()const { return call_price.base.asset_id; }
133  price collateralization()const { return get_collateral() / get_debt(); }
134 
135  account_id_type borrower;
139 
141 
142  pair<asset_id_type,asset_id_type> get_market()const
143  {
144  auto tmp = std::make_pair( call_price.base.asset_id, call_price.quote.asset_id );
145  if( tmp.first > tmp.second ) std::swap( tmp.first, tmp.second );
146  return tmp;
147  }
148 
159  share_type get_max_debt_to_cover( price match_price,
160  price feed_price,
161  const uint16_t maintenance_collateral_ratio,
162  const optional<price>& maintenance_collateralization = optional<price>()
163  )const;
164 };
165 
172 class force_settlement_object : public abstract_object<force_settlement_object,
173  protocol_ids, force_settlement_object_type>
174 {
175  public:
176  account_id_type owner;
179 
180  asset_id_type settlement_asset_id()const
181  { return balance.asset_id; }
182 };
183 
191 class collateral_bid_object : public abstract_object<collateral_bid_object,
192  implementation_ids, impl_collateral_bid_object_type>
193 {
194  public:
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 
optional< uint16_t > target_collateral_ratio
maximum CR to maintain when selling collateral on margin call
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_is_settled_debt >, composite_key< limit_order_object, member< limit_order_object, bool, &limit_order_object::is_settled_debt >, const_mem_fun< limit_order_object, asset_id_type, &limit_order_object::receive_asset_id >, member< object, object_id_type, &object::id > > >, 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
Definition: api.cpp:48
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:93
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
asset_id_type collateral_type() const
asset_id_type sell_asset_id() const
share_type for_sale
asset id is sell_price.base.asset_id
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:85
asset_id_type receive_asset_id() const
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
tracks bitassets scheduled for force settlement at some point in the future.
bids of collateral for debt after a black swan
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
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:37
pair< asset_id_type, asset_id_type > get_market() const
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 an amount of an asset at a specified exchange rate by a certain timeThe objects are ...