Flex
Flex exchange system
xchg_iterator.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "../PriceXchg.hpp"
10 #include "orders_queue.hpp"
11 
12 namespace tvm { namespace xchg {
13 
15 
17 public:
19  process_queue_state& state,
20  orders_queue_cached& orders,
21  uint128 deal_costs,
22  bool sell
23  ) : state_(state), orders_(orders), deal_costs_(deal_costs), sell_(sell) {
24  }
25 
27  bool first_active() {
28  while (!orders_.empty() && !state_.overlimit()) {
29  OrderInfoXchgWithIdx& ord_idx = orders_.front_with_idx();
30  [[maybe_unused]] auto [idx, ord] = ord_idx;
31 
32  // If the order is expired
33  if (!is_active_time(ord.order_finish_time)) {
34  state_.on_expired(ord_idx, sell_);
35  orders_.pop();
36  continue;
37  }
38  // If the order has not enough evers for processing
39  if (ord.account < deal_costs_) {
40  state_.on_out_of_evers(ord_idx, sell_);
41  orders_.pop();
42  continue;
43  }
44  return true;
45  }
46  return false;
47  }
48 
49  OrderInfoXchgWithIdx& cur() { return orders_.front_with_idx(); }
50 
51  OrderInfoXchg& operator*() {
52  return cur().second;
53  }
54 
56  void on_deal(uint128 deal_amount, uint128 costs, uint128 lend_spent) {
57  auto& ord_idx = cur();
58  [[maybe_unused]] auto& [idx, ord] = ord_idx;
59  ord.amount -= deal_amount;
60  ord.account -= costs;
61  ord.lend_amount -= lend_spent;
62 
63  // If order is done
64  if (state_.is_order_done(ord)) {
65  state_.on_order_done(ord_idx, sell_);
66  orders_.pop();
67  // If order is out-of-evers
68  } else if (ord.account < deal_costs_) {
69  state_.on_out_of_evers(ord_idx, sell_);
70  orders_.pop();
71  }
72  }
73 
75  void drop_with_ooc() {
77  orders_.pop();
78  }
79 
82  uint128 deal_costs_;
83  bool sell_;
84 };
85 
86 }} // namespace tvm::xchg
87 
Working version of orders_queue with cached head order.
Definition: orders_queue.hpp:40
void pop()
Pop front order.
Definition: orders_queue.hpp:59
bool empty() const
Is queue empty.
Definition: orders_queue.hpp:46
OrderInfoXchgWithIdx & front_with_idx()
Get queue head (front) with caching.
Definition: orders_queue.hpp:49
Processing orders queue state for PriceXchg.
Definition: process_queue_state.hpp:17
void on_expired(OrderInfoXchgWithIdx ord_idx, bool sell)
Definition: process_queue_state.hpp:45
bool overlimit() const
If we hit deals or messages limit.
Definition: process_queue_state.hpp:148
void on_order_done(OrderInfoXchgWithIdx ord_idx, bool sell)
When order is done.
Definition: process_queue_state.hpp:90
void on_out_of_evers(OrderInfoXchgWithIdx ord_idx, bool sell)
Definition: process_queue_state.hpp:65
bool is_order_done(OrderInfoXchg ord) const
Is the order done? Means the remaining amount is less than min_amount or minor_cost can't be calculat...
Definition: process_queue_state.hpp:143
Tip3/tip3 exchange orders iterator.
Definition: xchg_iterator.hpp:16
orders_queue_cached & orders_
Orders queue.
Definition: xchg_iterator.hpp:81
process_queue_state & state_
Processing orders queue state for PriceXchg.
Definition: xchg_iterator.hpp:80
bool sell_
Is it a sell order.
Definition: xchg_iterator.hpp:83
xchg_iterator(process_queue_state &state, orders_queue_cached &orders, uint128 deal_costs, bool sell)
Definition: xchg_iterator.hpp:18
void on_deal(uint128 deal_amount, uint128 costs, uint128 lend_spent)
When deal is completed.
Definition: xchg_iterator.hpp:56
bool first_active()
Move to the first active order in the queue.
Definition: xchg_iterator.hpp:27
uint128 deal_costs_
Deal processing costs.
Definition: xchg_iterator.hpp:82
void drop_with_ooc()
When order is out-of-evers.
Definition: xchg_iterator.hpp:75
Class for orders queue for PriceXchg.
tip3-tip3 exchange order info
Definition: PriceXchg.hpp:54