Created
May 29, 2014 11:38
-
-
Save nand2/11cdac05085ad652ef10 to your computer and use it in GitHub Desktop.
Update of rig price on Betarigs using its API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Config | |
$base_api_url = "https://www.betarigs.com/api/v1"; | |
$rig_id = 1; | |
$percent_above_market_price = 10; | |
$minimum_per_speed_unit_price = 0.001; | |
$api_key = 'XXX'; | |
echo "[" . date('Y-m-d H:i:s') . "] Launching price updating of rig $rig_id\n"; | |
// First fetch which algo the rig is using the API | |
echo "[" . date('Y-m-d H:i:s') . "] Getting infos about rig $rig_id ...\n"; | |
$url = $base_api_url . '/rig/' . $rig_id; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
$result = curl_exec($ch); | |
$rig_data = json_decode($result, true); | |
if(empty($rig_data) || empty($rig_data['algorithm']['id'])) { | |
echo "[" . date('Y-m-d H:i:s') . "] *** ERROR *** Failed to get infos, stopping.\n"; | |
exit; | |
} | |
$algo_id = $rig_data['algorithm']['id']; | |
$algo_name = $rig_data['algorithm']['name']; | |
echo "[" . date('Y-m-d H:i:s') . "] Rig $rig_id is on the algo $algo_name (id: $algo_id) \n"; | |
// Fetch the market price for this algo | |
echo "[" . date('Y-m-d H:i:s') . "] Fetching market price on $algo_name (id: $algo_id) ... \n"; | |
$url = $base_api_url . '/algorithm/' . $algo_id; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
$result = curl_exec($ch); | |
$algo_data = json_decode($result, true); | |
if(empty($algo_data) || empty($algo_data['market_price']['value'])) { | |
echo "[" . date('Y-m-d H:i:s') . "] *** ERROR *** Failed to get infos, stopping.\n"; | |
exit; | |
} | |
$algo_per_speed_unit_market_price = $algo_data['market_price']['value']; | |
echo "[" . date('Y-m-d H:i:s') . "] $algo_name (id: $algo_id) market price is $algo_per_speed_unit_market_price BTC/Mh/day \n"; | |
// Computing the new price | |
$new_per_speed_unit_rig_price = $algo_per_speed_unit_market_price * ((100 + $percent_above_market_price) / 100); | |
echo "[" . date('Y-m-d H:i:s') . "] Setting the rig $rig_id per-speed-unit price $percent_above_market_price% above \n"; | |
echo "[" . date('Y-m-d H:i:s') . "] Setting the rig $rig_id per-speed-unit price to $new_per_speed_unit_rig_price BTC/Mh/day \n"; | |
// Checking the floor price | |
if($new_per_speed_unit_rig_price < $minimum_per_speed_unit_price) { | |
$new_per_speed_unit_rig_price = $minimum_per_speed_unit_price; | |
echo "[" . date('Y-m-d H:i:s') . "] **WARN ** The price is below the floor price of $minimum_per_speed_unit_price BTC/Mh/day \n"; | |
echo "[" . date('Y-m-d H:i:s') . "] **WARN ** Setting the rig $rig_id per-speed-unit price to $new_per_speed_unit_rig_price BTC/Mh/day \n"; | |
} | |
echo "[" . date('Y-m-d H:i:s') . "] Updating the price of rig $rig_id to $new_per_speed_unit_rig_price BTC/Mh/day ... \n"; | |
$update_data = array( | |
'price' => array( | |
'per_speed_unit' => array( | |
'value' => $new_per_speed_unit_rig_price, | |
'unit' => 'BTC/Mh/day' | |
), | |
), | |
); | |
$headers = array( | |
'Content-Type: application/json', | |
'X-Api-Key: ' . $api_key | |
); | |
$url = $base_api_url . '/rig/' . $rig_id; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($update_data)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
$update_data = json_decode($result, true); | |
if(empty($update_data) || empty($update_data['result']) || $update_data['result'] != 'OK') { | |
echo "[" . date('Y-m-d H:i:s') . "] *** ERROR *** Failed to update the rig price, stopping.\n"; | |
exit; | |
} | |
// THERE IS CACHE! | |
// echo "[" . date('Y-m-d H:i:s') . "] Update succeeded, double-checking price ... \n"; | |
// $url = $base_api_url . '/rig/' . $rig_id; | |
// $ch = curl_init(); | |
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// curl_setopt($ch, CURLOPT_URL, $url); | |
// $result = curl_exec($ch); | |
// $rig_data = json_decode($result, true); | |
// if(empty($rig_data) || empty($rig_data['price']['per_speed_unit']['value'])) { | |
// echo "[" . date('Y-m-d H:i:s') . "] *** ERROR *** Failed to get infos, stopping.\n"; | |
// exit; | |
// } | |
// $percentage_diff = abs(($rig_data['price']['per_speed_unit']['value'] - $new_per_speed_unit_rig_price) / $new_per_speed_unit_rig_price); | |
// if($percentage_diff > 0.001) { | |
// echo "[" . date('Y-m-d H:i:s') . "] *** ERROR *** It seems the price wasn't updated correctly, target price: $new_per_speed_unit_rig_price BTC/Mh/day; actual price: " . $rig_data['price']['per_speed_unit']['value'] . " BTC/Mh/day\n"; | |
// exit; | |
// } | |
echo "[" . date('Y-m-d H:i:s') . "] All good! End of price updating of rig $rig_id\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment