-
Notifications
You must be signed in to change notification settings - Fork 44
/
transactions.php
187 lines (163 loc) · 7.01 KB
/
transactions.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
<?php
/**
* Process a single account for transactions.
*/
$q = db()->prepare("SELECT * FROM transaction_creators WHERE id=?");
$q->execute(array($job['arg_id']));
$creator = $q->fetch();
if (!$creator) {
throw new JobException("Could not find any creator " . $job['arg_id'] . " for user " . $job['user_id']);
}
$account_data = get_account_data($creator['exchange']);
$q = db()->prepare("SELECT * FROM " . $account_data['table'] . " WHERE id=? AND user_id=?");
$q->execute(array($creator['account_id'], $job['user_id']));
$account = $q->fetch();
if (!$account) {
throw new JobException("Could not find any " . $creator['exchange'] . " account ID " . $creator['account_id'] . " for user " . $job['user_id']);
}
crypto_log("Investigating account " . $account['id'] . " " . $creator['exchange']);
// for every currency this exchange should supports
$get_supported_wallets = get_supported_wallets();
// add all blockchain wallets
$account_data_grouped = account_data_grouped();
foreach ($account_data_grouped['Addresses'] as $exchange => $data) {
$get_supported_wallets[$exchange] = array($data['currency'], '_is_currency_');
}
$cursor = false;
if (!isset($get_supported_wallets[$account_data['title_key']])) {
crypto_log("No supported wallets for " . $account_data['title_key']);
} else {
foreach ($get_supported_wallets[$account_data['title_key']] as $currency) {
if ($currency == 'hash' || $currency == '_is_currency_')
continue;
$is_address_balance = false;
crypto_log("Processing currency $currency");
if (in_array('_is_currency_', $get_supported_wallets[$account_data['title_key']])) {
crypto_log("This is an address account: using address_balances rather than balances");
$is_address_balance = true;
}
$cursor = false;
if ($creator['transaction_cursor']) {
if ($is_address_balance) {
// select the cursor value
$q = db()->prepare("SELECT * FROM address_balances WHERE is_daily_data=1 AND user_id=:user_id AND address_id=:address_id
AND created_at_day <= :cursor ORDER BY created_at_day DESC LIMIT 1");
$q->execute(array(
"user_id" => $account['user_id'],
"address_id" => $account['id'],
"cursor" => $creator['transaction_cursor'],
));
$cursor = $q->fetch();
} else {
// select the cursor value
$q = db()->prepare("SELECT * FROM balances WHERE is_daily_data=1 AND user_id=:user_id AND account_id=:account_id AND exchange=:exchange AND currency=:currency
AND created_at_day <= :cursor ORDER BY created_at_day DESC LIMIT 1");
$q->execute(array(
"user_id" => $account['user_id'],
"account_id" => $account['id'],
"exchange" => $creator['exchange'],
"currency" => $currency,
"cursor" => $creator['transaction_cursor'],
));
$cursor = $q->fetch();
// is the cursor older than recent data?
if (!$cursor) {
crypto_log("Searching graph_data_balances for older cursor than " . $creator['transaction_cursor']);
$q = db()->prepare("SELECT exchange, account_id, currency, balance_closing AS balance, data_date AS created_at, data_date_day AS created_at_day
FROM graph_data_balances WHERE user_id=:user_id AND account_id=:account_id AND exchange=:exchange AND currency=:currency
AND data_date_day <= :cursor ORDER BY data_date_day DESC LIMIT 1");
$q->execute(array(
"user_id" => $account['user_id'],
"account_id" => $account['id'],
"exchange" => $creator['exchange'],
"currency" => $currency,
"cursor" => $creator['transaction_cursor'],
));
$cursor = $q->fetch();
}
}
}
// use the cursor to select new balances
$balances = array();
if ($is_address_balance) {
$q = db()->prepare("SELECT *, :currency AS currency FROM address_balances WHERE is_daily_data=1 AND user_id=:user_id AND address_id=:address_id
AND created_at_day > :cursor AND created_at_day <= TO_DAYS(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$q->execute(array(
"user_id" => $account['user_id'],
"address_id" => $account['id'],
"currency" => $currency,
"cursor" => $creator['transaction_cursor'],
));
$balances += $q->fetchAll();
} else {
$q = db()->prepare("SELECT exchange, account_id, currency, balance_closing AS balance, data_date AS created_at, data_date_day AS created_at_day
FROM graph_data_balances WHERE user_id=:user_id AND account_id=:account_id AND exchange=:exchange AND currency=:currency
AND data_date_day > :cursor AND data_date_day <= TO_DAYS(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$q->execute(array(
"user_id" => $account['user_id'],
"account_id" => $account['id'],
"exchange" => $creator['exchange'],
"currency" => $currency,
"cursor" => $creator['transaction_cursor'],
));
$balances += $q->fetchAll();
$q = db()->prepare("SELECT * FROM balances WHERE is_daily_data=1 AND user_id=:user_id AND account_id=:account_id AND exchange=:exchange AND currency=:currency
AND created_at_day > :cursor AND created_at_day <= TO_DAYS(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$q->execute(array(
"user_id" => $account['user_id'],
"account_id" => $account['id'],
"exchange" => $creator['exchange'],
"currency" => $currency,
"cursor" => $creator['transaction_cursor'],
));
$balances += $q->fetchAll();
}
crypto_log("Processing " . number_format(count($balances)) . " balances");
foreach ($balances as $balance) {
$transaction = false;
if (!$cursor && $balance['balance'] != 0) {
// this is a brand new account
crypto_log($balance['created_at'] . ": New cursor at " . $balance['balance']);
$transaction = array(
'user_id' => $account['user_id'],
'transaction_date' => $balance['created_at'],
'exchange' => $creator['exchange'],
'account_id' => $account['id'],
'currency1' => $balance['currency'],
'value1' => $balance['balance'],
);
} else if ((float) $balance['balance'] != (float) $cursor['balance']) {
// this is a transaction that has changed balances
crypto_log($balance['created_at'] . ": Balance is now " . $balance['balance'] . " from " . $cursor['balance']);
$transaction = array(
'user_id' => $account['user_id'],
'transaction_date' => $balance['created_at'],
'exchange' => $creator['exchange'],
'account_id' => $account['id'],
'currency1' => $balance['currency'],
'value1' => $balance['balance'] - $cursor['balance'],
);
}
if ($transaction) {
$q = db()->prepare("INSERT INTO transactions SET user_id=:user_id, is_automatic=1,
transaction_date=:transaction_date,
transaction_date_day=TO_DAYS(:transaction_date),
exchange=:exchange,
account_id=:account_id,
currency1=:currency1,
value1=:value1");
$q->execute($transaction);
crypto_log("Added transaction " . db()->lastInsertId());
}
$cursor = $balance;
}
// TODO
}
// update transaction cursor
if ($cursor) {
$q = db()->prepare("UPDATE transaction_creators SET transaction_cursor=? WHERE id=?");
$q->execute(array($cursor['created_at_day'], $creator['id']));
crypto_log("Updated cursor to " . $cursor['created_at_day']);
}
crypto_log("Complete.");
}