forked from cod1ng-studio/TRC20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributed-token.fc
More file actions
150 lines (129 loc) · 5.09 KB
/
distributed-token.fc
File metadata and controls
150 lines (129 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
int equal_slices (slice s1, slice s2) asm "SDEQ";
int op::request_transfer() asm "0x11 PUSHINT";
int op::transfer_notification() asm "0x12 PUSHINT";
int op::transfer() asm "0x14 PUSHINT";
int op::admin_request() asm "0x15 PUSHINT";
int op::balance_request() asm "0x16 PUSHINT";
int workchain() asm "0 PUSHINT";
int gas_consumption() asm "10000000 PUSHINT";
(int, int, slice, cell) load_data () {
slice ds = get_data().begin_parse();
return (ds~load_grams(), ds~load_uint(256), ds~load_msg_addr(), ds~load_ref());
}
cell pack_data (int balance, int token_id, slice owner, cell token_wallet_code) inline {
return begin_cell()
.store_grams(balance)
.store_uint(token_id, 256)
.store_slice(owner)
.store_ref(token_wallet_code)
.end_cell();
}
() save_data (int balance, int token_id, slice owner, cell token_wallet_code) impure {
set_data(pack_data(balance, token_id, owner, token_wallet_code));
}
cell calculate_token_state_init (int token_id, slice owner, cell code) inline {
return begin_cell()
.store_uint(0,2)
.store_dict(code)
.store_dict(pack_data(0, token_id, owner, code))
.store_uint(0,1)
.end_cell();
}
slice calc_address(cell state_init) inline {
return begin_cell().store_uint(4, 3)
.store_int(workchain(), 8)
.store_uint(
cell_hash(state_init), 256)
.end_cell()
.begin_parse();
}
(slice) calc_user_wallet (int token_id, slice owner, cell code) inline {
return calc_address(calculate_token_state_init(token_id, owner, code));
}
() send_tokens (int token_value, int token_id, slice to, cell code, slice from,
int ton_amount, int forward_amount, cell payload) impure {
cell state_init = calculate_token_state_init(token_id, to, code);
slice to_wallet = calc_address(state_init);
var msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(to_wallet)
.store_grams(ton_amount)
.store_uint(4 + 2 + 0, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1)
.store_ref(state_init)
.store_uint(op::transfer(), 32)
.store_grams(token_value)
.store_slice(from)
.store_grams(forward_amount)
.store_dict(payload);
return send_raw_message(msg.end_cell(), 1); ;; paying fees, revert on errors
}
() on_bounce (slice in_msg) impure {
in_msg~skip_bits(32); ;; 0xFFFFFFFF
var (balance, token_id, owner, token_wallet_code) = load_data();
int op = in_msg~load_uint(32);
throw_unless(709, op == op::transfer());
int value = in_msg~load_grams();
balance += value;
return save_data(balance, token_id, owner, token_wallet_code);
}
() on_receiving (owner, from, amount, int forward_tons, cell payload) impure {
;; actions here MUST be non-bouncable and SHOULD throw on error
var msg = begin_cell()
.store_uint(0x10, 6)
.store_slice(owner)
.store_grams(forward_tons)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::transfer_notification(), 32)
.store_grams(amount)
.store_slice(from)
.store_dict(payload);
return send_raw_message(msg.end_cell(), 1); ;; paying fees, revert on errors
}
() on_sending (to, amount) impure {
;; actions here MUST be non-bouncable and SHOULD throw on error
;; for instance we can reserve here 1TON and return excesses to owner
}
() recv_internal(int msg_value, cell in_msg_cell, slice in_msg) impure {
slice cs = in_msg_cell.begin_parse();
int flags = cs~load_uint(4);
if (flags & 1) {
return on_bounce(in_msg);
}
slice sender = cs~load_msg_addr();
var (balance, token_id, owner, token_wallet_code) = load_data();
int op = in_msg~load_uint(32);
if(op == op::balance_request()) { ;; withdraw TON balance
;;TODO
}
if(op == op::admin_request()) { ;; token owner command
cell preimage = begin_cell().store_slice(sender).store_slice(in_msg~load_bits(256)).end_cell();
throw_unless(708, token_id == cell_hash(preimage));
return set_data(in_msg~load_ref());
}
if(op == op::request_transfer()) { ;; outgoing transfer
throw_unless(705, equal_slices(owner, sender));
int value = in_msg~load_grams();
throw_unless(706, balance >= value);
balance -= value;
slice to = in_msg~load_msg_addr();
int ton_amount = in_msg~load_grams();
int forward_amount = in_msg~load_grams();
cell payload = in_msg~load_dict();
send_tokens(value, token_id, to, token_wallet_code, owner, ton_amount, forward_amount, payload);
on_sending(to, value);
}
if(op == op::transfer()) { ;; incoming transfer
int value = in_msg~load_grams();
slice from = in_msg~load_msg_addr();
throw_unless(707,
equal_slices(calc_user_wallet(token_id, from, token_wallet_code), sender)
);
;; At this point we sure that got message from actor authorized by it's code
balance += value;
int forward_amount = in_msg~load_grams();
cell payload = in_msg~load_dict();
raw_reserve(pair_first(get_balance()) - msg_value + gas_consumption(), 2);
on_receiving(owner, from, value, forward_amount, payload);
}
return save_data(balance, token_id, owner, token_wallet_code);
}