forked from EmelyanenkoK/toncoin-oraclized-bridge
-
Notifications
You must be signed in to change notification settings - Fork 12
/
bridge_code.fc
168 lines (140 loc) · 6.42 KB
/
bridge_code.fc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(slice, (int, int, int)) load_fees(slice s) inline {
var fees = (s~load_grams(), s~load_grams(), s~load_uint(14));
return (s, fees);
}
builder store_fees(builder b, (int, int, int) fees) inline {
(int flat_reward, int network_fee, int factor) = fees;
throw_if(391, factor > 10000);
return b.store_grams(flat_reward).store_grams(network_fee).store_uint(factor, 14);
}
(int, int, slice, (int, int, int)) load_data() inline_ref {
var ds = get_data().begin_parse();
return (ds~load_uint(8), ds~load_grams(), ds~load_msg_addr(), ds~load_fees());
}
() save_data(int state_flags, int total_locked, slice collector_address, fees) impure inline_ref {
var st = begin_cell().store_uint(state_flags, 8) ;; 0 - TON->EVM transfers enabled; 1 - TON->EVM transfers disabled
.store_grams(total_locked)
.store_slice(collector_address)
.store_fees(fees)
.end_cell();
set_data(st);
}
int calculate_fee(int msg_value, fees) {
(int flat_reward, int network_fee, int factor) = fees;
int remain = msg_value - flat_reward - network_fee;
throw_unless(400, remain > 0);
int percent_fee = remain * factor / 10000;
return flat_reward + network_fee + percent_fee;
}
;; create swap to external chain to destination address
() create_swap_from_ton(int destination_address, int amount, slice s_addr, int query_id, int is_text) impure {
(int state_flags, int total_locked, slice collector_address, var fees) = load_data();
throw_if(339, state_flags & 1);
int fee = calculate_fee(amount, fees);
amount -= fee;
throw_unless(306, amount > 0);
total_locked += amount;
emit_log_simple(0xc0470ccf, begin_cell().store_uint(destination_address, 160).store_uint(amount, 64).end_cell().begin_parse());
save_data(state_flags, total_locked, collector_address, fees);
if(is_text) {
return send_text_receipt_message(s_addr, 100000000, 3);
} else {
return send_receipt_message(s_addr, 0x10000 + 3, query_id, 0, 100000000, 3);
}
}
() process_comment_api_request (slice in_msg, int msg_value, slice s_addr) impure {
if (in_msg.slice_empty?() & (in_msg.slice_refs() == 1)) {
cell _cont = in_msg~load_ref();
in_msg = _cont.begin_parse();
}
int command = in_msg~load_uint(56);
throw_unless(328, command == 32500882701840163); ;; "swapTo#" other commands are unsupported
int destination_address = in_msg~load_text_hex_number(20);
in_msg.end_parse();
return create_swap_from_ton(destination_address, msg_value, s_addr, 0, true);
}
() execute_voting (slice s_addr, slice voting_data, int bridge_address) impure {
(int state_flags, int total_locked, slice collector_address, var fees) = load_data();
int operation = voting_data~load_uint(8);
if (operation == 0) { ;; swap
(int ext_chain_hash,
int internal_index,
int wc,
int addr_hash,
int swap_amount) = (voting_data~load_uint(256),
voting_data~load_int(16),
voting_data~load_int(8),
voting_data~load_uint(256),
voting_data~load_uint(64));
int fee = calculate_fee(swap_amount, fees);
total_locked -= swap_amount;
throw_unless(315, total_locked >= 0);
swap_amount -= fee;
var msg = begin_cell()
.store_uint(0x10, 6) ;; non-bounceable
.store_uint(4, 3).store_int(wc, 8).store_uint(addr_hash, 256)
.store_grams(swap_amount)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(ext_chain_hash, 256)
.store_uint(internal_index, 16);
send_raw_message(msg.end_cell(), 0);
} elseif (operation == 3) { ;; update state flags
state_flags = voting_data~load_uint(8);
} elseif (operation == 4) { ;; migrate
var msg = begin_cell()
.store_uint(0x18, 6) ;; bounceable
.store_uint(4, 3).store_int(-1, 8).store_uint(bridge_address, 256)
.store_grams(total_locked)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0xf00d, 32); ;; new bridge contract should recognize this opcode
send_raw_message(msg.end_cell(), 1);
} elseif (operation == 5) { ;; get reward
;; reserve total_locked + 100 Toncoins for storage fees
raw_reserve(total_locked + 100000000000, 2);
var msg = begin_cell()
.store_uint(0x18, 6) ;; bounceable
.store_slice(s_addr)
.store_grams(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1);
send_raw_message(msg.end_cell(), 128); ;; send all the remainings
} elseif (operation == 6) { ;; change fees
fees = voting_data~load_fees();
} elseif (operation == 7) { ;; change collector
collector_address = voting_data~load_msg_addr();
}
save_data(state_flags, total_locked, collector_address, fees);
}
() recv_internal(int msg_value, cell in_msg_cell, slice in_msg) impure {
var cs = in_msg_cell.begin_parse();
var flags = cs~load_uint(4); ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
if (flags & 1) {
;; ignore all bounced messages
return ();
}
slice s_addr = cs~load_msg_addr();
throw_if(299, in_msg.slice_empty?()); ;; inbound message has empty body
int op = in_msg~load_uint(32);
(int bridge_address, int oracles_address, _) = get_bridge_config();
(int wc, int addr) = parse_std_addr(s_addr);
var is_oracles = (wc == -1) & (oracles_address == addr);
if (op == 4) {
throw_unless(305, is_oracles);
return execute_voting(s_addr, in_msg, bridge_address);
}
throw_if(306, is_oracles); ;; don't allow to create swaps from oracles multisig
if (op == 0) {
return process_comment_api_request(in_msg, msg_value, s_addr);
}
int query_id = in_msg~load_uint(64);
if (op == 3) {
int destination_address = in_msg~load_uint(160);
return create_swap_from_ton(destination_address, msg_value, s_addr, query_id, false);
}
}
;; get methods
_ get_bridge_data() method_id {
(int state_flags, int total_locked, slice collector_address_slice, var fees) = load_data();
(int flat_reward, int network_fee, int factor) = fees;
(int wc, int addr) = parse_std_addr(collector_address_slice);
return (state_flags, total_locked, wc, addr, flat_reward, network_fee, factor, 10000);
}