Flash loan from AAVE and token swap on Uniswap results in no wETH

I write a simple contract to do a flash loan from aave and interact with uniswap in it. I found out that no wETH is sent to my contract after swaping on uniswap. Transcations are here: https://kovan.etherscan.io/tx/0xc98a9b944ef4815c95fd8308cec304cc3052e51f696258dbf23f90762c05008a

in which 0x68c78e23a27068fe4e2348b5b3cd6cfff45a0695 is my contract address

here is my code:

// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.0;

import { FlashLoanReceiverBase } from "./FlashLoanReceiverBase.sol";
import { ILendingPool, ILendingPoolAddressesProvider, IERC20 } from "./MyInterfaces.sol";
import { SafeMath } from "./MyLibraries.sol";

import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";

contract FlashLoanV5 is FlashLoanReceiverBase {
    address constant kovanDAI = address(0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD);
    address constant uniswapRouterAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    
    IUniswapV2Router02 private uniswapRouter;
    IERC20 private DAI;
    
    using SafeMath for uint256;
    
    event Received(address, uint);
    receive() external payable {
        emit Received(msg.sender, msg.value);
    }
    
    constructor(ILendingPoolAddressesProvider _addressProvider) FlashLoanReceiverBase(_addressProvider) public {
        uniswapRouter = IUniswapV2Router02(uniswapRouterAddress);
        DAI = IERC20(kovanDAI);
    }
    
    /**
     * This function is called after your contract has received the flash loaned amount
     * overriding executeOperation() in IFlashLoanReceiver 
     */
    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    )
        external
        override
        returns (bool)
    {   
        // do something
        uint256 loanAmount = amounts[0];
        uniswapSwap(loanAmount);
        
        // Approve the LendingPool contract allowance to *pull* the owed amount
        for (uint i = 0; i < assets.length; i++) {
            uint amountOwing = amounts[i].add(premiums[i]);
            IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
        }

        return true;
    }
    
    
    function myFlashLoanCall(uint256 loanAmount) public {
        address receiverAddress = address(this);

        address[] memory assets = new address[](1);
        assets[0] = kovanDAI;

        uint256[] memory amounts = new uint256[](1);
        amounts[0] = 1 ether;
        amounts[0] = amounts[0] * loanAmount;

        // 0 means revert the transaction if not validated
        uint256[] memory modes = new uint256[](1);
        modes[0] = 0;

        address onBehalfOf = address(this);
        bytes memory params = "";
        uint16 referralCode = 0;

        LENDING_POOL.flashLoan(
            receiverAddress,
            assets,
            amounts,
            modes,
            onBehalfOf,
            params,
            referralCode
        );
    }
    
    //event GetDestAmount(uint destAmount);
    
    /**
     * Swap from DAI to wETH on uniswap
     */
    function uniswapSwap(uint256 daiAmount) public payable {
        // convert to Wei
        uint256 amountIn = daiAmount;
        //require(DAI.transferFrom(msg.sender, address(this), amountIn), 'transferFrom failed.');
        
        require(DAI.approve(address(uniswapRouter), amountIn), 'approve failed.');
        
        address[] memory path = new address[](2);
        path[0] = address(DAI);
        path[1] = uniswapRouter.WETH();
        uint deadline = block.timestamp + 15;
        uint[] memory amounts = uniswapRouter.swapExactTokensForETH(daiAmount, 0, path, address(this), deadline);
        //emit GetDestAmount(amounts[1]);
    }
}

It will be greatful if anyone can help, thanks!

1 Like

Hi @Wei_Pan,

Welcome to the community :wave:

If you don’t get an answer from someone in the community, you could also try asking in the AAVE or Uniswap communities.

Hi abcoatup,

I have see my wETH balance, it is my mistake to mis it. Thanks anyway

1 Like