all files / contracts/test/ FlashLoanNativeTest.sol

100% Statements 12/12
60% Branches 6/10
100% Functions 2/2
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                   
// SPDX-License-Identifier: GPL-2.0-or-later
 
pragma solidity ^0.8.0;
 
import "../Interfaces.sol";
import "../modules/DToken.sol";
import "../modules/Markets.sol";
import "../modules/Exec.sol";
import "../modules/DToken.sol";
 
 
contract FlashLoanNativeTest is IDeferredLiquidityCheck {
    struct CallbackData {
        address eulerAddr;
        address marketsAddr;
        address execAddr;
        address underlying;
        uint amount;
        bool payItBack;
    }
 
    function testFlashLoan(CallbackData calldata data) external {
        Exec(data.execAddr).deferLiquidityCheck(address(this), abi.encode(data));
    }
 
    function onDeferredLiquidityCheck(bytes memory encodedData) external override {
        CallbackData memory data = abi.decode(encodedData, (CallbackData));
 
        address dTokenAddr = Markets(data.marketsAddr).underlyingToDToken(data.underlying);
        DToken dToken = DToken(dTokenAddr);
 
        dToken.borrow(0, data.amount);
 
        Erequire(IERC20(data.underlying).balanceOf(address(this)) == data.amount, "didn't receive underlying");
        Erequire(dToken.balanceOf(address(this)) == data.amount, "didn't receive dTokens");
 
        if (data.payItBack) {
            IERC20(data.underlying).approve(data.eulerAddr, type(uint).max);
            dToken.repay(0, data.amount);
 
            Erequire(IERC20(data.underlying).balanceOf(address(this)) == 0, "didn't repay underlying");
            Erequire(dToken.balanceOf(address(this)) == 0, "didn't burn dTokens");
        }
    }
}