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; } }