TronRPC.php 8.0 KB

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