forked from BladeBTC/BladeBTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_deposit.php
executable file
·158 lines (119 loc) · 4.28 KB
/
cron_deposit.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
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', getenv("DEBUG"));
require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
use BladeBTC\Helpers\Btc;
use BladeBTC\Helpers\Database;
use BladeBTC\Helpers\Wallet;
use BladeBTC\Models\Investment;
use BladeBTC\Models\Transactions;
use BladeBTC\Models\Users;
try {
/**
* Load .env file
*/
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
/*
* =========================================== HANDLE DEPOSIT ==========================================
*/
/**
* Recover all address
*/
$addresses = Wallet::listAddress();
foreach ($addresses['addresses'] as $address) {
echo '<pre>';
print_r($address);
/**
* Check if address have balance
*/
if ($address['total_received'] > 0) {
/**
* Build user object
*/
$user = new Users($address['label']);
/**
* Check if transaction have 6 confirmation
*/
$check_address = $address['address'];
if (Wallet::getConfirmedReceivedByAddress($check_address) > $user->getLastConfirmed()) {
$db = Database::get();
try {
$db->beginTransaction();
/**
* Check if is a new transaction
*/
if (Btc::SatoshiToBitcoin($address['total_received']) != $user->getLastConfirmed()) {
/**
* Set last confirmed
*/
$user->setLastConfirmed(Btc::SatoshiToBitcoin($address['total_received']));
/**
* Create investment
*/
Investment::create($user->getTelegramId(), (Btc::SatoshiToBitcoin($address['total_received']) - $user->getLastConfirmed()), $user->getUserRate());
/**
* Update invested
*/
$db->query(" UPDATE
`users`
SET
`invested` = `invested` + " . $db->quote(Btc::SatoshiToBitcoin($address['total_received']) - $user->getLastConfirmed()) . "
WHERE
`telegram_id` = " . $user->getTelegramId() . "
");
/**
* Give bonus to referent - First invest only
*/
if (Investment::getTotalInvestment($user->getTelegramId()) == 1) {
$referent_id = $db->query(" SELECT
`telegram_id_referent`
FROM
`referrals`
WHERE
`telegram_id_referred` = " . $user->getTelegramId() . "
")->fetchObject();
if (is_object($referent_id) && !empty($referent_id->telegram_id_referent)) {
/**
* Calculate commision
*/
$rate = getenv("COMMISSION_RATE");
$commission = (Btc::SatoshiToBitcoin($address['total_received']) - $user->getLastConfirmed()) * $rate / 100;
$db->query(" UPDATE
`users`
SET
`commission` = `commission` + " . $db->quote($commission) . ",
`balance` = `balance` + " . $db->quote($commission) . "
WHERE
`telegram_id` = " . $referent_id->telegram_id_referent . "
");
}
}
/**
* Log transaction
*/
Transactions::log([
"telegram_id" => $user->getTelegramId(),
"amount" => (Btc::SatoshiToBitcoin($address['total_received']) - $user->getLastConfirmed()),
"withdraw_address" => "",
"message" => "",
"tx_hash" => "",
"notice" => "",
"status" => 1,
"type" => "deposit",
]);
}
$db->commit();
} catch (\Exception $e) {
$db->rollBack();
throw new \Exception($e->getMessage());
}
}
}
}
} catch (Exception $e) {
if (getenv("DEBUG") == 1) {
mail(getenv("MAIL"), "BOT ERROR", $e->getMessage() . "\n" . $e->getFile() . "[" . $e->getLine() . "]");
}
}