Wallet.php 5.39 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
<?php

namespace Monero;

use App\Project;
use Carbon\Carbon;
use jsonRPCClient;

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 all balances (locked/unlocked) or exception state for site monitor
     *
     * @return array
     */
    public function balanceSiteMonitor()
    {
        $result = [];
        $balance = $this->client->getbalance();
        if (!$balance) {
            $result['unlocked_balance'] = 'DOWN';
            $result['balance'] = 'DOWN';

            return $result;
        }
        $result['unlocked_balance'] = $balance['unlocked_balance'] / 1000000000000;
        $result['balance'] = $balance['balance'] / 1000000000000;

        return $result;
    }

    /**
     * Returns the actual available and useable balance (unlocked balance)
     *
     * @return float|int|mixed
     */
    public function balance()
    {
        $balance = $this->client->getbalance();
        $result = $balance['unlocked_balance'] / 1000000000000;

        return $result;
    }

    public function mempoolTransfers()
    {
        return $this->client->mempoolTransfers();
    }

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

        return $this->client->bulk_payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer);
    }

    /**
     * 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()
    {
        $result = $this->client->getheight();
        if ($result && isset($result['height'])) {
            return $result['height'];
        }

        return 0;
    }

    /**
     * Returns monero wallet address
     *
     * @return string
     */
    public function getAddress()
    {
        $address = $this->client->getaddress();
        if ($address != null) {
            return $address['address'];
        }

        return 'Invalid';
    }

    /**
     * Returns XMR integrated address
     *
     * @return mixed
     */
    public function createIntegratedAddress()
    {
        try {
            return $this->client->make_integrated_address();
        } catch (\Throwable $e) {
            return false;
        }
    }

    /**
     * @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
     *
     * @return array
     */
    public function getPaymentIds()
    {
        $paymentIds = Project::pluck('payment_id'); //stop scanning for payment_ids after 24h

        return $paymentIds;
    }
}