BanRPC.php 3.3 KB

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