get('erc20usdt_monitor_fid'); if (blank($fid)) { $fid = $this->newFilter($web3, $cb); } try { $web3->eth->getFilterLogs($fid, $cb); $logs = $cb->result; } catch (\RuntimeException $e) { info($e); if ($e->getCode() == 32000) { // 过滤器被卸载 重新创建 $fid = $this->newFilter($web3, $cb); $web3->eth->getFilterLogs($fid, $cb); $logs = $cb->result; } else { break; } } echo count($logs) . '--' . datetime() . "\r\n"; if (count($logs) > 0) { // 监听地址列表 $address_list = \App\Models\UserWallet::query()->where('coin_id', 1)->where(function ($q) { $q->whereNotNull('wallet_address')->where('wallet_address', '<>', ''); })->pluck('wallet_address')->toArray(); foreach ($logs as $log) { $log = get_object_vars($log); if (!isset($log['topics'][2])) { info(json_encode($log)); continue; } $to = '0x' . substr($log['topics'][2], 26); if (in_array($to, $address_list)) { $txid = $log['transactionHash']; // 交易hash $logIndex = $log['logIndex']; // 确认交易是否已经存在; $is_exist = USDTTransactions::query()->where('transactionHash', $txid)->exists(); if ($is_exist) { // 已存在 continue; } else { if (isset($log['data']) && !blank($log['data'])) { $value = hexdec(ltrim(Utils::stripZero($log['data']), '0')) / pow(10, 6); $data = [ 'address' => $log['address'], 'topics' => $log['topics'][0], 'data' => $log['data'], 'blockNumber' => $log['blockNumber'], 'transactionHash' => $log['transactionHash'], 'transactionIndex' => $log['transactionIndex'], 'blockHash' => $log['blockHash'], 'logIndex' => $log['logIndex'], 'removed' => $log['removed'], 'from' => '0x' . substr($log['topics'][1], 26), 'to' => $to, ]; $data['amount'] = $value; $data['symbol'] = 'ETH_USDT'; $data['txid'] = $log['transactionHash']; $data['status'] = 0; $data['type'] = 'deposit'; USDTTransactions::query()->create($data); \App\Jobs\Deposit::dispatch($data)->onQueue('deposit'); } } } } } sleep(5); } } private function newFilter($web3, $cb) { $web3->eth->newFilter([ // "fromBlock"=> Utils::toHex(10859780,true), // "toBlock"=> Utils::toHex(10859780,true), "fromBlock" => "latest", "toBlock" => "latest", 'address' => '0xdac17f958d2ee523a2206206994597c13d831ec7', // usdt合约地址 [ "topics" => ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] ] ], $cb); $fid = $cb->result; Cache::store('redis')->put('erc20usdt_monitor_fid', $fid, 86400); return $fid; } }