TronAnalyzeServer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\BlockInfo;
  4. use App\Models\BlockItems;
  5. use App\Models\CoinType;
  6. use App\Models\Config;
  7. use App\Models\Member;
  8. use App\Models\MemberCoins;
  9. use App\Servers\Icon\TronRPC;
  10. use App\Servers\Icon\Utils;
  11. use Illuminate\Support\Facades\DB;
  12. /**
  13. * Tron区块分析模块
  14. */
  15. class TronAnalyzeServer
  16. {
  17. public static function tronAnalyze()
  18. {
  19. $block_info = BlockInfo::where('id', 1)->first();
  20. if($block_info->{'num'}>=$block_info->{'analyze'} - 5 || true){
  21. $tron_block = TronRPC::CreationTron()->getBlockByNum();
  22. }else{
  23. $tron_block=$block_info->{'num'};
  24. }
  25. if ($block_info->{'analyze'} + 6 < $tron_block) {
  26. $analyze = $block_info->{'analyze'} + 1;
  27. $block_deal = TronRPC::CreationTron()->getBlockById($analyze);
  28. if (empty($block_deal)) return false;
  29. DB::beginTransaction();
  30. if (!empty($block_deal['transactions'])) {
  31. //USDT 合约地址 0xdac17f958d2ee523a2206206994597c13d831ec7
  32. $hy_address = '41a614f803b6fd780986a42c78ec9c7f77e6ded13c';
  33. foreach ($block_deal['transactions'] as $t_key=>$deal_info) {
  34. if(empty($deal_info['ret'][0]['contractRet']) || $deal_info['ret'][0]['contractRet']!='SUCCESS'){
  35. continue;
  36. }
  37. $block_item = [];
  38. $pay_data=$deal_info['raw_data']['contract'][0]['parameter']['value'];
  39. $my_hy_address=empty($pay_data['contract_address']) ?'':$pay_data['contract_address'];
  40. if ( $my_hy_address == $hy_address) {
  41. if(empty($pay_data['data']))continue;
  42. $input = $pay_data['data'];
  43. $to = '41'.substr($input, 32, 40);
  44. $value = substr($input, 72);
  45. $money = Utils::int2fund(Utils::hex2dec('0x' . $value), 6);
  46. $to = TronRPC::CreationTron()->getBase58CheckAddress(hex2bin($to));
  47. $block_item = [
  48. 'm_id' => 0,
  49. 'coin_id' => 4,
  50. 'coin_name' => 'USDT',
  51. 'block_num' => $block_info->{'analyze'} + 1,
  52. 'money' => $money,
  53. 'from_a' => TronRPC::CreationTron()->getBase58CheckAddress(hex2bin($pay_data['owner_address'])),
  54. 'hash' => $deal_info['txID'],
  55. 'to_a' => $to,
  56. 'pay_at' => date('Y-m-d H:i:s'),
  57. 'fee_limit' =>empty($deal_info['raw_data']['fee_limit'])?0:$deal_info['raw_data']['fee_limit'],
  58. ];
  59. }
  60. if (!empty($block_item)) {
  61. $member_coin = Member::whereIn('address', [$block_item['to_a'],$block_item['from_a']])->select(['id','address'])->first();
  62. if (!empty($member_coin)) {
  63. $block_item['m_id'] = $member_coin->{'id'};
  64. $block_item['to_type'] = $block_item['to_a']==$member_coin->{'address'}?1:2;
  65. $item_num = BlockItems::where('m_id', $block_item['m_id'])->where('hash', $block_item['hash'])->where('coin_id', $block_item['coin_id'])->count();
  66. if($item_num<=0){
  67. BlockItems::create($block_item);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. BlockInfo::where('id', 1)->update(['analyze' => $block_info->{'analyze'} + 1, 'num' => $tron_block]);
  74. DB::commit();
  75. return true;
  76. }
  77. return false;
  78. }
  79. }