0xJ0n
0xJ0n

Reputation: 1

Web3.py: Function invocation failed due to no matching argument types

I'm currently facing an issue when interacting with an Ethereum smart contract using Python's Web3 module. More specifically, I'm trying to call a specific function named "requestL2Transaction" from the contract with a set of arguments, but I keep getting an error stating that the argument types do not match.

The function signature in the contract's ABI is as follows:

requestL2Transaction(address,uint256,bytes,uint256,uint256,bytes[],address)

And I'm trying to call it as follows (actual values have been replaced with generic representations for security reasons):

contract_address = "<contract_address>"
abi = <abi> # The actual ABI of the contract
function_name = "requestL2Transaction"
blockchain = "ethereum"
wallet = <wallet> # My wallet instance

# The arguments for the function
_contractL2 = "0x..."
_l2Value = 10000000000000000
_calldata = b''
_l2GasLimit = 1100000
_gasPricePerPubdata = 800
_factoryDeps = []
_refundRecipient = "0x..."

interact_with_contract(wallet, contract_address, abi, function_name, blockchain, None, 
                        _contractL2=_contractL2, _l2Value=_l2Value, _calldata=_calldata, 
                        _l2GasLimit=_l2GasLimit, _gasPricePerPubdata=_gasPricePerPubdata, _factoryDeps=_factoryDeps, _refundRecipient=_refundRecipient)

Here is the code of my "interact_with_contract" function:

async def interact_with_contract(self, wallet, contract_address, abi, function_name, blockchain, msg_value=None, *args, **kwargs):
    contract = self.web3.eth.contract(address=contract_address, abi=abi)
    function = contract.functions[function_name]
    try:
        function_call = function(*args, **kwargs)
    except Exception as e:
        # Handle exception
        # ...
        return

    # Other operations
    # ...

The returned error is the following:

ERROR - Error while building function call : Could not identify the intended function with name requestL2Transaction, positional arguments with type(s) `` and keyword arguments with type(s) {'_l2GasLimit': 'int', '_refundRecipient': 'address', '_gasPricePerPubdata': 'int', '_contractL2': 'address', '_calldata': 'bytes', '_factoryDeps': '(bytes)', '_l2Value': 'int'}. Found 1 function(s) with the name requestL2Transaction: ['requestL2Transaction(address,uint256,bytes,uint256,uint256,bytes[],address)'] Function invocation failed due to no matching argument types.

As you can see, it seems like the argument types passed do not match those expected by the contract function, even though I have checked multiple times and they seem correct.

If anyone has faced a similar issue before or has an idea of what might be causing this error, I would be very grateful for any help or advice.

Upvotes: 0

Views: 809

Answers (2)

SiXty
SiXty

Reputation: 1

If you have these values as strings and need to convert them to integers, you can use the built-in int() function:

_l2GasLimit = int("1100000")
_gasPricePerPubdata = int("800")

The int() function converts a valid string representation of an integer into an integer. If the string cannot be converted into an integer, a ValueError will be raised. Therefore, it's a good practice to use a try-except block when using int() to handle any potential ValueError exceptions:

try:
    _l2GasLimit = int("1100000")
    _gasPricePerPubdata = int("800")
except ValueError:
    print("Error: Invalid integer value.")

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83666

As this question is debugging problem for your own code, it is difficult to provide an answer. However to the following

  • Any address type is Python string

  • Any bytes type is Python bytes

  • Any uint and similar type is Python integer

Then

  • Use interactive debugger to inspect what are the types you are passing to the function

  • Figure out where is the mismatch

  • Correctly cast these types to what your function requires

Upvotes: 0

Related Questions