TronRPC.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * 获取Base58地址
  106. * @param $str
  107. * @return string
  108. */
  109. function getBase58CheckAddress($str){
  110. $new_str = $this->getTron()->getBase58CheckAddress($str);
  111. return $new_str;
  112. }
  113. /**
  114. * 获取TRX数量
  115. * @param $address
  116. * @return float
  117. */
  118. function getBalance($address)
  119. {
  120. $tron = $this->getTron();
  121. $balance = $tron->getBalance($address, true);
  122. return $balance;
  123. }
  124. /**
  125. * 获取合约余额
  126. * @param $contract_address
  127. * @param $address
  128. * @param int $digits
  129. * @return float|int
  130. */
  131. function getTokenBalance($contract_address, $address, $digits = 6)
  132. {
  133. $tron = $this->getTron();
  134. $abi = $this->getContract($contract_address);
  135. $transaction = $tron->getTransactionBuilder()->triggerConstantContract($abi, $tron->address2HexString($contract_address), 'balanceOf', [$tron->address2HexString($address)], $tron->address2HexString($address));
  136. if (empty($transaction[0]) || !is_object($transaction[0]) || !method_exists($transaction[0], 'toString')) {
  137. return 0;
  138. }
  139. $token_balance = Utils::int2fund($transaction[0]->toString(), $digits);
  140. return $token_balance;
  141. }
  142. /**
  143. * 生成Trx交易信息
  144. * @param $to_address 收款地址
  145. * @param $money 转账金额
  146. * @param $from_address 转出地址
  147. * @return array
  148. */
  149. function createTrx($to_address, $money, $from_address)
  150. {
  151. $tron = $this->getTron();
  152. $transaction = $tron->getTransactionBuilder()->sendTrx($to_address, $money, $from_address);
  153. return $transaction;
  154. }
  155. /**
  156. * 生成合约转账交易信息
  157. * @param $to_address 收款地址
  158. * @param $contract_address 合约地址
  159. * @param $money 转账金额
  160. * @param $from_address 转出地址
  161. * @return mixed
  162. */
  163. function createContract($to_address, $contract_address, $money, $from_address)
  164. {
  165. $tron = $this->getTron();
  166. $abi = $this->getContract($contract_address);
  167. $transaction = $tron->getTransactionBuilder()->triggerSmartContract($abi, $tron->address2HexString($contract_address), 'transfer', [$tron->address2HexString($to_address), Utils::fund2int($money, 6)], 1000000, $tron->address2HexString($from_address));
  168. return $transaction;
  169. }
  170. /**
  171. * 交易离线签名
  172. * @param $transaction 交易信息
  173. * @param $private_key 秘钥
  174. * @return array
  175. */
  176. function signTransaction($transaction, $private_key)
  177. {
  178. $tron = $this->getTron();
  179. $tron->setPrivateKey($private_key);
  180. $signedTransaction = $tron->signTransaction($transaction);
  181. return $signedTransaction;
  182. }
  183. /**
  184. * 广播签名信息
  185. * @param $signedTransaction 签名信息
  186. * @return array
  187. */
  188. function sendRawTransaction($signedTransaction)
  189. {
  190. $tron = $this->getTron();
  191. $response = $tron->sendRawTransaction($signedTransaction);
  192. return $response;
  193. }
  194. /**
  195. * 获取合约基本信息
  196. * @param $contract_address
  197. * @return array
  198. */
  199. function getContract($contract_address)
  200. {
  201. $abi = Redis::get($contract_address);
  202. if (empty($abi)) {
  203. $tron = $this->getTron();
  204. $ret = $tron->getManager()->request('wallet/getcontract', ['value' => $tron->address2HexString($contract_address)]);
  205. $abi = empty($ret['abi']['entrys']) ? [] : $ret['abi']['entrys'];
  206. Redis::set($contract_address, json_encode($abi));
  207. } else {
  208. $abi = json_decode($abi, true);
  209. }
  210. return $abi;
  211. }
  212. function getBlockByNum()
  213. {
  214. $tron = $this->getTron();
  215. $block_num = $tron->getCurrentBlock();
  216. return $block_num;
  217. }
  218. function getBlockById($block_id)
  219. {
  220. $tron = $this->getTron();
  221. $block_info = $tron->getBlockByNumber($block_id);
  222. return $block_info;
  223. }
  224. /**
  225. * @param array $params The params of jsonrpc
  226. * @return bool|mixed
  227. */
  228. public function send($url, $params = [], &$error = null)
  229. {
  230. $ch = curl_init();
  231. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  232. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  233. if (!empty($params)) curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
  234. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  235. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  236. if (!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  237. $request = curl_exec($ch);
  238. curl_close($ch);
  239. if ($request === false) {
  240. return false;
  241. }
  242. $resp = @json_decode($request, true);
  243. if ($resp === false || !is_array($resp)) {
  244. $error = $resp;
  245. return false;
  246. }
  247. if (isset($resp['error'])) {
  248. $error = $resp['error'];
  249. return false;
  250. }
  251. return $resp;
  252. }
  253. }