|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blocktrail\SDK\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Blocktrail\SDK\BlocktrailSDK; |
|
6
|
|
|
use Blocktrail\SDK\UnspentOutputFinder; |
|
7
|
|
|
|
|
8
|
|
|
class BlocktrailUnspentOutputFinder extends UnspentOutputFinder { |
|
9
|
|
|
|
|
10
|
|
|
protected $client; |
|
11
|
|
|
protected $retryLimit; |
|
12
|
|
|
protected $sleepTime; |
|
13
|
|
|
protected $retries; |
|
14
|
|
|
protected $paginationLimit = 50; //max results to retrieve at a time |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param $apiKey |
|
18
|
|
|
* @param $apiSecret |
|
19
|
|
|
* @param string $network |
|
20
|
|
|
* @param bool $testnet |
|
21
|
|
|
* @param string $apiVersion |
|
22
|
|
|
* @param null $apiEndpoint |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
25
|
3 |
|
$this->client = new BlocktrailSDK($apiKey, $apiSecret, $network, $testnet, $apiVersion, $apiEndpoint); |
|
26
|
|
|
|
|
27
|
3 |
|
$this->retryLimit = 5; |
|
28
|
3 |
|
$this->sleepTime = 20; |
|
29
|
3 |
|
$this->retries = 0; |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* modify the default limit on how many utxo results are returned per page |
|
34
|
|
|
* @param $limit |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function setPaginationLimit($limit) { |
|
37
|
1 |
|
$this->paginationLimit = $limit; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function getUTXOs(array $addresses) { |
|
41
|
3 |
|
$results = array(); |
|
42
|
|
|
|
|
43
|
3 |
|
foreach ($addresses as $address) { |
|
44
|
3 |
|
if ($this->debug) { |
|
45
|
|
|
echo "\nchecking $address"; |
|
46
|
|
|
} |
|
47
|
|
|
//get the utxos for this address |
|
48
|
3 |
|
$utxos = $this->getUnspentOutputs($address); |
|
49
|
|
|
|
|
50
|
3 |
|
if (count($utxos) > 0) { |
|
51
|
3 |
|
$results = array_merge($results, $utxos); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $results; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* gets unspent outputs for an address, returning and array of outputs with hash, index, value, and script pub hex |
|
60
|
|
|
* |
|
61
|
|
|
* @param $address |
|
62
|
|
|
* @return array 2d array of unspent outputs as ['hash' => $hash, 'index' => $index, 'value' => $value, 'script_hex' => $scriptHex] |
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
*/ |
|
65
|
2 |
|
protected function getUnspentOutputs($address) { |
|
66
|
|
|
//get unspent outputs for the address - required data: hash, index, value, and script hex |
|
67
|
2 |
|
$utxos = array(); |
|
68
|
|
|
try { |
|
69
|
2 |
|
$page = 1; |
|
70
|
|
|
do { |
|
71
|
2 |
|
$results = $this->client->addressUnspentOutputs($address, $page, $this->paginationLimit); |
|
72
|
2 |
|
$utxos = array_merge($utxos, $results['data']); |
|
73
|
2 |
|
$page++; |
|
74
|
2 |
|
} while (count($results['data']) > 0); |
|
75
|
|
|
} catch (\Exception $e) { |
|
76
|
|
|
//if rate limit hit, sleep for a short while and try again |
|
77
|
|
|
if ($this->retries < $this->retryLimit) { |
|
78
|
|
|
$this->retries++; |
|
79
|
|
|
sleep($this->sleepTime); |
|
80
|
|
|
|
|
81
|
|
|
return $this->getUnspentOutputs($address); |
|
82
|
|
|
} else { |
|
83
|
|
|
throw $e; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
//reset retry count |
|
88
|
2 |
|
$this->retries = 0; |
|
89
|
|
|
|
|
90
|
|
|
//reduce the returned data into the values we're interested in |
|
91
|
2 |
|
$result = array_map(function ($utxo) { |
|
92
|
|
|
return array( |
|
93
|
2 |
|
'hash' => $utxo['hash'], |
|
94
|
2 |
|
'index' => $utxo['index'], |
|
95
|
2 |
|
'value' => $utxo['value'], |
|
96
|
2 |
|
'script_hex' => $utxo['script_hex'], |
|
97
|
|
|
); |
|
98
|
2 |
|
}, $utxos); |
|
99
|
|
|
|
|
100
|
|
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|