contract StabilizeStrategyUSTFlashArb is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    using Address for address;
    
    address public treasuryAddress; // Address of the treasury
    address public stakingAddress; // Address to the STBZ staking pool
    address public zsTokenAddress; // The address of the controlling zs-Token
    
    uint256 constant DIVISION_FACTOR = 100000;
    uint256 public lastTradeTime = 0;
    uint256 public lastActionBalance = 0; // Balance before last deposit or withdraw
    uint256 public maxPoolSize = 3000000e18; // The maximum amount of ust tokens this strategy can hold, 3 mil by default
    uint256 public percentTradeTrigger = 10000; // 10% change in value will trigger a trade
    uint256 public maxSlippage = 200; // 0.2% max slippage is ok
    uint256 public maxPercentSell = 100000; // 100% of the tokens are sold to the cheapest token if slippage is ok
    uint256 public maxAmountSell = 100000; // The maximum amount of tokens that can be sold at once
    uint256 public percentDepositor = 50000; // 1000 = 1%, depositors earn 50% of all gains
    uint256 public percentExecutor = 10000; // 30000 = 30% of WETH goes to executor, 15% of total profit
    uint256 public percentStakers = 50000; // 50% of non-depositors WETH goes to stakers, can be changed
    uint256 public minTradeSplit = 20000; // If the balance is less than or equal to this, it trades the entire balance
    uint256 public maxPercentStipend = 30000; // The maximum amount of WETH profit that can be allocated to the executor for gas in percent
    uint256 public gasStipend = 800000; // This is the gas units that are covered by executing a trade taken from the WETH profit
    uint256[3] private flashParams; // Global parameters guiding the flash loan setup
    uint256 constant minGain = 1e16; // Minimum amount of gain (0.01 coin) before buying WETH and splitting it
    
    // Token information
    // This strategy accepts frax and usdc
    struct TokenInfo {
        IERC20 token; // Reference of token
        uint256 decimals; // Decimals of token
    }
    
    TokenInfo[] private tokenList; // An array of tokens accepted as deposits

    // Strategy specific variables
    address constant UNISWAP_ROUTER_ADDRESS = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); //Address of Uniswap
    address constant CURVE_UST_POOL = address(0x890f4e345B1dAED0367A877a1612f86A1f86985f);
    address constant WETH_ADDRESS = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
    address constant GAS_ORACLE_ADDRESS = address(0x169E633A2D1E6c10dD91238Ba11c4A708dfEF37C); // Chainlink address for fast gas oracle
    address constant LENDING_POOL_ADDRESS_PROVIDER = address(0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5); // Provider for Aave addresses

    constructor(
        address _treasury,
        address _staking,
        address _zsToken
    ) public {
        treasuryAddress = _treasury;
        stakingAddress = _staking;
        zsTokenAddress = _zsToken;
        setupWithdrawTokens();
    }

    // Initialization functions
    
    function setupWithdrawTokens() internal {
        // Start with UST
        IERC20 _token = IERC20(address(0xa47c8bf37f92aBed4A126BDA807A7b7498661acD));
        tokenList.push(
            TokenInfo({
                token: _token,
                decimals: _token.decimals()
            })
        );
        
        // USDT
        _token = IERC20(address(0xdAC17F958D2ee523a2206206994597C13D831ec7));
        tokenList.push(
            TokenInfo({
                token: _token,
                decimals: _token.decimals()
            })
        );
    }
    
    // Modifier
    modifier onlyZSToken() {
        require(zsTokenAddress == _msgSender(), "Call not sent from the zs-Token");
        _;
    }
    
    // Read functions
    
    function rewardTokensCount() external view returns (uint256) {
        return tokenList.length;
    }
    
    function rewardTokenAddress(uint256 _pos) external view returns (address) {
        require(_pos < tokenList.length,"No token at that position");
        return address(tokenList[_pos].token);
    }
    
    function balance() public view returns (uint256) {
        return getNormalizedTotalBalance(address(this));
    }
    
    function getNormalizedTotalBalance(address _address) public view returns (uint256) {
        uint256 _balance = 0;
        for(uint256 i = 0; i < tokenList.length; i++){
            uint256 _bal = tokenList[i].token.balanceOf(_address);
            _bal = _bal.mul(1e18).div(10**tokenList[i].decimals);
            _balance = _balance.add(_bal); // This has been normalized to 1e18 decimals
        }
        return _balance;
    }
    
    function withdrawTokenReserves() public view returns (address, uint256) {
        // This function will return the address and amount of frax, then usdc
        if(tokenList[0].token.balanceOf(address(this)) > 0){
            return (address(tokenList[0].token), tokenList[0].token.balanceOf(address(this)));
        }else if(tokenList[1].token.balanceOf(address(this)) > 0){
            return (address(tokenList[1].token), tokenList[1].token.balanceOf(address(this)));
        }else{
            return (address(0), 0); // No balance
        }
    }
    
    // Write functions
    
    function enter() external onlyZSToken {
        deposit(false);
    }
    
    function exit() external onlyZSToken {
        // The ZS token vault is removing all tokens from this strategy
        withdraw(_msgSender(),1,1, false);
    }
    
    function deposit(bool nonContract) public onlyZSToken {
        // Only the ZS token can call the function
        
        // No trading is performed on deposit
        if(nonContract == true){ }
        lastActionBalance = balance();
        require(lastActionBalance <= maxPoolSize,"This strategy has reached its maximum balance");
    }
    
    function simulateExchange(address _inputToken, address _outputToken, uint256 _amount, bool _uniswap) internal view returns (uint256) {
        if(_uniswap == true){
            // Possible Uniswap routes, UST / USDT, USDT / ETH
            UniswapRouter router = UniswapRouter(UNISWAP_ROUTER_ADDRESS);
            address[] memory path;
            if(_inputToken == address(tokenList[0].token) && _outputToken == WETH_ADDRESS){
                // Selling UST for WETH, must go through USDT
                path = new address[](3);
                path[0] = _inputToken;
                path[1] = address(tokenList[1].token);
                path[2] = _outputToken;
            }else{
                path = new address[](2);
                path[0] = _inputToken;
                path[1] = _outputToken;                
            }
            uint256[] memory estimates = router.getAmountsOut(_amount, path);
            _amount = estimates[estimates.length - 1]; // This is the amount of WETH returned
            return _amount;            
        }else{
            // We are swapping via curve
            CurvePool pool = CurvePool(CURVE_UST_POOL);
            int128 inCurve = 0;
            int128 outCurve = 0;
            if(_inputToken == address(tokenList[1].token)){inCurve = 3;}
            if(_outputToken == address(tokenList[1].token)){outCurve = 3;}
            return pool.get_dy_underlying(inCurve, outCurve, _amount);
        }
    }
    
    function exchange(address _inputToken, address _outputToken, uint256 _amount, bool _uniswap) internal {
        if(_uniswap == true){
            // Possible Uniswap routes, UST / USDT, USDT / ETH
            UniswapRouter router = UniswapRouter(UNISWAP_ROUTER_ADDRESS);
            address[] memory path;
            if(_inputToken == address(tokenList[0].token) && _outputToken == WETH_ADDRESS){
                // Selling UST for WETH, must go through USDT
                path = new address[](3);
                path[0] = _inputToken;
                path[1] = address(tokenList[1].token);
                path[2] = _outputToken;
            }else{
                path = new address[](2);
                path[0] = _inputToken;
                path[1] = _outputToken;                
            }
            IERC20(_inputToken).safeApprove(UNISWAP_ROUTER_ADDRESS, 0);
            IERC20(_inputToken).safeApprove(UNISWAP_ROUTER_ADDRESS, _amount);
            router.swapExactTokensForTokens(_amount, 1, path, address(this), now.add(60)); // Get WETH from token
            return;           
        }else{
            // We are swapping via curve
            CurvePool pool = CurvePool(CURVE_UST_POOL);
            int128 inCurve = 0;
            int128 outCurve = 0;
            if(_inputToken == address(tokenList[1].token)){inCurve = 3;}
            if(_outputToken == address(tokenList[1].token)){outCurve = 3;}
            IERC20(_inputToken).safeApprove(CURVE_UST_POOL, 0);
            IERC20(_inputToken).safeApprove(CURVE_UST_POOL, _amount);
            pool.exchange_underlying(inCurve, outCurve, _amount, 1);
        }
    }

    function getCheaperToken() internal view returns (uint256, uint256) {
        // This will give us the ID of the cheapest token for both Uniswap and Curve
        // We will estimate the return for trading 1000 UST
        // The higher the return, the lower the price of the other token
        uint256 targetID_1 = 0; // Our target ID is UST first
        uint256 targetID_2 = 0;
        uint256 ustAmount = uint256(1000).mul(10**tokenList[0].decimals);
        
        // Now compare it to USDT on Uniswap
        uint256 estimate = simulateExchange(address(tokenList[0].token),address(tokenList[1].token),ustAmount,true);
        estimate = estimate.mul(10**tokenList[0].decimals).div(10**tokenList[1].decimals);
        if(estimate > ustAmount){
            // This token is worth less than the UST on Uniswap
            targetID_1 = 1;
        }
        
        // Now on Curve
        estimate = simulateExchange(address(tokenList[0].token),address(tokenList[1].token),ustAmount,false);
        estimate = estimate.mul(10**tokenList[0].decimals).div(10**tokenList[1].decimals);
        if(estimate > ustAmount){
            // This token is worth less than UST on curve
            targetID_2 = 1;
        }
        return (targetID_1, targetID_2);
    }
    
    function estimateSellAtMaxSlippage(uint256 originID, uint256 targetID, uint256 _balance) internal view returns (uint256) {
        // This will estimate the amount that can be sold at the maximum slippage
        // We discover the price then compare it to the actual return
        // The estimate is based on a linear curve so not 100% representative of Uniswap but close enough
        
        // It will use a 0.1% sell to discover the price first
        uint256 minSellPercent = maxPercentSell.div(1000);
        uint256 _amount = _balance.mul(minSellPercent).div(DIVISION_FACTOR);
        if(_amount == 0){ return 0; } // Nothing to sell, can't calculate
        uint256 _maxReturn = simulateExchange(address(tokenList[originID].token), address(tokenList[targetID].token), _amount, true);
        _maxReturn = _maxReturn.mul(1000); // Without slippage, this would be our maximum return
        
        // Now calculate the slippage at the max percent
        _amount = _balance.mul(maxPercentSell).div(DIVISION_FACTOR);
        uint256 _return = simulateExchange(address(tokenList[originID].token), address(tokenList[targetID].token), _amount, true);
        if(_return >= _maxReturn){
            // This shouldn't be possible
            return _amount; // Sell the entire amount
        }
        // Calculate slippage
        uint256 percentSlip = uint256(_maxReturn.mul(DIVISION_FACTOR)).sub(_return.mul(DIVISION_FACTOR)).div(_maxReturn);
        if(percentSlip <= maxSlippage){
            return _amount; // This is less than our maximum slippage, sell it all
        }
        return _amount.mul(maxSlippage).div(percentSlip); // This will be the sell amount at max slip
    }
    
    function estimateFlashLoanResult(uint256 point1, uint256 point2, uint256 _amount) internal view returns (uint256) {
        // This will estimate the return of our flash loan minus the fee
        uint256 fee = _amount.mul(90).div(DIVISION_FACTOR); // This is the Aave fee (0.09%)
        uint256 gain = 0;
        if(point2 > point1){
            // USL price is lower than USDT on Uniswap, while higher on Curve
            // Use the borrowed USDT to buy USL on Uniswap
            gain = simulateExchange(address(tokenList[1].token), address(tokenList[0].token), _amount, true); // Receive USL
            gain = simulateExchange(address(tokenList[0].token), address(tokenList[1].token), gain, false); // Receive USDT
        }else{
            // USL price higher than USDT on Uniswap while lower on Curve
            gain = simulateExchange(address(tokenList[1].token), address(tokenList[0].token), _amount, false); // Receive USL
            gain = simulateExchange(address(tokenList[0].token), address(tokenList[1].token), gain, true); // Receive USDT
        }
        if(gain > _amount.add(fee)){
            // Positive return on the flash
            gain = gain.sub(fee).sub(_amount); // Get the pure gain after returning the funds with a fee
            return gain;
        }else{
            return 0; // Do not take out a flash loan as not enough gain
        }
    }
    
    function performFlashLoan(uint256 point1, uint256 point2, uint256 _amount) internal returns (uint256) {
        // Will call Aave
        uint256 flashGain = tokenList[1].token.balanceOf(address(this));
        
        flashParams[0] = 1; // Authorize flash loan receiving
        flashParams[1] = point1; 
        flashParams[2] = point2;
        
        // Call the flash loan
        LendingPool lender = LendingPool(LendingPoolAddressesProvider(LENDING_POOL_ADDRESS_PROVIDER).getLendingPool()); // Load the lending pool
        
        address[] memory assets = new address[](1);
        assets[0] = address(tokenList[1].token);       
        
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = _amount; // The amount we want to borrow
        
        uint256[] memory modes = new uint256[](1);
        modes[0] = 0; // Revert if fail to return funds
        
        bytes memory params = "";
        lender.flashLoan(address(this), assets, amounts, modes, address(this), params, 0);
        
        flashParams[0] = 0; // Deactivate flash loan receiving
        uint256 newBal = tokenList[1].token.balanceOf(address(this));
        require(newBal > flashGain, "Flash loan failed to increase balance");
        flashGain = newBal.sub(flashGain);
        uint256 payout = flashGain.mul(uint256(100000).sub(percentDepositor)).div(DIVISION_FACTOR);
        if(payout > 0){
            // Convert part to WETH
            exchange(address(tokenList[1].token), WETH_ADDRESS, payout, true);
        }
        return flashGain;
    }
    
    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    )
        external
        returns (bool)
    {
        require(flashParams[0] == 1, "No flash loan authorized on this contract");
        address lendingPool = LendingPoolAddressesProvider(LENDING_POOL_ADDRESS_PROVIDER).getLendingPool();
        require(_msgSender() == lendingPool, "Not called from Aave");
        require(initiator == address(this), "Not Authorized"); // Prevent other contracts from calling this function
        if(params.length == 0){} // Removes the warning
        flashParams[0] = 0; // Prevent a replay;
        {
            // Create inner scope to prevent stack too deep error
            uint256 point1 = flashParams[1];
            uint256 point2 = flashParams[2];
            uint256 _bal;
            
            // Swap the amounts to earn more
            if(point2 > point1){
                // USL price is lower than USDT on Uniswap, while higher on Curve
                // Use the borrowed USDT to buy USL on Uniswap
                _bal = tokenList[0].token.balanceOf(address(this));
                exchange(address(tokenList[1].token), address(tokenList[0].token), amounts[0], true); // Receive USL
                _bal = tokenList[0].token.balanceOf(address(this)).sub(_bal);
                exchange(address(tokenList[0].token), address(tokenList[1].token), _bal, false); // Receive USDT
            }else{
                // USL price higher than USDT on Uniswap while lower on Curve
                _bal = tokenList[0].token.balanceOf(address(this));
                exchange(address(tokenList[1].token), address(tokenList[0].token), amounts[0], false); // Receive USL
                _bal = tokenList[0].token.balanceOf(address(this)).sub(_bal);
                exchange(address(tokenList[0].token), address(tokenList[1].token), _bal, true); // Receive USDT
            }
        }
        
        // Authorize Aave to pull funds from this contract
        // Approve the LendingPool contract allowance to *pull* the owed amount
        for(uint256 i = 0; i < assets.length; i++) {
            uint256 amountOwing = amounts[i].add(premiums[i]);
            IERC20(assets[i]).safeApprove(lendingPool, 0);
            IERC20(assets[i]).safeApprove(lendingPool, amountOwing);
        }
        
        return true;
    }
    
    function getFastGasPrice() internal view returns (uint256) {
        AggregatorV3Interface gasOracle = AggregatorV3Interface(GAS_ORACLE_ADDRESS);
        ( , int intGasPrice, , , ) = gasOracle.latestRoundData(); // We only want the answer 
        return uint256(intGasPrice);
    }
    
    function checkAndSwapTokens(address _executor) internal {
        lastTradeTime = now;

        // Now find our target token to sell into
        (uint256 targetID_1, uint256 targetID_2) = getCheaperToken(); // Normally both these values should point to the same ID
        
        uint256 targetID;
        if(targetID_1 == targetID_2){
            targetID = targetID_1;
        }else{
            // The fun part (flash laon)
            // Since prices are inverse between the exchanges
            // We will call Aave to borrow USDT, sell it for UST, sell UST on another exchange for more USDT than we original borrowed
            // First determine borrow size at the max slippage
            uint256 maxBorrow = uint256(1000000).mul(10**tokenList[1].decimals); // Predict based on $1million
            maxBorrow = estimateSellAtMaxSlippage(1, 0, maxBorrow);
            // Now estimate the return of the flash loan
            if(estimateFlashLoanResult(targetID_1, targetID_2, maxBorrow) > 2){
                // Flash loan will be profitable, borrow the funds
                performFlashLoan(targetID_1, targetID_2, maxBorrow); // This will return USDT earned and exchange it for WETH
            }
            targetID = targetID_2;
        }
        
        uint256 length = tokenList.length;

        // Now sell all the other tokens into this token
        uint256 _totalBalance = balance(); // Get the token balance at this contract, should increase
        bool _expectIncrease = false;
        for(uint256 i = 0; i < length; i++){
            if(i != targetID){
                uint256 sellBalance = 0;
                uint256 _minTradeTarget = minTradeSplit.mul(10**tokenList[i].decimals);
                uint256 _maxTradeTarget = maxAmountSell.mul(10**tokenList[i].decimals); // Determine the maximum amount of tokens to sell at once
                if(tokenList[i].token.balanceOf(address(this)) <= _minTradeTarget){
                    // If balance is too small,sell all tokens at once
                    sellBalance = tokenList[i].token.balanceOf(address(this));
                }else{
                    sellBalance = estimateSellAtMaxSlippage(i, targetID, tokenList[i].token.balanceOf(address(this))); // This will select a balance with a max slippage
                }
                if(sellBalance > _maxTradeTarget){
                    // If greater than the maximum trade allowed, match it
                    sellBalance = _maxTradeTarget;
                }
                uint256 minReceiveBalance = sellBalance.mul(10**tokenList[targetID].decimals).div(10**tokenList[i].decimals); // Change to match decimals of destination
                if(sellBalance > 0){
                    bool useUniswap = true;
                    uint256 estimate = simulateExchange(address(tokenList[i].token), address(tokenList[targetID].token), sellBalance, true);
                    uint256 estimate2 = simulateExchange(address(tokenList[i].token), address(tokenList[targetID].token), sellBalance, false);
                    if(estimate2 > estimate){
                        useUniswap = false;
                        estimate = estimate2;
                    }
                    if(estimate > minReceiveBalance){
                        _expectIncrease = true;
                        exchange(address(tokenList[i].token), address(tokenList[targetID].token), sellBalance, useUniswap);
                    }                        
                }
            }
        }
        uint256 _newBalance = balance();
        if(_expectIncrease == true){
            // There may be rare scenarios where we don't gain any by calling this function
            require(_newBalance > _totalBalance, "Failed to gain in balance from selling tokens");
        }
        uint256 gain = _newBalance.sub(_totalBalance);
        IERC20 weth = IERC20(WETH_ADDRESS);
        uint256 _wethBalance = weth.balanceOf(address(this));
        if(gain >= minGain || _wethBalance > 0){
            // Minimum gain required to buy WETH is about 0.01 tokens
            
            if(gain >= minGain){
            // Buy WETH from Uniswap with tokens
                uint256 sellBalance = gain.mul(10**tokenList[targetID].decimals).div(1e18); // Convert to target decimals
                uint256 holdBalance = sellBalance.mul(percentDepositor).div(DIVISION_FACTOR);
                sellBalance = sellBalance.sub(holdBalance); // We will buy WETH with this amount
                if(sellBalance <= tokenList[targetID].token.balanceOf(address(this))){
                    // Sell some of our gained token for WETH
                    exchange(address(tokenList[targetID].token), WETH_ADDRESS, sellBalance, true);
                    _wethBalance = weth.balanceOf(address(this));
                }
            }
            if(_wethBalance > 0){
                // Split the rest between the stakers and such
                // This is pure profit, figure out allocation
                // Split the amount sent to the treasury, stakers and executor if one exists
                if(_executor != address(0)){
                    // Executors will get a gas reimbursement in WETH and a percent of the remaining
                    uint256 maxGasFee = getFastGasPrice().mul(gasStipend); // This is gas stipend in wei
                    uint256 gasFee = tx.gasprice.mul(gasStipend); // This is gas fee requested
                    if(gasFee > maxGasFee){
                        gasFee = maxGasFee; // Gas fee cannot be greater than the maximum
                    }
                    uint256 executorAmount = gasFee;
                    if(gasFee >= _wethBalance.mul(maxPercentStipend).div(DIVISION_FACTOR)){
                        executorAmount = _wethBalance.mul(maxPercentStipend).div(DIVISION_FACTOR); // The executor will get the entire amount up to point
                    }else{
                        // Add the executor percent on top of gas fee
                        executorAmount = _wethBalance.sub(gasFee).mul(percentExecutor).div(DIVISION_FACTOR).add(gasFee);
                    }
                    if(executorAmount > 0){
                        weth.safeTransfer(_executor, executorAmount);
                        _wethBalance = weth.balanceOf(address(this)); // Recalculate WETH in contract           
                    }
                }
                if(_wethBalance > 0){
                    uint256 stakersAmount = _wethBalance.mul(percentStakers).div(DIVISION_FACTOR);
                    uint256 treasuryAmount = _wethBalance.sub(stakersAmount);
                    if(treasuryAmount > 0){
                        weth.safeTransfer(treasuryAddress, treasuryAmount);
                    }
                    if(stakersAmount > 0){
                        if(stakingAddress != address(0)){
                            weth.safeTransfer(stakingAddress, stakersAmount);
                            StabilizeStakingPool(stakingAddress).notifyRewardAmount(stakersAmount);                                
                        }else{
                            // No staking pool selected, just send to the treasury
                            weth.safeTransfer(treasuryAddress, stakersAmount);
                        }
                    }
                }                
            }
        }
    }
    
    function expectedProfit(bool inWETHForExecutor) external view returns (uint256) {
        // This view will return the amount of gain a forced swap will make on next call
        
        // Now find our target token to sell into
        (uint256 targetID_1, uint256 targetID_2) = getCheaperToken(); // Normally both these values should point to the same ID
        
        uint256 targetID;
        uint256 flashGain = 0;
        if(targetID_1 == targetID_2){
            targetID = targetID_1;
        }else{
            // The fun part (flash loan)
            // Since prices are inverse between the exchanges
            // We will call Aave to borrow USDT, sell it for UST, sell UST on another exchange for more USDT than we original borrowed
            // First determine borrow size at the max slippage
            uint256 maxBorrow = uint256(1000000).mul(10**tokenList[1].decimals); // Predict based on $1million
            maxBorrow = estimateSellAtMaxSlippage(1, 0, maxBorrow);
            // Now estimate the return of the flash loan
            flashGain = estimateFlashLoanResult(targetID_1, targetID_2, maxBorrow);
            if(flashGain > 0){
                if(inWETHForExecutor == true){
                    flashGain = simulateExchange(address(tokenList[1].token), WETH_ADDRESS, flashGain.mul(uint256(100000).sub(percentDepositor)).div(DIVISION_FACTOR), true);
                }else{
                    // Normalize it
                    flashGain = flashGain.mul(1e18).div(10**tokenList[1].decimals);
                }                
            }
            targetID = targetID_2;
        }
        uint256 length = tokenList.length;

        // Now simulate sell all the other tokens into this token
        uint256 _normalizedGain = 0;
        for(uint256 i = 0; i < length; i++){
            if(i != targetID){
                uint256 sellBalance = 0;
                uint256 _minTradeTarget = minTradeSplit.mul(10**tokenList[i].decimals);
                uint256 _maxTradeTarget = maxAmountSell.mul(10**tokenList[i].decimals); // Determine the maximum amount of tokens to sell at once
                if(tokenList[i].token.balanceOf(address(this)) <= _minTradeTarget){
                    // If balance is too small,sell all tokens at once
                    sellBalance = tokenList[i].token.balanceOf(address(this));
                }else{
                    sellBalance = estimateSellAtMaxSlippage(i, targetID, tokenList[i].token.balanceOf(address(this))); // This will select a balance with the max slippage
                }
                if(sellBalance > _maxTradeTarget){
                    // If greater than the maximum trade allowed, match it
                    sellBalance = _maxTradeTarget;
                }
                uint256 minReceiveBalance = sellBalance.mul(10**tokenList[targetID].decimals).div(10**tokenList[i].decimals); // Change to match decimals of destination
                if(sellBalance > 0){
                    uint256 estimate = simulateExchange(address(tokenList[i].token), address(tokenList[targetID].token), sellBalance, true);
                    uint256 estimate2 = simulateExchange(address(tokenList[i].token), address(tokenList[targetID].token), sellBalance, false);
                    if(estimate2 > estimate){
                        estimate = estimate2;
                    }
                    if(estimate > minReceiveBalance){
                        uint256 _gain = estimate.sub(minReceiveBalance).mul(1e18).div(10**tokenList[targetID].decimals); // Normalized gain
                        _normalizedGain = _normalizedGain.add(_gain);
                    }                        
                }
            }
        }
        if(inWETHForExecutor == false){
            return _normalizedGain.add(flashGain);
        }else{
            if(_normalizedGain.add(flashGain) == 0){
                return 0;
            }
            // Calculate how much WETH the executor would make as profit
            uint256 estimate = flashGain; // WETH earned from flashloan
            if(_normalizedGain > 0){
                uint256 sellBalance = _normalizedGain.mul(10**tokenList[targetID].decimals).div(1e18); // Convert to target decimals
                uint256 holdBalance = sellBalance.mul(percentDepositor).div(DIVISION_FACTOR);
                sellBalance = sellBalance.sub(holdBalance); // We will buy WETH with this amount
                // Estimate output
                estimate = estimate.add(simulateExchange(address(tokenList[targetID].token), WETH_ADDRESS, sellBalance, true));           
            }
            // Now calculate the amount going to the executor
            uint256 gasFee = getFastGasPrice().mul(gasStipend); // This is gas stipend in wei
            if(gasFee >= estimate.mul(maxPercentStipend).div(DIVISION_FACTOR)){ // Max percent of total
                return estimate.mul(maxPercentStipend).div(DIVISION_FACTOR); // The executor will get max percent of total
            }else{
                estimate = estimate.sub(gasFee); // Subtract fee from remaining balance
                return estimate.mul(percentExecutor).div(DIVISION_FACTOR).add(gasFee); // Executor amount with fee added
            }
        }  
    }
    
    function withdraw(address _depositor, uint256 _share, uint256 _total, bool nonContract) public onlyZSToken returns (uint256) {
        require(balance() > 0, "There are no tokens in this strategy");
        if(nonContract == true){
            if( _share > _total.mul(percentTradeTrigger).div(DIVISION_FACTOR)){
                checkAndSwapTokens(_depositor);
            }
        }
        
        uint256 withdrawAmount = 0;
        uint256 _balance = balance();
        if(_share < _total){
            uint256 _myBalance = _balance.mul(_share).div(_total);
            withdrawPerOrder(_depositor, _myBalance, false); // This will withdraw based on token price
            withdrawAmount = _myBalance;
        }else{
            // We are all shares, transfer all
            withdrawPerOrder(_depositor, _balance, true);
            withdrawAmount = _balance;
        }       
        lastActionBalance = balance();
        
        return withdrawAmount;
    }
    
    // This will withdraw the tokens from the contract based on their order
    function withdrawPerOrder(address _receiver, uint256 _withdrawAmount, bool _takeAll) internal {
        uint256 length = tokenList.length;
        if(_takeAll == true){
            // Send the entire balance
            for(uint256 i = 0; i < length; i++){
                uint256 _bal = tokenList[i].token.balanceOf(address(this));
                if(_bal > 0){
                    tokenList[i].token.safeTransfer(_receiver, _bal);
                }
            }
            return;
        }
        
        for(uint256 i = 0; i < length; i++){
            // Determine the balance left
            uint256 _normalizedBalance = tokenList[i].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[i].decimals);
            if(_normalizedBalance <= _withdrawAmount){
                // Withdraw the entire balance of this token
                if(_normalizedBalance > 0){
                    _withdrawAmount = _withdrawAmount.sub(_normalizedBalance);
                    tokenList[i].token.safeTransfer(_receiver, tokenList[i].token.balanceOf(address(this)));                    
                }
            }else{
                // Withdraw a partial amount of this token
                if(_withdrawAmount > 0){
                    // Convert the withdraw amount to the token's decimal amount
                    uint256 _balance = _withdrawAmount.mul(10**tokenList[i].decimals).div(1e18);
                    _withdrawAmount = 0;
                    tokenList[i].token.safeTransfer(_receiver, _balance);
                }
                break; // Nothing more to withdraw
            }
        }
    }
    
    function executorSwapTokens(address _executor, uint256 _minSecSinceLastTrade) external {
        // Function designed to promote trading with incentive. Users get 20% of WETH from profitable trades
        require(now.sub(lastTradeTime) > _minSecSinceLastTrade, "The last trade was too recent");
        require(_msgSender() == tx.origin, "Contracts cannot interact with this function");
        checkAndSwapTokens(_executor);
        lastActionBalance = balance();
    }
    
    // Governance functions
    function governanceSwapTokens() external onlyGovernance {
        // This is function that force trade tokens at anytime. It can only be called by governance
        checkAndSwapTokens(governance());
        lastActionBalance = balance();
    }
    
    // Timelock variables
    
    uint256 private _timelockStart; // The start of the timelock to change governance variables
    uint256 private _timelockType; // The function that needs to be changed
    uint256 constant TIMELOCK_DURATION = 86400; // Timelock is 24 hours
    
    // Reusable timelock variables
    address private _timelock_address;
    uint256[7] private _timelock_data;
    
    modifier timelockConditionsMet(uint256 _type) {
        require(_timelockType == _type, "Timelock not acquired for this function");
        _timelockType = 0; // Reset the type once the timelock is used
        if(balance() > 0){ // Timelock only applies when balance exists
            require(now >= _timelockStart + TIMELOCK_DURATION, "Timelock time not met");
        }
        _;
    }
    
    // Change the owner of the token contract
    // --------------------
    function startGovernanceChange(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 1;
        _timelock_address = _address;       
    }
    
    function finishGovernanceChange() external onlyGovernance timelockConditionsMet(1) {
        transferGovernance(_timelock_address);
    }
    // --------------------
    
    // Change the treasury address
    // --------------------
    function startChangeTreasury(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 2;
        _timelock_address = _address;
    }
    
    function finishChangeTreasury() external onlyGovernance timelockConditionsMet(2) {
        treasuryAddress = _timelock_address;
    }
    // --------------------
    
    // Change the staking address
    // --------------------
    function startChangeStakingPool(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 3;
        _timelock_address = _address;
    }
    
    function finishChangeStakingPool() external onlyGovernance timelockConditionsMet(3) {
        stakingAddress = _timelock_address;
    }
    // --------------------
    
    // Change the zsToken address
    // --------------------
    function startChangeZSToken(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 4;
        _timelock_address = _address;
    }
    
    function finishChangeZSToken() external onlyGovernance timelockConditionsMet(4) {
        zsTokenAddress = _timelock_address;
    }
    // --------------------
   
    // Change the trading conditions used by the strategy
    // --------------------
    
    function startChangeTradingConditions(uint256 _pTradeTrigger, uint256 _pSellPercent, uint256 _mSellAmount, uint256 _minSplit, uint256 _maxStipend, uint256 _pMaxStipend, uint256 _pSlip) external onlyGovernance {
        // Changes a lot of trading parameters in one call
        require(_pTradeTrigger <= 100000 && _pSellPercent <= 100000 && _pMaxStipend <= 100000 && _pSlip <= 100000,"Percent cannot be greater than 100%");
        _timelockStart = now;
        _timelockType = 5;
        _timelock_data[0] = _pTradeTrigger;
        _timelock_data[1] = _pSellPercent;
        _timelock_data[2] = _minSplit;
        _timelock_data[3] = _maxStipend;
        _timelock_data[4] = _pMaxStipend;
        _timelock_data[5] = _pSlip;
        _timelock_data[6] = _mSellAmount;
    }
    
    function finishChangeTradingConditions() external onlyGovernance timelockConditionsMet(5) {
        percentTradeTrigger = _timelock_data[0];
        maxPercentSell = _timelock_data[1];
        minTradeSplit = _timelock_data[2];
        gasStipend = _timelock_data[3];
        maxPercentStipend = _timelock_data[4];
        maxSlippage = _timelock_data[5];
        maxAmountSell = _timelock_data[6];
    }
    // --------------------
    
    // Change the strategy allocations between the parties
    // --------------------
    
    function startChangeStrategyAllocations(uint256 _pDepositors, uint256 _pExecutor, uint256 _pStakers, uint256 _maxPool) external onlyGovernance {
        // Changes strategy allocations in one call
        require(_pDepositors <= 100000 && _pExecutor <= 100000 && _pStakers <= 100000,"Percent cannot be greater than 100%");
        _timelockStart = now;
        _timelockType = 6;
        _timelock_data[0] = _pDepositors;
        _timelock_data[1] = _pExecutor;
        _timelock_data[2] = _pStakers;
        _timelock_data[3] = _maxPool;
    }
    
    function finishChangeStrategyAllocations() external onlyGovernance timelockConditionsMet(6) {
        percentDepositor = _timelock_data[0];
        percentExecutor = _timelock_data[1];
        percentStakers = _timelock_data[2];
        maxPoolSize = _timelock_data[3];
    }
    // --------------------
}
function startGovernanceChange(address _address) external onlyGovernance {
    _timelockStart = now;
    _timelockType = 1;
    _timelock_address = _address;       
}

// Change the owner of the token contract
// --------------------