这个是 solidity
的常见错误,就是 solidity
嫌弃你方法所需参数过多。
- InternalCompilerError: Stack too deep, try removing local variables
参考资料
解决方案
重申一遍
这个是偶然之间发现的,如果你按照下面这样写就会报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| pragma solidity ^0.8.4;
interface functionContract { function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts); 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); }
contract MultiTrade {
constructor() { }
function tokenSwap(uint32 count, address[] calldata cexAddress, uint[] calldata amountIns, uint[] calldata amountOutMins, address[][] memory paths, address[] calldata tos, uint deadlines) public { uint amountIn = amountIns[0]; for (uint i = 0; i < count; i++) { address swapAddress = cexAddress[i]; address[] memory path = paths[i]; address base = path[0]; address token = path[path.length - 1]; address to = tos[i]; uint amountOut = amountOutMins[i]; functionContract routerCex = functionContract(swapAddress); functionContract baseToken = functionContract(base); functionContract tokenContract = functionContract(token); if(baseToken.allowance(address(this),swapAddress) == 0){ baseToken.approve(swapAddress,type(uint256).max); } routerCex.swapExactTokensForTokens(amountIn,amountOut,path,to,deadlines); amountIn = tokenContract.balanceOf(address(this)); } } }
|
这个报错是说 deadlines 有 stacktoodeep,即上面代码的第 41 行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| pragma solidity ^0.8.4;
interface functionContract { function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts); 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); }
contract MultiTrade {
constructor() { }
function tokenSwap(uint32 count, address[] calldata cexAddress, uint[] calldata amountIns, uint[] calldata amountOutMins, address[][] memory paths, address[] calldata tos, uint deadlines) public { uint amountIn = amountIns[0]; for (uint i = 0; i < count; i++) { address swapAddress = cexAddress[i]; address[] memory path = paths[i]; address base = path[0]; address token = path[path.length - 1]; address to = tos[i]; uint amountOut = amountOutMins[i]; uint deadline = deadlines; functionContract routerCex = functionContract(swapAddress); functionContract baseToken = functionContract(base); functionContract tokenContract = functionContract(token); if(baseToken.allowance(address(this),swapAddress) == 0){ baseToken.approve(swapAddress,type(uint256).max); } routerCex.swapExactTokensForTokens(amountIn,amountOut,path,to,deadline); amountIn = tokenContract.balanceOf(address(this)); } } }
|
只需要加上一句
uint deadline = deadlines;
然后引用 deadline
就好了,非常的魔性。