EnergyWalletTradeUsdtServices.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Service\Energy;
  3. use App\Model\Energy\EnergyWalletTradeList;
  4. use App\Library\Log;
  5. use Hyperf\DbConnection\Db;
  6. class EnergyWalletTradeUsdtServices
  7. {
  8. private $list_success_count = 0; //交易列表拉取成功数
  9. private $list_error_count = 0; //交易列表拉取失败数
  10. /**
  11. * 获取能力钱包数据
  12. * @param $in_list [钱包数据]
  13. * @param $start_timestamp [开始时间 13位时间戳]
  14. * @param $nexturl [下一页]
  15. */
  16. public function getList($in_list,$start_timestamp,$nexturl='0'){
  17. if($nexturl != '0'){
  18. $url = $nexturl;
  19. }else{
  20. $url = 'https://api.trongrid.io/v1/accounts/'.$in_list['receive_wallet'].'/transactions/trc20?limit=50&only_to=true&min_timestamp='.$start_timestamp.'&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
  21. }
  22. $api_key = config('apikey.gridapikey');
  23. $apikeyrand = $api_key[array_rand($api_key)];
  24. $heders = [
  25. "TRON-PRO-API-KEY:".$apikeyrand
  26. ];
  27. $data = Get_Pay($url,null,$heders);
  28. if(!empty($data) && $data){
  29. $data = json_decode($data,true);
  30. $data = $this->handleWalletData($data,$in_list);
  31. }
  32. return ['success_count'=>$this->list_success_count,'error_count'=>$this->list_error_count];
  33. }
  34. /**
  35. * 处理收款数据
  36. * @param $data [收款数据]
  37. * @param $in_list [钱包数据]
  38. */
  39. public function handleWalletData($data,$in_list){
  40. if(isset($data['data'])){
  41. $list = $data['data'];
  42. if($list){
  43. // 校验hash是否存在
  44. $hash_list = array_column($list,'transaction_id');
  45. $transaction_hash_list = EnergyWalletTradeList::whereIn('tx_hash',$hash_list)->pluck('tx_hash')->toArray();
  46. $success_hash_list = []; //成功hash值数组
  47. $error_hash_list = []; //失败hash值数组
  48. $time = nowDate();
  49. foreach ($list as $k => $v) {
  50. if(!in_array($v['transaction_id'],$transaction_hash_list) && $v['type'] == 'Transfer'){
  51. Db::beginTransaction();
  52. try {
  53. $res = $this->AddWalletData($v,$time,$in_list);
  54. if($res['code'] == 200){
  55. $success_hash_list[] = $v['transaction_id'];
  56. }else{
  57. $error_hash_list[] = $v['transaction_id'].'--------'.'不是收款记录或金额为0,交易失败';
  58. }
  59. Db::commit();
  60. }catch (\Exception $e){
  61. Db::rollBack();
  62. $error_hash_list[] = $v['transaction_id'].'--------'.$e->getMessage();
  63. }
  64. }else{
  65. $error_hash_list[] = $v['transaction_id'].'--------'.'已存在';
  66. }
  67. }
  68. $success_hash_list_count = count($success_hash_list);
  69. $error_hash_list_count = count($error_hash_list);
  70. // $log_list = [
  71. // 'receive_wallet' => $in_list['receive_wallet'],
  72. // 'success_hash_list' => $success_hash_list,
  73. // 'error_hash_list' => $error_hash_list,
  74. // ];
  75. // $this->log('getenergywallettrxtrade','钱包地址:'.$in_list['receive_wallet'].',成功hash值'.$success_hash_list_count.'条,失败hash值'.$error_hash_list_count.'条');
  76. // $this->log('getenergywallettrxtrade',json_encode($log_list,JSON_UNESCAPED_UNICODE));
  77. $this->list_success_count = $this->list_success_count + $success_hash_list_count;
  78. $this->list_error_count = $this->list_error_count + $error_hash_list_count;
  79. // 如果有下一页,再次去获取
  80. if(isset($data['meta']['links']['next'])){
  81. $this->getList($in_list,0,$data['meta']['links']['next']);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * 整合添加收款数据
  88. * @param $data [收款数据]
  89. * @param $time [当前时间]
  90. * @param $in_list [钱包数据]
  91. */
  92. public function AddWalletData($data,$time,$in_list){
  93. $txid_list = [];
  94. $txid_list['tx_hash'] = $data['transaction_id']; //交易hash
  95. $txid_list['transferfrom_address'] = $data['from']; //来源钱包地址
  96. $txid_list['timestamp'] = $data['block_timestamp']; //区块号时间戳
  97. $txid_list['transferto_address'] = $in_list['receive_wallet']; //收款钱包地址
  98. $txid_list['coin_name'] = 'usdt';
  99. $txid_list['amount'] = calculationExcept($data['value'],6); //交易数额
  100. $txid_list['get_time'] = $time; //拉取时间
  101. $txid_list['process_status'] = 1; //待兑换
  102. $txid_list['process_comments'] = '待处理'; //处理备注
  103. $txid_list['process_time'] = $time; //处理时间
  104. EnergyWalletTradeList::insert($txid_list); //添加收款钱包交易列表
  105. return ['code' => 200];
  106. }
  107. /**
  108. * 记入日志
  109. * @param $log_title [日志路径]
  110. * @param $message [内容,不支持数组]
  111. * @param $remarks [备注]
  112. */
  113. protected function log($log_title,$message,$remarks='info'){
  114. Log::get($remarks,$log_title)->info($message);
  115. }
  116. }