| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Servers;
- use App\Models\Broadcast;
- use App\Models\CoinType;
- use App\Models\Config;
- use App\Models\MemberCoins;
- use App\Models\Withdraw;
- use App\Servers\Icon\TronRPC;
- use App\Servers\Icon\Utils;
- use Illuminate\Support\Facades\Log;
- /**
- * 广播接口
- */
- class BroadcastServer
- {
- public static $type_str=[
- 1=>'BTC充值',
- 2=>'ETH充值',
- 3=>'USDT充值',
- 4=>'BTC提现',
- 5=>'ETH提现',
- 6=>'USDT提现',
- 7=>'ETH手续费',
- ];
- /**
- * 创建广播信息
- * @param $coin_id
- * @param $money
- * @param $from
- * @param $from_key
- * @param $to
- * @param $member_id
- * @param int $order_id
- * @param int $status_type
- * @param int $status
- * @return mixed
- */
- public static function addBroadcast($coin_id, $money, $from, $from_key, $to, $member_id, $order_id = 0, $status_type = 0, $status = 1)
- {
- //保留数量
- $transaction = [
- 'depend' => 0,
- 'from' => $from,
- 'to' => $to,
- 'nonce' => '',
- 'coin_id' => $coin_id,
- 'value' => '0x0',
- 'data' => !empty($tx['data']) ? $tx['data'] : Utils::fixHex(bin2hex('NYT')),
- 'chain_id' => 0,
- 'gas_price' => '',
- 'gas_limit' => '',
- 'to_address' => $to,
- 'sign' => '',
- 'hash' => $status == 5 ? '系统内部转账,直接成功,无需链上广播' : '',
- 'from_key' => $from_key,
- 'money' =>round($money,6),
- 'status' => $status,
- 'type' => $status_type,
- 'm_id' => $member_id,
- 'order_id' => $order_id,
- 'send_money' => $money,
- ];
- $transaction = Broadcast::create($transaction);
- return $transaction->{'id'};
- }
- /**
- * 广播查询
- */
- public static function getBroadcast()
- {
- $list = Broadcast::where('status', '2')->select(['id', 'm_id', 'from', 'to', 'send_money', 'value', 'coin_id', 'type', 'sign', 'money', 'from_key', 'coin_id', 'hash', 'send_money', 'order_id'])->limit(100)->get();
- $ret_type=false;
- if (empty($list)) return $ret_type;
- foreach ($list as $value) {
- $ret = TronRPC::CreationTron()->getTransaction($value->{'hash'});
- if (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'SUCCESS') {
- $update_info['status'] = 4;
- $update_info['end_time'] = time();
- } elseif (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'REVERT') {
- $update_info['status'] = 3;
- }
- if(!empty($update_info))Broadcast::where('id',$value->{'id'})->update($update_info);
- }
- return $ret_type;
- }
- /**
- * 交易广播
- */
- function sendBroadcast(){
- $list = Broadcast::where('status', '1')->where([['end_time','=',0,'or'],['end_time','<=',time()-60,'or']])->select(['id', 'm_id', 'from', 'to', 'nonce', 'value', 'data', 'gas_price', 'gas_limit', 'sign', 'money', 'from_key', 'coin_id', 'depend','type','contract_address','order_id'])->limit(100)->get();
- foreach ($list as $transaction){
- //验证trx余额
- $trx_num = TronRPC::CreationTron()->getBalance($transaction['from']);
- if ($trx_num < 5) {
- //trx不足,创建手续费订单
- CommonServer::creatServer()->addErrorRecord('系统账户手续不足',$transaction);
- continue;
- }
- $send_data =TronRPC::CreationTron()->createContract($transaction['to'], $transaction['contract_address'], $transaction['money'] * 1, $transaction['from']);
- if(empty($send_data)){
- CommonServer::creatServer()->addErrorRecord('交易信息生成失败',$transaction);
- continue;
- }else{
- $sign_data = TronRPC::CreationTron()->signTransaction($send_data, PassServer::creatServer()->getSecretKey('sadsadas','1'));
- if (empty($sign_data['signature'][0])) {
- CommonServer::creatServer()->addErrorRecord('签名失败',$sign_data);
- $transaction['status'] = 3;
- } else {
- $transaction['sign'] = $sign_data['signature'][0];
- $hash_data = TronRPC::CreationTron()->sendRawTransaction($sign_data);
- if (empty($hash_data['result'])) {
- CommonServer::creatServer()->addErrorRecord('广播失败',$hash_data);
- $transaction['status'] = 3;
- } else {
- $transaction['hash'] = $hash_data['txid'];
- $transaction['status'] = 2;
- }
- }
- $transaction->save();
- }
- }
- }
- }
|