Flex
Flex exchange system
TONTokenWallet.hpp
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <tvm/schema/message.hpp>
17 #include <tvm/sequence.hpp>
18 #include <tvm/small_dict_map.hpp>
19 
20 #include <tvm/replay_attack_protection/timestamp.hpp>
21 #include <tvm/smart_switcher.hpp>
22 #include <tvm/contract_handle.hpp>
23 
24 #include "FlexLendPayloadArgs.hpp"
25 #include "Tip3Config.hpp"
26 #include "Tip3Creds.hpp"
27 #include "bind_info.hpp"
28 #include "immutable_ids.hpp"
29 
30 namespace tvm {
31 
32 // #define TIP3_ENABLE_EXTERNAL
33 // #define TIP3_ENABLE_LEND_OWNERSHIP
34 // #define TIP3_ENABLE_BURN
35 // #define TIP3_ENABLE_DESTROY
36 // #define TIP3_ONLY_FLEX_EXTERNAL
37 
38 #if defined(TIP3_ENABLE_EXTERNAL) && !defined(TIP3_ONLY_FLEX_EXTERNAL)
39 #define TIP3_EXTERNAL [[external]]
40 #else
41 #define TIP3_EXTERNAL
42 #endif
43 
44 #if defined(TIP3_ENABLE_EXTERNAL) || defined(TIP3_ONLY_FLEX_EXTERNAL)
45 #define FLEX_EXTERNAL [[external]]
46 #else
47 #define FLEX_EXTERNAL
48 #endif
49 
50 static constexpr unsigned TOKEN_WALLET_TIMESTAMP_DELAY = 1800;
51 using wallet_replay_protection_t = replay_attack_protection::timestamp<TOKEN_WALLET_TIMESTAMP_DELAY>;
52 
55  addr_std_fixed dest;
56 };
57 
59 struct lend_owner {
60  uint128 lend_balance;
62  uint256 user_pubkey;
63 };
65 using lend_owners_map = small_dict_map<lend_owner_key, lend_owner>;
66 
70  uint128 lend_balance;
72 };
74 using lend_owners_array = dict_array<lend_owner_array_record>;
75 
77 struct details_info {
78  string name;
79  string symbol;
80  uint8 decimals;
81  uint128 balance;
82  uint256 root_pubkey;
83  address root_address;
84  uint256 wallet_pubkey;
85  address_opt owner_address;
86  opt<uint256> lend_pubkey;
88  uint128 lend_balance;
90  opt<bind_info> binding;
91  uint256 code_hash;
92  uint16 code_depth;
93  int8 workchain_id;
94 };
95 
102 
105  [[internal, answer_id]]
107  uint128 balance,
109  uint32 lend_finish_time,
110  Tip3Creds creds,
111  cell payload,
112  address answer_addr
113  ) = 201;
114 
116  [[internal, answer_id]]
118  uint128 balance,
119  uint128 new_tokens,
120  uint128 evers_balance,
121  Tip3Config tip3cfg,
122  opt<Tip3Creds> sender,
123  Tip3Creds receiver,
124  cell payload,
125  address answer_addr
126  ) = 202;
127 };
128 using ITONTokenWalletNotifyPtr = handle<ITONTokenWalletNotify>;
129 
133 __interface ITONTokenWallet {
134 
136  TIP3_EXTERNAL
137  [[internal, answer_id]]
138  void transfer(
139  address_opt answer_addr,
140  address to,
141  uint128 tokens,
142  uint128 evers,
145  uint128 return_ownership,
146  opt<cell> notify_payload
147  ) = 10;
148 
152  TIP3_EXTERNAL
153  [[internal, answer_id]]
155  address_opt answer_addr,
156  Tip3Creds to,
157  uint128 tokens,
158  uint128 evers,
161  uint128 keep_evers,
162  bool deploy,
165  uint128 return_ownership,
166  opt<cell> notify_payload
167  ) = 11;
168 
170  [[internal, answer_id]]
171  uint128 balance() = 12;
172 
174  [[internal]]
176  uint128 _value,
177  address answer_addr,
178  uint128 keep_evers,
179  opt<cell> notify_payload
180  ) = 0x4384F298;
181 
183  [[internal]]
185  uint128 _value,
186  address answer_addr,
187  uint128 keep_evers,
188  uint256 sender_pubkey,
189  address_opt sender_owner,
190  opt<cell> payload
191  ) = 0x67A0B95F;
192 
193 #ifdef TIP3_ENABLE_DESTROY
196  TIP3_EXTERNAL
197  [[internal]]
198  void destroy(
199  address dest
200  ) = 13;
201 #endif // TIP3_ENABLE_DESTROY
202 
203 #ifdef TIP3_ENABLE_BURN
208  [[internal, answer_id]]
209  void burn(
210  uint256 out_pubkey,
212  address_opt out_owner,
214  opt<cell> notify
216  ) = 14;
217 
222  [[internal, answer_id]]
223  void unwrap(
224  uint256 out_pubkey,
226  address_opt out_owner,
228  uint128 tokens,
229  opt<cell> notify
231  ) = 15;
232 #endif // TIP3_ENABLE_BURN
233 
234 #ifdef TIP3_ENABLE_LEND_OWNERSHIP
238  FLEX_EXTERNAL
239  [[internal, answer_id]]
240  void makeOrder(
241  address_opt answer_addr,
242  uint128 evers,
245  uint128 lend_balance,
246  uint32 lend_finish_time,
247  uint128 price_num,
248  cell unsalted_price_code,
249  cell salt,
250  FlexLendPayloadArgs args
251  ) = 16;
252 
253  FLEX_EXTERNAL
254  [[internal]]
255  void cancelOrder(
256  uint128 evers,
259  address price,
260  bool sell,
261  opt<uint256> order_id
262  ) = 17;
263 
265  [[internal]]
266  void returnOwnership(
267  uint128 tokens
268  ) = 18;
269 
273  [[internal]]
274  void bind(
275  bool set_binding,
276  opt<bind_info> binding,
277  bool set_trader,
278  opt<uint256> trader
279  ) = 19;
280 #endif // TIP3_ENABLE_LEND_OWNERSHIP
281 
283  [[internal, answer_id]]
285 
286  // =============================== getters =============================== //
288  [[getter]]
289  details_info getDetails() = immutable_ids::wallet_get_details_id;
290 
291 #ifdef TIP3_ENABLE_EXTERNAL
293  [[getter]]
294  uint128 getBalance() = 22;
295 #endif // TIP3_ENABLE_EXTERNAL
296 };
297 using ITONTokenWalletPtr = handle<ITONTokenWallet>;
298 
301  string name_;
302  string symbol_;
303  uint8 decimals_;
304  uint128 balance_;
305  uint256 root_pubkey_;
306  address root_address_;
307  uint256 wallet_pubkey_;
308  address_opt owner_address_;
309 #ifdef TIP3_ENABLE_LEND_OWNERSHIP
310  opt<uint256> lend_pubkey_;
311  lend_owners_map lend_owners_;
312  opt<bind_info> binding_;
314 #endif // TIP3_ENABLE_LEND_OWNERSHIP
315  uint256 code_hash_;
316  uint16 code_depth_;
318 };
319 
321  string name_;
322  string symbol_;
323  uint8 decimals_;
324  uint128 balance_;
325  uint256 root_pubkey_;
326  address root_address_;
327  uint256 wallet_pubkey_;
328  address_opt owner_address_;
329  uint256 code_hash_;
330  uint16 code_depth_;
332 };
333 
335  string name_;
336  string symbol_;
337  uint8 decimals_;
338  uint128 balance_;
339  uint256 root_pubkey_;
340  address root_address_;
341  uint256 wallet_pubkey_;
342  address_opt owner_address_;
343  opt<uint256> lend_pubkey_;
345  opt<bind_info> binding_;
347  uint256 code_hash_;
348  uint16 code_depth_;
350 };
351 
355 };
356 
358 inline
360  string name, string symbol, uint8 decimals,
361  uint256 root_pubkey, address root_address,
362  uint256 wallet_pubkey, address_opt wallet_owner,
363  uint256 code_hash, uint16 code_depth, int8 workchain_id
364 ) {
365  return {
366  name, symbol, decimals,
367  uint128(0), root_pubkey, root_address,
368  wallet_pubkey, wallet_owner,
369 #ifdef TIP3_ENABLE_LEND_OWNERSHIP
370  {}, {}, {},
371 #endif
372  code_hash, code_depth,
373  workchain_id
374  };
375 }
376 
379 __always_inline
381  string name, string symbol, uint8 decimals,
382  uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner,
383  uint256 code_hash, uint16 code_depth, int8 workchain_id
384 ) {
385  DTONTokenWallet wallet_data =
386  prepare_wallet_data(name, symbol, decimals, root_pubkey, root_address, wallet_pubkey, wallet_owner,
387  code_hash, code_depth, workchain_id);
388  auto init_hdr = persistent_data_header<ITONTokenWallet, wallet_replay_protection_t>::init();
389  cell data_cl = prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t>(init_hdr, wallet_data);
390  return tvm_state_init_hash(code_hash, uint256(tvm_hash(data_cl)), code_depth, uint16(data_cl.cdepth()));
391 }
392 
395 __always_inline
397  string name, string symbol, uint8 decimals,
398  uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner,
399  uint256 code_hash, uint16 code_depth, int8 workchain_id
400 ) {
401  DTONTokenWalletExternal wallet_data {
402  name, symbol, decimals,
403  uint128(0), root_pubkey, root_address, wallet_pubkey, wallet_owner,
404  code_hash, code_depth, workchain_id
405  };
406  auto init_hdr = persistent_data_header<ITONTokenWallet, wallet_replay_protection_t>::init();
407  cell data_cl = prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t>(init_hdr, wallet_data);
408  return tvm_state_init_hash(code_hash, uint256(tvm_hash(data_cl)), code_depth, uint16(data_cl.cdepth()));
409 }
410 
413 __always_inline
415  Tip3Config tip3cfg, uint256 wallet_pubkey, address_opt wallet_owner,
416  uint256 code_hash, uint16 code_depth, int8 workchain_id
417 ) {
418  DTONTokenWalletInternal wallet_data {
419  tip3cfg.name, tip3cfg.symbol, tip3cfg.decimals,
420  uint128(0), tip3cfg.root_pubkey, tip3cfg.root_address, wallet_pubkey, wallet_owner,
421  {}, {}, {}, code_hash, code_depth, workchain_id
422  };
423  auto init_hdr = persistent_data_header<ITONTokenWallet, wallet_replay_protection_t>::init();
424  cell data_cl = prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t>(init_hdr, wallet_data);
425  return tvm_state_init_hash(code_hash, uint256(tvm_hash(data_cl)), code_depth, uint16(data_cl.cdepth()));
426 }
427 
430 inline
431 std::pair<StateInit, uint256> prepare_wallet_state_init_and_addr(DTONTokenWallet wallet_data, cell code) {
432  auto init_hdr = persistent_data_header<ITONTokenWallet, wallet_replay_protection_t>::init();
433  cell wallet_data_cl =
434  prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t>(init_hdr, wallet_data);
435  StateInit wallet_init {
436  /*split_depth*/{}, /*special*/{},
437  code, wallet_data_cl, /*library*/{}
438  };
439  cell wallet_init_cl = build(wallet_init).make_cell();
440  return { wallet_init, uint256(tvm_hash(wallet_init_cl)) };
441 }
442 
445 inline
446 std::pair<StateInit, uint256> prepare_external_wallet_state_init_and_addr(
447  string name, string symbol, uint8 decimals,
448  uint256 root_pubkey, address root_address,
449  uint256 wallet_pubkey, address_opt wallet_owner,
450  uint256 code_hash, uint16 code_depth,
451  int8 workchain_id, cell code
452 ) {
453  DTONTokenWalletExternal wallet_data {
454  name, symbol, decimals,
455  uint128(0), root_pubkey, root_address, wallet_pubkey, wallet_owner,
456  code_hash, code_depth, workchain_id
457  };
458  cell wallet_data_cl =
459  prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t, DTONTokenWalletExternal>(
460  wallet_replay_protection_t::init(), wallet_data);
461  StateInit wallet_init {
462  /*split_depth*/{}, /*special*/{},
463  code, wallet_data_cl, /*library*/{}
464  };
465  cell wallet_init_cl = build(wallet_init).make_cell();
466  return { wallet_init, uint256(tvm_hash(wallet_init_cl)) };
467 }
468 
471 inline
472 std::pair<StateInit, uint256> prepare_internal_wallet_state_init_and_addr(
473  string name, string symbol, uint8 decimals,
474  uint256 root_pubkey, address root_address,
475  uint256 wallet_pubkey, address_opt wallet_owner,
476  uint256 code_hash, uint16 code_depth,
477  int8 workchain_id, cell code
478 ) {
479  DTONTokenWalletInternal wallet_data {
480  name, symbol, decimals,
481  uint128(0), root_pubkey, root_address, wallet_pubkey, wallet_owner,
482  {}, {}, {}, code_hash, code_depth, workchain_id
483  };
484  cell wallet_data_cl =
485  prepare_persistent_data<ITONTokenWallet, wallet_replay_protection_t, DTONTokenWalletInternal>(
486  wallet_replay_protection_t::init(), wallet_data
487  );
488  StateInit wallet_init {
489  /*split_depth*/{}, /*special*/{},
490  code, wallet_data_cl, /*library*/{}
491  };
492  cell wallet_init_cl = build(wallet_init).make_cell();
493  return { wallet_init, uint256(tvm_hash(wallet_init_cl)) };
494 }
495 
496 } // namespace tvm
497 
small_dict_map< lend_owner_key, lend_owner > lend_owners_map
Lend owners (contracts) map.
Definition: TONTokenWallet.hpp:65
__always_inline uint256 calc_wallet_init_hash(string name, string symbol, uint8 decimals, uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id)
Definition: TONTokenWallet.hpp:380
std::pair< StateInit, uint256 > prepare_external_wallet_state_init_and_addr(string name, string symbol, uint8 decimals, uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id, cell code)
Definition: TONTokenWallet.hpp:446
__always_inline uint256 calc_int_wallet_init_hash(Tip3Config tip3cfg, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id)
Definition: TONTokenWallet.hpp:414
std::pair< StateInit, uint256 > prepare_internal_wallet_state_init_and_addr(string name, string symbol, uint8 decimals, uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id, cell code)
Definition: TONTokenWallet.hpp:472
dict_array< lend_owner_array_record > lend_owners_array
Lend ownership array.
Definition: TONTokenWallet.hpp:74
std::pair< StateInit, uint256 > prepare_wallet_state_init_and_addr(DTONTokenWallet wallet_data, cell code)
Definition: TONTokenWallet.hpp:431
DTONTokenWallet prepare_wallet_data(string name, string symbol, uint8 decimals, uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id)
Prepare TONTokenWallet persistent data struct.
Definition: TONTokenWallet.hpp:359
__always_inline uint256 calc_ext_wallet_init_hash(string name, string symbol, uint8 decimals, uint256 root_pubkey, address root_address, uint256 wallet_pubkey, address_opt wallet_owner, uint256 code_hash, uint16 code_depth, int8 workchain_id)
Definition: TONTokenWallet.hpp:396
Definition: TONTokenWallet.hpp:320
address_opt owner_address_
Owner contract address for internal ownership.
Definition: TONTokenWallet.hpp:328
string symbol_
Token short symbol.
Definition: TONTokenWallet.hpp:322
uint256 root_pubkey_
Public key of the related RootTokenContract.
Definition: TONTokenWallet.hpp:325
string name_
Token name.
Definition: TONTokenWallet.hpp:321
uint128 balance_
Token balance of the wallet.
Definition: TONTokenWallet.hpp:324
uint16 code_depth_
Tip3 wallet code depth to verify other wallets.
Definition: TONTokenWallet.hpp:330
uint256 code_hash_
Tip3 wallet code hash to verify other wallets.
Definition: TONTokenWallet.hpp:329
int8 workchain_id_
Workchain id.
Definition: TONTokenWallet.hpp:331
uint256 wallet_pubkey_
Public key of wallet owner.
Definition: TONTokenWallet.hpp:327
uint8 decimals_
Decimals for ui purposes. ex: balance 100 with decimals 2 will be printed as 1.00.
Definition: TONTokenWallet.hpp:323
address root_address_
Address of the related RootTokenContract.
Definition: TONTokenWallet.hpp:326
Definition: TONTokenWallet.hpp:334
address_opt owner_address_
Owner contract address for internal ownership.
Definition: TONTokenWallet.hpp:342
string symbol_
Token short symbol.
Definition: TONTokenWallet.hpp:336
uint256 root_pubkey_
Public key of the related RootTokenContract.
Definition: TONTokenWallet.hpp:339
opt< uint256 > lend_pubkey_
Lend ownership pubkey.
Definition: TONTokenWallet.hpp:343
string name_
Token name.
Definition: TONTokenWallet.hpp:335
opt< bind_info > binding_
Definition: TONTokenWallet.hpp:345
uint128 balance_
Token balance of the wallet.
Definition: TONTokenWallet.hpp:338
uint16 code_depth_
Tip3 wallet code depth to verify other wallets.
Definition: TONTokenWallet.hpp:348
uint256 code_hash_
Tip3 wallet code hash to verify other wallets.
Definition: TONTokenWallet.hpp:347
lend_owners_map lend_owners_
Lend ownership map (service owner => lend_owner).
Definition: TONTokenWallet.hpp:344
int8 workchain_id_
Workchain id.
Definition: TONTokenWallet.hpp:349
uint256 wallet_pubkey_
Public key of wallet owner.
Definition: TONTokenWallet.hpp:341
uint8 decimals_
Decimals for ui purposes. ex: balance 100 with decimals 2 will be printed as 1.00.
Definition: TONTokenWallet.hpp:337
address root_address_
Address of the related RootTokenContract.
Definition: TONTokenWallet.hpp:340
TONTokenWallet persistent data struct.
Definition: TONTokenWallet.hpp:300
address_opt owner_address_
Owner contract address for internal ownership.
Definition: TONTokenWallet.hpp:308
string symbol_
Token short symbol.
Definition: TONTokenWallet.hpp:302
uint256 root_pubkey_
Public key of the related RootTokenContract.
Definition: TONTokenWallet.hpp:305
string name_
Token name.
Definition: TONTokenWallet.hpp:301
uint128 balance_
Token balance of the wallet.
Definition: TONTokenWallet.hpp:304
uint16 code_depth_
Tip3 wallet code depth to verify other wallets.
Definition: TONTokenWallet.hpp:316
uint256 code_hash_
Tip3 wallet code hash to verify other wallets.
Definition: TONTokenWallet.hpp:315
int8 workchain_id_
Workchain id.
Definition: TONTokenWallet.hpp:317
uint256 wallet_pubkey_
Public key of wallet owner.
Definition: TONTokenWallet.hpp:307
uint8 decimals_
Decimals for ui purposes. ex: balance 100 with decimals 2 will be printed as 1.00.
Definition: TONTokenWallet.hpp:303
address root_address_
Address of the related RootTokenContract.
Definition: TONTokenWallet.hpp:306
TONTokenWallet events interface.
Definition: TONTokenWallet.hpp:354
TON Token wallet notification callback interface.
Definition: TONTokenWallet.hpp:101
bool onTip3Transfer(uint128 balance, uint128 new_tokens, uint128 evers_balance, Tip3Config tip3cfg, opt< Tip3Creds > sender, Tip3Creds receiver, cell payload, address answer_addr)
Notification from tip3 wallet to its owner contract about received tokens transfer.
bool onTip3LendOwnership(uint128 balance, uint32 lend_finish_time, Tip3Creds creds, cell payload, address answer_addr)
TON Token wallet contract interface.
Definition: TONTokenWallet.hpp:133
TIP3_EXTERNAL void transfer(address_opt answer_addr, address to, uint128 tokens, uint128 evers, uint128 return_ownership, opt< cell > notify_payload)
Transfer tokens to another tip3 wallet contract.
details_info details()
Request info about contract state details (from another contract).
void acceptMint(uint128 _value, address answer_addr, uint128 keep_evers, opt< cell > notify_payload)=0x4384F298
Receive tokens from root (RootTokenContract).
uint128 balance()
Request wallet token balance using internal message (contract-to-contract).
void acceptTransfer(uint128 _value, address answer_addr, uint128 keep_evers, uint256 sender_pubkey, address_opt sender_owner, opt< cell > payload)=0x67A0B95F
Receive tokens from another tip3 wallet.
details_info getDetails()
Get info about contract state details.
TIP3_EXTERNAL void transferToRecipient(address_opt answer_addr, Tip3Creds to, uint128 tokens, uint128 evers, uint128 keep_evers, bool deploy, uint128 return_ownership, opt< cell > notify_payload)
TONTokenWallet details info (for getter).
Definition: TONTokenWallet.hpp:77
uint8 decimals
Decimals for ui purposes. ex: balance 100 with decimals 2 will be printed as 1.00.
Definition: TONTokenWallet.hpp:80
uint16 code_depth
Tip3 wallet code depth to verify other wallets.
Definition: TONTokenWallet.hpp:92
lend_owners_array lend_owners
All lend ownership records of the contract.
Definition: TONTokenWallet.hpp:87
address_opt owner_address
Owner contract address for internal ownership, will be 0:0..0 otherwise.
Definition: TONTokenWallet.hpp:85
uint256 wallet_pubkey
Public key of wallet owner (User id for FlexWallet).
Definition: TONTokenWallet.hpp:84
uint256 root_pubkey
Public key of the related RootTokenContract.
Definition: TONTokenWallet.hpp:82
address root_address
Address of the related RootTokenContract.
Definition: TONTokenWallet.hpp:83
string name
Token name.
Definition: TONTokenWallet.hpp:78
opt< bind_info > binding
Flex binding info.
Definition: TONTokenWallet.hpp:90
string symbol
Token short symbol.
Definition: TONTokenWallet.hpp:79
uint128 lend_balance
Definition: TONTokenWallet.hpp:88
opt< uint256 > lend_pubkey
Lend ownership pubkey.
Definition: TONTokenWallet.hpp:86
uint256 code_hash
Tip3 wallet code hash to verify other wallets.
Definition: TONTokenWallet.hpp:91
uint128 balance
Token balance of the wallet.
Definition: TONTokenWallet.hpp:81
int8 workchain_id
Workchain id.
Definition: TONTokenWallet.hpp:93
Lend ownership array record (for usage in getter).
Definition: TONTokenWallet.hpp:68
uint32 lend_finish_time
Lend ownership finish time.
Definition: TONTokenWallet.hpp:71
lend_owner_key lend_key
Lend ownership key (destination address + user id).
Definition: TONTokenWallet.hpp:69
uint128 lend_balance
Lend ownership balance.
Definition: TONTokenWallet.hpp:70
Complex key for lend owners mapping.
Definition: TONTokenWallet.hpp:54
addr_std_fixed dest
Destination contract address.
Definition: TONTokenWallet.hpp:55
Lend ownership record (for usage in address->lend_owner_record map).
Definition: TONTokenWallet.hpp:59
uint32 lend_finish_time
Lend ownership finish time.
Definition: TONTokenWallet.hpp:61
uint128 lend_balance
Lend ownership balance.
Definition: TONTokenWallet.hpp:60
uint256 user_pubkey
Lend pubkey of the user ordered to lend ownership to the address.
Definition: TONTokenWallet.hpp:62