-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
func_exchange.php
199 lines (179 loc) · 9.13 KB
/
func_exchange.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
function exchangeStart(){
global $chat_id, $link;
clean_temp_sess();
$i = 0;
$c = 0;
$row = getRowUsers();
if($row->ton_ton_full > 0.1){
$arInfo["inline_keyboard"][$i][0]["callback_data"] = 60;
$arInfo["inline_keyboard"][$i][0]["text"] = "TON";
#$i++;
$c++;
}
if(($row->tgr_ton_full + $row->tgr_bep20) > 0){
$arInfo["inline_keyboard"][$i][$c]["callback_data"] = 61;
$arInfo["inline_keyboard"][$i][$c]["text"] = "TGR";
$i++;
}
$arInfo["inline_keyboard"][$i][0]["callback_data"] = 15;
$arInfo["inline_keyboard"][$i][0]["text"] = "⏪ Назад в меню";
send($chat_id, "В этом разделе ты можешь обменять один из своих крипто-активов на другой.
<i>Комиссия за обмен: 0.5%</i>
Выбери криптовалюту:", $arInfo);
}
function exchangeChooseDirection($source){
global $chat_id, $link;
$assets = array("TON","TGR");
$c = 0;
for ($i = 0; $i < count($assets); $i++) {
if($assets[$i] == $source) continue;
$arInfo["inline_keyboard"][0][$c]["callback_data"] = "exc|$source|$assets[$i]";
$arInfo["inline_keyboard"][0][$c]["text"] = $assets[$i];
$c++;
} // end FOR
$arInfo["inline_keyboard"][1][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][1][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "Выбери направление обмена:", $arInfo);
}
function exchangeRequestSum($source,$direction){
global $chat_id, $link, $exchangefee;
$row = getRowUsers();
if($source == "TON"){
$maxsum = $row->ton_ton_full - 0.1;
}elseif($source == "TGR"){
$maxsum = $row->tgr_ton_full + $row->tgr_bep20;
}
$maxsum = $maxsum - $maxsum * $exchangefee;
clean_temp_sess();
save2temp("action", "exs|$source|$direction|$maxsum");
$arInfo["inline_keyboard"][0][0]["callback_data"] = "exsmax";
$arInfo["inline_keyboard"][0][0]["text"] = "Макс.: $maxsum $source";
$arInfo["inline_keyboard"][1][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][1][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "Укажи сумму в $source для обмена на $direction:", $arInfo);
}
function exchangePreProcessSum(){
global $chat_id, $link;
$str5select = "SELECT `action` FROM `temp_sess` WHERE `chatid`='$chat_id' ORDER BY `rowid` DESC LIMIT 1";
$result5 = mysqli_query($link, $str5select);
$row5 = @mysqli_fetch_object($result5);
if(preg_match("/exs\|/", $row5->action)){
exchangeProcessSum(-1000, $row5);
}else{
$arInfo["inline_keyboard"][0][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "❌ОШИБКА! Произошла неизвестная ошибка. Повтори попытку.", $arInfo);
}
}
function exchangeProcessSum($sum, $row5){
global $chat_id, $link;
$r = explode("|", $row5->action);
$source = $r[1];
$direction = $r[2];
if($sum == -1000) $sum = $r[3];
$maxsum = $r[3];
if($sum > $maxsum) {
$arInfo["inline_keyboard"][0][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "❌ОШИБКА! Сумма обмена ($sum $source) превышает сумму на балансе ($maxsum $source). Повтори попытку.", $arInfo);
}
elseif($sum <= 0){
$arInfo["inline_keyboard"][0][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "❌ОШИБКА! Введенное значение не похоже на правильную сумму. Повтори попытку.", $arInfo);
}else{
$arInfo["inline_keyboard"][0][0]["callback_data"] = "exf|$sum";
$arInfo["inline_keyboard"][0][0]["text"] = "✅ Обменять";
$arInfo["inline_keyboard"][0][1]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][1]["text"] = "⛔️ Отменить";
$arInfo["inline_keyboard"][1][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][1][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "Информация по обмену:
Обмен <b>$sum $source на $direction</b>
Подтверди операцию:", $arInfo);
}
}
function exchangeFinish($sum){
global $chat_id, $link, $exchangefee;
$str5select = "SELECT `action` FROM `temp_sess` WHERE `chatid`='$chat_id' ORDER BY `rowid` DESC LIMIT 1";
$result5 = mysqli_query($link, $str5select);
$row5 = @mysqli_fetch_object($result5);
$r = explode("|", $row5->action);
$source = $r[1];
$direction = $r[2];
//checking balance once again
$row = getRowUsers();
if($source == "TON"){
$maxsum = $row->ton_ton_full - 0.1;
}elseif($source == "TGR"){
$maxsum = $row->tgr_ton_full + $row->tgr_bep20;
}
$maxsum = $maxsum - $maxsum * $exchangefee;
if($sum > $maxsum) {
$arInfo["inline_keyboard"][0][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ Назад к обменам";
send($chat_id, "❌ОШИБКА! Сумма обмена ($sum $source) превышает сумму на балансе ($maxsum $source). Повтори попытку.", $arInfo);
}else{
//getting rates
$response = file_get_contents('https://tegro.money/rates/TON-TGR/');
$res = json_decode($response, true);
$tonTgrRate = $res['data']['value'];
if($tonTgrRate != 0 && !empty($tonTgrRate)){
//processing exchange
//taking fee from the user balance
$totalMinus = $sum + $sum * $exchangefee;
if($source == "TON" && $direction == "TGR"){
$resultSum = $sum * $tonTgrRate;
$sourceRestSumTon = $row->ton_ton_full - $totalMinus;
$newTotal = $row->tgr_ton_full + $resultSum;
$str2upd = "UPDATE `users` SET `ton_ton_full`='$sourceRestSumTon', `tgr_ton_full`='$newTotal' WHERE `chatid`='$chat_id'";
}else{
$resultSum = $sum / $tonTgrRate;
$newTotal = $row->ton_ton_full + $resultSum;
if($row->tgr_ton_full >= $totalMinus){
$sourceRestSumTgrTon = $row->tgr_ton_full - $totalMinus;
$sourceRestSumTgrBep = $row->tgr_bep20;
}else{
$minusInBep = $totalMinus - $row->tgr_ton_full;
$sourceRestSumTgrTon = 0;
$sourceRestSumTgrBep = $row->tgr_bep20 - $minusInBep;
}
$str2upd = "UPDATE `users` SET `tgr_ton_full`='$sourceRestSumTgrTon', `tgr_bep20`='$sourceRestSumTgrBep', `ton_ton_full`='$newTotal' WHERE `chatid`='$chat_id'";
}
mysqli_query($link, $str2upd);
//applying referral fee
if($row->ref > 10){
$str6select = "SELECT * FROM `users` WHERE `chatid`='".$row->ref."'";
$result6 = mysqli_query($link, $str6select);
$row6 = @mysqli_fetch_object($result6);
$refFee = $sum * $exchangefee / 2;
$refFeeCellName = strtolower($source)."_ton_full";
$newtotal = $row6->$refFeeCellName + $refFee;
$str2upd = "UPDATE `users` SET `$refFeeCellName`='$newtotal' WHERE `chatid`='".$row->ref."'";
mysqli_query($link, $str2upd);
$arInfo["inline_keyboard"][0][0]["callback_data"] = 1;
$arInfo["inline_keyboard"][0][0]["text"] = "💎 В кошелек";
send($row->ref, "Твой реферал выполнил новый обмен.
Твоя комиссия составила: <b>$refFee $source</b>
Данная сумма зачислена на твой баланс.", $arInfo);
}
//save transaction
saveTransaction($sum, $source, "TON", "swap-send", 0);
saveTransaction($newTotal, $direction, "TON", "swap-get", 0);
clean_temp_sess();
$arInfo["inline_keyboard"][0][0]["callback_data"] = 1;
$arInfo["inline_keyboard"][0][0]["text"] = "💎 В кошелек";
$arInfo["inline_keyboard"][1][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][1][0]["text"] = "⏪ К обменам";
send($chat_id, "Обмен выполнен.
<b>$sum $source на $direction</b>
$resultSum $direction зачислены на твой баланс.", $arInfo);
}
else{
$arInfo["inline_keyboard"][0][0]["callback_data"] = 3;
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ К обменам";
send($chat_id, "❌ОШИБКА! Произошла неизвестная ошибка, код 431. Повтори попытку.", $arInfo);
}
}
}