CollectService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Illuminate\Support\Facades\Log;
  9. use App\Helpers\TronHelper;
  10. use App\Services\WalletService;
  11. /**
  12. * 归集记录
  13. */
  14. class CollectService extends BaseService
  15. {
  16. public static string $MODEL = Collect::class;
  17. public static $THRESHOLD = 10; // 最小归集金额(USDT)
  18. /**
  19. * @description: 模型
  20. * @return {string}
  21. */
  22. public static function model() :string
  23. {
  24. return Collect::class;
  25. }
  26. /**
  27. * @description: 枚举
  28. * @return {*}
  29. */
  30. public static function enum() :string
  31. {
  32. return '';
  33. }
  34. /**
  35. * @description: 获取查询条件
  36. * @param {array} $search 查询内容
  37. * @return {array}
  38. */
  39. public static function getWhere(array $search = []) :array
  40. {
  41. $where = [];
  42. if(isset($search['coin']) && !empty($search['coin'])){
  43. $where[] = ['coin', '=', $search['coin']];
  44. }
  45. if(isset($search['net']) && !empty($search['net'])){
  46. $where[] = ['net', '=', $search['net']];
  47. }
  48. if(isset($search['to_address']) && !empty($search['to_address'])){
  49. $where[] = ['to_address', '=', $search['to_address']];
  50. }
  51. if(isset($search['from_address']) && !empty($search['from_address'])){
  52. $where[] = ['from_address', '=', $search['from_address']];
  53. }
  54. if(isset($search['id']) && !empty($search['id'])){
  55. $where[] = ['id', '=', $search['id']];
  56. }
  57. if(isset($search['txid']) && !empty($search['txid'])){
  58. $where[] = ['txid', '=', $search['txid']];
  59. }
  60. if(isset($search['status']) && $search['status'] != ''){
  61. $where[] = ['status', '=', $search['status']];
  62. }
  63. if (isset($search['amount']) && is_numeric($search['amount'])) {
  64. $where[] = ['amount', '>=', $search['amount']];
  65. }
  66. return $where;
  67. }
  68. /**
  69. * @description: 查询单条数据
  70. * @param array $search
  71. * @return \App\Models\Coin|null
  72. */
  73. public static function findOne(array $search): ?Collect
  74. {
  75. return self::model()::where(self::getWhere($search))->first();
  76. }
  77. /**
  78. * @description: 查询所有数据
  79. * @param array $search
  80. * @return \Illuminate\Database\Eloquent\Collection
  81. */
  82. public static function findAll(array $search = [])
  83. {
  84. return self::model()::where(self::getWhere($search))->get();
  85. }
  86. /**
  87. * @description: 分页查询
  88. * @param array $search
  89. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  90. */
  91. public static function paginate(array $search = [])
  92. {
  93. $limit = isset($search['limit'])?$search['limit']:15;
  94. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  95. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  96. }
  97. /**
  98. * @description: 生成归集记录
  99. * @param {*} $address
  100. * @param {*} $coin
  101. * @param {*} $net
  102. * @return {*}
  103. */
  104. public static function createCollect($address ,$coin ,$net)
  105. {
  106. $amount = TronHelper::getTrc20Balance($address); // 获取地址的余额
  107. $info = self::findOne(['from_address' => $address ,'status' => self::model()::STATUS_STAY]);
  108. if($amount >= 0 ){
  109. if(empty($info)){
  110. $data = [];
  111. $data['from_address'] = $address;
  112. $data['amount'] = $amount;
  113. $data['coin'] = $coin;
  114. $data['net'] = $net;
  115. self::model()::create($data);
  116. }else{
  117. $info->amount = $amount;
  118. $info->save();
  119. }
  120. }
  121. }
  122. /**
  123. * @description: 处理待归集的
  124. * @return {*}
  125. */
  126. public static function syncCollectStay()
  127. {
  128. $result = [
  129. 'threshold' => self::$THRESHOLD,
  130. 'to_address' => null,
  131. 'pending_count' => 0,
  132. 'handled_count' => 0,
  133. 'success_count' => 0,
  134. 'fail_count' => 0,
  135. 'items' => [],
  136. ];
  137. $to_address = self::getUsdtAddress(); // 转账的接收地址
  138. $trx_private_key = self::getTrxPrivateKey(); // 获取TRX能量的秘钥
  139. $result['to_address'] = $to_address;
  140. Log::info('syncCollectStay start', [
  141. 'threshold' => self::$THRESHOLD,
  142. 'to_address' => $to_address,
  143. 'has_trx_private_key' => !empty($trx_private_key),
  144. ]);
  145. if (!$to_address || !$trx_private_key) {
  146. $result['message'] = '归集配置不完整';
  147. Log::warning('syncCollectStay skipped: missing config', [
  148. 'to_address' => $to_address,
  149. 'has_trx_private_key' => !empty($trx_private_key),
  150. ]);
  151. return $result;
  152. }
  153. $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
  154. $result['pending_count'] = $list->count();
  155. if ($list->isEmpty()) {
  156. $result['message'] = '没有待归集记录';
  157. Log::info('syncCollectStay finished: no pending collects', $result);
  158. return $result;
  159. }
  160. foreach($list as $k => $v){
  161. $item = [
  162. 'id' => $v['id'],
  163. 'from_address' => $v['from_address'],
  164. 'amount' => $v['amount'],
  165. 'status' => 'pending',
  166. ];
  167. $data = [];
  168. $wallets = WalletService::findOne(['address' => $v['from_address']]);
  169. if (empty($wallets) || empty($wallets['private_key'])) {
  170. $item['status'] = 'wallet_not_found';
  171. $item['error'] = '未找到归集钱包私钥';
  172. $data['remark'] = $item['error'];
  173. $data['updated_at'] = now();
  174. self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
  175. $result['fail_count']++;
  176. $result['handled_count']++;
  177. $result['items'][] = $item;
  178. Log::warning('syncCollectStay wallet missing', $item);
  179. continue;
  180. }
  181. $privateKey = $wallets['private_key'];
  182. $trxBalance = TronHelper::getTrxBalance($v['from_address']);
  183. $item['trx_balance'] = $trxBalance;
  184. if($trxBalance < 10){
  185. $trxResult = TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
  186. $item['trx_topup_result'] = $trxResult;
  187. Log::info('syncCollectStay topup trx', [
  188. 'from_address' => $v['from_address'],
  189. 'trx_balance' => $trxBalance,
  190. 'result' => $trxResult,
  191. ]);
  192. }
  193. $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
  194. $item['transfer_result'] = $transferResult;
  195. $data['to_address'] = $to_address;
  196. if(is_array($transferResult) && !empty($transferResult['success'])){
  197. $data['txid'] = $transferResult['txid'] ?? '';
  198. $data['remark'] = 'success';
  199. $data['status'] = self::model()::STATUS_START;
  200. $item['status'] = 'success';
  201. $item['txid'] = $data['txid'];
  202. $result['success_count']++;
  203. Log::info('syncCollectStay transfer success', $item);
  204. }else{
  205. $error = is_array($transferResult)
  206. ? ($transferResult['error'] ?? 'USDT归集失败')
  207. : (is_string($transferResult) ? $transferResult : 'USDT归集失败');
  208. $data['remark'] = $error;
  209. $item['status'] = 'failed';
  210. $item['error'] = $error;
  211. $result['fail_count']++;
  212. Log::warning('syncCollectStay transfer failed', $item);
  213. }
  214. $data['updated_at'] = now();
  215. self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
  216. $result['handled_count']++;
  217. $result['items'][] = $item;
  218. }
  219. Log::info('syncCollectStay finished', $result);
  220. return $result;
  221. }
  222. /**
  223. * @description: 获取归集平台的接收地址
  224. * @return {*}
  225. */
  226. public static function getUsdtAddress()
  227. {
  228. $usdt_address = config('app.usdt_address');
  229. return $usdt_address;
  230. }
  231. /**
  232. * @description: 获取TRX能量账号秘钥
  233. * @return {*}
  234. */
  235. public static function getTrxPrivateKey()
  236. {
  237. $str = config('app.trx_private_key');
  238. return $str;
  239. }
  240. }