Skip to content

php demo

bl4z edited this page Apr 2, 2020 · 1 revision

/**
	"url_root": "https://api2.nicehash.com",
	"org_id": "xxx",
	"api_key": "xxx",
	"api_secret": "xxx",
	"algo": "GRINCUCKAROOD29",
	*/
$cfg = json_decode($config, true);

/*
$algo = file_get_contents("https://api2.nicehash.com/main/api/v2/mining/algorithms");
file_put_contents('algo.cache', $algo);
*/
$nhinfo = file_get_contents($cfg['algo_cache']);
$algo   = get_algo_settings($nhinfo, $cfg['algo']);

function get_algo_settings($raw, $a) {
	$algos = json_decode($raw, true)['miningAlgorithms'];
	foreach ($algos as $key => $algo) {
		if ($algo['algorithm'] == $a) {
			return $algo;
		}
	}
}

function get_time() {
	global $cfg;

	//get time
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_URL, $cfg['url_root']."/api/v2/time");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);
	$time = json_decode($result, true)['serverTime'];
	return $time;
}

function get_my_orders() {
	global $cfg;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/myOrders";
	$qs        = "ts=".$time."&op=LE&limit=1000&status=ACTIVE&algorithm=".$cfg['algo'];

	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."GET"."\x00".$path."\x00".$qs;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}"
	);
	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);
	return json_decode($result, true)['list'];
}

function get_order($id) {
	global $cfg;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/order/".$id;
	$qs        = "";

	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."GET"."\x00".$path."\x00".$qs;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}"
	);
	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);
	return json_decode($result, true);	
}

function refill_order($order_id) {
	global $cfg;
	global $algo;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/order/".$order_id."/refill";
	$qs        = "";

	$postbody  = json_encode(array(
		"amount"=>$algo['minimalOrderAmount']));

	$postlen   = strlen($postbody);
	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."POST"."\x00".$path."\x00".$qs."\x00".$postbody;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}",
		"Content-Type: application/json",
	    "Content-Length: {$postlen}" 
	);

	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); 

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);

	$info = json_decode($result, true);
	return $info;
}

function update_order($order_id, $price, $limit) {
	global $cfg;
	global $algo;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/order/".$order_id."/updatePriceAndLimit";
	$qs        = "";

	$postbody  = json_encode(array(
		"limit"=>round($limit,1),
		"price"=>$price,
		"marketFactor"=>$algo['marketFactor'],
		"displayMarketFactor"=>$algo['displayMarketFactor']));

	$postlen   = strlen($postbody);
	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."POST"."\x00".$path."\x00".$qs."\x00".$postbody;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}",
		"Content-Type: application/json",
	    "Content-Length: {$postlen}" 
	);

	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); 

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);

	$info = json_decode($result, true);
	return $info;
}

function cancel_order($order_id) {
	global $cfg;
	global $algo;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/order/".$order_id;
	$qs        = "";

	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."DELETE"."\x00".$path."\x00".$qs;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}",
		"Content-Type: application/json",
	    "Content-Length: {$postlen}" 
	);

	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); 

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);

	$info = json_decode($result, true);
	return $info;
}

function create_order($market, $price) {
	global $cfg;
	global $algo;

	$time      = get_time();
	$nonce     = uniqid();
	$path      = "/main/api/v2/hashpower/order";
	$qs        = "";

	$postbody  = json_encode(array(
		"type"=>"STANDARD",
		"limit"=>$algo['minSpeedLimit'],
		"poolId"=>$cfg['pool_id'],
		"price"=>$price,
		"marketFactor"=>$algo['marketFactor'],
		"displayMarketFactor"=>$algo['displayMarketFactor'],
		"amount"=>$algo['minimalOrderAmount'],
		"algorithm"=>$cfg['algo'],
		"market"=>$cfg['market']));

	print_r($postbody);

	$postlen   = strlen($postbody);
	$signature = $cfg['api_key']."\x00".$time."\x00".$nonce."\x00"."\x00".$cfg['org_id']."\x00"."\x00"."POST"."\x00".$path."\x00".$qs."\x00".$postbody;
	$signhash  = hash_hmac('sha256', $signature, $cfg['api_secret']);

	$headers = array(
		"X-Time: {$time}",
		"X-Nonce: {$nonce}",
		"X-Organization-Id: {$cfg['org_id']}",
		"X-Request-Id: {$nonce}",
		"X-Auth: {$cfg['api_key']}:{$signhash}",
		"Content-Type: application/json",
	    "Content-Length: {$postlen}" 
	);

	$curl = curl_init();
	//curl_setopt($curl, CURLOPT_VERBOSE, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); 

	curl_setopt($curl, CURLOPT_URL, $cfg['url_root'].$path."?".$qs);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($curl);
	curl_close($curl);

	$info = json_decode($result, true);
	return $info;
}
Clone this wiki locally