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.

merc1er
  • 143
  • 8

1 Answers1

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)
merc1er
  • 143
  • 8
  • 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 Sep 01 '23 at 20:00