Hi all! I was trying to solve the ethernaut level 22 ( https://ethernaut.openzeppelin.com/level/0xC084FC117324D7C628dBC41F17CAcAaF4765f49e ) using delegatecall to avoid doing extra approvals and transferFrom, but I must be doing something wrong.
function delegateSwap(address dex, address tokenIn, address tokenOut, uint amountIn) public {
bytes memory payload = abi.encodeWithSignature("swap(address,address,uint256)", tokenIn, tokenOut, amountIn);
(bool success, ) = dex.delegatecall(payload);
require(success, 'Swap failed!');
}
Does someone knows what I am doing wrong?
target dex swap function:
function swap(address from, address to, uint amount) public {
require((from == token1 && to == token2) || (from == token2 && to == token1), "Invalid tokens");
require(IERC20(from).balanceOf(msg.sender) >= amount, "Not enough to swap");
uint swapAmount = getSwapPrice(from, to, amount);
IERC20(from).transferFrom(msg.sender, address(this), amount);
IERC20(to).approve(address(this), swapAmount);
IERC20(to).transferFrom(address(this), msg.sender, swapAmount);
}
function getSwapPrice(address from, address to, uint amount) public view returns(uint){
return((amount * IERC20(to).balanceOf(address(this)))/IERC20(from).balanceOf(address(this)));
}
PS: I know you can solve it manually, but I wanted to write this contract that proxies swap requests
Jun 1, 2022, 4:07 PM