Newer
Older
<?php
namespace Monero;
use App\Project;
use Carbon\Carbon;
beardedwarrior
committed
use Illuminate\Support\Collection;
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()
{
beardedwarrior
committed
return $this->client->balance();
beardedwarrior
committed
return $this->client->incomingTransfers();
}
public function bulkPayments($paymentIds)
{
$blockBuffer = 10;
beardedwarrior
committed
return $this->client->payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer);
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()
{
beardedwarrior
committed
return $this->client->blockHeight();
}
/**
* Returns monero wallet address
*
* @return string
*/
public function getAddress()
{
beardedwarrior
committed
return $this->client->address();
}
/**
* Returns XMR integrated address
*
* @return mixed
*/
public function createIntegratedAddress()
{
beardedwarrior
committed
return $this->client->createIntegratedAddress();
}
/**
* @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
*
beardedwarrior
committed
* @return Collection
beardedwarrior
committed
return Project::pluck('payment_id'); //stop scanning for payment_ids after 24h