BitShares-Core  5.0.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>
28 
29 #include <fc/thread/future.hpp>
30 #include <fc/uint128.hpp>
31 
32 #include <boost/multi_index/composite_key.hpp>
33 
34 namespace graphene { namespace market_history {
35 using namespace chain;
36 
37 //
38 // Plugins should #define their SPACE_ID's so plugins with
39 // conflicting SPACE_ID assignments can be compiled into the
40 // same binary (by simply re-assigning some of the conflicting #defined
41 // SPACE_ID's in a build script).
42 //
43 // Assignment of SPACE_ID's cannot be done at run-time because
44 // various template automagic depends on them being known at compile
45 // time.
46 //
47 #ifndef MARKET_HISTORY_SPACE_ID
48 #define MARKET_HISTORY_SPACE_ID 5
49 #endif
50 
52 {
57 };
58 
59 struct bucket_key
60 {
61  bucket_key( asset_id_type a, asset_id_type b, uint32_t s, fc::time_point_sec o )
62  :base(a),quote(b),seconds(s),open(o){}
64 
65  asset_id_type base;
66  asset_id_type quote;
67  uint32_t seconds = 0;
69 
70  friend bool operator < ( const bucket_key& a, const bucket_key& b )
71  {
72  return std::tie( a.base, a.quote, a.seconds, a.open ) < std::tie( b.base, b.quote, b.seconds, b.open );
73  }
74  friend bool operator == ( const bucket_key& a, const bucket_key& b )
75  {
76  return std::tie( a.base, a.quote, b.seconds, a.open ) == std::tie( b.base, b.quote, b.seconds, b.open );
77  }
78 };
79 
80 struct bucket_object : public abstract_object<bucket_object>
81 {
82  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
83  static constexpr uint8_t type_id = bucket_object_type;
84 
85  price high()const { return asset( high_base, key.base ) / asset( high_quote, key.quote ); }
86  price low()const { return asset( low_base, key.base ) / asset( low_quote, key.quote ); }
87 
99 };
100 
101 struct history_key {
102  asset_id_type base;
103  asset_id_type quote;
104  int64_t sequence = 0;
105 
106  friend bool operator < ( const history_key& a, const history_key& b ) {
107  return std::tie( a.base, a.quote, a.sequence ) < std::tie( b.base, b.quote, b.sequence );
108  }
109  friend bool operator == ( const history_key& a, const history_key& b ) {
110  return std::tie( a.base, a.quote, a.sequence ) == std::tie( b.base, b.quote, b.sequence );
111  }
112 };
113 struct order_history_object : public abstract_object<order_history_object>
114 {
115  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
116  static constexpr uint8_t type_id = order_history_object_type;
117 
121 };
123 {
124  typedef asset_id_type result_type;
125  result_type operator()(const order_history_object& o)const { return o.key.base; }
126 };
128 {
129  typedef asset_id_type result_type;
130  result_type operator()(const order_history_object& o)const { return o.key.quote; }
131 };
133 {
134  typedef int64_t result_type;
135  result_type operator()(const order_history_object& o)const { return o.key.sequence; }
136 };
137 
138 struct market_ticker_object : public abstract_object<market_ticker_object>
139 {
140  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
141  static constexpr uint8_t type_id = market_ticker_object_type;
142 
143  asset_id_type base;
144  asset_id_type quote;
149  fc::uint128_t base_volume;
150  fc::uint128_t quote_volume;
151 };
152 
153 struct market_ticker_meta_object : public abstract_object<market_ticker_meta_object>
154 {
155  static constexpr uint8_t space_id = MARKET_HISTORY_SPACE_ID;
156  static constexpr uint8_t type_id = market_ticker_meta_object_type;
157 
159  bool skip_min_order_his_id = false;
160 };
161 
162 struct by_key;
163 typedef multi_index_container<
165  indexed_by<
166  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
167  ordered_unique< tag<by_key>, member< bucket_object, bucket_key, &bucket_object::key > >
168  >
170 
171 struct by_market_time;
172 typedef multi_index_container<
174  indexed_by<
175  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
176  ordered_unique< tag<by_key>, member< order_history_object, history_key, &order_history_object::key > >,
177  ordered_unique<
178  tag<by_market_time>,
179  composite_key<
180  order_history_object,
183  member<order_history_object, time_point_sec, &order_history_object::time>,
185  >,
186  composite_key_compare<
187  std::less< asset_id_type >,
188  std::less< asset_id_type >,
189  std::greater< time_point_sec >,
190  std::less< int64_t >
191  >
192  >
193  >
195 
196 struct by_market;
197 struct by_volume;
198 typedef multi_index_container<
200  indexed_by<
201  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
202  ordered_non_unique< tag<by_volume>,
203  member< market_ticker_object, fc::uint128_t, &market_ticker_object::base_volume > >,
204  ordered_unique<
205  tag<by_market>,
206  composite_key<
207  market_ticker_object,
208  member<market_ticker_object, asset_id_type, &market_ticker_object::base>,
209  member<market_ticker_object, asset_id_type, &market_ticker_object::quote>
210  >
211  >
212  >
214 
218 
219 
220 namespace detail
221 {
223 }
224 
231 {
232  public:
234  virtual ~market_history_plugin();
235 
236  std::string plugin_name()const override;
237  virtual void plugin_set_program_options(
238  boost::program_options::options_description& cli,
239  boost::program_options::options_description& cfg) override;
240  virtual void plugin_initialize(
241  const boost::program_options::variables_map& options) override;
242  virtual void plugin_startup() override;
243 
244  uint32_t max_history()const;
245  const flat_set<uint32_t>& tracked_buckets()const;
246  uint32_t max_order_his_records_per_market()const;
247  uint32_t max_order_his_seconds_per_market()const;
248 
249  private:
251  std::unique_ptr<detail::market_history_plugin_impl> my;
252 };
253 
254 } } //graphene::market_history
255 
256 FC_REFLECT( graphene::market_history::history_key, (base)(quote)(sequence) )
257 FC_REFLECT_DERIVED( graphene::market_history::order_history_object, (graphene::db::object), (key)(time)(op) )
258 FC_REFLECT( graphene::market_history::bucket_key, (base)(quote)(seconds)(open) )
259 FC_REFLECT_DERIVED( graphene::market_history::bucket_object, (graphene::db::object),
260  (key)
261  (high_base)(high_quote)
262  (low_base)(low_quote)
263  (open_base)(open_quote)
264  (close_base)(close_quote)
265  (base_volume)(quote_volume) )
266 FC_REFLECT_DERIVED( graphene::market_history::market_ticker_object, (graphene::db::object),
267  (base)(quote)
268  (last_day_base)(last_day_quote)
269  (latest_base)(latest_quote)
270  (base_volume)(quote_volume) )
271 FC_REFLECT_DERIVED( graphene::market_history::market_ticker_meta_object, (graphene::db::object),
272  (rolling_min_order_his_id)(skip_min_order_his_id) )
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
generic_index< order_history_object, order_history_multi_index_type > history_index
Definition: api.cpp:56
result_type operator()(const order_history_object &o) const
FC_REFLECT_DERIVED(graphene::app::extended_asset_object,(graphene::chain::asset_object),(total_in_collateral)(total_backing_collateral))
generic_index< bucket_object, bucket_object_multi_index_type > bucket_index
bool operator==(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:245
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
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
#define MARKET_HISTORY_SPACE_ID
result_type operator()(const order_history_object &o) const
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