Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • monero-project/ccs-back
  • xiphon/ccs-back
  • Fudin/ccs-back
  • john_r365/ccs-back
  • plowsofff/ccs-back
5 results
Show changes
Commits on Source (78)
Showing
with 873 additions and 79 deletions
...@@ -38,8 +38,10 @@ PUSHER_APP_CLUSTER=mt1 ...@@ -38,8 +38,10 @@ PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
RPC_URL= COIN=monero
WALLET_ADDRESS= RPC_URL=http://127.0.0.1:28080/json_rpc
RPC_USER=
RPC_PASSWORD=
GITLAB_URL= REPOSITORY_URL=https://repo.getmonero.org/api/v4/projects/54
GITLAB_ACCESS_TOKEN= GITHUB_ACCESS_TOKEN=
\ No newline at end of file
@task('pull')
git -C "storage/app/proposals" pull origin master
@endtask
\ No newline at end of file
BSD 3-Clause License
Copyright (c) 2021, monero-project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<?php
namespace App\Coin;
use Illuminate\Console\Command;
use Monero\WalletCommon;
interface Coin
{
public function newWallet() : WalletCommon;
public function onNotifyGetTransactions(Command $command, WalletCommon $wallet);
public function subaddrIndex($addressDetails, $project);
}
class CoinAuto
{
public static function newCoin() : Coin
{
$coin = env('COIN', 'monero');
switch ($coin) {
case 'monero':
return new CoinMonero();
case 'zcoin':
return new CoinZcoin();
default:
throw new \Exception('Unsupported COIN ' . $coin);
}
}
}
<?php
namespace App\Coin;
use App\Deposit;
use Illuminate\Console\Command;
use Monero\WalletCommon;
use Monero\WalletOld;
class CoinMonero implements Coin
{
public function newWallet() : WalletCommon
{
return new WalletOld();
}
public function onNotifyGetTransactions(Command $command, WalletCommon $wallet)
{
$min_height = $command->arguments()['height'] ?? Deposit::max('block_received');
return $wallet->scanIncomingTransfers(max($min_height, 50) - 50);
}
public function subaddrIndex($addressDetails, $project)
{
return $addressDetails['subaddr_index'];
}
}
<?php
namespace App\Coin;
use App\Deposit;
use App\Project;
use Illuminate\Console\Command;
use Monero\WalletCommon;
use Monero\WalletZcoin;
class CoinZcoin implements Coin
{
public function newWallet() : WalletCommon
{
return new WalletZcoin();
}
public function onNotifyGetTransactions(Command $command, WalletCommon $wallet)
{
return $wallet->scanIncomingTransfers()->each(function ($tx) {
$project = Project::where('address', $tx->address)->first();
if ($project) {
$tx->subaddr_index = $project->subaddr_index;
}
});
}
public function subaddrIndex($addressDetails, $project)
{
return $project->id;
}
}
<?php
namespace App\Console\Commands;
use App\Deposit;
use Illuminate\Console\Command;
class depositList extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'deposit:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Print all deposits in JSON format';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info(Deposit::all()->toJson(JSON_PRETTY_PRINT));
}
}
<?php
namespace App\Console\Commands;
use App\Coin\CoinAuto;
use App\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class GenerateAddresses extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:addresses';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generates monero addresses for any merged proposals';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$coin = CoinAuto::newCoin();
$wallet = $coin->newWallet();
$projects = Project::whereNotNull('filename')->whereNull('address')->where('state', 'FUNDING-REQUIRED')->get();
foreach ($projects as $project) {
$addressDetails = $wallet->getPaymentAddress();
$address = $addressDetails['address'];
$subaddr_index = $coin->subaddrIndex($addressDetails, $project);
if (Project::where('address', $address)->orWhere('subaddr_index', $subaddr_index)->first())
{
$this->error('Skipping already used address ' . $address . ' or subaddr_index ' . $subaddr_index);
continue;
}
$project->address_uri = $wallet->createQrCodeString($addressDetails['address']);
$project->address = $address;
$project->subaddr_index = $subaddr_index;
Storage::disk('public')->put("/img/qrcodes/{$project->subaddr_index}.png", $project->generateQrcode());
$project->qr_code = "img/qrcodes/{$project->subaddr_index}.png";
$project->raised_amount = 0;
$project->save();
$this->info('Project: ' . $project->filename . ', address: ' . $project->address);
}
}
}
<?php
namespace App\Console\Commands;
class moneroNotify extends walletNotify
{
private $coin;
private $wallet;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monero:notify
{height? : Scan wallet transactions starting from the specified height}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks the monero blockchain for transactions';
}
<?php
namespace App\Console\Commands;
use App\Project;
use App\Repository\State;
use App\Repository\Connection;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Yaml;
class ProcessProposals extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'proposal:process';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for changes to proposals';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
private function getMergedMrFilenameToUrlMap()
{
$result = [];
$connection = new Connection(new Client());
$merged = $connection->mergeRequests(State::Merged);
foreach ($merged as $request) {
$newFiles = $connection->getNewFiles($request);
if ($newFiles->count() != 1) {
continue;
}
$filename = $newFiles->first();
if (!preg_match('/.+\.md$/', $filename)) {
continue;
}
if (basename($filename) != $filename) {
continue;
}
$result[$filename] = $request->url();
}
return $result;
}
private const layoutToState = [ 'fr' => 'FUNDING-REQUIRED',
'wip' => 'WORK-IN-PROGRESS',
'cp' => 'COMPLETED'];
private const mandatoryFields = [ 'amount',
'author',
'date',
'layout',
'milestones',
'title'];
/**
* Execute the console command.
*/
public function handle()
{
$mergedMrFilenameToUrlMap = null;
$files = Storage::files('proposals');
foreach ($files as $file) {
if (!strpos($file,'.md')) {
continue;
}
$filename = basename($file);
try {
$detail['name'] = $filename;
$detail['values'] = $this->getAmountFromText($file);
foreach ($this::mandatoryFields as $field) {
if (empty($detail['values'][$field])) {
throw new \Exception("Mandatory field $field is missing");
}
}
$amount = floatval(str_replace(",", ".", $detail['values']['amount']));
$author = htmlspecialchars($detail['values']['author'], ENT_QUOTES);
$date = strtotime($detail['values']['date']);
$state = $this::layoutToState[$detail['values']['layout']];
$milestones = $detail['values']['milestones'];
$title = htmlspecialchars($detail['values']['title'], ENT_QUOTES);
$project = Project::where('filename', $filename)->first();
if (!$project) {
if ($mergedMrFilenameToUrlMap === null) {
$mergedMrFilenameToUrlMap = $this->getMergedMrFilenameToUrlMap();
}
if (!isset($mergedMrFilenameToUrlMap[$filename])) {
$this->error("Project $filename: failed to find matching merged MR");
$gitlab_url = null;
} else {
$gitlab_url = htmlspecialchars($mergedMrFilenameToUrlMap[$filename], ENT_QUOTES);
}
$this->info("New project $filename Gitlab MR '$gitlab_url'");
$project = new Project();
$project->gitlab_url = $gitlab_url;
$project->created_at = $date;
$project->filename = $filename;
} else {
$this->info("Updating project $filename");
}
if (isset($detail['values']['gitlab_url'])) {
$project->gitlab_url = htmlspecialchars($detail['values']['gitlab_url'], ENT_QUOTES);
}
$project->author = $author;
$project->state = $state;
$project->target_amount = $amount;
$project->title = $title;
$project->milestones = sizeof($milestones);
$project->milestones_completed = array_reduce($milestones, function($k, $milestone) { return $milestone['done'] ? $k + 1 : $k; }, 0);
$project->save();
} catch (\Exception $e) {
$this->error("Error processing project $filename: {$e->getMessage()}");
}
}
}
/**
* Gets the proposal variables out the top of the file
*
* @param string $filename
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getAmountFromText($filename = 'additional-gui-dev.md')
{
$contents = preg_split('/\r?\n?---\r?\n/m', Storage::get($filename));
if (sizeof($contents) < 3) {
throw new \Exception("Failed to parse proposal, can't find YAML description surrounded by '---' lines");
}
return Yaml::parse($contents[1]);
}
}
<?php
namespace App\Console\Commands;
use App\Project;
use App\Repository\State;
use App\Repository\Connection;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
use stdClass;
use Symfony\Component\Yaml\Yaml;
class UpdateSiteProposals extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'proposal:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the files required for jeykll site';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$response = [
$this->ideaProposals(),
$this->getProposals('Funding Required', 'FUNDING-REQUIRED'),
$this->getProposals('Work in Progress', 'WORK-IN-PROGRESS'),
];
$json = json_encode($response, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
\Storage::put('proposals.json', $json);
$response = [
$this->getProposals('Completed Proposals', 'COMPLETED'),
];
$json = json_encode($response, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
\Storage::put('complete.json', $json);
}
private function sortProposalsByDateDesc($responseProposals)
{
usort($responseProposals, function($a, $b){
return strtotime($a->date) < strtotime($b->date) ? 1 : -1;
});
return $responseProposals;
}
private function proposalFileExists($filename)
{
return \Storage::exists('proposals/'. basename($filename));
}
private function ideaProposals()
{
$group = new stdClass();
$group->stage = 'Ideas';
$responseProposals = [];
$ideas = [];
$connection = new Connection(new Client());
$mergeRequests = $connection->mergeRequests(State::Opened);
foreach ($mergeRequests as $mergeRequest) {
$newFiles = $connection->getNewFiles($mergeRequest);
if ($newFiles->count() != 1) {
$this->error ("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': contains multiple files");
continue;
}
$filename = $newFiles->first();
if (!preg_match('/.+\.md$/', $filename)) {
$this->error("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': doesn't contain any .md file");
continue;
}
if (basename($filename) != $filename) {
$this->error("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': $filename must be in the root folder");
continue;
}
if (in_array($filename, $ideas)) {
$this->error("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': duplicated $filename, another MR #$ideas[$filename]->id");
continue;
}
$project = Project::where('filename', $filename)->first();
if ($project && $this->proposalFileExists($filename)) {
$this->error("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': already have a project $filename");
continue;
}
$this->info("Idea MR #{$mergeRequest->id()} '{$mergeRequest->title()}': $filename");
$prop = new stdClass();
$prop->name = htmlspecialchars(trim($mergeRequest->title()), ENT_QUOTES);
$prop->{'gitlab-url'} = htmlspecialchars($mergeRequest->url(), ENT_QUOTES);
$prop->author = htmlspecialchars($mergeRequest->author(), ENT_QUOTES);
$prop->date = date('F j, Y', $mergeRequest->created_at());
$responseProposals[] = $prop;
}
$group->proposals = $this->sortProposalsByDateDesc($responseProposals);
return $group;
}
private function formatProposal($proposal)
{
$prop = new stdClass();
$prop->name = $proposal->title;
$prop->{'donate-address'} = $proposal->address;
$prop->{'donate-qr-code'} = $proposal->address_uri ? $proposal->getQrCodeSrcAttribute() : null;
$prop->{'gitlab-url'} = $proposal->gitlab_url;
$prop->{'local-url'} = '/proposals/'. pathinfo($proposal->filename, PATHINFO_FILENAME) . '.html';
$prop->contributions = $proposal->contributions;
$prop->milestones = $proposal->milestones;
$prop->{'milestones-completed'} = $proposal->milestones_completed;
$milestones_percentage = min(100, (int)(($proposal->milestones_completed * 100) / $proposal->milestones));
$prop->{'milestones-percentage'} = $milestones_percentage;
$prop->percentage = $proposal->percentage_funded;
$prop->amount = $proposal->target_amount;
$prop->{'amount-funded'} = $proposal->raised_amount;
$prop->author = $proposal->author;
$prop->date = $proposal->created_at->format('F j, Y');
return $prop;
}
private function getProposals($stage, $state)
{
$group = new stdClass();
$group->stage = $stage;
$responseProposals = [];
$proposals = Project::where('state', $state)->get();
foreach ($proposals as $proposal) {
if ($this->proposalFileExists($proposal->filename)) {
$responseProposals[] = $this->formatProposal($proposal);
}
}
$group->proposals = $this->sortProposalsByDateDesc($responseProposals);
return $group;
}
}
...@@ -2,27 +2,30 @@ ...@@ -2,27 +2,30 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Coin\CoinAuto;
use App\Deposit; use App\Deposit;
use App\Project; use App\Project;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Monero\Transaction; use Monero\Transaction;
use Monero\Wallet;
class walletnotify extends Command class walletNotify extends Command
{ {
private $coin;
private $wallet;
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'monero:notify'; protected $signature = 'wallet:notify';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Checks the monero blockchain for transactions'; protected $description = 'Checks the blockchain for transactions';
/** /**
* Create a new command instance. * Create a new command instance.
...@@ -32,6 +35,9 @@ class walletnotify extends Command ...@@ -32,6 +35,9 @@ class walletnotify extends Command
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->coin = CoinAuto::newCoin();
$this->wallet = $this->coin->newWallet();
} }
/** /**
...@@ -41,30 +47,18 @@ class walletnotify extends Command ...@@ -41,30 +47,18 @@ class walletnotify extends Command
*/ */
public function handle() public function handle()
{ {
$wallet = new Wallet(); $blockheight = $this->wallet->blockHeight();
$blockheight = $wallet->blockHeight();
if ($blockheight < 1) { if ($blockheight < 1) {
$this->error('monero daemon down or wrong port in db ?'); $this->error('failed to fetch blockchain height');
return; return;
} }
// check mempool $transactions = $this->coin->onNotifyGetTransactions($this, $this->wallet);
$transactionsMempool = $wallet->scanMempool($blockheight); $transactions->each(function ($transaction) {
$transactionsMempool->each(function ($transaction) use ($wallet) {
$this->processPayment($transaction); $this->processPayment($transaction);
}); });
$paymentIDs = $wallet->getPaymentIds();
if (count($paymentIDs)) {
// check blockchain
$transactions = $wallet->scanBlocks($blockheight, $paymentIDs);
$transactions->each(function ($transaction) use ($wallet) {
$this->processPayment($transaction);
});
}
$this->updateAllConfirmations($blockheight); $this->updateAllConfirmations($blockheight);
} }
...@@ -75,18 +69,27 @@ class walletnotify extends Command ...@@ -75,18 +69,27 @@ class walletnotify extends Command
*/ */
public function processPayment(Transaction $transaction) public function processPayment(Transaction $transaction)
{ {
// if the deposit exist, no need to try add it again $amountCoins = $transaction->amount / 10 ** $this->wallet->digitsAfterTheRadixPoint();
if (Deposit::where('tx_id', $transaction->id)->exists()) { $details = 'address: ' . $transaction->address . ' amount: '. $amountCoins . ' txid: '.$transaction->id;
$deposit = Deposit::where('tx_id', $transaction->id)->where('subaddr_index', $transaction->subaddr_index)->first();
if ($deposit) {
if ($deposit->block_received == 0) {
$deposit->block_received = $transaction->block_height;
$deposit->save();
}
return null; return null;
} }
$this->info('amount: '.$transaction->amount / 1000000000000 .' confirmations:'.$transaction->confirmations.' tx_hash:'.$transaction->id);
$this->info('paymentid: '.$transaction->paymentId);
$this->createDeposit($transaction); $this->createDeposit($transaction);
$project = Project::where('payment_id', $transaction->paymentId)->first(); if ($project = Project::where('subaddr_index', $transaction->subaddr_index)->first()) {
if ($project) {
// update the project total // update the project total
$project->raised_amount = $project->raised_amount + $amountCoins;
$project->save();
$this->info('Donation to "' . $project->filename . '" '. $details);
} else {
$this->error('Unrecognized donation, ' . $details);
} }
return; return;
...@@ -103,8 +106,8 @@ class walletnotify extends Command ...@@ -103,8 +106,8 @@ class walletnotify extends Command
{ {
$count = 0; $count = 0;
//update all xmr deposit confirmations //update all xmr deposit confirmations
Deposit::where('confirmations', '<', 10) Deposit::where('confirmations', '<', 50)
->where('processed', 0) ->where('block_received', '>', 0)
->each(function ($deposit) use ($blockheight, &$count) { ->each(function ($deposit) use ($blockheight, &$count) {
$this->updateConfirmation($blockheight, $deposit); $this->updateConfirmation($blockheight, $deposit);
$count++; $count++;
...@@ -123,7 +126,7 @@ class walletnotify extends Command ...@@ -123,7 +126,7 @@ class walletnotify extends Command
*/ */
public function updateConfirmation($blockheight, Deposit $deposit) public function updateConfirmation($blockheight, Deposit $deposit)
{ {
$diff = $blockheight - $deposit->block_received; $diff = $blockheight - $deposit->block_received + 1;
$deposit->confirmations = $diff; $deposit->confirmations = $diff;
$deposit->save(); $deposit->save();
...@@ -135,19 +138,18 @@ class walletnotify extends Command ...@@ -135,19 +138,18 @@ class walletnotify extends Command
* *
* @param Transaction $transaction * @param Transaction $transaction
* *
* @return bool * @return Deposit
*/ */
public function createDeposit(Transaction $transaction) public function createDeposit(Transaction $transaction)
{ {
return Deposit::create([ $deposit = new Deposit;
'tx_id' => $transaction->id, $deposit->tx_id = $transaction->id;
'amount' => $transaction->amount, $deposit->amount = $transaction->amount;
'confirmations' => $transaction->confirmations, $deposit->confirmations = $transaction->confirmations;
'payment_id' => $transaction->paymentId, $deposit->subaddr_index = $transaction->subaddr_index;
'time_received' => $transaction->time_received, $deposit->time_received = $transaction->time_received;
'block_received' => $transaction->blockHeight, $deposit->block_received = $transaction->block_height;
]); $deposit->save();
} }
} }
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
namespace App\Console; namespace App\Console;
use App\Console\Commands\GenerateAddresses;
use App\Console\Commands\ProcessProposals;
use App\Console\Commands\UpdateSiteProposals;
use App\Console\Commands\walletNotify;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
...@@ -24,8 +28,14 @@ class Kernel extends ConsoleKernel ...@@ -24,8 +28,14 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// $schedule->command('inspire') $schedule->command(ProcessProposals::class)
// ->hourly(); ->everyMinute();
$schedule->command(GenerateAddresses::class)
->everyMinute();
$schedule->command(walletNotify::class)
->everyMinute();
$schedule->command(UpdateSiteProposals::class)
->everyMinute();
} }
/** /**
......
...@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model; ...@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model;
* App\Deposit * App\Deposit
* *
* @property int $id * @property int $id
* @property string $payment_id * @property int $subaddr_index
* @property string $amount * @property string $amount
* @property string $time_received * @property string $time_received
* @property string $tx_id * @property string $tx_id
...@@ -28,6 +28,10 @@ use Illuminate\Database\Eloquent\Model; ...@@ -28,6 +28,10 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Deposit extends Model class Deposit extends Model
{ {
protected $fillable = [
'tx_id',
];
/** /**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */
......
...@@ -17,26 +17,19 @@ class FundingController extends Controller ...@@ -17,26 +17,19 @@ class FundingController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$projects = Project::paginate(15); return ProjectResource::collection(Project::all());
// If the request has header `Accept: */json`, return JSON
if ($request->wantsJson())
{
return ProjectResource::collection($projects);
}
return view('projects.index')
->with('projects', $projects);
} }
/** /**
* Shows the project based on the payment id * Shows the project based on the payment id
* *
* @param Request $request * @param Request $request
* @param $paymentId * @param $subaddr_index
* @return ProjectResource|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return ProjectResource|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/ */
public function show(Request $request, $paymentId) public function show(Request $request, $subaddr_index)
{ {
$project = Project::where('payment_id', $paymentId)->firstOrFail(); $project = Project::where('subaddr_index', $subaddr_index)->firstOrFail();
if ($request->wantsJson()) if ($request->wantsJson())
{ {
...@@ -46,4 +39,17 @@ class FundingController extends Controller ...@@ -46,4 +39,17 @@ class FundingController extends Controller
return view('projects.show') return view('projects.show')
->with('project', $project); ->with('project', $project);
} }
public function donate(Request $request, $subaddr_index)
{
$project = Project::where('subaddr_index', $subaddr_index)->firstOrFail();
if ($request->wantsJson())
{
return new ProjectResource($project);
}
return view('projects.donate')
->with('project', $project);
}
} }
...@@ -15,13 +15,15 @@ class ProjectResource extends JsonResource ...@@ -15,13 +15,15 @@ class ProjectResource extends JsonResource
public function toArray($request) public function toArray($request)
{ {
return [ return [
'payment_id' => $this->payment_id, 'address' => $this->address,
'status' => $this->status, 'author' => $this->author,
'amount_received' => $this->amount_received,
'target_amount' => $this->target_amount,
'percentage_funded' => $this->percentage_funded,
'qrcode' => ['base64' => base64_encode($this->qrcode)],
'contributions' => $this->contributions, 'contributions' => $this->contributions,
'date' => $this->created_at->format('F j, Y'),
'percentage_funded' => $this->percentage_funded,
'raised_amount' => $this->raised_amount,
'state' => $this->state,
'target_amount' => $this->target_amount,
'title' => $this->title,
]; ];
} }
} }
...@@ -9,14 +9,14 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode; ...@@ -9,14 +9,14 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode;
* App\ProjectResource * App\ProjectResource
* *
* @property int $id * @property int $id
* @property string $payment_id * @property int $subaddr_index
* @property string $target_amount * @property string $target_amount
* @property string $status * @property string $status
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Deposit[] $deposits * @property-read \Illuminate\Database\Eloquent\Collection|\App\Deposit[] $deposits
* @property-read mixed $amount_received * @property-read mixed $raised_amount
* @property-read string $uri * @property-read string $address_uri
* @property-read int $percentage_funded * @property-read int $percentage_funded
* @property-read int $contributions * @property-read int $contributions
* @property-read string $qrcode * @property-read string $qrcode
...@@ -28,7 +28,6 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode; ...@@ -28,7 +28,6 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode;
* @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 string $title
* @property int|null $merge_request_id
* @property string|null $commit_sha * @property string|null $commit_sha
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereCommitSha($value) * @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 whereMergeRequestId($value)
...@@ -37,31 +36,30 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode; ...@@ -37,31 +36,30 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode;
class Project extends Model class Project extends Model
{ {
protected $guarded = ['id']; protected $guarded = ['id'];
protected $dates = ['created_at', 'updated_at'];
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */
public function deposits() public function deposits()
{ {
return $this->hasMany(Deposit::class, 'payment_id', 'payment_id'); return $this->hasMany(Deposit::class, 'subaddr_index', 'subaddr_index');
}
public function getAmountReceivedAttribute() {
return $this->deposits->sum('amount');
}
public function getUriAttribute() {
return 'monero:'.env('WALLET_ADDRESS').'tx_payment_id='.$this->payment_id;
} }
public function getPercentageFundedAttribute() { public function getPercentageFundedAttribute() {
return round($this->amount_received / $this->target_amount * 100); return min(100, round($this->raised_amount / $this->target_amount * 100));
} }
public function getContributionsAttribute() { public function getContributionsAttribute() {
return $this->deposits->count() ?? 0; return $this->deposits->count() ?? 0;
} }
public function getQrcodeAttribute() { public function generateQrcode() {
return QrCode::format('png')->size(500)->generate($this->uri); return QrCode::format('png')->size(500)->generate($this->address_uri);
}
public function getQrCodeSrcAttribute() {
$encoded = base64_encode($this->generateQrcode());
return "data:image/png;base64, {$encoded}";
} }
} }
<?php
namespace App\Repository;
use GuzzleHttp\Client;
class Connection
{
private $repo;
public function __construct(Client $client)
{
$url = env('REPOSITORY_URL') ?? env('GITLAB_URL');
if (parse_url($url, PHP_URL_HOST) == 'github.com') {
$this->repo = new Github($client, $url);
} else {
$this->repo = new Gitlab($client, $url);
}
}
public function mergeRequests($state) {
return $this->repo->mergeRequests($state);
}
public function getNewFiles($merge_request_iid) {
return $this->repo->getNewFiles($merge_request_iid);
}
}
<?php
namespace App\Repository;
use GuzzleHttp\Client;
class PullRequest implements Proposal
{
private $pull_request;
public function __construct($pull_request)
{
$this->pull_request = $pull_request;
}
public function id() : int
{
return $this->pull_request->number;
}
public function url() : string
{
return $this->pull_request->html_url;
}
public function title() : string
{
return $this->pull_request->title;
}
public function author() : string
{
return $this->pull_request->user->login;
}
public function created_at() : int
{
return strtotime($this->pull_request->created_at);
}
}
class Github implements Repository
{
private $client;
private $options = [];
private $owner_repo;
private const stateToString = [ State::Opened => 'open' ,
State::Merged => 'closed'];
public function __construct(Client $client, string $repository_url)
{
$this->client = $client;
$this->owner_repo = parse_url($repository_url, PHP_URL_PATH);
if ($token = env('GITHUB_ACCESS_TOKEN')) {
$this->options = ['headers' => ['Authorization' => 'token ' . $token]];
}
}
private function getUrl($url)
{
return $this->client->request('GET', $url, $this->options);
}
public function mergeRequests($state)
{
$url = 'https://api.github.com/repos' . $this->owner_repo . '/pulls?state=' . Self::stateToString[$state];
$response = $this->getUrl($url);
$result = collect(json_decode($response->getBody()));
if ($state == State::Merged) {
$result = $result->filter(function ($pull_request) {
return $pull_request->merged_at !== null;
});
}
return $result->map(function ($pull_request) {
return new PullRequest($pull_request);
});
}
public function getNewFiles($pull_request)
{
$url = 'https://api.github.com/repos' . $this->owner_repo . '/pulls/' . $pull_request->id() . '/files';
$response = $this->getUrl($url);
return collect(json_decode($response->getBody()))->filter(function ($change) {
return $change->status == 'added';
})->map(function ($change) {
return $change->filename;
});
}
}
<?php
namespace App\Repository;
use GuzzleHttp\Client;
class MergeRequest implements Proposal
{
private $merge_request;
public function __construct($merge_request)
{
$this->merge_request = $merge_request;
}
public function iid() : int
{
return $this->merge_request->iid;
}
public function id() : int
{
return $this->merge_request->id;
}
public function url() : string
{
return $this->merge_request->web_url;
}
public function title() : string
{
return $this->merge_request->title;
}
public function author() : string
{
return $this->merge_request->author->username;
}
public function created_at() : int
{
return strtotime($this->merge_request->created_at);
}
}
class Gitlab implements Repository
{
private $client;
private $base_url;
private const stateToString = [ State::Opened => 'opened',
State::Merged => 'merged'];
public function __construct(Client $client, string $repository_url)
{
$this->client = $client;
$this->base_url = $repository_url;
}
public function mergeRequests($state)
{
$url = $this->base_url . '/merge_requests?scope=all&per_page=50&state=' . Self::stateToString[$state];
$response = $this->client->request('GET', $url);
return collect(json_decode($response->getBody()))->map(function ($merge_request) {
return new MergeRequest($merge_request);
});
}
public function getNewFiles($merge_request)
{
$url = $this->base_url . '/merge_requests/' . $merge_request->iid() . '/changes';
$response = $this->client->request('GET', $url);
return collect(json_decode($response->getBody())->changes)->filter(function ($change) {
return $change->new_file;
})->map(function ($change) {
return $change->new_path;
});
}
}