WalletNotify.php 3.96 KB
Newer Older
beardedwarrior's avatar
beardedwarrior committed
1 2 3 4 5 6 7 8
<?php

namespace App\Console\Commands;

use App\Deposit;
use App\Project;
use Illuminate\Console\Command;
use Monero\Transaction;
9
use Monero\WalletOld;
beardedwarrior's avatar
beardedwarrior committed
10

beardedwarrior's avatar
beardedwarrior committed
11
class walletNotify extends Command
beardedwarrior's avatar
beardedwarrior committed
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
{
    /**
     * 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()
    {
44
        $wallet = new WalletOld();
beardedwarrior's avatar
beardedwarrior committed
45 46 47 48 49 50 51 52

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

            return;
        }

53
        $min_height = Deposit::max('block_received');
54
        $transactions = $wallet->scanIncomingTransfers(max($min_height, 50) - 50);
55
        $transactions->each(function ($transaction) use ($wallet) {
beardedwarrior's avatar
beardedwarrior committed
56 57 58 59 60 61 62 63 64 65 66 67 68
            $this->processPayment($transaction);
        });

        $this->updateAllConfirmations($blockheight);
    }

    /**
     * @param Transaction $transaction
     *
     * @return null|void
     */
    public function processPayment(Transaction $transaction)
    {
69
        $deposit = Deposit::where('tx_id', $transaction->id)->where('subaddr_index', $transaction->subaddr_index)->first();
70 71 72 73 74
        if ($deposit) {
            if ($deposit->block_received == 0) {
                $deposit->block_received = $transaction->block_height;
                $deposit->save();
            }
beardedwarrior's avatar
beardedwarrior committed
75 76
            return null;
        }
77

beardedwarrior's avatar
beardedwarrior committed
78
        $this->info('amount: '.$transaction->amount / 1000000000000 .' confirmations:'.$transaction->confirmations.' tx_hash:'.$transaction->id);
79
        $this->info('subaddr_index: '.$transaction->subaddr_index);
beardedwarrior's avatar
beardedwarrior committed
80 81 82

        $this->createDeposit($transaction);

83
        $project = Project::where('subaddr_index', $transaction->subaddr_index)->first();
beardedwarrior's avatar
beardedwarrior committed
84 85
        if ($project) {
            // update the project total
86
            $project->raised_amount = $project->raised_amount + $transaction->amount * 1e-12;
beardedwarrior's avatar
beardedwarrior committed
87
            $project->save();
beardedwarrior's avatar
beardedwarrior committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        }

        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
104 105
        Deposit::where('confirmations', '<', 50)
            ->where('block_received', '>', 0)
beardedwarrior's avatar
beardedwarrior committed
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
            ->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
     *
beardedwarrior's avatar
beardedwarrior committed
136
     * @return Deposit
beardedwarrior's avatar
beardedwarrior committed
137 138 139
     */
    public function createDeposit(Transaction $transaction)
    {
140 141 142 143
        $deposit = new Deposit;
        $deposit->tx_id = $transaction->id;
        $deposit->amount = $transaction->amount;
        $deposit->confirmations = $transaction->confirmations;
144
        $deposit->subaddr_index = $transaction->subaddr_index;
145 146 147
        $deposit->time_received = $transaction->time_received;
        $deposit->block_received = $transaction->block_height;
        $deposit->save();
beardedwarrior's avatar
beardedwarrior committed
148 149 150
    }

}