| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace App\Services;
- use App\Services\BaseService;
- use App\Models\Collect;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use App\Helpers\TronHelper;
- use App\Services\WalletService;
- /**
- * 归集记录
- */
- class CollectService extends BaseService
- {
- public static string $MODEL = Collect::class;
- public static $THRESHOLD = 10; // 最小归集金额(USDT)
- /**
- * @description: 模型
- * @return {string}
- */
- public static function model() :string
- {
- return Collect::class;
- }
- /**
- * @description: 枚举
- * @return {*}
- */
- public static function enum() :string
- {
- return '';
- }
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []) :array
- {
- $where = [];
- if(isset($search['coin']) && !empty($search['coin'])){
- $where[] = ['coin', '=', $search['coin']];
- }
- if(isset($search['net']) && !empty($search['net'])){
- $where[] = ['net', '=', $search['net']];
- }
- if(isset($search['to_address']) && !empty($search['to_address'])){
- $where[] = ['to_address', '=', $search['to_address']];
- }
- if(isset($search['from_address']) && !empty($search['from_address'])){
- $where[] = ['from_address', '=', $search['from_address']];
- }
- if(isset($search['id']) && !empty($search['id'])){
- $where[] = ['id', '=', $search['id']];
- }
- if(isset($search['txid']) && !empty($search['txid'])){
- $where[] = ['txid', '=', $search['txid']];
- }
- if(isset($search['status']) && $search['status'] != ''){
- $where[] = ['status', '=', $search['status']];
- }
- if (isset($search['amount']) && is_numeric($search['amount'])) {
- $where[] = ['amount', '>=', $search['amount']];
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return \App\Models\Coin|null
- */
- public static function findOne(array $search): ?Collect
- {
- return self::model()::where(self::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function findAll(array $search = [])
- {
- return self::model()::where(self::getWhere($search))->get();
- }
- /**
- * @description: 分页查询
- * @param array $search
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit'])?$search['limit']:15;
- $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
-
- /**
- * @description: 生成归集记录
- * @param {*} $address
- * @param {*} $coin
- * @param {*} $net
- * @return {*}
- */
- public static function createCollect($address ,$coin ,$net)
- {
- $amount = TronHelper::getTrc20Balance($address); // 获取地址的余额
- $info = self::findOne(['from_address' => $address ,'status' => self::model()::STATUS_STAY]);
-
- if($amount >= 0 ){
- if(empty($info)){
- $data = [];
- $data['from_address'] = $address;
- $data['amount'] = $amount;
- $data['coin'] = $coin;
- $data['net'] = $net;
- self::model()::create($data);
- }else{
- $info->amount = $amount;
- $info->save();
- }
- }
- }
- /**
- * @description: 处理待归集的
- * @return {*}
- */
- public static function syncCollectStay()
- {
- $result = [
- 'threshold' => self::$THRESHOLD,
- 'to_address' => null,
- 'pending_count' => 0,
- 'handled_count' => 0,
- 'success_count' => 0,
- 'fail_count' => 0,
- 'items' => [],
- ];
- $to_address = self::getUsdtAddress(); // 转账的接收地址
- $trx_private_key = self::getTrxPrivateKey(); // 获取TRX能量的秘钥
- $result['to_address'] = $to_address;
- Log::info('syncCollectStay start', [
- 'threshold' => self::$THRESHOLD,
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- if (!$to_address || !$trx_private_key) {
- $result['message'] = '归集配置不完整';
- Log::warning('syncCollectStay skipped: missing config', [
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- return $result;
- }
- $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
- $result['pending_count'] = $list->count();
- if ($list->isEmpty()) {
- $result['message'] = '没有待归集记录';
- Log::info('syncCollectStay finished: no pending collects', $result);
- return $result;
- }
- foreach($list as $k => $v){
- $item = [
- 'id' => $v['id'],
- 'from_address' => $v['from_address'],
- 'amount' => $v['amount'],
- 'status' => 'pending',
- ];
- $data = [];
- $wallets = WalletService::findOne(['address' => $v['from_address']]);
- if (empty($wallets) || empty($wallets['private_key'])) {
- $item['status'] = 'wallet_not_found';
- $item['error'] = '未找到归集钱包私钥';
- $data['remark'] = $item['error'];
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['fail_count']++;
- $result['handled_count']++;
- $result['items'][] = $item;
- Log::warning('syncCollectStay wallet missing', $item);
- continue;
- }
- $privateKey = $wallets['private_key'];
- $trxBalance = TronHelper::getTrxBalance($v['from_address']);
- $item['trx_balance'] = $trxBalance;
- if($trxBalance < 10){
- $trxResult = TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
- $item['trx_topup_result'] = $trxResult;
- Log::info('syncCollectStay topup trx', [
- 'from_address' => $v['from_address'],
- 'trx_balance' => $trxBalance,
- 'result' => $trxResult,
- ]);
- }
- $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
- $item['transfer_result'] = $transferResult;
- $data['to_address'] = $to_address;
- if(is_array($transferResult) && !empty($transferResult['success'])){
- $data['txid'] = $transferResult['txid'] ?? '';
- $data['remark'] = 'success';
- $data['status'] = self::model()::STATUS_START;
- $item['status'] = 'success';
- $item['txid'] = $data['txid'];
- $result['success_count']++;
- Log::info('syncCollectStay transfer success', $item);
- }else{
- $error = is_array($transferResult)
- ? ($transferResult['error'] ?? 'USDT归集失败')
- : (is_string($transferResult) ? $transferResult : 'USDT归集失败');
- $data['remark'] = $error;
- $item['status'] = 'failed';
- $item['error'] = $error;
- $result['fail_count']++;
- Log::warning('syncCollectStay transfer failed', $item);
- }
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['handled_count']++;
- $result['items'][] = $item;
- }
- Log::info('syncCollectStay finished', $result);
- return $result;
- }
- /**
- * @description: 获取归集平台的接收地址
- * @return {*}
- */
- public static function getUsdtAddress()
- {
- $usdt_address = config('app.usdt_address');
- return $usdt_address;
- }
- /**
- * @description: 获取TRX能量账号秘钥
- * @return {*}
- */
- public static function getTrxPrivateKey()
- {
- $str = config('app.trx_private_key');
- return $str;
- }
-
- }
|