Web3.py Transactions – How to Get List of Transactions History for a Specific Contract Address Using Web3

addressesblockchaintokenstransactionsweb3.py

I really don't know how to get a list of transactions history about specific contract address using web3.py

and I use Infura HTTPProvider

I just have only one 'contract address'.

Can i get information about this contract address using web3.py?

Best Answer

Getting all transaction history requires you to iterate over each and every block since the contract creation. As you might guess, this would be extremely slow, plus it depends on your Infura plan: rate limits and number of historical blocks available.

Faster Solution without web3py

Instead, you can use indexing services, such as Etherscan.

As explained in API docs, you can then query for transaction history in the following way:

https://api.etherscan.io/api?module=account&action=txlist&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=YourApiKeyToken

Slower solution with web3py

If your Infura plan allows it, you can iterate over blocks and get block details using eth_getBlockByNumber JSON-RPC method. In web3py, it is equivalent to web3.eth.getBlock(blocknumber). Then you would iterate over transaction hashes using web3.eth.getTransaction(txhash) and check for from/to address against your contract address. See:

https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.getTransaction

https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.getBlock