eth_getLogs
or want to learn more information about it? You are in the right place. eth_getLogs
has many beneficial use cases that developers are often times unaware of. It also has some extreme vulnerabilities that can have huge consequences if you don't use it correctly. This page is a deep dive into the capabilities of eth_getLogs
to help you improve your usage and understanding of this method! For details about the request/response specifications for eth_getLogs
, check out our JSON-RPC reference page.eth_getLogs
and then take actions based off those results. For example, if a purchase is being made using crypto payments, we can use eth_getLogs
to see if the sender successfully made the payment before providing the item purchased.address
field of the contract in the search bar, then clicking on the "contract" tab and scrolling down to "Contract ABI". See below for guidance:0xb59f67A8BfF5d8Cd03f6AC17265c550Ed8F33907
, which we will be using in our example. Here we have just included the two events
listed in this ABI: the "Transfer"
event, and the "NewOwner"
event, but you can see the full contract ABI here.anonymous
refers to whether or not the method is exposed publicly (if it's false
then it is public)type
specifies what the data type isevents
named "Transfer"
and "NewOwner"
"address"
and "uint256"
name
field is the name of the item or parameterindexed
further down in the Deciphering the Response sectionTransfer(from, to, value)
. We can see in the ABI snippet above that this contract has a Transfer event defined in its ABI. The from
and to
inputs are stored as addresses
, and the value
input is stored as a uint256
.Transfer
and NewOwner
.keccak256(Transfer(address,address,uint256))
, which results in the hash: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
. If you would like to reproduce the hash yourself you can use this online keccak-256 converter and input "Transfer(address,address,uint256)". Or, convert the string to hexadecimal number and use the web3_sha3
JSON-RPC call to get the corresponding hash. For "Transfer(address,address,uint256)", the corresponding hex value is0x5472616e7366657228616464726573732c616464726573732c75696e7432353629
.Transfer
event in order to understand logs better.Transfer(address,address,uint256)
method defined in its ABI. If this Transfer
method is called on the contract by someone who wants to make a transfer, the contract should emit an event()/log
that contains information about the transfer.eth_getLogs
has extreme vulnerabilities? Here's what we mean. When you make a request to eth_getLogs
, all parameters are optional, meaning you don’t actually have to specify fromBlock
, toBlock
, address
, topics
, or blockHash
(learn more about each parameter in our JSON-RPC Reference page). However, if we leave these parameters empty, or specify too large of a range, we can risk trying to query millions of logs, both overloading the node and creating a massive payload that will be extremely difficult to return. This can result in huge consequences if the right safety nets are not put in place. Luckily, Alchemy has systems in place to prevent users from making these extreme requests, but if you are running your own node you might not be so lucky.eth_getLogs
requests with up to a 2K block range and 150MB limit on the response size eth_getLogs
:params
here we have specified the fromBlock
, toBlock
, address
, and topics
.blockHash
in our params
is because you can only use either fromBlock
and toBlock
or blockHash
, not both. Learn more about this specification here.fromBlock
and toBlock
params specify the start and end block numbers to restrict the search by, these are important to specify so we search over the correct blocks. The address
field represents the address of the contract emitting the log.Topics
is an ordered array of data. Notice how the first item in the topics
field above matches the event signature of our Transfer(address,address,uint256)
event in the previous section. This means we are specifically querying for a Transfer event between address 0xb46c2526e227482e2ebb8f4c69e4674d262e75
and 0x54a2d42a40f51259dedd1978f6c118a0f0eff078
(the second and third topics).[]
“anything”[A]
“A in first position (and anything after)”[null, B]
“anything in first position AND B in second position (and anything after)”[A, B]
“A in first position AND B in second position (and anything after)”[[A, B], [A, B]]
“(A OR B) in first position AND (A OR B) in second position (and anything after)”data
", and "topics
".topics
field in our request already, but the data
field is new. Let's break each of these down.topics
field can contain up to 4 topics. The first topic is required and will always contain the keccak 256 hash of the event signature. The other three topics are optional and typically used for indexing and provide a faster lookup time than using the data field described below.from
" and "to
" inputs have "indexed": true
. This means that these addresses will be stored in the topics
field rather than the data
field when the event gets fired off. Remember, the first topic is the event signature for this log which means the other two topics are the from
and to
addresses (in that order).value
" input, the uint256
will instead go into the data
field since it has "indexed":false
in the contract ABI.value
is of type uint256
we can translate the data 0x12a05f200
to 5,000,000,000
. So this transaction reads: transfer 5,000,000,000
from address 0xb46c2526e227482e2ebb8f4c69e4674d262e75
to address 0x54a2d42a40f51259dedd1978f6c118a0f0eff078
.decimals
which indicates the conversion from the base unit to the more common unit or token, specifying how much you should divide by to get the actual value. In this case, the decimals
value is 3 so you divide the given value by 10^3, which makes our true amount 5,000,000
. You can see the decimals value for this contract on Etherscan.eth_getLogs!