Advertisement
AmirVagapov

marketplace

Apr 16th, 2024
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. #include "imports/stdlib.fc";
  2. ;; NFT marketplace smart contract v2
  3. ;; Extends wallet v3r2 & adds ability to deploy sales
  4.  
  5. ;;
  6. ;; storage scheme
  7. ;;
  8. ;; storage#_ seqno:uint32 subwallet:uint32 public_key:uint25
  9. ;;           = Storage;
  10. ;;
  11. _ load_data() {
  12.     var ds = get_data().begin_parse();
  13.     return (
  14.         ds~load_uint(32),   ;; seqno
  15.         ds~load_uint(32),   ;; subwallet
  16.         ds~load_uint(256),  ;; public_key
  17.         ds~load_uint(32)    ;; total_count
  18.     );
  19. }
  20.  
  21. () store_data(var data) impure {
  22.     (
  23.         int seqno,
  24.         int subwallet,
  25.         int public_key,
  26.         int total_count
  27.     ) = data;
  28.  
  29.     set_data(
  30.         begin_cell()
  31.             .store_uint(seqno, 32)
  32.             .store_uint(subwallet, 32)
  33.             .store_uint(public_key, 256)
  34.             .store_uint(total_count, 32)
  35.             .end_cell()
  36.     );
  37. }
  38.  
  39. () recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
  40.     if (in_msg_body.slice_empty?()) { ;; ignore empty messages
  41.         return ();
  42.     }
  43.  
  44.     slice cs = in_msg_full.begin_parse();
  45.     int flags = cs~load_uint(4);
  46.  
  47.     if (flags & 1) { ;; ignore all bounced messages
  48.         return ();
  49.     }
  50.     slice sender_address = cs~load_msg_addr();
  51.  
  52.     var (seqno, subwallet, public_key, total_count) = load_data();
  53.  
  54.     int op = in_msg_body~load_uint(32);
  55.  
  56.     if (op == 1) { ;; deploy new signed sale
  57.         var signature = in_msg_body~load_bits(512);
  58.         throw_unless(35, check_signature(slice_hash(in_msg_body), signature, public_key));
  59.  
  60.         (cell state_init, cell body) = (in_msg_body~load_ref(), in_msg_body~load_ref());
  61.  
  62.         int state_init_hash = cell_hash(state_init);
  63.         slice dest_address = begin_cell().store_int(0, 8).store_uint(state_init_hash, 256).end_cell().begin_parse();
  64.  
  65.         var msg = begin_cell()
  66.             .store_uint(0x18, 6)
  67.             .store_uint(4, 3).store_slice(dest_address)
  68.             .store_grams(0)
  69.             .store_uint(4 + 2 + 1, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1)
  70.             .store_ref(state_init)
  71.             .store_ref(body);
  72.  
  73.         send_raw_message(msg.end_cell(), 64); ;; carry remaining value of message
  74.  
  75.         total_count = total_count + 1;
  76.  
  77.         store_data(seqno, subwallet, public_key, total_count);
  78.        
  79.         return ();
  80.     }
  81.  
  82.     return ();
  83. }
  84.  
  85. () recv_external(slice in_msg) impure {
  86.     var signature = in_msg~load_bits(512);
  87.     var cs = in_msg;
  88.     var (subwallet_id, valid_until, msg_seqno) = (cs~load_uint(32), cs~load_uint(32), cs~load_uint(32));
  89.     throw_if(35, valid_until <= now());
  90.     var (seqno, subwallet, public_key, total_count) = load_data();
  91.     throw_unless(33, msg_seqno == seqno);
  92.     throw_unless(34, subwallet_id == subwallet);
  93.     throw_unless(35, check_signature(slice_hash(in_msg), signature, public_key));
  94.     accept_message();
  95.     cs~touch();
  96.     while (cs.slice_refs()) {
  97.         var mode = cs~load_uint(8);
  98.         send_raw_message(cs~load_ref(), mode);
  99.     }
  100.  
  101.     store_data(
  102.         seqno + 1,
  103.         subwallet,
  104.         public_key,
  105.         total_count
  106.     );
  107. }
  108.  
  109. ;; Get methods
  110.  
  111. int seqno() method_id {
  112.     return get_data().begin_parse().preload_uint(32);
  113. }
  114.  
  115. int get_public_key() method_id {
  116.     var cs = get_data().begin_parse();
  117.     cs~load_uint(64);
  118.     return cs.preload_uint(256);
  119. }
  120.  
  121. (int) get_total_count() method_id {
  122.     var (_,_,_, total_count) = load_data();
  123.     return total_count;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement