WalletService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\AccountLogModel;
  13. use App\Models\BalanceLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\WalletLogModel;
  16. use App\Models\WalletModel;
  17. use App\Services\Api\FinanceService;
  18. use App\Services\Api\PledgeOrderService;
  19. use Illuminate\Support\Facades\DB;
  20. use Tighten\SolanaPhpSdk\Connection;
  21. use Tighten\SolanaPhpSdk\PublicKey;
  22. use Tighten\SolanaPhpSdk\SolanaRpcClient;
  23. /**
  24. * 钱包管理-服务类
  25. * @package App\Services
  26. */
  27. class WalletService extends BaseService
  28. {
  29. protected $apiUrl = '';
  30. protected $bianUrl = 'http://p2p.binance.com/bapi/c2c/v2/public/c2c/adv/quoted-price';
  31. protected $ouyiApi = 'https://www.okx.com/priapi/v3/b2c/deposit/quotedPrice';
  32. protected $config = [];
  33. protected $tokenAddress = 'So11111111111111111111111111111111111111111'; // solana
  34. protected $usdtTokenAddress = 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'; // solana-usdt
  35. // 静态对象
  36. protected static $instance = null;
  37. protected $apiUrls = [
  38. // 代币账号
  39. 'tokenAccount' => '/v2.0/account/token-accounts',
  40. // 账户交易记录
  41. 'accountTransfer' => '/v2.0/account/transfer?address=%s&activity_type[]=%s&block_time[]=%s&page=%s&page_size=%s',
  42. // 代币账户
  43. 'tokenAccounts'=>'/v2.0/account/token-accounts?address=%s&type=%s&hide_zero=true'
  44. ];
  45. /**
  46. * 构造函数
  47. */
  48. public function __construct()
  49. {
  50. $this->config = ConfigService::make()->getConfigOptionByGroup(14);
  51. if (empty($this->config)) {
  52. return false;
  53. }
  54. }
  55. /**
  56. * 静态入口
  57. * @return static|null
  58. */
  59. public static function make()
  60. {
  61. if (!self::$instance) {
  62. self::$instance = (new static());
  63. }
  64. return self::$instance;
  65. }
  66. /**
  67. * 获取平台钱包地址
  68. * @param int $type 钱包类型:1-充值入账,2-提现出账,默认充值
  69. * @param int $amount 提币时,提币金额验证,充值钱包可忽略
  70. * @param int $coinType 币种:1-USDT
  71. * @param boolean $privateKey 是否返回解码的私钥
  72. * @return array|false|mixed、
  73. */
  74. public function getWallet($type = 1, $amount = 0, $coinType = 1, $privateKey = false)
  75. {
  76. $cacheKey = "caches:wallet:platform:{$type}";
  77. $wallets = RedisService::get($cacheKey);
  78. if (empty($wallets)) {
  79. $wallets = WalletModel::where(['type' => $type, 'status' => 1, 'mark' => 1])->select(['address', 'private_key'])->get();
  80. $wallets = $wallets ? $wallets->toArray() : [];
  81. if ($wallets) {
  82. RedisService::set($cacheKey, $wallets, rand(300, 600));
  83. }
  84. }
  85. if (empty($wallets)) {
  86. $this->error = $type == 1 ? 1036 : 1037;
  87. return false;
  88. }
  89. // 需要验证余额
  90. if ($amount > 0) {
  91. foreach ($wallets as $item) {
  92. $address = isset($item['address']) ? trim($item['address']) : '';
  93. $privateKey = isset($item['private_key']) ? trim($item['private_key']) : '';
  94. $privateKey = $this->getAddressPrivateKey($privateKey, $address, 2);
  95. if ($coinType == 1) {
  96. $accountData = $this->getUsdtBalance($address);
  97. $balance = isset($accountData['amount'])? $accountData['amount'] : 0;
  98. } else {
  99. $accountData = $this->getUsdtBalance($address);
  100. $balance = isset($accountData['amount'])? $accountData['amount'] : 0;
  101. }
  102. if ($balance >= $amount) {
  103. $tokenAccount = isset($accountData['tokenAccount'])? $accountData['tokenAccount'] : '';
  104. return ['address' => $address,'tokenAccount'=>$tokenAccount, 'private_key' => $privateKey];
  105. }
  106. }
  107. $this->error = 1038;
  108. return false;
  109. } else {
  110. $rand = rand(0, count($wallets) - 1);
  111. $wallet = $wallets[$rand];
  112. if ($privateKey) {
  113. $wallet['private_key'] = $this->getAddressPrivateKey($wallet['private_key'], $wallet['address'], 2);
  114. }
  115. return $wallet;
  116. }
  117. }
  118. /**
  119. * 获取所有钱包地址
  120. * @return array|mixed
  121. */
  122. public function getWalletList()
  123. {
  124. try {
  125. $cacheKey = "caches:wallet:platform:all";
  126. $wallets = RedisService::get($cacheKey);
  127. if ($wallets) {
  128. return $wallets;
  129. }
  130. $wallets = WalletModel::where(['status' => 1, 'mark' => 1])->select(['id', 'address', 'type'])->get();
  131. $wallets = $wallets ? $wallets->toArray() : [];
  132. if ($wallets) {
  133. RedisService::set($cacheKey, $wallets, rand(300, 600));
  134. }
  135. return $wallets;
  136. } catch (\Exception $exception){
  137. $this->error = $exception->getMessage();
  138. return false;
  139. }
  140. }
  141. /**
  142. * 加解密密钥
  143. * @param $key 密钥或加密密钥
  144. * @param $address 地址
  145. * @param $type 类型:1-加密,2-解密
  146. * @return mixed|string|string[]
  147. */
  148. public function getAddressPrivateKey($key, $address, $type)
  149. {
  150. // 加密
  151. if ($type == 1) {
  152. $baseStr = base64_encode($key);
  153. $baseStr = str_replace(['==', '='], ['-2', '-1'], $baseStr);
  154. $str = substr($baseStr, -6, 7) . substr(md5($address), 2, 6) . substr($baseStr, 0, -6);
  155. return $str;
  156. } // 解密
  157. else {
  158. $str1 = substr($key, 12) . substr($key, 0, 6);
  159. $str1 = str_replace(['-1', '-2'], ['=', '=='], $str1);
  160. return base64_decode($str1);
  161. }
  162. }
  163. /**
  164. * 验证地址
  165. * @param $address
  166. * @return string
  167. */
  168. public function checkAddress($address)
  169. {
  170. try{
  171. $publicKey = new PublicKey($address);
  172. return PublicKey::isOnCurve($publicKey);
  173. } catch (\Exception $exception){
  174. $this->error = $exception->getMessage();
  175. return false;
  176. }
  177. }
  178. /**
  179. * solana余额
  180. * @param $address
  181. * @return false|float|string
  182. */
  183. public function getBalance($address, $cache = false)
  184. {
  185. if (empty($address)) {
  186. $this->error = '1018';
  187. return false;
  188. }
  189. $cacheKey = "caches:wallet:balance:sol_{$address}";
  190. if ($data = RedisService::get($cacheKey) && $cache) {
  191. return $data;
  192. }
  193. if (RedisService::get($cacheKey . '_lock') && $cache) {
  194. return false;
  195. }
  196. try {
  197. $sdk = new Connection(new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT));
  198. $publicKey = new PublicKey($address);
  199. $balance = $sdk->getBalance($publicKey);
  200. RedisService::set($cacheKey, $balance, rand(3, 5));
  201. RedisService::set($cacheKey . '_lock', true, rand(3, 5));
  202. return $balance;
  203. } catch (\Exception $exception) {
  204. $this->error = $exception->getMessage();
  205. return false;
  206. }
  207. }
  208. /**
  209. * solana-USDT余额
  210. * @param $address
  211. * @return false|float|string
  212. */
  213. public function getUsdtBalance($address, $cache = false)
  214. {
  215. if (empty($address)) {
  216. $this->error = 1018;
  217. return false;
  218. }
  219. $cacheKey = "caches:wallet:balance:sol_usdt_{$address}";
  220. if ($data = RedisService::get($cacheKey) && $cache) {
  221. return $data;
  222. }
  223. if (RedisService::get($cacheKey . '_lock') && $cache) {
  224. return false;
  225. }
  226. try {
  227. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  228. if (empty($apiUrl)) {
  229. $this->error = 1040;
  230. return false;
  231. }
  232. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  233. if (empty($token)) {
  234. $this->error = 1039;
  235. return false;
  236. }
  237. $headers = [
  238. "token: {$token}",
  239. ];
  240. $url = $apiUrl . sprintf($this->apiUrls['tokenAccounts'],$address,'token');
  241. $result = curl_get($url, '', $headers, 20);
  242. $data = $result ? json_decode($result, true) : [];
  243. RedisService::set($cacheKey.'_result', ['url'=>$url,'data'=>$data,'result'=>$result,'headers'=>$headers], 600);
  244. $accounts = isset($data['data']) ? $data['data'] : [];
  245. if(empty($accounts)){
  246. $this->error = 2208;
  247. return false;
  248. }
  249. $balance = 0;
  250. $tokenAccount = '';
  251. foreach ($accounts as $item){
  252. if($item['token_address'] = $this->usdtTokenAddress){
  253. $tokenAccount = $item['token_account'];
  254. $balance = moneyFormat($item['amount']/pow(10, $item['token_decimals']), 2);
  255. }
  256. }
  257. RedisService::set($cacheKey, ['amount'=> $balance,'tokenAccount'=>$tokenAccount], rand(3, 5));
  258. RedisService::set($cacheKey . '_lock', true, rand(3, 5));
  259. return ['amount'=> $balance,'tokenAccount'=>$tokenAccount];
  260. } catch (\Exception $exception) {
  261. $this->error = $exception->getMessage();
  262. return false;
  263. }
  264. }
  265. /**
  266. * solana usdt 转账
  267. * @param $to 进账账户
  268. * @param $amount 转账金额
  269. */
  270. public function usdtTransfer($to, $amount, $from = '', $fromPrivateKey = '')
  271. {
  272. if ($amount <= 0) {
  273. $this->error = '2205';
  274. return false;
  275. }
  276. }
  277. /**
  278. * 监听充值存币处理
  279. * @param $address 钱包地址
  280. * @param int $accountType 账户类型:1-USDT充值,2-USDT提现
  281. * @param int $page 分页:默认0-轮询分页20页,每页100条记录
  282. * @return array|false
  283. */
  284. public function listenWallet($address, $accountType = 1, $page = 0)
  285. {
  286. // 获取钱包参数
  287. try {
  288. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  289. if (empty($apiUrl)) {
  290. $this->error = 1040;
  291. return false;
  292. }
  293. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  294. if (empty($token)) {
  295. $this->error = 1039;
  296. return false;
  297. }
  298. $headers = [
  299. "token: {$token}",
  300. ];
  301. if ($page <= 0) {
  302. $page = RedisService::get('caches:wallet:listen_recharge_page_' . $address);
  303. $maxPage = ConfigService::make()->getConfigByCode('wallet_listen_max_page', 20);
  304. $page = $page > 0 && $page <= $maxPage ? $page : 1;
  305. }
  306. $limit = ConfigService::make()->getConfigByCode('wallet_listen_limit', 300);
  307. $limit = $limit > 10 && $limit <= 100 ? $limit : 100;
  308. $time = ConfigService::make()->getConfigByCode('wallet_listen_time', 48);
  309. $startTime = $time > 0 ? time() - $time * 3600 : time() - 7200;
  310. $url = $apiUrl . sprintf($this->apiUrls['accountTransfer'], $address, 'ACTIVITY_SPL_TRANSFER', $startTime, $page, $limit);
  311. $result = curl_get($url, [], $headers, 5);
  312. $result = $result ? json_decode($result, true) : [];
  313. $datas = isset($result['data']) && is_array($result['data']) ? $result['data'] : [];
  314. $status = isset($result['success']) ? $result['success'] : '';
  315. $error = isset($result['error_message']) ? $result['error_message'] : '';
  316. RedisService::set("caches:wallet:listen_{$address}", ['url' => $url, 'page' => $page, 'result' => $result], 7200);
  317. if ($status != true || empty($datas)) {
  318. $this->error = $error ? '第{$page}页接口错误-' . $error : "第{$page}页没有取到数据";
  319. return false;
  320. }
  321. RedisService::set('caches:wallet:listen_recharge_page', $page + 1, rand(3600, 7200));
  322. $coinInMin = ConfigService::make()->getConfigByCode('recharge_min_money');
  323. $coinInMin = $coinInMin > 0 ? $coinInMin : 0;
  324. $cacheKey = "caches:wallet:listen:{$address}_{$accountType}:";
  325. $dateTime = date('Y-m-d H:i:s');
  326. $success = 0;
  327. if ($datas) {
  328. foreach ($datas as $v) {
  329. $amount = isset($v['amount']) ? intval($v['amount']) : 0;
  330. $tokenDecimals = isset($v['token_decimals']) && $v['token_decimals'] ? intval($v['token_decimals']) : 6;
  331. $amount = moneyFormat($amount / pow(10, $tokenDecimals), 6);
  332. $time = isset($v['block_time']) ? intval($v['block_time']) : 0;
  333. $txid = isset($v['trans_id']) ? $v['trans_id'] : '';
  334. $ownerAddress = isset($v['from_address']) ? $v['from_address'] : '';
  335. $toAddress = isset($v['to_address']) ? $v['to_address'] : '';
  336. $tokenAddress = isset($v['token_address']) ? $v['token_address'] : '';
  337. $coinType = 0;
  338. $coinName = '';
  339. if ($tokenAddress == $this->tokenAddress) {
  340. $coinType = 2;
  341. $coinName = 'Sol';
  342. } else if ($tokenAddress == $this->usdtTokenAddress) {
  343. $coinType = 1;
  344. $coinName = 'USDT';
  345. }
  346. if($amount<=0){
  347. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  348. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  349. continue;
  350. }
  351. // 处理交易记录
  352. if (!WalletLogModel::checkExists($txid)) {
  353. $log = [
  354. 'owner_address' => $ownerAddress,
  355. 'to_address' => $toAddress,
  356. 'token_address' => $tokenAddress,
  357. 'token_type' => $coinType,
  358. 'hash' => $txid,
  359. 'amount' => $amount,
  360. 'create_time' => $time ? $time : time(),
  361. 'update_time' => time(),
  362. 'status' => 1,
  363. 'mark' => 1,
  364. ];
  365. WalletLogModel::insert($log);
  366. }
  367. // 充值处理(solana-usdt)
  368. if ($toAddress == $address && $ownerAddress && $accountType != 2) {
  369. if ($amount < $coinInMin) {
  370. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  371. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  372. continue;
  373. }
  374. if ($tokenAddress != $this->usdtTokenAddress) {
  375. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}币种不匹配\n";
  376. RedisService::set($cacheKey . "{$txid}:error", ['error' => '币种不匹配', 'data' => $v], 7200);
  377. continue;
  378. }
  379. if (BalanceLogModel::checkExists($txid)) {
  380. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}交易已处理过\n";
  381. RedisService::set($cacheKey . "{$txid}:error", ['error' => '交易已处理过', 'data' => $v], 7200);
  382. continue;
  383. }
  384. $memberInfo = MemberModel::where(['wallet_url' => $ownerAddress, 'mark' => 1])
  385. ->select(['id', 'nickname', 'wallet_url', 'usdt', 'sbt'])
  386. ->orderBy('id', 'asc')
  387. ->first();
  388. $trcUrl = isset($memberInfo['wallet_url']) ? trim($memberInfo['wallet_url']) : '';
  389. $usdt = isset($memberInfo['usdt']) ? floatval($memberInfo['usdt']) : 0;
  390. $userId = isset($memberInfo['id']) ? intval($memberInfo['id']) : 0;
  391. if (empty($memberInfo)) {
  392. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}未绑定用户\n";
  393. RedisService::set($cacheKey . "{$txid}:error", ['error' => '未绑定用户', 'data' => $v], 7200);
  394. continue;
  395. }
  396. // 入账
  397. try {
  398. DB::beginTransaction();
  399. $updateData = ['update_time' => time()];
  400. $total = $amount;
  401. $updateData['usdt'] = DB::raw("usdt + {$total}");
  402. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  403. DB::rollBack();
  404. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]入账处理失败\n";
  405. RedisService::set($cacheKey . "{$txid}:error", ['error' => '入账处理失败', 'update' => $updateData, 'member' => $memberInfo, 'data' => $v], 7200);
  406. continue;
  407. }
  408. // 充值明细
  409. $data = [
  410. 'user_id' => $userId,
  411. 'order_no' => get_order_num('SR'),
  412. 'type' => 1,
  413. 'coin_type' => 1,
  414. 'money' => $amount,
  415. 'actual_money' => $total,
  416. 'pay_type' => 20,
  417. 'pay_status' => 20,
  418. 'pay_at' => date('Y-m-d H:i:s', $time),
  419. 'hash' => $txid,
  420. 'wallet_url' => $trcUrl,
  421. 'pt_wallet_url' => $address,
  422. 'date' => date('Y-m-d'),
  423. 'create_time' => time(),
  424. 'update_time' => time(),
  425. 'status' => 2,
  426. 'mark' => 1,
  427. ];
  428. if (!BalanceLogModel::insert($data)) {
  429. DB::rollBack();
  430. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值明细处理失败\n";
  431. RedisService::set($cacheKey . "{$txid}:error", ['error' => '充值明细处理失败', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  432. continue;
  433. }
  434. // 用户账户明细
  435. $log = [
  436. 'user_id' => $userId,
  437. 'order_no' => $data['order_no'],
  438. 'user_type' => 1,
  439. 'type' => 3,
  440. 'coin_type' => 1,
  441. 'hash' => $txid,
  442. 'money' => $amount,
  443. 'before_money' => $usdt,
  444. 'create_time' => time(),
  445. 'update_time' => date('Y-m-d H:i:s'),
  446. 'remark' => 'USDT充值',
  447. 'status' => 1,
  448. 'mark' => 1,
  449. ];
  450. if (!AccountLogModel::insert($log)) {
  451. DB::rollBack();
  452. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]账户明细处理失败\n";
  453. RedisService::set($cacheKey . "{$txid}:error", ['error' => '账户明细处理失败', 'log' => $log, 'member' => $memberInfo, 'data' => $v], 7200);
  454. continue;
  455. }
  456. // 平台进账
  457. FinanceService::make()->saveLog(0, $amount, 1);
  458. DB::commit();
  459. $amount = moneyFormat($amount, 2);
  460. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  461. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值成功\n";
  462. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  463. RedisService::keyDel("caches:balanceLog:total*");
  464. $success++;
  465. } catch (\Exception $exception) {
  466. DB::rollBack();
  467. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  468. $this->error = "处理错误-" . $exception->getMessage();
  469. echo "处理错误-" . $exception->getMessage();
  470. continue;
  471. }
  472. } // 提现处理
  473. else if ($ownerAddress == $address && $toAddress && $accountType == 2) {
  474. // 处理锁跳过
  475. if (RedisService::get($cacheKey . "{$txid}:lock")) {
  476. echo "【{$dateTime} withdraw】提现记录[{$txid}]金额[{$amount}]间隔时间不处理\n";
  477. RedisService::set($cacheKey . "{$txid}:error", ['error' => '间隔时间内不处理', 'data' => $v], 600);
  478. continue;
  479. }
  480. $info = BalanceLogModel::checkExists($txid);
  481. $logId = isset($info['id']) ? $info['id'] : 0;
  482. $userId = isset($info['user_id']) ? $info['user_id'] : 0;
  483. $money = isset($info['money']) ? $info['money'] : 0;
  484. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  485. $payStatus = isset($info['pay_status']) ? $info['pay_status'] : 0;
  486. $status = isset($info['status']) ? $info['status'] : 0;
  487. $trcUrl = isset($info['wallet_url']) ? trim($info['wallet_url']) : '';
  488. $walletUrl = isset($info['pt_wallet_url']) ? trim($info['pt_wallet_url']) : '';
  489. if (empty($info) || $logId <= 0 || $userId <= 0) {
  490. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录不存在\n";
  491. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录不存在', 'data' => $v], 7200);
  492. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录不存在', 'data' => $v], rand(30, 60));
  493. continue;
  494. }
  495. if ($status != 2) {
  496. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录未审核\n";
  497. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录未审核', 'data' => $v], 7200);
  498. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录未审核', 'data' => $v], rand(30, 60));
  499. continue;
  500. }
  501. if ($payStatus == 20) {
  502. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录已打款\n";
  503. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录已打款', 'data' => $v], 7200);
  504. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录已打款', 'data' => $v], rand(30, 60));
  505. continue;
  506. }
  507. if ($trcUrl != $toAddress || $walletUrl != $ownerAddress) {
  508. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现订单交易地址不匹配\n";
  509. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现订单交易地址不匹配', 'info' => $info, 'data' => $v], 7200);
  510. continue;
  511. }
  512. // 金额验证
  513. if (abs($actualMoney - $amount) > 1) {
  514. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现金额与交易金额不匹配\n";
  515. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现金额与交易金额不匹配', 'info' => $info, 'data' => $v], 7200);
  516. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现金额与交易金额不匹配', 'data' => $v], rand(30, 60));
  517. continue;
  518. }
  519. // 更新状态
  520. try {
  521. DB::beginTransaction();
  522. if (!BalanceLogModel::where(['id' => $logId])->update(['pay_status' => 20, 'pay_at' => $dateTime, 'update_time' => time()])) {
  523. DB::rollBack();
  524. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现状态更新失败\n";
  525. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现状态更新失败', 'info' => $info, 'data' => $v], 7200);
  526. continue;
  527. }
  528. // 平台出账
  529. FinanceService::make()->saveLog(0, $amount, 2);
  530. DB::commit();
  531. $amount = moneyFormat($amount, 2);
  532. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $log, 'info' => $info, 'data' => $v], 7200);
  533. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现成功\n";
  534. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  535. $success++;
  536. } catch (\Exception $exception) {
  537. DB::rollBack();
  538. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  539. $this->error = "处理错误-" . $exception->getMessage();
  540. echo "处理错误-" . $exception->getMessage();
  541. continue;
  542. }
  543. }
  544. }
  545. }
  546. $this->error = 1010;
  547. return ['success' => $success, 'total' => count($datas)];
  548. } catch (\Exception $exception) {
  549. $message = $exception->getMessage();
  550. $this->error = $message;
  551. RedisService::set("caches:wallet:listen_error_{$address}", ['error' => $exception->getMessage(), 'trace' => $exception->getTrace()], 600);
  552. return false;
  553. }
  554. }
  555. }