TronRPC.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Servers\Icon;
  3. use IEXBase\TronAPI\Provider\HttpProvider;
  4. use IEXBase\TronAPI\Tron;
  5. use Illuminate\Support\Facades\Redis;
  6. /**
  7. * Class EthereumRPC
  8. * @package common\models\ethereum
  9. */
  10. class TronRPC
  11. {
  12. /**
  13. * @var string RPC URL
  14. */
  15. // public $url = 'http://tex.tomiao.com';
  16. /**
  17. * 对应节点服务器地址
  18. * @var string
  19. */
  20. public $full_node_url = 'http://api.trongrid.io';
  21. public $solidity_node_url = 'http://api.trongrid.io';
  22. public $event_server_url = 'https://api.trongrid.io';
  23. public $chainId = '1';
  24. private $date = '';
  25. private $nodes=[
  26. '3.225.171.164',
  27. '52.53.189.99',
  28. '18.196.99.16',
  29. '34.253.187.192',
  30. '18.133.82.227',
  31. '35.180.51.163',
  32. '54.252.224.209',
  33. '52.15.93.92',
  34. '34.220.77.106',
  35. '13.124.62.58',
  36. '3.218.137.187',
  37. '34.237.210.82',
  38. ];
  39. // request id
  40. protected $id = 0;
  41. /**
  42. * RPC timeout
  43. * @var int
  44. */
  45. public $timeout = 60;
  46. /**
  47. * 单列对象
  48. * @var null
  49. */
  50. static private $tron_model = null;
  51. /**
  52. *
  53. * @var null
  54. */
  55. private $tron = null;
  56. static function CreationTron()
  57. {
  58. if (empty(self::$tron_model)) {
  59. self::$tron_model = new TronRPC();
  60. }
  61. return self::$tron_model;
  62. }
  63. /**
  64. * 获取节点控制对象
  65. * @return Tron|null
  66. */
  67. function getTron()
  68. {
  69. if(empty($this->date) || $this->date==date('Ymd')){
  70. $this->tron='';
  71. }
  72. if (empty($this->tron)) {
  73. $this->setNodeUrl();
  74. $fullNode = new HttpProvider($this->full_node_url);
  75. $solidityNode = new HttpProvider($this->solidity_node_url);
  76. $eventServer = new HttpProvider($this->event_server_url);
  77. $tron = new Tron($fullNode, $solidityNode, $eventServer);
  78. $this->tron = $tron;
  79. } else {
  80. $tron = $this->tron;
  81. }
  82. return $tron;
  83. }
  84. function setNodeUrl(){
  85. $max=count($this->nodes)-1;
  86. $num=rand(0,$max);
  87. $ip=$this->nodes[$num];
  88. $this->full_node_url='http://'.$ip.':8090';
  89. $this->solidity_node_url='http://'.$ip.':8091';
  90. }
  91. /**
  92. * 离线生成波场地址
  93. * @return array
  94. */
  95. function addTronAddress()
  96. {
  97. $tron = $this->getTron();
  98. $generateAddress = $tron->generateAddress(); // 离线生成地址
  99. return $generateAddress->getRawData();
  100. }
  101. /**
  102. * 获取Base58地址
  103. * @param $str
  104. * @return string
  105. */
  106. function getBase58CheckAddress($str){
  107. $new_str = $this->getTron()->getBase58CheckAddress($str);
  108. return $new_str;
  109. }
  110. /**
  111. * 获取TRX数量
  112. * @param $address
  113. * @return float
  114. */
  115. function getBalance($address)
  116. {
  117. $tron = $this->getTron();
  118. $balance = $tron->getBalance($address, true);
  119. return $balance;
  120. }
  121. /**
  122. * 获取合约余额
  123. * @param $contract_address
  124. * @param $address
  125. * @param int $digits
  126. * @return float|int
  127. */
  128. function getTokenBalance($contract_address, $address, $digits = 6)
  129. {
  130. $tron = $this->getTron();
  131. $abi = $this->getContract($contract_address);
  132. $transaction = $tron->getTransactionBuilder()->triggerConstantContract($abi, $tron->address2HexString($contract_address), 'balanceOf', [$tron->address2HexString($address)], $tron->address2HexString($address));
  133. if (empty($transaction[0]) || !is_object($transaction[0]) || !method_exists($transaction[0], 'toString')) {
  134. return 0;
  135. }
  136. $token_balance = Utils::int2fund($transaction[0]->toString(), $digits);
  137. return $token_balance;
  138. }
  139. /**
  140. * 生成Trx交易信息
  141. * @param $to_address 收款地址
  142. * @param $money 转账金额
  143. * @param $from_address 转出地址
  144. * @return array
  145. */
  146. function createTrx($to_address, $money, $from_address)
  147. {
  148. $tron = $this->getTron();
  149. $transaction = $tron->getTransactionBuilder()->sendTrx($to_address, $money, $from_address);
  150. return $transaction;
  151. }
  152. /**
  153. * 生成合约转账交易信息
  154. * @param $to_address 收款地址
  155. * @param $contract_address 合约地址
  156. * @param $money 转账金额
  157. * @param $from_address 转出地址
  158. * @return mixed
  159. */
  160. function createContract($to_address, $contract_address, $money, $from_address)
  161. {
  162. $tron = $this->getTron();
  163. $abi = $this->getContract($contract_address);
  164. $transaction = $tron->getTransactionBuilder()->triggerSmartContract($abi, $tron->address2HexString($contract_address), 'transfer', [$tron->address2HexString($to_address), Utils::fund2int($money, 6)], 1000000, $tron->address2HexString($from_address));
  165. return $transaction;
  166. }
  167. /**
  168. * 交易离线签名
  169. * @param $transaction 交易信息
  170. * @param $private_key 秘钥
  171. * @return array
  172. */
  173. function signTransaction($transaction, $private_key)
  174. {
  175. $tron = $this->getTron();
  176. $tron->setPrivateKey($private_key);
  177. $signedTransaction = $tron->signTransaction($transaction);
  178. return $signedTransaction;
  179. }
  180. /**
  181. * 广播签名信息
  182. * @param $signedTransaction 签名信息
  183. * @return array
  184. */
  185. function sendRawTransaction($signedTransaction)
  186. {
  187. $tron = $this->getTron();
  188. $response = $tron->sendRawTransaction($signedTransaction);
  189. return $response;
  190. }
  191. /**
  192. * 获取合约基本信息
  193. * @param $contract_address
  194. * @return array
  195. */
  196. function getContract($contract_address)
  197. {
  198. $abi = Redis::get($contract_address);
  199. if (empty($abi)) {
  200. $tron = $this->getTron();
  201. $ret = $tron->getManager()->request('wallet/getcontract', ['value' => $tron->address2HexString($contract_address)]);
  202. $abi = empty($ret['abi']['entrys']) ? [] : $ret['abi']['entrys'];
  203. Redis::set($contract_address, json_encode($abi));
  204. } else {
  205. $abi = json_decode($abi, true);
  206. }
  207. return $abi;
  208. }
  209. function getBlockByNum()
  210. {
  211. $tron = $this->getTron();
  212. $block_num = $tron->getCurrentBlock();
  213. if(empty($block_num['block_header'])){
  214. return 0;
  215. }
  216. if(empty($block_num['block_header']['raw_data']['number'])){
  217. return 0;
  218. }
  219. return $block_num['block_header']['raw_data']['number'];
  220. }
  221. function getBlockById($block_id)
  222. {
  223. $tron = $this->getTron();
  224. $block_info = $tron->getBlockByNumber($block_id);
  225. return $block_info;
  226. }
  227. /**
  228. * @param array $params The params of jsonrpc
  229. * @return bool|mixed
  230. */
  231. public function send($url, $params = [], &$error = null)
  232. {
  233. $ch = curl_init();
  234. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  235. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  236. if (!empty($params)) curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
  237. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  238. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  239. if (!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  240. $request = curl_exec($ch);
  241. curl_close($ch);
  242. if ($request === false) {
  243. return false;
  244. }
  245. $resp = @json_decode($request, true);
  246. if ($resp === false || !is_array($resp)) {
  247. $error = $resp;
  248. return false;
  249. }
  250. if (isset($resp['error'])) {
  251. $error = $resp['error'];
  252. return false;
  253. }
  254. return $resp;
  255. }
  256. }