walletnotify.php 3.88 KB
Newer Older
beardedwarrior's avatar
beardedwarrior committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php

namespace App\Console\Commands;

use App\Deposit;
use App\Project;
use Illuminate\Console\Command;
use Monero\Transaction;
use Monero\Wallet;

class walletnotify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'monero:notify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Checks the monero blockchain for transactions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $wallet = new Wallet();

        $blockheight = $wallet->blockHeight();
        if ($blockheight < 1) {
            $this->error('monero daemon down or wrong port in db ?');

            return;
        }

        // check mempool
        $transactionsMempool = $wallet->scanMempool($blockheight);
        $transactionsMempool->each(function ($transaction) use ($wallet) {
            $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);
    }

    /**
     * @param Transaction $transaction
     *
     * @return null|void
     */
    public function processPayment(Transaction $transaction)
    {
        // if the deposit exist, no need to try add it again
        if (Deposit::where('tx_id', $transaction->id)->exists()) {
            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) {
            // update the project total
        }

        return;
    }

    /**
     * Adds confirmations on for all xmr transactions with confirmations below 50
     *
     * @param int blockheight
     *
     * @return int
     */
    public function updateAllConfirmations($blockheight)
    {
        $count = 0;
        //update all xmr deposit confirmations
        Deposit::where('confirmations', '<', 10)
            ->where('processed', 0)
            ->each(function ($deposit) use ($blockheight, &$count) {
                $this->updateConfirmation($blockheight, $deposit);
                $count++;
            });

        return $count;
    }

    /**
     * Updates the confirmations for the deposit and calls the process method if it is not assigned to a payflow
     *
     * @param $blockheight
     * @param Deposit $deposit
     *
     * @return bool
     */
    public function updateConfirmation($blockheight, Deposit $deposit)
    {
        $diff = $blockheight - $deposit->block_received;
        $deposit->confirmations = $diff;
        $deposit->save();

        return false;
    }

    /**
     * Creates a deposit entry in the deposit table
     *
     * @param Transaction $transaction
     *
     * @return bool
     */
    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,
        ]);

    }

}