Eth.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * This file is part of web3.php package.
  4. *
  5. * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
  6. *
  7. * @author Peter Lai <alk03073135@gmail.com>
  8. * @license MIT
  9. */
  10. namespace Web3;
  11. use Web3\Providers\Provider;
  12. use Web3\Providers\HttpProvider;
  13. use Web3\Providers\WsProvider;
  14. class Eth
  15. {
  16. /**
  17. * provider
  18. *
  19. * @var \Web3\Providers\Provider
  20. */
  21. protected $provider;
  22. /**
  23. * methods
  24. *
  25. * @var array
  26. */
  27. private $methods = [];
  28. /**
  29. * allowedMethods
  30. *
  31. * @var array
  32. */
  33. private $allowedMethods = [
  34. 'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getUncleByBlockHashAndIndex', 'eth_getUncleByBlockNumberAndIndex', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex', 'eth_getTransactionReceipt', 'eth_compileSolidity', 'eth_compileLLL', 'eth_compileSerpent', 'eth_getWork', 'eth_newFilter', 'eth_newBlockFilter', 'eth_newPendingTransactionFilter', 'eth_uninstallFilter', 'eth_getFilterChanges', 'eth_getFilterLogs', 'eth_getLogs', 'eth_submitWork', 'eth_submitHashrate', 'eth_feeHistory'
  35. ];
  36. /**
  37. * construct
  38. *
  39. * @param string|\Web3\Providers\Provider $provider
  40. * @param float $timeout
  41. * @return void
  42. */
  43. public function __construct($provider, $timeout = 1)
  44. {
  45. if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
  46. // check the uri schema
  47. if (preg_match('/^https?:\/\//', $provider) === 1) {
  48. $this->provider = new HttpProvider($provider, $timeout);
  49. } else if (preg_match('/^wss?:\/\//', $provider) === 1) {
  50. $this->provider = new WsProvider($provider, $timeout);
  51. }
  52. } else if ($provider instanceof Provider) {
  53. $this->provider = $provider;
  54. }
  55. }
  56. /**
  57. * call
  58. *
  59. * @param string $name
  60. * @param array $arguments
  61. * @return void|\React\Promise\PromiseInterface
  62. */
  63. public function __call($name, $arguments)
  64. {
  65. if (empty($this->provider)) {
  66. throw new \RuntimeException('Please set provider first.');
  67. }
  68. $class = explode('\\', get_class($this));
  69. if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
  70. $method = strtolower($class[1]) . '_' . $name;
  71. if (!in_array($method, $this->allowedMethods)) {
  72. throw new \RuntimeException('Unallowed rpc method: ' . $method);
  73. }
  74. if ($this->provider->isBatch) {
  75. $callback = null;
  76. } else {
  77. $callback = array_pop($arguments);
  78. if (is_callable($callback) !== true) {
  79. throw new \InvalidArgumentException('The last param must be callback function.');
  80. }
  81. }
  82. if (!array_key_exists($method, $this->methods)) {
  83. // new the method
  84. $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
  85. $methodObject = new $methodClass($method, $arguments);
  86. $this->methods[$method] = $methodObject;
  87. } else {
  88. $methodObject = $this->methods[$method];
  89. }
  90. if ($methodObject->validate($arguments)) {
  91. $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
  92. $methodObject->arguments = $inputs;
  93. return $this->provider->send($methodObject, $callback);
  94. }
  95. }
  96. }
  97. /**
  98. * get
  99. *
  100. * @param string $name
  101. * @return mixed
  102. */
  103. public function __get($name)
  104. {
  105. $method = 'get' . ucfirst($name);
  106. if (method_exists($this, $method)) {
  107. return call_user_func_array([$this, $method], []);
  108. }
  109. return false;
  110. }
  111. /**
  112. * set
  113. *
  114. * @param string $name
  115. * @param mixed $value
  116. * @return mixed
  117. */
  118. public function __set($name, $value)
  119. {
  120. $method = 'set' . ucfirst($name);
  121. if (method_exists($this, $method)) {
  122. return call_user_func_array([$this, $method], [$value]);
  123. }
  124. return false;
  125. }
  126. /**
  127. * getProvider
  128. *
  129. * @return \Web3\Providers\Provider
  130. */
  131. public function getProvider()
  132. {
  133. return $this->provider;
  134. }
  135. /**
  136. * setProvider
  137. *
  138. * @param \Web3\Providers\Provider $provider
  139. * @return bool
  140. */
  141. public function setProvider($provider)
  142. {
  143. if ($provider instanceof Provider) {
  144. $this->provider = $provider;
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * batch
  151. *
  152. * @param bool $status
  153. * @return void
  154. */
  155. public function batch($status)
  156. {
  157. $status = is_bool($status);
  158. $this->provider->batch($status);
  159. }
  160. }