Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
6 / 12 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Blockchain_Info_Api | |
50.00% |
6 / 12 |
|
25.00% |
1 / 4 |
6.00 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_api | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get_transactions_received | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| get_blockchain_height | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Blockchain.com API client for querying Bitcoin addresses and transactions. |
| 4 | * |
| 5 | * "Please limit your queries to a maximum of 1 every 10 seconds" |
| 6 | * |
| 7 | * @see https://www.blockchain.com/api/blockchain_api |
| 8 | * @see https://www.blockchain.com/api/q |
| 9 | * @see https://github.com/brianhenryie/bh-php-blockchain-info |
| 10 | * |
| 11 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 12 | */ |
| 13 | |
| 14 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain; |
| 15 | |
| 16 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain\Adapters\Blockchain_Info_Api_Transaction_Adapter; |
| 17 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Exceptions\BH_WP_Bitcoin_Gateway_Exception; |
| 18 | use BrianHenryIE\WP_Bitcoin_Gateway\Art4\Requests\Psr\HttpClient; |
| 19 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain_API_Interface; |
| 20 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface; |
| 21 | use BrianHenryIE\WP_Bitcoin_Gateway\BlockchainInfo\BlockchainInfoApi; |
| 22 | use BrianHenryIE\WP_Bitcoin_Gateway\BlockchainInfo\Model\Transaction; |
| 23 | use Psr\Log\LoggerAwareInterface; |
| 24 | use Psr\Log\LoggerAwareTrait; |
| 25 | use Psr\Log\LoggerInterface; |
| 26 | |
| 27 | /** |
| 28 | * Maps objects from brianhenryie/bh-php-blockchain-info library to internal representations. |
| 29 | */ |
| 30 | class Blockchain_Info_Api implements Blockchain_API_Interface, LoggerAwareInterface { |
| 31 | use LoggerAwareTrait; |
| 32 | |
| 33 | /** |
| 34 | * The Blockchain.com API client instance. |
| 35 | */ |
| 36 | protected BlockchainInfoApi $api; |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * @param LoggerInterface $logger A PSR logger for recording API requests and errors. |
| 42 | * @param BlockchainInfoApi|null $api Optional pre-configured API client instance, or null to create default client with HTTP adapter. |
| 43 | */ |
| 44 | public function __construct( |
| 45 | LoggerInterface $logger, |
| 46 | ?BlockchainInfoApi $api = null, |
| 47 | ) { |
| 48 | $this->logger = $logger; |
| 49 | $this->api = $api ?? $this->get_api(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Construct an instance of the Blockchain.com API client. |
| 54 | */ |
| 55 | protected function get_api(): BlockchainInfoApi { |
| 56 | // Define options Requests library will set on cURL request. |
| 57 | $options = array(); |
| 58 | |
| 59 | $client = new HttpClient( $options ); |
| 60 | |
| 61 | return new BlockchainInfoApi( $client, $client ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get all transactions received by a Bitcoin address. |
| 66 | * |
| 67 | * @param string $btc_address The Bitcoin address to query for incoming transactions from the blockchain. |
| 68 | * |
| 69 | * @return Transaction_Interface[] Array of transactions where this address received funds. |
| 70 | * @throws BH_WP_Bitcoin_Gateway_Exception When the API request fails or returns rate limit errors. |
| 71 | */ |
| 72 | public function get_transactions_received( string $btc_address ): array { |
| 73 | |
| 74 | /** |
| 75 | * TODO: In BlockchainInfoApi functions, first check is `$response->getBody()` = 'Rate limited'. |
| 76 | */ |
| 77 | |
| 78 | $raw_address = $this->api->getRawAddr( $btc_address ); |
| 79 | |
| 80 | $adapter = new Blockchain_Info_Api_Transaction_Adapter(); |
| 81 | |
| 82 | // TODO: check this returns the array indexed by the txid. |
| 83 | return array_map( |
| 84 | $adapter->adapt( ... ), |
| 85 | $raw_address->getTxs() |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the current height of the Bitcoin blockchain. |
| 91 | * |
| 92 | * @return int The current block height from Blockchain.com's API. |
| 93 | * @throws BH_WP_Bitcoin_Gateway_Exception When the API request fails or the blockchain height cannot be retrieved. |
| 94 | */ |
| 95 | public function get_blockchain_height(): int { |
| 96 | |
| 97 | return $this->api->getBlockCount(); |
| 98 | } |
| 99 | } |