11 #include "../PriceXchg.hpp"
12 #include "../FlexTransferPayloadArgs.hpp"
16 #include <tvm/suffixes.hpp>
17 #include <tvm/schema/build_chain_static.hpp>
19 namespace tvm {
namespace xchg {
27 Tip3Config major_tip3cfg,
28 Tip3Config minor_tip3cfg,
35 IFlexNotifyPtr notify_addr,
36 address major_reserve_wallet,
37 address minor_reserve_wallet
41 deal_costs_(ev_cfg.transfer_tip3 * 3 + ev_cfg.send_notify),
65 auto res =
make_deal(*sells_iter, *buys_iter);
66 if (res.sell_out_of_evers)
68 if (res.buy_out_of_evers)
70 if (!res.sell_out_of_evers && !res.buy_out_of_evers) {
71 sells_iter.
on_deal(res.deal_amount, res.seller_costs, res.seller_lend_spent);
72 buys_iter.
on_deal(res.deal_amount, res.buyer_costs, res.buyer_lend_spent);
73 state.
on_deal(res.seller_taker, res.deal_amount);
87 if (sells_empty && !buys_empty) {
89 }
else if (!sells_empty && buys_empty) {
125 return {.seller_taker =
false, last_tip3_sell, last_tip3_buy, 0u128, 0u128, 0u128, 0u128};
127 auto major_deal_amount = deal_amount;
128 auto minor_deal_amount = *buy_payment;
130 auto sell_extra_return = last_tip3_sell ? (sell.
lend_amount - major_deal_amount) : 0u128;
131 auto buy_extra_return = last_tip3_buy ? (buy.
lend_amount - minor_deal_amount) : 0u128;
135 bool seller_pays_costs = last_tip3_sell;
138 if (sell_out_of_evers || buy_out_of_evers)
139 return {.seller_taker =
false, sell_out_of_evers, buy_out_of_evers, 0u128, 0u128, 0u128, 0u128};
141 uint128 seller_costs = seller_pays_costs ?
deal_costs_ : 0u128;
142 uint128 buyer_costs = !seller_pays_costs ?
deal_costs_ : 0u128;
144 uint128 seller_lend_spent;
145 uint128 buyer_lend_spent;
176 uint128 taker_fee_val = mul(major_deal_amount, taker_fee);
177 uint128 maker_vig_val = mul(major_deal_amount, maker_vig);
178 uint128 reserve_val = taker_fee_val - maker_vig_val;
179 seller_lend_spent = major_deal_amount + taker_fee_val;
180 buyer_lend_spent = minor_deal_amount;
184 .sender_taker =
true,
185 .sender_user_id = sell.
user_id,
186 .receiver_user_id = buy.
user_id,
191 .taker_fee = taker_fee_val,
192 .maker_vig = maker_vig_val,
199 transferToRecipient({}, { buy.
user_id, buy.
client_addr }, major_deal_amount + maker_vig_val,
201 build_chain_static(seller_payload));
205 .sender_taker =
false,
207 .receiver_user_id = sell.
user_id,
212 .taker_fee = taker_fee_val,
213 .maker_vig = maker_vig_val,
222 build_chain_static(buyer_payload));
224 if (reserve_val > 0) {
227 build_chain_static(seller_payload));
230 uint128 taker_fee_val = mul(minor_deal_amount, taker_fee);
231 uint128 maker_vig_val = mul(minor_deal_amount, maker_vig);
232 uint128 reserve_val = taker_fee_val - maker_vig_val;
233 seller_lend_spent = major_deal_amount;
234 buyer_lend_spent = minor_deal_amount + taker_fee_val;
238 .sender_taker =
true,
240 .receiver_user_id = sell.
user_id,
245 .taker_fee = taker_fee_val,
246 .maker_vig = maker_vig_val,
253 transferToRecipient({}, { sell.
user_id, sell.
client_addr }, minor_deal_amount + maker_vig_val,
255 build_chain_static(buyer_payload));
259 .sender_taker =
false,
260 .sender_user_id = sell.
user_id,
261 .receiver_user_id = buy.
user_id,
266 .taker_fee = taker_fee_val,
267 .maker_vig = maker_vig_val,
276 build_chain_static(seller_payload));
278 if (reserve_val > 0) {
281 build_chain_static(buyer_payload));
285 .seller_taker = seller_taker,
false,
false, deal_amount,
286 seller_costs, buyer_costs,
287 seller_lend_spent, buyer_lend_spent
__always_inline std::optional< uint128 > minor_cost(uint128 amount, price_t price)
Definition: PriceXchg.hpp:46
Class for iterating queue and processing deals.
Definition: dealer.hpp:22
uint128 seller_costs
Seller evers costs to be taken.
Definition: dealer.hpp:108
address tip3root_minor_
Address of RootTokenContract for minor tip3 token.
Definition: dealer.hpp:303
orders_queue buys
Buy orders queue.
Definition: dealer.hpp:49
unsigned msgs_limit_
Messages limit.
Definition: dealer.hpp:300
uint128 seller_lend_spent
Seller lend tokens spent (major tokens for seller)
Definition: dealer.hpp:110
price_t price_
Price (rational value)
Definition: dealer.hpp:291
Tip3Config major_tip3cfg_
Major tip3 configuration.
Definition: dealer.hpp:293
opt< OrderRet > ret
Return value for the called function.
Definition: dealer.hpp:50
orders_queue sells
Sell orders queue.
Definition: dealer.hpp:48
process_result process(unsigned sell_idx, unsigned buy_idx)
Process order queues and make deals.
Definition: dealer.hpp:54
deal_result make_deal(OrderInfoXchg sell, OrderInfoXchg buy)
Make tip3/tip exchange deal.
Definition: dealer.hpp:115
uint128 buyer_lend_spent
Buyer lend tokens spent (minor tokens for buyer)
Definition: dealer.hpp:111
IFlexNotifyPtr notify_addr_
Notification address for AMM.
Definition: dealer.hpp:304
unsigned deals_limit_
Deals limit.
Definition: dealer.hpp:299
uint128 deal_amount
Amount of major tokens for the deal (without fees)
Definition: dealer.hpp:107
orders_queue sells_
Sell orders queue.
Definition: dealer.hpp:296
bool sell_out_of_evers
Is the sell order out of evers.
Definition: dealer.hpp:105
uint128 min_amount_
Minimum amount of major tokens for a deal or an order.
Definition: dealer.hpp:298
Tip3Config minor_tip3cfg_
Minor tip3 configuration.
Definition: dealer.hpp:294
bool buy_out_of_evers
Is the buy order out of evers.
Definition: dealer.hpp:106
EversConfig ev_cfg_
Processing costs configuration (evers)
Definition: dealer.hpp:295
dealer(price_t price, address pair, Tip3Config major_tip3cfg, Tip3Config minor_tip3cfg, EversConfig ev_cfg, orders_queue sells, orders_queue buys, uint128 min_amount, unsigned deals_limit, unsigned msgs_limit, IFlexNotifyPtr notify_addr, address major_reserve_wallet, address minor_reserve_wallet)
Definition: dealer.hpp:24
address pair_
Address of XchgPair contract.
Definition: dealer.hpp:292
address major_reserve_wallet_
Major reserve wallet.
Definition: dealer.hpp:305
bool seller_taker
Seller is a taker in deal.
Definition: dealer.hpp:104
uint128 buyer_costs
Buyer evers costs to be taken.
Definition: dealer.hpp:109
uint128 deal_costs_
Deal costs in evers.
Definition: dealer.hpp:301
address minor_reserve_wallet_
Minor reserve wallet.
Definition: dealer.hpp:306
orders_queue buys_
Buy orders queue.
Definition: dealer.hpp:297
address tip3root_major_
Address of RootTokenContract for major tip3 token.
Definition: dealer.hpp:302
Result of make_deal.
Definition: dealer.hpp:103
Result of process() call.
Definition: dealer.hpp:47
Working version of orders_queue with cached head order.
Definition: orders_queue.hpp:40
Orders queue to keep orders and common state (tokens amount).
Definition: orders_queue.hpp:16
bool empty() const
Is queue empty.
Definition: orders_queue.hpp:22
uint128 all_amount_
Amount of tokens in all orders.
Definition: orders_queue.hpp:18
void drop_no_post_orders(process_queue_state &state, bool sell)
Drop orders without post_order flag.
Definition: orders_queue.hpp:25
Processing orders queue state for PriceXchg.
Definition: process_queue_state.hpp:17
bool overlimit() const
If we hit deals or messages limit.
Definition: process_queue_state.hpp:148
opt< OrderRet > ret_
Return value.
Definition: process_queue_state.hpp:197
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_deal(bool seller_taker, uint128 deal_amount)
When a deal is processed.
Definition: process_queue_state.hpp:133
Tip3/tip3 exchange orders iterator.
Definition: xchg_iterator.hpp:16
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
void drop_with_ooc()
When order is out-of-evers.
Definition: xchg_iterator.hpp:75
Class for keeping iteration state for PriceXchg.
Processing native funds value ...
Definition: EversConfig.hpp:12
uint128 process_queue
... to process processQueue function. Also is used for buyTip3 / onTip3LendOwnership / cancelSell / c...
Definition: EversConfig.hpp:21
uint128 transfer_tip3
... for executing tip3 transfer
Definition: EversConfig.hpp:14
uint128 dest_wallet_keep_evers
... to keep evers in destination wallet
Definition: EversConfig.hpp:25
tip3-tip3 exchange order info
Definition: PriceXchg.hpp:54
uint128 account
Remaining native funds from client to pay for processing.
Definition: PriceXchg.hpp:59
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
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
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
Exchange orders iterator for PriceXchg.