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;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTargetAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereUpdatedAt($value)
* @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
{
protected $guarded = ['id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
......
......@@ -3,11 +3,14 @@
use Faker\Generator as Faker;
$factory->define(\App\Project::class, function (Faker $faker) {
$status = $faker->randomElement(['opened', 'closed', 'locked', 'merged']);
return [
'title' => $faker->sentence(),
'payment_id' => $faker->sha256,
'target_amount' => $faker->randomNumber(),
'status' => $faker->randomElement(['new', 'open', 'funded']),
'created_at' => $faker->dateTime,
'updated_at' => $faker->dateTime,
'target_amount' => $faker->randomFloat(2, 0, 2000),
'state' => $status,
'merge_request_id' => $faker->randomNumber(6),
'created_at' => $faker->dateTimeThisYear,
'updated_at' => $faker->dateTimeThisYear,
];
});
......@@ -15,9 +15,12 @@ class CreateProjectsTable extends Migration
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('payment_id')->unique();
$table->string('target_amount');
$table->string('status')->default('open');
$table->string('title');
$table->string('payment_id')->nullable();
$table->string('address')->nullable();
$table->string('target_amount')->nullable();
$table->string('state')->default('opened');
$table->unsignedInteger('merge_request_id')->unique();
$table->timestamps();
});
}
......
......@@ -4,6 +4,7 @@ namespace Monero;
use App\Project;
use Carbon\Carbon;
use Illuminate\Support\Collection;
class Wallet
{
......@@ -38,27 +39,6 @@ class Wallet
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)
*
......@@ -66,22 +46,19 @@ class Wallet
*/
public function balance()
{
$balance = $this->client->getbalance();
$result = $balance['unlocked_balance'] / 1000000000000;
return $result;
return $this->client->balance();
}
public function mempoolTransfers()
{
return $this->client->mempoolTransfers();
return $this->client->incomingTransfers();
}
public function bulkPayments($paymentIds)
{
$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
*/
public function blockHeight()
{
$result = $this->client->getheight();
if ($result && isset($result['height'])) {
return $result['height'];
}
return 0;
return $this->client->blockHeight();
}
/**
......@@ -167,12 +139,7 @@ class Wallet
*/
public function getAddress()
{
$address = $this->client->getaddress();
if ($address != null) {
return $address['address'];
}
return 'Invalid';
return $this->client->address();
}
/**
......@@ -182,11 +149,7 @@ class Wallet
*/
public function createIntegratedAddress()
{
try {
return $this->client->make_integrated_address();
} catch (\Throwable $e) {
return false;
}
return $this->client->createIntegratedAddress();
}
/**
......@@ -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
*
* @return array
* @return Collection
*/
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 @@
namespace Monero;
//json 2.0 rpc client
use App\Exceptions\ConnectionException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
/**
* Class jsonRPCClient
......@@ -11,215 +12,181 @@ use App\Exceptions\ConnectionException;
*/
class jsonRPCClient
{
/**
* @var
*/
private $url;
/**
* @var int
*/
private $timeout;
/** @var string */
private $username;
/**
* @var bool
*/
private $debug;
/** @var string */
private $password;
/** @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 = [
'Connection: close',
'Content-Type: application/json',
'Accept: application/json',
];
public function unlockedBalance() : int
{
$response = $this->request('get_balance');
return $response['unlocked_balance'];
}
/**
* jsonRPCClient constructor.
* Gets the primary address
*
* @param $url
* @param int $timeout
* @param bool|false $debug
* @param array $headers
* @return string wallets primary address
*/
public function __construct($url, $timeout = 20, $debug = false, $headers = [])
public function address() : string
{
$this->url = $url;
$this->timeout = $timeout;
$this->debug = $debug;
$this->headers = array_merge($this->headers, $headers);
$response = $this->request('get_address');
return $response['address'];
}
/**
* 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
* @param $password
* @return array ['integrated_address', 'payment_id']
*/
public function authentication($username, $password)
public function createIntegratedAddress() : array
{
$this->username = $username;
$this->password = $password;
$response = $this->request('make_integrated_address');
return $response;
}
/**
* Get XMR bulk payments
*
* @param $payment_ids
* @param $block_height
* Gets any incoming transactions
*
* @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 $destination
* @param $payment_id
* @param $mixin
* @param int $unlock_time
* @param array $paymentIds list of payment ids to be searched for
* @param int $minHeight the lowest block the search should start with
*
* @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];
$params = [
'destinations' => [$dest],
'payment_id' => $payment_id,
'mixin' => $mixin,
'unlock_time' => $unlock_time,
];
$response = $this->execute('transfer', $params);
$tx = trim($response['tx_hash'], '<>');
$response = $this->request('get_bulk_payments', ['payment_ids' => $paymentIds, 'min_block_height' => $minHeight]);
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
*
* @param $procedure
* @param $params
* @param null $params2
* Sets up the request data body
*
* @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 = [
'jsonrpc' => '2.0',
'method' => $procedure,
'id' => $id,
'id' => '0',
'method' => $method,
'params' => $params,
];
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');
return json_encode($payload);
}
/**
* 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 array|mixed
* @return mixed the rpc query result
*
* @throws \RuntimeException
*/
public function doRequest($payload)
protected function request(string $method, array $params = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_USERAGENT, 'JSON-RPC PHP Client');
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
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);
if ($this->username && $this->password) {
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
}
$payload = $this->preparePayload($method, $params);
try {
$response = $this->client->request('POST', '',[
'auth' => [$this->username, $this->password, 'digest'],
'body' => $payload,
'headers' => [
'Content-Type' => 'application/json',
]
]);
$body = $response->getBody();
} catch (GuzzleException $exception) {
Log::error($exception);
throw new \RuntimeException('Connection to node unsuccessful');
}
$result = json_decode((string) $body, true);
if (isset($result['error'])) {
if ($this->debug) {
print_r(json_encode($payload)."\n");
print_r($ch);
throw new \RuntimeException($result['error']['message']);
}
$result = curl_exec($ch);
$response = json_decode($result, true);
curl_close($ch);
return is_array($response) ? $response : [];
return $result['result'];
}
}
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