UsdtWalletService.php 79 KB

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