if(1==1){xxx} 为什么会影响xxx部分代码的执行?

最近在测试一个flashloan的合约,如果删除第9行的if(1==1){xxx} ,那么整个闪贷过程执行正常没有任何问题,但是加上if(1==1){xxx} 后,会报错:"There is no profit! Reverting!",这个错误是44行的require的。搞不清楚为什么同样的代码,增加控制语句后就无法正常执行呢?

测试环境: Truffle v5.1.47 (core: 5.1.47) Solidity v0.5.16 (solc-js) Node v12.18.4 Web3.js v1.2.1 sol pragma: pragma solidity ^0.5.0;

 function executeOperation(
        address _reserve,
        uint _amount,
        uint _fee,
        bytes calldata _params
    ) external {
        require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?");

        if(1 == 1) {
        uint256 deadline = now + 3000;
        address RESERVE_ADDRESS = _reserve;

        // get Exchange Address for the reserve asset

        address addressForLoanAssetExchangeA = uniswapFactoryA.getExchange(RESERVE_ADDRESS);
        address addressForLoanAssetExchangeB = uniswapFactoryB.getExchange(RESERVE_ADDRESS);
        // Instantiate Exchange A

        exchangeAforLoanAsset = IUniswapExchange(addressForLoanAssetExchangeA);
        exchangeBforLoanAsset = IUniswapExchange(addressForLoanAssetExchangeB);

        IERC20 loan = IERC20(RESERVE_ADDRESS);
        IERC20 bat = IERC20(BAT_ADDRESS);
        // Swap the reserve asset (e.g. DAI) for BAT
        require(loan.approve(address(exchangeBforLoanAsset), _amount), "Could not approve reserve asset sell");  // B dai to bat
        uint256 batPurchased = exchangeBforLoanAsset.tokenToTokenSwapInput(
            _amount,
            1,
            1,
            deadline,
            BAT_ADDRESS
        );
        require(bat.approve(address(exchangeAforBAT), batPurchased), "Could not approve BAT asset sell");  // A bat to dai
        // Swap BAT back to the reserve asset (e.g. DAIs)
        uint256 reserveAssetPurchased = exchangeAforBAT.tokenToTokenSwapInput(
            batPurchased,
            1,
            1,
            deadline,
            RESERVE_ADDRESS
        );
        uint amount = _amount;
        uint totalDebt = amount.add(_fee);
        require(reserveAssetPurchased > totalDebt, "There is no profit! Reverting!");
        transferFundsBackToPoolInternal(RESERVE_ADDRESS, amount.add(_fee)); 
    }
        }

正常执行的tx: https://kovan.etherscan.io/tx/0xb1f7d758028f71a824b65bacfacd5cac3a2e9034bcdf6f05fb60c95a89c2b919 出错的tx: https://kovan.etherscan.io/tx/0xa3a07fa388b2e97718f0ea60aeb1dfd14b59cc9bc16b53fc738d3b3e36471c07

请先 登录 后评论

1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 1 关注
  • 0 收藏,3153 浏览
  • tiger 提出于 2020-10-12 12:00