UsdtWalletService.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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\CapitalLogModel;
  13. use App\Models\CoinLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\TradeOrderModel;
  16. use App\Services\Api\MemberService;
  17. use App\Services\Common\CoinLogService;
  18. use App\Services\Oapi\TradeOrderService;
  19. use BitWasp\Bitcoin\Address\AddressCreator;
  20. use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
  21. use BitWasp\Bitcoin\Bitcoin;
  22. use BitWasp\Bitcoin\Crypto\Random\Random;
  23. use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
  24. use BitWasp\Bitcoin\Script\WitnessProgram;
  25. use etherscan\api\Etherscan;
  26. use GuzzleHttp\Client;
  27. use IEXBase\TronAPI\Tron;
  28. use Tron\Api;
  29. use Tron\TRC20;
  30. use Tron\TRX;
  31. use Web3p\EthereumTx\Transaction;
  32. use Web3p\EthereumUtil\Util;
  33. /**
  34. * USDT链管理-服务类
  35. * Class UsdtWalletService
  36. * @package App\Services
  37. */
  38. class UsdtWalletService extends BaseService
  39. {
  40. protected $apiUrl = '';
  41. protected $config = [];
  42. // 静态对象
  43. protected static $instance = null;
  44. protected $apiUrls = [
  45. 'usdt_trx2_transfer_log' => '/v1/accounts/%s/transactions/trc20?limit=%s&only_to=%s&only_from=%s&only_confirmed=true&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
  46. 'usdt_trx2_transfer_logs' => '/v1/accounts/%s/transactions/trc20?limit=%s&only_to=%s&only_from=%s&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
  47. 'erc_balance' => '/api?module=account&action=balance&address=%s&tag=latest&apikey=%s',
  48. 'erc20_balance' => '/api?module=account&action=tokenbalance&contractaddress=%s&address=%s&tag=latest&apikey=%s',
  49. 'erc_transaction' => '/api?module=proxy&action=eth_sendRawTransaction&hex=%s&apikey=%s',
  50. 'erc_transaction_log' => '/api?module=account&action=txlist&address=%s&page=%s&offset=%s&sort=desc&apikey=%s',
  51. 'erc20_transaction_log' => '/api?module=account&action=tokentx&address=%s&page=%s&offset=%s&sort=desc&apikey=%s',
  52. ];
  53. /**
  54. * 构造函数
  55. * UsdtWalletService constructor.
  56. */
  57. public function __construct()
  58. {
  59. $this->memberModel = new MemberModel();
  60. $this->coinModel = new CoinLogModel();
  61. $this->capitalModel = new CapitalLogModel();
  62. $this->config = ConfigService::make()->getConfigOptionByGroup(4);
  63. if (empty($this->config)) {
  64. return false;
  65. }
  66. }
  67. /**
  68. * 静态入口
  69. * @return static|null
  70. */
  71. public static function make()
  72. {
  73. if (!self::$instance) {
  74. self::$instance = (new UsdtWalletService());
  75. }
  76. return self::$instance;
  77. }
  78. /**
  79. * 获取TRC2.0钱包地址
  80. * @throws \Tron\Exceptions\TronErrorException
  81. */
  82. public function getTrxAddress()
  83. {
  84. try {
  85. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  86. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], $headers]));
  87. $trxWallet = new TRX($api);
  88. $addressData = $trxWallet->generateAddress();
  89. $addressData = (array)$addressData;
  90. return ['wif' => $addressData['privateKey'], 'hexAddress' => $addressData['hexAddress'], 'address' => $addressData['address']];
  91. } catch (\Exception $exception) {
  92. $this->error = $exception->getMessage();
  93. return false;
  94. }
  95. }
  96. /**
  97. * trx 转账
  98. * @param $to 进账账户
  99. * @param $amount 转账金额
  100. * @throws \Tron\Exceptions\TransactionException
  101. * @throws \Tron\Exceptions\TronErrorException
  102. */
  103. public function trxTransfer($to, $amount, $from = '')
  104. {
  105. if ($amount <= 0) {
  106. $this->error = '2205';
  107. return false;
  108. }
  109. if (empty($this->config['tron_api_url'])) {
  110. $this->error = '2206';
  111. return false;
  112. }
  113. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  114. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
  115. $trxWallet = new TRX($api);
  116. // 获取钱包参数
  117. try {
  118. $otcAddress = ConfigService::make()->getConfigByCode('trc_out_address');
  119. $otcAddressPrivate = ConfigService::make()->getConfigByCode('trc_out_private_key');
  120. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  121. $this->error = '2203';
  122. return false;
  123. }
  124. $tron = new Tron();
  125. // 获取平台钱包hex
  126. $tron->setAddress($otcAddress);
  127. $otcAddress = $tron->getAddress();
  128. $tron->setAddress($to);
  129. $toAddress = $tron->getAddress();
  130. $from = new \Tron\Address($otcAddress['base58'], $otcAddressPrivate, $otcAddress['hex']);
  131. $to = new \Tron\Address($toAddress['base58'], '', $toAddress['hex']);
  132. $result = $trxWallet->transfer($from, $to, $amount);
  133. return $result;
  134. } catch (\Exception $exception) {
  135. $message = $exception->getMessage();
  136. $this->error = $message;
  137. return false;
  138. }
  139. }
  140. /**
  141. * usdt-trc2.0 转账
  142. * @param $to 进账账户
  143. * @param $amount 转账金额
  144. * @param $from 转账账户
  145. * @param $fromPrivate 转账账户私钥
  146. * @throws \Tron\Exceptions\TransactionException
  147. * @throws \Tron\Exceptions\TronErrorException
  148. */
  149. public function usdtTrcTransfer($to, $amount, $from = '', $fromPrivate = '')
  150. {
  151. if ($amount <= 0) {
  152. $this->error = '2205';
  153. return false;
  154. }
  155. if (empty($this->config['tron_api_url'])) {
  156. $this->error = '2206';
  157. return false;
  158. }
  159. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  160. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
  161. $trxWallet = new TRC20($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
  162. // 获取钱包参数
  163. try {
  164. // 用出账钱包转账
  165. $otcAddress = $from ? $from : ConfigService::make()->getConfigByCode('trc_out_address');
  166. $otcAddressPrivate = $from ? $fromPrivate : ConfigService::make()->getConfigByCode('trc_out_private_key');
  167. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  168. $this->error = '2203';
  169. return false;
  170. }
  171. $tron = new Tron();
  172. // 获取平台钱包hex
  173. $tron->setAddress($otcAddress);
  174. $otcAddress = $tron->getAddress();
  175. // 获取收款钱包hex
  176. $tron->setAddress($to);
  177. $toAddress = $tron->getAddress();
  178. RedisService::set("caches:wallet:transfer:temp_{$to}", ['to' => $to, 'out' => $otcAddress, 'outPrivate' => $otcAddressPrivate, 'amount' => $amount], 7200);
  179. $from = new \Tron\Address($otcAddress['base58'], $otcAddressPrivate, $otcAddress['hex']);
  180. $to = new \Tron\Address($toAddress['base58'], '', $toAddress['hex']);
  181. $result = $trxWallet->transfer($from, $to, $amount);
  182. $result = (array)$result;
  183. if (isset($result['txID'])) {
  184. return $result;
  185. }
  186. return false;
  187. } catch (\Exception $exception) {
  188. $message = $exception->getMessage();
  189. $this->error = $message;
  190. RedisService::set("caches:wallet:transfer:error_{$toAddress['base58']}", $exception, 600);
  191. return false;
  192. }
  193. }
  194. /**
  195. * usdt-trc2.0 归集
  196. * @throws \Tron\Exceptions\TransactionException
  197. * @throws \Tron\Exceptions\TronErrorException
  198. */
  199. public function usdtTrcTrigger($force = false)
  200. {
  201. if (empty($this->config['tron_api_url'])) {
  202. $this->error = '2206';
  203. return false;
  204. }
  205. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  206. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
  207. $trcWallet = new TRC20($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
  208. $trxWallet = new TRX($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
  209. // 获取钱包参数
  210. try {
  211. // 用收账钱包归集
  212. $otcAddress = ConfigService::make()->getConfigByCode('trc_address');
  213. $otcAddressPrivate = ConfigService::make()->getConfigByCode('trc_private_key');
  214. // 出账手续费钱包
  215. $otcOutAddress = ConfigService::make()->getConfigByCode('trc_out_address');
  216. $otcOutAddressPrivate = ConfigService::make()->getConfigByCode('trc_out_private_key');
  217. $triggerMin = ConfigService::make()->getConfigByCode('trade_trigger_min');
  218. $triggerTime = ConfigService::make()->getConfigByCode('trade_trigger_time');
  219. $triggerFree = ConfigService::make()->getConfigByCode('trade_trigger_trx_free');
  220. $triggerFree = $triggerFree > 0 ? $triggerFree : 8;
  221. $triggerMin = $triggerMin > 0 ? $triggerMin : 0.1;
  222. $triggerTime = $triggerTime > 0 ? $triggerTime * 86400 : 86400;
  223. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  224. $this->error = '2203';
  225. return false;
  226. }
  227. // 出账钱包
  228. if (empty($otcOutAddress) || empty($otcOutAddressPrivate)) {
  229. $this->error = '2203';
  230. return false;
  231. }
  232. $page = RedisService::get("caches:wallet:transferTrxPage");
  233. $page = $page ? $page : 1;
  234. // 归集时间段为凌晨0点-5点
  235. if ((date('H:i') >= '05:00') && !$force) {
  236. $this->error = '不在归集时间段';
  237. return false;
  238. }
  239. if (RedisService::get("caches:wallet:triggerTrxLock:{$page}")) {
  240. $this->error = '不要频繁操作,30秒后重试';
  241. return false;
  242. }
  243. // 上锁
  244. RedisService::set("caches:wallet:triggerTrxLock:{$page}", 1, rand(10, 30));
  245. $addrList = MemberService::make()->getTriggerAddressList($triggerMin, $page, 200);
  246. if (empty($addrList)) {
  247. RedisService::set("caches:wallet:transferPage", 1, 600);
  248. $this->error = '1019';
  249. return false;
  250. }
  251. // 平台钱包地址
  252. $count = 0;
  253. $failedCount = 0;
  254. $retryCount = 0;
  255. $notBalance = false;
  256. $tron = new Tron();
  257. $tron->setAddress($otcAddress);
  258. $otcAddress = $tron->getAddress();
  259. $otcAddressData = new \Tron\Address($otcAddress['base58'], $otcAddressPrivate, $otcAddress['hex']);
  260. // 平台出账钱包地址
  261. $tron->setAddress($otcOutAddress);
  262. $otcOutAddress = $tron->getAddress();
  263. $otcOutAddressData = new \Tron\Address($otcOutAddress['base58'], $otcOutAddressPrivate, $otcOutAddress['hex']);
  264. $cacheKey = "caches:wallet:triggerTrx:";
  265. foreach ($addrList as $v) {
  266. try {
  267. // 获取子钱包TRC-USDT余额
  268. $userId = isset($v['id']) ? $v['id'] : 0;
  269. $address = isset($v['trc_address']) ? $v['trc_address'] : '';
  270. $hexAddress = isset($v['trc_hexaddress']) ? $v['trc_hexaddress'] : '';
  271. $addressPrivate = isset($v['trc_wif']) ? $v['trc_wif'] : '';
  272. $triggerAddress = new \Tron\Address($address, $addressPrivate, $hexAddress);
  273. // 可归集的USDT余额
  274. $triggerUsdt = $trcWallet->balance($triggerAddress);
  275. // USDT 余额低于归集金额,则不归集
  276. if ($triggerUsdt < $triggerMin) {
  277. $failedCount++;
  278. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'error' => '用户余额不足归集', 'date' => date('Y-m-d H:i:s')];
  279. RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
  280. continue;
  281. }
  282. // 获取子钱包TRX余额
  283. $triggerTrx = $trxWallet->balance($triggerAddress);
  284. // 获取平台出账钱包TRX余额
  285. $otcOutTrxTotal = $trxWallet->balance($otcOutAddressData);
  286. // 如果子钱包和平台钱包TRX余额不足手续费,则不归集
  287. if ($triggerTrx < $triggerFree && $otcOutTrxTotal < $triggerFree) {
  288. $failedCount++;
  289. $notBalance = true;
  290. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerTrx' => $triggerTrx, 'otcTrx' => $otcOutTrxTotal, 'error' => '平台钱包手续费不足', 'date' => date('Y-m-d H:i:s')];
  291. RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
  292. continue;
  293. }
  294. // 如果子钱包TRX不足手续费8个,平台TRX充足则自动充值后下次调用时归集
  295. if ($triggerTrx < $triggerFree) {
  296. $retryCount++;
  297. $result = $this->trxTransfer($address, $triggerFree);
  298. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerTrx' => $triggerTrx, 'transfer' => $result, 'error' => '归集钱包手续费不足,先充值', 'date' => date('Y-m-d H:i:s')];
  299. RedisService::set($cacheKey . "U_{$userId}:catch", $error, 7200);
  300. continue;
  301. }
  302. // 满足归集条件处理
  303. $result = $trcWallet->transfer($triggerAddress, $otcAddressData, $triggerUsdt);
  304. RedisService::set($cacheKey . "U_{$userId}:result", ['data' => $v, 'result' => $result, 'msg' => '归集已提交', 'date' => date('Y-m-d H:i:s')], 7200);
  305. $count++;
  306. } catch (\Exception $exception) {
  307. $failedCount++;
  308. RedisService::set($cacheKey . "U_{$userId}:exception", ['data' => $v, 'msg' => $exception->getMessage(), 'date' => date('Y-m-d H:i:s')], 7200);
  309. }
  310. }
  311. // 超出分页数,下次处理下一页
  312. if (count($addrList) >= 200) {
  313. RedisService::set("caches:wallet:transferPage", $page + 1, 600);
  314. }
  315. if ($count > 0) {
  316. $this->error = lang(2225, ['success' => $count, 'fail' => $failedCount, 'retry' => $retryCount]);
  317. return ['success' => $count, 'fail' => $failedCount, 'retryCount' => $retryCount];
  318. } else if ($failedCount == count($addrList)) {
  319. $this->error = 2221;
  320. return false;
  321. } else if ($retryCount) {
  322. $this->error = lang(2222, ['num' => $retryCount]);
  323. return false;
  324. } else if ($notBalance) {
  325. $this->error = 2221;
  326. return false;
  327. } else {
  328. $this->error = lang(2224, ['num' => $failedCount]);
  329. return false;
  330. }
  331. } catch (\Exception $exception) {
  332. $this->error = $exception->getMessage();
  333. return false;
  334. }
  335. }
  336. /**
  337. * 监听USDT-TRC2.0用户子钱包充值存币处理
  338. * @param $userId 用户ID
  339. * @param $address 用户钱包地址
  340. * @param int $coinType 币种:1-usdt
  341. * @param int $limit 转账记录数,最新的
  342. * @return array|false
  343. */
  344. public function getTrc20RechargeLog($userId, $address, $coinType = 1, $limit = 100)
  345. {
  346. if ($userId <= 0 || empty($address)) {
  347. $this->error = '1013';
  348. return false;
  349. }
  350. $url = sprintf($this->apiUrls['usdt_trx2_transfer_log'], $address, $limit, 'true', 'false');
  351. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  352. RedisService::set("caches:wallets:recharge_temp_{$userId}", ['url' => $this->config['tron_api_url'] . $url], 600);
  353. $result = curl_get($this->config['tron_api_url'] . $url, [], $headers, 10);
  354. $result = $result ? json_decode($result, true) : [];
  355. $datas = isset($result['data']) ? $result['data'] : [];
  356. $status = isset($result['success']) ? $result['success'] : '';
  357. if ($status != true || empty($datas)) {
  358. $this->error = '2207';
  359. return false;
  360. }
  361. $logs = [];
  362. $coinInMin = ConfigService::make()->getConfigByCode('trc_in_limit');
  363. $coinInMin = $coinInMin > 0 ? $coinInMin : 0;
  364. if ($datas) {
  365. foreach ($datas as $v) {
  366. $amount = isset($v['value']) ? intval($v['value']) : 0;
  367. $amount = moneyFormat($amount / 1000000, 6);
  368. $time = isset($v['block_timestamp']) ? intval($v['block_timestamp'] / 1000) : 0;
  369. $txid = isset($v['transaction_id']) ? $v['transaction_id'] : '';
  370. if (!CoinLogService::make()->checkExists('txid', $txid) && $time > time() - 12 * 3600) {
  371. $balance = $this->memberModel->where(['id' => $userId])->value('usdt_num');
  372. $orderNo = get_order_num('TR');
  373. $log = [
  374. 'user_id' => $userId,
  375. 'change_type' => 1,
  376. 'coin_type' => $coinType,
  377. 'contact_type' => 1,
  378. 'order_no' => $orderNo,
  379. 'from_address' => isset($v['from']) ? $v['from'] : '',
  380. 'to_address' => isset($v['to']) ? $v['to'] : '',
  381. 'txid' => $txid,
  382. 'num' => $amount,
  383. 'balance' => $balance,
  384. 'create_time' => $time ? $time : time(),
  385. 'update_time' => time(),
  386. 'status' => 2,
  387. 'mark' => 1,
  388. ];
  389. if ($amount >= $coinInMin && $this->memberModel->where(['id' => $userId])->increment('usdt_num', $amount)) {
  390. $this->memberModel->where(['id' => $userId])->increment('trc_usdt', $amount);
  391. $log['status'] = 1;
  392. $data = [
  393. 'order_no' => $orderNo,
  394. 'user_id' => $userId,
  395. 'type' => 4,
  396. 'pay_type' => 1,
  397. 'trade_type' => 3,
  398. 'change_type' => 1,
  399. 'num' => $amount,
  400. 'total' => 0,
  401. 'balance' => floatval($balance + $amount),
  402. 'create_time' => time(),
  403. 'update_time' => time(),
  404. 'remark' => '存币',
  405. 'status' => 1,
  406. 'mark' => 1,
  407. ];
  408. $this->capitalModel->edit($data);
  409. }
  410. $logs[] = $log;
  411. $this->coinModel->insert($log);
  412. }
  413. }
  414. }
  415. return $logs;
  416. }
  417. /**
  418. * 获取USDT-TRC2.0交易记录(进出账)
  419. * @param $address 用户钱包地址
  420. * @param int $coinType 币种:1-usdt
  421. * @param int $limit 转账记录数,最新的
  422. * @return array|false
  423. */
  424. public function getTrc20TransferLog($address, $type = 1, $limit = 50)
  425. {
  426. if (empty($address)) {
  427. $this->error = '1013';
  428. return false;
  429. }
  430. // 存币
  431. if ($type == 1) {
  432. $url = sprintf($this->apiUrls['usdt_trx2_transfer_log'], $address, $limit, 'true', 'false');
  433. } // 提币
  434. else {
  435. $url = sprintf($this->apiUrls['usdt_trx2_transfer_log'], $address, $limit, 'false', 'true');
  436. }
  437. try {
  438. $cacheKey = "caches:wallets:transfer:trc_{$address}";
  439. if (RedisService::get($cacheKey . '_lock')) {
  440. return false;
  441. }
  442. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  443. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  444. RedisService::set("caches:wallets:transfer_temp_otc_{$type}", ['url' => $this->config['tron_api_url'] . $url], 600);
  445. $result = curl_get($this->config['tron_api_url'] . $url, [], $headers, 10);
  446. $result = $result ? json_decode($result, true) : [];
  447. $datas = isset($result['data']) ? $result['data'] : [];
  448. $status = isset($result['success']) ? $result['success'] : '';
  449. if ($status != true || empty($datas)) {
  450. $this->error = '2207';
  451. return false;
  452. }
  453. return $datas;
  454. } catch (\Exception $exception) {
  455. $message = $exception->getMessage();
  456. $this->error = $message;
  457. return false;
  458. }
  459. }
  460. /**
  461. * 监听USDT-TRC2.0提币记录并进账(平台钱包)
  462. * @param $address 用户钱包地址
  463. * @param int $limit 转账记录数,最新的
  464. * @return array|false
  465. */
  466. public function getTrc20TransferLogByOtc($address, $tradeType=1, $limit = 50)
  467. {
  468. if (empty($address)) {
  469. $this->error = '1013';
  470. return false;
  471. }
  472. try {
  473. $cacheKey = "caches:wallets:transfer:trc_{$address}";
  474. if (RedisService::get($cacheKey . '_lock')) {
  475. return false;
  476. }
  477. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  478. $url = sprintf($this->apiUrls['usdt_trx2_transfer_log'], $address, $limit, 'false', 'true');
  479. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  480. RedisService::set("caches:wallets:transfer_temp_otc", ['url' => $this->config['tron_api_url'] . $url], 600);
  481. $result = curl_get($this->config['tron_api_url'] . $url, [], $headers, 10);
  482. $result = $result ? json_decode($result, true) : [];
  483. $datas = isset($result['data']) ? $result['data'] : [];
  484. $status = isset($result['success']) ? $result['success'] : '';
  485. if ($status != true || empty($datas)) {
  486. $this->error = '2207';
  487. return false;
  488. }
  489. $results = [
  490. 'logs' => [],
  491. 'trades' => []
  492. ];
  493. $cachekey = "caches:tradeOtc:{$address}_{$tradeType}";
  494. foreach ($datas as $v) {
  495. $amount = isset($v['value']) ? intval($v['value']) : 0;
  496. $amount = moneyFormat($amount / 1000000, 6);
  497. $time = isset($v['block_timestamp']) ? intval($v['block_timestamp'] / 1000) : 0;
  498. $txid = isset($v['transaction_id']) ? $v['transaction_id'] : '';
  499. if ($time > time() - 12 * 3600) {
  500. // 有记录,且是用户提币
  501. if($tradeType==1){
  502. $coinInfo = CoinLogService::make()->getCacheInfoByTxid($txid);
  503. $userId = isset($coinInfo['user_id']) ? $coinInfo['user_id'] : 0;
  504. if ($coinInfo && $userId && $coinInfo['status'] == 4 && $coinInfo['contact_type'] == 1) {
  505. // 直接更新提币状态
  506. CoinLogModel::where(['txid' => $txid, 'user_id' => $userId])->update(['from_address' => $v['from'], 'status' => 1, 'update_time' => time()]);
  507. // 明细处理
  508. $num = floatval($coinInfo['num'] + $coinInfo['free']);
  509. $data = [
  510. 'order_no' => $coinInfo['order_no'],
  511. 'user_id' => $userId,
  512. 'type' => 5,
  513. 'pay_type' => 1,
  514. 'trade_type' => 3,
  515. 'change_type' => 2,
  516. 'num' => $num,
  517. 'total' => 0,
  518. 'balance' => floatval($coinInfo['balance'] - $num),
  519. 'create_time' => time(),
  520. 'update_time' => time(),
  521. 'remark' => '提币:' . $amount,
  522. 'status' => 1,
  523. 'mark' => 1,
  524. ];
  525. $this->capitalModel->edit($data);
  526. $results['trades'][] = ['log' => $v, 'order' => $coinInfo];
  527. } // 其他交易明细
  528. else if ($time > time() - 7 * 12 * 3600 && !$coinInfo) {
  529. $results['logs'][] = [
  530. 'type' => 3,
  531. 'user_id' => 0,
  532. 'from_address' => $v['from'],
  533. 'to_address' => $v['to'],
  534. 'change_type' => $v['from'] == $address ? 2 : 1,
  535. 'coin_type' => 1,
  536. 'contact_type' => 1,
  537. 'order_no' => get_order_num('TC'),
  538. 'txid' => $txid,
  539. 'num' => $amount,
  540. 'free' => 0,
  541. 'balance' => 0,
  542. 'create_time' => time(),
  543. 'update_time' => time(),
  544. 'status' => 1,
  545. 'mark' => 1,
  546. ];
  547. }
  548. }
  549. // 外来客户派单监听转入币,并且匹配交易元给客户
  550. else{
  551. RedisService::set($cacheKey.'_temp',['data'=> $v], 7200);
  552. $orderInfo = TradeOrderService::make()->getInfoByTradeHash($txid);
  553. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  554. $apiId = isset($orderInfo['api_id']) ? $orderInfo['api_id'] : 0;
  555. $num = isset($orderInfo['num']) ? $orderInfo['num'] : 0;
  556. if ($orderInfo && ($userId||$apiId) && $num>0 && ($num == $amount) && $orderInfo['status'] == 0 && $orderInfo['contact_type'] == 1) {
  557. // 获取匹配商家
  558. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 1, $userId);
  559. if (empty($businessInfo)) {
  560. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'info'=> $orderInfo,'error'=>'未匹配到交易员'], 7200);
  561. continue;
  562. }
  563. if($businessInfo['usdt_num']<$num){
  564. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'info'=> $orderInfo,'error'=>'未匹配到合适的交易员'], 7200);
  565. continue;
  566. }
  567. if(!TradeOrderModel::where(['txid' => $txid, 'id' => $orderInfo['id']])->update(['business_id' => $businessInfo['id'], 'status' => 1,'create_time'=>time(), 'update_time' => time()])){
  568. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'business'=> $businessInfo,'info'=> $orderInfo,'error'=>'更新订单匹配交易数据失败'], 7200);
  569. continue;
  570. }
  571. // 记录平台钱包交易明细
  572. $results['logs'][] = [
  573. 'type' => 3,
  574. 'user_id' => 0,
  575. 'from_address' => $v['from'],
  576. 'to_address' => $v['to'],
  577. 'change_type' => $v['from'] == $address ? 2 : 1,
  578. 'coin_type' => 1,
  579. 'contact_type' => 1,
  580. 'order_no' => get_order_num('TP'),
  581. 'txid' => $txid,
  582. 'num' => $amount,
  583. 'free' => 0,
  584. 'balance' => 0,
  585. 'create_time' => time(),
  586. 'update_time' => time(),
  587. 'status' => 1,
  588. 'mark' => 1,
  589. ];
  590. $results['trades'][] = ['log' => $v, 'order' => $orderInfo];
  591. } // 其他交易明细
  592. else if ($time > time() - 7 * 12 * 3600 && !$coinInfo) {
  593. $results['logs'][] = [
  594. 'type' => 3,
  595. 'user_id' => 0,
  596. 'from_address' => $v['from'],
  597. 'to_address' => $v['to'],
  598. 'change_type' => $v['from'] == $address ? 2 : 1,
  599. 'coin_type' => 1,
  600. 'contact_type' => 1,
  601. 'order_no' => get_order_num('TC'),
  602. 'txid' => $txid,
  603. 'num' => $amount,
  604. 'free' => 0,
  605. 'balance' => 0,
  606. 'create_time' => time(),
  607. 'update_time' => time(),
  608. 'status' => 1,
  609. 'mark' => 1,
  610. ];
  611. }
  612. }
  613. }
  614. }
  615. if ($results['logs']) {
  616. $this->coinModel->insertAll($results['logs']);
  617. }
  618. return $results;
  619. } catch (\Exception $exception) {
  620. $message = $exception->getMessage();
  621. $this->error = $message;
  622. return false;
  623. }
  624. }
  625. /**
  626. * 监听USDT-TRC2.0用户子钱包提币处理
  627. * @param $userId 用户ID
  628. * @param $address 用户钱包地址
  629. * @param int $coinType 币种:1-usdt
  630. * @param int $limit 转账记录数,最新的
  631. * @return array|false
  632. */
  633. public function getTrc20TransferLogByUser($userId, $address, $coinType = 1, $limit = 50)
  634. {
  635. if ($userId <= 0 || empty($address)) {
  636. $this->error = '1013';
  637. return false;
  638. }
  639. $url = sprintf($this->apiUrls['usdt_trx2_transfer_log'], $address, $limit, 'false', 'true');
  640. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  641. RedisService::set("caches:wallets:transfer_temp_{$userId}", ['url' => $this->config['tron_api_url'] . $url], 600);
  642. $result = curl_get($this->config['tron_api_url'] . $url, [], $headers, 10);
  643. $result = $result ? json_decode($result, true) : [];
  644. $datas = isset($result['data']) ? $result['data'] : [];
  645. $status = isset($result['success']) ? $result['success'] : '';
  646. if ($status != true || empty($datas)) {
  647. $this->error = '2207';
  648. return false;
  649. }
  650. $logs = [];
  651. $coinOutMin = ConfigService::make()->getConfigByCode('trc_out_limit');
  652. $coinOutMin = $coinOutMin > 0 ? $coinOutMin : 0;
  653. foreach ($datas as $v) {
  654. $amount = isset($v['value']) ? intval($v['value']) : 0;
  655. $amount = moneyFormat($amount / 1000000, 6);
  656. $time = isset($v['block_timestamp']) ? intval($v['block_timestamp'] / 1000) : 0;
  657. $txid = isset($v['transaction_id']) ? $v['transaction_id'] : '';
  658. if (!CoinLogService::make()->checkExists('txid', $txid) && $time > time() - 12 * 3600) {
  659. $balance = $this->memberModel->where(['id' => $userId])->value('usdt_num');
  660. $orderNo = get_order_num('TW');
  661. $log = [
  662. 'user_id' => $userId,
  663. 'change_type' => 2,
  664. 'coin_type' => $coinType,
  665. 'contact_type' => 1,
  666. 'order_no' => $orderNo,
  667. 'from_address' => isset($v['from']) ? $v['from'] : '',
  668. 'to_address' => isset($v['to']) ? $v['to'] : '',
  669. 'txid' => $txid,
  670. 'num' => $amount,
  671. 'balance' => $balance,
  672. 'create_time' => $time ? $time : time(),
  673. 'update_time' => time(),
  674. 'status' => 2,
  675. 'mark' => 1,
  676. ];
  677. if ($amount >= $coinOutMin && $this->memberModel->where(['id' => $userId])->decrement('usdt_num', $amount)) {
  678. $this->memberModel->where(['id' => $userId])->decrement('trc_usdt', $amount);
  679. $log['status'] = 1;
  680. // 明细处理
  681. $data = [
  682. 'order_no' => $orderNo,
  683. 'user_id' => $userId,
  684. 'type' => 5,
  685. 'pay_type' => 1,
  686. 'trade_type' => 3,
  687. 'change_type' => 2,
  688. 'num' => $amount,
  689. 'total' => 0,
  690. 'balance' => floatval($balance - $amount),
  691. 'create_time' => time(),
  692. 'update_time' => time(),
  693. 'remark' => '提币',
  694. 'status' => 1,
  695. 'mark' => 1,
  696. ];
  697. $this->capitalModel->edit($data);
  698. }
  699. $logs[] = $log;
  700. $this->coinModel->insert($log);
  701. }
  702. }
  703. return $logs;
  704. }
  705. /**
  706. * TRX余额
  707. * @param $address
  708. * @return false|float|string
  709. */
  710. public function getTrxBalance($address, $cache = false)
  711. {
  712. if (empty($address)) {
  713. $this->error = '1018';
  714. return false;
  715. }
  716. if (empty($this->config['tron_api_url'])) {
  717. $this->error = '2206';
  718. return false;
  719. }
  720. $cacheKey = "caches:wallet:balance:trx_{$address}";
  721. if ($data = RedisService::get($cacheKey) && $cache) {
  722. return $data;
  723. }
  724. if (RedisService::get($cacheKey . '_lock') && $cache) {
  725. return false;
  726. }
  727. try {
  728. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  729. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
  730. $trxWallet = new TRX($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
  731. $tron = new Tron();
  732. $tron->setAddress($address);
  733. $address = $tron->getAddress();
  734. $address = new \Tron\Address($address['base58'], '', $address['hex']);
  735. $result = $trxWallet->balance($address);
  736. $result = $result ? floatval($result) : '0.00';
  737. RedisService::set($cacheKey, $result, rand(5, 10));
  738. RedisService::set($cacheKey . '_lock', true, rand(10, 20));
  739. return $result;
  740. } catch (\Exception $exception) {
  741. $this->error = $exception->getMessage();
  742. return false;
  743. }
  744. }
  745. /**
  746. * USDT-TRC20余额
  747. * @param $address
  748. * @return false|float|string
  749. */
  750. public function getTrc20Usdt($address, $cache = false)
  751. {
  752. if (empty($address)) {
  753. $this->error = '1018';
  754. return false;
  755. }
  756. if (empty($this->config['tron_api_url'])) {
  757. $this->error = '2206';
  758. return false;
  759. }
  760. $cacheKey = "caches:wallet:balance:usdt_{$address}";
  761. if ($data = RedisService::get($cacheKey) && $cache) {
  762. return $data;
  763. }
  764. if (RedisService::get($cacheKey . '_lock') && $cache) {
  765. return false;
  766. }
  767. try {
  768. $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
  769. $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
  770. $trxWallet = new TRC20($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
  771. $tron = new Tron();
  772. $tron->setAddress($address);
  773. $address = $tron->getAddress();
  774. $address = new \Tron\Address($address['base58'], '', $address['hex']);
  775. $result = $trxWallet->balance($address);
  776. $result = $result ? floatval($result) : '0.00';
  777. RedisService::set($cacheKey, $result, rand(5, 10));
  778. RedisService::set($cacheKey . '_lock', true, rand(10, 20));
  779. return $result;
  780. } catch (\Exception $exception) {
  781. $this->error = $exception->getMessage();
  782. return false;
  783. }
  784. }
  785. /********************** ERC钱包 **************************/
  786. /**
  787. * 获取ERC2.0钱包地址
  788. * @param string $type
  789. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  790. */
  791. public function getErcAddress()
  792. {
  793. $random = new Random();
  794. $network = Bitcoin::getNetwork();
  795. $privateKeyFactory = new PrivateKeyFactory();
  796. $privateKey = $privateKeyFactory->generateCompressed($random);
  797. $publicKey = $privateKey->getPublicKey();
  798. // p2pkh 格式的地址
  799. $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
  800. // 将生成的钱包保存到数据库中
  801. $wif = $privateKey->toWif($network);
  802. $address = $addressService->getAddress();
  803. return ['wif' => $wif, 'hexAddress' => $this->getHexAddress($address), 'address' => $address];
  804. }
  805. /**
  806. * 获取HASH钱包地址
  807. * @param $address 钱包地址
  808. * @return \BitWasp\Buffertools\BufferInterface
  809. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  810. */
  811. public function getHexAddress($address)
  812. {
  813. $data = WitnessProgram::v0((new AddressCreator())->fromString($address)->getHash());
  814. $buffer = $data->getProgram()->getHex();
  815. return $buffer ? '0x' . $buffer : '';
  816. }
  817. /**
  818. * TRC余额
  819. * @param $address
  820. * @return false|float|string
  821. */
  822. public function getErcBalance($address, $cache = false)
  823. {
  824. if (empty($address)) {
  825. $this->error = '1018';
  826. return false;
  827. }
  828. if (empty($this->config['eth_api_url'])) {
  829. $this->error = '2206';
  830. return false;
  831. }
  832. if (empty($this->config['eth_api_key'])) {
  833. $this->error = '2207';
  834. return false;
  835. }
  836. $cacheKey = "caches:wallet:balance:erc_{$address}";
  837. if ($data = RedisService::get($cacheKey) && $cache) {
  838. return floatval($data);
  839. }
  840. if (RedisService::get($cacheKey . '_lock') && $cache) {
  841. return false;
  842. }
  843. try {
  844. $url = sprintf($this->apiUrls['erc_balance'],$address, $this->config['eth_api_key']);
  845. $url = $this->config['eth_api_url'] . $url;
  846. RedisService::set("caches:trades:erc:balance_{$address}", ['url' => $url, 'config' => $this->config], 600);
  847. $result = curl_api($url);
  848. $result = $result? json_decode($result, true) : [];
  849. $balance = isset($result['result']) ? floatval($result['result']) : 0.00;
  850. $balance = $balance>0? floatval($balance / pow(10, 18)) : '0.00';
  851. RedisService::set($cacheKey, $balance, rand(5, 10));
  852. RedisService::set($cacheKey . '_lock', true, rand(10, 20));
  853. return floatval($balance);
  854. } catch (\Exception $exception) {
  855. $this->error = $exception->getMessage();
  856. return false;
  857. }
  858. }
  859. /**
  860. * USDT-ERC20余额
  861. * @param $address
  862. * @return false|float|string
  863. */
  864. public function getErc20Usdt($address, $cache = false)
  865. {
  866. if (empty($address)) {
  867. $this->error = '1018';
  868. return false;
  869. }
  870. if (empty($this->config['eth_api_url'])) {
  871. $this->error = '2206';
  872. return false;
  873. }
  874. if (empty($this->config['eth_api_key'])) {
  875. $this->error = '2207';
  876. return false;
  877. }
  878. if (empty($this->config['erc_contract_address'])) {
  879. $this->error = '2228';
  880. return false;
  881. }
  882. $cacheKey = "caches:wallet:balance:erc_usdt_{$address}";
  883. if ($data = RedisService::get($cacheKey) && $cache) {
  884. return floatval($data);
  885. }
  886. if (RedisService::get($cacheKey . '_lock') && $cache) {
  887. return false;
  888. }
  889. try {
  890. $url = sprintf($this->apiUrls['erc20_balance'], $this->config['erc_contract_address'],$address, $this->config['eth_api_key']);
  891. $url = $this->config['eth_api_url'] . $url;
  892. RedisService::set("caches:trades:erc:balance20_{$address}", ['url' => $url, 'config' => $this->config], 600);
  893. $result = curl_api($url);
  894. $result = $result? json_decode($result, true) : [];
  895. $balance = isset($result['result']) ? floatval($result['result']) : 0.00;
  896. $balance = $balance>0? floatval($balance / pow(10, 6)) : '0.00';
  897. RedisService::set($cacheKey, floatval($balance), rand(5, 10));
  898. RedisService::set($cacheKey . '_lock', true, rand(10, 20));
  899. return floatval($balance);
  900. } catch (\Exception $exception) {
  901. $this->error = $exception->getMessage();
  902. return false;
  903. }
  904. }
  905. /**
  906. * ERC 转账
  907. * @param $to
  908. * @param $amount
  909. * @param string $from
  910. * @param string $fromPrivate
  911. * @return array|false
  912. */
  913. public function ercTransfer($to, $amount, $from = '', $fromPrivate = '')
  914. {
  915. if ($amount <= 0) {
  916. $this->error = '2205';
  917. return false;
  918. }
  919. if (empty($this->config['eth_api_url'])) {
  920. $this->error = '2206';
  921. return false;
  922. }
  923. if (empty($this->config['eth_api_key'])) {
  924. $this->error = '2207';
  925. return false;
  926. }
  927. // 获取钱包参数
  928. try {
  929. $otcAddress = $from ? $from : ConfigService::make()->getConfigByCode('erc_out_address');
  930. $otcAddressPrivate = $from ? $fromPrivate : ConfigService::make()->getConfigByCode('erc_out_private_key');
  931. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  932. $this->error = '2228';
  933. return false;
  934. }
  935. $amount = floatval($amount * pow(10, 18));
  936. $params = [
  937. "from" => $otcAddress, // 来源
  938. "to" => $to, // 收款
  939. "gas" => '0x' . dechex(90000), // gas量,默认90000
  940. "gasPrice" => "0x9184e72a000",
  941. "value" => '0x' . dechex($amount),
  942. "data" => ''
  943. ];
  944. $transaction = new Transaction($params);
  945. $transaction->sign($otcAddressPrivate);
  946. $hex = '0x' . $transaction;
  947. $url = sprintf($this->apiUrls['erc_transaction'], $hex, $this->config['eth_api_key']);
  948. $url = $this->config['eth_api_url'] . $url;
  949. RedisService::set("caches:trades:erc:transfer_{$to}", ['url' => $url, 'params' => $params, 'hex' => $hex], 600);
  950. $result = curl_api($url);
  951. $result = $result ? json_decode($result, true) : [];
  952. $tradeHash = isset($result['result']) ? $result['result'] : '';
  953. if ($tradeHash && hexdec($tradeHash) != 0) {
  954. return ['txid' => $tradeHash, 'amount' => $amount];
  955. } else {
  956. $this->error = '2229';
  957. return false;
  958. }
  959. } catch (\Exception $exception) {
  960. $message = $exception->getMessage();
  961. $this->error = $message;
  962. return false;
  963. }
  964. }
  965. /**
  966. * usdt-erc2.0 转账
  967. * @param $to 进账账户
  968. * @param $amount 转账金额
  969. * @param $from 转账账户
  970. * @param $fromPrivate 转账账户私钥
  971. * @throws \Tron\Exceptions\TransactionException
  972. * @throws \Tron\Exceptions\TronErrorException
  973. */
  974. public function usdtErcTransfer($to, $amount, $from = '', $fromPrivate = '')
  975. {
  976. if ($amount <= 0) {
  977. $this->error = '2205';
  978. return false;
  979. }
  980. if (empty($this->config['eth_api_url'])) {
  981. $this->error = '2206';
  982. return false;
  983. }
  984. if (empty($this->config['eth_api_key'])) {
  985. $this->error = '2207';
  986. return false;
  987. }
  988. // 获取钱包参数
  989. try {
  990. // 用出账钱包转账
  991. $otcAddress = $from ? $from : ConfigService::make()->getConfigByCode('erc_out_address');
  992. $otcAddressPrivate = $from ? $fromPrivate : ConfigService::make()->getConfigByCode('erc_out_private_key');
  993. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  994. $this->error = '2228';
  995. return false;
  996. }
  997. $amount = floatval($amount * pow(10, 18));
  998. $sign = $this->getSha3Sign("transfer(address,uint256)");
  999. $params = [
  1000. "from" => $otcAddress, // 来源
  1001. "to" => $to, // 收款
  1002. "gas" => '0x' . dechex(90000), // gas量,默认90000
  1003. "gasPrice" => "0x9184e72a000",
  1004. "value" => '0x' . dechex($amount),
  1005. "data" => $this->getTransactionSignData($sign, $to, $amount)
  1006. ];
  1007. $transaction = new Transaction($params);
  1008. $transaction->sign($otcAddressPrivate);
  1009. $hex = '0x' . $transaction;
  1010. $url = sprintf($this->apiUrls['erc_transaction'], $hex, $this->config['eth_api_key']);
  1011. $url = $this->config['eth_api_url'] . $url;
  1012. RedisService::set("caches:trades:erc20:transfer_{$to}", ['url' => $url, 'params' => $params, 'hex' => $hex], 600);
  1013. $result = curl_api($url);
  1014. $result = $result ? json_decode($result, true) : [];
  1015. $tradeHash = isset($result['result']) ? $result['result'] : '';
  1016. $error = isset($result['error']) ? $result['error'] : [];
  1017. if ($tradeHash && hexdec($tradeHash) != 0) {
  1018. return ['txID' => $tradeHash, 'amount' => $amount];
  1019. } else {
  1020. $this->error = isset($error['message']) ? $error['message'] : '2229';
  1021. return false;
  1022. }
  1023. } catch (\Exception $exception) {
  1024. $message = $exception->getMessage();
  1025. $this->error = $message;
  1026. return false;
  1027. }
  1028. }
  1029. /**
  1030. * 合约方法签名编号
  1031. * @param string $data
  1032. * @return string
  1033. */
  1034. public function getSha3Sign($data)
  1035. {
  1036. $util = new Util();
  1037. $result = $util->sha3($data);
  1038. return '0x' . substr($result, 0, 8);
  1039. }
  1040. /**
  1041. * 获取签名数据
  1042. * @param $sign
  1043. * @param $address
  1044. * @return string
  1045. */
  1046. public function getSignData($sign, $address)
  1047. {
  1048. return $sign . str_repeat('0', 24) . substr($address, 2);
  1049. }
  1050. /**
  1051. * 获取交易签名参数
  1052. * @param $sign 调用的合约方法生成的sha3签名编号
  1053. * @param $toAddress 收款地址
  1054. * @param $amount 转账金额/十进制
  1055. * @return string
  1056. */
  1057. public function getTransactionSignData($sign, $toAddress, $amount)
  1058. {
  1059. $toAddress = substr($toAddress, 2); // 去掉0x
  1060. $toAddressSign = str_repeat('0', 64 - strlen($toAddress)) . $toAddress; // 前面补齐0为64位
  1061. $amount = dechex($amount); // 十进制转十六进制
  1062. $amountSign = str_repeat('0', 64 - strlen($amount)) . $amount; // 前面补齐0为64位
  1063. return $sign . $toAddressSign . $amountSign;
  1064. }
  1065. /**
  1066. * 监听USDT-ERC2.0用户子钱包存币处理
  1067. * @param $userId 用户ID
  1068. * @param $address 用户钱包地址
  1069. * @param int $coinType 币种:1-usdt
  1070. * @param int $limit 转账记录数,最新的
  1071. * @return array|false
  1072. */
  1073. public function getErc20RechargeLog($userId, $address, $coinType = 1, $limit = 50)
  1074. {
  1075. if ($userId <= 0 || empty($address)) {
  1076. $this->error = '1013';
  1077. return false;
  1078. }
  1079. if (empty($this->config['eth_api_url'])) {
  1080. $this->error = '2206';
  1081. return false;
  1082. }
  1083. if (empty($this->config['eth_api_key'])) {
  1084. $this->error = '2207';
  1085. return false;
  1086. }
  1087. try {
  1088. $cacheKey = "caches:wallets:recharge:erc_{$userId}_{$address}";
  1089. if (RedisService::get($cacheKey . '_lock')) {
  1090. return false;
  1091. }
  1092. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  1093. $url = sprintf($this->apiUrls['erc20_transaction_log'], $address, 1, $limit, $this->config['eth_api_key']);
  1094. $url = $this->config['eth_api_url'] . $url;
  1095. RedisService::set("caches:trades:erc20:logs_{$address}", ['url' => $url, 'params' => ['address' => $address, 'config' => $this->config, 'limit' => $limit]], 600);
  1096. $result = curl_api($url);
  1097. $result = $result ? json_decode($result, true) : [];
  1098. $status = isset($result['status']) ? $result['status'] : 0;
  1099. $datas = isset($result['result']) ? $result['result'] : [];
  1100. if ($status != 1 || empty($datas)) {
  1101. $this->error = '2207';
  1102. return false;
  1103. }
  1104. $logs = [];
  1105. $coinInMin = ConfigService::make()->getConfigByCode('erc_in_limit');
  1106. $coinInMin = $coinInMin > 0 ? $coinInMin : 0;
  1107. if ($datas) {
  1108. foreach ($datas as $v) {
  1109. $amount = isset($v['value']) ? intval($v['value']) : 0;
  1110. $amount = moneyFormat($amount / 1000000, 6);
  1111. $time = isset($v['timeStamp']) ? intval($v['timeStamp']) : 0;
  1112. $txid = isset($v['hash']) ? $v['hash'] : '';
  1113. if (!CoinLogService::make()->checkExists('txid', $txid) && $time > time() - 12 * 3600) {
  1114. $balance = $this->memberModel->where(['id' => $userId])->value('usdt_num');
  1115. $orderNo = get_order_num('ER');
  1116. $log = [
  1117. 'type' => 1,
  1118. 'user_id' => $userId,
  1119. 'change_type' => 1,
  1120. 'coin_type' => $coinType,
  1121. 'contact_type' => 2,
  1122. 'order_no' => $orderNo,
  1123. 'from_address' => isset($v['from']) ? $v['from'] : '',
  1124. 'to_address' => isset($v['to']) ? $v['to'] : '',
  1125. 'txid' => $txid,
  1126. 'num' => $amount,
  1127. 'balance' => $balance,
  1128. 'create_time' => $time ? $time : time(),
  1129. 'update_time' => time(),
  1130. 'status' => 2,
  1131. 'mark' => 1,
  1132. ];
  1133. if ($amount >= $coinInMin && $this->memberModel->where(['id' => $userId])->increment('usdt_num', $amount)) {
  1134. $this->memberModel->where(['id' => $userId])->increment('erc_usdt', $amount);
  1135. $log['status'] = 1;
  1136. $data = [
  1137. 'order_no' => $orderNo,
  1138. 'user_id' => $userId,
  1139. 'type' => 4,
  1140. 'pay_type' => 1,
  1141. 'trade_type' => 3,
  1142. 'change_type' => 1,
  1143. 'num' => $amount,
  1144. 'total' => 0,
  1145. 'balance' => floatval($balance + $amount),
  1146. 'create_time' => time(),
  1147. 'update_time' => time(),
  1148. 'remark' => '存币:' . $amount,
  1149. 'status' => 1,
  1150. 'mark' => 1,
  1151. ];
  1152. $this->capitalModel->edit($data);
  1153. }
  1154. $logs[] = $log;
  1155. $this->coinModel->insert($log);
  1156. }
  1157. }
  1158. }
  1159. return $datas;
  1160. } catch (\Exception $exception) {
  1161. $message = $exception->getMessage();
  1162. $this->error = $message;
  1163. return false;
  1164. }
  1165. }
  1166. /**
  1167. * 获取USDT-ERC2.0交易记录(进出账)
  1168. * @param $address 用户钱包地址
  1169. * @param int $coinType 币种:1-usdt
  1170. * @param int $limit 转账记录数,最新的
  1171. * @return array|false
  1172. */
  1173. public function getErc20TransferLog($address, $type = 1, $page = 1, $limit = 50)
  1174. {
  1175. if (empty($this->config['eth_api_url'])) {
  1176. $this->error = '2206';
  1177. return false;
  1178. }
  1179. if (empty($this->config['eth_api_key'])) {
  1180. $this->error = '2207';
  1181. return false;
  1182. }
  1183. try {
  1184. $cacheKey = "caches:wallets:transfer:erc_{$address}";
  1185. if (RedisService::get($cacheKey . '_lock')) {
  1186. return false;
  1187. }
  1188. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  1189. $url = sprintf($this->apiUrls['erc20_transaction_log'], $address, $page, $limit, $this->config['eth_api_key']);
  1190. $url = $this->config['eth_api_url'] . $url;
  1191. RedisService::set("caches:trades:erc20:logs_{$address}", ['url' => $url, 'params' => ['address' => $address, 'config' => $this->config, 'limit' => $limit]], 600);
  1192. $result = curl_api($url);
  1193. $result = $result ? json_decode($result, true) : [];
  1194. $status = isset($result['status']) ? $result['status'] : 0;
  1195. $datas = isset($result['result']) ? $result['result'] : [];
  1196. if ($status != 1 || empty($datas)) {
  1197. $this->error = '2207';
  1198. return false;
  1199. }
  1200. return $datas;
  1201. } catch (\Exception $exception) {
  1202. $message = $exception->getMessage();
  1203. $this->error = $message;
  1204. return false;
  1205. }
  1206. }
  1207. /**
  1208. * 监听USDT-ERC2.0提币记录并进账(平台钱包)
  1209. * @param $address 用户钱包地址
  1210. * @param int $limit 转账记录数,最新的
  1211. * @return array|false
  1212. */
  1213. public function getErc20TransferLogByOtc($address, $tradeType=1, $limit = 100)
  1214. {
  1215. if (empty($address)) {
  1216. $this->error = '1013';
  1217. return false;
  1218. }
  1219. if (empty($this->config['eth_api_url'])) {
  1220. $this->error = '2206';
  1221. return false;
  1222. }
  1223. if (empty($this->config['eth_api_key'])) {
  1224. $this->error = '2207';
  1225. return false;
  1226. }
  1227. try {
  1228. $cacheKey = "caches:wallets:transfer:erc_{$address}";
  1229. if (RedisService::get($cacheKey . '_lock')) {
  1230. return false;
  1231. }
  1232. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  1233. $url = sprintf($this->apiUrls['erc20_transaction_log'], $address, 1, $limit, $this->config['eth_api_key']);
  1234. $url = $this->config['eth_api_url'] . $url;
  1235. RedisService::set("caches:trades:erc20:logs_{$address}", ['url' => $url, 'params' => ['address' => $address, 'config' => $this->config, 'limit' => $limit]], 600);
  1236. $result = curl_api($url);
  1237. $result = $result ? json_decode($result, true) : [];
  1238. $status = isset($result['status']) ? $result['status'] : 0;
  1239. $datas = isset($result['result']) ? $result['result'] : [];
  1240. if ($status != 1 || empty($datas)) {
  1241. $this->error = '2207';
  1242. return false;
  1243. }
  1244. $results = [
  1245. 'logs' => [],
  1246. 'trades' => [],
  1247. ];
  1248. if ($datas) {
  1249. $cachekey = "caches:tradeOtc:{$address}_{$tradeType}";
  1250. foreach ($datas as $v) {
  1251. $from = isset($v['from']) ? trim($v['from']) : '';
  1252. $to = isset($v['to']) ? trim($v['to']) : '';
  1253. $amount = isset($v['value']) ? intval($v['value']) : 0;
  1254. $decimal = isset($v['tokenDecimal']) ? intval($v['tokenDecimal']) : 0;
  1255. $decimal = $decimal ? $decimal : 6;
  1256. $amount = moneyFormat($amount / pow(10, $decimal), 6);
  1257. $time = isset($v['timeStamp']) ? intval($v['timeStamp']) : 0;
  1258. $txid = isset($v['hash']) ? $v['hash'] : '';
  1259. if($tradeType==1){
  1260. if ($time > time() - 12 * 3600 && $from == $address) {
  1261. // 有记录,且是用户提币
  1262. $coinInfo = CoinLogService::make()->getCacheInfoByTxid($txid);
  1263. $userId = isset($coinInfo['user_id']) ? $coinInfo['user_id'] : 0;
  1264. if ($coinInfo && $userId && $coinInfo['status'] == 4 && $coinInfo['contact_type'] == 2) {
  1265. // 直接更新提币状态
  1266. CoinLogModel::where(['txid' => $txid, 'user_id' => $userId])->update(['status' => 1, 'update_time' => time()]);
  1267. // 明细处理
  1268. $num = floatval($coinInfo['num'] + $coinInfo['free']);
  1269. $data = [
  1270. 'order_no' => $coinInfo['order_no'],
  1271. 'user_id' => $userId,
  1272. 'type' => 5,
  1273. 'pay_type' => 1,
  1274. 'trade_type' => 3,
  1275. 'change_type' => 2,
  1276. 'num' => $num,
  1277. 'total' => 0,
  1278. 'balance' => floatval($coinInfo['balance'] - $num),
  1279. 'create_time' => time(),
  1280. 'update_time' => time(),
  1281. 'remark' => '提币:' . $amount,
  1282. 'status' => 1,
  1283. 'mark' => 1,
  1284. ];
  1285. $this->capitalModel->edit($data);
  1286. $results['trades'][] = ['log' => $v, 'order' => $coinInfo];
  1287. }
  1288. } // 其他交易明细
  1289. else if ($time > time() - 7 * 12 * 3600 && !$coinInfo) {
  1290. $results['logs'][] = [
  1291. 'type' => 3,
  1292. 'user_id' => 0,
  1293. 'from_address' => $from,
  1294. 'to_address' => $to,
  1295. 'change_type' => $from == $address ? 2 : 1,
  1296. 'coin_type' => 1,
  1297. 'contact_type' => 2,
  1298. 'order_no' => get_order_num('TC'),
  1299. 'txid' => $txid,
  1300. 'num' => $amount,
  1301. 'free' => 0,
  1302. 'balance' => 0,
  1303. 'create_time' => time(),
  1304. 'update_time' => time(),
  1305. 'status' => 1,
  1306. 'mark' => 1,
  1307. ];
  1308. }
  1309. }
  1310. // 外来客户派单监听转入币,并且匹配交易元给客户
  1311. else{
  1312. RedisService::set($cacheKey.'_temp',['data'=> $v], 7200);
  1313. $orderInfo = TradeOrderService::make()->getInfoByTradeHash($txid);
  1314. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1315. $apiId = isset($orderInfo['api_id']) ? $orderInfo['api_id'] : 0;
  1316. $num = isset($orderInfo['num']) ? $orderInfo['num'] : 0;
  1317. if ($orderInfo && ($userId||$apiId) && $num>0 && ($num == $amount) && $orderInfo['status'] == 0 && $orderInfo['contact_type'] == 2) {
  1318. // 获取匹配商家
  1319. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 1, $userId);
  1320. if (empty($businessInfo)) {
  1321. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'info'=> $orderInfo,'error'=>'未匹配到交易员'], 7200);
  1322. continue;
  1323. }
  1324. if($businessInfo['usdt_num']<$num){
  1325. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'info'=> $orderInfo,'error'=>'未匹配到合适的交易员'], 7200);
  1326. continue;
  1327. }
  1328. if(!TradeOrderModel::where(['txid' => $txid, 'id' => $orderInfo['id']])->update(['business_id' => $businessInfo['id'], 'status' => 1,'create_time'=>time(), 'update_time' => time()])){
  1329. RedisService::set($cachekey.":{$userId}_{$apiId}_error", ['data'=> $v,'business'=> $businessInfo,'info'=> $orderInfo,'error'=>'更新订单匹配交易数据失败'], 7200);
  1330. continue;
  1331. }
  1332. // 记录平台钱包交易明细
  1333. $results['logs'][] = [
  1334. 'type' => 3,
  1335. 'user_id' => 0,
  1336. 'from_address' => $v['from'],
  1337. 'to_address' => $v['to'],
  1338. 'change_type' => $v['from'] == $address ? 2 : 1,
  1339. 'coin_type' => 1,
  1340. 'contact_type' => 1,
  1341. 'order_no' => get_order_num('TP'),
  1342. 'txid' => $txid,
  1343. 'num' => $amount,
  1344. 'free' => 0,
  1345. 'balance' => 0,
  1346. 'create_time' => time(),
  1347. 'update_time' => time(),
  1348. 'status' => 1,
  1349. 'mark' => 1,
  1350. ];
  1351. $results['trades'][] = ['log' => $v, 'order' => $orderInfo];
  1352. } // 其他交易明细
  1353. else if ($time > time() - 7 * 12 * 3600 && !$coinInfo) {
  1354. $results['logs'][] = [
  1355. 'type' => 3,
  1356. 'user_id' => 0,
  1357. 'from_address' => $v['from'],
  1358. 'to_address' => $v['to'],
  1359. 'change_type' => $v['from'] == $address ? 2 : 1,
  1360. 'coin_type' => 1,
  1361. 'contact_type' => 1,
  1362. 'order_no' => get_order_num('TC'),
  1363. 'txid' => $txid,
  1364. 'num' => $amount,
  1365. 'free' => 0,
  1366. 'balance' => 0,
  1367. 'create_time' => time(),
  1368. 'update_time' => time(),
  1369. 'status' => 1,
  1370. 'mark' => 1,
  1371. ];
  1372. }
  1373. }
  1374. }
  1375. }
  1376. if ($results['logs']) {
  1377. $this->coinModel->insertAll($results['logs']);
  1378. }
  1379. return $results;
  1380. } catch (\Exception $exception) {
  1381. $message = $exception->getMessage();
  1382. $this->error = $message;
  1383. return false;
  1384. }
  1385. }
  1386. /**
  1387. * USDT-ERC20 用户子钱包提币处理
  1388. * @param $userId
  1389. * @param $address
  1390. * @param int $coinType
  1391. * @param int $limit
  1392. * @return false
  1393. */
  1394. public function getErc20TransferLogByUser($userId, $address, $coinType = 1, $limit = 50)
  1395. {
  1396. if (empty($address)) {
  1397. $this->error = '1013';
  1398. return false;
  1399. }
  1400. if (empty($this->config['eth_api_url'])) {
  1401. $this->error = '2206';
  1402. return false;
  1403. }
  1404. if (empty($this->config['eth_api_key'])) {
  1405. $this->error = '2207';
  1406. return false;
  1407. }
  1408. try {
  1409. $cacheKey = "caches:wallets:transfer:erc_{$address}";
  1410. if (RedisService::get($cacheKey . '_lock')) {
  1411. return false;
  1412. }
  1413. RedisService::set($cacheKey . '_lock', ['config' => $this->config, 'address' => $address], rand(5, 10));
  1414. $url = sprintf($this->apiUrls['erc20_transaction_log'], $address, 1, $limit, $this->config['eth_api_key']);
  1415. $url = $this->config['eth_api_url'] . $url;
  1416. RedisService::set("caches:trades:erc20:logs_{$address}", ['url' => $url, 'params' => ['address' => $address, 'config' => $this->config, 'limit' => $limit]], 600);
  1417. $result = curl_api($url);
  1418. $result = $result ? json_decode($result, true) : [];
  1419. $status = isset($result['status']) ? $result['status'] : 0;
  1420. $datas = isset($result['result']) ? $result['result'] : [];
  1421. if ($status != 1 || empty($datas)) {
  1422. $this->error = '2207';
  1423. return false;
  1424. }
  1425. $logs = [];
  1426. $coinOutMin = ConfigService::make()->getConfigByCode('trc_out_limit');
  1427. $coinOutMin = $coinOutMin > 0 ? $coinOutMin : 0;
  1428. if ($datas) {
  1429. foreach ($datas as $v) {
  1430. $decimal = isset($v['tokenDecimal']) ? intval($v['tokenDecimal']) : 0;
  1431. $decimal = $decimal ? $decimal : 6;
  1432. $amount = isset($v['value']) ? intval($v['value']) : 0;
  1433. $amount = moneyFormat($amount / pow(10, $decimal), 6);
  1434. $time = isset($v['timestamp']) ? intval($v['timestamp']) : 0;
  1435. $txid = isset($v['hash']) ? $v['hash'] : '';
  1436. if (!CoinLogService::make()->checkExists('txid', $txid) && $time > time() - 12 * 3600) {
  1437. $balance = $this->memberModel->where(['id' => $userId])->value('usdt_num');
  1438. $orderNo = get_order_num('TW');
  1439. $log = [
  1440. 'user_id' => $userId,
  1441. 'change_type' => 2,
  1442. 'coin_type' => $coinType,
  1443. 'contact_type' => 2,
  1444. 'order_no' => $orderNo,
  1445. 'from_address' => isset($v['from']) ? $v['from'] : '',
  1446. 'to_address' => isset($v['to']) ? $v['to'] : '',
  1447. 'txid' => $txid,
  1448. 'num' => $amount,
  1449. 'balance' => $balance,
  1450. 'create_time' => $time ? $time : time(),
  1451. 'update_time' => time(),
  1452. 'status' => 2,
  1453. 'mark' => 1,
  1454. ];
  1455. if ($amount >= $coinOutMin && $this->memberModel->where(['id' => $userId])->decrement('usdt_num', $amount)) {
  1456. $this->memberModel->where(['id' => $userId])->decrement('erc_usdt', $amount);
  1457. $log['status'] = 1;
  1458. // 明细处理
  1459. $data = [
  1460. 'order_no' => $orderNo,
  1461. 'user_id' => $userId,
  1462. 'type' => 5,
  1463. 'pay_type' => 1,
  1464. 'trade_type' => 3,
  1465. 'change_type' => 2,
  1466. 'num' => $amount,
  1467. 'total' => 0,
  1468. 'balance' => floatval($balance - $amount),
  1469. 'create_time' => time(),
  1470. 'update_time' => time(),
  1471. 'remark' => '提币:' . $amount,
  1472. 'status' => 1,
  1473. 'mark' => 1,
  1474. ];
  1475. $this->capitalModel->edit($data);
  1476. }
  1477. $logs[] = $log;
  1478. $this->coinModel->insert($log);
  1479. }
  1480. }
  1481. }
  1482. return $logs;
  1483. } catch (\Exception $exception) {
  1484. $message = $exception->getMessage();
  1485. $this->error = $message;
  1486. return false;
  1487. }
  1488. }
  1489. /**
  1490. * usdt-erc2.0 归集
  1491. * @throws \Tron\Exceptions\TransactionException
  1492. * @throws \Tron\Exceptions\TronErrorException
  1493. */
  1494. public function usdtErcTrigger($force = false)
  1495. {
  1496. if (empty($this->config['eth_api_url'])) {
  1497. $this->error = '2206';
  1498. return false;
  1499. }
  1500. if (empty($this->config['eth_api_key'])) {
  1501. $this->error = '2207';
  1502. return false;
  1503. }
  1504. // 获取钱包参数
  1505. try {
  1506. // 用收账钱包归集
  1507. $otcAddress = ConfigService::make()->getConfigByCode('erc_address');
  1508. $otcAddressPrivate = ConfigService::make()->getConfigByCode('erc_private_key');
  1509. // 出账手续费钱包
  1510. $otcOutAddress = ConfigService::make()->getConfigByCode('erc_out_address');
  1511. $otcOutAddressPrivate = ConfigService::make()->getConfigByCode('erc_out_private_key');
  1512. $triggerMin = ConfigService::make()->getConfigByCode('trade_trigger_min');
  1513. $triggerTime = ConfigService::make()->getConfigByCode('trade_trigger_time');
  1514. $triggerFree = ConfigService::make()->getConfigByCode('trade_trigger_eth_free');
  1515. $triggerFree = $triggerFree > 0 ? $triggerFree : (5/pow(10, 8));
  1516. $triggerMin = $triggerMin > 0 ? $triggerMin : 0.1;
  1517. $triggerTime = $triggerTime > 0 ? $triggerTime * 86400 : 86400;
  1518. if (empty($otcAddress) || empty($otcAddressPrivate)) {
  1519. $this->error = '2203';
  1520. return false;
  1521. }
  1522. // 出账钱包
  1523. if (empty($otcOutAddress) || empty($otcOutAddressPrivate)) {
  1524. $this->error = '2203';
  1525. return false;
  1526. }
  1527. $page = RedisService::get("caches:wallet:transferRecPage");
  1528. $page = $page ? $page : 1;
  1529. // 归集时间段为凌晨0点-5点
  1530. if ((date('H:i') >= '05:00') && !$force) {
  1531. $this->error = '不在归集时间段';
  1532. return false;
  1533. }
  1534. if (RedisService::get("caches:wallet:triggerErcLock:{$page}")) {
  1535. $this->error = '不要频繁操作,30秒后重试';
  1536. return false;
  1537. }
  1538. // 上锁
  1539. RedisService::set("caches:wallet:triggerErcLock:{$page}", 1, rand(10, 30));
  1540. $addrList = MemberService::make()->getTriggerAddressList($triggerMin, $page, 200);
  1541. if (empty($addrList)) {
  1542. RedisService::set("caches:wallet:transferErcPage", 1, 600);
  1543. $this->error = '1019';
  1544. return false;
  1545. }
  1546. // 平台钱包地址
  1547. $count = 0;
  1548. $failedCount = 0;
  1549. $api = new Etherscan($this->config['eth_api_key']);
  1550. $cacheKey = "caches:wallet:triggerErc:";
  1551. foreach ($addrList as $v) {
  1552. try {
  1553. // 获取子钱包TRC-USDT余额
  1554. $userId = isset($v['id']) ? $v['id'] : 0;
  1555. $address = isset($v['trc_address']) ? $v['trc_address'] : '';
  1556. $triggerAddress = isset($v['trc_hexaddress']) ? $v['trc_hexaddress'] : '';
  1557. $addressPrivate = isset($v['trc_wif']) ? $v['trc_wif'] : '';
  1558. // 可归集的USDT余额
  1559. $result = $api->tokenBalance($triggerAddress);
  1560. $triggerUsdt = isset($result['result']) ? floatval($result['result']) : 0.00;
  1561. $triggerUsdt = $triggerUsdt ? floatval($triggerUsdt / pow(10, 6)) : '0.00';
  1562. // USDT 余额低于归集金额,则不归集
  1563. if ($triggerUsdt < $triggerMin) {
  1564. $failedCount++;
  1565. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'error' => '用户余额不足归集', 'date' => date('Y-m-d H:i:s')];
  1566. RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
  1567. continue;
  1568. }
  1569. // 获取子钱包Eth余额
  1570. $result = $api->balance($triggerAddress);
  1571. $triggerEth = isset($result['result']) ? floatval($result['result']) : 0.00;
  1572. $triggerEth = $triggerEth ? floatval($triggerEth / pow(10, 18)) : '0.00';
  1573. // 获取平台出账钱包ETH余额
  1574. $result = $api->balance($otcOutAddress);
  1575. $otcOutEthTotal = isset($result['result']) ? floatval($result['result']) : 0.00;
  1576. $otcOutEthTotal = $otcOutEthTotal ? floatval($otcOutEthTotal / pow(10, 18)) : '0.00';
  1577. // 如果子钱包和平台钱包TRX余额不足手续费,则不归集
  1578. if ($triggerEth < $triggerFree && $otcOutEthTotal < $triggerFree) {
  1579. $failedCount++;
  1580. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerTrx' => $triggerEth, 'otcTrx' => $otcOutEthTotal, 'error' => '平台钱包ETH手续费不足', 'date' => date('Y-m-d H:i:s')];
  1581. RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
  1582. continue;
  1583. }
  1584. // 如果子钱包ETH不足手续费5个gwei,平台ETH充足则自动充值后下次调用时归集
  1585. if ($triggerEth < $triggerFree) {
  1586. $failedCount++;
  1587. $result = $this->ercTransfer($address, $triggerFree);
  1588. $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerEth' => $triggerEth, 'transfer' => $result, 'error' => '归集钱包ETH手续费不足,先充值', 'date' => date('Y-m-d H:i:s')];
  1589. RedisService::set($cacheKey . "U_{$userId}:catch", $error, 7200);
  1590. continue;
  1591. }
  1592. // 满足归集条件处理
  1593. $result = $this->usdtErcTransfer($otcOutAddress, $triggerUsdt, $triggerAddress, $addressPrivate);
  1594. RedisService::set($cacheKey . "U_{$userId}:result", ['data' => $v, 'result' => $result, 'msg' => '归集已提交', 'date' => date('Y-m-d H:i:s')], 7200);
  1595. $count++;
  1596. } catch (\Exception $exception) {
  1597. $failedCount++;
  1598. RedisService::set($cacheKey . "U_{$userId}:exception", ['data' => $v, 'msg' => $exception->getMessage(), 'date' => date('Y-m-d H:i:s')], 7200);
  1599. }
  1600. }
  1601. // 超出分页数,下次处理下一页
  1602. if (count($addrList) >= 200) {
  1603. RedisService::set("caches:wallet:transferErcPage", $page + 1, 600);
  1604. }
  1605. if ($count > 0) {
  1606. return ['success' => $count, 'fail' => $failedCount];
  1607. } else {
  1608. $this->error = 1021;
  1609. return false;
  1610. }
  1611. } catch (\Exception $exception) {
  1612. $this->error = $exception->getMessage();
  1613. return false;
  1614. }
  1615. }
  1616. }