【问题标题】:ValidationError on smart contract function call for no apparent reason(web3py)?没有明显原因的智能合约函数调用上的ValidationError(web3py)?
【发布时间】:2021-05-03 04:17:21
【问题描述】:

我正在尝试调用 Uniswap 的路由器函数 swapExactTokensForETHSupportingFeeOnTransferTokens()。当我在 etherscan 上手动输入值时,它会通过。但是,当我通过 python 代码执行此操作时,它会给我一个验证错误。错误如下所示:

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.

这是我使用的代码:

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)

我应该以某种方式在 python 中将我的 int 转换为 unsigned int 吗?我试过了,但没有解决。我正在使用 web3py 库。有人可以指导我解决问题或调用所述函数的现有代码吗?

谢谢。

编辑:

我将时间戳转换为 int,并使用 web3.toChecksum 方法确保我的地址字符串是校验和。

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)

当我运行它时,它给了我这个错误:

引发 SolidityError(response['error']['message']) web3.exceptions.SolidityError:执行恢复:TransferHelper: TRANSFER_FROM_FAILED

【问题讨论】:

    标签: python ethereum smartcontracts web3 web3py


    【解决方案1】:

    您传递的参数类型与函数的预期参数类型不匹配。

    你正在通过:

    int, int, list, str, float
    

    但函数期望:

    uint256, uint256, address[], address, uint256
    

    我猜是最后一个参数unix_time_stamp_deadline 导致了不匹配。它是一个浮点数,但该函数需要一个 int。您可以在将它传递给函数时将其转换为 int,如下所示:

    int(unix_time_stamp_deadline)
    

    【讨论】:

    • 谢谢,我按照你的建议做了,它似乎不再给出那个错误了。但是,我仍然无法估计交易的 gas。它给了我一个可靠的执行错误,这让我感到困惑,因为我只是试图估计气体而不是实际发送交易。如果你想看看,我已经更新了我的 OP。
    猜你喜欢
    • 2022-07-08
    • 2017-08-07
    • 2019-08-27
    • 2020-04-08
    • 2019-01-24
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多