Flash Loan Contract Transaction Reverted

I deployed this contract on the BSC mainnet but each time i call the startSwap function, i get a transaction reverted error. Can someone please tell me what i’m doing wrong?


import './interfaces/IUniswapV2Factory.sol';
import './interfaces/IUniswapV2Router02.sol';
import './interfaces/IBakerySwapRouter.sol';
import './interfaces/IPancakeCallee.sol';
import './libraries/PancakeLibrary.sol';
import './interfaces/IERC20.sol';
import './BurgerSwapRouter.sol';
import './BakerySwapRouter.sol';



contract FlashSwap is IPancakeCallee {
    address public factory = 0xBCfCcbde45cE874adCB698cC183deBcF17952812; 
    
    DemaxPlatform public BurgerRouter =  DemaxPlatform(0x42591f57f707739b95C5C486c014B525f19d70ca);
    BakerySwapRouter public BakeryRouter = BakerySwapRouter(0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F);
    IUniswapV2Router02 public PancakeRouter = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
    IUniswapV2Router02 public AnonymousRouter;
    
    
    address pairAddress;
    uint ROUTER_ID;
    uint deadline;
    bool isBNB;
    bool BNBIsFirst;
    string stage;
   
   
   
    event _stage(string);
    
    
    function updateBNB (
        bool _isBNB,
        bool _BNBIsFirst
        ) external{
        isBNB = _isBNB;
        BNBIsFirst = _BNBIsFirst;
    }
    
    function updateParams(
        address    _factory,
        address payable _BakeryRouter,
        address payable _BurgerRouter,
        address payable _PancakeRouter
        ) external {
        
        factory = _factory;
        BakeryRouter = BakerySwapRouter(_BakeryRouter);
        BurgerRouter = DemaxPlatform(_BurgerRouter);
        PancakeRouter = IUniswapV2Router02(_PancakeRouter);    
        
    }
    
    
    
    function startSwap(
        address token0,
        address token1,
        uint amount0,
        uint amount1,
        bool _isBNB,
        bool _BNBIsFirst,
        uint  _ROUTER_ID,
        uint _deadline
    ) external {
        
        isBNB = _isBNB;
        BNBIsFirst = _BNBIsFirst;
        ROUTER_ID = _ROUTER_ID;
        deadline = _deadline;
       
       
        pairAddress = IUniswapV2Factory(factory).getPair(token0, token1);
        require(pairAddress != address(0), 'This pool does not exist');
        
        stage = 'calling IUniswapV2Pair(pairAddress)';
        emit _stage(stage);
         
        IUniswapV2Pair(pairAddress).swap(
          amount0, 
          amount1, 
          address(this), 
          bytes('not empty')
        );
    }
    

    
    receive() external  payable  {
        
    }
    
    function pancakeCall(
        address sender, 
        uint _amount0, 
        uint _amount1, 
        bytes calldata data

    ) external override {
        stage = 'pancakeCall made';
        emit _stage(stage);
        
        
        address[] memory path = new address[](2);
        uint amountToken = _amount0 == 0 ? _amount1 : _amount0;
        
        address token0 = IUniswapV2Pair(pairAddress).token0();
        address token1 = IUniswapV2Pair(pairAddress).token1();

        
        //require(msg.sender == PancakeLibrary.pairFor(factory, token0, token1), 'Unauthorized'); 
          
        require(_amount0 == 0 || _amount1 == 0);
    
        path[0] = _amount0 == 0 ? token1 : token0;
        path[1] = _amount0 == 0 ? token0 : token1;
    
        IERC20 token = IERC20(path[0]);
        
         uint amountRequired = PancakeLibrary.getAmountsIn(
          factory, 
          amountToken, 
          path
        )[0];
        
         stage = 'before swap';
        emit _stage(stage);
        
        
        if (ROUTER_ID == 0) {
            swapWithBakery(
                amountToken,
                amountRequired,
                path,
                pairAddress,
                deadline
            );
            
            
        } else if (ROUTER_ID == 1) {
             swapWithBurger(
                amountToken,
                amountRequired,
                path,
                pairAddress,
                deadline
            );
        }
    }
    
    function getPairs(
       address factory,
       uint index
    ) public returns (address[] memory pairs) {
        address[] memory pairs;
        address pairAddress = IUniswapV2Factory(factory).allPairs(index);
        
        pairs[0]= IUniswapV2Pair(pairAddress).token0();
        pairs[1]= IUniswapV2Pair(pairAddress).token1();
        return pairs;        
    }
    
    
    
    function swapWithPancake (  
        uint amountToken,
        uint amountRequired,
        address[] memory path,
        address returnAddress,
        uint deadline
        
        ) public {
    
          uint amountReceived;
          IERC20(path[0]).approve(address(PancakeRouter), amountToken);
          
      
         
           
          if (isBNB == true) {
                if (BNBIsFirst == true) {
                  amountReceived =  PancakeRouter.swapExactETHForTokens(
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
               
                } else {
                    amountReceived = PancakeRouter.swapExactTokensForETH(
                      amountToken, 
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
                        
                }
            
            } else {
                amountReceived = PancakeRouter.swapExactTokensForTokens(
                  amountToken, 
                  amountRequired, 
                  path, 
                  address(this), 
                  deadline
                )[1];
            }
            
            completeSwap(
                amountRequired,
                amountReceived,
                path[1],
                returnAddress
            );
            
    }
    
    function swapWithBakery(
        uint amountToken,
        uint amountRequired,
        address[] memory path,
        address returnAddress,
        uint deadline
        
        ) public {
        
          uint amountReceived;
          IERC20(path[0]).approve(address(BakeryRouter), amountToken);
          if (isBNB == true) {
                if (BNBIsFirst == true) {
                  amountReceived = BakeryRouter.swapExactBNBForTokens(
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
               
                } else {
                    amountReceived = BakeryRouter.swapExactTokensForBNB(
                      amountToken, 
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
                        
                }
            
            } else {
                amountReceived = BakeryRouter.swapExactTokensForTokens(
                  amountToken, 
                  amountRequired, 
                  path, 
                  address(this), 
                  deadline
                )[1];
            }
        
          completeSwap(
            amountRequired,
            amountReceived,
            path[1],
            returnAddress
        );
    }

    function swapWithBurger(
        uint amountToken,
        uint amountRequired,
        address[] memory path,
        address returnAddress,
        uint deadline
        
        ) public {
    
          uint amountReceived;
          IERC20(path[0]).approve(address(BurgerRouter), amountToken);
          if (isBNB == true) {
                if (BNBIsFirst == true) {
                  amountReceived = BurgerRouter.swapExactETHForTokens(
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
               
                } else {
                    amountReceived = BurgerRouter.swapExactTokensForETH(
                      amountToken, 
                      amountRequired, 
                      path, 
                      address(this), 
                      deadline
                    )[1];
                        
                }
            
            } else {
                amountReceived = BurgerRouter.swapExactTokensForTokens(
                  amountToken, 
                  amountRequired, 
                  path, 
                  address(this), 
                  deadline
                )[1];
            }
            
            completeSwap(
                amountRequired,
                amountReceived,
                path[1],
                returnAddress
            );
    }
    
    function completeSwap(
        uint amountRequired,
        uint amountReceived,
        address _token,
        address returnAddress
        ) internal {
            
        IERC20 token = IERC20(_token);
        token.transfer(returnAddress, amountRequired);
        token.transfer(tx.origin, amountReceived - amountRequired);
        stage  = 'token returned, Swap complete!';
        emit _stage(stage);
    }
    
  
    
}```

Do you get an error or do you have a transaction link?

1 Like
1 Like

May I ask you pragma version? so I can test?
EDIT:
I saw only now that I should need also the imported libraries lol

1 Like

did you noticed all the warnings?

1 Like

What warnings? I didn’t see any

1 Like

There are like 12 warinings of unused variables etc

1 Like

I didn’t get those errors. I used remix to write the code

1 Like

Yes I am using it too. Anyway there are variables used for different things but same name

1 Like

Okay, is that the cause of the error?

1 Like

I must check. Did you wrote this code?

1 Like

Just the flash_swap.sol.

1 Like

How it should work? I mean: you only have to call start swap ?

1 Like

Yes, with the parameters

1 Like

Then all the other functions are useless right?

1 Like

swapWithBakery, swapWithBurger, pancakeCallee will be called through the startSwap function

1 Like

But they are not called. Why?

1 Like

The startSwap function calls swap on the pair Address of the two tokens, this swap function then calls the pancakeCallee that calls either swapWithBurger or swapWithBakery depending on the _ROUTER_ID that was passed in the startSwap function

1 Like

Ok and how the swap happens, you have to borrow some tokens no?

1 Like

Also I see that demaxPlatform is gaining only failed transactions

1 Like