Search code examples
pythonethereumsmartcontractsweb3py

ValidationError on smart contract function call for no apparent reason?


I am trying to call Uniswap's Router's function swapExactTokensForETHSupportingFeeOnTransferTokens(). When I enter the values manually on etherscan, it goes through. However, when I do it via python code it gives me a validation error. The error looks like this:

web3.exceptions.ValidationError: Could not identify the intended function with name swapExactTokensForETHSupportingFeeOnTransferTokens, positional argument(s) of type (, , , , ) and keyword argument(s) of type {}. Found 1 function(s) with the name swapExactTokensForETHSupportingFeeOnTransferTokens: [swapExactTokensForETHSupportingFeeOnTransferTokens(uint256,uint256,address[],address,uint256)] Function invocation failed due to no matching argument types.`

Here's the code im using:

swap = uniswap_router_contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(uint amount, 0, list_of_two_token_addresses, my_address_string, unix_time_stamp_deadline).buildTransaction({'nonce': some_nonce})

gas_amount = web3.eth.estimateGas(swap)

print(gas amount)

Am I supposed to somehow turn my ints into unsigned int in python? I tried but it didn't fix it. I'm using the web3py library. Could someone direct me to the issue or to existing code that calls said function?

I converted timestamp into int and also made sure my address strings were checksum using the web3.toChecksum method.

swap = uniswap_router_contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(uint amount, 0, list_of_two_token_addresses, my_address_string, int(unix_time_stamp_deadline)).buildTransaction({'nonce': some_nonce})
gas = web3.eth.estimateGas(swap)
print(gas)

When I run this it gives me this error:

raise SolidityError(response['error']['message']) web3.exceptions.SolidityError: execution reverted: TransferHelper: TRANSFER_FROM_FAILED


Solution

  • The arguments types that you are passing do not match the expected argument types for the function.

    You are passing:

    int, int, list, str, float
    

    but the function expects:

    uint256, uint256, address[], address, uint256
    

    I'm guessing that it is the last argument, unix_time_stamp_deadline, that is causing the mismatch. It is a float, but the function expects an int. You can convert it to an int as you pass it to the function like this:

    int(unix_time_stamp_deadline)