TronManager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace IEXBase\TronAPI;
  3. use IEXBase\TronAPI\Exception\TronException;
  4. use IEXBase\TronAPI\Provider\{HttpProvider, HttpProviderInterface};
  5. class TronManager
  6. {
  7. /**
  8. * Default Nodes
  9. *
  10. * @var array
  11. */
  12. protected $defaultNodes = [
  13. 'fullNode' => 'https://api.trongrid.io',
  14. 'solidityNode' => 'https://api.trongrid.io',
  15. 'eventServer' => 'https://api.trongrid.io',
  16. 'explorer' => 'https://apilist.tronscan.org',
  17. 'signServer' => ''
  18. ];
  19. /**
  20. * Providers
  21. *
  22. * @var array
  23. */
  24. protected $providers = [
  25. 'fullNode' => [],
  26. 'solidityNode' => [],
  27. 'eventServer' => [],
  28. 'explorer' => [],
  29. 'signServer' => []
  30. ];
  31. /**
  32. * Status Page
  33. *
  34. * @var array
  35. */
  36. protected $statusPage = [
  37. 'fullNode' => 'wallet/getnowblock',
  38. 'solidityNode' => 'walletsolidity/getnowblock',
  39. 'eventServer' => 'healthcheck',
  40. 'explorer' => 'api/system/status'
  41. ];
  42. /**
  43. * @param $tron
  44. * @param $providers
  45. * @throws Exception\TronException
  46. */
  47. public function __construct($tron, $providers)
  48. {
  49. $this->providers = $providers;
  50. foreach ($providers as $key => $value)
  51. {
  52. //Do not skip the supplier is empty
  53. if ($value == null) {
  54. $this->providers[$key] = new HttpProvider(
  55. $this->defaultNodes[$key]
  56. );
  57. };
  58. if(is_string($providers[$key]))
  59. $this->providers[$key] = new HttpProvider($value);
  60. if(in_array($key, ['signServer']))
  61. continue;
  62. $this->providers[$key]->setStatusPage($this->statusPage[$key]);
  63. }
  64. }
  65. /**
  66. * List of providers
  67. *
  68. * @return array
  69. */
  70. public function getProviders() {
  71. return $this->providers;
  72. }
  73. /**
  74. * Full Node
  75. *
  76. * @throws TronException
  77. * @return HttpProviderInterface
  78. */
  79. public function fullNode() : HttpProviderInterface
  80. {
  81. if (!array_key_exists('fullNode', $this->providers)) {
  82. throw new TronException('Full node is not activated.');
  83. }
  84. return $this->providers['fullNode'];
  85. }
  86. /**
  87. * Solidity Node
  88. *
  89. * @throws TronException
  90. * @return HttpProviderInterface
  91. */
  92. public function solidityNode() : HttpProviderInterface
  93. {
  94. if (!array_key_exists('solidityNode', $this->providers)) {
  95. throw new TronException('Solidity node is not activated.');
  96. }
  97. return $this->providers['solidityNode'];
  98. }
  99. /**
  100. * Sign server
  101. *
  102. * @throws TronException
  103. * @return HttpProviderInterface
  104. */
  105. public function signServer(): HttpProviderInterface
  106. {
  107. if (!array_key_exists('signServer', $this->providers)) {
  108. throw new TronException('Sign server is not activated.');
  109. }
  110. return $this->providers['signServer'];
  111. }
  112. /**
  113. * TronScan server
  114. *
  115. * @throws TronException
  116. * @return HttpProviderInterface
  117. */
  118. public function explorer(): HttpProviderInterface
  119. {
  120. if (!array_key_exists('explorer', $this->providers)) {
  121. throw new TronException('explorer is not activated.');
  122. }
  123. return $this->providers['explorer'];
  124. }
  125. /**
  126. * Event server
  127. *
  128. * @throws TronException
  129. * @return HttpProviderInterface
  130. */
  131. public function eventServer(): HttpProviderInterface
  132. {
  133. if (!array_key_exists('eventServer', $this->providers)) {
  134. throw new TronException('Event server is not activated.');
  135. }
  136. return $this->providers['eventServer'];
  137. }
  138. /**
  139. * Basic query to nodes
  140. *
  141. * @param $url
  142. * @param $params
  143. * @param string $method
  144. * @return array
  145. * @throws TronException
  146. */
  147. public function request($url, $params = [], $method = 'post')
  148. {
  149. $split = explode('/', $url);
  150. if(in_array($split[0], ['walletsolidity', 'walletextension'])) {
  151. $response = $this->solidityNode()->request($url, $params, $method);
  152. } elseif(in_array($split[0], ['event'])) {
  153. $response = $this->eventServer()->request($url, $params, 'get');
  154. } elseif (in_array($split[0], ['trx-sign'])) {
  155. $response = $this->signServer()->request($url, $params, 'post');
  156. } elseif(in_array($split[0], ['api'])) {
  157. $response = $this->explorer()->request($url, $params, 'get');
  158. }else {
  159. $response = $this->fullNode()->request($url, $params, $method);
  160. }
  161. return $response;
  162. }
  163. /**
  164. * Check connections
  165. *
  166. * @return array
  167. */
  168. public function isConnected()
  169. {
  170. $array = [];
  171. foreach ($this->providers as $key => $value) {
  172. array_push($array, [
  173. $key => boolval($value->isConnected())
  174. ]);
  175. }
  176. return $array;
  177. }
  178. }