BroadcastServer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\Broadcast;
  4. use App\Models\CoinType;
  5. use App\Models\Config;
  6. use App\Models\MemberCoins;
  7. use App\Models\Withdraw;
  8. use App\Servers\Icon\TronRPC;
  9. use App\Servers\Icon\Utils;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * 广播接口
  13. */
  14. class BroadcastServer
  15. {
  16. public static $type_str=[
  17. 1=>'BTC充值',
  18. 2=>'ETH充值',
  19. 3=>'USDT充值',
  20. 4=>'BTC提现',
  21. 5=>'ETH提现',
  22. 6=>'USDT提现',
  23. 7=>'ETH手续费',
  24. ];
  25. /**
  26. * 创建广播信息
  27. * @param $coin_id
  28. * @param $money
  29. * @param $from
  30. * @param $from_key
  31. * @param $to
  32. * @param $member_id
  33. * @param int $order_id
  34. * @param int $status_type
  35. * @param int $status
  36. * @return mixed
  37. */
  38. public static function addBroadcast($coin_id, $money, $from, $from_key, $to, $member_id, $order_id = 0, $status_type = 0, $status = 1)
  39. {
  40. //保留数量
  41. $transaction = [
  42. 'depend' => 0,
  43. 'from' => $from,
  44. 'to' => $to,
  45. 'nonce' => '',
  46. 'coin_id' => $coin_id,
  47. 'value' => '0x0',
  48. 'data' => !empty($tx['data']) ? $tx['data'] : Utils::fixHex(bin2hex('NYT')),
  49. 'chain_id' => 0,
  50. 'gas_price' => '',
  51. 'gas_limit' => '',
  52. 'to_address' => $to,
  53. 'sign' => '',
  54. 'hash' => $status == 5 ? '系统内部转账,直接成功,无需链上广播' : '',
  55. 'from_key' => $from_key,
  56. 'money' =>round($money,6),
  57. 'status' => $status,
  58. 'type' => $status_type,
  59. 'm_id' => $member_id,
  60. 'order_id' => $order_id,
  61. 'send_money' => $money,
  62. ];
  63. $transaction = Broadcast::create($transaction);
  64. return $transaction->{'id'};
  65. }
  66. /**
  67. * 广播查询
  68. */
  69. public static function getBroadcast()
  70. {
  71. $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();
  72. $ret_type=false;
  73. if (empty($list)) return $ret_type;
  74. foreach ($list as $value) {
  75. $ret = TronRPC::CreationTron()->getTransaction($value->{'hash'});
  76. if (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'SUCCESS') {
  77. $update_info['status'] = 4;
  78. $update_info['end_time'] = time();
  79. } elseif (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'REVERT') {
  80. $update_info['status'] = 3;
  81. }
  82. if(!empty($update_info))Broadcast::where('id',$value->{'id'})->update($update_info);
  83. }
  84. return $ret_type;
  85. }
  86. /**
  87. * 交易广播
  88. */
  89. function sendBroadcast(){
  90. $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();
  91. foreach ($list as $transaction){
  92. //验证trx余额
  93. $trx_num = TronRPC::CreationTron()->getBalance($transaction['from']);
  94. if ($trx_num < 5) {
  95. //trx不足,创建手续费订单
  96. CommonServer::creatServer()->addErrorRecord('系统账户手续不足',$transaction);
  97. continue;
  98. }
  99. $send_data =TronRPC::CreationTron()->createContract($transaction['to'], $transaction['contract_address'], $transaction['money'] * 1, $transaction['from']);
  100. if(empty($send_data)){
  101. CommonServer::creatServer()->addErrorRecord('交易信息生成失败',$transaction);
  102. continue;
  103. }else{
  104. $sign_data = TronRPC::CreationTron()->signTransaction($send_data, PassServer::creatServer()->getSecretKey('sadsadas','1'));
  105. if (empty($sign_data['signature'][0])) {
  106. CommonServer::creatServer()->addErrorRecord('签名失败',$sign_data);
  107. $transaction['status'] = 3;
  108. } else {
  109. $transaction['sign'] = $sign_data['signature'][0];
  110. $hash_data = TronRPC::CreationTron()->sendRawTransaction($sign_data);
  111. if (empty($hash_data['result'])) {
  112. CommonServer::creatServer()->addErrorRecord('广播失败',$hash_data);
  113. $transaction['status'] = 3;
  114. } else {
  115. $transaction['hash'] = $hash_data['txid'];
  116. $transaction['status'] = 2;
  117. }
  118. }
  119. $transaction->save();
  120. }
  121. }
  122. }
  123. }