Solidity
為令牌交換確切的令牌:false 交易已探勘但執行失敗
我將 Remix 與 Foundry Provider 一起使用。
我正在嘗試創建一個智能合約以在 2 個或多個令牌之間進行交換。編譯和部署契約時沒有問題,但是當我呼叫該
swap
函式時,我收到以下消息:氣體估算錯誤並顯示以下消息(見下文)。事務執行可能會失敗。是否要強制發送?執行恢復
當我發送交易執行時,我有這個狀態:
false 交易被探勘但執行失敗
你有什麼想法嗎?我試圖增加氣體限制,但它不起作用。
這是我的程式碼:
pragma solidity >=0.8.7; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); function deposit() external payable; } interface IQuickSwapRouter { function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts); } contract swapToken { function swap(address contractRouter, address tokenAddr, address[] memory path, uint256 nbtoken, address to) public { IERC20(tokenAddr).transferFrom(msg.sender, address(this), nbtoken); IERC20(tokenAddr).approve(contractRouter, nbtoken); IQuickSwapRouter(contractRouter).swapExactTokensForTokens( nbtoken, 0, path, to, block.timestamp); } }
我通過評論以下行找到了解決方案:
IERC20(tokenAddr).transferFrom(msg.sender, address(this), nbtoken);.
它運作良好:)