简体   繁体   中英

ValidationError on smart contract function call for no apparent reason(web3py)?

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 (<class int>, <class int>, <class list>, <class str>, <class float>) 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. Im using the web3py library. Could someone direct me to the issue or to existing code that calls said function?

Thanks.

Edit:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM