[Ethereum] How to decode transaction input data on Polygon network

transactionsweb3.py

I am trying to decode the input data that was passed within a transaction. I managed to figure this out on Etherscan, however performing the complementary action on Polygonscan leaves me with an exception I do not understand. See the code below for my approach:

from web3 import Web3
import requests

# api keys for etherscan and polygonscan
eth_api_key = <YOUR_ETHERSCAN_API_KEY>
pol_api_key = <YOUR_POLYGONSCAN_API_KEY>

# http providers
eth_httpprovider = <YOUR_ETH_HTTPPROVIDER_HERE>
pol_httpprovider = <YOUR_POL_HTTPPROVIDER_HERE>

# contracts of interest
eth_contract_addr = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984" # Uniswap
pol_contract_addr = "0x308e44cA2153C61103b0DC67Fd038De650912b73" # GET Ticket NFT Factory

# transaction interacting with contract of interest
eth_tx_hash = "0x4907f0482ed87e637144b502af0ec84af4134d109c53a68f30560d4eea7be59b" # Transfer
pol_tx_hash = "0x926cf150b3882668c6514a3176f17e92c1b8c77c4d076fa83696bacab501e2fb" # Mint

# abi endpoints
eth_abi_addr = f"https://api.etherscan.io/api?module=contract&action=getabi&address={eth_contract_addr}&apikey={eth_api_key}"
pol_abi_addr = f"https://api.polygonscan.com/api?module=contract&action=getabi&address={pol_contract_addr}&apikey={pol_api_key}"

# abi data
eth_abi_data = requests.get(eth_abi_addr).json()
pol_abi_data = requests.get(pol_abi_addr).json()

# create connection to http provider
eth_w3 = Web3(Web3.HTTPProvider(eth_httpprovider))
pol_w3 = Web3(Web3.HTTPProvider(pol_httpprovider))
print(f'Eth node connected: {eth_w3.isConnected()}')
print(f'Pol node connected: {pol_w3.isConnected()}')

# fetch transaction data from its hash
eth_tx = eth_w3.eth.get_transaction(eth_tx_hash)
pol_tx = pol_w3.eth.get_transaction(pol_tx_hash)

# create contract objects
eth_contract = eth_w3.eth.contract(eth_tx['to'], abi=eth_abi_data['result'])
pol_contract = pol_w3.eth.contract(pol_tx['to'], abi=pol_abi_data['result'])

# decode input data
eth_func_obj, eth_func_params = eth_contract.decode_function_input(eth_tx['input'])
print(f'Function: {eth_func_obj}')
print(f'Function params: {eth_func_params}')

pol_func_obj, pol_func_params = pol_contract.decode_function_input(pol_tx['input'])

Which produces the following output:

Eth node connected: True
Pol node connected: True
Function: <Function transfer(address,uint256)>
Function params: {'dst': '0x6cC5F688a315f3dC28A7781717a9A798a59fDA7b', 'rawAmount': 11114622349243359999}
Traceback (most recent call last):
  File "decode_input.py", line 47, in <module>
    pol_func_obj, pol_func_params = pol_contract.decode_function_input(pol_tx['input'])
  File "/home/bram/.venv/main/lib/python3.6/site-packages/eth_utils/decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "/home/bram/.venv/main/lib/python3.6/site-packages/web3/contract.py", line 466, in decode_function_input
    func = self.get_function_by_selector(selector)
  File "/home/bram/.venv/main/lib/python3.6/site-packages/eth_utils/decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "/home/bram/.venv/main/lib/python3.6/site-packages/web3/contract.py", line 459, in get_function_by_selector
    return get_function_by_identifier(fns, 'selector')
  File "/home/bram/.venv/main/lib/python3.6/site-packages/web3/contract.py", line 1681, in get_function_by_identifier
    'Could not find any function with matching {0}'.format(identifier)
ValueError: Could not find any function with matching selector

What is the reason of this exception? I know that the approach to decoding should be different for contract creation, but that is not the case here.

Thanks, in advance.

Best Answer

The problem is that the polygon contract 0x308e44cA2153C61103b0DC67Fd038De650912b73 is a proxy, so its ABI doesn't have the full implementation.

The polygonscan page says the proxy is pointing to the contract at 0x66a9abb8e32d7c7056a886ee4b3b3eb3cc92b54b. To decode the transaction input you should use the ABI from that contract instead.

Related Topic