1

It seems that web3js has a getPastEvents() method (link) which returns all previous events for a given contract.

Is there a similar feature in web3.py?

I want to find all previous Transfer events of an ERC20 contract to keep a database of holders and their balance.

1 Answer 1

3

The get_logs() function does this.

For example:

contract = web3.eth.contract(address=Web3.toChecksumAddress(CONTRACT_ADDRESS), abi=ABI)
events = contract.events.Transfer.getLogs(fromBlock=CONTRACT_CREATION_BLOCK)
1
  • How to do the same without initializing a contract instance? I would like to get all logs for a certain block in the past, and then parse/filter them. It seems that WS subscription is capable of receiving only the latest blocks/logs.
    – remort
    Commented Sep 1, 2023 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.