1

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 enter image description here

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 grateful if anyone can help, thanks!

4
  • 1
    From the transaction hash details 0.000109756275610263 unwrapped ethers were sent to the contract 0x68c78e23....
    – Ismael
    Feb 23 at 18:18
  • Thanks Ismael, but which transaction are you talking about? I can only see address 0x7a250d5630b4cf539739df2c5dacb4c659f2488d received 0.000109756275610263 unwrapped ethers rather than my contract
    – Wei Pan
    Feb 24 at 3:35
  • 1
    It is the transaction linked in the question. First 0xd0a1e3 transfer ethers to 0x7a250d and then 0x7a250d transfer to 0x68c78e. Looking at 0x7a250d swapExactTokensForETH implementation near the end it calls withdraw from WETH to unwrap ether.
    – Ismael
    Feb 24 at 4:06
  • 1
    Got it, thanks my friend
    – Wei Pan
    Feb 24 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.