TronRPC.php 7.3 KB

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