Commit f5711101 authored by beardedwarrior's avatar beardedwarrior

updated the project class to reflect the changes needed to store the unapproved merge requests

parent ef518e0f
...@@ -27,9 +27,16 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode; ...@@ -27,9 +27,16 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTargetAmount($value) * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTargetAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereUpdatedAt($value)
* @mixin \Eloquent * @mixin \Eloquent
* @property string $title
* @property int|null $merge_request_id
* @property string|null $commit_sha
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereCommitSha($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereMergeRequestId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTitle($value)
*/ */
class Project extends Model class Project extends Model
{ {
protected $guarded = ['id'];
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */
......
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
use Faker\Generator as Faker; use Faker\Generator as Faker;
$factory->define(\App\Project::class, function (Faker $faker) { $factory->define(\App\Project::class, function (Faker $faker) {
$status = $faker->randomElement(['opened', 'closed', 'locked', 'merged']);
return [ return [
'title' => $faker->sentence(),
'payment_id' => $faker->sha256, 'payment_id' => $faker->sha256,
'target_amount' => $faker->randomNumber(), 'target_amount' => $faker->randomFloat(2, 0, 2000),
'status' => $faker->randomElement(['new', 'open', 'funded']), 'state' => $status,
'created_at' => $faker->dateTime, 'merge_request_id' => $faker->randomNumber(6),
'updated_at' => $faker->dateTime, 'created_at' => $faker->dateTimeThisYear,
'updated_at' => $faker->dateTimeThisYear,
]; ];
}); });
...@@ -15,9 +15,12 @@ class CreateProjectsTable extends Migration ...@@ -15,9 +15,12 @@ class CreateProjectsTable extends Migration
{ {
Schema::create('projects', function (Blueprint $table) { Schema::create('projects', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('payment_id')->unique(); $table->string('title');
$table->string('target_amount'); $table->string('payment_id')->nullable();
$table->string('status')->default('open'); $table->string('address')->nullable();
$table->string('target_amount')->nullable();
$table->string('state')->default('opened');
$table->unsignedInteger('merge_request_id')->unique();
$table->timestamps(); $table->timestamps();
}); });
} }
......
...@@ -4,6 +4,7 @@ namespace Monero; ...@@ -4,6 +4,7 @@ namespace Monero;
use App\Project; use App\Project;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Collection;
class Wallet class Wallet
{ {
...@@ -38,27 +39,6 @@ class Wallet ...@@ -38,27 +39,6 @@ class Wallet
return ['address' => $integratedAddress['integrated_address'], 'paymentId' => $integratedAddress['payment_id']]; return ['address' => $integratedAddress['integrated_address'], 'paymentId' => $integratedAddress['payment_id']];
} }
/**
* Returns all balances (locked/unlocked) or exception state for site monitor
*
* @return array
*/
public function balanceSiteMonitor()
{
$result = [];
$balance = $this->client->getbalance();
if (!$balance) {
$result['unlocked_balance'] = 'DOWN';
$result['balance'] = 'DOWN';
return $result;
}
$result['unlocked_balance'] = $balance['unlocked_balance'] / 1000000000000;
$result['balance'] = $balance['balance'] / 1000000000000;
return $result;
}
/** /**
* Returns the actual available and useable balance (unlocked balance) * Returns the actual available and useable balance (unlocked balance)
* *
...@@ -66,22 +46,19 @@ class Wallet ...@@ -66,22 +46,19 @@ class Wallet
*/ */
public function balance() public function balance()
{ {
$balance = $this->client->getbalance(); return $this->client->balance();
$result = $balance['unlocked_balance'] / 1000000000000;
return $result;
} }
public function mempoolTransfers() public function mempoolTransfers()
{ {
return $this->client->mempoolTransfers(); return $this->client->incomingTransfers();
} }
public function bulkPayments($paymentIds) public function bulkPayments($paymentIds)
{ {
$blockBuffer = 10; $blockBuffer = 10;
return $this->client->bulk_payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer); return $this->client->payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer);
} }
/** /**
...@@ -152,12 +129,7 @@ class Wallet ...@@ -152,12 +129,7 @@ class Wallet
*/ */
public function blockHeight() public function blockHeight()
{ {
$result = $this->client->getheight(); return $this->client->blockHeight();
if ($result && isset($result['height'])) {
return $result['height'];
}
return 0;
} }
/** /**
...@@ -167,12 +139,7 @@ class Wallet ...@@ -167,12 +139,7 @@ class Wallet
*/ */
public function getAddress() public function getAddress()
{ {
$address = $this->client->getaddress(); return $this->client->address();
if ($address != null) {
return $address['address'];
}
return 'Invalid';
} }
/** /**
...@@ -182,11 +149,7 @@ class Wallet ...@@ -182,11 +149,7 @@ class Wallet
*/ */
public function createIntegratedAddress() public function createIntegratedAddress()
{ {
try { return $this->client->createIntegratedAddress();
return $this->client->make_integrated_address();
} catch (\Throwable $e) {
return false;
}
} }
/** /**
...@@ -206,12 +169,11 @@ class Wallet ...@@ -206,12 +169,11 @@ class Wallet
/** /**
* gets all the payment_ids outstanding from the address_pool, we use these to check against the latest mined blocks * gets all the payment_ids outstanding from the address_pool, we use these to check against the latest mined blocks
* *
* @return array * @return Collection
*/ */
public function getPaymentIds() public function getPaymentIds()
{ {
$paymentIds = Project::pluck('payment_id'); //stop scanning for payment_ids after 24h
return $paymentIds; return Project::pluck('payment_id'); //stop scanning for payment_ids after 24h
} }
} }
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
namespace Monero; namespace Monero;
//json 2.0 rpc client use GuzzleHttp\Client;
use App\Exceptions\ConnectionException; use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
/** /**
* Class jsonRPCClient * Class jsonRPCClient
...@@ -11,215 +12,181 @@ use App\Exceptions\ConnectionException; ...@@ -11,215 +12,181 @@ use App\Exceptions\ConnectionException;
*/ */
class jsonRPCClient class jsonRPCClient
{ {
/**
* @var
*/
private $url;
/** /** @var string */
* @var int private $username;
*/
private $timeout;
/** /** @var string */
* @var bool private $password;
*/
private $debug; /** @var Client|null */
private $client;
/** /**
* @var * JsonRPCClient constructor.
* @param null $client
*/ */
private $username; public function __construct($client = null)
{
if (empty($client)) {
$client = new Client([
'base_uri' => env('RPC_URL'),
]);
}
$this->username = env('MONERO_USERNAME');
$this->password = env('MONERO_PASSWORD');
$this->client = $client;
}
/** /**
* @var * Gets the balance
*
* @return int the overall value after inputs unlock
*/ */
private $password; public function balance() : int
{
$response = $this->request('get_balance');
return $response['balance'];
}
/** /**
* @var array * Gets the unlocked balance
*
* @return int the spendable balance
*/ */
private $headers = [ public function unlockedBalance() : int
'Connection: close', {
'Content-Type: application/json', $response = $this->request('get_balance');
'Accept: application/json', return $response['unlocked_balance'];
]; }
/** /**
* jsonRPCClient constructor. * Gets the primary address
* *
* @param $url * @return string wallets primary address
* @param int $timeout
* @param bool|false $debug
* @param array $headers
*/ */
public function __construct($url, $timeout = 20, $debug = false, $headers = []) public function address() : string
{ {
$this->url = $url; $response = $this->request('get_address');
$this->timeout = $timeout; return $response['address'];
$this->debug = $debug;
$this->headers = array_merge($this->headers, $headers);
} }
/** /**
* Generic method executor * Gets the current block height
*
* @param $method
* @param $params
* *
* @return null * @return int block height
*/ */
public function __call($method, $params) public function blockHeight() : int
{ {
return $this->execute($method, $params); $response = $this->request('get_height');
return $response['height'];
} }
/** /**
* Set auth credentials * Creates a new integrated address
* *
* @param $username * @return array ['integrated_address', 'payment_id']
* @param $password
*/ */
public function authentication($username, $password) public function createIntegratedAddress() : array
{ {
$this->username = $username; $response = $this->request('make_integrated_address');
$this->password = $password; return $response;
} }
/** /**
* Get XMR bulk payments * Gets any incoming transactions
*
* @param $payment_ids
* @param $block_height
* *
* @return null * @return array
*/ */
public function bulk_payments($payment_ids, $block_height) public function incomingTransfers() : array
{ {
return $this->execute('get_bulk_payments', $payment_ids, $block_height); $response = $this->request('get_transfers', ['pool' => true, 'in' => true]);
return $response;
} }
/** /**
* Transfer XMR to another destination * Checks for any payments made to the paymentIds
* *
* @param $amount * @param array $paymentIds list of payment ids to be searched for
* @param $destination * @param int $minHeight the lowest block the search should start with
* @param $payment_id
* @param $mixin
* @param int $unlock_time
* *
* @return string * @return array payments received since min block height with a payment id provided
*/ */
public function transferXMR($amount, $destination, $payment_id, $mixin, $unlock_time = 0) public function payments($paymentIds, $minHeight) : array
{ {
$dest = ['amount' => intval(0 + $amount * 1000000000000), 'address' => $destination]; $response = $this->request('get_bulk_payments', ['payment_ids' => $paymentIds, 'min_block_height' => $minHeight]);
$params = [
'destinations' => [$dest],
'payment_id' => $payment_id,
'mixin' => $mixin,
'unlock_time' => $unlock_time,
];
$response = $this->execute('transfer', $params);
$tx = trim($response['tx_hash'], '<>');
return $tx; return $response;
} }
public function mempoolTransfers() /**
* creates a uri for easier wallet parsing
*
* @param string $address address comprising of primary, sub or integrated address
* @param string $paymentId payment id when not using integrated addresses
* @param int $amount atomic amount requested
*
* @return string the uri string which can be used to generate a QR code
*/
public function createUri($address, $paymentId = null, $amount = null) : string
{ {
$response = $this->execute('get_transfers', ['pool' => true]); $response = $this->request('make_uri', ['address' => $address, 'amount' => $amount, 'payment_id' => $paymentId]);
return $response; return $response['uri'];
} }
/** /**
* Prepares the payload for CURL and evaluates the result from the RPC * Sets up the request data body
*
* @param $procedure
* @param $params
* @param null $params2
* *
* @return null * @param string $method name of the rpc command
* @param array $params associative array of variables being passed to the method
* *
* @throws WalletErrorException * @return false|string will return a json string or false
*/ */
public function execute($procedure, $params, $params2 = null) private function preparePayload($method, $params)
{ {
$id = mt_rand();
$payload = [ $payload = [
'jsonrpc' => '2.0', 'jsonrpc' => '2.0',
'method' => $procedure, 'id' => '0',
'id' => $id, 'method' => $method,
'params' => $params,
]; ];
return json_encode($payload);
if (!empty($params)) {
if ($params2 != null) {
$payload['params']['payment_ids'] = $params;
$payload['params']['min_block_height'] = $params2;
} else {
if (is_array($params)) {
// no keys
//$params = array_values($params);
$payload['params'] = $params;
}
}
}
if ($this->debug) {
print_r($payload);
}
$result = $this->doRequest($payload);
if (isset($result['id']) && $result['id'] == $id && array_key_exists('result', $result)) {
if ($this->debug) {
print_r($result['result']);
}
return $result['result'];
}
if (isset($result['error'])) {
throw new ConnectionException($result['error']['message']);
}
throw new ConnectionException('no response');
} }
/** /**
* Executes the CURL request.
* *
* @param $payload * @param string $method name of the rpc command
* @param array $params associative array of variables being passed to the method
*
* @return mixed the rpc query result
* *
* @return array|mixed * @throws \RuntimeException
*/ */
public function doRequest($payload) protected function request(string $method, array $params = [])
{ {
$ch = curl_init(); $payload = $this->preparePayload($method, $params);
curl_setopt($ch, CURLOPT_URL, $this->url); try {
curl_setopt($ch, CURLOPT_HEADER, false); $response = $this->client->request('POST', '',[
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 'auth' => [$this->username, $this->password, 'digest'],
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); 'body' => $payload,
curl_setopt($ch, CURLOPT_USERAGENT, 'JSON-RPC PHP Client'); 'headers' => [
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 'Content-Type' => 'application/json',
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); ]
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); ]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_VERBOSE, true); $body = $response->getBody();
if ($this->username && $this->password) { } catch (GuzzleException $exception) {
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password); Log::error($exception);
throw new \RuntimeException('Connection to node unsuccessful');
} }
$result = json_decode((string) $body, true);
if (isset($result['error'])) {
if ($this->debug) { throw new \RuntimeException($result['error']['message']);
print_r(json_encode($payload)."\n");
print_r($ch);
} }
return $result['result'];
$result = curl_exec($ch);
$response = json_decode($result, true);
curl_close($ch);
return is_array($response) ? $response : [];
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment