Completed
Branch master (56c2f5)
by
unknown
07:05
created

InsightUnspentOutputFinder::getUnspentOutputs()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 48
Code Lines 30

Duplication

Lines 9
Ratio 18.75 %

Importance

Changes 0
Metric Value
dl 9
loc 48
rs 8.551
c 0
b 0
f 0
cc 5
eloc 30
nc 7
nop 1
1
<?php
2
3
namespace Blocktrail\SDK\Services;
4
5
use Blocktrail\SDK\BlocktrailSDK;
6
use Blocktrail\SDK\UnspentOutputFinder;
7
8
class InsightUnspentOutputFinder extends UnspentOutputFinder {
9
10
    protected $testnet;
11
12
    protected $retryLimit;
13
    protected $sleepTime;
14
15
    /**
16
     * @param bool $testnet
17
     */
18
    public function __construct($testnet = false) {
19
        $this->testnet = $testnet;
20
21
        $this->retryLimit = 5;
22
        $this->sleepTime = 20;
23
    }
24
25 View Code Duplication
    public function getUTXOs(array $addresses) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
        $results = array();
27
28
        foreach (array_chunk($addresses, 500) as $addresses) {
29
            if ($this->debug) {
30
                echo "\nchecking " . count($addresses) . " addresses ...";
31
            }
32
            //get the utxos for this address
33
            $utxos = $this->getUnspentOutputs($addresses);
34
35
            if (count($utxos) > 0) {
36
                $results = array_merge($results, $utxos);
37
            }
38
        }
39
40
        return $results;
41
    }
42
43
    /**
44
     * gets unspent outputs for a batch of addresses, returning and array of outputs with hash, index, value, and script pub hex
45
     *
46
     * @param string[] $addresses
47
     * @return array        2d array of unspent outputs as ['hash' => $hash, 'index' => $index, 'value' => $value, 'address' => $address, 'script_hex' => $scriptHex]
48
     * @throws \Exception
49
     */
50
    protected function getUnspentOutputs($addresses) {
51
        //get unspent outputs for the address - required data: hash, index, value, and script hex
52
        $utxos = [];
53
        $retries = 0;
54
55
        do {
56
            $more = true;
57
58
            try {
59
                $client = new \GuzzleHttp\Client();
60
                $response = $client->post(
61
                    'https://' . ($this->testnet ? 'test-' : '') . 'insight.bitpay.com/api/addrs/utxo',
62
                    [
63
                        'json' => ['addrs' => implode(",", $addresses)],
64
                        'timeout' => 30,
65
                    ]
66
                );
67
68
                $json = json_decode($response->getBody(), true);
69
70
                $utxos = $json;
71
72
                $more = false;
73
74
            } catch (\Exception $e) {
75
                //if rate limit hit, sleep for a short while and try again
76
                if ($retries < $this->retryLimit) {
77
                    $retries++;
78
                    sleep($this->sleepTime);
79
                } else {
80
                    throw $e;
81
                }
82
            }
83
        } while ($more);
84
85
        //reduce the returned data into the values we're interested in
86 View Code Duplication
        $result = array_map(function ($utxo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            return array(
88
                'hash'       => $utxo['txid'],
89
                'index'      => $utxo['vout'],
90
                'value'      => BlocktrailSDK::toSatoshi($utxo['amount']),
91
                'address'    => $utxo['address'],
92
                'script_hex' => $utxo['scriptPubKey'],
93
            );
94
        }, $utxos);
95
96
        return $result;
97
    }
98
}
99