Ton

Форк
0
122 строки · 4.6 Кб
1
;; Jettons minter smart contract
2

3
;; storage scheme
4
;; storage#_ total_supply:Coins admin_address:MsgAddress content:^Cell jetton_wallet_code:^Cell = Storage;
5

6
#include "imports/stdlib.fc";
7
#include "imports/params.fc";
8
#include "imports/constants.fc";
9
#include "imports/jetton-utils.fc";
10
#include "imports/op-codes.fc";
11
#include "imports/utils.fc";
12
#pragma version >=0.2.0;
13

14
(int, slice, cell, cell) load_data() inline {
15
  slice ds = get_data().begin_parse();
16
  return (
17
      ds~load_coins(), ;; total_supply
18
      ds~load_msg_addr(), ;; admin_address
19
      ds~load_ref(), ;; content
20
      ds~load_ref()  ;; jetton_wallet_code
21
  );
22
}
23

24
() save_data(int total_supply, slice admin_address, cell content, cell jetton_wallet_code) impure inline {
25
  set_data(begin_cell()
26
            .store_coins(total_supply)
27
            .store_slice(admin_address)
28
            .store_ref(content)
29
            .store_ref(jetton_wallet_code)
30
           .end_cell()
31
          );
32
}
33

34
() mint_tokens(slice to_address, cell jetton_wallet_code, int amount, cell master_msg) impure {
35
  cell state_init = calculate_jetton_wallet_state_init(to_address, my_address(), jetton_wallet_code);
36
  slice to_wallet_address = calculate_jetton_wallet_address(state_init);
37
  var msg = begin_cell()
38
    .store_uint(0x18, 6)
39
    .store_slice(to_wallet_address)
40
    .store_coins(amount)
41
    .store_uint(4 + 2 + 1, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1)
42
    .store_ref(state_init)
43
    .store_ref(master_msg);
44
  send_raw_message(msg.end_cell(), 1); ;; pay transfer fees separately, revert on errors
45
}
46

47
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
48
    if (in_msg_body.slice_empty?()) { ;; ignore empty messages
49
        return ();
50
    }
51
    slice cs = in_msg_full.begin_parse();
52
    int flags = cs~load_uint(4);
53

54
    if (flags & 1) { ;; ignore all bounced messages
55
        return ();
56
    }
57
    slice sender_address = cs~load_msg_addr();
58
  
59
    int op = in_msg_body~load_uint(32);
60
    int query_id = in_msg_body~load_uint(64);
61

62
    (int total_supply, slice admin_address, cell content, cell jetton_wallet_code) = load_data();
63

64
    if (op == op::mint()) {
65
        throw_unless(73, equal_slices(sender_address, admin_address));
66
        slice to_address = in_msg_body~load_msg_addr();
67
        int amount = in_msg_body~load_coins();
68
        cell master_msg = in_msg_body~load_ref();
69
        slice master_msg_cs = master_msg.begin_parse();
70
        master_msg_cs~skip_bits(32 + 64); ;; op + query_id
71
        int jetton_amount = master_msg_cs~load_coins();
72
        mint_tokens(to_address, jetton_wallet_code, amount, master_msg);
73
        save_data(total_supply + jetton_amount, admin_address, content, jetton_wallet_code);
74
        return ();
75
    }
76

77
    if (op == op::burn_notification()) {
78
        int jetton_amount = in_msg_body~load_coins();
79
        slice from_address = in_msg_body~load_msg_addr();
80
        throw_unless(74,
81
            equal_slices(calculate_user_jetton_wallet_address(from_address, my_address(), jetton_wallet_code), sender_address)
82
        );
83
        save_data(total_supply - jetton_amount, admin_address, content, jetton_wallet_code);
84
        slice response_address = in_msg_body~load_msg_addr();
85
        if (response_address.preload_uint(2) != 0) {
86
          var msg = begin_cell()
87
            .store_uint(0x10, 6) ;; nobounce - int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool src:MsgAddress -> 011000
88
            .store_slice(response_address)
89
            .store_coins(0)
90
            .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
91
            .store_uint(op::excesses(), 32)
92
            .store_uint(query_id, 64);
93
          send_raw_message(msg.end_cell(), 2 + 64);
94
        }
95
        return ();
96
    }
97

98
    if (op == 3) { ;; change admin
99
        throw_unless(73, equal_slices(sender_address, admin_address));
100
        slice new_admin_address = in_msg_body~load_msg_addr();
101
        save_data(total_supply, new_admin_address, content, jetton_wallet_code);
102
        return ();
103
    }
104

105
    if (op == 4) { ;; change content, delete this for immutable tokens
106
        throw_unless(73, equal_slices(sender_address, admin_address));
107
        save_data(total_supply, admin_address, in_msg_body~load_ref(), jetton_wallet_code);
108
        return ();
109
    }
110

111
    throw(0xffff);
112
}
113

114
(int, int, slice, cell, cell) get_jetton_data() method_id {
115
    (int total_supply, slice admin_address, cell content, cell jetton_wallet_code) = load_data();
116
    return (total_supply, -1, admin_address, content, jetton_wallet_code);
117
}
118

119
slice get_wallet_address(slice owner_address) method_id {
120
    (int total_supply, slice admin_address, cell content, cell jetton_wallet_code) = load_data();
121
    return calculate_user_jetton_wallet_address(owner_address, my_address(), jetton_wallet_code);
122
}
123

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.