CollectService.php 5.7 KB

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