python - Web3.py web3.exceptions.ContractLogicError:在 getAmountOutMin 和交换函数上恢复执行
问题描述
本质上,我正在尝试对 web3.py 使用令牌交换函数,并且在调用该函数时,它给了我一个 ContractLogic 错误:执行还原,这发生在 getAmountOutMin 和交换函数上。我已经彻底查看了文档和其他帖子,但我不确定为什么这段代码会重现这个错误。如果有人能找到解决方案,我将不胜感激。
这是使用的代码:
uniswap_factory_abi = json.loads('''[
{
"inputs": [
{
"internalType": "address",
"name": "_tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "_tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amountIn",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_amountOutMin",
"type": "uint256"
},
{
"internalType": "address",
"name": "_to",
"type": "address"
}
],
"name": "swap",
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "_tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amountIn",
"type": "uint256"
}
],
"name": "getAmountOutMin",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}]''')
actual_min = float(min)
amountout = contract.functions.getAmountOutMin("0xd0A1E359811322d97991E03f863a0C30C2cF029C", "0xF6fF8efc1e11A17E410F248475ffE870E8FC6ebD", int(actual_min * 1e18))
txn = {
'from': account,
'gas': 650000,
'gasPrice': web3.eth.gasPrice,
'nonce': web3.eth.getTransactionCount(account)
}
amountoutmin = amountout.call(txn)
token_swap = contract.functions.swap("0xd0A1E359811322d97991E03f863a0C30C2cF029C", "0xF6fF8efc1e11A17E410F248475ffE870E8FC6ebD", int(actual_min * 1e18), amountoutmin, account)
token_swap.call(txn)
编辑:
使用solcx编译solidity代码后,解决了我的问题
编辑2:
显然使用solcx编译solidity代码并没有解决问题,但是我现在发现我一直使用的solidity代码有问题,这是我问题的根源
解决方案
我会先尝试检查你的合约是否部署成功,然后看看你使用的功能是否也能正常工作。不确定你用什么来部署它,但是在 Remix 中,你可以使用 Web 界面来测试你的功能,并且还有一个不错的 dubug 功能。在此处输入值,检查输出,如果有效,则转到代码部分。我的猜测是您的 txn 可能缺少一些参数,例如 chainId、value 等。您是否也签署了交易?
推荐阅读
- perl - 对另一个哈希内的哈希的引用给出了在连接 (.) 或字符串中使用未初始化的值
- mysql - mysql索引优化问题
- asp.net-core - ASP.NET Core DI 直接访问容器
- security - 保护 .NET Core MVC 网站免受暴力登录尝试(通过内置方式)
- html - 我的标题在按钮悬停效果时晃动
- javascript - JavaScript 动态同步函数队列
- sql-server - 在 If 语句中使用值后从存储过程中返回一个值
- r - 文件中出现错误(文件,“rt”):无法打开连接
- monitoring - Geneos 文件系统监控 .snapshot
- javascript - 带有 Typescript TS2604 错误的动态导入 ES6 React 组件?