/ src / Mining / MiningResult.php
MiningResult.php
  1  <?php
  2  
  3  namespace BitcoindRPC\Mining;
  4  
  5  use BitcoindRPC\RpcResponse;
  6  
  7  class MiningResult
  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 getBlockTemplate(): ?array
 42      {
 43          $data = $this->getData();
 44          return is_array($data) ? $data : null;
 45      }
 46  
 47      public function getMiningInfo(): ?array
 48      {
 49          $data = $this->getData();
 50          return is_array($data) ? $data : null;
 51      }
 52  
 53      public function getNetworkHashRate(): ?float
 54      {
 55          $data = $this->getData();
 56          return is_numeric($data) ? (float) $data : null;
 57      }
 58  
 59      public function getFeeEstimate(): ?array
 60      {
 61          $data = $this->getData();
 62          if (!is_array($data)) {
 63              return null;
 64          }
 65  
 66          return [
 67              'feerate' => $data['feerate'] ?? null,
 68              'blocks' => $data['blocks'] ?? null,
 69              'errors' => $data['errors'] ?? []
 70          ];
 71      }
 72  
 73      public function getBlockSubmissionStatus(): string
 74      {
 75          if (!$this->isSuccess()) {
 76              return 'failed';
 77          }
 78  
 79          $data = $this->getData();
 80          if ($data === null) {
 81              return 'accepted';
 82          } elseif (is_string($data)) {
 83              return 'rejected: ' . $data;
 84          }
 85  
 86          return 'unknown';
 87      }
 88  
 89      public function getBlockTemplateFields(): ?array
 90      {
 91          $template = $this->getBlockTemplate();
 92          if (!$template) {
 93              return null;
 94          }
 95  
 96          return [
 97              'version' => $template['version'] ?? null,
 98              'previous_block_hash' => $template['previousblockhash'] ?? null,
 99              'transactions' => $template['transactions'] ?? [],
100              'coinbase_value' => $template['coinbasevalue'] ?? null,
101              'target' => $template['target'] ?? null,
102              'min_time' => $template['mintime'] ?? null,
103              'mutable' => $template['mutable'] ?? [],
104              'nonce_range' => $template['noncerange'] ?? null,
105              'sigop_limit' => $template['sigoplimit'] ?? null,
106              'size_limit' => $template['sizelimit'] ?? null,
107              'cur_time' => $template['curtime'] ?? null,
108              'bits' => $template['bits'] ?? null,
109              'height' => $template['height'] ?? null
110          ];
111      }
112  
113      public function getTransactionCount(): ?int
114      {
115          $template = $this->getBlockTemplate();
116          if (!$template || !isset($template['transactions'])) {
117              return null;
118          }
119  
120          return count($template['transactions']);
121      }
122  
123      public function getBlockReward(): ?float
124      {
125          $template = $this->getBlockTemplate();
126          if (!$template || !isset($template['coinbasevalue'])) {
127              return null;
128          }
129  
130          return $template['coinbasevalue'] / 100000000; // Convert satoshis to BTC
131      }
132  
133      public function getMiningDifficulty(): ?float
134      {
135          $miningInfo = $this->getMiningInfo();
136          return $miningInfo['difficulty'] ?? null;
137      }
138  
139      public function getNetworkHashesPerSecond(): ?float
140      {
141          $miningInfo = $this->getMiningInfo();
142          return $miningInfo['networkhashps'] ?? null;
143      }
144  
145      public function getChainType(): ?string
146      {
147          $miningInfo = $this->getMiningInfo();
148          return $miningInfo['chain'] ?? null;
149      }
150  
151      public function getMempoolTransactionCount(): ?int
152      {
153          $miningInfo = $this->getMiningInfo();
154          return $miningInfo['pooledtx'] ?? null;
155      }
156  
157      public function getBlockHeight(): ?int
158      {
159          $miningInfo = $this->getMiningInfo();
160          return $miningInfo['blocks'] ?? null;
161      }
162  
163      public function getValidationInfo(): ?array
164      {
165          $data = $this->getData();
166          if (!is_array($data)) {
167              return null;
168          }
169  
170          return [
171              'is_valid' => $data['isvalid'] ?? false,
172              'address' => $data['address'] ?? null,
173              'script_pub_key' => $data['scriptPubKey'] ?? null,
174              'is_script' => $data['isscript'] ?? false,
175              'is_witness' => $data['iswitness'] ?? false,
176              'witness_version' => $data['witness_version'] ?? null,
177              'witness_program' => $data['witness_program'] ?? null
178          ];
179      }
180  
181      public function isBlockTemplateValid(): bool
182      {
183          return $this->isSuccess() && $this->getBlockTemplate() !== null;
184      }
185  
186      public function isFeeEstimationAvailable(): bool
187      {
188          $feeData = $this->getFeeEstimate();
189          return $feeData !== null && $feeData['feerate'] !== null;
190      }
191  
192      public function getMiningSummary(): array
193      {
194          $summary = [
195              'success' => $this->isSuccess(),
196              'error' => $this->getError(),
197              'status_code' => $this->getStatusCode()
198          ];
199  
200          if ($this->isSuccess()) {
201              $data = $this->getData();
202              
203              if (is_array($data)) {
204                  // Mining info response
205                  $summary['type'] = 'mining_info';
206                  $summary['blocks'] = $data['blocks'] ?? null;
207                  $summary['difficulty'] = $data['difficulty'] ?? null;
208                  $summary['network_hash_ps'] = $data['networkhashps'] ?? null;
209                  $summary['pooled_tx'] = $data['pooledtx'] ?? null;
210                  $summary['chain'] = $data['chain'] ?? null;
211              } elseif (is_numeric($data)) {
212                  // Network hash rate response
213                  $summary['type'] = 'network_hash_rate';
214                  $summary['hash_rate'] = (float) $data;
215              } else {
216                  $summary['type'] = 'other';
217                  $summary['data'] = $data;
218              }
219          }
220  
221          return $summary;
222      }
223  }