GeneratingResult.php
1 <?php 2 3 namespace BitcoindRPC\Generating; 4 5 use BitcoindRPC\RpcResponse; 6 7 class GeneratingResult 8 { 9 private RpcResponse $response; 10 11 public function __construct(RpcResponse $response) 12 { 13 $this->response = $response; 14 } 15 16 public function isSuccess(): bool 17 { 18 return $this->response->isSuccess(); 19 } 20 21 public function getData(): mixed 22 { 23 return $this->response->getData(); 24 } 25 26 public function getError(): ?string 27 { 28 return $this->response->getError(); 29 } 30 31 public function getStatusCode(): int 32 { 33 return $this->response->getStatusCode(); 34 } 35 36 public function toArray(): array 37 { 38 return $this->response->toArray(); 39 } 40 41 public function getBlockHashes(): ?array 42 { 43 $data = $this->getData(); 44 45 if (is_array($data)) { 46 // For methods that return multiple block hashes 47 return $data; 48 } elseif (is_array($data) && isset($data['hash'])) { 49 // For methods that return a single block with hash 50 return [$data['hash']]; 51 } 52 53 return null; 54 } 55 56 public function getFirstBlockHash(): ?string 57 { 58 $hashes = $this->getBlockHashes(); 59 return $hashes[0] ?? null; 60 } 61 62 public function getLastBlockHash(): ?string 63 { 64 $hashes = $this->getBlockHashes(); 65 return $hashes ? end($hashes) : null; 66 } 67 68 public function getBlockCount(): int 69 { 70 $hashes = $this->getBlockHashes(); 71 return $hashes ? count($hashes) : 0; 72 } 73 74 public function getGeneratedCoins(): ?float 75 { 76 $data = $this->getData(); 77 78 if (is_array($data) && isset($data['coinbasevalue'])) { 79 // For getblocktemplate response 80 return $data['coinbasevalue'] / 100000000; // Convert to BTC 81 } 82 83 // For generated blocks, we might need to calculate based on block reward 84 // This is a simplified version - you might want to implement more sophisticated logic 85 $blockCount = $this->getBlockCount(); 86 if ($blockCount > 0) { 87 // Approximate block reward (this changes over time with halvings) 88 $blockReward = 6.25; // Current Bitcoin block reward in BTC 89 return $blockCount * $blockReward; 90 } 91 92 return null; 93 } 94 95 public function getGenerationStats(): ?array 96 { 97 $data = $this->getData(); 98 99 if (!is_array($data)) { 100 return null; 101 } 102 103 return [ 104 'block_hashes' => $this->getBlockHashes(), 105 'block_count' => $this->getBlockCount(), 106 'estimated_coins' => $this->getGeneratedCoins(), 107 'first_block' => $this->getFirstBlockHash(), 108 'last_block' => $this->getLastBlockHash(), 109 'success' => $this->isSuccess(), 110 'error' => $this->getError() 111 ]; 112 } 113 114 public function isBlockGenerated(): bool 115 { 116 return $this->isSuccess() && $this->getBlockCount() > 0; 117 } 118 119 public function getTransactionIds(): ?array 120 { 121 $data = $this->getData(); 122 123 if (is_array($data) && isset($data['transactions'])) { 124 $txids = []; 125 foreach ($data['transactions'] as $tx) { 126 if (isset($tx['txid'])) { 127 $txids[] = $tx['txid']; 128 } 129 } 130 return $txids; 131 } 132 133 return null; 134 } 135 136 public function getMiningInfo(): ?array 137 { 138 $data = $this->getData(); 139 140 if (is_array($data)) { 141 return [ 142 'blocks' => $data['blocks'] ?? null, 143 'difficulty' => $data['difficulty'] ?? null, 144 'network_hash_ps' => $data['networkhashps'] ?? null, 145 'pooled_tx' => $data['pooledtx'] ?? null, 146 'chain' => $data['chain'] ?? null, 147 'warnings' => $data['warnings'] ?? null 148 ]; 149 } 150 151 return null; 152 } 153 }