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 (54)
Showing
with 501 additions and 164 deletions
......@@ -38,11 +38,10 @@ PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
MONERO_USERNAME=
MONERO_PASSWORD=
COIN=monero
RPC_URL=http://127.0.0.1:28080/json_rpc
RPC_USER=
RPC_PASSWORD=
WALLET_ADDRESS=
GITLAB_URL=https://repo.getmonero.org/api/v4/projects/54
GITLAB_ACCESS_TOKEN=
\ No newline at end of file
REPOSITORY_URL=https://repo.getmonero.org/api/v4/projects/54
GITHUB_ACCESS_TOKEN=
[submodule "storage/app/ffs-proposals"]
path = storage/app/ffs-proposals
url = https://repo.getmonero.org/monero-project/ffs-proposals.git
@task('pull')
git -C "storage/app/ffs-proposals" pull origin master
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));
}
}
......@@ -2,10 +2,10 @@
namespace App\Console\Commands;
use App\Coin\CoinAuto;
use App\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Monero\WalletOld;
class GenerateAddresses extends Command
{
......@@ -40,18 +40,30 @@ class GenerateAddresses extends Command
*/
public function handle()
{
$projects = Project::whereNotNull('filename')->whereNull('payment_id')->where('state', 'FUNDING-REQUIRED')->get();
$wallet = new WalletOld();
foreach ($projects as $project) {
$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 = $addressDetails['address'];
$project->payment_id = $addressDetails['paymentId'];
Storage::disk('public')->put("/img/qrcodes/{$project->payment_id}.png", $project->generateQrcode());
$project->qr_code = "img/qrcodes/{$project->payment_id}.png";
$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';
}
......@@ -3,7 +3,8 @@
namespace App\Console\Commands;
use App\Project;
use GitLab\Connection;
use App\Repository\State;
use App\Repository\Connection;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
......@@ -40,28 +41,36 @@ class ProcessProposals extends Command
$result = [];
$connection = new Connection(new Client());
$merged = $connection->mergeRequests('merged');
$merged = $connection->mergeRequests(State::Merged);
foreach ($merged as $request) {
$newFiles = $connection->getNewFiles($request->iid);
if (sizeof($newFiles) != 1) {
$newFiles = $connection->getNewFiles($request);
if ($newFiles->count() != 1) {
continue;
}
$filename = $newFiles[0];
$filename = $newFiles->first();
if (!preg_match('/.+\.md$/', $filename)) {
continue;
}
if (basename($filename) != $filename) {
continue;
}
$result[$filename] = $request->web_url;
}
$result[$filename] = $request->url();
}
return $result;
}
private const layoutToState = [ 'ffs-fr' => 'FUNDING-REQUIRED',
'ffs-wip' => 'WORK-IN-PROGRESS',
'ffs-cp' => 'COMPLETED'];
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.
*/
......@@ -69,7 +78,7 @@ class ProcessProposals extends Command
{
$mergedMrFilenameToUrlMap = null;
$files = Storage::files('ffs-proposals');
$files = Storage::files('proposals');
foreach ($files as $file) {
if (!strpos($file,'.md')) {
continue;
......@@ -81,10 +90,17 @@ class ProcessProposals extends Command
$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();
......@@ -94,7 +110,7 @@ class ProcessProposals extends Command
}
if (!isset($mergedMrFilenameToUrlMap[$filename])) {
$this->error("Project $filename: failed to find matching merged MR");
$gitlab_url = null;
$gitlab_url = null;
} else {
$gitlab_url = htmlspecialchars($mergedMrFilenameToUrlMap[$filename], ENT_QUOTES);
}
......@@ -108,10 +124,16 @@ class ProcessProposals extends Command
$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()}");
......@@ -120,7 +142,7 @@ class ProcessProposals extends Command
}
/**
* Gets the ffs variables out the top of the file
* Gets the proposal variables out the top of the file
*
* @param string $filename
* @return array
......@@ -130,7 +152,7 @@ class ProcessProposals extends Command
{
$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");
throw new \Exception("Failed to parse proposal, can't find YAML description surrounded by '---' lines");
}
return Yaml::parse($contents[1]);
}
......
......@@ -3,8 +3,9 @@
namespace App\Console\Commands;
use App\Project;
use App\Repository\State;
use App\Repository\Connection;
use Illuminate\Console\Command;
use GitLab\Connection;
use GuzzleHttp\Client;
use stdClass;
use Symfony\Component\Yaml\Yaml;
......@@ -16,7 +17,7 @@ class UpdateSiteProposals extends Command
*
* @var string
*/
protected $signature = 'ffs:update';
protected $signature = 'proposal:update';
/**
* The console command description.
......@@ -42,14 +43,33 @@ class UpdateSiteProposals extends Command
{
$response = [
$this->ideaProposals(),
$this->fundingRequiredProposals(),
$this->workInProgressProposals(),
$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('ffs.json', $json);
\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);
}
public function ideaProposals()
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';
......@@ -57,91 +77,78 @@ class UpdateSiteProposals extends Command
$ideas = [];
$connection = new Connection(new Client());
$mergeRequests = $connection->mergeRequests('opened');
$mergeRequests = $connection->mergeRequests(State::Opened);
foreach ($mergeRequests as $mergeRequest) {
$newFiles = $connection->getNewFiles($mergeRequest->iid);
if (sizeof($newFiles) != 1) {
$this->error ("Skipping MR #$mergeRequest->id '$mergeRequest->title': contains multiple files");
$newFiles = $connection->getNewFiles($mergeRequest);
if ($newFiles->count() != 1) {
$this->error ("Skipping MR #{$mergeRequest->id()} '{$mergeRequest->title()}': contains multiple files");
continue;
}
$filename = $newFiles[0];
$filename = $newFiles->first();
if (!preg_match('/.+\.md$/', $filename)) {
$this->error("Skipping MR #$mergeRequest->id '$mergeRequest->title': doesn't contain any .md file");
$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");
$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");
$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->error("Skipping MR #$mergeRequest->id '$mergeRequest->title': already have a project $filename");
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");
$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->web_url, ENT_QUOTES);
$prop->author = htmlspecialchars($mergeRequest->author->username, ENT_QUOTES);
$prop->date = date('F j, Y', strtotime($mergeRequest->created_at));
$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 = $responseProposals;
$group->proposals = $this->sortProposalsByDateDesc($responseProposals);
return $group;
}
public function fundingRequiredProposals()
private function formatProposal($proposal)
{
$group = new stdClass();
$group->stage = 'Funding Required';
$responseProposals = [];
$proposals = Project::where('state', 'FUNDING-REQUIRED')->get();
foreach ($proposals as $proposal) {
$prop = new stdClass();
$prop->name = $proposal->title;
$prop->{'gitlab-url'} = $proposal->gitlab_url;
$prop->{'local-url'} = '#';
$prop->{'donate-url'} = url("projects/{$proposal->payment_id}/donate");
$prop->percentage = $proposal->percentage_funded;
$prop->amount = $proposal->target_amount;
$prop->{'amount-funded'} = $proposal->amount_received;
$prop->author = $proposal->author;
$prop->date = $proposal->created_at->format('F j, Y');
$responseProposals[] = $prop;
}
$group->proposals = $responseProposals;
return $group;
$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;
}
public function workInProgressProposals()
private function getProposals($stage, $state)
{
$group = new stdClass();
$group->stage = 'Work in Progress';
$group->stage = $stage;
$responseProposals = [];
$proposals = Project::where('state', 'WORK-IN-PROGRESS')->get();
$proposals = Project::where('state', $state)->get();
foreach ($proposals as $proposal) {
$prop = new stdClass();
$prop->name = $proposal->title;
$prop->{'gitlab-url'} = $proposal->gitlab_url;
$prop->{'local-url'} = '#';
$prop->milestones = 0;
$prop->{'milestones-completed'} = 0;
$prop->{'milestones-percentage'} = 0;
$prop->percentage = $proposal->percentage_funded;
$prop->amount = $proposal->target_amount;
$prop->{'amount-funded'} = $proposal->amount_received;
$prop->author = $proposal->author;
$prop->date = $proposal->created_at->format('F j, Y');
$responseProposals[] = $prop;
if ($this->proposalFileExists($proposal->filename)) {
$responseProposals[] = $this->formatProposal($proposal);
}
}
$group->proposals = $responseProposals;
$group->proposals = $this->sortProposalsByDateDesc($responseProposals);
return $group;
}
}
......@@ -2,27 +2,30 @@
namespace App\Console\Commands;
use App\Coin\CoinAuto;
use App\Deposit;
use App\Project;
use Illuminate\Console\Command;
use Monero\Transaction;
use Monero\WalletOld;
class walletNotify extends Command
{
private $coin;
private $wallet;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monero:notify';
protected $signature = 'wallet:notify';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks the monero blockchain for transactions';
protected $description = 'Checks the blockchain for transactions';
/**
* Create a new command instance.
......@@ -32,6 +35,9 @@ class walletNotify extends Command
public function __construct()
{
parent::__construct();
$this->coin = CoinAuto::newCoin();
$this->wallet = $this->coin->newWallet();
}
/**
......@@ -41,30 +47,18 @@ class walletNotify extends Command
*/
public function handle()
{
$wallet = new WalletOld();
$blockheight = $wallet->blockHeight();
$blockheight = $this->wallet->blockHeight();
if ($blockheight < 1) {
$this->error('monero daemon down or wrong port in db ?');
$this->error('failed to fetch blockchain height');
return;
}
// check mempool
$transactionsMempool = $wallet->scanMempool($blockheight);
$transactionsMempool->each(function ($transaction) use ($wallet) {
$transactions = $this->coin->onNotifyGetTransactions($this, $this->wallet);
$transactions->each(function ($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);
}
......@@ -75,20 +69,27 @@ class walletNotify extends Command
*/
public function processPayment(Transaction $transaction)
{
// if the deposit exist, no need to try add it again
if (Deposit::where('tx_id', $transaction->id)->exists()) {
$amountCoins = $transaction->amount / 10 ** $this->wallet->digitsAfterTheRadixPoint();
$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;
}
$this->info('amount: '.$transaction->amount / 1000000000000 .' confirmations:'.$transaction->confirmations.' tx_hash:'.$transaction->id);
$this->info('paymentid: '.$transaction->paymentId);
$this->createDeposit($transaction);
$project = Project::where('payment_id', $transaction->paymentId)->first();
if ($project) {
if ($project = Project::where('subaddr_index', $transaction->subaddr_index)->first()) {
// update the project total
$project->raised_amount = $project->raised_amount + $transaction->amount;
$project->raised_amount = $project->raised_amount + $amountCoins;
$project->save();
$this->info('Donation to "' . $project->filename . '" '. $details);
} else {
$this->error('Unrecognized donation, ' . $details);
}
return;
......@@ -105,8 +106,8 @@ class walletNotify extends Command
{
$count = 0;
//update all xmr deposit confirmations
Deposit::where('confirmations', '<', 10)
->where('processed', 0)
Deposit::where('confirmations', '<', 50)
->where('block_received', '>', 0)
->each(function ($deposit) use ($blockheight, &$count) {
$this->updateConfirmation($blockheight, $deposit);
$count++;
......@@ -125,7 +126,7 @@ class walletNotify extends Command
*/
public function updateConfirmation($blockheight, Deposit $deposit)
{
$diff = $blockheight - $deposit->block_received;
$diff = $blockheight - $deposit->block_received + 1;
$deposit->confirmations = $diff;
$deposit->save();
......@@ -141,15 +142,14 @@ class walletNotify extends Command
*/
public function createDeposit(Transaction $transaction)
{
return Deposit::create([
'tx_id' => $transaction->id,
'amount' => $transaction->amount,
'confirmations' => $transaction->confirmations,
'payment_id' => $transaction->paymentId,
'time_received' => $transaction->time_received,
'block_received' => $transaction->blockHeight,
]);
$deposit = new Deposit;
$deposit->tx_id = $transaction->id;
$deposit->amount = $transaction->amount;
$deposit->confirmations = $transaction->confirmations;
$deposit->subaddr_index = $transaction->subaddr_index;
$deposit->time_received = $transaction->time_received;
$deposit->block_received = $transaction->block_height;
$deposit->save();
}
}
......@@ -28,14 +28,14 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command(GenerateAddresses::class)
->everyMinute();
$schedule->command(ProcessProposals::class)
->everyMinute();
$schedule->command(UpdateSiteProposals::class)
$schedule->command(GenerateAddresses::class)
->everyMinute();
$schedule->command(walletNotify::class)
->everyMinute();
$schedule->command(UpdateSiteProposals::class)
->everyMinute();
}
/**
......
......@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model;
* App\Deposit
*
* @property int $id
* @property string $payment_id
* @property int $subaddr_index
* @property string $amount
* @property string $time_received
* @property string $tx_id
......@@ -28,6 +28,10 @@ use Illuminate\Database\Eloquent\Model;
*/
class Deposit extends Model
{
protected $fillable = [
'tx_id',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
......
......@@ -17,26 +17,19 @@ class FundingController extends Controller
*/
public function index(Request $request)
{
$projects = Project::paginate(15);
// If the request has header `Accept: */json`, return JSON
if ($request->wantsJson())
{
return ProjectResource::collection($projects);
}
return view('projects.index')
->with('projects', $projects);
return ProjectResource::collection(Project::all());
}
/**
* Shows the project based on the payment id
*
* @param Request $request
* @param $paymentId
* @param $subaddr_index
* @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())
{
......@@ -47,9 +40,9 @@ class FundingController extends Controller
->with('project', $project);
}
public function donate(Request $request, $paymentId)
public function donate(Request $request, $subaddr_index)
{
$project = Project::where('payment_id', $paymentId)->firstOrFail();
$project = Project::where('subaddr_index', $subaddr_index)->firstOrFail();
if ($request->wantsJson())
{
......
......@@ -15,13 +15,15 @@ class ProjectResource extends JsonResource
public function toArray($request)
{
return [
'payment_id' => $this->payment_id,
'status' => $this->status,
'amount_received' => $this->amount_received,
'target_amount' => $this->target_amount,
'percentage_funded' => $this->percentage_funded,
'qrcode' => ['base64' => base64_encode($this->qrcode)],
'address' => $this->address,
'author' => $this->author,
'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;
* App\ProjectResource
*
* @property int $id
* @property string $payment_id
* @property int $subaddr_index
* @property string $target_amount
* @property string $status
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Deposit[] $deposits
* @property-read mixed $amount_received
* @property-read string $uri
* @property-read mixed $raised_amount
* @property-read string $address_uri
* @property-read int $percentage_funded
* @property-read int $contributions
* @property-read string $qrcode
......@@ -43,15 +43,11 @@ class Project extends Model
*/
public function deposits()
{
return $this->hasMany(Deposit::class, 'payment_id', 'payment_id');
}
public function getAmountReceivedAttribute() {
return $this->deposits->sum('amount');
return $this->hasMany(Deposit::class, 'subaddr_index', 'subaddr_index');
}
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() {
......@@ -59,7 +55,7 @@ class Project extends Model
}
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() {
......
<?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;
});
}
}