How to swap tokens using a flash loan on Solidity

SimpleFlashLoan.sol - Bharati - Replit

I was actually watching a video by BharatiDWConsultancy, hence the name Bharati (he isn’t the only one with a tutorial that I try). Specifically, this one: Flash Loan Crypto Arbitrage Step-by-Step | AAVE Flash Loans Solidity DIY Tutorials | Solidity Code - YouTube

I was able to successfully test the flash loans and receive test money. Anything he did in the video, I also did, such as sending $1,000 USDC to my account as profit by borrowing a flash loan of $10,000 USDC.

But now, I’m trying to add business logic, specifically SwapExactTokensForTokens, because I do want to do this in real time, so this is what I try to add to SimpleFlashLoan (In ArbitrageFlashLoan, we assume we get 10% profit from a token that isn’t even specified, which certainly isn’t sure to happen in real time):

        interface IUniswapV2Router {
        function swapExactTokensForTokens(
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline
          ) external returns (uint[] memory amounts);
        }

But whenever I try to compile and run the script on Remix IDE, it keeps saying there is an error, and when I try resolving that, there’s another, and then another! I don’t know what to do! So here I am.

First it says there’s a parser error because nested functions are not allowed in Solidity, so I removed ExecuteOperation, and then it says there is an error by the left parenthesis of “asset” in the following code:

IERC20(asset).approve(address(POOL), totalAmount);

I can go on and on. Sometimes, it’ll say a certain character was expected, that caused an error in the first place!

And by the way, I was also gonna try to use swapExactTokensForTokensSupportingFeeOnTransferTokens to swap with one token on Uniswap V2 and another on a different exchange (Uniswap V3 or PancakeSwap). I’m not even sure if this is what I need to use to do that, or I can just do this on swapExactTokensForTokens.

Help would be highly appreciated.

I don’t wish to be a bother, but I need an answer, please and thanks in advance.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.