CollectService.php 5.6 KB

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