forked from staccDOTsol/arbbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
184 lines (139 loc) · 4.71 KB
/
main.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
<?php
require_once 'bot/utils.php';
require_once 'bot/Config.php';
require_once 'bot/Exchange.php';
require_once 'bot/Arbitrator.php';
require_once 'bot/TradeMatcher.php';
require_once 'lib/composer/vendor/autoload.php';
use React\EventLoop;
$gVerbose = true;
date_default_timezone_set( "UTC" );
if ( $files = installDirectoryDirty() ) {
echo "Error launching due to the following files being found in the installation directory.\n";
echo "Please remove them before proceeding.\n";
foreach ( $files as $file ) {
echo "\t* $file\n";
}
return;
}
try {
Config::refresh();
}
catch ( Exception $ex ) {
echo "Error loading config: " . $ex->getMessage() . "\n";
return;
}
logg( "ARBITRATOR V3.0alpha launching..." );
sendmail( "Startup mail service test", "This is a test message to confirm that the mail service is working properly!" );
logg( "Loading config..." );
Database::handleAddressUpgrade();
Database::handleAlertsUpgrade();
Database::handleCoinUpgrade();
Database::handleTrackUpgrade();
if ( !Database::alertsTableExists() ) {
logg( "Upgrading the database to create the separate alerts table" );
logg( "This is a one time operation which may be really slow, please wait..." );
Database::createAlertsTable();
Database::importAlerts();
}
if ( !Database::balancesTableExists() ) {
logg( "Upgrading the database to create the separate balances table" );
logg( "This is a one time operation which may be really slow, please wait..." );
Database::createBalancesTable();
Database::importBalances();
}
Database::createBalancesIndexIfNeeded();
if ( !Database::currentSimulatedProfitsRateViewExists() ) {
Database::createCurrentSimulatedProfitsRateView();
}
if ( !Database::pendingDepositsTableExists() ) {
Database::createPendingDepositsTable();
}
if ( !Database::pendingDepositsTableExists() ) {
Database::createPendingDepositsTable();
} else {
Database::ensurePendingDepositsUpgraded();
}
if ( !Database::profitsTableExists() ) {
Database::createProfitsTable();
Database::importProfits();
}
if ( !Database::walletsTableExists() ) {
Database::createWalletsTable();
}
if ( !Database::profitLossTableExists() ) {
require_once __DIR__ . '/import-profit-loss.php';
importProfitLoss();
}
// Configure exchanges...
$exchanges = [ ];
$msg = '';
$gEventLoop = EventLoop\Factory::create();
foreach ( glob( 'bot/xchange/*.php' ) as $filename ) {
$name = basename( $filename, '.php' );
logg( "Enabling $name..." );
require_once $filename;
try {
$exchanges[] = new $name;
}
catch ( Exception $ex ) {
logg( $ex->getMessage() );
}
}
logg( "Configured " . count( $exchanges ) . " exchanges!" );
if ( count( $exchanges ) < 2 ) {
logg( "ERROR: At least two exchanges are required!" );
return;
}
logg( "Testing exchange access..." );
foreach ( $exchanges as $exchange ) {
try {
$exchange->testAccess();
logg( $exchange->getName() . " [OK]" );
}
catch ( Exception $ex ) {
logg( $exchange->getName() . " [ERROR]\n" . $ex->getMessage() );
return;
}
}
Database::fixupProfitLossCalculations( $exchanges );
$tradeMatcher = new TradeMatcher( $exchanges );
foreach ( $exchanges as $exchange ) {
if ( $tradeMatcher->hasExchangeNewTrades( $exchange->getID() ) ) {
logg( "Noticed new trades on " . $exchange->getName() . " that we haven't seen before, importing them now..." );
$name = $exchange->getTradeHistoryCSVName();
if ( $name ) {
$csvPath = __DIR__ . '/' . $name;
if ( ! is_readable( $csvPath ) ) {
$prompt = file_get_contents( __DIR__ . '/bot/xchange/' . $exchange->getName() . '-csv-missing.txt' );
logg( $prompt );
readline();
if ( !is_readable( $csvPath ) ) {
die( "Still can't find the file, refusing to continue\n" );
}
}
}
// Now read the full history
$hist = $exchange->queryTradeHistory();
// Insert what we don't have into the database
foreach ( $hist as $market => $data ) {
$arr = explode( '_', $market );
$currency = $arr[ 0 ];
$tradeable = $arr[ 1 ];
foreach ( $data as $row ) {
$tradeMatcher->saveTrade( $exchange->getID(), $row[ 'type' ],
$tradeable, $currency, $row );
}
}
}
}
$arbitrator = new Arbitrator( $gEventLoop, $exchanges, $tradeMatcher );
$arbitrator->run();
function sendmail( $title, $message ) {
//
$mailRecipient = Config::get( Config::MAIL_RECIPIENT, null );
if ( is_null( $mailRecipient ) ) {
$mailRecipient = '[email protected]';
}
mail( $mailRecipient, "[ARB] " . $title, $message );
}