BitShares-Core  5.1.0
BitShares blockchain implementation and command-line interface software
market_history_plugin.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/app/plugin.hpp>
29 
30 #include <fc/thread/future.hpp>
31 #include <fc/uint128.hpp>
32 
33 #include <boost/multi_index/composite_key.hpp>
34 
35 namespace graphene { namespace market_history {
36 using namespace chain;
37 
38 //
39 // Plugins should #define their SPACE_ID's so plugins with
40 // conflicting SPACE_ID assignments can be compiled into the
41 // same binary (by simply re-assigning some of the conflicting #defined
42 // SPACE_ID's in a build script).
43 //
44 // Assignment of SPACE_ID's cannot be done at run-time because
45 // various template automagic depends on them being known at compile
46 // time.
47 //
48 #ifndef MARKET_HISTORY_SPACE_ID
49 #define MARKET_HISTORY_SPACE_ID 5
50 #endif
51 
53 {
61 };
62 
63 struct bucket_key
64 {
65  bucket_key( asset_id_type a, asset_id_type b, uint32_t s, fc::time_point_sec o )
66  :base(a),quote(b),seconds(s),open(o){}
68 
69  asset_id_type base;
70  asset_id_type quote;
71  uint32_t seconds = 0;
73 
74  friend bool operator < ( const bucket_key& a, const bucket_key& b )
75  {
76  return std::tie( a.base, a.quote, a.seconds, a.open ) < std::tie( b.base, b.quote, b.seconds, b.open );
77  }
78  friend bool operator == ( const bucket_key& a, const bucket_key& b )
79  {
80  return std::tie( a.base, a.quote, b.seconds, a.open ) == std::tie( b.base, b.quote, b.seconds, b.open );
81  }
82 };
83 
84 struct bucket_object : public abstract_object<bucket_object>
85 {
86  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
87  static constexpr uint8_t type_id = bucket_object_type;
88 
89  price high()const { return asset( high_base, key.base ) / asset( high_quote, key.quote ); }
90  price low()const { return asset( low_base, key.base ) / asset( low_quote, key.quote ); }
91 
103 };
104 
105 struct history_key {
106  asset_id_type base;
107  asset_id_type quote;
108  int64_t sequence = 0;
109 
110  friend bool operator < ( const history_key& a, const history_key& b ) {
111  return std::tie( a.base, a.quote, a.sequence ) < std::tie( b.base, b.quote, b.sequence );
112  }
113  friend bool operator == ( const history_key& a, const history_key& b ) {
114  return std::tie( a.base, a.quote, a.sequence ) == std::tie( b.base, b.quote, b.sequence );
115  }
116 };
117 struct order_history_object : public abstract_object<order_history_object>
118 {
119  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
120  static constexpr uint8_t type_id = order_history_object_type;
121 
125 };
127 {
128  typedef asset_id_type result_type;
129  result_type operator()(const order_history_object& o)const { return o.key.base; }
130 };
132 {
133  typedef asset_id_type result_type;
134  result_type operator()(const order_history_object& o)const { return o.key.quote; }
135 };
137 {
138  typedef int64_t result_type;
139  result_type operator()(const order_history_object& o)const { return o.key.sequence; }
140 };
141 
142 struct market_ticker_object : public abstract_object<market_ticker_object>
143 {
144  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
145  static constexpr uint8_t type_id = market_ticker_object_type;
146 
147  asset_id_type base;
148  asset_id_type quote;
153  fc::uint128_t base_volume;
154  fc::uint128_t quote_volume;
155 };
156 
157 struct market_ticker_meta_object : public abstract_object<market_ticker_meta_object>
158 {
159  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
160  static constexpr uint8_t type_id = market_ticker_meta_object_type;
161 
163  bool skip_min_order_his_id = false;
164 };
165 
166 struct by_key;
167 typedef multi_index_container<
169  indexed_by<
170  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
171  ordered_unique< tag<by_key>, member< bucket_object, bucket_key, &bucket_object::key > >
172  >
174 
175 struct by_market_time;
176 typedef multi_index_container<
178  indexed_by<
179  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
180  ordered_unique< tag<by_key>, member< order_history_object, history_key, &order_history_object::key > >,
181  ordered_unique<
182  tag<by_market_time>,
183  composite_key<
184  order_history_object,
187  member<order_history_object, time_point_sec, &order_history_object::time>,
189  >,
190  composite_key_compare<
191  std::less< asset_id_type >,
192  std::less< asset_id_type >,
193  std::greater< time_point_sec >,
194  std::less< int64_t >
195  >
196  >
197  >
199 
200 struct by_market;
201 struct by_volume;
202 typedef multi_index_container<
204  indexed_by<
205  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
206  ordered_non_unique< tag<by_volume>,
207  member< market_ticker_object, fc::uint128_t, &market_ticker_object::base_volume > >,
208  ordered_unique<
209  tag<by_market>,
210  composite_key<
211  market_ticker_object,
212  member<market_ticker_object, asset_id_type, &market_ticker_object::base>,
213  member<market_ticker_object, asset_id_type, &market_ticker_object::quote>
214  >
215  >
216  >
218 
222 
223 
225 struct liquidity_pool_history_object : public abstract_object<liquidity_pool_history_object>
226 {
227  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
228  static constexpr uint8_t type_id = liquidity_pool_history_object_type;
229 
230  liquidity_pool_id_type pool;
231  uint64_t sequence = 0;
233  int64_t op_type;
235 };
236 
237 struct by_pool_seq;
238 struct by_pool_time;
239 struct by_pool_op_type_seq;
240 struct by_pool_op_type_time;
241 
242 typedef multi_index_container<
244  indexed_by<
245  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
246  ordered_unique< tag<by_pool_seq>,
247  composite_key< liquidity_pool_history_object,
248  member<liquidity_pool_history_object, liquidity_pool_id_type, &liquidity_pool_history_object::pool>,
249  member<liquidity_pool_history_object, uint64_t, &liquidity_pool_history_object::sequence>
250  >,
251  composite_key_compare<
252  std::less< liquidity_pool_id_type >,
253  std::greater< uint64_t >
254  >
255  >,
256  ordered_unique< tag<by_pool_time>,
257  composite_key< liquidity_pool_history_object,
258  member<liquidity_pool_history_object, liquidity_pool_id_type, &liquidity_pool_history_object::pool>,
259  member<liquidity_pool_history_object, time_point_sec, &liquidity_pool_history_object::time>,
260  member<liquidity_pool_history_object, uint64_t, &liquidity_pool_history_object::sequence>
261  >,
262  composite_key_compare<
263  std::less< liquidity_pool_id_type >,
264  std::greater< time_point_sec >,
265  std::greater< uint64_t >
266  >
267  >,
268  ordered_unique< tag<by_pool_op_type_seq>,
269  composite_key< liquidity_pool_history_object,
270  member<liquidity_pool_history_object, liquidity_pool_id_type, &liquidity_pool_history_object::pool>,
271  member<liquidity_pool_history_object, int64_t, &liquidity_pool_history_object::op_type>,
272  member<liquidity_pool_history_object, uint64_t, &liquidity_pool_history_object::sequence>
273  >,
274  composite_key_compare<
275  std::less< liquidity_pool_id_type >,
276  std::less< int64_t >,
277  std::greater< uint64_t >
278  >
279  >,
280  ordered_unique< tag<by_pool_op_type_time>,
281  composite_key< liquidity_pool_history_object,
282  member<liquidity_pool_history_object, liquidity_pool_id_type, &liquidity_pool_history_object::pool>,
283  member<liquidity_pool_history_object, int64_t, &liquidity_pool_history_object::op_type>,
284  member<liquidity_pool_history_object, time_point_sec, &liquidity_pool_history_object::time>,
285  member<liquidity_pool_history_object, uint64_t, &liquidity_pool_history_object::sequence>
286  >,
287  composite_key_compare<
288  std::less< liquidity_pool_id_type >,
289  std::less< int64_t >,
290  std::greater< time_point_sec >,
291  std::greater< uint64_t >
292  >
293  >
294  >
296 
297 typedef generic_index< liquidity_pool_history_object,
298  liquidity_pool_history_multi_index_type > liquidity_pool_history_index;
299 
300 
302 struct liquidity_pool_ticker_meta_object : public abstract_object<liquidity_pool_ticker_meta_object>
303 {
304  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
305  static constexpr uint8_t type_id = liquidity_pool_ticker_meta_object_type;
306 
308  bool skip_min_lp_his_id = false;
309 };
310 
312 
314 struct liquidity_pool_ticker_object : public abstract_object<liquidity_pool_ticker_object>
315 {
316  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
317  static constexpr uint8_t type_id = liquidity_pool_ticker_object_type;
318 
319  uint32_t _24h_deposit_count = 0;
320  fc::uint128_t _24h_deposit_amount_a = 0;
321  fc::uint128_t _24h_deposit_amount_b = 0;
322  fc::uint128_t _24h_deposit_share_amount = 0;
323  uint32_t _24h_withdrawal_count = 0;
324  fc::uint128_t _24h_withdrawal_amount_a = 0;
325  fc::uint128_t _24h_withdrawal_amount_b = 0;
326  fc::uint128_t _24h_withdrawal_share_amount = 0;
327  fc::uint128_t _24h_withdrawal_fee_a = 0;
328  fc::uint128_t _24h_withdrawal_fee_b = 0;
329  uint32_t _24h_exchange_a2b_count = 0;
330  fc::uint128_t _24h_exchange_a2b_amount_a = 0;
331  fc::uint128_t _24h_exchange_a2b_amount_b = 0;
332  uint32_t _24h_exchange_b2a_count = 0;
333  fc::uint128_t _24h_exchange_b2a_amount_a = 0;
334  fc::uint128_t _24h_exchange_b2a_amount_b = 0;
335  fc::uint128_t _24h_exchange_fee_a = 0;
336  fc::uint128_t _24h_exchange_fee_b = 0;
339  uint64_t total_deposit_count = 0;
340  fc::uint128_t total_deposit_amount_a = 0;
341  fc::uint128_t total_deposit_amount_b = 0;
342  fc::uint128_t total_deposit_share_amount = 0;
343  uint64_t total_withdrawal_count = 0;
344  fc::uint128_t total_withdrawal_amount_a = 0;
345  fc::uint128_t total_withdrawal_amount_b = 0;
346  fc::uint128_t total_withdrawal_share_amount = 0;
347  fc::uint128_t total_withdrawal_fee_a = 0;
348  fc::uint128_t total_withdrawal_fee_b = 0;
349  uint64_t total_exchange_a2b_count = 0;
350  fc::uint128_t total_exchange_a2b_amount_a = 0;
351  fc::uint128_t total_exchange_a2b_amount_b = 0;
352  uint64_t total_exchange_b2a_count = 0;
353  fc::uint128_t total_exchange_b2a_amount_a = 0;
354  fc::uint128_t total_exchange_b2a_amount_b = 0;
355  fc::uint128_t total_exchange_fee_a = 0;
356  fc::uint128_t total_exchange_fee_b = 0;
357 };
358 
359 typedef multi_index_container<
361  indexed_by<
362  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >
363  >
365 
366 typedef generic_index< liquidity_pool_ticker_object,
368 
369 
370 namespace detail
371 {
373 }
374 
381 {
382  public:
384  virtual ~market_history_plugin();
385 
386  std::string plugin_name()const override;
387  virtual void plugin_set_program_options(
388  boost::program_options::options_description& cli,
389  boost::program_options::options_description& cfg) override;
390  virtual void plugin_initialize(
391  const boost::program_options::variables_map& options) override;
392  virtual void plugin_startup() override;
393 
394  uint32_t max_history()const;
395  const flat_set<uint32_t>& tracked_buckets()const;
396  uint32_t max_order_his_records_per_market()const;
397  uint32_t max_order_his_seconds_per_market()const;
398 
399  private:
401  std::unique_ptr<detail::market_history_plugin_impl> my;
402 };
403 
404 } } //graphene::market_history
405 
406 FC_REFLECT( graphene::market_history::history_key, (base)(quote)(sequence) )
407 FC_REFLECT_DERIVED( graphene::market_history::order_history_object, (graphene::db::object), (key)(time)(op) )
408 FC_REFLECT( graphene::market_history::bucket_key, (base)(quote)(seconds)(open) )
409 FC_REFLECT_DERIVED( graphene::market_history::bucket_object, (graphene::db::object),
410  (key)
411  (high_base)(high_quote)
412  (low_base)(low_quote)
413  (open_base)(open_quote)
414  (close_base)(close_quote)
415  (base_volume)(quote_volume) )
416 FC_REFLECT_DERIVED( graphene::market_history::market_ticker_object, (graphene::db::object),
417  (base)(quote)
418  (last_day_base)(last_day_quote)
419  (latest_base)(latest_quote)
420  (base_volume)(quote_volume) )
421 FC_REFLECT_DERIVED( graphene::market_history::market_ticker_meta_object, (graphene::db::object),
422  (rolling_min_order_his_id)(skip_min_order_his_id) )
423 FC_REFLECT_DERIVED( graphene::market_history::liquidity_pool_history_object, (graphene::db::object),
424  (pool)(sequence)(time)(op_type)(op) )
426  (rolling_min_lp_his_id)(skip_min_lp_his_id) )
427 FC_REFLECT_DERIVED( graphene::market_history::liquidity_pool_ticker_object, (graphene::db::object),
428  (_24h_deposit_count)
429  (_24h_deposit_amount_a)
430  (_24h_deposit_amount_b)
431  (_24h_deposit_share_amount)
432  (_24h_withdrawal_count)
433  (_24h_withdrawal_amount_a)
434  (_24h_withdrawal_amount_b)
435  (_24h_withdrawal_share_amount)
436  (_24h_withdrawal_fee_a)
437  (_24h_withdrawal_fee_b)
438  (_24h_exchange_a2b_count)
439  (_24h_exchange_a2b_amount_a)
440  (_24h_exchange_a2b_amount_b)
441  (_24h_exchange_b2a_count)
442  (_24h_exchange_b2a_amount_a)
443  (_24h_exchange_b2a_amount_b)
444  (_24h_exchange_fee_a)
445  (_24h_exchange_fee_b)
446  (_24h_balance_delta_a)
447  (_24h_balance_delta_b)
448  (total_deposit_count)
449  (total_deposit_amount_a)
450  (total_deposit_amount_b)
451  (total_deposit_share_amount)
452  (total_withdrawal_count)
453  (total_withdrawal_amount_a)
454  (total_withdrawal_amount_b)
455  (total_withdrawal_share_amount)
456  (total_withdrawal_fee_a)
457  (total_withdrawal_fee_b)
458  (total_exchange_a2b_count)
459  (total_exchange_a2b_amount_a)
460  (total_exchange_a2b_amount_b)
461  (total_exchange_b2a_count)
462  (total_exchange_b2a_amount_a)
463  (total_exchange_b2a_amount_b)
464  (total_exchange_fee_a)
465  (total_exchange_fee_b)
466  )
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
multi_index_container< liquidity_pool_history_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_pool_seq >, composite_key< liquidity_pool_history_object, member< liquidity_pool_history_object, liquidity_pool_id_type,&liquidity_pool_history_object::pool >, member< liquidity_pool_history_object, uint64_t,&liquidity_pool_history_object::sequence > >, composite_key_compare< std::less< liquidity_pool_id_type >, std::greater< uint64_t > > >, ordered_unique< tag< by_pool_time >, composite_key< liquidity_pool_history_object, member< liquidity_pool_history_object, liquidity_pool_id_type,&liquidity_pool_history_object::pool >, member< liquidity_pool_history_object, time_point_sec,&liquidity_pool_history_object::time >, member< liquidity_pool_history_object, uint64_t,&liquidity_pool_history_object::sequence > >, composite_key_compare< std::less< liquidity_pool_id_type >, std::greater< time_point_sec >, std::greater< uint64_t > > >, ordered_unique< tag< by_pool_op_type_seq >, composite_key< liquidity_pool_history_object, member< liquidity_pool_history_object, liquidity_pool_id_type,&liquidity_pool_history_object::pool >, member< liquidity_pool_history_object, int64_t,&liquidity_pool_history_object::op_type >, member< liquidity_pool_history_object, uint64_t,&liquidity_pool_history_object::sequence > >, composite_key_compare< std::less< liquidity_pool_id_type >, std::less< int64_t >, std::greater< uint64_t > > >, ordered_unique< tag< by_pool_op_type_time >, composite_key< liquidity_pool_history_object, member< liquidity_pool_history_object, liquidity_pool_id_type,&liquidity_pool_history_object::pool >, member< liquidity_pool_history_object, int64_t,&liquidity_pool_history_object::op_type >, member< liquidity_pool_history_object, time_point_sec,&liquidity_pool_history_object::time >, member< liquidity_pool_history_object, uint64_t,&liquidity_pool_history_object::sequence > >, composite_key_compare< std::less< liquidity_pool_id_type >, std::less< int64_t >, std::greater< time_point_sec >, std::greater< uint64_t > > > >> liquidity_pool_history_multi_index_type
tracks the history of all logical operations on blockchain stateAll operations and virtual operations...
generic_index< order_history_object, order_history_multi_index_type > history_index
generic_index< liquidity_pool_ticker_object, liquidity_pool_ticker_multi_index_type > liquidity_pool_ticker_index
Definition: api.cpp:56
result_type operator()(const order_history_object &o) const
generic_index< bucket_object, bucket_object_multi_index_type > bucket_index
bool operator==(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:249
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:114
microseconds seconds(int64_t s)
Definition: time.hpp:34
multi_index_container< market_ticker_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_non_unique< tag< by_volume >, member< market_ticker_object, fc::uint128_t,&market_ticker_object::base_volume > >, ordered_unique< tag< by_market >, composite_key< market_ticker_object, member< market_ticker_object, asset_id_type,&market_ticker_object::base >, member< market_ticker_object, asset_id_type,&market_ticker_object::quote > > > >> market_ticker_object_multi_index_type
generic_index< liquidity_pool_history_object, liquidity_pool_history_multi_index_type > liquidity_pool_history_index
multi_index_container< order_history_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_key >, member< order_history_object, history_key,&order_history_object::key > >, ordered_unique< tag< by_market_time >, composite_key< order_history_object, order_history_object_key_base_extractor, order_history_object_key_quote_extractor, member< order_history_object, time_point_sec,&order_history_object::time >, order_history_object_key_sequence_extractor >, composite_key_compare< std::less< asset_id_type >, std::less< asset_id_type >, std::greater< time_point_sec >, std::less< int64_t > > > >> order_history_multi_index_type
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< liquidity_pool_ticker_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > > >> liquidity_pool_ticker_multi_index_type
safe< int64_t > share_type
Definition: types.hpp:247
#define MARKET_HISTORY_SPACE_ID
result_type operator()(const order_history_object &o) const
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition: reflect.hpp:305
bucket_key(asset_id_type a, asset_id_type b, uint32_t s, fc::time_point_sec o)
bool operator<(const variant &a, const variant &b)
Definition: variant.cpp:734
multi_index_container< bucket_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type,&object::id > >, ordered_unique< tag< by_key >, member< bucket_object, bucket_key,&bucket_object::key > > >> bucket_object_multi_index_type
generic_index< market_ticker_object, market_ticker_object_multi_index_type > market_ticker_index