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

BlocktrailBatchUnspentOutputFinder::getUTXOs()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

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