首页 > 解决方案 > 错误:无法识别名称为“swapExactTokensForTokens”的预期函数

问题描述

我正在尝试使用swapExactTokensForTokens()(Pancakeswap 路由器功能),在 Python 中导入的 web3 交换令牌。下面是我的代码和错误。请具体回答,因为我是编码新手。

tokenToSpend=$BUSD, tokenToBuy=$CAKE 

代码:

#Calculate minimum amount of tokens to receive

receive = contract.functions.getAmountsOut(amount, [tokenToSpend, tokenToBuy]).call()
minReceived = receive[1] * (9/10)                                                       
receiveReadable = web3.fromWei(minReceived,'ether')                       
print("Minimum tokens to recieve:", str(receiveReadable))

#Trade execution:

pancakeswap2_txn = contract.functions.swapExactTokensForTokens(minReceived, [tokenToSpend,tokenToBuy], sender_address, (int(time.time()) + 1000000)).buildTransaction({   
            'from': sender_address,
            'value': web3.toWei(amount,'ether'), 
            'gasPrice': web3.toWei('5','gwei'),
            'nonce': nonce,
            })

这是错误消息:

Traceback (most recent call last):
  File "c:/Users/Owner/Documents/BlockchainPy/MyCodes/BuyPancake.py", line 64, in <module>
    pancakeswap2_txn = contract.functions.swapExactTokensForTokens(minReceived, [spend,tokenToBuy], sender_address, (int(time.time()) + 1000000)).buildTransaction({

  File "C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\web3\contract.py",
line 876, in __call__
    clone._set_function_info()
  File "C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\web3\contract.py",
line 881, in _set_function_info
    self.abi = find_matching_fn_abi(
  File "C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\web3\_utils\contracts.py", line 163, in find_matching_fn_abi
    raise ValidationError(message)
web3.exceptions.ValidationError:
Could not identify the intended function with name `swapExactTokensForTokens`, positional argument(s) of type `(<class 'float'>, <class 'list'>, <class 'str'>, <class 'int'>)` and keyword argument(s) of type `{}`.
Found 1 function(s) with the name `swapExactTokensForTokens`: ['swapExactTokensForTokens(uint256,uint256,address[],address,uint256)']
Function invocation failed due to improper number of arguments.

标签: python-3.xblockchain

解决方案


在 minReceived 之后,您需要提供基本上是滑点的 minAmountOut。尝试 0,这将基本上匹配最优惠的价格,它应该可以工作


推荐阅读