Json-Rpc

Infura getLogs 返回空結果

  • January 4, 2020

我正在Infura上使用 PHP 編寫一個基於加密的系統,現在我正在編寫一個腳本來檢索特定地址的日誌。這是我的程式碼:

$ch = curl_init($my_server_url);

$data = array(
   'jsonrpc' => '2.0',
   'id' => 1,
   'method' => 'eth_getLogs',
   'params' => array(array('address'=>'0x7d8f5F4ACa6Ca7Eb857A9F36d46b0aA715a88849'))
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

但是在執行結束時什麼都沒有返回,而我在這個地址上有 2 筆交易。我只收到這個最終結果:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": []
}

你能告訴我我的錯誤在哪裡嗎?

事件是由合約發出的,但您使用的地址0x7d8f5F4ACa6Ca7Eb857A9F36d46b0aA715a88849不是合約。但該地址有三個相關的 Erc20 Token 交易。

假設您希望查看相關合約0xdac17f958d2ee523a2206206994597c13d831ec7中的事件。

您必須使用您的特定帳戶過濾主題,如下例所示:

   <?php 
   $my_server_url = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_KEY";
   $ch = curl_init($my_server_url);
   $data = array(
       'jsonrpc' => '2.0',
       'id' => 1,
       'method' => 'eth_getLogs',
       'params' => array(array(
           "fromBlock" => "0x0",
           "toBlock"   => "latest",
           "topics" => array(null, null, "0x0000000000000000000000007d8f5F4ACa6Ca7Eb857A9F36d46b0aA715a88849"),
           "address"   => "0xdAC17F958D2ee523a2206206994597C13D831ec7"
       ))
   );
   $payload = json_encode($data);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $result = curl_exec($ch);
   curl_close($ch);
   echo $result;

結果將是:

 {
   "jsonrpc": "2.0",
   "id": 1,
   "result": [
     {
       "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
       "blockHash": "0x688cdbabf192c897d54ccee88e8523b69fc6a52929670206f2596c969c416e78",
       "blockNumber": "0x8c39ab",
       "data": "0x00000000000000000000000000000000000000000000000000000000002dc6c0",
       "logIndex": "0x3",
       "removed": false,
       "topics": [
         "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
         "0x0000000000000000000000003f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be",
         "0x0000000000000000000000007d8f5f4aca6ca7eb857a9f36d46b0aa715a88849"
       ],
       "transactionHash": "0x4d8f2afd9a312bc93d357f36be845675d3531d2f05b476e8789ec22ef57e5fbe",
       "transactionIndex": "0x4"
     },
     {
       "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
       "blockHash": "0x94e9257a373e5a7bb84ef035744d8aca0a29d0ebbc18f252fc39920dc0f37935",
       "blockNumber": "0x8c95c0",
       "data": "0x00000000000000000000000000000000000000000000000000000000001e8480",
       "logIndex": "0x5",
       "removed": false,
       "topics": [
         "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
         "0x000000000000000000000000e8670d7251573033b95065e0e2d6db9b08fdb683",
         "0x0000000000000000000000007d8f5f4aca6ca7eb857a9f36d46b0aa715a88849"
       ],
       "transactionHash": "0x281fed746f05f9fad14d8ab4fb9172b50199927fd70a7ee196bda3df100da893",
       "transactionIndex": "0x7"
     },
     {
       "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
       "blockHash": "0x55fb9aeb0a66243117ef3a9295b7c06b0a4b8e139dc6312946cf7895b4acb19b",
       "blockNumber": "0x8c971b",
       "data": "0x00000000000000000000000000000000000000000000000000000000000f4240",
       "logIndex": "0x67",
       "removed": false,
       "topics": [
         "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
         "0x000000000000000000000000e8670d7251573033b95065e0e2d6db9b08fdb683",
         "0x0000000000000000000000007d8f5f4aca6ca7eb857a9f36d46b0aa715a88849"
       ],
       "transactionHash": "0x46e804c7acaaf3e86fc257f676fb552ce7176fdd4462a8acb252cbd791a594e8",
       "transactionIndex": "0x82"
     }
   ]
 }

引用自:https://ethereum.stackexchange.com/questions/78706