eth_getLogsor want to learn more information about it? You are in the right place.
eth_getLogshas 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_getLogsto 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_getLogsand then take actions based off those results. For example, if a purchase is being made using crypto payments, we can use
eth_getLogsto see if the sender successfully made the payment before providing the item purchased.
addressfield 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
eventslisted in this ABI: the
"Transfer"event, and the
"NewOwner"event, but you can see the full contract ABI here.
anonymousrefers to whether or not the method is exposed publicly (if it's
falsethen it is public)
typespecifies what the data type is
namefield is the name of the item or parameter
indexedfurther down in the Deciphering the Response section
Transfer(from, to, value). We can see in the ABI snippet above that this contract has a Transfer event defined in its ABI. The
toinputs are stored as
addresses, and the
valueinput is stored as a
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_sha3JSON-RPC call to get the corresponding hash. For "Transfer(address,address,uint256)", the corresponding hex value is
Transferevent in order to understand logs better.
Transfer(address,address,uint256)method defined in its ABI. If this
Transfermethod is called on the contract by someone who wants to make a transfer, the contract should emit an
event()/logthat contains information about the transfer.
paramshere we have specified the
toBlockparams 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
addressfield represents the address of the contract emitting the log.
Topicsis an ordered array of data. Notice how the first item in the
topicsfield 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
0x54a2d42a40f51259dedd1978f6c118a0f0eff078(the second and third topics).
data", and "
topicsfield in our request already, but the
datafield is new. Let's break each of these down.
topicsfield 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
topicsfield rather than the
datafield 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
toaddresses (in that order).
value" input, the
uint256will instead go into the
datafield since it has
"indexed":falsein the contract ABI.
valueis of type
uint256we can translate the data
5,000,000,000. So this transaction reads: transfer
decimalswhich 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
decimalsvalue 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.