Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| BlockStream_Info_API_Transaction_Adapter | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| adapt | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| map_v_in | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| map_v_out | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adapter to convert Blockstream.info API transaction data to internal Transaction objects. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain\Adapters; |
| 9 | |
| 10 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain\Blockstream_Info_API; |
| 11 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction; |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_VIn; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_VOut; |
| 15 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Services\Exchange_Rate_Service; |
| 16 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Exception\UnknownCurrencyException; |
| 17 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money; |
| 18 | use DateTimeImmutable; |
| 19 | use DateTimeZone; |
| 20 | |
| 21 | /** |
| 22 | * @phpstan-import-type BlockStreamApiTransactionArray from Blockstream_Info_API |
| 23 | * @phpstan-import-type BlockStreamApiTransactionVInArray from Blockstream_Info_API |
| 24 | * @phpstan-import-type BlockStreamApiTransactionVOutArray from Blockstream_Info_API |
| 25 | */ |
| 26 | class BlockStream_Info_API_Transaction_Adapter { |
| 27 | |
| 28 | // TODO: Confirmations was returning the block height - 1. Presumably that meant mempool/0 confirmations, but I need test data to understand. |
| 29 | // Correct solution is probably to check does $blockstream_transaction['status']['block_height'] exist, else ??? |
| 30 | // Quick fix. |
| 31 | |
| 32 | /** |
| 33 | * @param array&BlockStreamApiTransactionArray $blockstream_transaction The transaction data as returned by Blockstream.info API. |
| 34 | */ |
| 35 | public function adapt( array $blockstream_transaction ): Transaction_Interface { |
| 36 | return new Transaction( |
| 37 | tx_id: (string) $blockstream_transaction['txid'], // TODO: what is `$blockstream_transaction['status']['block_hash']`? |
| 38 | block_time: new DateTimeImmutable( '@' . $blockstream_transaction['status']['block_time'], new DateTimeZone( 'UTC' ) ), |
| 39 | version: $blockstream_transaction['version'], |
| 40 | v_in: array_map( $this->map_v_in( ... ), $blockstream_transaction['vin'] ), |
| 41 | v_out: array_map( $this->map_v_out( ... ), $blockstream_transaction['vout'] ), |
| 42 | block_height: $blockstream_transaction['status']['block_height'], |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Map a Blockstream API transaction vector input to internal Transaction_VIn. |
| 48 | * |
| 49 | * @param BlockStreamApiTransactionVInArray&array $v_in The transaction input from Blockstream API. |
| 50 | * @return Transaction_VIn The mapped transaction input. |
| 51 | * @throws UnknownCurrencyException When BTC currency code is not recognized by the Money library. |
| 52 | */ |
| 53 | protected function map_v_in( array $v_in ): Transaction_VIn { |
| 54 | return new Transaction_VIn( |
| 55 | sequence: $v_in['sequence'], |
| 56 | scriptsig: $v_in['scriptsig'], |
| 57 | address: $v_in['prevout']['scriptpubkey_address'], |
| 58 | prevout_scriptpubkey: $v_in['prevout']['scriptpubkey'], |
| 59 | value: Money::of( $v_in['prevout']['value'] / Exchange_Rate_Service::SATOSHI_RATE, 'BTC' ), |
| 60 | prev_out_n: $v_in['vout'], |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Map a Blockstream API transaction output to internal Transaction_VOut. |
| 66 | * |
| 67 | * @param BlockStreamApiTransactionVOutArray&array $v_out The transaction output from Blockstream API. |
| 68 | * @return Transaction_VOut The mapped transaction output. |
| 69 | * @throws UnknownCurrencyException When BTC currency code is not recognized by the Money library. |
| 70 | */ |
| 71 | protected function map_v_out( array $v_out ): Transaction_VOut { |
| 72 | return new Transaction_VOut( |
| 73 | value: Money::of( $v_out['value'] / Exchange_Rate_Service::SATOSHI_RATE, 'BTC' ), |
| 74 | scriptpubkey_address: $v_out['scriptpubkey_address'], |
| 75 | ); |
| 76 | } |
| 77 | } |