EthereumRPC.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Servers\Icon;
  3. /**
  4. * Class EthereumRPC
  5. * @package common\models\ethereum
  6. */
  7. class EthereumRPC
  8. {
  9. /**
  10. * @var string RPC URL
  11. */
  12. public $url='https://mainnet.infura.io/v3/b5cab4389fc04d749c961899b57384cb';
  13. public $chainId='1';
  14. // request id
  15. protected $id = 0;
  16. /**
  17. * RPC timeout
  18. * @var int
  19. */
  20. public $timeout = 60;
  21. // 执行eth_call
  22. public function call($params)
  23. {
  24. return $this->send('eth_call', [$params, 'latest']);
  25. }
  26. // 执行sendRawTransaction
  27. public function sendTransaction($data, &$error = null)
  28. {
  29. return $this->send('sendTransaction', $data, $error);
  30. }
  31. // 执行sendRawTransaction
  32. public function sendRawTransaction($sign, &$error = null)
  33. {
  34. return $this->send('eth_sendRawTransaction', [$sign], $error);
  35. }
  36. // 执行getTransactionReceipt
  37. public function getTransactionReceipt($hash)
  38. {
  39. return $this->send('eth_getTransactionReceipt', [$hash]);
  40. }
  41. // 获取交易笔数
  42. public function getTransactionCount($address)
  43. {
  44. return $this->send('eth_getTransactionCount', [$address, 'pending']);
  45. }
  46. // 获取gas单价
  47. protected $gasPrice;
  48. public function getGasPrice()
  49. {
  50. if ($this->gasPrice === null) {
  51. $gasPrice = $this->send('eth_gasPrice');
  52. $range = [15, 20]; // 单位gwei
  53. $gasPrice = Utils::hex2dec($gasPrice);
  54. $gasPrice = Utils::convertUnit($gasPrice, 'wei', 'gwei');
  55. $gasPrice = max($gasPrice, $range[0]);
  56. $gasPrice = min($gasPrice, $range[1]);
  57. $gasPrice = Utils::convertUnit($gasPrice, 'gwei', 'wei');
  58. $this->gasPrice = Utils::dec2hex($gasPrice);
  59. }
  60. return $this->gasPrice;
  61. }
  62. // 预估tx消耗的gas
  63. public function estimateGas($tx)
  64. {
  65. $limit = $this->send('eth_estimateGas', [$tx]);
  66. if (!$limit) {
  67. $limit = 100000;
  68. }
  69. if(is_numeric($limit))$limit=Utils::dec2hex($limit);
  70. return $limit;
  71. }
  72. // 获取eth余额
  73. public function getBalance($address)
  74. {
  75. return $this->send('eth_getBalance', [$address, 'latest']);
  76. }
  77. /**
  78. *
  79. * @param string $method The method of jsonrpc
  80. * @param array $params The params of jsonrpc
  81. * @return bool|mixed
  82. */
  83. public function send($method, $params = [], &$error = null)
  84. {
  85. $query = [];
  86. $query['jsonrpc'] = '2.0';
  87. $query['method'] = $method;
  88. $query['params'] = $params;
  89. $query['id'] = ++$this->id;
  90. $rawResp = Utils::post($this->url, json_encode($query), $this->timeout);
  91. $resp = @json_decode($rawResp, true);
  92. if ($resp === false || !is_array($resp)) {
  93. $error = $resp;
  94. return false;
  95. }
  96. if (isset($resp['error'])) {
  97. $error = $resp['error'];
  98. return false;
  99. }
  100. return $resp['result'];
  101. }
  102. public function __call($name, $params)
  103. {
  104. return $this->send($name, $params);
  105. }
  106. }