Flex
Flex exchange system
PriceXchg.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <tvm/schema/message.hpp>
11 
12 #include <tvm/replay_attack_protection/timestamp.hpp>
13 #include <tvm/smart_switcher.hpp>
14 #include <tvm/small_dict_map.hpp>
15 #include <tvm/contract_handle.hpp>
16 #include <tvm/big_queue.hpp>
17 #include <tvm/string.hpp>
18 #include <tvm/numeric_limits.hpp>
19 #include <tvm/suffixes.hpp>
20 
21 #include "Flex.hpp"
22 #include "PriceCommon.hpp"
23 #include "FlexWallet.hpp"
25 #include "FlexLendPayloadArgs.hpp"
26 #include "PriceXchgSalt.hpp"
27 #include "RationalValue.hpp"
28 
29 namespace tvm {
30 
31 using ITONTokenWalletPtr = handle<ITONTokenWallet>;
32 
36 static constexpr unsigned safe_delay_period = 5 * 60;
37 
38 __always_inline
39 bool is_active_time(uint32 order_finish_time) {
40  return tvm_now() < static_cast<int>(order_finish_time.get());
41 }
42 
45 __always_inline
46 std::optional<uint128> minor_cost(uint128 amount, price_t price) {
47  unsigned cost = __builtin_tvm_muldivr(amount.get(), price.numerator().get(), price.denominator().get());
48  if ((cost >> 128) || (cost == 0))
49  return {};
50  return uint128{cost};
51 }
52 
54 struct OrderInfoXchg {
56  bool post_order;
57  uint128 original_amount;
58  uint128 amount;
59  uint128 account;
60  uint128 lend_amount;
61  addr_std_fixed tip3_wallet_provide;
62  addr_std_fixed client_addr;
65  uint256 user_id;
66  uint256 order_id;
67  uint64 ltime;
68 };
69 using OrderInfoXchgWithIdx = std::pair<unsigned, OrderInfoXchg>;
70 
73  uint128 price_num;
74  dict_array<OrderInfoXchg> sells;
75  dict_array<OrderInfoXchg> buys;
77 };
78 
84 __interface IPriceXchg {
85 
87 
88  [[internal, noaccept, answer_id, deploy]]
90  uint128 balance,
91  uint32 lend_finish_time,
92  Tip3Creds creds,
93  cell payload,
94  address answer_addr
95  ) = 201;
96 
98 
100  [[internal, noaccept]]
101  void processQueue() = 202;
102 
104  [[internal, noaccept]]
106  bool sell,
107  opt<uint256> user_id,
108  opt<uint256> order_id
109  ) = 203;
110 
112  [[internal, noaccept]]
114  bool sell,
115  address owner,
116  uint256 user_id,
117  opt<uint256> order_id
118  ) = 205;
119 
121  [[getter]]
123 };
124 using IPriceXchgPtr = handle<IPriceXchg>;
125 
127 struct DPriceXchg {
128  uint128 price_num_;
129  uint128 sells_amount_;
131  uint128 buys_amount_;
133 
134  big_queue<OrderInfoXchg> sells_;
135  big_queue<OrderInfoXchg> buys_;
136 };
137 
140 __interface EPriceXchg {
141 };
142 
144 template<>
145 struct preparer<IPriceXchg, DPriceXchg> {
146  __always_inline
147  static std::pair<StateInit, uint256> execute(DPriceXchg data, cell code) {
148  cell data_cl = prepare_persistent_data<IPriceXchg, void>({}, data);
149  StateInit init { {}, {}, code, data_cl, {} };
150  cell init_cl = build(init).make_cell();
151  return { init, uint256(tvm_hash(init_cl)) };
152  }
153 };
154 
155 } // namespace tvm
156 
Flex transfer payload args structure.
Flex root contract interfaces and data-structs.
Price common interfaces and data-structs.
PriceXchgSalt - configuration structure for PriceXchg (stored in code salt)
__always_inline std::optional< uint128 > minor_cost(uint128 amount, price_t price)
Definition: PriceXchg.hpp:46
Flex rational price (numerator / denominator)
PriceXchg persistent data struct.
Definition: PriceXchg.hpp:127
uint128 sells_amount_
Definition: PriceXchg.hpp:129
big_queue< OrderInfoXchg > buys_
Queue of buy orders.
Definition: PriceXchg.hpp:135
big_queue< OrderInfoXchg > sells_
Queue of sell orders.
Definition: PriceXchg.hpp:134
uint128 price_num_
Price numerator in minor tokens for one minor token - rational number, denominator kept in config.
Definition: PriceXchg.hpp:128
PriceXchg events interface.
Definition: PriceXchg.hpp:140
PriceXchg contract interface.
Definition: PriceXchg.hpp:84
OrderRet onTip3LendOwnership(uint128 balance, uint32 lend_finish_time, Tip3Creds creds, cell payload, address answer_addr)
Implementation of ITONTokenWalletNotify::onTip3LendOwnership().
void cancelWalletOrder(bool sell, address owner, uint256 user_id, opt< uint256 > order_id)
Will cancel orders, may be requested only from FlexWallet.
void processQueue()
Process enqueued orders.
PriceXchgDetails getDetails()
Get contract details.
void cancelOrder(bool sell, opt< uint256 > user_id, opt< uint256 > order_id)
Will cancel all sell/buy orders with this sender's client_addr.
tip3-tip3 exchange order info
Definition: PriceXchg.hpp:54
uint128 original_amount
Original amount of major tokens to buy or sell.
Definition: PriceXchg.hpp:57
uint128 account
Remaining native funds from client to pay for processing.
Definition: PriceXchg.hpp:59
bool post_order
Should this order be enqueued if it doesn't already have corresponding orders.
Definition: PriceXchg.hpp:56
bool immediate_client
Should this order try to be executed as a client order first (find existing corresponding orders).
Definition: PriceXchg.hpp:55
uint128 amount
Current remaining amount of major tokens to buy or sell.
Definition: PriceXchg.hpp:58
addr_std_fixed client_addr
Definition: PriceXchg.hpp:62
uint64 ltime
Logical time of starting transaction for the order.
Definition: PriceXchg.hpp:67
uint32 order_finish_time
Order finish time.
Definition: PriceXchg.hpp:64
addr_std_fixed tip3_wallet_provide
Client tip3 wallet to provide tokens (major for sell or minor for buy).
Definition: PriceXchg.hpp:61
uint128 lend_amount
Current remaining amount of lend tokens (major tokens for sell, minor tokens for buy).
Definition: PriceXchg.hpp:60
uint256 order_id
Order id.
Definition: PriceXchg.hpp:66
uint256 user_id
User id.
Definition: PriceXchg.hpp:65
Notification struct about order to a client.
Definition: PriceCommon.hpp:16
PriceXchg contract details (for getter)
Definition: PriceXchg.hpp:72
PriceXchgSalt salt
Configuration from code salt.
Definition: PriceXchg.hpp:76
uint128 price_num
Price numerator in minor tokens for one minor token - rational number, denominator kept in config.
Definition: PriceXchg.hpp:73
dict_array< OrderInfoXchg > buys
Buy orders.
Definition: PriceXchg.hpp:75
dict_array< OrderInfoXchg > sells
Sell orders.
Definition: PriceXchg.hpp:74
Price configuration data (common for prices of one pair). Stored in code salt.
Definition: PriceXchgSalt.hpp:12
Rational value - numerator/denominator.
Definition: RationalValue.hpp:12