BanRPC.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = [5, 200]; // 单位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. $bnb= $this->send('eth_getBalance', [$address, 'latest']);
  86. if(empty($bnb)){
  87. return 0;
  88. }else{
  89. return Utils::int2fund(Utils::hex2dec($bnb));
  90. }
  91. }
  92. /**
  93. *
  94. * @param string $method The method of jsonrpc
  95. * @param array $params The params of jsonrpc
  96. * @return bool|mixed
  97. */
  98. public function send($method, $params = [], &$error = null)
  99. {
  100. $query = [];
  101. $query['jsonrpc'] = '2.0';
  102. $query['method'] = $method;
  103. $query['params'] = $params;
  104. $query['id'] = ++$this->id;
  105. $rawResp = Utils::post($this->url, json_encode($query), $this->timeout);
  106. $resp = @json_decode($rawResp, true);
  107. if ($resp === false || !is_array($resp)) {
  108. $error = $resp;
  109. return false;
  110. }
  111. if (isset($resp['error'])) {
  112. $error = $resp['error'];
  113. return false;
  114. }
  115. return $resp['result'];
  116. }
  117. public function __call($name, $params)
  118. {
  119. return $this->send($name, $params);
  120. }
  121. }