Skip to content

Instantly share code, notes, and snippets.

@mmitech
Last active November 3, 2016 13:00
Show Gist options
  • Save mmitech/5507edb9b64ae9b49b2b5f5cf036874d to your computer and use it in GitHub Desktop.
Save mmitech/5507edb9b64ae9b49b2b5f5cf036874d to your computer and use it in GitHub Desktop.
Shielding and Clearing Zcash blocks
<?php
// Zcash daemon credentials
$rpcuser = "";
$rpcpass = "";
$rpchost = "";
$rpcport = "";
// Wallet addresses for shielding and clearing
$t_address = ""; //your t_coinbase address
$z_address = ""; //your z_address
$t_polo = ""; //your polo address
?>
<?php
/*
EasyBitcoin-PHP
A simple class for making calls to Bitcoin's API using PHP.
https://github.com/aceat64/EasyBitcoin-PHP
====================
The MIT License (MIT)
Copyright (c) 2013 Andrew LeCody
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
====================
// Initialize Bitcoin connection/object
$bitcoin = new Bitcoin('username','password');
// Optionally, you can specify a host and port.
$bitcoin = new Bitcoin('username','password','host','port');
// Defaults are:
// host = localhost
// port = 8332
// proto = http
// If you wish to make an SSL connection you can set an optional CA certificate or leave blank
// This will set the protocol to HTTPS and some CURL flags
$bitcoin->setSSL('/full/path/to/mycertificate.cert');
// Make calls to bitcoind as methods for your object. Responses are returned as an array.
// Examples:
$bitcoin->getinfo();
$bitcoin->getrawtransaction('0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',1);
$bitcoin->getblock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
// The full response (not usually needed) is stored in $this->response while the raw JSON is stored in $this->raw_response
// When a call fails for any reason, it will return FALSE and put the error message in $this->error
// Example:
echo $bitcoin->error;
// The HTTP status code can be found in $this->status and will either be a valid HTTP status code or will be 0 if cURL was unable to connect.
// Example:
echo $bitcoin->status;
*/
class Bitcoin {
// Configuration options
private $username;
private $password;
private $proto;
private $host;
private $port;
private $url;
private $CACertificate;
// Information and debugging
public $status;
public $error;
public $raw_response;
public $response;
private $id = 0;
/**
* @param string $username
* @param string $password
* @param string $host
* @param int $port
* @param string $proto
* @param string $url
*/
function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null) {
$this->username = $username;
$this->password = $password;
$this->host = $host;
$this->port = $port;
$this->url = $url;
// Set some defaults
$this->proto = 'http';
$this->CACertificate = null;
}
/**
* @param string|null $certificate
*/
function setSSL($certificate = null) {
$this->proto = 'https'; // force HTTPS
$this->CACertificate = $certificate;
}
function __call($method, $params) {
$this->status = null;
$this->error = null;
$this->raw_response = null;
$this->response = null;
// If no parameters are passed, this will be an empty array
$params = array_values($params);
// The ID should be unique for each call
$this->id++;
// Build the request, it's ok that params might have any empty array
$request = json_encode(array(
'method' => $method,
'params' => $params,
'id' => $this->id
));
// Build the cURL session
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
$options = array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $request
);
// This prevents users from getting the following warning when open_basedir is set:
// Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
if (ini_get('open_basedir')) {
unset($options[CURLOPT_FOLLOWLOCATION]);
}
if ($this->proto == 'https') {
// If the CA Certificate was specified we change CURL to look for it
if ($this->CACertificate != null) {
$options[CURLOPT_CAINFO] = $this->CACertificate;
$options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate);
}
else {
// If not we need to assume the SSL cannot be verified so we set this flag to FALSE to allow the connection
$options[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
}
curl_setopt_array($curl, $options);
// Execute the request and decode to an array
$this->raw_response = curl_exec($curl);
$this->response = json_decode($this->raw_response, TRUE);
// If the status is not 200, something is wrong
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// If there was no error, this will be an empty string
$curl_error = curl_error($curl);
curl_close($curl);
if (!empty($curl_error)) {
$this->error = $curl_error;
}
if ($this->response['error']) {
// If bitcoind returned an error, put that in $this->error
$this->error = $this->response['error']['message'];
}
elseif ($this->status != 200) {
// If bitcoind didn't return a nice error message, we need to make our own
switch ($this->status) {
case 400:
$this->error = 'HTTP_BAD_REQUEST';
break;
case 401:
$this->error = 'HTTP_UNAUTHORIZED';
break;
case 403:
$this->error = 'HTTP_FORBIDDEN';
break;
case 404:
$this->error = 'HTTP_NOT_FOUND';
break;
}
}
if ($this->error) {
throw new Exception(date("Y-m-d H:i:s") .": RPC call failed with error, ".$this->error."\n");
return FALSE;
}
return $this->response['result'];
}
}
<?php
include 'easybitcoin.php';
include 'config.php';
$zcash = new Bitcoin($rpcuser, $rpcpass, $rpchost, $rpcport);
$round = 0;
echo "\n===================== Starting Loop =====================\n";
while (1){
$round++;
echo "=========================================================\n";
$balance = ($zcash->getbalance() - 0.0001);
if ($balance > 0){
echo date("Y-m-d H:i:s") .": we will try to shield: $balance\n";
$to = array(
array(
"address" => $z_address,
"memo" => "48656c6c6f20ceb221",
"amount" => $balance
)
);
$opid = $zcash->z_sendmany($t_address, $to);
if ($opid) {
echo date("Y-m-d H:i:s") .": funds sent to shielding with operation id: $opid\n";
$array = array($opid);
$operation = $zcash->z_getoperationstatus($array);
if(count($operation) > 0 && $operation["0"]["status"] == "executing") {
while(count($operation) > 0 && $operation["0"]["status"] == "executing") {
echo date("Y-m-d H:i:s") .": operation $opid is executing\n";
$operation = $zcash->z_getoperationstatus($array);
sleep(10);
}
}
if(count($operation) > 0 && $operation["0"]["status"] == "queued") {
while(count($operation) > 0 && $operation["0"]["status"] == "queued") {
echo date("Y-m-d H:i:s") .": operation is queued for now\n";
$operation = $zcash->z_getoperationstatus($array);
sleep(10);
}
}
if(count($operation) > 0 && $operation["0"]["status"] == "failed") {
$error = $operation["0"]["error"]["message"];
echo date("Y-m-d H:i:s") .": operation shielding failed with message: $error\n";
break;
}
if(count($operation) > 0 && $operation["0"]["status"] == "success"){
echo date("Y-m-d H:i:s") .": operation was successfully executed\n";
$z_balance = $zcash->z_getbalance($z_address);
while ($z_balance < $balance ) {
echo date("Y-m-d H:i:s") .": waiting for $opid to get braodcasted and confirmed in network\n";
$z_balance = $zcash->z_getbalance($z_address);
sleep(10);
}
echo date("Y-m-d H:i:s") .": operation: $opid braodcasted and confirmed, we have $z_balance shielded balance to clear\n";
$polo = array(
array(
"address" => $t_polo,
"amount" => ($z_balance - 0.0001)
)
);
echo date("Y-m-d H:i:s") .": trying to clear the shielded balance\n";
$clearing = $zcash->z_sendmany($z_address, $polo);
$array = array($clearing);
$z_operation = $zcash->z_getoperationstatus($array);
if(count($z_operation) > 0 && $z_operation["0"]["status"] == "executing") {
while(count($z_operation) > 0 && $z_operation["0"]["status"] == "executing") {
echo date("Y-m-d H:i:s") .": operation $clearing is executing\n";
$z_operation = $zcash->z_getoperationstatus($array);
sleep(10);
}
}
if(count($z_operation) > 0 && $z_operation["0"]["status"] == "queued") {
while(count($z_operation) > 0 && $z_operation["0"]["status"] == "queued") {
echo date("Y-m-d H:i:s") .": operation is queued for now\n";
$z_operation = $zcash->z_getoperationstatus($array);
sleep(10);
}
}
if(count($z_operation) > 0 && $z_operation["0"]["status"] == "failed") {
$error = $z_operation["0"]["error"]["message"];
echo date("Y-m-d H:i:s") .": operation $clearing failed with message: $error\n";
break;
}
if(count($z_operation) > 0 && $z_operation["0"]["status"] == "success") {
echo date("Y-m-d H:i:s") .": operation $clearing cleared successfully\n";
}
else {
echo date("Y-m-d H:i:s") .": please check what happened to this operation $clearing\n";
print_r($z_operation["0"]);
}
}
} else {
echo date("Y-m-d H:i:s") .": please check what happened to this operation $opid\n";
print_r($operation["0"]);
}
} else {
echo date("Y-m-d H:i:s") .": no balance awaiting shielding\n";
}
echo date("Y-m-d H:i:s") .": round number $round ended\n";
echo date("Y-m-d H:i:s") .": sleeping for 30 min\n";
echo "=========================================================\n";
sleep(1800);
}
echo date("Y-m-d H:i:s") .": loop ended unexpectedly\n";
echo "=========================================================\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment