Flex
Flex exchange system
process_queue_state.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "../PriceXchg.hpp"
10 
11 #include "error_code.hpp"
12 #include <tvm/suffixes.hpp>
13 
14 namespace tvm { namespace xchg {
15 
18 public:
19  static constexpr unsigned msgs_per_deal = 4;
20  static constexpr unsigned msgs_per_expired = 1;
21  static constexpr unsigned msgs_per_out_of_evers = 2;
22  static constexpr unsigned msgs_per_no_post_order_done = 2;
23 
25  price_t price,
26  address pair,
27  Tip3Config major_tip3cfg,
28  Tip3Config minor_tip3cfg,
29  EversConfig ev_cfg,
30  uint128 min_amount,
31  unsigned deals_limit,
32  unsigned msgs_limit,
33  IFlexNotifyPtr notify_addr,
34  unsigned sell_idx,
36  unsigned buy_idx
38  ) : price_(price), pair_(pair), major_tip3cfg_(major_tip3cfg), minor_tip3cfg_(minor_tip3cfg),
39  ev_cfg_(ev_cfg), min_amount_(min_amount), deals_limit_(deals_limit), msgs_limit_(msgs_limit),
40  tip3root_major_(major_tip3cfg.root_address), tip3root_minor_(minor_tip3cfg.root_address), notify_addr_(notify_addr),
41  sell_idx_(sell_idx), buy_idx_(buy_idx) {}
42 
45  void on_expired(OrderInfoXchgWithIdx ord_idx, bool sell) {
46  auto ord = ord_idx.second;
47  ++expired_;
48  // For the first expired order we also registering one message for amm notification
49  if (sell) {
51  sell_cancels_amount_ += ord.amount;
52  } else {
54  buy_cancels_amount_ += ord.amount;
55  }
56  OrderRet ret { uint32(ec::expired), ord.original_amount - ord.amount, 0u128, price_.num, price_.denum,
57  ord.user_id, ord.order_id, pair_, major_tip3cfg_.decimals, minor_tip3cfg_.decimals, sell };
58  IPriceCallbackPtr(ord.tip3_wallet_provide)(Evers(ord.account.get())).
59  onOrderFinished(ret);
60  }
61 
65  void on_out_of_evers(OrderInfoXchgWithIdx ord_idx, bool sell) {
66  auto ord = ord_idx.second;
67  ++out_of_evers_;
68  // For the first expired order we also registering one message for amm notification
69  if (sell) {
71  sell_cancels_amount_ += ord.amount;
72  } else {
74  buy_cancels_amount_ += ord.amount;
75  }
76 
77  OrderRet ret { uint32(ec::out_of_tons), ord.original_amount - ord.amount, 0u128, price_.num, price_.denum,
78  ord.user_id, ord.order_id, pair_, major_tip3cfg_.decimals, minor_tip3cfg_.decimals, sell };
79  check_ret(sell, ord_idx.first, ret);
80  if (ord.account > ev_cfg_.return_ownership) {
81  ord.account -= ev_cfg_.return_ownership;
82  ITONTokenWalletPtr(ord.tip3_wallet_provide)(Evers(ev_cfg_.return_ownership.get())).
83  returnOwnership(ord.lend_amount);
84  IPriceCallbackPtr(ord.tip3_wallet_provide)(Evers(ord.account.get())).
85  onOrderFinished(ret);
86  }
87  }
88 
90  void on_order_done(OrderInfoXchgWithIdx ord_idx, bool sell) {
91  auto ord = ord_idx.second;
92  OrderRet ret { uint32(ok), ord.original_amount - ord.amount, 0u128, price_.num, price_.denum,
93  ord.user_id, ord.order_id, pair_, major_tip3cfg_.decimals, minor_tip3cfg_.decimals, sell };
94  check_ret(sell, ord_idx.first, ret);
95  IPriceCallbackPtr(ord.tip3_wallet_provide)(Evers(ord.account.get())).
96  onOrderFinished(ret);
97  }
98 
100  void on_no_post_order_done(OrderInfoXchgWithIdx ord_idx, bool sell) {
101  auto ord = ord_idx.second;
103  // For the first partially done no-post order we also registering one message for amm notification
104  if (sell) {
106  sell_cancels_amount_ += ord.amount;
107  } else {
109  buy_cancels_amount_ += ord.amount;
110  }
111 
112  OrderRet ret { uint32(ec::no_post_order_partially_done), ord.original_amount - ord.amount, 0u128, price_.num, price_.denum,
113  ord.user_id, ord.order_id, pair_, major_tip3cfg_.decimals, minor_tip3cfg_.decimals, sell };
114  check_ret(sell, ord_idx.first, ret);
115  if (ord.account > ev_cfg_.return_ownership) {
116  ord.account -= ev_cfg_.return_ownership;
117  ITONTokenWalletPtr(ord.tip3_wallet_provide)(Evers(ev_cfg_.return_ownership.get())).
118  returnOwnership(ord.lend_amount);
119  IPriceCallbackPtr(ord.tip3_wallet_provide)(Evers(ord.account.get())).
120  onOrderFinished(ret);
121  }
122  }
123 
125  void check_ret(bool sell, unsigned idx, OrderRet ret) {
126  if (sell && sell_idx_ == idx)
127  ret_ = ret;
128  if (!sell && buy_idx_ == idx)
129  ret_ = ret;
130  }
131 
133  void on_deal(bool seller_taker, uint128 deal_amount) {
135  sum_deals_amount_ += deal_amount;
136  if (seller_taker)
137  sum_taker_sells_amount_ += deal_amount;
138  else
139  sum_taker_buys_amount_ += deal_amount;
140  }
141 
143  bool is_order_done(OrderInfoXchg ord) const {
144  return (ord.amount == 0) || (ord.amount < min_amount_) || !minor_cost(ord.amount, price_);
145  }
146 
148  bool overlimit() const {
150  }
151 
153  void finalize(uint128 rest_sell_amount, uint128 rest_buy_amount) {
154  if (sum_deals_amount_) {
155  bool seller_taker = sum_taker_sells_amount_ > sum_taker_buys_amount_;
156  notify_addr_(Evers(ev_cfg_.send_notify.get())).
157  onXchgDealCompleted(seller_taker, pair_,
161  }
162  if (sell_cancels_amount_) {
163  notify_addr_(Evers(ev_cfg_.send_notify.get())).
164  onXchgOrderCanceled(true, tip3root_major_, tip3root_minor_,
165  price_.numerator(), price_.denominator(), sell_cancels_amount_, rest_sell_amount);
166  }
167  if (buy_cancels_amount_) {
168  notify_addr_(Evers(ev_cfg_.send_notify.get())).
169  onXchgOrderCanceled(false, tip3root_major_, tip3root_minor_,
170  price_.numerator(), price_.denominator(), buy_cancels_amount_, rest_buy_amount);
171  }
172  }
173 
175  address pair_;
176  Tip3Config major_tip3cfg_;
177  Tip3Config minor_tip3cfg_;
179  uint128 expired_;
180  uint128 out_of_evers_;
183  uint128 msgs_outs_;
189  uint128 min_amount_;
190  unsigned deals_limit_;
191  unsigned msgs_limit_;
192  address tip3root_major_;
193  address tip3root_minor_;
194  IFlexNotifyPtr notify_addr_;
195  unsigned sell_idx_;
196  unsigned buy_idx_;
197  opt<OrderRet> ret_;
198 };
199 
200 }} // namespace tvm::xchg
201 
__always_inline std::optional< uint128 > minor_cost(uint128 amount, price_t price)
Definition: PriceXchg.hpp:46
Processing orders queue state for PriceXchg.
Definition: process_queue_state.hpp:17
uint128 msgs_outs_
Out messages to sent.
Definition: process_queue_state.hpp:183
static constexpr unsigned msgs_per_expired
Messages per order expired.
Definition: process_queue_state.hpp:20
uint128 sum_taker_sells_amount_
Summarized amount of major tokens in all taker-sell deals.
Definition: process_queue_state.hpp:185
address tip3root_minor_
Address of RootTokenContract for minor tip3 token.
Definition: process_queue_state.hpp:193
unsigned sell_idx_
If we are processing onTip3LendOwnership with sell, this index we can use for return value.
Definition: process_queue_state.hpp:195
uint128 deals_processed_
Deals processed.
Definition: process_queue_state.hpp:182
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
unsigned msgs_limit_
Messages limit.
Definition: process_queue_state.hpp:191
void on_order_done(OrderInfoXchgWithIdx ord_idx, bool sell)
When order is done.
Definition: process_queue_state.hpp:90
price_t price_
Price (rational value)
Definition: process_queue_state.hpp:174
void on_no_post_order_done(OrderInfoXchgWithIdx ord_idx, bool sell)
When we have order without post-order flag and other side queue is empty.
Definition: process_queue_state.hpp:100
Tip3Config major_tip3cfg_
Major tip3 configuration.
Definition: process_queue_state.hpp:176
static constexpr unsigned msgs_per_deal
Messages per deal completed.
Definition: process_queue_state.hpp:19
opt< OrderRet > ret_
Return value.
Definition: process_queue_state.hpp:197
uint128 expired_
Expired orders met during current processing.
Definition: process_queue_state.hpp:179
IFlexNotifyPtr notify_addr_
Notification address for AMM (IFlexNotify).
Definition: process_queue_state.hpp:194
void check_ret(bool sell, unsigned idx, OrderRet ret)
Check that order is the current requested order and we need to save return value.
Definition: process_queue_state.hpp:125
unsigned buy_idx_
If we are processing onTip3LendOwnership with buy, this index we can use for return value.
Definition: process_queue_state.hpp:196
uint128 sum_taker_buys_amount_
Summarized amount of major tokens in all taker-buy deals.
Definition: process_queue_state.hpp:186
uint128 no_post_order_dones_
Partially done orders without post-order flag.
Definition: process_queue_state.hpp:181
void finalize(uint128 rest_sell_amount, uint128 rest_buy_amount)
Finalize state - send AMM notifications about processed deals and canceled orders.
Definition: process_queue_state.hpp:153
void on_out_of_evers(OrderInfoXchgWithIdx ord_idx, bool sell)
Definition: process_queue_state.hpp:65
unsigned deals_limit_
Deals limit.
Definition: process_queue_state.hpp:190
uint128 out_of_evers_
Out-of-evers orders met during current processing.
Definition: process_queue_state.hpp:180
uint128 min_amount_
Minimum amount of major tokens for a deal or an order.
Definition: process_queue_state.hpp:189
Tip3Config minor_tip3cfg_
Minor tip3 configuration.
Definition: process_queue_state.hpp:177
static constexpr unsigned msgs_per_out_of_evers
Messages per order out-of-evers.
Definition: process_queue_state.hpp:21
EversConfig ev_cfg_
Evers processing costs configuration.
Definition: process_queue_state.hpp:178
address pair_
Address of XchgPair contract.
Definition: process_queue_state.hpp:175
static constexpr unsigned msgs_per_no_post_order_done
Messages per non-post-order partially done.
Definition: process_queue_state.hpp:22
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
void on_deal(bool seller_taker, uint128 deal_amount)
When a deal is processed.
Definition: process_queue_state.hpp:133
uint128 buy_cancels_amount_
Canceled buy orders (ooe/expired)
Definition: process_queue_state.hpp:188
uint128 sell_cancels_amount_
Canceled sell orders (ooe/expired)
Definition: process_queue_state.hpp:187
uint128 sum_deals_amount_
Summarized amount of major tokens in all deals.
Definition: process_queue_state.hpp:184
process_queue_state(price_t price, address pair, Tip3Config major_tip3cfg, Tip3Config minor_tip3cfg, EversConfig ev_cfg, uint128 min_amount, unsigned deals_limit, unsigned msgs_limit, IFlexNotifyPtr notify_addr, unsigned sell_idx, unsigned buy_idx)
Definition: process_queue_state.hpp:24
address tip3root_major_
Address of RootTokenContract for major tip3 token.
Definition: process_queue_state.hpp:192
Error codes for PriceXchg.
Processing native funds value ...
Definition: EversConfig.hpp:12
uint128 send_notify
... to send notification about completed deal (IFlexNotify)
Definition: EversConfig.hpp:23
uint128 return_ownership
... to return tip3 ownership
Definition: EversConfig.hpp:16
tip3-tip3 exchange order info
Definition: PriceXchg.hpp:54
uint128 amount
Current remaining amount of major tokens to buy or sell.
Definition: PriceXchg.hpp:58
Notification struct about order to a client.
Definition: PriceCommon.hpp:16
Rational value - numerator/denominator.
Definition: RationalValue.hpp:12
uint128 denominator() const
Denominator part of rational number.
Definition: RationalValue.hpp:14
uint128 numerator() const
Numerator part of rational number.
Definition: RationalValue.hpp:13
static constexpr unsigned expired
Order is expired.
Definition: error_code.hpp:21
static constexpr unsigned out_of_tons
Partially processed because out of tons.
Definition: error_code.hpp:14
static constexpr unsigned no_post_order_partially_done
Order without post-order flag is partially done.
Definition: error_code.hpp:22