Txmx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use app\common\model\GasLog;
  6. use blockchain\TronService;
  7. use blockchain\Web3Service;
  8. use fast\Random;
  9. use think\Config;
  10. use think\Log;
  11. use think\Session;
  12. use think\Validate;
  13. use app\common\model\Txjl;
  14. use think\Db;
  15. use Tron\Address;
  16. /**
  17. * 个人配置
  18. *
  19. * @icon fa fa-user
  20. */
  21. class Txmx extends Backend
  22. {
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. //设置过滤方法
  29. $this->request->filter(['strip_tags']);
  30. if ($this->request->isAjax()) {
  31. $model = model('Txjl');
  32. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  33. $total = $model
  34. ->where($where)
  35. ->where('admin_id', $this->auth->id)
  36. ->order($sort, $order)
  37. ->count();
  38. $list = $model
  39. ->where($where)
  40. ->where('admin_id', $this->auth->id)
  41. ->order($sort, $order)
  42. ->limit($offset, $limit)
  43. ->select();
  44. $result = array("total" => $total, "rows" => $list);
  45. return json($result);
  46. }
  47. $admin = Admin::get($this->auth->id);
  48. Session::set("admin", $admin->toArray());
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 更新个人信息
  53. */
  54. public function update()
  55. {
  56. if ($this->request->isPost()) {
  57. $params = $this->request->post("row/a");
  58. if ($params) {
  59. $site=config('site');
  60. $money=$params['money'];
  61. if($money<$site['zstx']){
  62. $this->error($site['txbz']);
  63. }
  64. if(!$params['type']){
  65. $this->error('请填写提现类型');
  66. }
  67. if(!$params['name']){
  68. $this->error('请填写提现名字');
  69. }
  70. if(!$params['cord']){
  71. $this->error('请填写提现帐号');
  72. }
  73. $user_id=$this->auth->id;
  74. $vadmin=Db::name('admin')->where('id',$user_id)->find();
  75. if ($vadmin && $money != 0) {
  76. if($vadmin['money']<$money){
  77. $this->error('余额不足');
  78. }
  79. Db::startTrans();
  80. try {
  81. $moneys=$money;
  82. //更新会员信息
  83. $before = $vadmin['money'];
  84. $after = $vadmin['money'] - $moneys;
  85. Db::name('admin') ->where('id',$user_id)->setDec('money', $moneys);
  86. //写入日志
  87. Db::name('admin_money_log')->insertGetId(['admin_id' =>$user_id, 'money' => $moneys,'createtime' => time(),'before' => $before, 'after' => $after, 'memo' => '用户提现']);
  88. $site['txsxf']=isset($site['txsxf'])?$site['txsxf']:0;
  89. $sxf=$money*$site['txsxf'];
  90. $up=[
  91. 'admin_id' => $user_id,
  92. 'type' => $params['type'],
  93. 'name' => $params['name'],
  94. 'cord' => $params['cord'],
  95. 'money' => $money,
  96. 'moneydz' => $money-$sxf,
  97. 'sxf' => $sxf,
  98. 'iscl' => 1,
  99. 'islx' => 2,
  100. 'memo' => '后台用户提现'
  101. ];
  102. //写入提现记录
  103. Txjl::create($up);
  104. $admin = Admin::get($this->auth->id);
  105. Session::set("admin", $admin->toArray());
  106. Db::commit();
  107. $this->success('提交成功');
  108. }catch (Exception $e){
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. }else{
  113. $this->error(__('金额不对'));
  114. }
  115. }
  116. $this->error();
  117. }
  118. return;
  119. }
  120. // 归集(把所有用户的USDT归集到指定地址)
  121. public function summary(){
  122. $trcUsdtAddress= config('blockchain.trc.tokens.usdt.gas_address');
  123. // $trcUsdtAddress = Config::get('blockchain.trc.tokens.usdt.address');
  124. $trcgasAddress = Config::get('blockchain.trc.tokens.usdt.gas_address');
  125. $bscUsdtAddress = Config::get('blockchain.bsc.tokens.usdt.address');
  126. $bscgasAddress = Config::get('blockchain.bsc.tokens.usdt.gas_address');
  127. var_dump($trcUsdtAddress);
  128. var_dump($trcgasAddress);
  129. var_dump($bscUsdtAddress);
  130. var_dump($bscgasAddress);die();
  131. $list = Db::name('user_usdt_address')->where('last_recharge','>',0)->select();
  132. foreach ($list as $key => $value) {
  133. $trx_money_info = [
  134. 'address' => $value['trx_address'],
  135. 'hexAddress' => $value['trx_16_address'],
  136. 'privateKey' => $value['trx_key '],
  137. ];
  138. $this->trx_summary($trx_money_info,$trcUsdtAddress,$trcgasAddress );
  139. $bsc_money_info = [
  140. 'address' => $value['bsc_address'],
  141. 'privateKey' => $value['bsc_address '],
  142. ];
  143. $this->bsc_summary($bsc_money_info,$bscUsdtAddress,$bscgasAddress );
  144. }
  145. }
  146. public function trx_summary(array $money_info, string $summary_address, Address $gas_address): bool
  147. {
  148. //实例化TRON
  149. $service = TronService::instance('usdt');
  150. //实例化出款地址[有Usdt余额的用户钱包]
  151. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  152. //获取代币余额
  153. $balance = $service->tokenBalance($money_info['address']);
  154. //USDT余额大于0,触发归集
  155. if ($balance > 0) {
  156. //查询地址中主币【GAS费余额】
  157. $trx_balance = $service->balance($money_info['address']);
  158. if ($trx_balance < 30) {
  159. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  160. //计算要达到30个trx需要再转几个给用户地址
  161. $trade_trx = ceil(30 - $trx_balance);
  162. //处理手续费转账,从系统地址分配gas给用户
  163. //查询系统地址的TRX余额
  164. $gas_trx_balance = $service->balance($gas_address->address);
  165. //系统地址TRX余额不足,退出处理
  166. if ($gas_trx_balance < $trade_trx + 2) {
  167. Log::error('手续费账户余额不足');
  168. return false;
  169. }
  170. try {
  171. //执行转账
  172. $res = $service->transfer($gas_address, $address, $trade_trx);
  173. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'trx']);
  174. if (isset($res->txID)) {
  175. Log::error('转账TRX交易哈希:' . $res->txID);
  176. return true;
  177. } else {
  178. Log::error('转账TRX失败');
  179. return false;
  180. }
  181. } catch (\Exception $e) {
  182. Log::error('转账TRX失败' . $e->getMessage());
  183. return false;
  184. }
  185. } else {
  186. //账户内TRX充足,执行USDT转账交易
  187. try {
  188. //将账户内的USDT余额转入归集指定地址
  189. $res = $service->tokenTransfer($address, new Address($summary_address), $balance);
  190. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  191. if ($res['status']) {
  192. Log::error('转账交易哈希:' . $res['data']->txID);
  193. return true;
  194. } else {
  195. Log::error('转账失败' . $res['msg']);
  196. return false;
  197. }
  198. } catch (\Exception $e) {
  199. Log::error('转账失败' . $e->getMessage());
  200. return false;
  201. }
  202. }
  203. }
  204. return true;
  205. }
  206. public function bsc_summary(array $money_info, string $summary_address, Address $gas_address): bool
  207. {
  208. //实例化TRON
  209. $service = Web3Service::instance('bsc', 'usdt', '');
  210. //实例化出款地址[有Usdt余额的用户钱包]
  211. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  212. //获取代币余额
  213. $balance = $service->getTokenBalance($money_info['address']);
  214. //USDT余额大于0,触发归集
  215. if ($balance > 0) {
  216. //查询地址中主币【GAS费余额】
  217. $trx_balance = $service->getBalance($money_info['address']);
  218. if ($trx_balance < 0.001) {
  219. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  220. //计算要达到30个trx需要再转几个给用户地址
  221. $trade_trx = ceil(30 - $trx_balance);
  222. //处理手续费转账,从系统地址分配gas给用户
  223. //查询系统地址的TRX余额
  224. $gas_trx_balance = $service->getBalance($gas_address->address);
  225. //系统地址TRX余额不足,退出处理
  226. if ($gas_trx_balance < $trade_trx + 2) {
  227. Log::error('手续费账户余额不足');
  228. return false;
  229. }
  230. try {
  231. //执行转账
  232. $res = $service->transfer($address, $trade_trx);
  233. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'bsc']);
  234. if (isset($res->txID)) {
  235. Log::error('转账BSC交易哈希:' . $res->txID);
  236. return true;
  237. } else {
  238. Log::error('转账BSC失败');
  239. return false;
  240. }
  241. } catch (\Exception $e) {
  242. Log::error('转账BSC失败' . $e->getMessage());
  243. return false;
  244. }
  245. } else {
  246. //账户内TRX充足,执行USDT转账交易
  247. try {
  248. //将账户内的USDT余额转入归集指定地址
  249. $res = $service->transferToken($address, new Address($summary_address), $balance);
  250. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  251. if ($res['status']) {
  252. Log::error('转账交易哈希:' . $res['data']->txID);
  253. return true;
  254. } else {
  255. Log::error('转账失败' . $res['msg']);
  256. return false;
  257. }
  258. } catch (\Exception $e) {
  259. Log::error('转账失败' . $e->getMessage());
  260. return false;
  261. }
  262. }
  263. }
  264. return true;
  265. }
  266. }