BanRPC.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. private $url='https://bsc-dataseed1.binance.org/';
  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. private function __construct(){
  32. // $testServe=env('TEST_SERVE');
  33. // if($testServe){
  34. // $this->url='https://data-seed-prebsc-1-s1.binance.org:8545';
  35. // $this->chainId='97';
  36. // }
  37. }
  38. function sendBlockNumber(){
  39. $num= $this->send('eth_blockNumber', []);
  40. if(empty($num)){
  41. return 0;
  42. }
  43. return Utils::hex2dec($num);
  44. }
  45. function sendBlockByNumber($num){
  46. return $this->send('eth_getBlockByNumber', [Utils::dec2hex($num),true]);
  47. }
  48. // 执行eth_call
  49. public function call($params)
  50. {
  51. return $this->send('eth_call', [$params, 'latest']);
  52. }
  53. // 执行sendRawTransaction
  54. public function sendTransaction($data, &$error = null)
  55. {
  56. return $this->send('sendTransaction', $data, $error);
  57. }
  58. // 执行sendRawTransaction
  59. public function sendRawTransaction($sign, &$error = null)
  60. {
  61. return $this->send('eth_sendRawTransaction', [$sign], $error);
  62. }
  63. // 执行getTransactionReceipt
  64. public function getTransactionReceipt($hash)
  65. {
  66. return $this->send('eth_getTransactionReceipt', [$hash]);
  67. }
  68. // 获取交易笔数
  69. public function getTransactionCount($address)
  70. {
  71. return $this->send('eth_getTransactionCount', [$address, 'pending']);
  72. }
  73. // 获取gas单价
  74. protected $gasPrice;
  75. public function getGasPrice()
  76. {
  77. if ($this->gasPrice === null) {
  78. $gasPrice = $this->send('eth_gasPrice');
  79. $range = [5, 200]; // 单位gwei
  80. $gasPrice = Utils::hex2dec($gasPrice);
  81. $gasPrice = Utils::convertUnit($gasPrice, 'wei', 'gwei');
  82. $gasPrice = max($gasPrice, $range[0]);
  83. $gasPrice = min($gasPrice, $range[1]);
  84. $gasPrice = Utils::convertUnit($gasPrice, 'gwei', 'wei');
  85. $this->gasPrice = Utils::dec2hex($gasPrice);
  86. }
  87. return $this->gasPrice;
  88. }
  89. // 预估tx消耗的gas
  90. public function estimateGas($tx)
  91. {
  92. $limit = $this->send('eth_estimateGas', [$tx]);
  93. if (!$limit) {
  94. $limit = 100000;
  95. }
  96. if(is_numeric($limit))$limit=Utils::dec2hex($limit);
  97. return $limit;
  98. }
  99. // 获取eth余额
  100. public function getBalance($address)
  101. {
  102. $bnb= $this->send('eth_getBalance', [$address, 'latest']);
  103. if(empty($bnb)){
  104. return 0;
  105. }else{
  106. return Utils::int2fund(Utils::hex2dec($bnb));
  107. }
  108. }
  109. /**
  110. *
  111. * @param string $method The method of jsonrpc
  112. * @param array $params The params of jsonrpc
  113. * @return bool|mixed
  114. */
  115. public function send($method, $params = [], &$error = null)
  116. {
  117. $query = [];
  118. $query['jsonrpc'] = '2.0';
  119. $query['method'] = $method;
  120. $query['params'] = $params;
  121. $query['id'] = ++$this->id;
  122. $rawResp = Utils::post($this->url, json_encode($query), $this->timeout);
  123. $resp = @json_decode($rawResp, true);
  124. if ($resp === false || !is_array($resp)) {
  125. $error = $resp;
  126. return false;
  127. }
  128. if (isset($resp['error'])) {
  129. $error = $resp['error'];
  130. return false;
  131. }
  132. return $resp['result'];
  133. }
  134. public function __call($name, $params)
  135. {
  136. return $this->send($name, $params);
  137. }
  138. }