Flex
Flex exchange system
Flex.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <tvm/schema/message.hpp>
10 #include <tvm/smart_switcher.hpp>
11 #include <tvm/contract_handle.hpp>
12 #include <tvm/replay_attack_protection/timestamp.hpp>
13 #include "PriceCommon.hpp"
14 #include "FlexWallet.hpp"
15 #include "EversConfig.hpp"
16 #include "FlexSalt.hpp"
17 #include "PairCloneEvers.hpp"
18 #include "XchgPair.hpp"
19 #include "immutable_ids.hpp"
20 
21 namespace tvm {
22 
26 __interface IFlexNotify {
28  [[internal]]
30  bool seller_is_taker,
31  address pair,
32  address tip3root_major,
33  address tip3root_minor,
34  Tip3Config major_tip3cfg,
35  Tip3Config minor_tip3cfg,
36  uint128 price_num,
37  uint128 price_denum,
38  uint128 amount
39  ) = 10;
41  [[internal]]
43  bool sell,
44  address tip3root_major,
45  address tip3root_minor,
46  uint128 price_num,
47  uint128 price_denum,
48  uint128 amount,
49  uint128 sum_amount
50  ) = 11;
52  [[internal]]
54  bool sell,
55  address tip3root_major,
56  address tip3root_minor,
57  uint128 price_num,
58  uint128 price_denum,
59  uint128 amount,
60  uint128 sum_amount
61  ) = 12;
62 };
63 using IFlexNotifyPtr = handle<IFlexNotify>;
64 
67  uint256 deployer_pubkey;
69  address_opt owner;
70 };
71 
73 struct FlexDetails {
76  address_opt first_pair;
77  address_opt last_pair;
78  uint32 pairs_count;
79 };
80 
82 struct PairsRange {
83  address_opt first_pair;
84  address_opt last_pair;
85 };
86 
91 __interface IFlex {
92 
94  [[internal, deploy]]
95  resumable<void> onDeploy(
96  uint128 flex_keep_evers,
97  PairCloneEvers evers,
98  address_opt old_flex
99  ) = immutable_ids::flex_on_deploy_id;
100 
102  [[internal, answer_id]]
103  address addXchgPair(
104  PairCloneEvers evers,
105  Tip3Config major_tip3cfg,
106  Tip3Config minor_tip3cfg,
107  uint128 min_amount,
108  uint128 minmove,
109  uint128 price_denum,
110  address notify_addr
111  ) = immutable_ids::flex_register_xchg_pair_id;
112 
114  [[internal]]
116  address pair
117  );
118 
120  [[internal, answer_id]]
121  PairsRange requestPairs() = immutable_ids::flex_request_pairs_id;
122  // ========== getters ==========
123 
125  [[getter]]
127 
129  [[getter]]
131 
133  [[getter]]
134  address getXchgTradingPair(address tip3_major_root, address tip3_minor_root) = 22;
135 
137  [[getter]]
138  uint128 calcLendTokensForOrder(bool sell, uint128 major_tokens, price_t price) = 23;
139 };
140 using IFlexPtr = handle<IFlex>;
141 
143 struct DFlex {
145  // ^^^ Fields defined at deploy (used for address calculation) ^^^
147  optcell xchg_pair_code_;
148  address_opt first_pair_;
149  address_opt last_pair_;
150  uint32 pairs_count_;
152  address_opt it_;
153  address_opt prev_clone_;
154  address_opt next_;
155  address_opt notify_addr_;
156  uint128 min_amount_;
157  uint128 minmove_;
158  uint128 price_denum_;
159  opt<Tip3Config> major_tip3cfg_;
160  opt<Tip3Config> minor_tip3cfg_;
161 };
162 
165 __interface EFlex {
166 };
167 
169 template<>
170 struct preparer<IFlex, DFlex> {
171  __always_inline
172  static std::pair<StateInit, uint256> execute(DFlex data, cell code) {
173  cell data_cl = prepare_persistent_data<IFlex, void>({}, data);
174  StateInit init { {}, {}, code, data_cl, {} };
175  cell init_cl = build(init).make_cell();
176  return { init, uint256(tvm_hash(init_cl)) };
177  }
178 };
179 
180 } // namespace tvm
Flex evers configuration (processing costs)
Flex contract configuration structure (stored in code salt)
Pair clone evers configuration structure.
Price common interfaces and data-structs.
Exchange pair (tip3/tip3) contract interfaces and data-structs. XchgPair - contract defining tip3/tip...
Flex persistent data struct.
Definition: Flex.hpp:143
uint128 minmove_
Minimum move for price.
Definition: Flex.hpp:157
address_opt prev_clone_
Previous pair clone (for cloning)
Definition: Flex.hpp:153
uint128 flex_keep_evers_
Evers to keep in the contract.
Definition: Flex.hpp:151
opt< Tip3Config > major_tip3cfg_
Configuration of the major tip3 token for current pair (for cloning)
Definition: Flex.hpp:159
uint32 exchange_version_
Exchange update group version.
Definition: Flex.hpp:144
opt< Tip3Config > minor_tip3cfg_
Configuration of the minor tip3 token for current pair (for cloning)
Definition: Flex.hpp:160
uint128 min_amount_
Minimum amount of major tokens for a deal or an order.
Definition: Flex.hpp:156
address_opt notify_addr_
Notification address (AMM) (for cloning)
Definition: Flex.hpp:155
address_opt next_
Next XchgPair address (for cloning)
Definition: Flex.hpp:154
address_opt last_pair_
Last XchgPair in linked list.
Definition: Flex.hpp:149
uint128 price_denum_
Price denominator for the pair.
Definition: Flex.hpp:158
address_opt first_pair_
First XchgPair in linked list.
Definition: Flex.hpp:148
address_opt it_
Old pair iterator (for cloning)
Definition: Flex.hpp:152
optcell xchg_pair_code_
XchgPair code (with salt added)
Definition: Flex.hpp:147
int8 workchain_id_
Workchain id.
Definition: Flex.hpp:146
uint32 pairs_count_
Count of XchgPair contracts.
Definition: Flex.hpp:150
Flex events interface.
Definition: Flex.hpp:165
Flex root details (for getter)
Definition: Flex.hpp:73
uint32 pairs_count
Count of XchgPair contracts.
Definition: Flex.hpp:78
address_opt first_pair
First XchgPair in linked list.
Definition: Flex.hpp:76
address_opt last_pair
Last XchgPair in linked list.
Definition: Flex.hpp:77
cell xchg_pair_code
Exchange pair code (XchgPair)
Definition: Flex.hpp:74
uint256 unsalted_price_code_hash
PriceXchg code hash (unsalted)
Definition: Flex.hpp:75
Ownership info for Flex root.
Definition: Flex.hpp:66
address_opt owner
If Flex is managed by other contract (deployer will not be able to execute non-deploy methods)
Definition: Flex.hpp:69
string ownership_description
Ownership description.
Definition: Flex.hpp:68
uint256 deployer_pubkey
Deployer's public key.
Definition: Flex.hpp:67
Flex configuration structure (stored in code salt)
Definition: FlexSalt.hpp:16
Notifications to AMM about orders.
Definition: Flex.hpp:26
void onXchgOrderCanceled(bool sell, address tip3root_major, address tip3root_minor, uint128 price_num, uint128 price_denum, uint128 amount, uint128 sum_amount)
Notification about canceled exchange order (tip3/tip3)
void onXchgOrderAdded(bool sell, address tip3root_major, address tip3root_minor, uint128 price_num, uint128 price_denum, uint128 amount, uint128 sum_amount)
Notification about added exchange order (tip3/tip3)
void onXchgDealCompleted(bool seller_is_taker, address pair, address tip3root_major, address tip3root_minor, Tip3Config major_tip3cfg, Tip3Config minor_tip3cfg, uint128 price_num, uint128 price_denum, uint128 amount)
Notification about completed exchange deal tip3/tip3.
Flex root contract interface. Flex is a root contract for exchange system.
Definition: Flex.hpp:91
address addXchgPair(PairCloneEvers evers, Tip3Config major_tip3cfg, Tip3Config minor_tip3cfg, uint128 min_amount, uint128 minmove, uint128 price_denum, address notify_addr)
Register tip3/tip3 xchg pair (returns pre-calculated address of future xchg pair)
resumable< void > onDeploy(uint128 flex_keep_evers, PairCloneEvers evers, address_opt old_flex)
Constructor of Flex.
FlexSalt getConfig()
Get Flex configuration from code salt.
uint128 calcLendTokensForOrder(bool sell, uint128 major_tokens, price_t price)
Calculate necessary lend tokens for order.
PairsRange requestPairs()
Request first/last pair in list.
void unlistXchgPair(address pair)
Unlist tip3/tip3 xchg pair.
address getXchgTradingPair(address tip3_major_root, address tip3_minor_root)
Get address of tip3/tip3 exchange pair.
FlexDetails getDetails()
Get contract state details.
Evers configuration for pair clone procedure.
Definition: PairCloneEvers.hpp:12
Flex pairs list.
Definition: Flex.hpp:82
address_opt first_pair
First XchgPair in linked list.
Definition: Flex.hpp:83
address_opt last_pair
Last XchgPair in linked list.
Definition: Flex.hpp:84
Rational value - numerator/denominator.
Definition: RationalValue.hpp:12