13

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?

1
  • The question says web3.py. The tagging says web3.js. Oct 12, 2020 at 9:37

1 Answer 1

10

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

1
  • 3
    account is not contract. This gives you the list of transactions for an account, not the list of all transaction with a smart contract.
    – shredding
    Apr 10, 2022 at 9:28

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.