Search code examples
pythonethereumweb3py

func_obj, func_params not responding when decoding an ethereum transaction


I am trying to decode a transaction using the steps from https://medium.com/coinmonks/discovering-the-secrets-of-an-ethereum-transaction-64febb00935c, but whenever I try to run the last line I get the following error:

ValueError: Could not find any function with matching selector

from web3 import Web3
import json
import requests
web3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/46cb623357d54c54b86ae7aa0afa1245'))
# Get transaction object
tx = web3.eth.get_transaction(0xd37e09810d624a3149913e6a769be1cf200d0c511b36008044a0922108fa488a)
# Get ABI for smart contract NOTE: Use "to" address as smart contract 'interacted with'
abi_endpoint = f"https://api.etherscan.io/api?module=contract&action=getabi&address={tx['to']}&apikey={'MDVQWNWT1ZXW2MV5TWKYU9P57WFY3F3P6J'}"
abi = json.loads(requests.get(abi_endpoint).text)
# Create Web3 contract object
contract = web3.eth.contract(address=tx["to"], abi=abi["result"])
# Decode input data using Contract object's decode_function_input() method
func_obj, func_params = contract.decode_function_input(tx["input"])
print(func_params)

How can this be fixed?


Solution

  • I have tested your code and it seems fine in most cases.

    It seems to be a problem with the abi belonging to the contract you're interacting with, since the code works successfully on, example these hashes (randomly selected from ethscan):

    0xb670ab16162f8e4b00cbd57175b0dfb0025690b79a31876d802af84bcc793246 0x4bb2e49bf8965b6acdf5c1b7c18e8811a0e4704a6c3775d743cd8304054adea9

    i did some googling myself and came over this thread that states the same problem:

    https://ethereum.stackexchange.com/questions/70340/how-exactly-do-you-decode-input-data-using-web3-py-using-the-decode-function-inp

    Sorry i couldnt give you an exact solution to the problem but i hope it points you in the right direction!