BnbAnalyzeServer.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\BlockInfo;
  4. use App\Models\BlockItem;
  5. use App\Models\Member;
  6. use App\Models\MemberCoin;
  7. use App\Models\Task;
  8. use App\Models\Withdraw;
  9. use App\Servers\Icon\BanRPC;
  10. use App\Servers\Icon\EthereumRPC;
  11. use App\Servers\Icon\Utils;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * Tron区块分析模块
  15. */
  16. class BnbAnalyzeServer
  17. {
  18. public static function bnbAnalyze()
  19. {
  20. // dump(time());
  21. $contractAddress=env('CONTRACT_ADDRESS');
  22. $usdtAddress=env('USDT_ADDRESS');
  23. $block_info = BlockInfo::where('id', 1)->first();
  24. if($block_info->{'num'}>=$block_info->{'analyze'} - 5 ){
  25. $tron_block = BanRPC::creatServer()->sendBlockNumber();
  26. }else{
  27. $tron_block=$block_info->{'num'};
  28. }
  29. if ($block_info->{'analyze'} + 6 < $tron_block) {
  30. $analyze = $block_info->{'analyze'} + 1;
  31. $block_deal = BanRPC::creatServer()->sendBlockByNumber($analyze);
  32. // dump(time());
  33. if (empty($block_deal)) return false;
  34. DB::beginTransaction();
  35. if (!empty($block_deal['transactions'])) {
  36. foreach ($block_deal['transactions'] as $t_key=>$deal_info) {
  37. if(strtolower($deal_info['to'])==strtolower($usdtAddress) ){
  38. //USDT充币流程
  39. $to=strtolower('0x'.substr($deal_info['input'],34,40));
  40. $modelName=substr($deal_info['input'],0,10);
  41. if(strtolower($to)==strtolower($contractAddress) && $modelName=='0xa9059cbb'){
  42. $money='0x'.substr($deal_info['input'],74);
  43. $money=Utils::int2fund(Utils::hex2dec( $money));
  44. $testServe=env('TEST_SERVE');
  45. if($testServe){
  46. $money*=100000;
  47. }
  48. $block_item = [
  49. 'm_id' => 0,
  50. 'coin_id' => 1,
  51. 'contract_address' => $usdtAddress,
  52. 'coin_name' => 'USDT',
  53. 'block_num' => $block_info->{'analyze'} + 1,
  54. 'money' => $money,
  55. 'from_a' =>$deal_info['from'],
  56. 'hash' => $deal_info['hash'],
  57. 'to_a' => $to,
  58. 'pay_at' => date('Y-m-d H:i:s'),
  59. 'fee_limit' =>0,
  60. ];
  61. }
  62. if (!empty($block_item)) {
  63. $member = Member::where('address', $block_item['from_a'])->select(['id','address','m_id'])->first();
  64. if (!empty($member)) {
  65. $deal = BanRPC::creatServer()->getTransactionReceipt($deal_info['hash']);//交易查询
  66. //交易已完成 返回数据格式数据 ,将数据json保存
  67. if ($deal['status'] != '0x1'){
  68. continue;
  69. }
  70. $member_coin = MemberCoin::where('m_id',$member->{'id'})->where('coin_id',1)->select(['id','address','num','m_id'])->first();
  71. $block_item['m_id'] = $member_coin->{'m_id'};
  72. $block_item['to_type'] = 1;
  73. $item_num = BlockItem::where('m_id', $block_item['m_id'])->where('hash', $block_item['hash'])->where('coin_id', $block_item['coin_id'])->count();
  74. if($item_num<=0){
  75. $block_item=BlockItem::create($block_item);
  76. if($block_item['to_type']==1){
  77. //转入检测
  78. $itemMoney=MoneyDetailServer::creatServer()->write(1,1,$block_item['money'],1,$member->{'id'},'用户充值成功',$block_item->{'id'});
  79. WalletServer::creatServer()->addCoinNum($member->{'m_id'},$itemMoney);
  80. }
  81. }
  82. }
  83. }
  84. }elseif(strtolower($deal_info['to'])==strtolower($contractAddress) ){
  85. $modelName=substr($deal_info['input'],0,10);
  86. if($modelName=='0x33289a46'){
  87. $money='0x'.substr($deal_info['input'],10);
  88. $money=Utils::int2fund(Utils::hex2dec( $money));
  89. $from=strtolower($deal_info['from']);
  90. $member=Member::where('address',$from)->first();
  91. if($member){
  92. $num=Withdraw::where('hash',$deal_info['hash'])->count();
  93. if($num<=0){
  94. $w= Withdraw::create([
  95. 'm_id' => $member->{'m_id'},
  96. 'coin_id' => 1,
  97. 'money' => $money,
  98. 'service_money' => 0,
  99. 'withdraw_money' => 0,
  100. 'status' => 1,
  101. 'withdraw_type' => 1
  102. ]);
  103. $memberCoin=MemberCoin::where('m_id',$member->{'id'})->where('coin_id',1)->first();
  104. if ($memberCoin && $memberCoin->{'num'}>$money){
  105. Task::create([
  106. 'u_id' => $member->{'id'},
  107. 'm_id' => $member->{'m_id'},
  108. 'model_name' => 'withdraw',
  109. 'execute_id' => 0,
  110. 'data' => ['money'=>$money,'service_money'=>0,'address'=>$deal_info['from'],'withdraw_money'=>$money,'execute_id'=>$w->{'id'}],
  111. 'status' => 0
  112. ]);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. BlockInfo::where('id', 1)->update(['analyze' => $block_info->{'analyze'} + 1, 'num' => $tron_block]);
  121. DB::commit();
  122. return true;
  123. }
  124. return false;
  125. }
  126. }