Stack Exchange
How to Decode Transaction Input Data using Etherscan API in Python
am trying to decode the Transaction Input Data using Etherscan API in Python, I tried different answers on this, but still can't do it. Maybe someone can Help me figure it out, my Code so far:
transactions_url = "https://api.etherscan.io/api?module=account&action=txlist&address=0x7666ca32ef844ff435506568f66d6de6792e8425&startblock=0&endblock=99999999&sort=asc&apikey={myApiKey}"
response = get(transactions_url)
data = response.json()["result"]
data.sort(key=lambda x: int(x["timeStamp"]))

for tx in data:
    inputdata = tx['input']
    print(inputdata)

My input Data is
 inputdata = 0x246bb536000000000000000000000000000000000000000000000000000023404b7778c00000000000000000000000000000000000000000000000000000000000000064

I want this to be Decoded into:
#   Name    Type    Data
0   _satoshiAmount  uint256 38759051000000
1   _duration   uint256 100


to do that, for each transaction, you need the ABI of smart contract that the transaction interacted with.

thanks, do you have any example how to bind the abi of smart contract in this example please?

different transactions interact with different smart contracts. to get the ABI for a specific smart contract, you can also use the Etherscan API.

once you have the ABI, there's plenty of examples how to decode the Tx data using the ABI.

thanks for your answers, but I posted this question because I didn't find any example that explains it, tried different examples but none worked, i don't want to use infura or any other 3rd party api, wanted to solve it only with Etherscan API

I wouldn't give much hope your question will be answered as it's posted here, it's basically a programming project that should be a paid work if you want this to be implemented. suggest that you reduce the scope of your question and/or clarify what aspects you get stuck in.

It's not perfect, but it'll get you the data you're looking for without needing to connect to a provider node. Answer edited to retrieve the ABI from Etherscan in the code.
import json
from requests import get
from web3 import Web3
from other_file import ETHERSCAN_API_KEY

ADDRESS = "0x7666ca32ef844ff435506568f66d6de6792e8425"

abi_url = f"https://api.etherscan.io/api?module=contract&action=getabi&address={ADDRESS}&apikey={ETHERSCAN_API_KEY}"
abi_response = get(abi_url)
abi_str = abi_response.json()["result"]
abi = json.loads(abi_str)

w3 = Web3()
contract = w3.eth.contract(address=ADDRESS, abi=abi)

transactions_url = f"https://api.etherscan.io/api?module=account&action=txlist&address={ADDRESS}&startblock=0&endblock=99999999&sort=asc&apikey={ETHERSCAN_API_KEY}"
response = get(transactions_url)
data = response.json()["result"]
data.sort(key=lambda x: int(x["timeStamp"]))

for tx in data:
    inputdata = tx['input']
    try:
        _function_object, function_params = contract.decode_function_input(inputdata)
        print(function_params)
    except:
        print('unable to decode')

This will print out dicts of the data you're looking for. You can format it as you like from there.

Thanks a lot for your answer, I tried the code but I am having problems with Importing from web3 import Web3, I get the following Error: ImportError: cannot import name 'Web3' from partially initialized module 'web3' (most likely due to a circular import)

Thanks a lot for your answer @pacrob, I changed my Code according to your answer. I got rid of that circular Import error, by changing the name of the .py file into "bla.py", then I run into an Address Checksum Error, I fixed that by adding w3.to_checksum_address(ADDRESS):
>     w3 = Web3()
>     dbAddress = w3.to_checksum_address(ADDRESS)
>     contract = w3.eth.contract(address=dbAddress, abi=abi)

Now everything is working. Great thanks
