CollectService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Collect;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Helpers\TronHelper;
  9. use App\Services\WalletService;
  10. /**
  11. * 归集记录
  12. */
  13. class CollectService extends BaseService
  14. {
  15. public static $THRESHOLD = 10; // 最小归集金额(USDT)
  16. /**
  17. * @description: 模型
  18. * @return {string}
  19. */
  20. public static function model() :string
  21. {
  22. return Collect::class;
  23. }
  24. /**
  25. * @description: 枚举
  26. * @return {*}
  27. */
  28. public static function enum() :string
  29. {
  30. return '';
  31. }
  32. /**
  33. * @description: 获取查询条件
  34. * @param {array} $search 查询内容
  35. * @return {array}
  36. */
  37. public static function getWhere(array $search = []) :array
  38. {
  39. $where = [];
  40. if(isset($search['coin']) && !empty($search['coin'])){
  41. $where[] = ['coin', '=', $search['coin']];
  42. }
  43. if(isset($search['net']) && !empty($search['net'])){
  44. $where[] = ['net', '=', $search['net']];
  45. }
  46. if(isset($search['to_address']) && !empty($search['to_address'])){
  47. $where[] = ['to_address', '=', $search['to_address']];
  48. }
  49. if(isset($search['id']) && !empty($search['id'])){
  50. $where[] = ['id', '=', $search['id']];
  51. }
  52. if(isset($search['txid']) && !empty($search['txid'])){
  53. $where[] = ['txid', '=', $search['txid']];
  54. }
  55. if(isset($search['status']) && $search['status'] != ''){
  56. $where[] = ['status', '=', $search['status']];
  57. }
  58. if (isset($search['amount']) && is_numeric($search['amount'])) {
  59. $where[] = ['amount', '>=', $search['amount']];
  60. }
  61. return $where;
  62. }
  63. /**
  64. * @description: 查询单条数据
  65. * @param array $search
  66. * @return \App\Models\Coin|null
  67. */
  68. public static function findOne(array $search): ?Collect
  69. {
  70. return self::model()::where(self::getWhere($search))->first();
  71. }
  72. /**
  73. * @description: 查询所有数据
  74. * @param array $search
  75. * @return \Illuminate\Database\Eloquent\Collection
  76. */
  77. public static function findAll(array $search = [])
  78. {
  79. return self::model()::where(self::getWhere($search))->get();
  80. }
  81. /**
  82. * @description: 分页查询
  83. * @param array $search
  84. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  85. */
  86. public static function paginate(array $search = [])
  87. {
  88. $limit = isset($search['limit'])?$search['limit']:15;
  89. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  90. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  91. }
  92. /**
  93. * @description: 生成归集记录
  94. * @param {*} $address
  95. * @param {*} $coin
  96. * @param {*} $net
  97. * @return {*}
  98. */
  99. public static function createCollect($address ,$coin ,$net)
  100. {
  101. $amount = TronHelper::getTrc20Balance($address); // 获取地址的余额
  102. $info = self::findOne(['from_address' => $address ,'status' => self::model()::STATUS_STAY]);
  103. if($amount >= 0 ){
  104. if(empty($info)){
  105. $data = [];
  106. $data['from_address'] = $address;
  107. $data['amount'] = $amount;
  108. $data['coin'] = $coin;
  109. $data['net'] = $net;
  110. self::model()::create($data);
  111. }else{
  112. $info->amount = $amount;
  113. $info->save();
  114. }
  115. }
  116. }
  117. /**
  118. * @description: 处理待归集的
  119. * @return {*}
  120. */
  121. public static function syncCollectStay()
  122. {
  123. $to_address = self::getUsdtAddress(); // 转账的接收地址
  124. $trx_private_key = self::getTrxPrivateKey(); // 获取TRX能量的秘钥
  125. if($to_address && $trx_private_key){
  126. $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
  127. foreach($list as $k => $v){
  128. $data = [];
  129. $wallets = WalletService::findOne(['address' => $v['from_address']]);
  130. $privateKey = $wallets['private_key'];
  131. $trxBalance = TronHelper::getTrxBalance($v['from_address']);
  132. if($trxBalance < 10){
  133. TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
  134. }
  135. $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
  136. $data['to_address'] = $to_address;
  137. if($transferResult['success']){
  138. $data['txid'] = $transferResult['txid'];
  139. $data['remark'] = 'success';
  140. $data['status'] = self::model()::STATUS_START;
  141. }else{
  142. $data['remark'] = $transferResult['error'];
  143. }
  144. $data['updated_at'] = now();
  145. self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
  146. }
  147. }
  148. }
  149. /**
  150. * @description: 获取归集平台的接收地址
  151. * @return {*}
  152. */
  153. public static function getUsdtAddress()
  154. {
  155. $usdt_address = config('app.usdt_address');
  156. return $usdt_address;
  157. }
  158. /**
  159. * @description: 获取TRX能量账号秘钥
  160. * @return {*}
  161. */
  162. public static function getTrxPrivateKey()
  163. {
  164. $str = config('app.trx_private_key');
  165. return $str;
  166. }
  167. }