-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrr.php
225 lines (199 loc) · 6.81 KB
/
mrr.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Created by IntelliJ IDEA.
* User: svet
* Date: 22.02.18
* Time: 16:03
*/
require_once 'mrr_config.php';
class mrr {
//Root URI for the api
public $root_uri = "https://www.miningrigrentals.com/api/v2";
public $decode = true;
public $pretty = false;
public $print = false;
private $key;
private $secret;
function __construct($key,$secret) {
//define the api_key and api_secret on construct
$this->key = $key;
$this->secret = $secret;
}
//Raw query function -- includes signing the request
function query($type,$endpoint,$parms=array()) {
$ch = curl_init();
$rest = "";
//if there is any url params, remove it for the signature
if(strpos($endpoint,"?")!==false){
$arr = explode("?",$endpoint);
$endpoint = $arr[0];
$rest = "?".$arr[1];
}
//URI is our root_uri + the endpoint
$uri = $this->root_uri.$endpoint.$rest;
switch($type) {
case 'GET':
$parms = json_encode($parms);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($type));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
break;
case 'POST':
$parms = json_encode($parms);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
break;
case 'DELETE':
case 'HEAD':
case 'PUT':
$parms = json_encode($parms);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($type));
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($type));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
}
if($this->pretty) {
if(strpos($uri,"?") == false) $uri.='?pretty';
else $uri.="&pretty";
}
//Get an incrementing/uinque nonce
$mtime = explode('.',microtime(true));
$mtime[1] = str_pad($mtime[1], 4, 0, STR_PAD_RIGHT);
$nonce = implode('',$mtime);
//String to sign is api_key + nonce + endpoint
$sign_string = $this->key.$nonce.$endpoint;
//Sign the string using a sha1 hmac
$sign = hash_hmac("sha1", $sign_string, $this->secret);
//Headers to include our key, signature and nonce
$headers = array(
'Content-Type: application/json',
'x-api-key: '.$this->key,
'x-api-sign: '.$sign,
'x-api-nonce: '.$nonce,
);
//Curl request
curl_setopt($ch, CURLOPT_URL,$uri);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MRR API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//echo $parms."\n";
if($this->print) {
echo "$type $uri\n";
}
return array(
'status'=>$info['http_code'],
'header'=>trim(substr($response,0,$info['header_size'])),
'data'=>substr($response,$info['header_size'])
);
}
function parseReturn($array) {
$data = array();
if($array["status"] != 200) {
$data = $array;
}else {
if($this->decode)
$data = json_decode($array["data"],true);
else
$data = $array["data"];
}
if($this->print) {
echo "\nReturned Data::\n";
if(is_array($data))
print_r($data);
else
echo $data;
echo "\n";
} else {
return $data;
}
}
//helper aliases just to make things easier
function get($endpoint,$parms=array()) {
return $this->parseReturn($this->query("GET",$endpoint,$parms));
}
function post($endpoint,$parms=array()) {
return $this->parseReturn($this->query("POST",$endpoint,$parms));
}
function put($endpoint,$parms=array()) {
return $this->parseReturn($this->query("PUT",$endpoint,$parms));
}
function delete($endpoint,$parms=array()) {
return $this->parseReturn($this->query("DELETE",$endpoint,$parms));
}
}
if (php_sapi_name() != "cli") {
die;
}
$shortopts = "";
$longopts = array(
"algo:", // Required value
"currency:",
"rigID:",
"updatePrice:",
"th:",
"modifier::",
"minPrice::",
);
$options = getopt($shortopts, $longopts);
foreach ($options as $var => $option) {
$$var = $option;
}
$mrr = new mrr($key,$secret);
$mrr->decode=true;
$mrr->pretty=false;
$mrr->print=false;
$modifier = isset($modifier) ? $modifier : 0;
$currency = isset($currency) ? $currency : 'LTC';
$algo = isset($algo) ? $algo : 'scrypt';
$rigID = isset($rigID) ? $rigID : 12345;
$updatePrice = (isset($updatePrice) and $updatePrice != 'false') ? true : false;
$th = isset($th) ? $th : 'mh';
$minPrice = isset($minPrice) ? $minPrice : 0;
$algoInfo = $mrr->get("/info/algos/$algo", ['currency' => $currency]);
$suggestedPrice = 0;
if (isset($algoInfo['success']) and $algoInfo['success'] == true) {
if (!empty($data=$algoInfo['data']) and !empty($suggestedPrice=$data['suggested_price'])) {
echo 'Suggested price from MRR: ' . $suggestedPrice['amount'] . PHP_EOL;
$suggestedPrice = $suggestedPrice['amount'] * (1+$modifier);
}
}
$currentPrice = 0;
if ($suggestedPrice > $minPrice) {
$rig = $mrr->get("/rig/$rigID");
if ($rig and !empty($rig['success']) and !empty(($data=$rig['data']) and !empty($price=$data['price']) and !empty($currencyPrice=$price[$currency])))
$currentPrice = !empty($currencyPrice['price']) ? $currencyPrice['price'] : 0;
$params = array(
// "hash"=>array(
// "hash"=>50,
// "type"=>"gh",
// ),
// "type"=>"scrypt",
// "server"=>"us-central01.miningrigrentals.com",
"price"=>array(
"type"=>$th,
$currency=>array(
"price"=>$suggestedPrice,
)
),
// "minhours"=>4,
// "maxhours"=>24,
// "status"=>"disabled"
);
if ($updatePrice) {
try {
$status = $mrr->put("/rig/$rigID", $params);
if (!empty($status['success']))
echo 'Update price: ' . $suggestedPrice . PHP_EOL;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}