Wallet.php 4.4 KB
Newer Older
beardedwarrior's avatar
beardedwarrior committed
1 2 3 4 5 6
<?php

namespace Monero;

use App\Project;
use Carbon\Carbon;
7
use Illuminate\Support\Collection;
beardedwarrior's avatar
beardedwarrior committed
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

class Wallet
{
    /**
     * Wallet constructor.
     *
     * @param null $client
     */
    public function __construct($client = null)
    {
        $this->client = $client ?: new jsonRPCClient(env('RPC_URL'));
    }

    /**
     * Gets a Payment address for receiving payments
     *
     * @return array
     *
     * @internal param \Wallet $wallet
     */
    public function getPaymentAddress()
    {

        $integratedAddress = $this->createIntegratedAddress();
        if (!$integratedAddress) {
            return ['address' => 'not valid', 'expiration_time' => 900];
        }
        $project = new Project();
        $project->payment_id = $integratedAddress['payment_id'];
        $project->save();

        return ['address' => $integratedAddress['integrated_address'], 'paymentId' => $integratedAddress['payment_id']];
    }

    /**
     * Returns the actual available and useable balance (unlocked balance)
     *
     * @return float|int|mixed
     */
    public function balance()
    {
49
        return $this->client->balance();
beardedwarrior's avatar
beardedwarrior committed
50 51 52 53
    }

    public function mempoolTransfers()
    {
54
        return $this->client->incomingTransfers();
beardedwarrior's avatar
beardedwarrior committed
55 56 57 58 59 60
    }

    public function bulkPayments($paymentIds)
    {
        $blockBuffer = 10;

61
        return $this->client->payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer);
beardedwarrior's avatar
beardedwarrior committed
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
    }

    /**
     * Scans the monero blockchain for transactions for the payment ids
     *
     * @param $blockheight
     * @param $paymentIDs
     *
     * @return array|Transaction
     */
    public function scanBlocks($blockheight, $paymentIDs)
    {
        $response = $this->bulkPayments($paymentIDs);
        $address = $this->getAddress();
        $transactions = [];
        if ($response && isset($response['payments'])) {
            foreach ($response['payments'] as $payment) {
                $transaction = new Transaction(
                    $payment['tx_hash'],
                    $payment['amount'],
                    $address,
                    $blockheight - $payment['block_height'],
                    0,
                    Carbon::now(),
                    $payment['payment_id'],
                    $payment['block_height']
                );
                $transactions[] = $transaction;
            }
        }

        return collect($transactions);
    }

    /**
     * @param $blockheight
     *
     * @return \Illuminate\Support\Collection
     */
    public function scanMempool($blockheight)
    {
        $address = $this->getAddress();
        $transactions = [];
        $response = $this->mempoolTransfers();
        if ($response && isset($response['pool'])) {
            foreach ($response['pool'] as $payment) {
                $transaction = new Transaction(
                    $payment['txid'],
                    $payment['amount'],
                    $address,
                    0,
                    0,
                    Carbon::now(),
                    $payment['payment_id'],
                    $blockheight
                );
                $transactions[] = $transaction;
            }
        }

        return collect($transactions);
    }

    /**
     * Gets the current blockheight of xmr
     *
     * @return int
     */
    public function blockHeight()
    {
132
        return $this->client->blockHeight();
beardedwarrior's avatar
beardedwarrior committed
133 134 135 136 137 138 139 140 141
    }

    /**
     * Returns monero wallet address
     *
     * @return string
     */
    public function getAddress()
    {
142
        return $this->client->address();
beardedwarrior's avatar
beardedwarrior committed
143 144 145 146 147 148 149 150 151
    }

    /**
     * Returns XMR integrated address
     *
     * @return mixed
     */
    public function createIntegratedAddress()
    {
152
        return $this->client->createIntegratedAddress();
beardedwarrior's avatar
beardedwarrior committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }

    /**
     * @param $amount
     * @param $address
     * @param $paymentId
     *
     * @return string
     */
    public function createQrCodeString($amount, $address, $paymentId = ''): string
    {
        // @todo add tx_payment_id support
        // monero payment_id is passed through the address
        return 'monero:'.$address.'?tx_amount='.$amount;
    }

    /**
     * gets all the payment_ids outstanding from the address_pool, we use these to check against the latest mined blocks
     *
172
     * @return Collection
beardedwarrior's avatar
beardedwarrior committed
173 174 175 176
     */
    public function getPaymentIds()
    {

177
        return Project::pluck('payment_id'); //stop scanning for payment_ids after 24h
beardedwarrior's avatar
beardedwarrior committed
178 179
    }
}