123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace App\Servers\Icon;
- use IEXBase\TronAPI\Provider\HttpProvider;
- use IEXBase\TronAPI\Tron;
- use Illuminate\Support\Facades\Redis;
- /**
- * Class EthereumRPC
- * @package common\models\ethereum
- */
- class TronRPC
- {
- /**
- * @var string RPC URL
- */
- // public $url = 'http://tex.tomiao.com';
- /**
- * 对应节点服务器地址
- * @var string
- */
- public $full_node_url = 'http://api.trongrid.io';
- public $solidity_node_url = 'http://api.trongrid.io';
- public $event_server_url = 'https://api.trongrid.io';
- public $chainId = '1';
- private $date = '';
- private $nodes=[
- '3.225.171.164',
- '52.53.189.99',
- '18.196.99.16',
- '34.253.187.192',
- '18.133.82.227',
- '35.180.51.163',
- '54.252.224.209',
- '52.15.93.92',
- '34.220.77.106',
- '13.124.62.58',
- '3.218.137.187',
- '34.237.210.82',
- ];
- // request id
- protected $id = 0;
- /**
- * RPC timeout
- * @var int
- */
- public $timeout = 60;
- /**
- * 单列对象
- * @var null
- */
- static private $tron_model = null;
- /**
- *
- * @var null
- */
- private $tron = null;
- static function CreationTron()
- {
- if (empty(self::$tron_model)) {
- self::$tron_model = new TronRPC();
- }
- return self::$tron_model;
- }
- /**
- * 获取节点控制对象
- * @return Tron|null
- */
- function getTron()
- {
- if(empty($this->date) || $this->date==date('Ymd')){
- $this->tron='';
- }
- if (empty($this->tron)) {
- $this->setNodeUrl();
- $fullNode = new HttpProvider($this->full_node_url);
- $solidityNode = new HttpProvider($this->solidity_node_url);
- $eventServer = new HttpProvider($this->event_server_url);
- $tron = new Tron($fullNode, $solidityNode, $eventServer);
- $this->tron = $tron;
- } else {
- $tron = $this->tron;
- }
- return $tron;
- }
- function setNodeUrl(){
- $max=count($this->nodes)-1;
- $num=rand(0,$max);
- $ip=$this->nodes[$num];
- $this->full_node_url='http://'.$ip.':8090';
- $this->solidity_node_url='http://'.$ip.':8091';
- }
- /**
- * 离线生成波场地址
- * @return array
- */
- function addTronAddress()
- {
- $tron = $this->getTron();
- $generateAddress = $tron->generateAddress(); // 离线生成地址
- return $generateAddress->getRawData();
- }
- /**
- * 获取Base58地址
- * @param $str
- * @return string
- */
- function getBase58CheckAddress($str){
- $new_str = $this->getTron()->getBase58CheckAddress($str);
- return $new_str;
- }
- /**
- * 获取交易详情
- * @param $txID
- * @return array|\string[][][]
- */
- function getTransaction($txID){
- try {
- $data = $this->getTron()->getTransaction($txID);
- }catch (\Exception $e){
- $data=['ret'=>[['contractRet'=>'']]];
- }
- return $data;
- }
- /**
- * 获取TRX数量
- * @param $address
- * @return float
- */
- function getBalance($address)
- {
- $tron = $this->getTron();
- $balance = $tron->getBalance($address, true);
- return $balance;
- }
- /**
- * 获取合约余额
- * @param $contract_address
- * @param $address
- * @param int $digits
- * @return float|int
- */
- function getTokenBalance($contract_address, $address, $digits = 6)
- {
- $tron = $this->getTron();
- $abi = $this->getContract($contract_address);
- $transaction = $tron->getTransactionBuilder()->triggerConstantContract($abi, $tron->address2HexString($contract_address), 'balanceOf', [$tron->address2HexString($address)], $tron->address2HexString($address));
- if (empty($transaction[0]) || !is_object($transaction[0]) || !method_exists($transaction[0], 'toString')) {
- return 0;
- }
- $token_balance = Utils::int2fund($transaction[0]->toString(), $digits);
- return $token_balance;
- }
- /**
- * 生成Trx交易信息
- * @param $to_address 收款地址
- * @param $money 转账金额
- * @param $from_address 转出地址
- * @return array
- */
- function createTrx($to_address, $money, $from_address)
- {
- $tron = $this->getTron();
- $transaction = $tron->getTransactionBuilder()->sendTrx($to_address, $money, $from_address);
- return $transaction;
- }
- /**
- * 生成合约转账交易信息
- * @param $to_address 收款地址
- * @param $contract_address 合约地址
- * @param $money 转账金额
- * @param $from_address 转出地址
- * @return mixed
- */
- function createContract($to_address, $contract_address, $money, $from_address)
- {
- $tron = $this->getTron();
- $abi = $this->getContract($contract_address);
- $transaction = $tron->getTransactionBuilder()->triggerSmartContract($abi, $tron->address2HexString($contract_address), 'transfer', [$tron->address2HexString($to_address), Utils::fund2int($money, 6)], 1000000, $tron->address2HexString($from_address));
- return $transaction;
- }
- /**
- * 交易离线签名
- * @param $transaction 交易信息
- * @param $private_key 秘钥
- * @return array
- */
- function signTransaction($transaction, $private_key)
- {
- $tron = $this->getTron();
- $tron->setPrivateKey($private_key);
- $signedTransaction = $tron->signTransaction($transaction);
- return $signedTransaction;
- }
- /**
- * 广播签名信息
- * @param $signedTransaction 签名信息
- * @return array
- */
- function sendRawTransaction($signedTransaction)
- {
- $tron = $this->getTron();
- $response = $tron->sendRawTransaction($signedTransaction);
- return $response;
- }
- /**
- * 获取合约基本信息
- * @param $contract_address
- * @return array
- */
- function getContract($contract_address)
- {
- $abi = Redis::get($contract_address);
- if (empty($abi)) {
- $tron = $this->getTron();
- $ret = $tron->getManager()->request('wallet/getcontract', ['value' => $tron->address2HexString($contract_address)]);
- $abi = empty($ret['abi']['entrys']) ? [] : $ret['abi']['entrys'];
- Redis::set($contract_address, json_encode($abi));
- } else {
- $abi = json_decode($abi, true);
- }
- return $abi;
- }
- function getBlockByNum()
- {
- $tron = $this->getTron();
- $block_num = $tron->getCurrentBlock();
- if(empty($block_num['block_header'])){
- return 0;
- }
- if(empty($block_num['block_header']['raw_data']['number'])){
- return 0;
- }
- return $block_num['block_header']['raw_data']['number'];
- }
- function getBlockById($block_id)
- {
- $tron = $this->getTron();
- $block_info = $tron->getBlockByNumber($block_id);
- return $block_info;
- }
- /**
- * @param array $params The params of jsonrpc
- * @return bool|mixed
- */
- public function send($url, $params = [], &$error = null)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
- if (!empty($params)) curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if (!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $request = curl_exec($ch);
- curl_close($ch);
- if ($request === false) {
- return false;
- }
- $resp = @json_decode($request, true);
- if ($resp === false || !is_array($resp)) {
- $error = $resp;
- return false;
- }
- if (isset($resp['error'])) {
- $error = $resp['error'];
- return false;
- }
- return $resp;
- }
- }
|