python-3.x - 错误:无法识别名称为“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.
解决方案
在 minReceived 之后,您需要提供基本上是滑点的 minAmountOut。尝试 0,这将基本上匹配最优惠的价格,它应该可以工作
推荐阅读
- react-native - navigation.navigate 不是 react-navigation 中的功能问题
- ignite - 用备份计数为零点燃
- machine-learning - 决策树性能,ML
- c++ - 是否可以在特定类型的元组上定义扩展方法?
- laravel - 如何从javascript中的innerhtml获取textarea的值并使用laravel中的控制器插入数据库......?
- spring-boot - 弹性4j注释不适用于chlid类
- github - 从 GitHub API 构建明星历史
- git - 隐藏一个文件上的推送提交
- javascript - 我在 fetch api 中没有得到预期的响应
- c# - Html 到格式化文本 C#